1
+ #![ allow( unused) ]
2
+
1
3
use icicle:: * ;
2
4
use pyo3:: PyResult ;
3
5
use std:: process:: exit;
@@ -22,6 +24,20 @@ fn new_vm(jit: bool) -> PyResult<Icicle> {
22
24
)
23
25
}
24
26
27
+ fn new_trace_vm ( jit : bool ) -> PyResult < Icicle > {
28
+ Icicle :: new (
29
+ "x86_64" . to_string ( ) ,
30
+ jit,
31
+ true ,
32
+ false ,
33
+ true ,
34
+ false ,
35
+ true ,
36
+ false ,
37
+ true ,
38
+ )
39
+ }
40
+
25
41
fn nx_start ( ) -> PyResult < ( ) > {
26
42
let mut vm = new_vm ( false ) ?;
27
43
let page = 0x10000 ;
@@ -102,7 +118,7 @@ fn block_optimization() -> PyResult<()> {
102
118
vm. mem_map ( heap, 0x1000 , MemoryProtection :: ReadWrite ) ?;
103
119
vm. mem_write ( heap + 4 , b"\x37 \x13 \x00 \x00 " . to_vec ( ) ) ?;
104
120
vm. mem_map ( addr & !0xFFF , 0x1000 , MemoryProtection :: ExecuteRead ) ?;
105
- vm. mem_write ( addr, b"\x41 \xc1 \xea \x07 \x41 \x83 \xe2 \x1f \x74 \x08 \x44 \x89 \xd0 \x48 \x89 \x54 \xc6 \x08 \x49 \x83 \xc1 \x04 \x4c \x89 \x0e \x4c \x89 \xc9 \x44 \x8b \x11 \x44 \x89 \xd0 \xf7 \xd0 \x49 \x89 \xc9 \xa8 \x03 \x0f \x84 \x88 \xf6 \xff \xff \xeb \x4c " . to_vec ( ) ) ?;
121
+ vm. mem_write ( addr, b"\x41 \xc1 \xea \x07 \x41 \x83 \xe2 \x1f \x74 \x08 \x44 \x89 \xd0 \x48 \x89 \x54 \xc6 \x08 \x49 \x83 \xc1 \x04 \x4c \x89 \x0e \x4c \x89 \xc9 \x44 \x8b \x11 \x44 \x89 \xd0 \xf7 \xd0 \x49 \x89 \xc9 \xa8 \x03 \x0f \x84 \x88 \xf6 \xff \xff \xeb \x4c \xcc " . to_vec ( ) ) ?;
106
122
107
123
// Register setup
108
124
vm. reg_write ( "r9" , heap) ?;
@@ -142,7 +158,7 @@ fn rewind() -> PyResult<()> {
142
158
vm. mem_map ( 0x100 , 0x20 , MemoryProtection :: ExecuteRead ) ?;
143
159
vm. mem_map ( 0x200 , 0x20 , MemoryProtection :: ReadOnly ) ?;
144
160
145
- vm. mem_write ( 0x100 , b"\x55 " . to_vec ( ) ) ?; // push rbp
161
+ vm. mem_write ( 0x100 , b"\x55 \xCC " . to_vec ( ) ) ?; // push rbp
146
162
vm. reg_write ( "rbp" , 0xF00 ) ?;
147
163
vm. reg_write ( "rsp" , 0x210 ) ?;
148
164
vm. reg_write ( "rip" , 0x100 ) ?;
@@ -155,16 +171,60 @@ fn rewind() -> PyResult<()> {
155
171
Ok ( ( ) )
156
172
}
157
173
174
+ fn execute_uninitialized ( ) -> PyResult < ( ) > {
175
+ let mut vm = Icicle :: new (
176
+ "x86_64" . to_string ( ) ,
177
+ false ,
178
+ true ,
179
+ false ,
180
+ true ,
181
+ true , // NOTE: setting this to true is not properly supported
182
+ true ,
183
+ false ,
184
+ true ,
185
+ ) ?;
186
+
187
+ // \x48\x8d\x05\x01\x00\x00\x00\x90\x8a\x18\x90
188
+
189
+ vm. mem_map ( 0x100 , 0x20 , MemoryProtection :: ExecuteOnly ) ?;
190
+ vm. mem_write ( 0x100 , b"\xFF \xC0 " . to_vec ( ) ) ?; // inc eax
191
+ vm. reg_write ( "rip" , 0x100 ) ?;
192
+ let status = vm. step ( 1 ) ;
193
+ // NOTE: the real reason is that INIT is not set
194
+ println ! ( "run status : {:?}" , status) ;
195
+ println ! ( "exception code : {:?}" , vm. get_exception_code( ) ) ;
196
+ println ! ( "exception value : {:#x}" , vm. get_exception_value( ) ) ;
197
+ println ! ( "rax : {:#x}" , vm. reg_read( "rax" ) ?) ;
198
+
199
+ // TODO: status is now UnhandledException, should be InstructionLimit
200
+ // on the next stpe it should be UnhandledException -> ExecViolation
201
+
202
+ Ok ( ( ) )
203
+ }
204
+
158
205
fn execute_only ( ) -> PyResult < ( ) > {
159
- let mut vm = new_vm ( true ) ?;
206
+ let mut vm = new_vm ( false ) ?;
160
207
161
208
vm. mem_map ( 0x100 , 0x20 , MemoryProtection :: ExecuteOnly ) ?;
162
- vm. mem_write ( 0x100 , b"\x90 " . to_vec ( ) ) ?; // nop
209
+ /*
210
+ 0x100: lea rax, [rip]
211
+ 0x107: nop
212
+ 0x108: mov bl, byte ptr [rax]
213
+ 0x10A: int3
214
+ */
215
+ vm. mem_write (
216
+ 0x100 ,
217
+ b"\x48 \x8d \x05 \x00 \x00 \x00 \x00 \x90 \x8a \x18 \xCC " . to_vec ( ) ,
218
+ ) ?; // nop
163
219
vm. reg_write ( "rip" , 0x100 ) ?;
220
+ vm. step ( 2 ) ;
164
221
let status = vm. step ( 1 ) ;
222
+ // NOTE: the real reason is that INIT is not set
165
223
println ! ( "run status : {:?}" , status) ;
166
224
println ! ( "exception code : {:?}" , vm. get_exception_code( ) ) ;
167
225
println ! ( "exception value : {:#x}" , vm. get_exception_value( ) ) ;
226
+ println ! ( "bl: {:#x}" , vm. reg_read( "bl" ) ?) ;
227
+ println ! ( "rip: {:#x}" , vm. reg_read( "rip" ) ?) ;
168
228
169
229
Ok ( ( ) )
170
230
}
@@ -178,7 +238,10 @@ fn step_modify_rip() -> PyResult<()> {
178
238
// 0x107: 48 89 d9 mov rcx,rbx
179
239
// 0x10a: 90 nop
180
240
// 0x10b: 90 nop
181
- vm. mem_write ( 0x100 , b"\x48 \x01 \xD8 \x48 \x83 \xE9 \x05 \x48 \x89 \xD9 \x90 \x90 " . to_vec ( ) ) ?;
241
+ vm. mem_write (
242
+ 0x100 ,
243
+ b"\x48 \x01 \xD8 \x48 \x83 \xE9 \x05 \x48 \x89 \xD9 \x90 \x90 \xCC " . to_vec ( ) ,
244
+ ) ?;
182
245
183
246
vm. reg_write ( "rax" , 0xF00 ) ?;
184
247
vm. reg_write ( "rbx" , 0x210 ) ?;
@@ -194,7 +257,7 @@ fn step_modify_rip() -> PyResult<()> {
194
257
) ;
195
258
vm. reg_write ( "rip" , 0x100 ) ?;
196
259
//vm.write_pc(0x100);
197
- //println!("pc: {:#x}", vm.read_pc());
260
+ //println!("pc: {:#x}", vm.read_pc());
198
261
println ! ( "rip rewritten {:#x}" , vm. reg_read( "rip" ) ?) ;
199
262
status = vm. step ( 1 ) ;
200
263
println ! (
@@ -210,7 +273,7 @@ fn eflags_reconstruction() -> PyResult<()> {
210
273
let mut vm = new_vm ( false ) ?;
211
274
vm. mem_map ( 0x100 , 0x20 , MemoryProtection :: ExecuteRead ) ?;
212
275
213
- vm. mem_write ( 0x100 , b"\x48 \x01 \xD8 " . to_vec ( ) ) ?;
276
+ vm. mem_write ( 0x100 , b"\x48 \x01 \xD8 \xCC " . to_vec ( ) ) ?;
214
277
vm. reg_write ( "rax" , 0x7FFFFFFFFFFFFFFF ) ?;
215
278
vm. reg_write ( "rbx" , 0x1 ) ?;
216
279
@@ -232,7 +295,10 @@ fn eflags_reconstruction() -> PyResult<()> {
232
295
let rflags = vm. reg_read ( "rflags" ) ?;
233
296
let of = vm. reg_read ( "OF" ) ?;
234
297
let of_set = ( eflags & of_mask) == of_mask;
235
- println ! ( "[post] eflags: {:#x} == {:#x}, OF: {:#x} == {}" , eflags, rflags, of, of_set) ;
298
+ println ! (
299
+ "[post] eflags: {:#x} == {:#x}, OF: {:#x} == {}" ,
300
+ eflags, rflags, of, of_set
301
+ ) ;
236
302
}
237
303
238
304
{
@@ -249,7 +315,10 @@ fn eflags_reconstruction() -> PyResult<()> {
249
315
vm. reg_write ( "rflags" , eflags) ?;
250
316
let of = vm. reg_read ( "OF" ) ?;
251
317
let of_set = ( eflags >> 11 ) & 1 ;
252
- println ! ( "[rflags|={:#x}] eflags: {:#x}, OF: {:#x} == {}" , of_mask, eflags, of, of_set) ;
318
+ println ! (
319
+ "[rflags|={:#x}] eflags: {:#x}, OF: {:#x} == {}" ,
320
+ of_mask, eflags, of, of_set
321
+ ) ;
253
322
}
254
323
255
324
Ok ( ( ) )
@@ -284,6 +353,7 @@ fn main() {
284
353
( "Block optimization bug" , block_optimization) ,
285
354
( "Rewind" , rewind) ,
286
355
( "Execute only" , execute_only) ,
356
+ ( "Execute uninitialized" , execute_uninitialized) ,
287
357
( "Step modify rip" , step_modify_rip) ,
288
358
( "EFlags reconstruction" , eflags_reconstruction) ,
289
359
] ;
0 commit comments