Search in sources :

Example 1 with ReilTranslator

use of com.google.security.zynamics.reil.translators.ReilTranslator in project binnavi by google.

the class PostgreSQLProviderTest method testSave.

@Test
public void testSave() throws CouldntSaveDataException, CouldntLoadDataException, CouldntDeleteException, CPartialLoadException, InternalTranslationException, LoadCancelledException, MaybeNullException {
    final CTagManager tagManager = getProvider().loadTagManager(TagType.NODE_TAG);
    tagManager.addTag(tagManager.getRootTag(), "Node Tag I");
    tagManager.addTag(tagManager.getRootTag(), "Node Tag II");
    final ITreeNode<CTag> tag1 = tagManager.getRootTag().getChildren().get(0);
    final ITreeNode<CTag> tag2 = tagManager.getRootTag().getChildren().get(1);
    final INaviModule module = getProvider().loadModules().get(0);
    module.load();
    final CView view = module.getContent().getViewContainer().createView("Save View", "Save View Description");
    final INaviFunction function = module.getContent().getFunctionContainer().getFunction("sub_1002B87");
    function.load();
    final List<COperandTree> operands = new ArrayList<COperandTree>();
    final COperandTreeNode root1 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
    final COperandTreeNode child1 = module.createOperandExpression("eax", ExpressionType.REGISTER);
    COperandTreeNode.link(root1, child1);
    final COperandTreeNode root2 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
    final COperandTreeNode child2 = module.createOperandExpression("16", ExpressionType.IMMEDIATE_INTEGER);
    COperandTreeNode.link(root2, child2);
    final COperandTree operand1 = module.createOperand(root1);
    final COperandTree operand2 = module.createOperand(root2);
    operands.add(operand1);
    operands.add(operand2);
    final Iterable<INaviInstruction> instructions = function.getBasicBlocks().get(0).getInstructions();
    final Iterable<INaviInstruction> instructions2 = function.getBasicBlocks().get(1).getInstructions();
    final CCodeNode codeNode = view.getContent().createCodeNode(function, Lists.newArrayList(instructions));
    codeNode.tagNode(tag1.getObject());
    codeNode.getComments().appendLocalCodeNodeComment("XXX");
    codeNode.getComments().appendLocalInstructionComment(Iterables.getLast(codeNode.getInstructions()), "YYY");
    Iterables.getLast(codeNode.getInstructions()).appendGlobalComment(" GLOBAL INSTRUCTION COMMENT ");
    @SuppressWarnings("unused") final CCodeNode codeNode2 = view.getContent().createCodeNode(null, Lists.newArrayList(instructions2));
    final CFunctionNode functionNode = view.getContent().createFunctionNode(function);
    functionNode.tagNode(tag2.getObject());
    functionNode.appendLocalFunctionComment("ZZZ");
    @SuppressWarnings("unused") final CNaviViewEdge edge = view.getContent().createEdge(codeNode, functionNode, EdgeType.JUMP_UNCONDITIONAL);
    view.save();
    view.close();
    view.load();
    assertEquals(3, view.getGraph().getNodes().size());
    assertEquals(1, view.getGraph().getEdges().size());
    assertTrue(view.getGraph().getNodes().get(0).isTagged(tag1.getObject()));
    assertTrue(view.getGraph().getNodes().get(2).isTagged(tag2.getObject()));
    final CCodeNode loadedCodeNode = (CCodeNode) view.getGraph().getNodes().get(0);
    final CCodeNode loadedCodeNode2 = (CCodeNode) view.getGraph().getNodes().get(1);
    assertEquals("XXX", loadedCodeNode.getComments().getLocalCodeNodeComment().get(0).getComment());
    final INaviInstruction customInstruction = Iterables.getLast(loadedCodeNode.getInstructions());
    assertEquals(" GLOBAL INSTRUCTION COMMENT ", customInstruction.getGlobalComment().get(0).getComment());
    assertEquals("YYY", loadedCodeNode.getComments().getLocalInstructionComment(customInstruction).get(0).getComment());
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    translator.translate(new StandardEnvironment(), loadedCodeNode);
    translator.translate(new StandardEnvironment(), loadedCodeNode2);
    final CFunctionNode loadedFunctionNode = (CFunctionNode) view.getGraph().getNodes().get(2);
    assertEquals("ZZZ", loadedFunctionNode.getLocalFunctionComment().get(0).getComment());
    tagManager.deleteTag(tag1);
    tagManager.deleteTag(tag2);
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 2 with ReilTranslator

use of com.google.security.zynamics.reil.translators.ReilTranslator in project binnavi by google.

the class CReilInstructionDialog method show.

/**
   * Shows an instruction dialog.
   *
   * @param parent Parent window used for dialogs.
   * @param instruction The instruction whose REIL code is shown.
   *
   * @throws InternalTranslationException Thrown if the instruction could not be converted to REIL
   *         code.
   */
public static void show(final Window parent, final INaviInstruction instruction) throws InternalTranslationException {
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    final ReilGraph reilGraph = translator.translate(new StandardEnvironment(), instruction);
    final String text = reilGraphToText(reilGraph);
    final String title = String.format("REIL code of '%s'", instruction.toString());
    final CReilInstructionDialog dialog = new CReilInstructionDialog(parent, title, text);
    GuiHelper.centerChildToParent(parent, dialog, true);
    dialog.setVisible(true);
}
Also used : ReilGraph(com.google.security.zynamics.reil.ReilGraph) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment)

