Repository navigation
Expand file tree
/
Copy pathsmallvm.rs
More file actions
268 lines (250 loc) · 11.9 KB
/
Copy pathsmallvm.rs
File metadata and controls
268 lines (250 loc) · 11.9 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
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
use anyhow::Result;
use log::{debug, trace};
use num_bigint::ToBigInt;
use num_traits::{Pow, ToPrimitive};
use py27_marshal::bstr::BString;
use py27_marshal::*;
use pydis::prelude::*;
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::convert::TryFrom;
use std::io::{Cursor, Read};
use std::sync::{Arc, Mutex};
use unfuck::smallvm::*;
type TargetOpcode = pydis::opcode::py27::Standard;
pub fn exec_stage2(code: Arc<Code>, outer_code: Arc<Code>) -> Result<Vec<u8>> {
let output = Arc::new(BString::from(Vec::with_capacity(outer_code.code.len())));
let mut state = State::FindXorStart {
make_functions_found: 0,
function_index: 0,
};
#[derive(Clone)]
enum State {
FindXorStart {
make_functions_found: usize,
function_index: u16,
},
FindSwapMap(VecDeque<TargetOpcode>, u16),
AssertInstructionSequence(VecDeque<TargetOpcode>, Box<State>),
ExecuteVm(
VmStack<()>,
VmVars<()>,
// names
VmNames<()>,
// globals
VmNames<()>,
LoadedNames,
),
}
// while let Some(current_state) = state.take() {
// match current_state {
// State::Start => {
// state = Some(State::FindExec);
// }
// State::FindExec => {
// }
// }
// }
let mut original_code = Vec::clone(&outer_code.code);
const_jmp_instruction_walker(
code.code.as_slice(),
Arc::clone(&code.consts),
|instr, offset| {
trace!("Instruction at {}: {:?}", offset, instr);
match &mut state {
State::FindXorStart {
make_functions_found,
function_index,
} => {
if let TargetOpcode::LOAD_CONST = instr.opcode {
*function_index = instr.arg.unwrap();
}
if let TargetOpcode::MAKE_FUNCTION = instr.opcode {
*make_functions_found += 1;
}
if *make_functions_found == 3 {
// The next instruction processed will be our code that
// invokes the swapmap
state = State::FindSwapMap(
vec![
TargetOpcode::STORE_FAST,
TargetOpcode::BUILD_LIST,
TargetOpcode::BUILD_LIST,
TargetOpcode::LOAD_FAST,
TargetOpcode::LOAD_FAST,
TargetOpcode::CALL_FUNCTION,
]
.into(),
*function_index,
);
return WalkerState::ContinueIgnoreAnalyzedInstructions;
}
}
State::FindSwapMap(seq, function_index) => {
assert_eq!(instr.opcode, seq.pop_front().unwrap());
// The last instruction is calling our SWAP_MAP function. Invoke that now
if seq.is_empty() {
// Now that we've discovered our swapmap function, let's figure out which
// of these consts is our swapmap
let function_const = &code.consts[*function_index as usize];
if let py27_marshal::Obj::Code(function_code) = function_const {
let mut swapmap_index = None;
trace!("Found the swapmap function -- finding swapmap index");
const_jmp_instruction_walker(
function_code.code.as_slice(),
Arc::clone(&function_code.consts),
|instr, _offset| {
if let TargetOpcode::LOAD_CONST = instr.opcode {
swapmap_index = Some(instr.arg.unwrap() as usize);
WalkerState::Break
} else {
WalkerState::Continue
}
},
)
.expect("failed to walk function instructions");
// Now that we've found the swapmap, let's apply it to our
// original code
let swapmap_const = &function_code.consts[swapmap_index.unwrap()];
if let Obj::Dict(swapmap) = swapmap_const {
let swapmap = swapmap.read().unwrap();
for byte in &mut original_code {
let byte_as_bigint = (*byte).to_bigint().unwrap();
let swapmap_value = &swapmap[&ObjHashable::try_from(
&Obj::Long(Arc::new(byte_as_bigint)),
)
.unwrap()];
if let Obj::Long(value) = swapmap_value {
*byte = (&*value).to_u8().unwrap();
} else {
panic!(
"swapmap value should be a long, found: {:?}",
swapmap_value.typ()
);
}
}
} else {
panic!(
"suspected swapmap at index {} is a {:?}, not dict!",
swapmap_index.unwrap(),
function_const.typ()
);
}
} else {
panic!(
"const index {} is a {:?}, not code!",
function_index,
function_const.typ()
);
}
// We've successfully applied the swapmap! Let's now get
// to the point where we may execute the VM freely
state = State::AssertInstructionSequence(
vec![
TargetOpcode::GET_ITER,
// when we encounter the FOR_ITER we need to jump
// out of the loop
TargetOpcode::FOR_ITER,
// These instructions are post-loop
TargetOpcode::GET_ITER,
]
.into(),
Box::new(State::ExecuteVm(
vec![
(
Some(Obj::String(Arc::clone(&output))),
InstructionTracker::new(),
),
(
Some(Obj::String(Arc::new(
// reverse this data so we can use it as a proper-ordered stack
BString::from(
original_code
.iter()
.rev()
.cloned()
.collect::<Vec<u8>>(),
),
))),
InstructionTracker::new(),
),
],
HashMap::new(),
HashMap::new(),
HashMap::new(),
Default::default(),
)),
);
}
return WalkerState::ContinueIgnoreAnalyzedInstructions;
}
State::AssertInstructionSequence(seq, next_state) => {
assert_eq!(instr.opcode, seq.pop_front().unwrap());
if seq.is_empty() {
// TODO: bad allocation since we cannot move out of a referenced
// box
state = *(next_state.clone());
}
// Jump out of any loops
if let TargetOpcode::FOR_ITER = instr.opcode {
return WalkerState::JumpTo(offset + 3 + (instr.arg.unwrap() as u64));
}
return WalkerState::ContinueIgnoreAnalyzedInstructions;
}
State::ExecuteVm(stack, vars, names, globals, names_loaded) => {
// Check if our bytecode has been drained. This should be index 0 on the satck
if let (Some(Obj::String(s)), _modifying_instrs) = &stack[1] {
if s.is_empty() && instr.opcode == TargetOpcode::FOR_ITER {
return WalkerState::Break;
}
}
execute_instruction(
&instr,
Arc::clone(&code),
stack,
vars,
names,
globals,
Arc::clone(&*names_loaded),
|_function, args, _kwargs| match names_loaded.lock().unwrap().last() {
Some(s) => match std::str::from_utf8(&*s.as_slice())
.expect("string is not valid utf8")
{
"chr" => match &args[0] {
Some(Obj::Long(l)) => {
return Some(Obj::Long(Arc::new(
l.to_u8().unwrap().to_bigint().unwrap(),
)));
}
Some(other) => {
panic!(
"unexpected input type of {:?} for chr",
other.typ()
);
}
None => {
panic!("cannot use chr on unknown value");
}
},
other => {
panic!("unsupported function: {}", other);
}
},
other => {
panic!("unsupported callable: {:?}", other);
}
},
(), // we don't care about tracking offsets
)
.expect("error executing stage2");
// We want to execute sequentially -- ignore the rest of the queue
// for now
return WalkerState::ContinueIgnoreAnalyzedInstructions;
}
}
WalkerState::Continue
},
)?;
// Reverse the bytecode
let output: Vec<u8> = output.iter().rev().copied().collect();
Ok(output)
}