33# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44#
55
6- import sys , subprocess , threading , unittest , socket , time
7-
8- from binascii import unhexlify
6+ import sys , threading , unittest , socket , time
97
108sys .path .append (".." )
11- from qiling import *
12- from qiling .exception import *
9+ from qiling import Qiling
1310from qiling .const import QL_VERBOSE
1411
15- DELAY = 1
16-
17- def checksum (data ):
18- checksum = 0
19- for c in data :
20- if type (c ) == str :
21- checksum += (ord (c ))
22- else :
23- checksum += c
24- return checksum & 0xff
25-
26- def send_raw (netout , r ):
27- netout .write (r )
28- netout .flush ()
29-
30- def send (netout , msg ):
31- time .sleep (DELAY )
32- send_raw (netout , '$%s#%.2x' % (msg , checksum (msg )))
12+ class SimpleGdbClient :
13+ DELAY = 0.6
14+
15+ def __init__ (self , host : str , port : int ):
16+ sock = socket .socket (socket .AF_INET ,socket .SOCK_STREAM )
17+ txtf = sock .makefile ('w' )
18+
19+ sock .connect ((host , port ))
20+
21+ self .__sock = sock
22+ self .__file = txtf
23+
24+ def __enter__ (self ):
25+ return self
26+
27+ def __exit__ (self , ex_type , ex_value , ex_traceback ):
28+ self .__sock .close ()
29+
30+ @staticmethod
31+ def checksum (data : str ) -> int :
32+ return sum (ord (c ) for c in data ) & 0xff
33+
34+ def send (self , msg : str ):
35+ time .sleep (SimpleGdbClient .DELAY )
36+
37+ self .__file .write (f'${ msg } #{ SimpleGdbClient .checksum (msg ):02x} ' )
38+ self .__file .flush ()
3339
3440class DebuggerTest (unittest .TestCase ):
35-
41+
3642 def test_pe_gdbdebug (self ):
3743 ql = Qiling (["../examples/rootfs/x86_windows/bin/x86_hello.exe" ], "../examples/rootfs/x86_windows/" , verbose = QL_VERBOSE .DEBUG )
38- ql .debugger = " 127.0.0.1:9996"
44+ ql .debugger = 'gdb: 127.0.0.1:9996'
3945
4046 # some random command test just to make sure we covered most of the command
4147 def gdb_test_client ():
42- time .sleep (DELAY )
43- gdb_client = socket .socket (socket .AF_INET ,socket .SOCK_STREAM )
44- netout = gdb_client .makefile ('w' )
45- gdb_client .connect (('127.0.0.1' ,9996 ))
46- time .sleep (DELAY )
47- send (netout , "qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;no-resumed+;xmlRegisters=i386" )
48- time .sleep (DELAY )
49- send (netout , "vMustReplyEmpty" )
50- time .sleep (DELAY )
51- send (netout , "QStartNoAckMode" )
52- time .sleep (DELAY )
53- send (netout , "Hgp0.0" )
54- time .sleep (DELAY )
55- send (netout , "qXfer:auxv:read::0, 1000" )
56- time .sleep (DELAY )
57- send (netout , "?" )
58- time .sleep (DELAY )
59- send (netout , "qXfer:threads:read::0,fff" )
60- time .sleep (DELAY )
61- send (netout , "qAttached:" + str (ql .os .pid ))
62- time .sleep (DELAY )
63- send (netout , "qC" )
64- time .sleep (DELAY )
65- send (netout , "g" )
66- time .sleep (DELAY )
67- send (netout , "m200, 100" )
68- time .sleep (DELAY )
69- send (netout , "p10" )
70- time .sleep (DELAY )
71- send (netout , "c" )
72- time .sleep (DELAY )
73- send (netout , "k" )
74- time .sleep (DELAY )
75- gdb_client .close ()
76-
77- debugger_file_therad = threading .Thread (target = gdb_test_client , daemon = True )
78- debugger_file_therad .start ()
79-
48+ # yield to allow ql to launch its gdbserver
49+ time .sleep (1.337 * 2 )
50+
51+ with SimpleGdbClient ('127.0.0.1' , 9996 ) as client :
52+ client .send ('qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;no-resumed+;xmlRegisters=i386' )
53+ client .send ('vMustReplyEmpty' )
54+ client .send ('QStartNoAckMode' )
55+ client .send ('Hgp0.0' )
56+ client .send ('qXfer:auxv:read::0, 1000' )
57+ client .send ('?' )
58+ client .send ('qXfer:threads:read::0,fff' )
59+ client .send ('qAttached:' + str (ql .os .pid ))
60+ client .send ('qC' )
61+ client .send ('g' )
62+ client .send ('m200, 100' )
63+ client .send ('p10' )
64+ client .send ('c' )
65+ client .send ('k' )
66+
67+ # yield to make sure ql gdbserver has enough time to receive our last command
68+ time .sleep (1.337 )
69+
70+ threading .Thread (target = gdb_test_client , daemon = True ).start ()
71+
8072 ql .run ()
8173 del ql
8274
83- if __name__ == " __main__" :
75+ if __name__ == ' __main__' :
8476 unittest .main ()
0 commit comments