-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathextcall.rs
More file actions
218 lines (203 loc) · 9.13 KB
/
Copy pathextcall.rs
File metadata and controls
218 lines (203 loc) · 9.13 KB
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
use alloy::primitives::U256;
use futures::future::BoxFuture;
use heimdall_common::utils::{hex::ToLowerHex, strings::encode_hex_reduced};
use heimdall_vm::{
core::{opcodes::opcode_name, vm::State},
w_gas, w_push0,
};
use tracing::trace;
use crate::{
core::analyze::AnalyzerState, interfaces::AnalyzedFunction,
utils::precompile::decode_precompile, Error,
};
use heimdall_decoder::{decode, DecodeArgsBuilder};
pub(crate) fn extcall_heuristic<'a>(
function: &'a mut AnalyzedFunction,
state: &'a State,
analyzer_state: &'a mut AnalyzerState,
) -> BoxFuture<'a, Result<(), Error>> {
Box::pin(async move {
let instruction = &state.last_instruction;
match instruction.opcode {
// CALL / CALLCODE
0xf1 | 0xf2 => {
let address = instruction.input_operations[1].solidify();
let memory =
function.get_memory_range(instruction.inputs[3], instruction.inputs[4]);
let extcalldata =
memory.iter().map(|x| x.value.to_lower_hex()).collect::<Vec<String>>().join("");
let gas_solidified = instruction.input_operations[0].solidify();
let value_solidified = instruction.input_operations[2].solidify();
// if gas is 2,300, this is a value transfer
if gas_solidified.contains("0x08fc") {
trace!(
"instruction {} ({}) with 2300 gas indicates a value transfer",
instruction.instruction,
opcode_name(instruction.opcode)
);
function
.logic
.push(format!("address({address}).transfer({value_solidified});"));
return Ok(());
}
if extcalldata.is_empty() {
trace!(
"instruction {} ({}) with no calldata indicates a value transfer",
instruction.instruction,
opcode_name(instruction.opcode)
);
function
.logic
.push(format!("address({address}).transfer({value_solidified});"));
return Ok(());
}
let extcalldata_clone = extcalldata.clone();
let decoded = decode(
DecodeArgsBuilder::new()
.target(extcalldata_clone)
.raw(true)
.skip_resolving(analyzer_state.skip_resolving)
.build()
.expect("Failed to build DecodeArgs"),
)
.await
.ok();
// build modifiers
// - if gas is just the default (GAS()), we don't need to include it
// - if value is just the default (0), we don't need to include it
let mut modifiers = vec![];
if instruction.input_operations[0] != w_gas!() {
modifiers.push(format!("gas: {gas_solidified}"));
}
if instruction.input_operations[2] != w_push0!() {
// if the value is just a hex string, we can parse it as ether for readability
if let Ok(value) =
u128::from_str_radix(value_solidified.trim_start_matches("0x"), 16)
{
let ether_value = value as f64 / 10_f64.powi(18);
modifiers.push(format!("value: {ether_value} ether"));
} else {
modifiers.push(format!("value: {value_solidified}"));
}
}
let modifier = if modifiers.is_empty() {
"".to_string()
} else {
format!("{{ {} }}", modifiers.join(", "))
};
// check if the external call is a precompiled contract
if let Some(precompile_logic) = decode_precompile(
instruction.inputs[1],
&memory,
&instruction.input_operations[5],
) {
function.logic.push(precompile_logic);
} else if let Some(decoded) = decoded {
let start_slot = instruction.inputs[3] + U256::from(4);
function.logic.push(format!(
"(bool success, bytes memory ret0) = address({}).{}{}({}); // {}",
address,
modifier,
decoded.decoded.name,
decoded
.decoded
.inputs
.iter()
.enumerate()
.map(|(i, _)| {
format!(
"memory[{}]",
encode_hex_reduced(start_slot + U256::from(i * 32))
)
})
.collect::<Vec<String>>()
.join(", "),
opcode_name(instruction.opcode).to_lowercase(),
));
} else {
function.logic.push(format!(
"(bool success, bytes memory ret0) = address({}).Unresolved_{}{}(msg.data[{}:{}]); // {}",
address,
extcalldata.get(2..10).unwrap_or(""),
modifier,
instruction.input_operations[3].solidify(),
instruction.input_operations[4].solidify(),
opcode_name(instruction.opcode).to_lowercase(),
));
}
}
// STATICCALL / DELEGATECALL
0xfa | 0xf4 => {
let gas = format!("gas: {}", instruction.input_operations[0].solidify());
let address = instruction.input_operations[1].solidify();
let memory =
function.get_memory_range(instruction.inputs[2], instruction.inputs[3]);
let extcalldata = memory
.iter()
.map(|x| x.value.to_lower_hex().trim_start_matches("0x").to_owned())
.collect::<Vec<String>>()
.join("");
let extcalldata_clone = extcalldata.clone();
let decoded = decode(
DecodeArgsBuilder::new()
.target(extcalldata_clone)
.raw(true)
.skip_resolving(analyzer_state.skip_resolving)
.build()
.expect("Failed to build DecodeArgs"),
)
.await
.ok();
// build the modifier w/ gas
// if the modifier is just the default (GAS()), we don't need to include it
let modifier = if instruction.input_operations[0] != w_gas!() {
format!("{{ {gas} }}")
} else {
"".to_string()
};
// check if the external call is a precompiled contract
if let Some(precompile_logic) = decode_precompile(
instruction.inputs[1],
&memory,
&instruction.input_operations[4],
) {
function.logic.push(precompile_logic);
} else if let Some(decoded) = decoded {
let start_slot = instruction.inputs[2] + U256::from(4);
function.logic.push(format!(
"(bool success, bytes memory ret0) = address({}).{}{}({}); // {}",
address,
modifier,
decoded.decoded.name,
decoded
.decoded
.inputs
.iter()
.enumerate()
.map(|(i, _)| {
format!(
"memory[{}]",
encode_hex_reduced(start_slot + U256::from(i * 32))
)
})
.collect::<Vec<String>>()
.join(", "),
opcode_name(instruction.opcode).to_lowercase(),
));
} else {
function.logic.push(format!(
"(bool success, bytes memory ret0) = address({}).Unresolved_{}{}(memory[{}:{}]); // {}",
address,
extcalldata.get(2..10).unwrap_or(""),
modifier,
instruction.input_operations[2].solidify(),
instruction.input_operations[3].solidify(),
opcode_name(instruction.opcode).to_lowercase(),
));
}
}
_ => {}
};
Ok(())
})
}