Skip to content

Commit a21a7fc

Browse files
committed
Minor code refactoring
1 parent f6bffb6 commit a21a7fc

5 files changed

Lines changed: 42 additions & 33 deletions

File tree

lib/core/common.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323
"""
2424

25+
import cProfile
2526
import os
2627
import random
2728
import re
@@ -54,6 +55,7 @@
5455
from lib.core.exception import sqlmapSyntaxException
5556
from lib.core.settings import DESCRIPTION
5657
from lib.core.settings import IS_WIN
58+
from lib.core.settings import PLATFORM
5759
from lib.core.settings import SITE
5860
from lib.core.settings import SQL_STATEMENTS
5961
from lib.core.settings import SUPPORTED_DBMS
@@ -1087,45 +1089,48 @@ def isBase64EncodedString(subject):
10871089
def isHexEncodedString(subject):
10881090
return re.match(r"\A[0-9a-fA-F]+\Z", subject) is not None
10891091

1090-
def profile(profileOutputFile='sqlmap.profile', imageOutputFile='profile.png'):
1091-
import cProfile
1092-
cProfile.run("start()", profileOutputFile)
1092+
def profile(profileOutputFile=None, imageOutputFile=None):
1093+
if profileOutputFile is None:
1094+
profileOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.raw")
10931095

1094-
graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py')
1096+
if imageOutputFile is None:
1097+
imageOutputFile = os.path.join(paths.SQLMAP_OUTPUT_PATH, "sqlmap_profile.png")
10951098

1096-
infoMsg = "converting profile data to a graph image."
1097-
logger.info(infoMsg)
1099+
if os.path.exists(profileOutputFile):
1100+
os.remove(profileOutputFile)
10981101

10991102
if os.path.exists(imageOutputFile):
11001103
os.remove(imageOutputFile)
11011104

1102-
msg = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read()
1105+
infoMsg = "profiling the execution into file %s" % profileOutputFile
1106+
logger.info(infoMsg)
11031107

1104-
if msg:
1105-
errMsg = "there was an error while converting ('%s'), " % msg.strip()
1106-
errMsg += "but you can still find raw profile data "
1107-
errMsg += "inside file '%s'" % profileOutputFile
1108-
logger.error(errMsg)
1109-
else:
1110-
try:
1111-
if os.name == 'mac':
1112-
subprocess.call(('open', imageOutputFile))
1113-
elif os.name == 'posix':
1114-
subprocess.call(('xdg-open', imageOutputFile))
1115-
elif os.name == 'nt':
1116-
subprocess.call(('start', imageOutputFile))
1117-
except:
1118-
pass
1108+
cProfile.run("start()", profileOutputFile)
11191109

1120-
if os.path.exists(imageOutputFile):
1121-
infoMsg = "done. you can find a graph image inside file '%s'." % imageOutputFile
1122-
logger.info(infoMsg)
1123-
else:
1124-
errMsg = "there was an error while converting, "
1110+
infoMsg = "converting profile data into a graph image, %s" % imageOutputFile
1111+
logger.info(infoMsg)
1112+
1113+
graphScriptPath = os.path.join(paths.SQLMAP_EXTRAS_PATH, 'gprof2dot', 'gprof2dot.py')
1114+
stderr = subprocess.Popen('python %s -f pstats %s | dot -Tpng -o %s' % (graphScriptPath, profileOutputFile, imageOutputFile), shell=True, stderr=subprocess.PIPE).stderr.read()
1115+
1116+
if stderr or not os.path.exists(imageOutputFile):
1117+
errMsg = "there was an error while converting ('%s')" % stderr.strip()
11251118
errMsg += "but you can still find raw profile data "
11261119
errMsg += "inside file '%s'" % profileOutputFile
11271120
logger.error(errMsg)
11281121

1122+
return
1123+
1124+
try:
1125+
if PLATFORM == 'mac':
1126+
subprocess.call(('open', imageOutputFile))
1127+
elif PLATFORM == 'posix':
1128+
subprocess.call(('xdg-open', imageOutputFile))
1129+
elif PLATFORM == 'nt':
1130+
subprocess.call(('start', imageOutputFile))
1131+
except:
1132+
pass
1133+
11291134
def getConsoleWidth(default=80):
11301135
width = None
11311136

lib/core/option.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def __setMetasploit():
323323
if conf.osSmb:
324324
isAdmin = False
325325

326-
if "linux" in PLATFORM or "darwin" in PLATFORM:
326+
if PLATFORM in ( "posix", "mac" ):
327327
isAdmin = os.geteuid()
328328

329329
if isinstance(isAdmin, (int, float, long)) and isAdmin == 0:

lib/core/readlineng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
# Thanks to Boyd Waters for this patch.
6262
uses_libedit = False
6363

64-
if PLATFORM == 'darwin' and haveReadline:
64+
if PLATFORM == 'mac' and haveReadline:
6565
import commands
6666

6767
(status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ )

lib/core/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424

2525
import logging
26+
import os
2627
import subprocess
2728
import sys
2829

@@ -46,7 +47,10 @@
4647

4748
# System variables
4849
IS_WIN = subprocess.mswindows
49-
PLATFORM = sys.platform.lower()
50+
# The name of the operating system dependent module imported. The following
51+
# names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce',
52+
# 'java', 'riscos'
53+
PLATFORM = os.name
5054
PYVERSION = sys.version.split()[0]
5155

5256
# Url to update Microsoft SQL Server XML versions file from

lib/takeover/upx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ class UPX:
4646
"""
4747

4848
def __initialize(self, srcFile, dstFile=None):
49-
if "darwin" in PLATFORM:
49+
if PLATFORM == "mac":
5050
self.__upxPath = "%s/upx/macosx/upx" % paths.SQLMAP_CONTRIB_PATH
5151

52-
elif "win" in PLATFORM:
52+
elif PLATFORM in ( "ce", "nt" ):
5353
self.__upxTempExe = decloakToMkstemp("%s\upx\windows\upx.exe_" % paths.SQLMAP_CONTRIB_PATH, suffix=".exe")
5454
self.__upxPath = self.__upxTempExe.name
5555
self.__upxTempExe.close() #needed for execution rights
5656

57-
elif "linux" in PLATFORM:
57+
elif PLATFORM == "posix":
5858
self.__upxPath = "%s/upx/linux/upx" % paths.SQLMAP_CONTRIB_PATH
5959

6060
else:

0 commit comments

Comments
 (0)