Skip to main content

No project description provided

Project description

Ghidra .pyi Generator

The Ghidra .pyi Generator generates .pyi type stubs for the entire Ghidra API. Those stub files can later be used in PyCharm to enhance the development experience.

You can either use the stubs released here, or follow the instructions below to generate them yourself.

Using The Stubs

Installation

The release contains PEP 561 stub package, which can simply be installed with pip install ghidra-stubs*.whl into the environment in which the real ghidra module is available. Any conformant tool will then use the stub package for type analysis purposes.

If you want to manually add the stub files to PyCharm, follow the instructions in Install, uninstall, and upgrade interpreter paths.

Usage

Once installed, all you need to do is import the Ghidra modules as usual, and PyCharm will do the rest.

import ghidra

To get support for the Ghidra builtins, you need to import them as well. The type hints for those exist in the generated ghidra_builtins.pyi stub. Since it is not a real Python module, importing it at runtime will fail. But the .pyi gives PyCharm all the information it needs to help you.

try:
    from ghidra.ghidra_builtins import *
except:
    pass

If you are using ghidra_bridge from a Python 3 environment where no real ghidra module exists you can use a snippet like the following:

import typing
if typing.TYPE_CHECKING:
    import ghidra
    from ghidra.ghidra_builtins import *
else:
    b = ghidra_bridge.GhidraBridge(namespace=globals())

# actual code follows here

typing.TYPE_CHECKING is a special value that is always False at runtime but True during any kind of type checking or completion.

Once done, just code & enjoy.

Pycharm Demo

Dependencies

Ghidra Docs

To properly extract all types from Ghidra, make sure to extract the API documentation.

  1. Open the Ghidra CodeBrowser
  2. Go to Help -> Ghidra API Help
  3. Wait for Ghidra to extract the docs

Python Packages

The script depends on both the attr and typing packages. They are now vendored under the vendor directory as Python2.7 support is gradually being dropped from the ecosystem, making it hard to install and fetch packages.

 
# Create Jython's site-pacakges directory.
jython_site_packages=~/.local/lib/jython2.7/site-packages
mkdir -p $jython_site_packages
 
# Create a PTH file to point Jython to our vendored site-packages

# Outside a virtualenv, use
echo "$(realpath ./vendor)" > $jython_site_packages/python.pth

Creating the .pyi files

GUI

  1. Add this directory to the Script Directories in the Ghidra Script Manager
  2. Refresh the script list
  3. Run generate_ghidra_pyi.py (will be located under IDE Helpers)
  4. When a directory-selection dialog appears, choose the directory you'd like to save the .pyi files in.

CLI

$GHIDRA_ROOT/support/analyzeHeadless /tmp tmp -scriptPath $(pwd) -preScript generate_ghidra_pyi.py ./

Python Package

generate_ghidra_pyi.py generates a setup.py inside the directory that was selected.

This allows using pip install to install a PEP 561 stub package that is recognized by PyCharm and other tools as containing type information for the ghidra module.

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

ghidra-stubs-11.0.2.1.0.4.tar.gz (4.5 MB view details)

Uploaded Source

Built Distribution

ghidra_stubs-11.0.2.1.0.4-py2.py3-none-any.whl (11.2 MB view details)

Uploaded Python 2 Python 3

File details

Details for the file ghidra-stubs-11.0.2.1.0.4.tar.gz.

File metadata

  • Download URL: ghidra-stubs-11.0.2.1.0.4.tar.gz
  • Upload date:
  • Size: 4.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for ghidra-stubs-11.0.2.1.0.4.tar.gz
Algorithm Hash digest
SHA256 ce3cc03ee869540f8f9677e71d3a2df0aa47fd944a3d35f48f851991dea75756
MD5 4fecb80ed132fcb5fa028a7bb6b55979
BLAKE2b-256 d79d7aed1428b72391214a7b4bdc4ddeb51e23604726dfe061a15227091bbacf

See more details on using hashes here.

File details

Details for the file ghidra_stubs-11.0.2.1.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ghidra_stubs-11.0.2.1.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ca5dfb665a44a5ae8447178f5930e20fb577906db90b5f6c2b52319137a5fcd0
MD5 af8a356fd192c07b2c4a2c56f325cc90
BLAKE2b-256 a4a4ec6eabab44be44c8eb0adc5bba1f0e91ca1495a294a0497b3fc63be44a2f

See more details on using hashes here.

Supported by

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