Example 3 with ReilTranslator

use of com.google.security.zynamics.reil.translators.ReilTranslator in project binnavi by google.

the class COperandsDeterminer method getRegisters.

/**
   * Returns the registers read and written by a native instruction.
   * 
   * @param instruction The instruction whose accessed registers are returned.
   * 
   * @return The read and written registers of the instruction.
   * 
   * @throws InternalTranslationException Thrown if the instruction could not be translated to REIL.
   */
public static Pair<Set<String>, Set<String>> getRegisters(final INaviInstruction instruction) throws InternalTranslationException {
    final Set<String> inSet = new HashSet<String>();
    final Set<String> outSet = new HashSet<String>();
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    final DirectedGraph<ReilBlock, ReilEdge> reilCode = translator.translate(new StandardEnvironment(), instruction);
    final boolean translatingReil = instruction.getArchitecture().equals("REIL");
    for (final ReilBlock reilBlock : reilCode) {
        for (final ReilInstruction reilInstruction : reilBlock) {
            if (writesThirdOperand(reilInstruction, translatingReil)) {
                outSet.add(reilInstruction.getThirdOperand().getValue());
            }
            if (!writesThirdOperand(reilInstruction, translatingReil) && isRegister(reilInstruction.getThirdOperand(), translatingReil)) {
                // JCC + STM
                inSet.add(reilInstruction.getThirdOperand().getValue());
            }
            if (isRegister(reilInstruction.getFirstOperand(), translatingReil)) {
                inSet.add(reilInstruction.getFirstOperand().getValue());
            }
            if (isRegister(reilInstruction.getSecondOperand(), translatingReil)) {
                inSet.add(reilInstruction.getSecondOperand().getValue());
            }
        }
    }
    return new Pair<Set<String>, Set<String>>(inSet, outSet);
}
Also used : ReilInstruction(com.google.security.zynamics.reil.ReilInstruction) ReilEdge(com.google.security.zynamics.reil.ReilEdge) ReilBlock(com.google.security.zynamics.reil.ReilBlock) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) HashSet(java.util.HashSet) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 4 with ReilTranslator

use of com.google.security.zynamics.reil.translators.ReilTranslator in project binnavi by google.

the class SimpleTest method simpleTracking.

@Test
public void simpleTracking() throws CouldntLoadDataException, InternalTranslationException, CPartialLoadException, LoadCancelledException {
    final INaviModule module = m_database.getContent().getModules().get(0);
    module.load();
    final INaviView view = module.getViewsWithAddresses(Lists.newArrayList(new UnrelocatedAddress(new CAddress(0x10044BB))), true).get(0);
    assertEquals(0x10044BB, module.getContent().getViewContainer().getFunction(view).getAddress().toLong());
    view.load();
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    final ReilFunction reilFunction = translator.translate(new StandardEnvironment(), view);
    assertEquals(0, reilFunction.getGraph().getEdges().size());
    final IStateVector<InstructionGraphNode, ValueTrackerElement> result = ValueTracker.track(reilFunction);
    System.out.println(result);
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) UnrelocatedAddress(com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress) ReilFunction(com.google.security.zynamics.reil.ReilFunction) ValueTrackerElement(com.google.security.zynamics.reil.algorithms.mono.valuetracking.ValueTrackerElement) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) InstructionGraphNode(com.google.security.zynamics.reil.algorithms.mono.InstructionGraphNode) Test(org.junit.Test)

Example 5 with ReilTranslator

use of com.google.security.zynamics.reil.translators.ReilTranslator in project binnavi by google.

the class CCodeNodeParserTest method testAddZero.

