Skip to content

Commit 8d130f1

Browse files
committed
Major bug fix to correctly update sqlmap to the latest stable release
with command line --update
1 parent bfe1863 commit 8d130f1

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

lib/core/update.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -203,29 +203,25 @@ def __updateMSSQLXML():
203203

204204
def __createFile(pathname, data):
205205
mkpath(os.path.dirname(pathname))
206+
206207
fileFP = open(pathname, "wb")
207208
fileFP.write(data)
208209
fileFP.close()
209210

210211

211-
def __extractZipFile(zipFile):
212+
def __extractZipFile(tempDir, zipFile, sqlmapNewestVersion):
212213
# Check if the saved binary file is really a ZIP file
213214
if zipfile.is_zipfile(zipFile):
214215
sqlmapZipFile = zipfile.ZipFile(zipFile)
215216
else:
216-
raise sqlmapFilePathException, "the downloaded file does not seem to be a zipfile"
217-
218-
# Create a temporary directory
219-
tempDir = tempfile.mkdtemp("", "sqlmap_latest-")
217+
raise sqlmapFilePathException, "the downloaded file does not seem to be a ZIP file"
220218

221219
# Extract each file within the ZIP file in the temporary directory
222220
for info in sqlmapZipFile.infolist():
223221
if info.filename[-1] != '/':
224222
data = sqlmapZipFile.read(info.filename)
225223
__createFile(os.path.join(tempDir, info.filename), data)
226224

227-
return tempDir
228-
229225

230226
def __updateSqlmap():
231227
infoMsg = "updating sqlmap"
@@ -247,6 +243,7 @@ def __updateSqlmap():
247243
return
248244

249245
sqlmapNewestVersion = str(sqlmapNewestVersion).replace("\n", "")
246+
sqlmapNewestVersion = "0.6.1"
250247

251248
if not re.search("^([\w\.\-]+)$", sqlmapNewestVersion):
252249
errMsg = "sqlmap version is in a wrong syntax"
@@ -259,11 +256,19 @@ def __updateSqlmap():
259256
logger.info(infoMsg)
260257

261258
return
262-
else:
259+
260+
elif sqlmapNewestVersion > VERSION:
263261
infoMsg = "sqlmap latest stable version is %s. " % sqlmapNewestVersion
264262
infoMsg += "Going to download it from the SourceForge File List page"
265263
logger.info(infoMsg)
266264

265+
elif sqlmapNewestVersion < VERSION:
266+
infoMsg = "if you are running a version of sqlmap more updated than "
267+
infoMsg += "the latest stable version (%s)" % sqlmapNewestVersion
268+
logger.info(infoMsg)
269+
270+
return
271+
267272
sqlmapBinaryStringUrl = SQLMAP_SOURCE_URL % sqlmapNewestVersion
268273

269274
try:
@@ -278,25 +283,28 @@ def __updateSqlmap():
278283

279284
return
280285

281-
# Save the sqlmap compressed source to a ZIP file in a temporary
282-
# directory and extract it
283-
zipFile = os.path.join(tempfile.gettempdir(), "sqlmap-%s.zip" % sqlmapNewestVersion)
286+
debugMsg = 'saving the sqlmap compressed source to a ZIP file into '
287+
debugMsg += 'the temporary directory and extract it'
288+
logger.debug(debugMsg)
289+
290+
tempDir = tempfile.gettempdir()
291+
zipFile = os.path.join(tempDir, "sqlmap-%s.zip" % sqlmapNewestVersion)
284292
__createFile(zipFile, sqlmapBinaryString)
285-
tempDir = __extractZipFile(zipFile)
293+
__extractZipFile(tempDir, zipFile, sqlmapNewestVersion)
286294

287295
# For each file and directory in the temporary directory copy it
288296
# to the sqlmap root path and set right permission
289297
# TODO: remove files not needed anymore and all pyc within the
290298
# sqlmap root path in the end
291-
for root, dirs, files in os.walk(os.path.join(tempDir, "sqlmap")):
299+
for root, dirs, files in os.walk(os.path.join(tempDir, "sqlmap-%s" % sqlmapNewestVersion)):
292300
# Just for development release
293-
if '.svn' in dirs:
294-
dirs.remove('.svn')
301+
if '.svn' in root:
302+
continue
295303

296304
cleanRoot = root.replace(tempDir, "")
297-
cleanRoot = cleanRoot.replace("%ssqlmap" % os.sep, "")
305+
cleanRoot = cleanRoot.replace("%ssqlmap-%s" % (os.sep, sqlmapNewestVersion), "")
298306

299-
if cleanRoot.startswith("/"):
307+
if cleanRoot.startswith(os.sep):
300308
cleanRoot = cleanRoot[1:]
301309

302310
for f in files:
@@ -307,18 +315,18 @@ def __updateSqlmap():
307315
srcFile = os.path.join(root, f)
308316
dstFile = os.path.join(paths.SQLMAP_ROOT_PATH, os.path.join(cleanRoot, f))
309317

318+
if f == "sqlmap.conf" and os.path.exists(dstFile):
319+
infoMsg = "backupping configuration file to '%s.bak'" % dstFile
320+
logger.info(infoMsg)
321+
shutil.move(dstFile, "%s.bak" % dstFile)
322+
310323
if os.path.exists(dstFile):
311324
debugMsg = "replacing file '%s'" % dstFile
312325
else:
313326
debugMsg = "creating new file '%s'" % dstFile
314327

315328
logger.debug(debugMsg)
316329

317-
if f == "sqlmap.conf" and os.path.exists(dstFile):
318-
infoMsg = "backupping configuration file to '%s.bak'" % dstFile
319-
logger.info(infoMsg)
320-
shutil.move(dstFile, "%s.bak" % dstFile)
321-
322330
mkpath(os.path.dirname(dstFile))
323331
shutil.copy(srcFile, dstFile)
324332

0 commit comments

Comments
 (0)