Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 323cf2b

Browse files
committed
Fixes #177 - Don't exit at exception if in "multiple targets" mode (-l or -g)
1 parent 6d0ea86 commit 323cf2b

4 files changed

Lines changed: 151 additions & 153 deletions

File tree

lib/controller/checks.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,11 @@ def checkStability():
302302
logMsg = "url is stable"
303303
logger.info(logMsg)
304304
else:
305-
exceptionMsg = "there was an error checking the stability of page "
306-
exceptionMsg += "because of lack of content. please check the "
307-
exceptionMsg += "page request results (and probable errors) by "
308-
exceptionMsg += "using higher verbosity levels"
309-
raise sqlmapNoneDataException, exceptionMsg
305+
errMsg = "there was an error checking the stability of page "
306+
errMsg += "because of lack of content. please check the "
307+
errMsg += "page request results (and probable errors) by "
308+
errMsg += "using higher verbosity levels"
309+
raise sqlmapNoneDataException, errMsg
310310

311311
elif not condition:
312312
warnMsg = "url is not stable, sqlmap will base the page "
@@ -387,15 +387,8 @@ def checkConnection():
387387
page, _ = Request.getPage()
388388
conf.seqMatcher.set_seq1(page)
389389

390-
except sqlmapConnectionException, exceptionMsg:
391-
exceptionMsg = str(exceptionMsg)
392-
393-
if conf.multipleTargets:
394-
exceptionMsg += ", skipping to next url"
395-
logger.warn(exceptionMsg)
396-
397-
return False
398-
else:
399-
raise sqlmapConnectionException, exceptionMsg
390+
except sqlmapConnectionException, errMsg:
391+
errMsg = str(errMsg)
392+
raise sqlmapConnectionException, errMsg
400393

401394
return True

lib/controller/controller.py

