Minor code refactoring

This commit is contained in:
Bernardo Damele 2010-05-21 12:09:31 +00:00
parent f6bffb61d3
commit a21a7fc56d
5 changed files with 42 additions and 33 deletions

View File

@ -22,6 +22,7 @@ with sqlmap; if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
""" """
import cProfile
import os import os
import random import random
import re import re
@ -54,6 +55,7 @@ from lib.core.exception import sqlmapMissingDependence
from lib.core.exception import sqlmapSyntaxException from lib.core.exception import sqlmapSyntaxException
from lib.core.settings import DESCRIPTION from lib.core.settings import DESCRIPTION
from lib.core.settings import IS_WIN from lib.core.settings import IS_WIN
from lib.core.settings import PLATFORM
from lib.core.settings import SITE from lib.core.settings import SITE
from lib.core.settings import SQL_STATEMENTS from lib.core.settings import SQL_STATEMENTS
from lib.core.settings import SUPPORTED_DBMS from lib.core.settings import SUPPORTED_DBMS
@ -1087,44 +1089,47 @@ def isBase64EncodedString(subject):
def isHexEncodedString(subject): def isHexEncodedString(subject):
return re.match(r"\A[0-9a-fA-F]+\Z", subject) is not None return re.match(r"\A[0-9a-fA-F]+\Z", subject) is not None
def profile(profileOutputFile='sqlmap.profile', imageOutputFile='profile.png'): def profile(profileOutputFile=None, imageOutputFile=None):
import cProfile if profileOutputFile is None:
cProfile.run("start()", profileOutputFile) profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw")
graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py') if imageOutputFile is None:
imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png")
infoMsg = "converting profile data to a graph image." if os.path.exists(profileOutputFile):
logger.info(infoMsg) os.remove(profileOutputFile)
if os.path.exists(imageOutputFile): if os.path.exists(imageOutputFile):
os.remove(imageOutputFile) os.remove(imageOutputFile)
msg = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read() infoMsg = "profiling the execution into file %s" % profileOutputFile
logger.info(infoMsg)
if msg: cProfile.run("start()", profileOutputFile)
errMsg = "there was an error while converting ('%s'), " % msg.strip()
infoMsg = "converting profile data into a graph image, %s" % imageOutputFile
logger.info(infoMsg)
graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py')
stderr = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read()
if stderr or not os.path.exists(imageOutputFile):
errMsg = "there was an error while converting ('%s')" % stderr.strip()
errMsg += "but you can still find raw profile data " errMsg += "but you can still find raw profile data "
errMsg += "inside file '%s'" % profileOutputFile errMsg += "inside file '%s'" % profileOutputFile
logger.error(errMsg) logger.error(errMsg)
else:
try:
if os.name == 'mac':
subprocess.call(('open', imageOutputFile))
elif os.name == 'posix':
subprocess.call(('xdg-open', imageOutputFile))
elif os.name == 'nt':
subprocess.call(('start', imageOutputFile))
except:
pass
if os.path.exists(imageOutputFile): return
infoMsg = "done. you can find a graph image inside file '%s'." % imageOutputFile
logger.info(infoMsg) try:
else: if PLATFORM == 'mac':
errMsg = "there was an error while converting, " subprocess.call(('open', imageOutputFile))
errMsg += "but you can still find raw profile data " elif PLATFORM == 'posix':
errMsg += "inside file '%s'" % profileOutputFile subprocess.call(('xdg-open', imageOutputFile))
logger.error(errMsg) elif PLATFORM == 'nt':
subprocess.call(('start', imageOutputFile))
except:
pass
def getConsoleWidth(default=80): def getConsoleWidth(default=80):
width = None width = None

View File

@ -323,7 +323,7 @@ def __setMetasploit():
if conf.osSmb: if conf.osSmb:
isAdmin = False isAdmin = False
if "linux" in PLATFORM or "darwin" in PLATFORM: if PLATFORM in ( "posix", "mac" ):
isAdmin = os.geteuid() isAdmin = os.geteuid()
if isinstance(isAdmin, (int, float, long)) and isAdmin == 0: if isinstance(isAdmin, (int, float, long)) and isAdmin == 0:

View File

@ -61,7 +61,7 @@ if IS_WIN and haveReadline:
# Thanks to Boyd Waters for this patch. # Thanks to Boyd Waters for this patch.
uses_libedit = False uses_libedit = False
if PLATFORM == 'darwin' and haveReadline: if PLATFORM == 'mac' and haveReadline:
import commands import commands
(status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ ) (status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ )

View File

@ -23,6 +23,7 @@ Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
""" """
import logging import logging
import os
import subprocess import subprocess
import sys import sys
@ -46,7 +47,10 @@ LOGGER.setLevel(logging.WARN)
# System variables # System variables
IS_WIN = subprocess.mswindows IS_WIN = subprocess.mswindows
PLATFORM = sys.platform.lower() # The name of the operating system dependent module imported. The following
# names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce',
# 'java', 'riscos'
PLATFORM = os.name
PYVERSION = sys.version.split()[0] PYVERSION = sys.version.split()[0]
# Url to update Microsoft SQL Server XML versions file from # Url to update Microsoft SQL Server XML versions file from

View File

@ -46,15 +46,15 @@ class UPX:
""" """
def __initialize(self, srcFile, dstFile=None): def __initialize(self, srcFile, dstFile=None):
if "darwin" in PLATFORM: if PLATFORM == "mac":
self.__upxPath = "%s/upx/macosx/upx" % paths.SQLMAP_CONTRIB_PATH self.__upxPath = "%s/upx/macosx/upx" % paths.SQLMAP_CONTRIB_PATH
elif "win" in PLATFORM: elif PLATFORM in ( "ce", "nt" ):
self.__upxTempExe = decloakToMkstemp("%s\upx\windows\upx.exe_" % paths.SQLMAP_CONTRIB_PATH, suffix=".exe") self.__upxTempExe = decloakToMkstemp("%s\upx\windows\upx.exe_" % paths.SQLMAP_CONTRIB_PATH, suffix=".exe")
self.__upxPath = self.__upxTempExe.name self.__upxPath = self.__upxTempExe.name
self.__upxTempExe.close() #needed for execution rights self.__upxTempExe.close() #needed for execution rights
elif "linux" in PLATFORM: elif PLATFORM == "posix":
self.__upxPath = "%s/upx/linux/upx" % paths.SQLMAP_CONTRIB_PATH self.__upxPath = "%s/upx/linux/upx" % paths.SQLMAP_CONTRIB_PATH
else: else: