backtrip 0.1.4

A decompiler in Rust — turns .NET assemblies (.dll/.exe) into C# and JVM class files (.class) into Java, with IL/javap-style disassembly and round-trip verification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use clap::Parser;
use std::path::PathBuf;
use backtrip::{cil, decompile, error, java, metadata, output, pe};

/// backtrip - a .NET IL / JVM bytecode decompiler and disassembler in Rust.
#[derive(Parser, Debug)]
#[command(name = "backtrip", version, about)]
struct Cli {
    /// Path to the .NET assembly (.dll/.exe), Java class file (.class), or a
    /// directory when --recursive is used.
    assembly: PathBuf,

    /// Output directory for decompiled files.
    #[arg(short, long, default_value = "decompiled")]
    output: PathBuf,

    /// Emit CIL disassembly (ildasm-style) for .NET inputs, or javap-style
    /// bytecode disassembly for Java class files.
    #[arg(long)]
    il: bool,

    /// List types in the input and exit.
    #[arg(long)]
    list: bool,

    /// Decompile only the type whose simple or fully-qualified name matches.
    #[arg(long = "type", name = "type")]
    type_name: Option<String>,

    /// Print the matched type to stdout instead of writing files
    /// (requires --type).
    #[arg(long)]
    stdout: bool,

    /// Recursively decompile all .dll/.exe/.class files in a directory.
    #[arg(long)]
    recursive: bool,

    /// Emit a JSON model of the assembly (types/methods/fields) to stdout.
    #[arg(long)]
    json: bool,

    /// Detect obfuscation indicators (non-printable names, control-flow
    /// flattening, string encryption) and emit warnings.
    #[arg(long)]
    detect_obfuscation: bool,

    /// Verify decompiled output against metadata (check all types/methods/
    /// fields appear in the output).
    #[arg(long)]
    verify: bool,
}

fn main() {
    if let Err(e) = run() {
        eprintln!("error: {e}");
        std::process::exit(1);
    }
}

fn run() -> error::Result<()> {
    let cli = Cli::parse();

    // Recursive mode: decompile all .dll/.exe in a directory.
    if cli.recursive {
        return run_recursive(&cli);
    }

    decompile_one(&cli)
}

/// Decompile a single input file (.NET assembly, Java class file, or jar).
/// The format is detected from the magic bytes: `MZ` → .NET PE,
/// `0xCAFEBABE` → JVM class file, `PK\x03\x04` → jar archive.
fn decompile_one(cli: &Cli) -> error::Result<()> {
    let data = std::fs::read(&cli.assembly)?;
    if data.len() >= 4
        && (data[..4] == [0xCA, 0xFE, 0xBA, 0xBE] || data[..4] == [0x50, 0x4B, 0x03, 0x04])
    {
        return decompile_java(cli, &data);
    }
    let pe = pe::PeImage::parse(data)?;
    let (root, tables) = metadata::load(&pe)?;
    let reader = metadata::Reader::new(&pe, &root, &tables)?;

    if cli.list {
        list_types(&reader)?;
        return Ok(());
    }

    if cli.json {
        let json = decompile::json::assembly_to_json(&reader)?;
        print!("{json}");
        return Ok(());
    }

    if cli.detect_obfuscation {
        let warnings = decompile::obfuscation::detect_obfuscation(&reader)?;
        print!("{}", decompile::obfuscation::format_warnings(&warnings));
        return Ok(());
    }

    if cli.verify {
        let types = decompile::decompile_assembly(&reader)?;
        let results = decompile::verify::verify(&reader, &types)?;
        print!("{}", decompile::verify::format_results(&results));
        return Ok(());
    }

    if cli.stdout && cli.type_name.is_none() {
        return Err(error::Error::Usage(
            "--stdout requires --type <NAME>".into(),
        ));
    }

    if cli.il {
        let types = decompile_il(&reader, cli.type_name.as_deref())?;
        if types.is_empty() {
            return Err(error::Error::NotFound(format!(
                "no type matching '{}'",
                cli.type_name.as_deref().unwrap_or("")
            )));
        }
        if cli.stdout {
            for t in &types {
                print!("{}", t.source);
            }
            return Ok(());
        }
        let n = output::write_types(&cli.output, &types)?;
        println!("Wrote {n} IL file(s) to {}", cli.output.display());
        return Ok(());
    }

    let types = if let Some(q) = cli.type_name.as_deref() {
        match decompile::decompile_type_by_name(&reader, q)? {
            Some(t) => vec![t],
            None => {
                return Err(error::Error::NotFound(format!(
                    "no type matching '{q}'"
                )));
            }
        }
    } else {
        decompile::decompile_assembly(&reader)?
    };
    if cli.stdout {
        for t in &types {
            print!("{}", t.source);
        }
        return Ok(());
    }
    let n = output::write_types(&cli.output, &types)?;
    println!("Wrote {n} file(s) to {}", cli.output.display());
    Ok(())
}

