Format output of disassemble command with colors to highlight: - dangerous functions (rats/flawfinder) - branching: jmp, call, ret - testing: cmp, test Args: - code: input asm code (String) - nearby: address for nearby style format (Int) Returns
(code, nearby=None)
| 488 | ] |
| 489 | @memoized |
| 490 | def format_disasm_code(code, nearby=None): |
| 491 | """ |
| 492 | Format output of disassemble command with colors to highlight: |
| 493 | - dangerous functions (rats/flawfinder) |
| 494 | - branching: jmp, call, ret |
| 495 | - testing: cmp, test |
| 496 | |
| 497 | Args: |
| 498 | - code: input asm code (String) |
| 499 | - nearby: address for nearby style format (Int) |
| 500 | |
| 501 | Returns: |
| 502 | - colorized text code (String) |
| 503 | """ |
| 504 | colorcodes = { |
| 505 | "cmp": "red", |
| 506 | "test": "red", |
| 507 | "call": "green", |
| 508 | "j": "yellow", # jump |
| 509 | "ret": "blue", |
| 510 | } |
| 511 | result = "" |
| 512 | |
| 513 | if not code: |
| 514 | return result |
| 515 | |
| 516 | if to_int(nearby) is not None: |
| 517 | target = to_int(nearby) |
| 518 | else: |
| 519 | target = 0 |
| 520 | |
| 521 | for line in code.splitlines(): |
| 522 | if ":" not in line: # not an assembly line |
| 523 | result += line + "\n" |
| 524 | else: |
| 525 | color = style = None |
| 526 | m = re.search(".*(0x[^ ]*).*:\s*([^ ]*)", line) |
| 527 | if not m: # failed to parse |
| 528 | result += line + "\n" |
| 529 | continue |
| 530 | addr, opcode = to_int(m.group(1)), m.group(2) |
| 531 | for c in colorcodes: |
| 532 | if c in opcode: |
| 533 | color = colorcodes[c] |
| 534 | if c == "call": |
| 535 | for f in VULN_FUNCTIONS: |
| 536 | if f in line.split(":\t", 1)[-1]: |
| 537 | style = "bold, underline" |
| 538 | color = "red" |
| 539 | break |
| 540 | break |
| 541 | |
| 542 | prefix = line.split(":\t")[0] |
| 543 | addr = re.search("(0x[^\s]*)", prefix) |
| 544 | if addr: |
| 545 | addr = to_int(addr.group(1)) |
| 546 | else: |
| 547 | addr = -1 |