class Disassembler(): def __init__(self, filename, raw_type, raw_base, raw_big_endian): import capstone as CAPSTONE self.code = {} self.binary = Binary(filename, raw_type, raw_base, raw_big_endian) arch, mode = self.binary.get_arch() if arch is None or mode is None: raise ExcArch(self.binary.get_arch_string()) self.binary.load_extra() self.md = CAPSTONE.Cs(arch, mode) self.md.detail = True self.arch = arch self.mode = mode def check_addr(self, ctx, addr): addr_exists, is_exec = self.binary.check_addr(addr) if not ctx.print_data and not is_exec: raise ExcNotExec(addr) if not addr_exists: raise ExcNotAddr(addr) def load_arch_module(self): import capstone as CAPSTONE if self.arch == CAPSTONE.CS_ARCH_X86: import lib.arch.x86 as ARCH elif self.arch == CAPSTONE.CS_ARCH_ARM: import lib.arch.arm as ARCH elif self.arch == CAPSTONE.CS_ARCH_MIPS: import lib.arch.mips as ARCH else: raise NotImplementedError return ARCH def get_addr_from_string(self, opt_addr, raw=False): if opt_addr is None: if raw: return 0 search = ["main", "_main"] else: search = [opt_addr] for s in search: if s.startswith("0x"): a = int(opt_addr, 16) else: a = self.binary.symbols.get(s, -1) if a != -1: return a raise ExcSymNotFound(search[0]) def print_section_meta(self, name, start, end): print_no_end(color_section(name.ljust(20))) print_no_end(" [") print_no_end(hex(start)) print_no_end(" - ") print_no_end(hex(end)) print("]") def dump_asm(self, ctx, lines): from capstone import CS_OP_IMM ARCH = self.load_arch_module() ARCH_UTILS = ARCH.utils ARCH_OUTPUT = ARCH.output s_name, s_start, s_end = self.binary.get_section_meta(ctx.entry_addr) self.print_section_meta(s_name, s_start, s_end) # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) # set jumps color ad = ctx.entry_addr l = 0 while l < lines and ad < s_end: i = self.lazy_disasm(ad, s_start) if i is None: ad += 1 else: if ARCH_UTILS.is_jump(i) and i.operands[-1].type == CS_OP_IMM: pick_color(i.operands[-1].value.imm) ad += i.size l += 1 # Here we have loaded all instructions we want to print if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) o = ARCH_OUTPUT.Output(ctx) # dump ad = ctx.entry_addr l = 0 while l < lines and ad < s_end: i = self.lazy_disasm(ad, s_start) if i is None: ad += 1 o.print_bad(ad) else: o.print_inst(i) ad += i.size l += 1 def dump_data(self, ctx, lines): N = 128 addr = ctx.entry_addr s_name, s_start, s_end = self.binary.get_section_meta(ctx.entry_addr) self.print_section_meta(s_name, s_start, s_end) addr_ascii_str = -1 ascii_str = [] l = 0 is_new_line = True while l < lines: buf = self.binary.section_stream_read(addr, N) if not buf: break i = 0 while i < len(buf): c = buf[i] if c in BYTES_PRINTABLE_SET: if addr_ascii_str == -1: addr_ascii_str = addr ascii_str.append(c) addr += 1 i += 1 continue stack_char = [] if addr_ascii_str != -1: if c == 0 and len(ascii_str) >= 2: if not is_new_line: print() l += 1 if l >= lines: return print_no_end(color_addr(addr_ascii_str)) print_no_end(color_string("\"" + "".join(map(get_char, ascii_str)) + "\"")) print(", 0") is_new_line = True l += 1 if l >= lines: return ascii_str = [] addr_ascii_str = -1 addr += 1 i += 1 continue stack_char = ascii_str i -= len(ascii_str) addr -= len(ascii_str) stack_char.append(c) for c in stack_char: if is_new_line: print_no_end(color_addr(addr)) elif addr % 4 == 0 and addr != ctx.entry_addr: print() is_new_line = True l += 1 if l >= lines: return print_no_end(color_addr(addr)) print_no_end("%.2x " % c) addr += 1 i += 1 is_new_line = False ascii_str = [] addr_ascii_str = -1 if not is_new_line: print() def print_calls(self, ctx): ARCH = self.load_arch_module() ARCH_UTILS = ARCH.utils ARCH_OUTPUT = ARCH.output s_name, s_start, s_end = self.binary.get_section_meta(ctx.entry_addr) self.print_section_meta(s_name, s_start, s_end) o = ARCH_OUTPUT.Output(ctx) ad = s_start while ad < s_end: i = self.lazy_disasm(ad, s_start) if i is None: ad += 1 else: ad += i.size if ARCH_UTILS.is_call(i): o.print_inst(i) def print_symbols(self, print_sections, sym_filter=None): if sym_filter is not None: sym_filter = sym_filter.lower() for addr in self.binary.reverse_symbols: sy = self.binary.reverse_symbols[addr] if sym_filter is None or sym_filter in sy.lower(): sec_name, _ = self.binary.is_address(addr) if sy: print_no_end(color_addr(addr) + " " + color_symbol("<" + sy + ">")) if print_sections and sec_name is not None: print_no_end(" (" + color_section(sec_name) + ")") print() def load_user_sym_file(self, fd): for l in fd: arg = l.split() addr = int(arg[0], 16) self.binary.reverse_symbols[addr] = arg[1] self.binary.symbols[arg[1]] = addr def lazy_disasm(self, addr, stay_in_section=-1): meta = self.binary.get_section_meta(addr) if meta is None: return None _, start, _ = meta if stay_in_section != -1 and start != stay_in_section: return None if addr in self.code: return self.code[addr] # Disassemble by block of N bytes N = 1024 d = self.binary.section_stream_read(addr, N) gen = self.md.disasm(d, addr) first = None try: first = next(gen) self.code[first.address] = first # Max N instructions (N is in bytes) for n in range(N): i = next(gen) if i.address in self.code: return first self.code[i.address] = i except StopIteration: pass return first def __prefetch_inst(self, inst): return self.lazy_disasm(inst.address + inst.size) # Generate a flow graph of the given function (addr) def get_graph(self, addr): from capstone import CS_OP_IMM, CS_ARCH_MIPS ARCH_UTILS = self.load_arch_module().utils curr = self.lazy_disasm(addr) if curr == None: return None gph = Graph(self, addr) rest = [] start = time.clock() prefetch = None # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) while 1: if not gph.exists(curr): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(curr) if ARCH_UTILS.is_uncond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: addr = curr.operands[-1].value.imm nxt = self.lazy_disasm(addr) gph.set_next(curr, nxt, prefetch) rest.append(nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr, prefetch) gph.uncond_jumps_set.add(curr.address) elif ARCH_UTILS.is_cond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: nxt_jump = self.lazy_disasm(curr.operands[-1].value.imm) if self.arch == CS_ARCH_MIPS: direct_nxt = \ self.lazy_disasm(prefetch.address + prefetch.size) else: direct_nxt = \ self.lazy_disasm(curr.address + curr.size) gph.set_cond_next(curr, nxt_jump, direct_nxt, prefetch) rest.append(nxt_jump.address) rest.append(direct_nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr, prefetch) gph.cond_jumps_set.add(curr.address) elif ARCH_UTILS.is_ret(curr): gph.add_node(curr, prefetch) else: try: nxt = self.lazy_disasm(curr.address + curr.size) gph.set_next(curr, nxt) rest.append(nxt.address) except: gph.add_node(curr) pass try: curr = self.lazy_disasm(rest.pop()) except IndexError: break if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) elapsed = time.clock() elapsed = elapsed - start debug__("Graph built in %fs" % elapsed) return gph
class Disassembler(): def __init__(self, filename, raw_type, raw_base, raw_big_endian): import capstone as CAPSTONE self.code = {} self.binary = Binary(filename, raw_type, raw_base, raw_big_endian) arch, mode = self.binary.get_arch() if arch is None or mode is None: raise ExcArch(self.binary.get_arch_string()) self.binary.load_extra() self.md = CAPSTONE.Cs(arch, mode) self.md.detail = True self.arch = arch self.mode = mode def check_addr(self, addr): addr_exists, is_exec = self.binary.check_addr(addr) if not is_exec: raise ExcNotExec(addr) if not addr_exists: raise ExcNotAddr(addr) def load_arch_module(self): import capstone as CAPSTONE if self.arch == CAPSTONE.CS_ARCH_X86: import lib.arch.x86 as ARCH elif self.arch == CAPSTONE.CS_ARCH_ARM: import lib.arch.arm as ARCH elif self.arch == CAPSTONE.CS_ARCH_MIPS: import lib.arch.mips as ARCH else: raise NotImplementedError return ARCH def get_addr_from_string(self, opt_addr, raw=False): if opt_addr is None: if raw: return 0 search = ["main", "_main"] else: search = [opt_addr] for s in search: if s.startswith("0x"): a = int(opt_addr, 16) else: a = self.binary.symbols.get(s, -1) if a != -1: return a raise ExcSymNotFound(search[0]) def print_section_meta(self, name, start, end): print_no_end(color_section(name)) print_no_end(" [") print_no_end(hex(start)) print_no_end(" - ") print_no_end(hex(end)) print("]") def dump(self, ctx, lines): from capstone import CS_OP_IMM ARCH = self.load_arch_module() ARCH_UTILS = ARCH.utils ARCH_OUTPUT = ARCH.output s_name, s_start, s_end = self.binary.get_section_meta(ctx.addr) self.print_section_meta(s_name, s_start, s_end) # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) # set jumps color i = self.lazy_disasm(ctx.addr, s_start) l = 0 while i is not None and l < lines: if ARCH_UTILS.is_jump(i) and i.operands[-1].type == CS_OP_IMM: pick_color(i.operands[-1].value.imm) i = self.lazy_disasm(i.address + i.size, s_start) l += 1 # Here we have loaded all instructions we want to print if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) o = ARCH_OUTPUT.Output(ctx) # dump i = self.lazy_disasm(ctx.addr, s_start) l = 0 while i is not None and l < lines: o.print_inst(i) i = self.lazy_disasm(i.address + i.size, s_start) l += 1 def print_calls(self, ctx): # Print all calls which are in the section containing ctx.addr ARCH = self.load_arch_module() ARCH_UTILS = ARCH.utils ARCH_OUTPUT = ARCH.output s_name, s_start, s_end = self.binary.get_section_meta(ctx.addr) self.print_section_meta(s_name, s_start, s_end) o = ARCH_OUTPUT.Output(ctx) i = self.lazy_disasm(s_start, s_start) while i is not None: if ARCH_UTILS.is_call(i): o.print_inst(i) i = self.lazy_disasm(i.address + i.size, s_start) def print_symbols(self, print_sections, sym_filter=None): if sym_filter is not None: sym_filter = sym_filter.lower() for addr in self.binary.reverse_symbols: sy = self.binary.reverse_symbols[addr] if sym_filter is None or sym_filter in sy.lower(): sec_name, _ = self.binary.is_address(addr) if sy: print_no_end(color_addr(addr) + " " + color_symbol("<" + sy + ">")) if print_sections and sec_name is not None: print_no_end(" (" + color_section(sec_name) + ")") print() def load_user_sym_file(self, fd): for l in fd: arg = l.split() addr = int(arg[0], 16) self.binary.reverse_symbols[addr] = arg[1] self.binary.symbols[arg[1]] = addr def lazy_disasm(self, addr, stay_in_section=-1): _, start, _ = self.binary.get_section_meta(addr) if stay_in_section != -1 and start != stay_in_section: return None if addr in self.code: return self.code[addr] # Disassemble by block of N bytes N = 1024 d = self.binary.section_stream_read(addr, N) gen = self.md.disasm(d, addr) first = None try: first = next(gen) self.code[first.address] = first # Max N instructions (N is in bytes) for n in range(N): i = next(gen) if i.address in self.code: return first self.code[i.address] = i except StopIteration: pass return first # Generate a flow graph of the given function (addr) def get_graph(self, addr): from capstone import CS_OP_IMM ARCH_UTILS = self.load_arch_module().utils curr = self.lazy_disasm(addr) if curr == None: return None gph = Graph(self, addr) rest = [] start = time.clock() # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) while 1: if not gph.exists(curr): if ARCH_UTILS.is_uncond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: addr = curr.operands[-1].value.imm nxt = self.lazy_disasm(addr) gph.set_next(curr, nxt) rest.append(nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr) gph.uncond_jumps_set.add(curr.address) elif ARCH_UTILS.is_cond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: nxt_jump = self.lazy_disasm(curr.operands[-1].value.imm) direct_nxt = self.lazy_disasm(curr.address + curr.size) gph.set_cond_next(curr, nxt_jump, direct_nxt) rest.append(nxt_jump.address) rest.append(direct_nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr) gph.cond_jumps_set.add(curr.address) elif ARCH_UTILS.is_ret(curr): gph.add_node(curr) else: try: nxt = self.lazy_disasm(curr.address + curr.size) gph.set_next(curr, nxt) rest.append(nxt.address) except: gph.add_node(curr) pass try: curr = self.lazy_disasm(rest.pop()) except IndexError: break if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) elapsed = time.clock() elapsed = elapsed - start debug__("Graph built in %fs" % elapsed) return gph