3030from lib .core .enums import OS
3131from lib .core .exception import SqlmapDataException
3232from lib .core .exception import SqlmapFilePathException
33+ from lib .core .exception import SqlmapGenericException
3334from lib .core .settings import IS_WIN
35+ from lib .core .settings import METERPRETER_INIT_TIMEOUT
3436from lib .core .settings import UNICODE_ENCODING
3537from lib .core .subprocessng import blockingReadFromFD
3638from lib .core .subprocessng import blockingWriteToFD
@@ -443,8 +445,9 @@ def _loadMetExtensions(self, proc, metSess):
443445 send_all (proc , "getuid\n " )
444446
445447 def _controlMsfCmd (self , proc , func ):
448+ initialized = False
449+ start_time = time .time ()
446450 stdin_fd = sys .stdin .fileno ()
447- initiated_properly = False
448451
449452 while True :
450453 returncode = proc .poll ()
@@ -461,7 +464,7 @@ def _controlMsfCmd(self, proc, func):
461464 timeout = 3
462465
463466 inp = ""
464- start_time = time .time ()
467+ _ = time .time ()
465468
466469 while True :
467470 if msvcrt .kbhit ():
@@ -472,7 +475,7 @@ def _controlMsfCmd(self, proc, func):
472475 elif ord (char ) >= 32 : # space_char
473476 inp += char
474477
475- if len (inp ) == 0 and (time .time () - start_time ) > timeout :
478+ if len (inp ) == 0 and (time .time () - _ ) > timeout :
476479 break
477480
478481 if len (inp ) > 0 :
@@ -494,14 +497,6 @@ def _controlMsfCmd(self, proc, func):
494497 out = recv_some (proc , t = .1 , e = 0 )
495498 blockingWriteToFD (sys .stdout .fileno (), out )
496499
497- # Dirty hack to allow Metasploit integration to be tested
498- # in --live-test mode
499- if initiated_properly and conf .liveTest :
500- try :
501- send_all (proc , "exit\n " )
502- except TypeError :
503- continue
504-
505500 # For --os-pwn and --os-bof
506501 pwnBofCond = self .connectionStr .startswith ("reverse" )
507502 pwnBofCond &= "Starting the payload handler" in out
@@ -512,19 +507,20 @@ def _controlMsfCmd(self, proc, func):
512507 if pwnBofCond or smbRelayCond :
513508 func ()
514509
515- if "Starting the payload handler" in out and "shell" in self .payloadStr :
516- if Backend .isOs (OS .WINDOWS ):
517- send_all (proc , "whoami\n " )
518- else :
519- send_all (proc , "uname -a ; id\n " )
520-
521- time .sleep (2 )
522- initiated_properly = True
523-
524- metSess = re .search ("Meterpreter session ([\d]+) opened" , out )
525-
526- if metSess :
527- self ._loadMetExtensions (proc , metSess .group (1 ))
510+ if not initialized :
511+ match = re .search ("session ([\d]+) opened" , out )
512+ if match :
513+ initialized = True
514+ self ._loadMetExtensions (proc , match .group (1 ))
515+ if "shell" in self .payloadStr :
516+ send_all (proc , "whoami\n " if Backend .isOs (OS .WINDOWS ) else "uname -a ; id\n " )
517+ if conf .liveTest :
518+ send_all (proc , "exit\n " )
519+ elif time .time () - start_time > METERPRETER_INIT_TIMEOUT :
520+ proc .kill ()
521+ errMsg = "Timeout occurred while attempting "
522+ errMsg += "to open a remote session"
523+ raise SqlmapGenericException (errMsg )
528524
529525 except EOFError :
530526 returncode = proc .wait ()
0 commit comments