|
| 1 | +Guide |
| 2 | +===== |
| 3 | + |
| 4 | +Installation |
| 5 | +------------ |
| 6 | +This package can be installed on Linux, macOS, and Windows platforms for recent (3.8+) versions of both CPython and |
| 7 | +PyPy. Wheels are provided for several configurations. The latest release can be installed from PyPI using ``pip``: |
| 8 | + |
| 9 | +.. code:: bash |
| 10 | +
|
| 11 | + pip install pypcode |
| 12 | +
|
| 13 | +The very latest development version can be installed from GitHub via: |
| 14 | + |
| 15 | +.. code:: bash |
| 16 | +
|
| 17 | + pip install --user https://github.com/angr/pypcode/archive/refs/heads/master.zip |
| 18 | +
|
| 19 | +Usage Example |
| 20 | +------------- |
| 21 | +.. code:: python |
| 22 | +
|
| 23 | + from pypcode import Arch, Context, PcodePrettyPrinter |
| 24 | +
|
| 25 | + # Enumerate available languages, select target language |
| 26 | + langs = {lang.id: lang for arch in Arch.enumerate() for lang in arch.languages} |
| 27 | + lang = langs["x86:LE:64:default"] |
| 28 | +
|
| 29 | + # Load machine code |
| 30 | + base_address = 0 |
| 31 | + machine_code = b"\x48\x35\x78\x56\x34\x12\xc3" |
| 32 | +
|
| 33 | + # Create a translation context, lift a block |
| 34 | + ctx = Context(lang) |
| 35 | + result = ctx.translate(machine_code, base_address, bb_terminating=True) |
| 36 | + if result.error: |
| 37 | + print(f"Error: {result.error}") |
| 38 | + exit(1) |
| 39 | +
|
| 40 | + # Print instruction disassembly, P-Code |
| 41 | + for insn in result.instructions: |
| 42 | + print(f"-- {insn.address.offset:#x}: {insn.asm_mnem} {insn.asm_body}") |
| 43 | + for op in insn.ops: |
| 44 | + print(f"{op.seq.uniq}) {PcodePrettyPrinter.fmt_op(op)}") |
| 45 | +
|
| 46 | +.. highlight:: none |
| 47 | +:: |
| 48 | + |
| 49 | + -- 0x0: XOR RAX,0x12345678 |
| 50 | + 0) CF = 0x0 |
| 51 | + 1) OF = 0x0 |
| 52 | + 2) RAX = RAX ^ 0x12345678 |
| 53 | + 3) SF = RAX s< 0x0 |
| 54 | + 4) ZF = RAX == 0x0 |
| 55 | + 5) unique[0x13180:8] = RAX & 0xff |
| 56 | + 6) unique[0x13200:1] = popcount(unique[0x13180:8]) |
| 57 | + 7) unique[0x13280:1] = unique[0x13200:1] & 0x1 |
| 58 | + 8) PF = unique[0x13280:1] == 0x0 |
| 59 | + -- 0x6: RET |
| 60 | + 0) RIP = *[ram]RSP |
| 61 | + 1) RSP = RSP + 0x8 |
| 62 | + 2) return RIP |
| 63 | + |
| 64 | +Command Line Usage Example |
| 65 | +-------------------------- |
| 66 | +The ``pypcode`` module can be invoked from command line to disassemble and translate supported machine code to P-code |
| 67 | +from command line. Run ``python -m pypcode --help`` for usage information. |
| 68 | + |
| 69 | +:: |
| 70 | + |
| 71 | + $ python -m pypcode -b x86:LE:64:default test-x64.bin |
| 72 | + -------------------------------------------------------------------------------- |
| 73 | + 00000000/2: XOR EAX,EAX |
| 74 | + -------------------------------------------------------------------------------- |
| 75 | + 0: CF = 0x0 |
| 76 | + 1: OF = 0x0 |
| 77 | + 2: EAX = EAX ^ EAX |
| 78 | + 3: RAX = zext(EAX) |
| 79 | + 4: SF = EAX s< 0x0 |
| 80 | + 5: ZF = EAX == 0x0 |
| 81 | + 6: unique[0x2580:4] = EAX & 0xff |
| 82 | + 7: unique[0x2590:1] = popcount(unique[0x2580:4]) |
| 83 | + 8: unique[0x25a0:1] = unique[0x2590:1] & 0x1 |
| 84 | + 9: PF = unique[0x25a0:1] == 0x0 |
| 85 | + |
| 86 | + -------------------------------------------------------------------------------- |
| 87 | + 00000002/2: CMP ESI,EAX |
| 88 | + -------------------------------------------------------------------------------- |
| 89 | + 0: CF = ESI < EAX |
| 90 | + 1: OF = sborrow(ESI, EAX) |
| 91 | + 2: unique[0x5180:4] = ESI - EAX |
| 92 | + 3: SF = unique[0x5180:4] s< 0x0 |
| 93 | + 4: ZF = unique[0x5180:4] == 0x0 |
| 94 | + 5: unique[0x2580:4] = unique[0x5180:4] & 0xff |
| 95 | + 6: unique[0x2590:1] = popcount(unique[0x2580:4]) |
| 96 | + 7: unique[0x25a0:1] = unique[0x2590:1] & 0x1 |
| 97 | + 8: PF = unique[0x25a0:1] == 0x0 |
| 98 | + |
| 99 | + -------------------------------------------------------------------------------- |
| 100 | + 00000004/2: JBE 0x17 |
| 101 | + -------------------------------------------------------------------------------- |
| 102 | + 0: unique[0x18f0:1] = CF || ZF |
| 103 | + 1: if (unique[0x18f0:1]) goto ram[0x17:8] |
| 104 | + |
| 105 | +SLEIGH & P-Code References |
| 106 | +-------------------------- |
| 107 | +Extensive documentation covering SLEIGH and P-Code is available online: |
| 108 | + |
| 109 | +* `SLEIGH, P-Code Introduction <https://htmlpreview.github.io/?https://github.com/NationalSecurityAgency/ghidra/blob/Ghidra_10.2.2_build/GhidraDocs/languages/html/sleigh.html>`_ |
| 110 | +* `P-Code Reference Manual <https://htmlpreview.github.io/?https://github.com/NationalSecurityAgency/ghidra/blob/Ghidra_10.2.2_build/GhidraDocs/languages/html/pcoderef.html>`_ |
0 commit comments