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.4.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.4.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.4.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.4.0.tar.gz.

File metadata

  • Download URL: psleigh-0.4.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.4.0.tar.gz
Algorithm Hash digest
SHA256 54f4edf57294b950ac09fe226202b976381edd80bab03d23f7840240e19fc40e
MD5 33aa6e728f3664df76a8a3d301664f13
BLAKE2b-256 70b19552df23aab8c1d4472e8e3ff296ac6dcad7100ec103a57ba6f2a23a5cb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.4.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.4.0-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for psleigh-0.4.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1e62c6b33bee5618515876f5630151c5d575c580b9bb60c989ac9e6d7a383332
MD5 c1f09c3a4010cca3ae698a3b38504317
BLAKE2b-256 5ff08883c2810cbccf5d4d4b7828504483727c39479617de3e69e96eca05a839

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.4.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.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psleigh-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3229ae66ee9205a9326811062c1af5fe15675bea7ba9c4cfe93197a80f860de9
MD5 6fb7ce606e7f3e05220d7cdaf8618a30
BLAKE2b-256 bd31f0fc97c2e2e8c49e25df25fb986b95c83e23bdc67e835e4bc7e6076e5865

See more details on using hashes here.

Provenance

The following attestation bundles were made for psleigh-0.4.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