Lines changed: 135 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from lib.core.data import conf
3636
from lib.core.data import kb
3737
from lib.core.data import logger
38+
from lib.core.exception import exceptionsTuple
3839
from lib.core.exception import sqlmapNotVulnerableException
3940
from lib.core.session import setInjection
4041
from lib.core.target import initTargetEnv
@@ -88,6 +89,9 @@ def start():
8889
check if they are dynamic and SQL injection affected
8990
"""
9091

92+
if not conf.start:
93+
return
94+
9195
if conf.url:
9296
kb.targetUrls.add(( conf.url, conf.method, conf.data, conf.cookie ))
9397

@@ -105,158 +109,165 @@ def start():
105109
setCookieAsInjectable = True
106110

107111
for targetUrl, targetMethod, targetData, targetCookie in kb.targetUrls:
108-
conf.url = targetUrl
109-
conf.method = targetMethod
110-
conf.data = targetData
111-
conf.cookie = targetCookie
112-
injData = []
112+
try:
113+
conf.url = targetUrl
114+
conf.method = targetMethod
115+
conf.data = targetData
116+
conf.cookie = targetCookie
117+
injData = []
118+
119+
if conf.multipleTargets:
120+
hostCount += 1
121+
message = "url %d:\n%s %s" % (hostCount, conf.method or "GET", targetUrl)
113122

114-
if conf.multipleTargets:
115-
hostCount += 1
116-
message = "url %d:\n%s %s" % (hostCount, conf.method or "GET", targetUrl)
123+
if conf.cookie:
124+
message += "\nCookie: %s" % conf.cookie
117125

118-
if conf.cookie:
119-
message += "\nCookie: %s" % conf.cookie
126+
if conf.data:
127+
message += "\nPOST data: %s" % conf.data
120128

121-
if conf.data:
122-
message += "\nPOST data: %s" % conf.data
129+
message += "\ndo you want to test this url? [Y/n/q]"
130+
test = readInput(message, default="Y")
123131

124-
message += "\ndo you want to test this url? [Y/n/q]"
125-
test = readInput(message, default="Y")
132+
if not test:
133+
pass
134+
elif test[0] in ("n", "N"):
135+
continue
136+
elif test[0] in ("q", "Q"):
137+
break
126138

127-
if not test:
128-
pass
129-
elif test[0] in ("n", "N"):
139+
logMsg = "testing url %s" % targetUrl
140+
logger.info(logMsg)
141+
142+
initTargetEnv()
143+
parseTargetUrl()
144+
setupTargetEnv()
145+
146+
if not checkConnection() or not checkString() or not checkRegexp():
130147
continue
131-
elif test[0] in ("q", "Q"):
132-
break
133148

134-
logMsg = "testing url %s" % targetUrl
135-
logger.info(logMsg)
149+
if not conf.dropSetCookie:
150+
for _, cookie in enumerate(conf.cj):
151+
cookie = str(cookie)
152+
index = cookie.index(" for ")
136153

137-
initTargetEnv()
138-
parseTargetUrl()
139-
setupTargetEnv()
140-
141-
if not checkConnection() or not checkString() or not checkRegexp():
142-
continue
143-
144-
if not conf.dropSetCookie:
145-
for _, cookie in enumerate(conf.cj):
146-
cookie = str(cookie)
147-
index = cookie.index(" for ")
148-
149-
cookieStr += "%s;" % cookie[8:index]
150-
151-
if cookieStr:
152-
cookieStr = cookieStr[:-1]
153-
154-
if "Cookie" in conf.parameters:
155-
message = "you provided an HTTP Cookie header value. "
156-
message += "The target url provided its own Cookie within "
157-
message += "the HTTP Set-Cookie header. Do you want to "
158-
message += "continue using the HTTP Cookie values that "
159-
message += "you provided? [Y/n] "
160-
test = readInput(message, default="Y")
161-
162-
if not test or test[0] in ("y", "Y"):
163-
setCookieAsInjectable = False
164-
165-
if setCookieAsInjectable:
166-
conf.httpHeaders.append(("Cookie", cookieStr))
167-
conf.parameters["Cookie"] = cookieStr
168-
__paramDict = paramToDict("Cookie", cookieStr)
169-
170-
if __paramDict:
171-
conf.paramDict["Cookie"] = __paramDict
172-
__testableParameters = True
173-
174-
if not kb.injPlace or not kb.injParameter or not kb.injType:
175-
if not conf.string and not conf.regexp and not conf.eRegexp:
176-
# NOTE: this is not needed anymore, leaving only to display
177-
# a warning message to the user in case the page is not stable
178-
checkStability()
179-
180-
for place in conf.parameters.keys():
181-
if not conf.paramDict.has_key(place):
182-
continue
154+
cookieStr += "%s;" % cookie[8:index]
183155

184-
paramDict = conf.paramDict[place]
156+
if cookieStr:
157+
cookieStr = cookieStr[:-1]
158+
159+
if "Cookie" in conf.parameters:
160+
message = "you provided an HTTP Cookie header value. "
161+
message += "The target url provided its own Cookie within "
162+
message += "the HTTP Set-Cookie header. Do you want to "
163+
message += "continue using the HTTP Cookie values that "
164+
message += "you provided? [Y/n] "
165+
test = readInput(message, default="Y")
166+
167+
if not test or test[0] in ("y", "Y"):
168+
setCookieAsInjectable = False
169+
170+
if setCookieAsInjectable:
171+
conf.httpHeaders.append(("Cookie", cookieStr))
172+
conf.parameters["Cookie"] = cookieStr
173+
__paramDict = paramToDict("Cookie", cookieStr)
174+
175+
if __paramDict:
176+
conf.paramDict["Cookie"] = __paramDict
177+
__testableParameters = True
185178

186-
for parameter, value in paramDict.items():
187-
testSqlInj = True
179+
if not kb.injPlace or not kb.injParameter or not kb.injType:
180+
if not conf.string and not conf.regexp and not conf.eRegexp:
181+
# NOTE: this is not needed anymore, leaving only to display
182+
# a warning message to the user in case the page is not stable
183+
checkStability()
188184

189-
# Avoid dinamicity test if the user provided the
190-
# parameter manually
191-
if parameter in conf.testParameter:
192-
pass
185+
for place in conf.parameters.keys():
186+
if not conf.paramDict.has_key(place):
187+
continue
193188

194-
elif not checkDynParam(place, parameter, value):
195-
warnMsg = "%s parameter '%s' is not dynamic" % (place, parameter)
196-
logger.warn(warnMsg)
197-
testSqlInj = False
189+
paramDict = conf.paramDict[place]
198190

199-
else:
200-
logMsg = "%s parameter '%s' is dynamic" % (place, parameter)
201-
logger.info(logMsg)
191+
for parameter, value in paramDict.items():
192+
testSqlInj = True
193+
194+
# Avoid dinamicity test if the user provided the
195+
# parameter manually
196+
if parameter in conf.testParameter:
197+
pass
198+
199+
elif not checkDynParam(place, parameter, value):
200+
warnMsg = "%s parameter '%s' is not dynamic" % (place, parameter)
201+
logger.warn(warnMsg)
202+
testSqlInj = False
202203

203-
if testSqlInj:
204-
for parenthesis in range(0, 4):
205-
logMsg = "testing sql injection on %s " % place
206-
logMsg += "parameter '%s' with " % parameter
207-
logMsg += "%d parenthesis" % parenthesis
204+
else:
205+
logMsg = "%s parameter '%s' is dynamic" % (place, parameter)
208206
logger.info(logMsg)
209207

210-
injType = checkSqlInjection(place, parameter, value, parenthesis)
208+
if testSqlInj:
209+
for parenthesis in range(0, 4):
210+
logMsg = "testing sql injection on %s " % place
211+
logMsg += "parameter '%s' with " % parameter
212+
logMsg += "%d parenthesis" % parenthesis
213+
logger.info(logMsg)
211214

212-
if injType:
213-
injData.append((place, parameter, injType))
215+
injType = checkSqlInjection(place, parameter, value, parenthesis)
214216

215-
break
216-
else:
217-
infoMsg = "%s parameter '%s' is not " % (place, parameter)
218-
infoMsg += "injectable with %d parenthesis" % parenthesis
219-
logger.info(infoMsg)
217+
if injType:
218+
injData.append((place, parameter, injType))
220219

221-
if not injData:
222-
warnMsg = "%s parameter '%s' is not " % (place, parameter)
223-
warnMsg += "injectable"
224-
logger.warn(warnMsg)
220+
break
221+
else:
222+
infoMsg = "%s parameter '%s' is not " % (place, parameter)
223+
infoMsg += "injectable with %d parenthesis" % parenthesis
224+
logger.info(infoMsg)
225225

226-
if not kb.injPlace or not kb.injParameter or not kb.injType:
227-
if len(injData) == 1:
228-
injDataSelected = injData[0]
226+
if not injData:
227+
warnMsg = "%s parameter '%s' is not " % (place, parameter)
228+
warnMsg += "injectable"
229+
logger.warn(warnMsg)
229230

230-
elif len(injData) > 1:
231-
injDataSelected = __selectInjection(injData)
231+
if not kb.injPlace or not kb.injParameter or not kb.injType:
232+
if len(injData) == 1:
233+
injDataSelected = injData[0]
232234

233-
elif conf.multipleTargets:
234-
continue
235+
elif len(injData) > 1:
236+
injDataSelected = __selectInjection(injData)
235237

236-
else:
237-
return
238+
else:
239+
raise sqlmapNotVulnerableException, "all parameters are not injectable"
240+
return
238241

239-
if injDataSelected == "Quit":
240-
return
242+
if injDataSelected == "Quit":
243+
return
241244

242-
else:
243-
kb.injPlace, kb.injParameter, kb.injType = injDataSelected
244-
setInjection()
245+
else:
246+
kb.injPlace, kb.injParameter, kb.injType = injDataSelected
247+
setInjection()
245248

246-
if not conf.multipleTargets and ( not kb.injPlace or not kb.injParameter or not kb.injType ):
247-
raise sqlmapNotVulnerableException, "all parameters are not injectable"
248-
elif kb.injPlace and kb.injParameter and kb.injType:
249-
if conf.multipleTargets:
250-
message = "do you want to exploit this SQL injection? [Y/n] "
251-
exploit = readInput(message, default="Y")
249+
elif kb.injPlace and kb.injParameter and kb.injType:
250+
if conf.multipleTargets:
251+
message = "do you want to exploit this SQL injection? [Y/n] "
252+
exploit = readInput(message, default="Y")
252253

253-
condition = not exploit or exploit[0] in ("y", "Y")
254-
else:
255-
condition = True
254+
condition = not exploit or exploit[0] in ("y", "Y")
255+
else:
256+
condition = True
257+
258+
if condition:
259+
checkForParenthesis()
260+
action()
261+
262+
except exceptionsTuple, e:
263+
e = str(e)
256264

257-
if condition:
258-
checkForParenthesis()
259-
action()
265+
if conf.multipleTargets:
266+
e += ", skipping to next url"
267+
logger.error(e)
268+
else:
269+
logger.error(e)
270+
return
260271

261272
if conf.loggedToOut:
262273
logger.info("Fetched data logged to text files under '%s'" % conf.outputPath)

lib/request/connect.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ def getPage(**kwargs):
178178

179179
except urllib2.HTTPError, e:
180180
if e.code == 401:
181-
exceptionMsg = "not authorized, try to provide right HTTP "
182-
exceptionMsg += "authentication type and valid credentials"
183-
raise sqlmapConnectionException, exceptionMsg
181+
errMsg = "not authorized, try to provide right HTTP "
182+
errMsg += "authentication type and valid credentials"
183+
raise sqlmapConnectionException, errMsg
184184
elif e.code == 404 and raise404:
185-
exceptionMsg = "page not found"
186-
raise sqlmapConnectionException, exceptionMsg
185+
errMsg = "page not found"
186+
raise sqlmapConnectionException, errMsg
187187
else:
188188
page = e.read()
189189
code = e.code
@@ -210,12 +210,6 @@ def getPage(**kwargs):
210210
if "BadStatusLine" not in tbMsg:
211211
warnMsg += " or proxy"
212212

213-
if conf.multipleTargets:
214-
warnMsg += ", skipping to next url"
215-
logger.warn(warnMsg)
216-
217-
return None, None
218-
219213
if silent:
220214
return None, None
221215
elif conf.retriesCount < conf.retries:

sqlmap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ def main():
7272

7373
try:
7474
init(cmdLineOptions)
75-
76-
if conf.start:
77-
start()
75+
start()
7876

7977
except exceptionsTuple, e:
78+
e = str(e)
8079
logger.error(e)
8180

8281
except KeyboardInterrupt:
@@ -90,6 +89,7 @@ def main():
9089
logger.error(errMsg)
9190

9291
except:
92+
print
9393
errMsg = unhandledException()
9494
logger.error(errMsg)
9595
traceback.print_exc()

0 commit comments

Comments
 (0)