def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if prefix == "# ": if self.ctx.comments: print_comment_no_end(prefix, tab) print_no_end(color_addr(i.address)) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() modified = self.__print_inst(i, tab, prefix) if i.address in INTERN_COMMENTS: print_no_end(color_intern_comment(" ; ")) print_no_end(color_intern_comment(INTERN_COMMENTS[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if prefix == "# ": if self.ctx.comments: print_comment_no_end(prefix, tab) print_no_end(color_addr(i.address)) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if i.address != self.ctx.addr and \ i.address in self.ctx.dis.binary.reverse_symbols: print_tabbed_no_end("", tab) self.print_symbol(i.address) print() modified = self.__print_inst(i, tab, prefix) if i.address in INTERN_COMMENTS: print_no_end(color_intern_comment(" ; ")) print_no_end(color_intern_comment(INTERN_COMMENTS[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print(self, o, tab=0): o.print_commented_jump(self.orig_jump, self.fused_inst, tab) if self.prefetch is not None: o.print_inst(self.prefetch, tab) print_tabbed_no_end(color_keyword("and ") + color_keyword("if "), tab) o.print_if_cond(self.cond_id, self.fused_inst) print()
def print(self, o, tab=0): o.print_commented_jump(self.orig_jump, self.fused_inst, tab) if self.prefetch is not None: o.print_inst(self.prefetch, tab) print_tabbed_no_end(color_keyword("if "), tab) o.print_if_cond(self.cond_id, self.fused_inst) print_no_end(color_keyword(" goto ")) print(color_addr(self.addr_jump, False))
def print_rep_begin(): nonlocal tab if i.prefix[0] in REP_PREFIX: print_tabbed_no_end(color_keyword("while"), tab) # TODO: for 16 and 32 bits print_no_end(" (!rcx)") print(") {") tab += 1
def print_rep_end(): nonlocal tab if i.prefix[0] in REP_PREFIX: print() print_label_or_addr(i.address, tab) print("rcx--") if i.prefix[0] == X86_PREFIX_REPNE: print_tabbed_no_end(color_keyword("if"), tab) print_no_end(" (!Z) ") print(color_keyword("break")) tab -= 1 print_tabbed_no_end("}", tab)
def print(self, o, tab=0): o.print_commented_jump(None, self.fused_inst, tab) print_tabbed_no_end(color_keyword("if "), tab) o.print_if_cond(self.cond_id, self.fused_inst) # If it contains only one instruction if self.fused_inst == None and len(self.br.nodes) == 1 and \ len(self.br.nodes[0]) == 1 and isinstance(self.br.nodes[0], list): print_no_end(" : ") o.print_inst(self.br.nodes[0][0], 0) else: print(" {") self.br.print(o, tab + 1) print_tabbed("}", tab)
def print(self, o, tab=0): o.print_commented_jump(None, self.fused_inst, tab) print_tabbed_no_end(color_keyword("if "), tab) o.print_if_cond(self.cond_id, self.fused_inst) # If it contains only one instruction if self.fused_inst == None and len(self.br.nodes) == 1 and \ len(self.br.nodes[0]) == 1 and isinstance(self.br.nodes[0], list): print_no_end(" : ") o.print_inst(self.br.nodes[0][0], 0) else: print(" {") self.br.print(o, tab+1) print_tabbed("}", tab)
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if i.address in self.ctx.dis.previous_comments: for comm in self.ctx.dis.previous_comments[i.address]: print_tabbed(color_intern_comment("; %s" % comm), tab) if prefix == "# ": if self.ctx.comments: if i.address in self.ctx.labels: print_label(i.address, tab) print() print_comment_no_end(prefix + hex(i.address) + ": ", tab) else: print_comment_no_end(prefix, tab) print_addr(i.address) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() modified = self.__print_inst(i, tab, prefix) if i.address in self.ctx.dis.inline_comments: print_no_end(color_intern_comment(" ; ")) print_no_end( color_intern_comment(self.ctx.dis.inline_comments[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if i.address in self.ctx.dis.previous_comments: for comm in self.ctx.dis.previous_comments[i.address]: print_tabbed(color_intern_comment("; %s" % comm), tab) if prefix == "# ": if self.ctx.comments: if i.address in self.ctx.labels: print_label(i.address, tab) print() print_comment_no_end(prefix + hex(i.address) + ": ", tab) else: print_comment_no_end(prefix, tab) print_addr(i.address) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() modified = self.__print_inst(i, tab, prefix) if i.address in self.ctx.dis.inline_comments: print_no_end(color_intern_comment(" ; ")) print_no_end(color_intern_comment(self.ctx.dis.inline_comments[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print(self, o, tab=0, print_else_keyword=False): ARCH_UTILS = o.ctx.libarch.utils # # if cond { # } else { # ... # } # # become # # if !cond { # ... # } # br_next = self.br_next br_next_jump = self.br_next_jump inv_if = False if len(self.br_next.nodes) == 0: br_next, br_next_jump = br_next_jump, br_next inv_if = True o.print_commented_jump(self.jump_inst, self.fused_inst, tab) if self.prefetch is not None: o.print_inst(self.prefetch, tab) if print_else_keyword: print_tabbed_no_end(color_keyword("else if "), tab) else: print_tabbed_no_end(color_keyword("if "), tab) # jump_inst is the condition to go to the else-part if inv_if: o.print_if_cond(ARCH_UTILS.get_cond(self.jump_inst), self.fused_inst) else: o.print_if_cond(ARCH_UTILS.invert_cond(self.jump_inst), self.fused_inst) print(" {") # if-part br_next.print(o, tab+1) # else-part if len(br_next_jump.nodes) > 0: print_tabbed_no_end("} ", tab) # # if { # ... # } else { # if { # ... # } # } # # become : # # if { # ... # } # else if { # ... # } # br = br_next_jump if len(br.nodes) == 1 and isinstance(br.nodes[0], Ast_Ifelse): print() br.nodes[0].print(o, tab, True) return if len(br.nodes) == 2 and isinstance(br.nodes[0], list) and \ len(br.nodes[0]) == 1 and ARCH_UTILS.is_cmp(br.nodes[0][0]) and \ isinstance(br.nodes[1], Ast_Ifelse): print() br.nodes[1].print(o, tab, True) return print(color_keyword("else ") + "{") br.print(o, tab+1) print_tabbed("}", tab)
def __print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) print_tabbed_no_end(color_addr(i.address), tab) if is_ret(i): print_no_end(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) return # Here we can have conditional jump with the option --dump if is_jump(i): if i.operands[0].type != X86_OP_IMM: print_no_end(i.mnemonic + " ") self.print_operand(i, 0) if is_uncond_jump(i) and self.ctx.comments: print_comment_no_end(" # STOPPED") return try: addr = i.operands[0].value.imm print_no_end(i.mnemonic + " " + color(hex(addr), self.ctx.addr_color[addr])) except KeyError: print_no_end(i.mnemonic + " " + hex(addr)) return modified = False if i.id in INST_CHECK: if (i.id == X86_INS_OR and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == -1): self.print_operand(i, 0) print_no_end(" = -1") elif (i.id == X86_INS_AND and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == 0): self.print_operand(i, 0) print_no_end(" = 0") elif (all(op.type == X86_OP_REG for op in i.operands) and len(set(op.value.reg for op in i.operands)) == 1 and i.id == X86_INS_XOR): self.print_operand(i, 0) print_no_end(" = 0") elif i.id == X86_INS_INC or i.id == X86_INS_DEC: self.print_operand(i, 0) print_no_end(inst_symbol(i)) elif i.id == X86_INS_LEA: self.print_operand(i, 0) print_no_end(" = &(") self.print_operand(i, 1) print_no_end(")") elif i.id == X86_INS_IMUL: if len(i.operands) == 3: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i).rstrip('=') + " ") self.print_operand(i, 2) elif len(i.operands) == 2: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) elif len(i.operands) == 1: sz = i.operands[0].size if sz == 1: print_no_end("ax = al * ") elif sz == 2: print_no_end("dx:ax = ax * ") elif sz == 4: print_no_end("edx:eax = eax * ") elif sz == 8: print_no_end("rdx:rax = rax * ") self.print_operand(i, 0) else: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) modified = True elif i.id == X86_INS_CDQE: print_no_end("rax = eax") modified = True elif i.id == X86_INS_IDIV: print_no_end('eax = edx:eax / ') self.print_operand(i, 0) print_no_end('; edx = edx:eax % ') self.print_operand(i, 0) modified = True elif i.id == X86_INS_MUL: lut = {1: ("al", "ax"), 2: ("ax", "dx:ax"), 4: ("eax", "edx:eax"), 8: ("rax", "rdx:rax")} src, dst = lut[i.operands[0].size] print_no_end('{0} = {1} * '.format(dst, src)) self.print_operand(i, 0) modified = True elif i.id == X86_INS_NOT: self.print_operand(i, 0) print_no_end(' ^= -1') modified = True elif i.id == X86_INS_SCASB and i.prefix[0] == X86_PREFIX_REPNE: print_no_end('while (') self.print_operand(i, 1) print_no_end(' != ') self.print_operand(i, 0) print_no_end(') { ') self.print_operand(i, 1, show_deref=False) print_no_end('++; cx--; } ') self.print_operand(i, 1, show_deref=False) print_no_end('++; cx--;') modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 return modified
def __print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) print_tabbed_no_end(color_addr(i.address), tab) self.print_bytes(i) if is_ret(i): print_no_end(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) return # Here we can have conditional jump with the option --dump if is_jump(i): if i.operands[0].type != X86_OP_IMM: print_no_end(i.mnemonic + " ") self.print_operand(i, 0) if is_uncond_jump( i) and self.ctx.comments and not self.ctx.dump: print_comment_no_end(" # STOPPED") return try: addr = i.operands[0].value.imm print_no_end(i.mnemonic + " " + color(hex(addr), self.ctx.addr_color[addr])) except KeyError: print_no_end(i.mnemonic + " " + hex(addr)) return modified = False if i.id in INST_CHECK: if (i.id == X86_INS_OR and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == -1): self.print_operand(i, 0) print_no_end(" = -1") elif (i.id == X86_INS_AND and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == 0): self.print_operand(i, 0) print_no_end(" = 0") elif (all(op.type == X86_OP_REG for op in i.operands) and len(set(op.value.reg for op in i.operands)) == 1 and i.id == X86_INS_XOR): self.print_operand(i, 0) print_no_end(" = 0") elif i.id == X86_INS_INC or i.id == X86_INS_DEC: self.print_operand(i, 0) print_no_end(inst_symbol(i)) elif i.id == X86_INS_LEA: self.print_operand(i, 0) print_no_end(" = &(") self.print_operand(i, 1) print_no_end(")") elif i.id == X86_INS_IMUL: if len(i.operands) == 3: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i).rstrip('=') + " ") self.print_operand(i, 2) elif len(i.operands) == 2: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) elif len(i.operands) == 1: sz = i.operands[0].size if sz == 1: print_no_end("ax = al * ") elif sz == 2: print_no_end("dx:ax = ax * ") elif sz == 4: print_no_end("edx:eax = eax * ") elif sz == 8: print_no_end("rdx:rax = rax * ") self.print_operand(i, 0) else: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) modified = True elif i.id == X86_INS_CDQE: print_no_end("rax = eax") modified = True elif i.id == X86_INS_IDIV: print_no_end('eax = edx:eax / ') self.print_operand(i, 0) print_no_end('; edx = edx:eax % ') self.print_operand(i, 0) modified = True elif i.id == X86_INS_MUL: lut = { 1: ("al", "ax"), 2: ("ax", "dx:ax"), 4: ("eax", "edx:eax"), 8: ("rax", "rdx:rax") } src, dst = lut[i.operands[0].size] print_no_end('{0} = {1} * '.format(dst, src)) self.print_operand(i, 0) modified = True elif i.id == X86_INS_NOT: self.print_operand(i, 0) print_no_end(' ^= -1') modified = True elif i.id == X86_INS_SCASB and i.prefix[0] == X86_PREFIX_REPNE: print_no_end('while (') self.print_operand(i, 1) print_no_end(' != ') self.print_operand(i, 0) print_no_end(') { ') self.print_operand(i, 1, show_deref=False) print_no_end('++; cx--; } ') self.print_operand(i, 1, show_deref=False) print_no_end('++; cx--;') modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 return modified
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if isinstance(i, NopInst): return if isinstance(i, PseudoInst): for i2 in i.real_inst_list: self.print_inst(i2, tab, "# ") print_label_and_addr(i.real_inst_list[0].address, tab) print(i.pseudo) return if i.address in self.ctx.dis.previous_comments: for comm in self.ctx.dis.previous_comments[i.address]: print_tabbed(color_intern_comment("; %s" % comm), tab) if prefix == "# ": if self.ctx.comments: if i.address in self.ctx.labels: print_label(i.address, tab) print() print_comment_no_end(prefix, tab) print_addr(i.address) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_label_and_addr(i.address, tab) self.print_bytes(i) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") modified = self.print_operand(i, 0, hexa=True) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print() return # Here we can have conditional jump with the option --dump if is_jump(i): print_no_end(i.mnemonic + " ") if i.operands[-1].type != MIPS_OP_IMM: print_no_end(i.op_str) if is_uncond_jump(i) and self.ctx.comments and not self.ctx.dump \ and not i.address in self.ctx.dis.jmptables: print_comment_no_end(" # STOPPED") print() return for num in range(len(i.operands)-1): self.print_operand(i, num) print_no_end(", ") addr = i.operands[0].value.imm if addr in self.ctx.addr_color: print_label_or_addr(addr, -1, False) else: print_no_end(hex(addr)) print() return modified = False if i.id in LD_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LD_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in ST_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(ST_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: if i.id == MIPS_INS_LUI: print_no_end("(load upper) ") self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) elif i.id == MIPS_INS_MOVE: self.print_operand(i, 0) print_no_end(" = ") if i.operands[1].value.reg == MIPS_REG_ZERO: print_no_end("0") else: self.print_operand(i, 1) else: self.print_operand(i, 0) if i.operands[0].type == i.operands[1].type == MIPS_OP_REG and \ i.operands[0].value.reg == i.operands[1].value.reg: print_no_end(" " + inst_symbol(i) + "= ") else: print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if i.address in self.ctx.dis.inline_comments: print_no_end(color_intern_comment(" ; ")) print_no_end(color_intern_comment(self.ctx.dis.inline_comments[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if prefix == "# ": if self.ctx.comments: print_comment_no_end(prefix, tab) print_no_end(color_addr(i.address)) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if i.address != self.ctx.addr and \ i.address in self.ctx.dis.binary.reverse_symbols: print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_tabbed_no_end(color_addr(i.address), tab) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) print() return # Here we can have conditional jump with the option --dump if is_jump(i): if i.operands[0].type != ARM_OP_IMM: print_no_end(i.mnemonic + " ") print_no_end(i.op_str) if is_uncond_jump(i) and self.ctx.comments: print_comment_no_end(" # STOPPED") print() return try: addr = i.operands[0].value.imm print(i.mnemonic + " " + color(hex(addr), self.ctx.addr_color[addr])) except KeyError: print(i.mnemonic + " " + hex(addr)) return modified = False if i.id in LDR_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LDR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in STR_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(STR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: self.print_operand(i, 0) if i.id == ARM_INS_CMP: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) else: print_no_end(" = ") self.print_operand(i, 1) if len(i.operands) == 3: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if i.update_flags and i.id != ARM_INS_CMP and i.id != ARM_INS_TST: print_no_end(color_type(" (FLAGS)")) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print(self, o, tab=0, print_else_keyword=False): ARCH_UTILS = o.ctx.libarch.utils # # if cond { # } else { # ... # } # # become # # if !cond { # ... # } # br_next = self.br_next br_next_jump = self.br_next_jump inv_if = False if len(self.br_next.nodes) == 0: br_next, br_next_jump = br_next_jump, br_next inv_if = True o.print_commented_jump(self.jump_inst, self.fused_inst, tab) if print_else_keyword: print_tabbed_no_end(color_keyword("else if "), tab) else: print_tabbed_no_end(color_keyword("if "), tab) # jump_inst is the condition to go to the else-part if inv_if: o.print_if_cond(ARCH_UTILS.get_cond(self.jump_inst), self.fused_inst) else: o.print_if_cond(ARCH_UTILS.invert_cond(self.jump_inst), self.fused_inst) print(" {") # if-part br_next.print(o, tab + 1) # else-part if len(br_next_jump.nodes) > 0: print_tabbed_no_end("} ", tab) # # if { # ... # } else { # if { # ... # } # } # # become : # # if { # ... # } # else if { # ... # } # br = br_next_jump if len(br.nodes) == 1 and isinstance(br.nodes[0], Ast_Ifelse): print() br.nodes[0].print(o, tab, True) return if len(br.nodes) == 2 and isinstance(br.nodes[0], list) and \ len(br.nodes[0]) == 1 and ARCH_UTILS.is_cmp(br.nodes[0][0]) and \ isinstance(br.nodes[1], Ast_Ifelse): print() br.nodes[1].print(o, tab, True) return print(color_keyword("else ") + "{") br.print(o, tab + 1) print_tabbed("}", tab)
def print(self, tab=0): print_cmp_jump_commented(self.cmp_inst, self.orig_jump, tab) print_tabbed_no_end(color_keyword("and ") + color_keyword("if "), tab) print_if_cond(self.cmp_inst, self.cond_id) print()
def print(self, tab=0): print_cmp_jump_commented(self.cmp_inst, self.orig_jump, tab) print_tabbed_no_end(color_keyword("if "), tab) print_if_cond(self.cmp_inst, self.cond_id) print_no_end(color_keyword(" goto ")) print_addr(self.addr_jump)
def print(self, tab=0): print_tabbed_no_end("jmp ", tab) print_addr(self.addr_jump)
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if prefix == "# ": if self.ctx.comments: print_comment_no_end(prefix, tab) print_no_end(color_addr(i.address)) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_bytes(i) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) print() return # Here we can have conditional jump with the option --dump if is_jump(i): if i.operands[0].type != ARM_OP_IMM: print_no_end(i.mnemonic + " ") print_no_end(i.op_str) if is_uncond_jump( i) and self.ctx.comments and not self.ctx.dump: print_comment_no_end(" # STOPPED") print() return try: addr = i.operands[0].value.imm print(i.mnemonic + " " + color(hex(addr), self.ctx.addr_color[addr])) except KeyError: print(i.mnemonic + " " + hex(addr)) return modified = False if i.id in LDR_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LDR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in STR_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(STR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: self.print_operand(i, 0) if i.id == ARM_INS_CMP: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) else: print_no_end(" = ") self.print_operand(i, 1) if len(i.operands) == 3: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if i.update_flags and i.id != ARM_INS_CMP and i.id != ARM_INS_TST: print_no_end(color_type(" (FLAGS)")) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def __print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) def print_rep_begin(): nonlocal tab if i.prefix[0] in REP_PREFIX: print_tabbed_no_end(color_keyword("while"), tab) # TODO: for 16 and 32 bits print_no_end(" (!rcx)") print(") {") tab += 1 def print_rep_end(): nonlocal tab if i.prefix[0] in REP_PREFIX: print() print_tabbed_no_end(color_addr(i.address), tab) print("rcx--") if i.prefix[0] == X86_PREFIX_REPNE: print_tabbed_no_end(color_keyword("if"), tab) print_no_end(" (!Z) ") print(color_keyword("break")) tab -= 1 print_tabbed_no_end("}", tab) print_rep_begin() print_tabbed_no_end(color_addr(i.address), tab) self.print_bytes(i) if is_ret(i): print_no_end(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) return # Here we can have conditional jump with the option --dump if is_jump(i): if i.operands[0].type != X86_OP_IMM: print_no_end(i.mnemonic + " ") self.print_operand(i, 0) if is_uncond_jump(i) and self.ctx.comments and not self.ctx.dump: print_comment_no_end(" # STOPPED") return try: addr = i.operands[0].value.imm print_no_end(i.mnemonic + " " + color(hex(addr), self.ctx.addr_color[addr])) except KeyError: print_no_end(i.mnemonic + " " + hex(addr)) return modified = False if i.id in INST_CHECK: if (i.id == X86_INS_OR and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == -1): self.print_operand(i, 0) print_no_end(" = -1") elif (i.id == X86_INS_AND and i.operands[1].type == X86_OP_IMM and i.operands[1].value.imm == 0): self.print_operand(i, 0) print_no_end(" = 0") elif (all(op.type == X86_OP_REG for op in i.operands) and len(set(op.value.reg for op in i.operands)) == 1 and i.id == X86_INS_XOR): self.print_operand(i, 0) print_no_end(" = 0") elif i.id == X86_INS_INC or i.id == X86_INS_DEC: self.print_operand(i, 0) print_no_end(inst_symbol(i)) elif i.id == X86_INS_LEA: self.print_operand(i, 0) print_no_end(" = &(") self.print_operand(i, 1) print_no_end(")") elif i.id == X86_INS_MOVZX: self.print_operand(i, 0) print_no_end(" = (zero ext) ") self.print_operand(i, 1) elif i.id == X86_INS_IMUL: if len(i.operands) == 3: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i).rstrip('=') + " ") self.print_operand(i, 2) elif len(i.operands) == 2: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) elif len(i.operands) == 1: sz = i.operands[0].size if sz == 1: print_no_end("ax = al * ") elif sz == 2: print_no_end("dx:ax = ax * ") elif sz == 4: print_no_end("edx:eax = eax * ") elif sz == 8: print_no_end("rdx:rax = rax * ") self.print_operand(i, 0) else: self.print_operand(i, 0) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) modified = True elif i.id == X86_INS_CDQE: print_no_end("rax = eax") modified = True elif i.id == X86_INS_IDIV: print_no_end('eax = edx:eax / ') self.print_operand(i, 0) print_no_end('; edx = edx:eax % ') self.print_operand(i, 0) modified = True elif i.id == X86_INS_MUL: lut = {1: ("al", "ax"), 2: ("ax", "dx:ax"), 4: ("eax", "edx:eax"), 8: ("rax", "rdx:rax")} src, dst = lut[i.operands[0].size] print_no_end('{0} = {1} * '.format(dst, src)) self.print_operand(i, 0) modified = True elif i.id == X86_INS_NOT: self.print_operand(i, 0) print_no_end(' ^= -1') modified = True elif i.id in INST_SCAS: self.print_operand(i, 0) print_no_end(" cmp ") self.print_operand(i, 1) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 1, show_deref=False) print_no_end(" += D") modified = True elif i.id in INST_STOS: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 0, show_deref=False) print_no_end(" += D") modified = True elif i.id in INST_LODS: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 1, show_deref=False) print_no_end(" += D") modified = True elif i.id in INST_CMPS: self.print_operand(i, 0) print_no_end(" cmp ") self.print_operand(i, 1) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 0, show_deref=False) print(" += D") print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 1, show_deref=False) print_no_end("' += D") modified = True elif i.id in INST_MOVS: self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) print() print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 0, show_deref=False) print(" += D") print_tabbed_no_end(color_addr(i.address), tab) self.print_operand(i, 1, show_deref=False) print_no_end(" += D") modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 print_rep_end() return modified
def print(self, o, tab=0): print_tabbed_no_end(color_keyword("goto "), tab) print(color_addr(self.addr_jump, False))
def print(self, tab=0, print_else_keyword=False): # # if cond { # } else { # ... # } # # become # # if !cond { # ... # } # br_next = self.br_next br_next_jump = self.br_next_jump inv_if = False if len(self.br_next.nodes) == 0: br_next, br_next_jump = br_next_jump, br_next inv_if = True print_cmp_jump_commented(self.cmp_inst, self.jump_inst, tab) if print_else_keyword: print_tabbed_no_end(color_keyword("else if "), tab) else: print_tabbed_no_end(color_keyword("if "), tab) # jump_inst is the condition to go to the else-part if inv_if: print_if_cond(self.cmp_inst, self.jump_inst.id) else: print_if_cond(self.cmp_inst, invert_cond(self.jump_inst.id)) print(" {") # if-part br_next.print(tab+1) # else-part if len(br_next_jump.nodes) > 0: print_tabbed_no_end("} ", tab) # # if { # ... # } else { # if { # ... # } # } # # become : # # if { # ... # } # else if { # ... # } # br = br_next_jump if len(br.nodes) == 1 and isinstance(br.nodes[0], Ast_Ifelse): print() br.nodes[0].print(tab, True) return if len(br.nodes) == 2 and isinstance(br.nodes[0], list) and \ len(br.nodes[0]) == 1 and br.nodes[0][0].id == X86_INS_CMP and \ isinstance(br.nodes[1], Ast_Ifelse): print() br.nodes[1].print(tab, True) return print(color_keyword("else ") + "{") br.print(tab+1) print_tabbed("}", tab)
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if i.address in self.ctx.dis.previous_comments: for comm in self.ctx.dis.previous_comments[i.address]: print_tabbed(color_intern_comment("; %s" % comm), tab) if prefix == "# ": if self.ctx.comments: if i.address in self.ctx.labels: print_label(i.address, tab) print() print_comment_no_end(prefix, tab) print_addr(i.address) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_label_and_addr(i.address, tab) self.print_bytes(i) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") modified = self.print_operand(i, 0, hexa=True) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print() return # Here we can have conditional jump with the option --dump if is_jump(i): print_no_end(i.mnemonic + " ") if i.operands[0].type != ARM_OP_IMM: print_no_end(i.op_str) if is_uncond_jump(i) and self.ctx.comments and not self.ctx.dump \ and not i.address in self.ctx.dis.jmptables: print_comment_no_end(" # STOPPED") print() return addr = i.operands[0].value.imm if addr in self.ctx.addr_color: print_label_or_addr(addr, -1, False) else: print_no_end(hex(addr)) print() return modified = False if i.id in LDR_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LDR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in STR_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(STR_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: self.print_operand(i, 0) if i.id == ARM_INS_CMP: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 1) else: print_no_end(" = ") self.print_operand(i, 1) if len(i.operands) == 3: print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if i.update_flags and i.id != ARM_INS_CMP and i.id != ARM_INS_TST: print_no_end(color_type(" (FLAGS)")) if i.address in self.ctx.dis.inline_comments: print_no_end(color_intern_comment(" ; ")) print_no_end(color_intern_comment(self.ctx.dis.inline_comments[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if prefix == "# ": if self.ctx.comments: print_comment_no_end(prefix, tab) print_no_end(color_addr(i.address)) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if i.address != self.ctx.addr and \ i.address in self.ctx.dis.binary.reverse_symbols: print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_tabbed_no_end(color_addr(i.address), tab) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") self.print_operand(i, 0, hexa=True) print() return # Here we can have conditional jump with the option --dump if is_jump(i): print_no_end(i.mnemonic + " ") if i.operands[-1].type != MIPS_OP_IMM: print_no_end(i.op_str) if is_uncond_jump(i) and self.ctx.comments: print_comment_no_end(" # STOPPED") print() return for num in range(len(i.operands)-1): self.print_operand(i, num) print_no_end(", ") addr = i.operands[-1].value.imm if addr in self.ctx.addr_color: print(color(hex(addr), self.ctx.addr_color[addr])) else: print(hex(addr)) return modified = False if i.id in LD_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LD_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in ST_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(ST_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: if i.id == MIPS_INS_LUI: print_no_end("(load upper) ") self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) elif i.id == MIPS_INS_MOVE: self.print_operand(i, 0) print_no_end(" = ") if i.operands[1].value.reg == MIPS_REG_ZERO: print_no_end("0") else: self.print_operand(i, 1) else: self.print_operand(i, 0) if i.operands[0].type == i.operands[1].type == MIPS_OP_REG and \ i.operands[0].value.reg == i.operands[1].value.reg: print_no_end(" " + inst_symbol(i) + "= ") else: print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()
def print_inst(self, i, tab=0, prefix=""): def get_inst_str(): nonlocal i return "%s %s" % (i.mnemonic, i.op_str) if isinstance(i, NopInst): return if isinstance(i, PseudoInst): for i2 in i.real_inst_list: self.print_inst(i2, tab, "# ") print_label_and_addr(i.real_inst_list[0].address, tab) print(i.pseudo) return if i.address in self.ctx.dis.previous_comments: for comm in self.ctx.dis.previous_comments[i.address]: print_tabbed(color_intern_comment("; %s" % comm), tab) if prefix == "# ": if self.ctx.comments: if i.address in self.ctx.labels: print_label(i.address, tab) print() print_comment_no_end(prefix, tab) print_addr(i.address) self.print_bytes(i, True) print_comment(get_inst_str()) return if i.address in self.ctx.all_fused_inst: return if self.is_symbol(i.address): print_tabbed_no_end("", tab) self.print_symbol(i.address) print() print_label_and_addr(i.address, tab) self.print_bytes(i) if is_ret(i): print(color_retcall(get_inst_str())) return if is_call(i): print_no_end(color_retcall(i.mnemonic) + " ") modified = self.print_operand(i, 0, hexa=True) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print() return # Here we can have conditional jump with the option --dump if is_jump(i): print_no_end(i.mnemonic + " ") if i.operands[-1].type != MIPS_OP_IMM: print_no_end(i.op_str) if is_uncond_jump(i) and self.ctx.comments and not self.ctx.dump \ and not i.address in self.ctx.dis.jmptables: print_comment_no_end(" # STOPPED") print() return for num in range(len(i.operands) - 1): self.print_operand(i, num) print_no_end(", ") addr = i.operands[0].value.imm if addr in self.ctx.addr_color: print_label_or_addr(addr, -1, False) else: print_no_end(hex(addr)) print() return modified = False if i.id in LD_CHECK: self.print_operand(i, 0) print_no_end(" = (") print_no_end(color_type(LD_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 1) modified = True elif i.id in ST_CHECK: self.print_operand(i, 1) print_no_end(" = (") print_no_end(color_type(ST_TYPE[i.id])) print_no_end(") ") self.print_operand(i, 0) modified = True elif i.id in INST_CHECK: if i.id == MIPS_INS_LUI: print_no_end("(load upper) ") self.print_operand(i, 0) print_no_end(" = ") self.print_operand(i, 1) elif i.id == MIPS_INS_MOVE: self.print_operand(i, 0) print_no_end(" = ") if i.operands[1].value.reg == MIPS_REG_ZERO: print_no_end("0") else: self.print_operand(i, 1) else: self.print_operand(i, 0) if i.operands[0].type == i.operands[1].type == MIPS_OP_REG and \ i.operands[0].value.reg == i.operands[1].value.reg: print_no_end(" " + inst_symbol(i) + "= ") else: print_no_end(" = ") self.print_operand(i, 1) print_no_end(" " + inst_symbol(i) + " ") self.print_operand(i, 2) modified = True else: print_no_end("%s " % i.mnemonic) if len(i.operands) > 0: modified = self.print_operand(i, 0) k = 1 while k < len(i.operands): print_no_end(", ") modified |= self.print_operand(i, k) k += 1 if i.address in self.ctx.dis.inline_comments: print_no_end(color_intern_comment(" ; ")) print_no_end( color_intern_comment(self.ctx.dis.inline_comments[i.address])) if modified and self.ctx.comments: print_comment_no_end(" # " + get_inst_str()) print()