2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
from subprocess import PIPE
|
|
|
|
from subprocess import STDOUT
|
|
|
|
from subprocess import Popen as execute
|
|
|
|
|
|
|
|
from lib.core.common import dataToStdout
|
2010-01-29 13:12:09 +03:00
|
|
|
from lib.core.common import decloakToMkstemp
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import pollProcess
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.data import paths
|
|
|
|
from lib.core.settings import PLATFORM
|
|
|
|
|
|
|
|
class UPX:
|
|
|
|
"""
|
|
|
|
This class defines methods to compress binary files with UPX (Ultimate
|
|
|
|
Packer for eXecutables).
|
|
|
|
|
|
|
|
Reference:
|
|
|
|
* http://upx.sourceforge.net
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __initialize(self, srcFile, dstFile=None):
|
2010-05-21 16:09:31 +04:00
|
|
|
if PLATFORM == "mac":
|
2009-04-30 14:46:50 +04:00
|
|
|
self.__upxPath = "%s/upx/macosx/upx" % paths.SQLMAP_CONTRIB_PATH
|
|
|
|
|
2010-05-21 16:09:31 +04:00
|
|
|
elif PLATFORM in ( "ce", "nt" ):
|
2010-01-29 13:12:09 +03:00
|
|
|
self.__upxTempExe = decloakToMkstemp("%s\upx\windows\upx.exe_" % paths.SQLMAP_CONTRIB_PATH, suffix=".exe")
|
|
|
|
self.__upxPath = self.__upxTempExe.name
|
|
|
|
self.__upxTempExe.close() #needed for execution rights
|
2009-04-30 14:46:50 +04:00
|
|
|
|
2010-05-21 16:09:31 +04:00
|
|
|
elif PLATFORM == "posix":
|
2009-04-22 15:48:07 +04:00
|
|
|
self.__upxPath = "%s/upx/linux/upx" % paths.SQLMAP_CONTRIB_PATH
|
|
|
|
|
2009-04-30 14:46:50 +04:00
|
|
|
else:
|
|
|
|
warnMsg = "unsupported platform for the compression tool "
|
|
|
|
warnMsg += "(upx), sqlmap will continue anyway"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
self.__upxPath = "%s/upx/linux/upx" % paths.SQLMAP_CONTRIB_PATH
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
self.__upxCmd = "%s -9 -qq %s" % (self.__upxPath, srcFile)
|
|
|
|
|
|
|
|
if dstFile:
|
|
|
|
self.__upxCmd += " -o %s" % dstFile
|
|
|
|
|
|
|
|
def pack(self, srcFile, dstFile=None):
|
|
|
|
self.__initialize(srcFile, dstFile)
|
|
|
|
|
|
|
|
logger.debug("executing local command: %s" % self.__upxCmd)
|
|
|
|
process = execute(self.__upxCmd, shell=True, stdout=PIPE, stderr=STDOUT)
|
2010-01-29 13:12:09 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] compression in progress " % time.strftime("%X"))
|
|
|
|
pollProcess(process)
|
2009-07-20 18:36:33 +04:00
|
|
|
upxStdout, upxStderr = process.communicate()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-29 22:32:02 +03:00
|
|
|
if hasattr(self, '__upxTempExe'):
|
2010-01-29 13:15:05 +03:00
|
|
|
os.remove(self.__upxTempExe.name)
|
|
|
|
|
2009-12-18 01:04:01 +03:00
|
|
|
msg = "failed to compress the file"
|
2009-07-20 18:36:33 +04:00
|
|
|
|
|
|
|
if "NotCompressibleException" in upxStdout:
|
2009-12-18 01:04:01 +03:00
|
|
|
msg += " because you provided a Metasploit version above "
|
|
|
|
msg += "3.3-dev revision 6681. This will not inficiate "
|
|
|
|
msg += "the correct execution of sqlmap. It might "
|
|
|
|
msg += "only slow down a bit the execution"
|
|
|
|
logger.debug(msg)
|
2009-07-20 18:36:33 +04:00
|
|
|
|
|
|
|
elif upxStderr:
|
2009-12-18 01:04:01 +03:00
|
|
|
logger.warn(msg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
else:
|
|
|
|
return os.path.getsize(srcFile)
|
|
|
|
|
2009-07-20 18:36:33 +04:00
|
|
|
return None
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def unpack(self, srcFile, dstFile=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def verify(self, filePath):
|
|
|
|
pass
|
|
|
|
|
|
|
|
upx = UPX()
|