Implementation:2akouwu Reverify parse binary
| Knowledge Sources | |
|---|---|
| Domains | Binary_Analysis, Parsing |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for parsing PE, ELF, and Mach-O binaries into a unified BinaryInfo structure provided by the Reverify library.
Description
The parse_binary function is the entry point for all binary analysis in Reverify. It takes raw bytes and returns a BinaryInfo dataclass with format (PE/ELF/MachO/raw), architecture, bits, entrypoint, image_base, sections, imports, exports, and backend (lief or pure-python). It uses LIEF when available (richer parsing) and falls back to pure-Python parsers for PE and ELF that have zero dependencies, so the tool works even with no optional engines installed. Mach-O requires LIEF.
Usage
Import this function when you need to:
- Parse a binary into a structured view for the fact sheet (used by
binary_facts). - Resolve offsets (file ↔ RVA ↔ VA) for claim verification.
- Inspect sections, imports, and exports in the CLI or MCP server.
Code Reference
Source Location
- Repository: Reverify
- File: reverify/binary.py
- Lines: L312-344 (entry point); pure-Python PE parser in
_parse_pe_pure; ELF parser in_parse_elf_pure
Signature
def parse_binary(data: bytes, prefer: str = "auto") -> BinaryInfo:
"""Parse ``data`` into a BinaryInfo.
``prefer`` is "auto" (lief when available), "lief" (require it) or
"pure" (force the pure-Python fallbacks).
"""
The returned BinaryInfo dataclass fields:
@dataclass
class BinaryInfo:
format: str # "PE", "ELF", "MachO", "raw"
arch: str # "x86", "x86_64", "arm", "arm64", "unknown"
bits: int # 32 or 64
entrypoint: Optional[int]
image_base: Optional[int]
sections: List[SectionInfo] # name, offset, virtual_address, raw_size, virtual_size
imports: Dict[str, List[str]] # lib_name -> [function_names]
exports: List[str]
export_rvas: Dict[str, int] # export name -> RVA
backend: str # "lief" or "pure-python"
error: Optional[str] # set when parsing degraded
Import
from reverify.binary import parse_binary
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | bytes | Yes | Raw binary file bytes |
| prefer | str | No | Parsing engine: "auto" (lief when available), "lief" (require it), "pure" (force pure-Python). Default "auto". |
Outputs
| Name | Type | Description |
|---|---|---|
| return | BinaryInfo | Parsed binary info dataclass |
Usage Examples
Basic Parsing
from reverify.binary import parse_binary
with open("kernel32.dll", "rb") as f:
data = f.read()
info = parse_binary(data)
print(f"Format: {info.format}, Arch: {info.arch}, Bits: {info.bits}")
print(f"Entrypoint: {hex(info.entrypoint) if info.entrypoint else 'N/A'}")
print(f"Sections: {[s.name for s in info.sections]}")
print(f"Imports: {list(info.imports.keys())}")
Force Pure-Python Parsing
# No LIEF required; works with zero optional dependencies
info = parse_binary(data, prefer="pure")
Handle Malformed Input
# Malformed input degrades gracefully — never crashes
info = parse_binary(garbage_bytes)
if info.error:
print(f"Parsing degraded: {info.error}")
# info.format is "raw" or the best-guess format with error set