Hermes 字节码反汇编器和汇编器
A nearly dependency-free disassembler and assembler for the Hermes bytecode, written in Rust.
Note: Still a WIP - A PR is always welcome. The API is subject to change as I iterate over use cases and improve the design.
For the sake transparency, the current dependencies are:
sha1 serde - Optional specta - Optional specta-util - Optional A special thanks to P1sec for digging through the Hermes git repo, pulling all of the BytecodeDef files out, and tagging them. This made writing this tool much easier.
I created and launched Bytecode Studio - a comprehensive tool built for reverse engineering, analyzing, disassembling, and decompiling React Native applications and Hermes binaries.
If you're using hermes_rs or any other tool for static analysis for an engagement or want a more comprehensive analysis and workflow, I'd use Bytecode Studio!
| HBC Version | Disassembler | (Binary) Assembler | (Textual) Assembler | Decompiler |
|---|---|---|---|---|
| 76 | ✅ | ✅ | ❌ | ❌ |
| 89 | ✅ | ✅ | ❌ | ❌ |
| 90 | ✅ | ✅ | ❌ | ❌ |
| 93 | ✅ | ✅ | ❌ | ❌ |
| 94 | ✅ | ✅ | ❌ | ❌ |
| 95 | ✅ | ✅ | ❌ | ❌ |
| 96 | ✅ | ✅ | ❌ | ❌ |
A couple of features are missing currently, as they're low priority for me at the moment.
* Supports u8 buffer for manual population
Chances are you need to compile the program that uses this crate with the --release flag and run the binary directly.
The size of a binary produced with hermes_rs is going to be a bit larger than one compiled directly with hermes. This is primarily due to the fact that hermes does some string merging to keep bundles down.
Example: If car and racecar are both strings within the app, only racecar will be written. Two StringTableEntry objects are created: Entry { offset: 0, length: 7 } and Entry { offset: 4, length: 3 }.
As of right now, hermes_rs does not implement this functionality. It's pretty literal when it comes to strings, and doesn't try to be clever or save space.
cargo run --bin bytecode index.android.bundlecargo run --bin strings index.android.bundleTo add hermes_rs to your project, simply run:
cargo add hermes_rs
["v89","v90","v93","v94","v95", "v96"] ["v94","v95", "v96"]serde feature specta feature cargo run --bin gen_ts --features specta will output *.d.ts to ./ts. specta.Dumps the entire applications bytecode - definitely direct it to a file since it's probably going to be huge.
cargo run --bin bytecode ./path/to/file/index.android.bundle > bytecode_ouput.txtDumps all string definitions
cargo run --bin strings ./path/to/file/index.android.bundle > strings_output.txtMost React Native applications use the Metro bundler as a pre-Hermes compilation step. This utility finds the bundler function and where it's used, then outputs JavaScript-like representation of calls to it.
cargo run --bin modules ./path/to/file/index.android.bundle > output.txtExample output:
…Parses and dumps the contents of the Array storage buffer within the bundle in a JavaScript-like representation.
cargo run --bin arrays ./path/to/file/index.android.bundle > output.txtExample Output:
…Dump the raw object keys and values from an application bundle.
Note: The object keys and values aren't joined with this utility,
as they're correlated by individual indexes with the NewObjectWithBuffer or NewObjectWithBufferLong instructions.
To see proper Object key/value pairs, use the Dump Bytecode utility. When you disassemble the binary
with this utility, it'll print out the full object next to the two instructions above in the output.
cargo run --bin objects ./path/to/file/index.android.bundle > output.txtExample Output:
const objkeys_7 = ["trace", "info", "warn"];
const objkeys_9 = ["error"];
const objkeys_12 = ["value", "enumerable"];
const objkeys_16 = ["message", "title", "color"];
const objkeys_19 = ["channelId"];
const objkeys_22 = ["value", "enumerable"];
const objkeys_27 = ["configurable", "writable"];
....
const objvals_17 = [0, 1, 2, 3];
const objvals_18 = [true];
const objvals_19 = [false];
const objvals_24 = ["You have new messages", "Kustomer Support"];
const objvals_25 = [null];
Special thanks to AbhiTheModder for this one!
Generate a r2 script for a hermes binary to take advantage of all of the amazing features r2 has to offer.
cargo run --bin r2_script ./path/to/file/index.android.bundle > my_script.r2Example Output:
# R2 script for Hermes bytecode file: ./index.android.bundle
# Generated by hermes_rs
# Hermes File Version: 84
# File Size: 5471568 bytes
# Function Count: 27835
f hbc.header=0x0
f hbc.header.size=0x537d50
# Function flags
'f func.hermes.global 32 0x1cb368
'f func.hermes.func_1 16 0x1dd8b4
'f func.hermes.clear 16 0x1dd9b1
'f func.hermes.define 16 0x1dd9ce
'f func.hermes.metroRequire 16 0x1dda2b
...
# String flags
'f str.API_STATUS_0 10 0x28
'f str.ActionCreator_1 13 0x17a
'f str.Actions_2 7 0x1c9
'f str.Animated_3 8 0x2ac
'f str.d_4 1 0x2b3
... let filename = "./input_data/index.android.bundle";
let f = File::open(filename).expect("no file found");
let mut reader = io::BufReader::new(f);
let mut hermes_file = HermesFile::deserialize(&mut reader);
println!("{:?}", hermes_file.header);Output:
…println!("Strings: {:?}", hermes_file.get_strings());Output:
Strings: ["$$typeof", "type", "API", "isArray", "Array", ... ]…Single Function
Call print_bytecode_for_fn(fidx), where fidx is the function index of the element in hermes_file.function_headers.
hermes_file.parse_bytecode_for_fn(1337);Output:
Function(3 params, 11 registers, 0 symbols):
0x00000000 GetEnvironment r0, 0
0x00000001 LoadFromEnvironment r2, r0, 6
0x00000002 LoadConstUndefined r0
0x00000003 LoadParam r1, 1
0x00000004 Call2 r2, r2, r0, r1
0x00000005 LoadConstNull r1
0x00000006 PutById r2, r1, 1, "currentTarget"
0x00000007 Ret r0Entire File
Printing out the bytecode for the entire executable in a human-readable format is possible by calling print_bytecode.
hermes_file.print_bytecode();Which outputs:
…Raw Bytes
In the event that you want to access just the raw bytes for a specific function, you can use hermes_file.get_bytecode() and iterate.
The function returns a Vec, which has the function index and bytecode (Vec) pairing.
let bc = hermes_file.get_bytecode();
for func in bc {
println!("{:?}", func);
}Output:
…Encoding instructions is trivial - each Instruction implements a trait with deserialize and serialize methods.
…Take a look at the Creating Binaries example.
Working example: cargo run --example create
Want to use a specific version of the Hermes bytecode and reduce your binary size?
In Cargo.toml, find the hermes_rs dependency and select which HBC version(s) you'd like to use in your application.
Example:
[dependencies]
hermes_rs = { features = ["v89", "v90", "v93", "v94", "v95", "v96"] }My Stuff
Other Resources
暂无开放 Issues,或尚未同步最近议题。