def _syntax_highlighting(self, data): try: from pygments import highlight from pygments.lexers import NasmLexer, GasLexer from pygments.formatters import TerminalFormatter, Terminal256Formatter from pygments.styles import get_style_by_name style = get_style_by_name('colorful') import curses curses.setupterm() if curses.tigetnum('colors') >= 256: FORMATTER = Terminal256Formatter(style=style) else: FORMATTER = TerminalFormatter() if self.ks.syntax == keystone.KS_OPT_SYNTAX_INTEL: lexer = NasmLexer() else: lexer = GasLexer() # When pygments is available, we # can print the disassembled # instructions with syntax # highlighting. data = highlight(data, lexer, FORMATTER) except ImportError: pass finally: data = data.encode() return data
def __init__(self, trace=True, sca_mode=False): self.breakpoints = [] self.skips = [] self.emu = None self.disasm = None self.uc_reg = None self.mapped_regions = [] self.page_size = 0 self.functions = {} self.function_names = {} self.OTHER_REGS = {} self.OTHER_REGS_NAMES = {} # Tracing properties self.trace = trace self.mem_trace = False self.function_calls = False self.trace_regs = False self.stubbed_functions = {} self.sca_mode = sca_mode ## Prepare a live disassembler self.asm_hl = NasmLexer() self.asm_fmt = formatter(outencoding="utf-8") colorama.init() self.trace_reset()
def __pstr_codeblock(self, ip) -> str: """Get the pretty version of a basic block with Pygemnts""" try: block = self.state.project.factory.block(ip) code = self._disassembler.disass_block(block) return highlight(code, NasmLexer(), TerminalFormatter()) except SimEngineError: return None
def __pstr_codeblock(self, ip) -> str: """Get the pretty version of a basic block with Pygemnts""" try: block = self.state.project.factory.block(ip) code = self._disassembler.disass_block(block) return highlight(code, NasmLexer(), TerminalFormatter()) except SimEngineError: return self.red( "No code at current ip. Please specify self_modifying code ")
def cap_disasm(self, ip): md = capstone.Cs(self.state.project.arch.cs_arch, self.state.project.arch.cs_mode) r = "" code = self.state.memory.load(ip, MAX_DISASS_LENGHT * 10) code = self.state.solver.eval(code, cast_to=bytes) cnt = 0 for i in md.disasm(code, MAX_DISASS_LENGHT * 10): r += "0x%x:\t%s\t%s\n" % (ip + i.address, i.mnemonic, i.op_str) cnt += 1 if cnt == 18: break return highlight(r, NasmLexer(), TerminalFormatter())
class NasmLexerTest(unittest.TestCase): def setUp(self): self.lexer = NasmLexer() def testCPUID(self): # CPU is a valid directive, and we don't want to parse this as # cpu id, but as a single token. See bug #1517 fragment = 'cpuid' expected = [ (Token.Name.Function, u'cpuid'), (Token.Text, u'\n'), ] self.assertEqual(expected, list(self.lexer.get_tokens(fragment)))
def __pprint_codeblock(self, ip): # Check if we are currently in the extern object in which case printing disassembly is pointless o = self.state.project.loader.find_object_containing(ip) if o == self.state.project.loader.extern_object: print(self.state.project._sim_procedures[ip]) return try: f = self.state.project.kb.functions.floor_func(ip) print(f.name + "+" + hex(ip - f.addr)) except: pass try: code = self.state.project.factory.block(ip).capstone.__str__() highlighed_code = highlight(code, NasmLexer(), TerminalFormatter()) print("\n".join(highlighed_code.split('\n') [:20])) #HACK: limit printed lines to 20 except: self.red( "No code at current ip. Please specify self_modifying code ")
def lexer_nasm(): yield NasmLexer()
def setUp(self): self.lexer = NasmLexer()