sqlmap/lib/core/update.py

142 lines
6.0 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2008-10-15 19:38:22 +04:00
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2008-10-15 19:38:22 +04:00
"""
import glob
2012-07-04 00:13:01 +04:00
import os
2008-10-15 19:38:22 +04:00
import re
import shutil
2016-12-20 01:47:39 +03:00
import subprocess
2010-01-18 17:59:24 +03:00
import time
import zipfile
2008-10-15 19:38:22 +04:00
2010-01-18 17:05:23 +03:00
from lib.core.common import dataToStdout
from lib.core.common import getLatestRevision
2019-06-04 15:44:06 +03:00
from lib.core.common import getSafeExString
2019-05-13 12:51:47 +03:00
from lib.core.common import openFile
from lib.core.common import pollProcess
from lib.core.common import readInput
2019-05-27 14:09:13 +03:00
from lib.core.convert import getText
2008-10-15 19:38:22 +04:00
from lib.core.data import conf
from lib.core.data import logger
from lib.core.data import paths
2012-07-26 02:02:38 +04:00
from lib.core.revision import getRevisionNumber
2012-07-08 21:24:25 +04:00
from lib.core.settings import GIT_REPOSITORY
from lib.core.settings import IS_WIN
from lib.core.settings import VERSION
from lib.core.settings import ZIPBALL_PAGE
2019-05-03 00:51:54 +03:00
from thirdparty.six.moves import urllib as _urllib
2008-10-15 19:38:22 +04:00
def update():
2012-07-02 03:00:46 +04:00
if not conf.updateAll:
return
2012-07-04 00:13:01 +04:00
success = False
2010-01-18 17:59:24 +03:00
2015-10-22 21:51:05 +03:00
if not os.path.exists(os.path.join(paths.SQLMAP_ROOT_PATH, ".git")):
warnMsg = "not a git repository. It is recommended to clone the 'sqlmapproject/sqlmap' repository "
warnMsg += "from GitHub (e.g. 'git clone --depth 1 %s sqlmap')" % GIT_REPOSITORY
logger.warn(warnMsg)
if VERSION == getLatestRevision():
logger.info("already at the latest revision '%s'" % getRevisionNumber())
return
message = "do you want to try to fetch the latest 'zipball' from repository and extract it (experimental) ? [y/N]"
if readInput(message, default='N', boolean=True):
directory = os.path.abspath(paths.SQLMAP_ROOT_PATH)
try:
open(os.path.join(directory, "sqlmap.py"), "w+b")
2019-01-22 02:40:48 +03:00
except Exception as ex:
errMsg = "unable to update content of directory '%s' ('%s')" % (directory, getSafeExString(ex))
logger.error(errMsg)
else:
2018-06-01 12:23:41 +03:00
attrs = os.stat(os.path.join(directory, "sqlmap.py")).st_mode
for wildcard in ('*', ".*"):
for _ in glob.glob(os.path.join(directory, wildcard)):
try:
if os.path.isdir(_):
shutil.rmtree(_)
else:
os.remove(_)
except:
pass
if glob.glob(os.path.join(directory, '*')):
errMsg = "unable to clear the content of directory '%s'" % directory
logger.error(errMsg)
else:
try:
2019-05-03 00:51:54 +03:00
archive = _urllib.request.urlretrieve(ZIPBALL_PAGE)[0]
with zipfile.ZipFile(archive) as f:
for info in f.infolist():
info.filename = re.sub(r"\Asqlmap[^/]+", "", info.filename)
if info.filename:
f.extract(info, directory)
filepath = os.path.join(paths.SQLMAP_ROOT_PATH, "lib", "core", "settings.py")
if os.path.isfile(filepath):
2019-05-13 12:51:47 +03:00
with openFile(filepath, "rb") as f:
version = re.search(r"(?m)^VERSION\s*=\s*['\"]([^'\"]+)", f.read()).group(1)
logger.info("updated to the latest version '%s#dev'" % version)
success = True
2019-01-22 02:40:48 +03:00
except Exception as ex:
logger.error("update could not be completed ('%s')" % getSafeExString(ex))
else:
if not success:
logger.error("update could not be completed")
2018-06-01 12:23:41 +03:00
else:
2018-06-09 23:59:08 +03:00
try:
os.chmod(os.path.join(directory, "sqlmap.py"), attrs)
except OSError:
logger.warning("could not set the file attributes of '%s'" % os.path.join(directory, "sqlmap.py"))
2012-07-04 00:13:01 +04:00
else:
2018-01-21 13:49:50 +03:00
infoMsg = "updating sqlmap to the latest development revision from the "
2012-07-04 00:13:01 +04:00
infoMsg += "GitHub repository"
logger.info(infoMsg)
2010-01-18 17:59:24 +03:00
2012-07-04 00:13:01 +04:00
debugMsg = "sqlmap will try to update itself using 'git' command"
logger.debug(debugMsg)
2019-01-29 16:59:31 +03:00
dataToStdout("\r[%s] [INFO] update in progress" % time.strftime("%X"))
2015-10-17 00:59:39 +03:00
2019-08-27 14:41:30 +03:00
output = ""
2015-10-17 00:59:39 +03:00
try:
2019-05-19 08:44:32 +03:00
process = subprocess.Popen("git checkout . && git pull %s HEAD" % GIT_REPOSITORY, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=paths.SQLMAP_ROOT_PATH)
2015-10-17 00:59:39 +03:00
pollProcess(process, True)
2019-05-13 12:08:25 +03:00
output, _ = process.communicate()
2015-10-17 00:59:39 +03:00
success = not process.returncode
2019-05-19 08:44:32 +03:00
except Exception as ex:
2015-10-17 00:59:39 +03:00
success = False
2019-05-13 12:08:25 +03:00
output = getSafeExString(ex)
finally:
output = getText(output)
2011-02-01 01:51:14 +03:00
2012-07-04 00:13:01 +04:00
if success:
2019-05-13 12:08:25 +03:00
logger.info("%s the latest revision '%s'" % ("already at" if "Already" in output else "updated to", getRevisionNumber()))
2012-07-04 00:13:01 +04:00
else:
2019-05-13 12:08:25 +03:00
if "Not a git repository" in output:
errMsg = "not a valid git repository. Please checkout the 'sqlmapproject/sqlmap' repository "
errMsg += "from GitHub (e.g. 'git clone --depth 1 %s sqlmap')" % GIT_REPOSITORY
logger.error(errMsg)
else:
2019-05-13 12:08:25 +03:00
logger.error("update could not be completed ('%s')" % re.sub(r"\W+", " ", output).strip())
2012-07-03 18:49:34 +04:00
2012-07-04 00:13:01 +04:00
if not success:
2012-07-03 18:49:34 +04:00
if IS_WIN:
infoMsg = "for Windows platform it's recommended "
infoMsg += "to use a GitHub for Windows client for updating "
2012-07-04 22:28:18 +04:00
infoMsg += "purposes (http://windows.github.com/) or just "
infoMsg += "download the latest snapshot from "
infoMsg += "https://github.com/sqlmapproject/sqlmap/downloads"
2012-07-03 18:49:34 +04:00
else:
infoMsg = "for Linux platform it's recommended "
2019-12-10 00:13:52 +03:00
infoMsg += "to install a standard 'git' package (e.g.: 'sudo apt install git')"
2012-07-03 18:34:11 +04:00
logger.info(infoMsg)