/
mod.rs
672 lines (589 loc) · 24.1 KB
/
mod.rs
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
mod tests;
pub mod util;
pub mod output;
pub mod analyze;
pub mod resolve;
pub mod constants;
pub mod precompile;
pub mod postprocess;
use crate::decompile::util::*;
use crate::decompile::output::*;
use crate::decompile::resolve::*;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::time::Duration;
use indicatif::ProgressBar;
use clap::{AppSettings, Parser};
use ethers::{
core::types::{Address},
providers::{Middleware, Provider, Http},
};
use heimdall_common::{
ether::evm::{
disassemble::{
DisassemblerArgs,
disassemble
},
vm::VM
},
ether::signatures::*,
constants::{ ADDRESS_REGEX, BYTECODE_REGEX },
io::{ logging::* },
};
#[derive(Debug, Clone, Parser)]
#[clap(about = "Decompile EVM bytecode to Solidity",
after_help = "For more information, read the wiki: https://jbecker.dev/r/heimdall-rs/wiki",
global_setting = AppSettings::DeriveDisplayOrder,
override_usage = "heimdall decompile <TARGET> [OPTIONS]")]
pub struct DecompilerArgs {
/// The target to decompile, either a file, bytecode, contract address, or ENS name.
#[clap(required=true)]
pub target: String,
/// Set the output verbosity level, 1 - 5.
#[clap(flatten)]
pub verbose: clap_verbosity_flag::Verbosity,
/// The output directory to write the decompiled files to
#[clap(long="output", short, default_value = "", hide_default_value = true)]
pub output: String,
/// The RPC provider to use for fetching target bytecode.
#[clap(long="rpc-url", short, default_value = "", hide_default_value = true)]
pub rpc_url: String,
/// When prompted, always select the default value.
#[clap(long, short)]
pub default: bool,
/// Whether to skip resolving function selectors.
#[clap(long="skip-resolving")]
pub skip_resolving: bool,
/// Whether to include solidity source code in the output (in beta).
#[clap(long="include-sol")]
pub include_solidity: bool,
}
pub fn decompile(args: DecompilerArgs) {
use std::time::Instant;
let now = Instant::now();
let (logger, mut trace)= Logger::new(args.verbose.log_level().unwrap().as_str());
let mut all_resolved_events: HashMap<String, ResolvedLog> = HashMap::new();
let mut all_resolved_errors: HashMap<String, ResolvedError> = HashMap::new();
// truncate target for prettier display
let mut shortened_target = args.target.clone();
if shortened_target.len() > 66 {
shortened_target = shortened_target.chars().take(66).collect::<String>() + "..." + &shortened_target.chars().skip(shortened_target.len() - 16).collect::<String>();
}
let decompile_call = trace.add_call(
0, line!(),
"heimdall".to_string(),
"decompile".to_string(),
vec![shortened_target],
"()".to_string()
);
// parse the output directory
let mut output_dir: String;
if &args.output.len() <= &0 {
output_dir = match env::current_dir() {
Ok(dir) => dir.into_os_string().into_string().unwrap(),
Err(_) => {
logger.error("failed to get current directory.");
std::process::exit(1);
}
};
output_dir.push_str("/output");
}
else {
output_dir = args.output.clone();
}
let contract_bytecode: String;
if ADDRESS_REGEX.is_match(&args.target).unwrap() {
// push the address to the output directory
if &output_dir != &args.output {
output_dir.push_str(&format!("/{}", &args.target));
}
// create new runtime block
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
// We are decompiling a contract address, so we need to fetch the bytecode from the RPC provider.
contract_bytecode = rt.block_on(async {
// make sure the RPC provider isn't empty
if &args.rpc_url.len() <= &0 {
logger.error("decompiling an on-chain contract requires an RPC provider. Use `heimdall decompile --help` for more information.");
std::process::exit(1);
}
// create new provider
let provider = match Provider::<Http>::try_from(&args.rpc_url) {
Ok(provider) => provider,
Err(_) => {
logger.error(&format!("failed to connect to RPC provider '{}' .", &args.rpc_url).to_string());
std::process::exit(1)
}
};
// safely unwrap the address
let address = match args.target.parse::<Address>() {
Ok(address) => address,
Err(_) => {
logger.error(&format!("failed to parse address '{}' .", &args.target).to_string());
std::process::exit(1)
}
};
// fetch the bytecode at the address
let bytecode_as_bytes = match provider.get_code(address, None).await {
Ok(bytecode) => bytecode,
Err(_) => {
logger.error(&format!("failed to fetch bytecode from '{}' .", &args.target).to_string());
std::process::exit(1)
}
};
return bytecode_as_bytes.to_string().replacen("0x", "", 1);
});
}
else if BYTECODE_REGEX.is_match(&args.target).unwrap() {
contract_bytecode = args.target.clone();
}
else {
// push the address to the output directory
if &output_dir != &args.output {
output_dir.push_str("/local");
}
// We are decompiling a file, so we need to read the bytecode from the file.
contract_bytecode = match fs::read_to_string(&args.target) {
Ok(contents) => {
if BYTECODE_REGEX.is_match(&contents).unwrap() && contents.len() % 2 == 0 {
contents.replacen("0x", "", 1)
}
else {
logger.error(&format!("file '{}' doesn't contain valid bytecode.", &args.target).to_string());
std::process::exit(1)
}
},
Err(_) => {
logger.error(&format!("failed to open file '{}' .", &args.target).to_string());
std::process::exit(1)
}
};
}
// disassemble the bytecode
let disassembled_bytecode = disassemble(DisassemblerArgs {
target: contract_bytecode.clone(),
default: args.default.clone(),
verbose: args.verbose.clone(),
output: output_dir.clone(),
rpc_url: args.rpc_url.clone(),
});
trace.add_call(
decompile_call,
line!(),
"heimdall".to_string(),
"disassemble".to_string(),
vec![format!("{} bytes", contract_bytecode.len()/2usize)],
"()".to_string()
);
// perform versioning and compiler heuristics
let (compiler, version) = detect_compiler(contract_bytecode.clone());
trace.add_call(
decompile_call,
line!(),
"heimdall".to_string(),
"detect_compiler".to_string(),
vec![format!("{} bytes", contract_bytecode.len()/2usize)],
format!("({}, {})", compiler, version)
);
if compiler == "solc" {
logger.debug(&format!("detected compiler {} {}.", compiler, version));
}
else {
logger.warn(&format!("detected compiler {} {} is not supported by heimdall.", compiler, version));
}
// create a new EVM instance
let evm = VM::new(
contract_bytecode.clone(),
String::from("0x"),
String::from("0x6865696d64616c6c000000000061646472657373"),
String::from("0x6865696d64616c6c0000000000006f726967696e"),
String::from("0x6865696d64616c6c00000000000063616c6c6572"),
0,
u128::max_value(),
);
let mut shortened_target = contract_bytecode.clone();
if shortened_target.len() > 66 {
shortened_target = shortened_target.chars().take(66).collect::<String>() + "..." + &shortened_target.chars().skip(shortened_target.len() - 16).collect::<String>();
}
let vm_trace = trace.add_creation(decompile_call, line!(), "contract".to_string(), shortened_target, (contract_bytecode.len()/2usize).try_into().unwrap());
// find and resolve all selectors in the bytecode
let selectors = find_function_selectors(disassembled_bytecode);
let mut resolved_selectors = HashMap::new();
if !args.skip_resolving {
resolved_selectors = resolve_function_selectors(selectors.clone(), &logger);
logger.info(&format!("resolved {} possible functions from {} detected selectors.", resolved_selectors.len(), selectors.len()).to_string());
}
else {
logger.info(&format!("found {} possible function selectors.", selectors.len()).to_string());
}
logger.info(&format!("performing symbolic execution on '{}' .", &args.target).to_string());
let decompilation_progress = ProgressBar::new_spinner();
decompilation_progress.enable_steady_tick(Duration::from_millis(100));
decompilation_progress.set_style(logger.info_spinner());
// perform EVM analysis
let mut analyzed_functions = Vec::new();
for selector in selectors.clone() {
decompilation_progress.set_message(format!("executing '0x{}'", selector));
// get the function's entry point
let function_entry_point = resolve_entry_point(&evm.clone(), selector.clone());
if function_entry_point == 0 {
continue;
}
let func_analysis_trace = trace.add_call(
vm_trace,
line!(),
"heimdall".to_string(),
"analyze".to_string(),
vec![format!("0x{}", selector)],
"()".to_string()
);
trace.add_info(
func_analysis_trace,
function_entry_point.try_into().unwrap(),
format!("discovered entry point: {}", function_entry_point).to_string()
);
// get a map of possible jump destinations
let (map, jumpdests) = map_selector(&evm.clone(), selector.clone(), function_entry_point);
trace.add_debug(
func_analysis_trace,
function_entry_point.try_into().unwrap(),
format!("execution tree {}",
match jumpdests.len() {
0 => "appears to be linear".to_string(),
_ => format!("has {} branches", jumpdests.len()+1)
}
).to_string()
);
if jumpdests.len() >= 1000 {
trace.add_error(
func_analysis_trace,
function_entry_point.try_into().unwrap(),
format!("Execution tree truncated to {} branches", jumpdests.len()).to_string()
);
}
decompilation_progress.set_message(format!("analyzing '0x{}'", selector));
// solidify the execution tree
let mut analyzed_function = map.analyze(
Function {
selector: selector.clone(),
entry_point: function_entry_point.clone(),
arguments: HashMap::new(),
storage: HashMap::new(),
memory: HashMap::new(),
returns: None,
logic: Vec::new(),
events: HashMap::new(),
errors: HashMap::new(),
resolved_function: None,
indent_depth: 0,
notices: Vec::new(),
pure: true,
view: true,
payable: true,
},
&mut trace,
func_analysis_trace,
&mut Vec::new()
);
// add notice for long execution trees
if jumpdests.len() >= 1000 {
analyzed_function.notices.push(format!("execution tree truncated to {} branches", jumpdests.len()));
}
let argument_count = analyzed_function.arguments.len();
if argument_count != 0 {
let parameter_trace_parent = trace.add_debug(
func_analysis_trace,
line!(),
format!("discovered and analyzed {} function parameters", argument_count).to_string()
);
let mut parameter_vec = Vec::new();
for (_, value) in analyzed_function.arguments.clone() {
parameter_vec.push(value);
}
parameter_vec.sort_by(|a, b| a.0.slot.cmp(&b.0.slot));
for (frame, _) in parameter_vec {
trace.add_message(
parameter_trace_parent,
line!(),
vec![
format!(
"parameter {} {} {} bytes. {}",
frame.slot,
if frame.mask_size == 32 { "has size of" } else { "is masked to" },
frame.mask_size,
if frame.heuristics.len() > 0 {
format!("heuristics suggest param used as '{}'", frame.heuristics[0])
} else {
"".to_string()
}
).to_string()
]
);
}
}
if !args.skip_resolving {
let resolved_functions = match resolved_selectors.get(&selector) {
Some(func) => func.clone(),
None => {
trace.add_error(
func_analysis_trace,
line!(),
"failed to resolve function.".to_string()
);
continue;
}
};
let matched_resolved_functions = match_parameters(resolved_functions, &analyzed_function);
trace.br(func_analysis_trace);
if matched_resolved_functions.len() == 0 {
trace.add_warn(
func_analysis_trace,
line!(),
"no resolved signatures matched this function's parameters".to_string()
);
}
else {
let mut selected_function_index: u8 = 0;
if matched_resolved_functions.len() > 1 {
decompilation_progress.suspend(|| {
selected_function_index = logger.option(
"warn", "multiple possible matches found. select an option below",
matched_resolved_functions.iter()
.map(|x| x.signature.clone()).collect(),
Some(*&(matched_resolved_functions.len()-1) as u8),
args.default
);
});
}
let selected_match = match matched_resolved_functions.get(selected_function_index as usize) {
Some(selected_match) => selected_match,
None => {
logger.error("invalid selection.");
std::process::exit(1)
}
};
analyzed_function.resolved_function = Some(selected_match.clone());
let match_trace = trace.add_info(
func_analysis_trace,
line!(),
format!(
"{} resolved signature{} matched this function's parameters",
matched_resolved_functions.len(),
if matched_resolved_functions.len() > 1 { "s" } else { "" }
).to_string()
);
for resolved_function in matched_resolved_functions {
trace.add_message(
match_trace,
line!(),
vec![resolved_function.signature]
);
}
}
}
if !args.skip_resolving {
// resolve custom error signatures
let mut resolved_counter = 0;
for (error_selector, _) in analyzed_function.errors.clone() {
decompilation_progress.set_message(format!("resolving error '0x{}'", &error_selector));
let resolved_error_selectors = resolve_error_signature(&error_selector);
// only continue if we have matches
match resolved_error_selectors {
Some(resolved_error_selectors) => {
let mut selected_error_index: u8 = 0;
if resolved_error_selectors.len() > 1 {
decompilation_progress.suspend(|| {
selected_error_index = logger.option(
"warn", "multiple possible matches found. select an option below",
resolved_error_selectors.iter()
.map(|x| x.signature.clone()).collect(),
Some(*&(resolved_error_selectors.len()-1) as u8),
args.default
);
});
}
let selected_match = match resolved_error_selectors.get(selected_error_index as usize) {
Some(selected_match) => selected_match,
None => {
logger.error("invalid selection.");
std::process::exit(1)
}
};
resolved_counter += 1;
analyzed_function.errors.insert(error_selector.clone(), Some(selected_match.clone()));
all_resolved_errors.insert(error_selector.clone(), selected_match.clone());
},
None => {}
}
}
if resolved_counter > 0 {
trace.br(func_analysis_trace);
trace.add_info(
func_analysis_trace,
line!(),
format!("resolved {} error signatures from {} selectors.", resolved_counter, analyzed_function.errors.len()).to_string()
);
}
// resolve custom event signatures
resolved_counter = 0;
for (event_selector, (_, raw_event)) in analyzed_function.events.clone() {
decompilation_progress.set_message(format!("resolving event '0x{}'", &event_selector.get(0..8).unwrap().to_string()));
let resolved_event_selectors = resolve_event_signature(&event_selector.get(0..8).unwrap().to_string());
// only continue if we have matches
match resolved_event_selectors {
Some(resolved_event_selectors) => {
let mut selected_event_index: u8 = 0;
if resolved_event_selectors.len() > 1 {
decompilation_progress.suspend(|| {
selected_event_index = logger.option(
"warn", "multiple possible matches found. select an option below",
resolved_event_selectors.iter()
.map(|x| x.signature.clone()).collect(),
Some(*&(resolved_event_selectors.len()-1) as u8),
args.default
);
});
}
let selected_match = match resolved_event_selectors.get(selected_event_index as usize) {
Some(selected_match) => selected_match,
None => {
logger.error("invalid selection.");
std::process::exit(1)
}
};
resolved_counter += 1;
analyzed_function.events.insert(event_selector.clone(), (Some(selected_match.clone()), raw_event));
all_resolved_events.insert(event_selector, selected_match.clone());
},
None => {}
}
}
if resolved_counter > 0 {
trace.add_info(
func_analysis_trace,
line!(),
format!("resolved {} event signatures from {} selectors.", resolved_counter, analyzed_function.events.len()).to_string()
);
}
}
analyzed_functions.push(analyzed_function.clone());
}
decompilation_progress.finish_and_clear();
logger.info("symbolic execution completed.");
logger.info("building decompilation output.");
// create the decompiled source output
build_output(
&args,
output_dir,
analyzed_functions,
all_resolved_errors,
all_resolved_events,
&logger,
&mut trace,
decompile_call,
);
trace.display();
logger.debug(&format!("decompilation completed in {:?}.", now.elapsed()).to_string());
}
/// Builder pattern for using decompile method as a library.
///
/// Default values may be overriden individually.
/// ## Example
/// Use with normal settings:
/// ```no_run
/// # use crate::heimdall::decompile::DecompileBuilder;
/// const SOURCE: &'static str = "7312/* snip */04ad";
///
/// DecompileBuilder::new(SOURCE)
/// .decompile();
/// ```
/// Or change settings individually:
/// ```no_run
/// # use crate::heimdall::decompile::DecompileBuilder;
///
/// const SOURCE: &'static str = "7312/* snip */04ad";
/// DecompileBuilder::new(SOURCE)
/// .default(false)
/// .include_sol(false)
/// .output("my_contract_dir")
/// .rpc("https://127.0.0.1:8545")
/// .skip_resolving(true)
/// .verbosity(5)
/// .decompile();
/// ```
#[allow(dead_code)]
pub struct DecompileBuilder {
args: DecompilerArgs
}
impl DecompileBuilder where {
/// A new builder for the decompilation of the specified target.
///
/// The target may be a file, bytecode, contract address, or ENS name.
#[allow(dead_code)]
pub fn new(target: &str) -> Self {
DecompileBuilder {
args: DecompilerArgs {
target: target.to_string(),
verbose: clap_verbosity_flag::Verbosity::new(0, 0),
output: String::from(""),
rpc_url: String::from(""),
default: true,
skip_resolving: false,
include_solidity: true
}
}
}
/// Set the output verbosity level.
///
/// - -1 None
/// - 0 Error
/// - 1 Warn
/// - 2 Info
/// - 3 Debug
/// - 4 Trace
#[allow(dead_code)]
pub fn verbosity(mut self, level: i8) -> DecompileBuilder {
// Calculated by the log library as: 1 + verbose - quiet.
// Set quiet as 1, and the level corresponds to the appropriate Log level.
self.args.verbose = clap_verbosity_flag::Verbosity::new(level, 1);
self
}
/// The output directory to write the decompiled files to
#[allow(dead_code)]
pub fn output(mut self, directory: &str) -> DecompileBuilder {
self.args.output = directory.to_string();
self
}
/// The RPC provider to use for fetching target bytecode.
#[allow(dead_code)]
pub fn rpc(mut self, url: &str) -> DecompileBuilder {
self.args.rpc_url = url.to_string();
self
}
/// When prompted, always select the default value.
#[allow(dead_code)]
pub fn default(mut self, accept: bool) -> DecompileBuilder {
self.args.default = accept;
self
}
/// Whether to skip resolving function selectors.
#[allow(dead_code)]
pub fn skip_resolving(mut self, skip: bool) -> DecompileBuilder {
self.args.skip_resolving = skip;
self
}
/// Whether to include solidity source code in the output (in beta).
#[allow(dead_code)]
pub fn include_sol(mut self, include: bool) -> DecompileBuilder {
self.args.include_solidity = include;
self
}
/// Starts the decompilation.
#[allow(dead_code)]
pub fn decompile(self) {
decompile(self.args)
}
}