-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpylibemu.pyx
1242 lines (936 loc) · 36.5 KB
/
pylibemu.pyx
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# pylibemu.pyx
#
# Copyright(c) 2011-2022 Angelo Dell'Aera <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#cython: language_level=3
cimport pylibemu
__version__ = '0.8'
import sys
try:
import urllib.request as urllib2
except ImportError:
import urllib2
import hashlib
import logging
logging.basicConfig(format = '%(asctime)s %(message)s', datefmt = '[%Y-%m-%d %H:%M:%S]')
# export register numbers
class EMU_REGS:
eax = 0
ecx = 1
edx = 2
ebx = 3
esp = 4
ebp = 5
esi = 6
edi = 7
# User hooks
cdef uint32_t ExitProcess(c_emu_env *env, c_emu_env_hook *hook...) noexcept:
cdef va_list args
cdef int _exitcode
va_start(args, <void *>hook)
_exitcode = <int>va_arg(args, int_type)
va_end(args)
return 0
cdef uint32_t ExitThread(c_emu_env *env, c_emu_env_hook *hook...) noexcept:
cdef va_list args
cdef int _exitcode
va_start(args, <void *>hook)
_exitcode = <int>va_arg(args, int_type)
va_end(args)
return 0
cdef uint32_t URLDownloadToFile(c_emu_env *env, c_emu_env_hook *hook...) noexcept:
cdef va_list args
cdef void *_pCaller
cdef char *szURL
cdef char *szFileName
cdef int _dwReserved
cdef void *_lpfnCB
va_start(args, <void*>hook)
_pCaller = <void *>va_arg(args, void_ptr_type)
szURL = <char *>va_arg(args, char_ptr_type)
szFileName = <char *>va_arg(args, char_ptr_type)
_dwReserved = <int>va_arg(args, int_type)
_lpfnCB = <void *>va_arg(args, void_ptr_type)
va_end(args)
logging.warning("Downloading %s (%s)" % (szURL.decode('utf-8'),
szFileName.decode('utf-8')))
try:
url = urllib2.urlopen(szURL.decode('utf-8'), timeout = 10)
content = url.read()
except Exception:
logging.warning("Error while downloading from %s" % (szURL, ))
return 0x800C0008 # INET_E_DOWNLOAD_FAILURE
m = hashlib.md5(content)
with open(str(m.hexdigest()), mode = 'wb') as fd:
fd.write(content)
return 0
DEF OUTPUT_SIZE = 1024 * 1024 # 1MB
DEF SEP_SIZE = 16
DEF S_SIZE = 4096
cdef class EmuProfile:
cdef char *sep[SEP_SIZE]
cdef char *output
cdef char *t
cdef char *s
cdef bint truncate
cdef unsigned long output_size
def __cinit__(self, size_t output_size):
self.truncate = False
self.output_size = output_size
self.output = <char *>malloc(output_size)
self.s = <char *>malloc(S_SIZE)
self.check_memalloc()
memset(self.output, 0, output_size)
memset(self.s , 0, S_SIZE)
self.build_sep()
cdef check_memalloc(self):
if self.output is NULL or self.s is NULL:
logging.warning("Memory allocation error")
sys._exit(-1)
cdef concatenate(self, char *dst, char *src, unsigned long n):
if self.truncate:
return
if <unsigned long>len(dst) + <unsigned long>len(src) > n - 1:
self.truncate = True
return
strncat(dst, src, n)
cdef build_sep(self):
cdef char *ssep = ' '
cdef int counter
cdef int i
cdef int max_len
max_len = len(ssep) * SEP_SIZE + 1
for i in range(SEP_SIZE):
counter = i
t = <char *>malloc(max_len)
if t is NULL:
logging.warning("Memory allocation error")
sys._exit(-1)
memset(t, 0, sizeof(char) * max_len)
while counter:
self.concatenate(t, ssep, max_len)
counter -= 1
self.sep[i] = t
cdef log_function_header(self, c_emu_profile_function *function):
snprintf(self.s,
S_SIZE,
"%s %s (\n",
function.return_value.argtype,
function.fnname)
self.concatenate(self.output, self.s, self.output_size)
cdef log_bracket_closed(self):
cdef char *s = ")"
self.concatenate(self.output, s, self.output_size)
cdef log_array_start(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s %s %s = [ \n",
self.sep[indent],
argument.argtype,
argument.argname)
self.concatenate(self.output, self.s, self.output_size)
cdef log_array_end(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s ];\n",
self.sep[indent])
self.concatenate(self.output, self.s, self.output_size)
cdef log_struct_start(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s struct %s %s = {\n",
self.sep[indent],
argument.argtype,
argument.argname)
self.concatenate(self.output, self.s, self.output_size)
cdef log_struct_end(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s };\n",
self.sep[indent])
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_int(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s %s %s = %i;\n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tint)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_string(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s %s %s = \"%s\";\n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tchar)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_bytea(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s %s %s = \".binary.\" (%i bytes);\n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.bytea.size)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_ptr(self, c_emu_profile_argument *argument, int is_struct, int indent):
if is_struct:
snprintf(self.s,
S_SIZE,
"%s struct %s %s = 0x%x => \n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tptr.addr)
else:
snprintf(self.s,
S_SIZE,
"%s %s %s = 0x%x => \n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tptr.addr)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_ip(self, c_emu_profile_argument *argument, int indent):
cdef c_in_addr *addr
cdef char *host
addr = <c_in_addr *>&argument.value.tint
host = inet_ntoa(addr[0])
snprintf(self.s,
S_SIZE,
"%s %s %s = %i (host = %s);\n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tint,
host)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_port(self, c_emu_profile_argument *argument, int indent):
cdef uint16_t port
port = ntohs(<uint16_t>argument.value.tint)
snprintf(self.s,
S_SIZE,
"%s %s %s = %i (port = %i);\n",
self.sep[indent],
argument.argtype,
argument.argname,
argument.value.tint,
port)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_render_none(self, c_emu_profile_argument *argument, int indent):
snprintf(self.s,
S_SIZE,
"%s none;\n",
self.sep[indent])
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_function_render_none(self):
return
cdef emu_profile_function_render_int(self, int value):
snprintf(self.s,
S_SIZE,
" = 0x%x;\n",
value)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_function_render_ptr(self, void* ptr):
snprintf(self.s,
S_SIZE,
" = 0x%p;\n",
ptr)
self.concatenate(self.output, self.s, self.output_size)
cdef emu_profile_argument_debug(self, c_emu_profile_argument *argument, int indent):
cdef c_emu_profile_argument *argit
cdef c_emu_profile_argument *argumentit
cdef int is_struct
if argument.render == render_struct:
self.log_struct_start(argument, indent)
argumentit = emu_profile_arguments_first(argument.value.tstruct.arguments)
while not emu_profile_arguments_istail(argumentit):
self.emu_profile_argument_debug(argumentit, indent + 1)
argumentit = emu_profile_arguments_next(argumentit)
self.log_struct_end(argument, indent)
return
if argument.render == render_array:
self.log_array_start(argument, indent)
argumentit = emu_profile_arguments_first(argument.value.tstruct.arguments)
while not emu_profile_arguments_istail(argumentit):
self.emu_profile_argument_debug(argumentit, indent + 1)
argumentit = emu_profile_arguments_next(argumentit)
self.log_array_end(argument, indent)
return
if argument.render == render_int:
self.emu_profile_argument_render_int(argument, indent)
return
if argument.render == render_short:
self.emu_profile_argument_render_int(argument, indent)
return
if argument.render == render_string:
self.emu_profile_argument_render_string(argument, indent)
return
if argument.render == render_bytea:
self.emu_profile_argument_render_bytea(argument, indent)
return
if argument.render == render_ptr:
argit = argument
while argit.render == render_ptr:
argit = argit.value.tptr.ptr
is_struct = 0
if argit.render == render_struct:
is_struct = 1
self.emu_profile_argument_render_ptr(argument, is_struct, indent)
self.emu_profile_argument_debug(argument.value.tptr.ptr, indent + 1)
return
if argument.render == render_ip:
self.emu_profile_argument_render_ip(argument, indent)
return
if argument.render == render_port:
self.emu_profile_argument_render_port(argument, indent)
return
if argument.render == render_none:
self.emu_profile_argument_render_none(argument, indent)
return
cdef emu_profile_function_debug(self, c_emu_profile_function *function):
self.log_function_header(function)
argument = emu_profile_arguments_first(function.arguments)
while not emu_profile_arguments_istail(argument):
self.emu_profile_argument_debug(argument, 1)
argument = emu_profile_arguments_next(argument)
self.log_bracket_closed()
render = function.return_value.render
if render == render_none:
self.emu_profile_function_render_none()
if render == render_int:
value = function.return_value.value.tint
self.emu_profile_function_render_int(value)
if render == render_ptr:
ptr = function.return_value.value.tptr.addr
self.emu_profile_function_render_int(ptr)
self.emu_profile_function_render_none()
cdef emu_profile_debug(self, c_emu_env *_env):
function = emu_profile_functions_first(_env.profile.functions)
while not emu_profile_functions_istail(function):
self.emu_profile_function_debug(function)
function = emu_profile_functions_next(function)
cdef class Emulator:
cdef c_emu *_emu
cdef EmuProfile emu_profile
cdef int32_t _offset
cdef size_t output_size
cdef bint enable_hooks
def __cinit__(self, output_size = OUTPUT_SIZE, enable_hooks = True):
self.output_size = output_size
self.enable_hooks = enable_hooks
self.new()
def __dealloc__(self):
self.free()
def free(self):
if self._emu is not NULL:
emu_free(self._emu)
self._emu = NULL
def new(self):
self._emu = emu_new()
self.emu_profile = EmuProfile(self.output_size)
def set_output_size(self, output_size):
self.free()
self.output_size = output_size
self.new()
def shellcode_getpc_test(self, shellcode):
'''
GetPC code is code that determines its own location in a process address
space. It is commonly used in code that needs to reference itself, for
instance in self-decoding and self-modifying code. This method tries to
identify GetPC within the shellcode.
@type shellcode: Binary string
@param shellcode: Shellcode
@rtype: Integer
@return: If GetPC code is successfully identified the offset from the
start of the shellcode is returned, otherwise -1.
'''
cdef char *buffer
if self._emu is NULL:
self.new()
buffer = <char *>shellcode
sclen = len(bytes(shellcode))
if buffer is NULL:
return -1
self._offset = emu_shellcode_test(self._emu, <uint8_t *>buffer, sclen)
return self._offset
def prepare(self, shellcode, offset):
'''
Method used to prepare the execution environment. The offset parameter
value should be determined by the `shellcode_getpc_test method'. If such
method is not able to identify the GetPC code (thus returning -1) the
suggested value for offset parameter is 0.
@type shellcode: Binary string
@param shellcode: Shellcode
@type offset : Integer
@param offset : GetPC offset
'''
cdef c_emu_cpu *_cpu
cdef c_emu_memory *_mem
cdef char *scode
cdef int j, static_offset
if self._emu is NULL:
self.new()
_cpu = emu_cpu_get(self._emu)
_mem = emu_memory_get(self._emu)
for j in range(8):
emu_cpu_reg32_set(_cpu, <c_emu_reg32>j, 0)
emu_memory_write_dword(_mem, 0xef787c3c, 4711)
emu_memory_write_dword(_mem, 0x0 , 4711)
emu_memory_write_dword(_mem, 0x00416f9a, 4711)
emu_memory_write_dword(_mem, 0x0044fcf7, 4711)
emu_memory_write_dword(_mem, 0x00001265, 4711)
emu_memory_write_dword(_mem, 0x00002583, 4711)
emu_memory_write_dword(_mem, 0x00e000de, 4711)
emu_memory_write_dword(_mem, 0x01001265, 4711)
emu_memory_write_dword(_mem, 0x8a000066, 4711)
emu_memory_write_dword(_mem, 0x7c80ffec, 0xc330408b)
# Set the flags
emu_cpu_eflags_set(_cpu, 0)
# Write the code to the offset
scode = <char *>shellcode
static_offset = 0x417000
emu_memory_write_block(_mem, static_offset, scode, len(shellcode))
# Set eip to the code
emu_cpu_eip_set(emu_cpu_get(self._emu), static_offset + offset)
emu_memory_write_block(_mem, 0x0012fe98, scode, len(shellcode))
emu_cpu_reg32_set(emu_cpu_get(self._emu), esp, 0x0012fe98)
cdef check_stop_emulation(self, c_emu_env_hook *hook):
return hook.hook.win.fnname.decode('utf-8') in ('ExitProcess', 'ExitThread', 'exit')
cpdef int test(self, steps = 1000000):
'''
Method used to test and emulate the shellcode. The method must be always
called after the `prepare' method.
@type steps: Integer
@param steps: Max number of steps to run
'''
cdef c_emu_cpu *_cpu
cdef c_emu_memory *_mem
cdef c_emu_env *_env
cdef uint32_t eipsave
cdef int j
cdef int ret
cdef c_emu_env_hook *hook
if self._emu is NULL:
return -1
_cpu = emu_cpu_get(self._emu)
_mem = emu_memory_get(self._emu)
_env = emu_env_new(self._emu)
if _env is NULL:
print(emu_strerror(self._emu))
return -1
_env.profile = emu_profile_new()
# IAT for sqlslammer
emu_memory_write_dword(_mem, 0x42ae1018, 0x7c801d77)
emu_memory_write_dword(_mem, 0x42ae1010, 0x7c80ada0)
emu_memory_write_dword(_mem, 0x7c80ada0, 0x51ec8b55)
emu_memory_write_byte(_mem, 0x7c814eeb, 0xc3)
emu_env_w32_load_dll(_env.env.win, "urlmon.dll")
if self.enable_hooks:
emu_env_w32_export_hook(_env, "ExitProcess", ExitProcess, NULL)
emu_env_w32_export_hook(_env, "ExitThread", ExitThread, NULL)
emu_env_w32_export_hook(_env, "URLDownloadToFileA", URLDownloadToFile, NULL)
eipsave = 0
ret = 0
for j in range(steps):
if not _cpu.repeat_current_instr:
eipsave = emu_cpu_eip_get(emu_cpu_get(self._emu))
hook = emu_env_w32_eip_check(_env)
if hook is not NULL:
if hook.hook.win.fnname is NULL:
logging.warning("Unhooked call to %s\n" % (hook.hook.win.fnname, ))
break
if self.check_stop_emulation(hook):
break
else:
ret = emu_cpu_parse(emu_cpu_get(self._emu))
hook = NULL
if ret != -1:
hook = emu_env_linux_syscall_check(_env)
if hook is NULL:
ret = emu_cpu_step(emu_cpu_get(self._emu))
else:
logging.warning("Error")
if ret == -1:
break
self.emu_profile.emu_profile_debug(_env)
return 0
cpdef int run(self, shellcode, steps = 1000000):
cdef int32_t offset
offset = self.shellcode_getpc_test(shellcode)
if offset < 0:
offset = 0
self.prepare(shellcode, offset)
return self.test(steps = steps)
@property
def offset(self):
return self._offset
@property
def emu_profile_output(self):
return self.emu_profile.output
@property
def emu_profile_truncated(self):
return self.emu_profile.truncate
# CPU methods
def cpu_reg32_get(self, c_emu_reg32 reg):
'''
Method used to get the 32-bit value stored in a register
@type reg: Integer
@param reg: Register index
eax = 0
ecx = 1
edx = 2
ebx = 3
esp = 4
ebp = 5
esi = 6
edi = 7
@rtype: uint32_t
@return: 32-bit value stored in the register
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <uint32_t>emu_cpu_reg32_get(_cpu, reg)
def cpu_reg32_set(self, c_emu_reg32 reg, uint32_t val):
'''
Method used to set a register with a 32-bit value
@type reg: Integer
@param reg: Register index
eax = 0
ecx = 1
edx = 2
ebx = 3
esp = 4
ebp = 5
esi = 6
edi = 7
@type val: uint32_t
@param val: 32-bit value
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_reg32_set(_cpu, reg, val)
def cpu_reg16_get(self, c_emu_reg16 reg):
'''
Method used to get the 16-bit value stored in a register
@type reg: Integer
@param reg: Register index
ax = 0
cx = 1
dx = 2
bx = 3
sp = 4
bp = 5
si = 6
di = 7
@rtype: uint16_t
@return: 16-bit value stored in the register
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <uint16_t>emu_cpu_reg16_get(_cpu, reg)
def cpu_reg16_set(self, c_emu_reg16 reg, uint16_t val):
'''
Method used to set a register with a 16-bit value
@type reg: Integer
@param reg: Register index
ax = 0
cx = 1
dx = 2
bx = 3
sp = 4
bp = 5
si = 6
di = 7
@type val: uint16_t
@param val: 16-bit value
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_reg16_set(_cpu, reg, val)
def cpu_reg8_get(self, c_emu_reg8 reg):
'''
Method used to get the 8-bit value stored in a register
@type reg: Integer
@param reg: Register index
al = 0
cl = 1
dl = 2
bl = 3
ah = 4
ch = 5
dh = 6
bh = 7
@rtype: uint8_t
@return: 8-bit value stored in the register
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <uint8_t>emu_cpu_reg8_get(_cpu, reg)
def cpu_reg8_set(self, c_emu_reg8 reg, uint8_t val):
'''
Method used to set a register with a 8-bit value
@type reg: Integer
@param reg: Register index
al = 0
cl = 1
dl = 2
bl = 3
ah = 4
ch = 5
dh = 6
bh = 7
@type val: uint8_t
@param val: 8-bit value
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_reg8_set(_cpu, reg, val)
def cpu_eflags_get(self):
'''
Method used to get the 32-bit value stored in the register eflags
@rtype: uint32_t
@return: 32-bit value stored in the register eflags
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <uint32_t>emu_cpu_eflags_get(_cpu)
def cpu_eflags_set(self, uint32_t val):
'''
Method used to set the register eflags with a 32-bit value
@type val: uint32_t
@param val: 32-bit value
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_eflags_set(_cpu, val)
def cpu_eip_set(self, uint32_t eip):
'''
Method used to set the register eip with a 32-bit value
@type val: uint32_t
@param val: 32-bit value
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_eip_set(_cpu, eip)
def cpu_eip_get(self):
'''
Method used to get the 32-bit value stored in the register eip
@rtype: uint32_t
@return: 32-bit value stored in the register eip
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <uint32_t>emu_cpu_eip_get(_cpu)
def cpu_parse(self):
'''
Method used to parse an instruction at eip
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <int32_t>emu_cpu_parse(_cpu)
def cpu_step(self):
'''
Method used to step the last instruction
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
return <int32_t>emu_cpu_step(_cpu)
def cpu_debugflag_set(self, uint8_t flag):
'''
Method used to set a cpu debug flag
@type flag: uint8_t
@param flag: flag to set
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_debugflag_set(_cpu, flag)
def cpu_debugflag_unset(self, uint8_t flag):
'''
Method used to unset a cpu debug flag
@type flag: uint8_t
@param flag: flag to unset
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_cpu *_cpu
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_cpu = emu_cpu_get(self._emu)
emu_cpu_debugflag_unset(_cpu, flag)
def cpu_get_current_instruction(self):
'''
Method used to disassemble the current instruction
@rtype : string
@return : disassembled current instruction
Raises RuntimeError if the Emulator is not initialized
'''
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
self.cpu_debugflag_set(1)
self.cpu_parse()
instr_string = emu_cpu_get(self._emu).instr_string
self.cpu_debugflag_unset(1)
return instr_string
# Memory methods
def memory_write_byte(self, uint32_t addr, uint8_t byte):
'''
Method used to write a byte at a memory location
@type addr: uint32_t
@param addr: memory location address
@type byte: uint8_t
@param byte: byte to write
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_memory *_mem
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_mem = emu_memory_get(self._emu)
emu_memory_write_byte(_mem, addr, byte)
def memory_write_word(self, uint32_t addr, uint16_t word):
'''
Method used to write a word at a memory location
@type addr: uint32_t
@param addr: memory location address
@type word: uint16_t
@param word: word to write
Raises RuntimeError if the Emulator is not initialized
'''
cdef c_emu_memory *_mem
if self._emu is NULL:
raise RuntimeError('Emulator not initialized')
_mem = emu_memory_get(self._emu)
emu_memory_write_word(_mem, addr, word)
def memory_write_dword(self, uint32_t addr, uint32_t dword):
'''
Method used to write a dword at a memory location