/// Decompile a JVM class file: Java source (default) or javap-style
/// disassembly (--il). Mirrors the .NET CLI surface where applicable.
fn decompile_java(cli: &Cli, data: &[u8]) -> error::Result<()> {
    let is_jar = data.len() >= 4 && data[..4] == [0x50, 0x4B, 0x03, 0x04];

    if cli.list {
        if is_jar {
            for name in decompile::java::jar_class_names(data)? {
                println!("{name}");
            }
        } else {
            println!("{}", decompile::java::list_name(data)?);
        }
        return Ok(());
    }
    if cli.json {
        return Err(error::Error::Usage(
            "--json is not supported for Java class files".into(),
        ));
    }
    if cli.detect_obfuscation {
        return Err(error::Error::Usage(
            "--detect-obfuscation is not supported for Java class files".into(),
        ));
    }
    if cli.verify {
        return Err(error::Error::Usage(
            "--verify is not supported for Java class files".into(),
        ));
    }
    // A class file contains exactly one class, so --stdout works without
    // --type here (unlike assemblies, where --type selects among many).

    // Name filter: match the simple or fully-qualified class name.
    let matches_filter = |name: &str, pkg: &str| -> bool {
        match cli.type_name.as_deref() {
            None => true,
            Some(q) => name == q || format!("{pkg}.{name}") == q,
        }
    };

    // Parse every class input into (classfile, source) pairs. Broken jar
    // entries are skipped instead of failing the whole archive.
    let mut units: Vec<(java::ClassFile, decompile::DecompiledType)> = Vec::new();
    if is_jar {
        for entry in java::jar::read_entries(data)? {
            if !entry.name.ends_with(".class") {
                continue;
            }
            if let Ok(cf) = java::ClassFile::parse(&entry.data)
                && let Ok(t) = decompile::java::decompile_class(&cf) {
                    units.push((cf, t));
                }
        }
    } else {
        let cf = java::ClassFile::parse(data)?;
        let t = decompile::java::decompile_class_file(data)?;
        units.push((cf, t));
    }

    if cli.il {
        let mut types = Vec::new();
        for (cf, _) in &units {
            if !matches_filter(&cf.simple_name(), &cf.package()) {
                continue;
            }
            let source = java::disasm::disassemble_class(cf)?;
            types.push(decompile::DecompiledType {
                file_name: java_disasm_file_name(cf),
                source,
            });
        }
        if types.is_empty() {
            return Err(error::Error::NotFound(format!(
                "no class matching '{}'",
                cli.type_name.as_deref().unwrap_or("")
            )));
        }
        if cli.stdout {
            for t in &types {
                print!("{}", t.source);
            }
            return Ok(());
        }
        let n = output::write_types(&cli.output, &types)?;
        println!("Wrote {n} disassembly file(s) to {}", cli.output.display());
        return Ok(());
    }

    let types: Vec<decompile::DecompiledType> = units
        .iter()
        .filter(|(cf, _)| matches_filter(&cf.simple_name(), &cf.package()))
        .map(|(_, t)| t.clone())
        .collect();
    if types.is_empty() {
        return Err(error::Error::NotFound(format!(
            "no class matching '{}'",
            cli.type_name.as_deref().unwrap_or("")
        )));
    }
    if cli.stdout {
        for t in &types {
            print!("{}", t.source);
        }
        return Ok(());
    }
    let n = output::write_types(&cli.output, &types)?;
    println!("Wrote {n} file(s) to {}", cli.output.display());
    Ok(())
}

fn java_disasm_file_name(cf: &java::ClassFile) -> String {
    let simple = cf.simple_name();
    let pkg = cf.package();
    let stem = if pkg.is_empty() {
        simple
    } else {
        format!("{}_{}", pkg.replace('.', "_"), simple)
    };
    format!("{stem}.jbc")
}

/// Recursively find and decompile all .dll/.exe files in a directory.
fn run_recursive(cli: &Cli) -> error::Result<()> {
    if !cli.assembly.is_dir() {
        return Err(error::Error::Usage(
            "--recursive requires a directory path".into(),
        ));
    }

    // Collect all .dll and .exe files.
    let mut assemblies: Vec<PathBuf> = Vec::new();
    collect_assemblies(&cli.assembly, &mut assemblies)?;
    assemblies.sort();

    if assemblies.is_empty() {
        eprintln!("No .dll or .exe files found in {}", cli.assembly.display());
        return Ok(());
    }

    let total_files = 0;
    let mut total_assemblies = 0;
    let mut errors = 0;

    for asm_path in &assemblies {
        let rel = asm_path.strip_prefix(&cli.assembly).unwrap_or(asm_path);
        let stem = rel.with_extension("").to_string_lossy().replace('/', "_");
        let out_dir = cli.output.join(&stem);

        // Clone the CLI args with this assembly and output dir.
        let sub_cli = Cli {
            assembly: asm_path.clone(),
            output: out_dir,
            il: cli.il,
            list: false,
            type_name: cli.type_name.clone(),
            stdout: false,
            recursive: false,
            json: false,
            detect_obfuscation: false,
            verify: false,
        };

        match decompile_one(&sub_cli) {
            Ok(_) => {
                total_assemblies += 1;
            }
            Err(e) => {
                errors += 1;
                eprintln!("Failed to decompile {}: {e}", asm_path.display());
            }
        }
    }

    println!(
        "Decompiled {total_assemblies} assemblies ({} errors) to {}",
        errors,
        cli.output.display()
    );
    let _ = total_files;
    Ok(())
}