@Test
public void testAddZero() throws ParserException, CPartialLoadException, IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException {
    // 00000000: mov eax, [esp+0]
    final MockCodeNodeProvider cnProvider = new MockCodeNodeProvider();
    final MockCodeNodeData instruction2 = new MockCodeNodeData();
    instruction2.nodeId = 5193;
    instruction2.address = new CAddress(0x4180dd);
    instruction2.mnemonic = "mov";
    instruction2.operandPosition = 0;
    instruction2.expressionId = 1;
    instruction2.expressionType = 6;
    instruction2.symbol = "b4";
    instruction2.immediate = null;
    instruction2.parentId = 0;
    instruction2.replacement = null;
    final MockCodeNodeData instruction3 = new MockCodeNodeData();
    instruction3.nodeId = 5193;
    instruction3.address = new CAddress(0x4180dd);
    instruction3.mnemonic = "mov";
    instruction3.operandPosition = 0;
    instruction3.expressionId = 29;
    instruction3.expressionType = 4;
    instruction3.symbol = "ss:";
    instruction3.immediate = null;
    instruction3.parentId = 1;
    instruction3.replacement = null;
    final MockCodeNodeData instruction4 = new MockCodeNodeData();
    instruction4.nodeId = 5193;
    instruction4.address = new CAddress(0x4180dd);
    instruction4.mnemonic = "mov";
    instruction4.operandPosition = 0;
    instruction4.expressionId = 30;
    instruction4.expressionType = 7;
    instruction4.symbol = "[";
    instruction4.immediate = null;
    instruction4.parentId = 29;
    instruction4.replacement = null;
    final MockCodeNodeData instruction5 = new MockCodeNodeData();
    instruction5.nodeId = 5193;
    instruction5.address = new CAddress(0x4180dd);
    instruction5.mnemonic = "mov";
    instruction5.operandPosition = 0;
    instruction5.expressionId = 31;
    instruction5.expressionType = 4;
    instruction5.symbol = "+";
    instruction5.immediate = null;
    instruction5.parentId = 30;
    instruction5.replacement = null;
    final MockCodeNodeData instruction6 = new MockCodeNodeData();
    instruction6.nodeId = 5193;
    instruction6.address = new CAddress(0x4180dd);
    instruction6.mnemonic = "mov";
    instruction6.operandPosition = 0;
    instruction6.expressionId = 32;
    instruction6.expressionType = 5;
    instruction6.symbol = "esp";
    instruction6.immediate = null;
    instruction6.parentId = 31;
    instruction6.replacement = null;
    final MockCodeNodeData instruction7 = new MockCodeNodeData();
    instruction7.nodeId = 5193;
    instruction7.address = new CAddress(0x4180dd);
    instruction7.mnemonic = "mov";
    instruction7.operandPosition = 0;
    instruction7.expressionId = 498;
    instruction7.expressionType = 2;
    instruction7.symbol = null;
    instruction7.immediate = "0";
    instruction7.parentId = 31;
    instruction7.replacement = "134h+var_134";
    final MockCodeNodeData instruction1 = new MockCodeNodeData();
    instruction1.nodeId = 5193;
    instruction1.address = new CAddress(0x4180dd);
    instruction1.mnemonic = "mov";
    instruction1.operandPosition = 1;
    instruction1.expressionId = 1594;
    instruction1.expressionType = 2;
    instruction1.symbol = null;
    instruction1.immediate = "4561216";
    instruction1.parentId = 0;
    instruction1.replacement = null;
    cnProvider.data.add(instruction2);
    cnProvider.data.add(instruction3);
    cnProvider.data.add(instruction4);
    cnProvider.data.add(instruction5);
    cnProvider.data.add(instruction6);
    cnProvider.data.add(instruction7);
    cnProvider.data.add(instruction1);
    final MockSqlProvider provider = new MockSqlProvider();
    final MockModule module = new MockModule();
    CFunctionContainerHelper.addFunction(module.getContent().getFunctionContainer(), new MockFunction(0));
    final CCodeNodeParser p = new CCodeNodeParser(cnProvider, Lists.newArrayList(module), provider);
    final List<CCodeNode> result = p.parse();
    assertEquals(1, result.size());
    assertEquals(1, Iterables.size(result.get(0).getInstructions()));
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    try {
        translator.translate(new StandardEnvironment(), Iterables.get(result.get(0).getInstructions(), 0));
    } catch (final InternalTranslationException exception) {
        CUtilityFunctions.logException(exception);
    }
}
Also used : MockFunction(com.google.security.zynamics.binnavi.disassembly.MockFunction) MockCodeNodeData(com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeData) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) MockModule(com.google.security.zynamics.binnavi.disassembly.Modules.MockModule) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) MockCodeNodeProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeProvider) CCodeNodeParser(com.google.security.zynamics.binnavi.Database.NodeParser.CCodeNodeParser) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) Test(org.junit.Test)

Aggregations

ReilTranslator (com.google.security.zynamics.reil.translators.ReilTranslator)6 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)5 StandardEnvironment (com.google.security.zynamics.reil.translators.StandardEnvironment)5 Test (org.junit.Test)3 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)2 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)2 ReilFunction (com.google.security.zynamics.reil.ReilFunction)2 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)2 MockCodeNodeData (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeData)1 MockCodeNodeProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeProvider)1 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)1 CCodeNodeParser (com.google.security.zynamics.binnavi.Database.NodeParser.CCodeNodeParser)1 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)1 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)1 CFunctionNode (com.google.security.zynamics.binnavi.disassembly.CFunctionNode)1 CNaviViewEdge (com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)1 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)1 COperandTreeNode (com.google.security.zynamics.binnavi.disassembly.COperandTreeNode)1 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)1 MockFunction (com.google.security.zynamics.binnavi.disassembly.MockFunction)1