Skip to main content

python bindings for ghidra's sleigh engine

Project description

psleigh

python bindings for ghidra's sleigh engine.

this library provides high level pythonic bindings for the sleigh engine, with a focus on ease of use and convenience.

instead of working with cumbersome ffi objects, this library provides pythonic dataclass objects that are much easier to work with.

installation

pip install psleigh

usage

here is a simple example of how to use this library to lift a single x86_64 instruction:

from psleigh import Sleigh, SleighArch, BufMemReader

# mov rax, 0x1234567890abcdef
code = bytes.fromhex("48 b8 ef cd ab 90 78 56 34 12")

sleigh = Sleigh(SleighArch.x86_64(), BufMemReader(code, 0))

# lift the instruction at address 0
res = sleigh.lift_one(0)

# print the lifted p-code
print(res.fmt_insns(sleigh))

this will output the following:

COPY RAX, 0x1234567890abcdef:8

some instructions translate to multiple p-code operations. for example, a push rax instruction on x86_64 will first decrement the stack pointer and then store the value of rax at the new stack pointer location:

from psleigh import Sleigh, SleighArch, BufMemReader

# push rax
code = bytes.fromhex("50")

sleigh = Sleigh(SleighArch.x86_64(), BufMemReader(code, 0))
res = sleigh.lift_one(0)

# print the lifted p-code
print(res.fmt_insns(sleigh))

this will output the following raw p-code. note that this is a direct, unsimplified representation of the instruction's operations. the exact addresses and unique ids may vary.

COPY unique[162944]:8, RAX
INT_SUB RSP, RSP, 0x8:8
STORE 0x1e68f670:8, RSP, unique[162944]:8

an example of lifting a mips instruction:

from psleigh import Sleigh, SleighArch, BufMemReader, Opcode

# sw $ra, 0x10($sp)
code = bytes.fromhex("af bf 00 10")

sleigh = Sleigh(SleighArch.mips32be(), BufMemReader(code, 0))
res = sleigh.lift_one(0)

# print the lifted p-code
print(res.fmt_insns(sleigh))

this will output the following raw p-code. the exact addresses and unique ids may vary.

INT_ADD unique[256]:4, sp, 0x10:4
COPY unique[384]:4, 0x0:4
COPY unique[384]:4, unique[256]:4
STORE 0x20339f70:8, unique[384]:4, ra

you can also manually construct all of the pcode related objects, which makes it easier to work with them. for example, you can construct an Insn object and compare it to the results of a lift operation:

from psleigh import Sleigh, SleighArch, BufMemReader, Opcode, Insn, Vn, VnAddr, VnSpace

# li $v0, 1
code = bytes.fromhex("24 02 00 01")

reader = BufMemReader(code, 0)
sleigh = Sleigh(SleighArch.mips32be(), reader)
res = sleigh.lift_one(0)

# manually construct the expected Insn
expected_insn = Insn(
    opcode=Opcode.COPY,
    inputs=[Vn(addr=VnAddr(off=1, space=VnSpace.const()), size=4)],
    output=Vn(addr=VnAddr(off=8, space=VnSpace.register()), size=4),
)

# compare the lifted instruction to the expected one
assert res.insns[0] == expected_insn

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psleigh-0.5.0.tar.gz (18.3 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

psleigh-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl (16.0 MB view details)

Uploaded CPython 3.12musllinux: musl 1.1+ x86-64

psleigh-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

File details

Details for the file psleigh-0.5.0.tar.gz.

File metadata

  • Download URL: psleigh-0.5.0.tar.gz
  • Upload date:
  • Size: 18.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for psleigh-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d94bfaf26474343c93d25a2fa139d7e9eebc1e17118bd026fa12036cddec4d36
MD5 8a9d64d86e36a0aeccb5ef04f6e572fd
BLAKE2b-256 b1daf63c1aa85d24996a9b90a6c8e798272d72c9f70a9b9dd68c3ee48055552e

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.5.0.tar.gz:

Publisher: publish.yml on roeeshoshani/psleigh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file psleigh-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for psleigh-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2ab8f3eea3cbac0f4ac29a73abd6a8c954283ade027f00adee66317b8bea3c62
MD5 b0acd5d4b5d7bf8789f6a812182c8668
BLAKE2b-256 d56369de184edc1ab04bc3fcd0a8873ef059d7b5f4ea8f5518be320bf31c9f6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.5.0-cp312-cp312-musllinux_1_1_x86_64.whl:

Publisher: publish.yml on roeeshoshani/psleigh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file psleigh-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psleigh-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ac50f7db77d4ba53a34e4ea4cf523e66afe16372b3528b9d604dd77cf0fd6f47
MD5 7410944bb3873159499ad4263b6c76cb
BLAKE2b-256 ce367101397b39e63523eb6fa3607f0f816d2cdecb61c2e599f65f0be95cf5b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on roeeshoshani/psleigh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page