/// Recursively collect .dll and .exe files from a directory.
fn collect_assemblies(dir: &PathBuf, out: &mut Vec<PathBuf>) -> error::Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            collect_assemblies(&path, out)?;
        } else if let Some(ext) = path.extension() {
            let ext = ext.to_ascii_lowercase();
            if ext == "dll" || ext == "exe" || ext == "class" || ext == "jar" {
                out.push(path);
            }
        }
    }
    Ok(())
}

fn list_types(reader: &metadata::Reader<'_>) -> error::Result<()> {
    let type_defs = reader.tables.get(metadata::tbl::TYPEDEF);
    for (i, row) in type_defs.iter().enumerate() {
        let name = reader.type_def_name(row);
        if name == "<Module>" {
            continue;
        }
        let ns = reader.type_def_namespace(row);
        let row_idx = (i + 1) as u32;
        let nested = reader.nested_parent(row_idx).map(|p| {
            let parent = &reader.tables.get(metadata::tbl::TYPEDEF)[p as usize - 1];
            reader.type_def_name(parent)
        });
        if let Some(parent) = nested {
            println!("{parent}/{name}");
        } else if ns.is_empty() {
            println!("{name}");
        } else {
            println!("{ns}.{name}");
        }
    }
    Ok(())
}

/// Produce IL disassembly files, one per type. If `filter` is given, only the
/// type whose simple or fully-qualified name matches is included.
fn decompile_il(reader: &metadata::Reader<'_>, filter: Option<&str>) -> error::Result<Vec<decompile::DecompiledType>> {
    let mut out = Vec::new();
    let type_defs = reader.tables.get(metadata::tbl::TYPEDEF);
    for (i, row) in type_defs.iter().enumerate() {
        let row_idx = (i + 1) as u32;
        let name = reader.type_def_name(row);
        if name == "<Module>" {
            continue;
        }
        let ns = reader.type_def_namespace(row);
        if let Some(q) = filter {
            let full = if ns.is_empty() {
                name.clone()
            } else {
                format!("{ns}.{name}")
            };
            if name != q && full != q {
                continue;
            }
        }
        let source = il_for_type(reader, row_idx)?;
        let file_name = {
            let clean = name.replace(['`', '/'], "_");
            if ns.is_empty() {
                format!("{clean}.il")
            } else {
                format!("{}_{clean}.il", ns.replace('.', "_"))
            }
        };
        out.push(decompile::DecompiledType { file_name, source });
    }
    Ok(out)
}

fn il_for_type(reader: &metadata::Reader<'_>, row_idx: u32) -> error::Result<String> {
    let row = &reader.tables.get(metadata::tbl::TYPEDEF)[row_idx as usize - 1];
    let name = reader.type_def_name(row);
    let ns = reader.type_def_namespace(row);
    let mut s = String::new();
    s.push_str(&format!(".class {ns} {name}\n{{\n"));

    // Fields
    for fi in reader.type_field_rows(row_idx) {
        let f = &reader.tables.get(metadata::tbl::FIELD)[fi];
        let ftype = reader.field_type(f).map(|t| reader.type_name(&t)).unwrap_or_else(|_| "object".into());
        s.push_str(&format!("  .field {} {}\n", ftype, reader.field_name(f)));
    }

    // Methods
    for mi in reader.type_method_rows(row_idx) {
        let m = &reader.tables.get(metadata::tbl::METHODDEF)[mi];
        let mname = reader.method_name(m);
        let sig = reader.method_sig(m)?;
        let ret = reader.type_name(&sig.ret_type);
        let params: Vec<String> = sig.param_types.iter().enumerate().map(|(i, t)| {
            format!("{} arg{i}", reader.type_name(t))
        }).collect();
        s.push_str(&format!("  .method {} {}({})\n  {{\n", ret, mname, params.join(", ")));
        let rva = reader.method_rva(m);
        if let Some(body) = reader.method_body(rva)? {
            // Emit a .locals header when the method has locals.
            let locals = reader.local_types(body.local_token);
            if !locals.is_empty() {
                let decls: Vec<String> = locals.iter().enumerate()
                    .map(|(i, t)| format!("{} V_{i}", reader.type_name(t)))
                    .collect();
                s.push_str(&format!("    .locals init ({})\n", decls.join(", ")));
            }
            let il = cil::disasm::disassemble(reader, &body.code)?;
            for line in il.lines() {
                s.push_str(&format!("    {line}\n"));
            }
        } else {
            s.push_str("    // no body (abstract / extern / pinvoke)\n");
        }
        s.push_str("  }\n");
    }
    s.push_str("}\n");
    Ok(s)
}