Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

59 Commits

Folders and files

Repository files navigation

mmiomapper

Identify BSP (Board Support Package) functions in stripped microcontroller firmware by the memory-mapped I/O registers they touch, rather than by code-byte signatures.

Unlike byte-signature tools such as IDA's FLIRT or Ghidra's FunctionID — which depend on the exact compiler and options used to build the firmware — mmiomapper fingerprints a function by the hardware registers it reads, writes and read-modify-writes. Because a BSP function must access the registers of the peripheral it drives, this fingerprint survives recompilation, optimisation, stripping, and even a change of driver library (HAL, LL, bare-metal).

Developed as part of a Bachelor thesis at HEIG-VD with the Cyberdefence Campus. Status: research prototype (see Status).

How it works

The engine runs on top of Ghidra (via PyGhidra) in three stages:

  1. Extract — walk each function's P-code, track values symbolically, and record every LOAD/STORE to a peripheral address as a (register, mode) access. The CMSIS-SVD file of the target chip resolves raw addresses to names (USART.CR1, RCC.APB2ENR). Architecture-independent and non-speculative (no value is ever guessed, so there are no false positives).
  2. Learn — run the extractor on a firmware you compiled yourself with symbols to build a reference database of name -> signature pairs. Several reference firmwares (HAL, LL, bare-metal, different chips) can be accumulated into one database to recognise more functions.
  3. Identify — extract the signature of each unknown function in a stripped firmware and match it against the database. A confident match yields the function name; otherwise the function is still classified by role (e.g. "Init UART"), inferred purely from the registers and access modes.

Requirements

  • Ghidra 12.x, launched with PyGhidra (CPython 3)
  • Python 3.13+
  • A CMSIS-SVD file for the target microcontroller (a few are bundled in data/svd/)

The engine itself has no third-party runtime dependency (SVD parsing uses the standard library).

Installation

pip install -e .          # or: pip install -e ".[dev]" for the test extra

Usage

The command-line tools drive the engine head-less against a Ghidra project that already contains the imported, analysed program.

1. Build a reference database (from a firmware with symbols)

python scripts/build_db.py \
    --project-loc /path/to/ghidra_projects --project-name my_project \
    --program /reference.elf \
    --svd data/svd/STM32F103.svd --chip-family STM32F1 \
    --source hal-f1 --out data/signatures/STM32F1.json

Accumulate more references into the same database with --append:

python scripts/build_db.py ... --program /reference_ll.elf \
    --source ll-f1 --append data/signatures/STM32F1.json \
    --out data/signatures/STM32F1.json

2. Identify a stripped firmware

python scripts/annotate.py \
    --project-loc /path/to/ghidra_projects --project-name my_project \
    --program /unknown_stripped.bin \
    --svd data/svd/STM32L100.svd \
    --db data/signatures/STM32L1.json

This prints, per function, the inferred role, the best-matching reference name and a confidence score, e.g. 0x08006e5c -> Config horloge (HAL_RCC_ClockConfig, 0.67). Add --rename to write the names back into the Ghidra program.

3. Find a known function in a firmware (reverse search)

The reverse of annotate.py: given a reference function (e.g. HAL_RCC_ClockConfig), list the best-matching addresses in the target.

python scripts/search.py \
    --project-loc /path/to/ghidra_projects --project-name my_project \
    --program /unknown_stripped.bin \
    --svd data/svd/STM32L100.svd --db data/signatures/STM32L1.json \
    --function HAL_RCC_ClockConfig          # omit to search every reference function

Output: HAL_RCC_ClockConfig Config horloge -> 0x08006E5C (0.69), 0x0800262C (0.69), ...

4. Classify by role without any database

python scripts/classify_run.py \
    --project-loc /path/to/ghidra_projects --project-name my_project \
    --program /unknown_stripped.bin --svd data/svd/STM32L100.svd

5. From the Ghidra GUI

Ghidra must be started with PyGhidra (CPython 3) for .py scripts to run. Then:

  1. In the Script Manager, Manage Script Directories → add this repo's ghidra_scripts/.
  2. Open and analyse the target program.
  3. Run run_mmiomapper.py. It asks for the CMSIS-SVD file for the chip (e.g. one from data/svd/) and, optionally, a reference database (data/signatures/*.json). It then, for each function, prints the identified BSP role / name and the MMIO accesses with their decoded write bit-fields (e.g. RCC.APB2ENR (USART1EN)) to the Console window, reports the run time, and offers to annotate the program: every function gets a comment, and the confident reference matches (score >= 0.6) are renamed. Lower-confidence guesses are shown marked (?) but keep their FUN_ name, to avoid asserting a misleading name.

(Open the Console window if you don't see the output. The console also shows a summary pop-up with the function count and elapsed time.)

Project structure

mmiomapper/        Python package — the analysis library
  peripherals.py   PeripheralMap: address -> PERIPH.REG resolution
  svd.py           CMSIS-SVD loader (single file or merged), builds a PeripheralMap
  parser.py        Hybrid Low/High P-code MMIO signature extractor (the engine)
  signatures.py    Signature database: build, accumulate, role labels, IDF stats
  scoring.py       Similarity scoring (IDF-weighted cosine + sequence) and matching
  classify.py      Unsupervised role classifier (RMW = config) used as a fallback
scripts/           Tool entry points (build a DB, annotate, search, classify, run head-less)
ghidra_scripts/    Driver to run the engine from the Ghidra GUI Script Manager
experiments/       Validation/measurement scripts for the thesis (not the tool)
tests/             Pure-Python unit tests (no Ghidra needed)
data/svd/          Bundled CMSIS-SVD files
data/signatures/   Reference signature databases

Testing

The analysis logic is covered by pure-Python unit tests that need neither Ghidra nor a target firmware:

pytest tests/

Status

Research prototype. The extraction engine and the identification pipeline (database, scoring, role classification) are implemented and validated on STM32 firmware across optimisation levels, compilers (GCC/Clang) and driver implementations (HAL/LL/bare). Cross-architecture validation (RISC-V, AVR) and broader reference coverage are planned. This is not a final release.

License

To be defined.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages