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

Skip to content

Commit 2de5292

Browse files
committed
Code refactoring (epecially Google search code)
1 parent 76b793b commit 2de5292

5 files changed

Lines changed: 60 additions & 86 deletions

File tree

lib/controller/controller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,22 @@ def start():
242242
return True
243243

244244
if conf.url and not any((conf.forms, conf.crawlDepth)):
245-
kb.targetUrls.add((conf.url, conf.method, conf.data, conf.cookie))
245+
kb.targets.add((conf.url, conf.method, conf.data, conf.cookie))
246246

247-
if conf.configFile and not kb.targetUrls:
247+
if conf.configFile and not kb.targets:
248248
errMsg = "you did not edit the configuration file properly, set "
249249
errMsg += "the target url, list of targets or google dork"
250250
logger.error(errMsg)
251251
return False
252252

253-
if kb.targetUrls and len(kb.targetUrls) > 1:
254-
infoMsg = "sqlmap got a total of %d targets" % len(kb.targetUrls)
253+
if kb.targets and len(kb.targets) > 1:
254+
infoMsg = "sqlmap got a total of %d targets" % len(kb.targets)
255255
logger.info(infoMsg)
256256

257257
hostCount = 0
258258
cookieStr = ""
259259

260-
for targetUrl, targetMethod, targetData, targetCookie in kb.targetUrls:
260+
for targetUrl, targetMethod, targetData, targetCookie in kb.targets:
261261
try:
262262
conf.url = targetUrl
263263
conf.method = targetMethod

lib/core/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3093,7 +3093,7 @@ def geturl(self):
30933093

30943094
if addToTargets and retVal:
30953095
for target in retVal:
3096-
kb.targetUrls.add(target)
3096+
kb.targets.add(target)
30973097

30983098
return retVal
30993099

lib/core/option.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,37 +85,38 @@
8585
from lib.core.log import LOGGER_HANDLER
8686
from lib.core.optiondict import optDict
8787
from lib.core.purge import purge
88+
from lib.core.settings import ACCESS_ALIASES
89+
from lib.core.settings import BURP_REQUEST_REGEX
8890
from lib.core.settings import CODECS_LIST_PAGE
8991
from lib.core.settings import CRAWL_EXCLUDE_EXTENSIONS
92+
from lib.core.settings import DB2_ALIASES
9093
from lib.core.settings import DEFAULT_GET_POST_DELIMITER
9194
from lib.core.settings import DEFAULT_PAGE_ENCODING
9295
from lib.core.settings import DEFAULT_TOR_HTTP_PORTS
9396
from lib.core.settings import DEFAULT_TOR_SOCKS_PORT
97+
from lib.core.settings import FIREBIRD_ALIASES
9498
from lib.core.settings import IS_WIN
99+
from lib.core.settings import LOCALHOST
100+
from lib.core.settings import MAXDB_ALIASES
101+
from lib.core.settings import MAX_NUMBER_OF_THREADS
102+
from lib.core.settings import MSSQL_ALIASES
103+
from lib.core.settings import MYSQL_ALIASES
95104
from lib.core.settings import NULL
105+
from lib.core.settings import ORACLE_ALIASES
106+
from lib.core.settings import PARAMETER_SPLITTING_REGEX
107+
from lib.core.settings import PGSQL_ALIASES
96108
from lib.core.settings import PYVERSION
97109
from lib.core.settings import SITE
110+
from lib.core.settings import SQLITE_ALIASES
98111
from lib.core.settings import SUPPORTED_DBMS
99112
from lib.core.settings import SUPPORTED_OS
100-
from lib.core.settings import VERSION_STRING
101-
from lib.core.settings import MSSQL_ALIASES
102-
from lib.core.settings import MYSQL_ALIASES
103-
from lib.core.settings import PGSQL_ALIASES
104-
from lib.core.settings import ORACLE_ALIASES
105-
from lib.core.settings import SQLITE_ALIASES
106-
from lib.core.settings import ACCESS_ALIASES
107-
from lib.core.settings import FIREBIRD_ALIASES
108-
from lib.core.settings import MAXDB_ALIASES
109113
from lib.core.settings import SYBASE_ALIASES
110-
from lib.core.settings import DB2_ALIASES
111-
from lib.core.settings import BURP_REQUEST_REGEX
112-
from lib.core.settings import LOCALHOST
113-
from lib.core.settings import MAX_NUMBER_OF_THREADS
114-
from lib.core.settings import PARAMETER_SPLITTING_REGEX
115114
from lib.core.settings import TIME_DELAY_CANDIDATES
116115
from lib.core.settings import UNENCODED_ORIGINAL_VALUE
117116
from lib.core.settings import UNION_CHAR_REGEX
118117
from lib.core.settings import UNKNOWN_DBMS_VERSION
118+
from lib.core.settings import URI_INJECTABLE_REGEX
119+
from lib.core.settings import VERSION_STRING
119120
from lib.core.settings import WEBSCARAB_SPLITTER
120121
from lib.core.threads import getCurrentThreadData
121122
from lib.core.update import update
@@ -212,8 +213,8 @@ def __parseWebScarabLog(content):
212213
continue
213214

214215
if not(conf.scope and not re.search(conf.scope, url, re.I)):
215-
if not kb.targetUrls or url not in addedTargetUrls:
216-
kb.targetUrls.add((url, method, None, cookie))
216+
if not kb.targets or url not in addedTargetUrls:
217+
kb.targets.add((url, method, None, cookie))
217218
addedTargetUrls.add(url)
218219

219220
def __parseBurpLog(content):
@@ -322,8 +323,8 @@ def __parseBurpLog(content):
322323
port = None
323324

324325
if not(conf.scope and not re.search(conf.scope, url, re.I)):
325-
if not kb.targetUrls or url not in addedTargetUrls:
326-
kb.targetUrls.add((url, method, urldecode(data) if data and urlencode(DEFAULT_GET_POST_DELIMITER, None) not in data else data, cookie))
326+
if not kb.targets or url not in addedTargetUrls:
327+
kb.targets.add((url, method, urldecode(data) if data and urlencode(DEFAULT_GET_POST_DELIMITER, None) not in data else data, cookie))
327328
addedTargetUrls.add(url)
328329

329330
fp = openFile(reqFile, "rb")
@@ -374,7 +375,7 @@ def __setMultipleTargets():
374375
mode.
375376
"""
376377

377-
initialTargetsCount = len(kb.targetUrls)
378+
initialTargetsCount = len(kb.targets)
378379
addedTargetUrls = set()
379380

380381
if not conf.logFile:
@@ -405,7 +406,7 @@ def __setMultipleTargets():
405406
errMsg += "nor a directory"
406407
raise sqlmapFilePathException, errMsg
407408

408-
updatedTargetsCount = len(kb.targetUrls)
409+
updatedTargetsCount = len(kb.targets)
409410

410411
if updatedTargetsCount > initialTargetsCount:
411412
infoMsg = "sqlmap parsed %d " % (updatedTargetsCount - initialTargetsCount)
@@ -493,37 +494,48 @@ def __setGoogleDorking():
493494
handlers.append(keepAliveHandler)
494495

495496
googleObj = Google(handlers)
496-
googleObj.getCookie()
497+
kb.data.onlyGETs = None
497498

498-
def search():
499-
matches = googleObj.search(conf.googleDork)
499+
def retrieve():
500+
links = googleObj.search(conf.googleDork)
500501

501-
if not matches:
502+
if not links:
502503
errMsg = "unable to find results for your "
503504
errMsg += "Google dork expression"
504505
raise sqlmapGenericException, errMsg
505506

506-
googleObj.getTargetUrls()
507-
return matches
507+
for link in links:
508+
link = urldecode(link)
509+
if re.search(r"(.*?)\?(.+)", link):
510+
kb.targets.add((link, conf.method, conf.data, conf.cookie))
511+
elif re.search(URI_INJECTABLE_REGEX, link, re.I):
512+
if kb.data.onlyGETs is None and conf.data is None:
513+
message = "do you want to scan only results containing GET parameters? [Y/n] "
514+
test = readInput(message, default="Y")
515+
kb.data.onlyGETs = test.lower() != 'n'
516+
if not kb.data.onlyGETs:
517+
kb.targets.add((link, conf.method, conf.data, conf.cookie))
518+
519+
return links
508520

509521
while True:
510-
matches = search()
522+
links = retrieve()
511523

512-
if kb.targetUrls:
513-
infoMsg = "sqlmap got %d results for your " % len(matches)
524+
if kb.targets:
525+
infoMsg = "sqlmap got %d results for your " % len(links)
514526
infoMsg += "Google dork expression, "
515527

516-
if len(matches) == len(kb.targetUrls):
528+
if len(links) == len(kb.targets):
517529
infoMsg += "all "
518530
else:
519-
infoMsg += "%d " % len(kb.targetUrls)
531+
infoMsg += "%d " % len(kb.targets)
520532

521533
infoMsg += "of them are testable targets"
522534
logger.info(infoMsg)
523535
break
524536

525537
else:
526-
message = "sqlmap got %d results " % len(matches)
538+
message = "sqlmap got %d results " % len(links)
527539
message += "for your Google dork expression, but none of them "
528540
message += "have GET parameters to test for SQL injection. "
529541
message += "Do you want to skip to the next result page? [Y/n]"
@@ -550,7 +562,7 @@ def __setBulkMultipleTargets():
550562

551563
for line in getFileItems(conf.bulkFile):
552564
if re.search(r"[^ ]+\?(.+)", line, re.I):
553-
kb.targetUrls.add((line.strip(), None, None, None))
565+
kb.targets.add((line.strip(), None, None, None))
554566

555567
def __findPageForms():
556568
if not conf.forms or conf.crawlDepth:
@@ -1571,9 +1583,8 @@ def __setKnowledgeBaseAttributes(flushAll=True):
15711583
kb.headerPaths = {}
15721584
kb.keywords = set(getFileItems(paths.SQL_KEYWORDS))
15731585
kb.passwordMgr = None
1574-
kb.scanOnlyGoogleGETs = None
15751586
kb.tamperFunctions = []
1576-
kb.targetUrls = oset()
1587+
kb.targets = oset()
15771588
kb.testedParams = set()
15781589
kb.userAgents = None
15791590
kb.vainRun = True

lib/utils/crawler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ def crawlThread():
126126
logger.warn(warnMsg)
127127
else:
128128
for url in threadData.shared.outputs:
129-
kb.targetUrls.add(( url, None, None, None ))
129+
kb.targets.add(( url, None, None, None ))

lib/utils/google.py

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,23 @@ class Google:
3333
"""
3434

3535
def __init__(self, handlers):
36-
self._matches = []
3736
self._cj = cookielib.CookieJar()
3837

3938
handlers.append(urllib2.HTTPCookieProcessor(self._cj))
4039

4140
self.opener = urllib2.build_opener(*handlers)
4241
self.opener.addheaders = conf.httpHeaders
4342

44-
def _parsePage(self, page):
45-
"""
46-
Parse Google dork search results page to get the list of
47-
HTTP addresses
48-
"""
49-
50-
retVal = [urllib.unquote(match.group(1)) for match in re.finditer(GOOGLE_REGEX, page, re.I | re.S)]
51-
52-
return retVal
53-
54-
def getTargetUrls(self):
55-
"""
56-
This method returns the list of hosts with parameters out of
57-
your Google dork search results
58-
"""
59-
60-
for _ in self._matches:
61-
_ = urldecode(_)
62-
if re.search(r"(.*?)\?(.+)", _):
63-
kb.targetUrls.add((_, conf.method, conf.data, conf.cookie))
64-
elif re.search(URI_INJECTABLE_REGEX, _, re.I):
65-
if kb.scanOnlyGoogleGETs is None:
66-
message = "do you want to scan only results containing GET parameters? [Y/n] "
67-
test = readInput(message, default="Y")
68-
kb.scanOnlyGoogleGETs = test.lower() != 'n'
69-
if not kb.scanOnlyGoogleGETs:
70-
kb.targetUrls.add((_, conf.method, conf.data, conf.cookie))
71-
72-
def getCookie(self):
73-
"""
74-
This method is the first to be called when initializing a
75-
Google dorking object through this library. It is used to
76-
retrieve the Google session cookie needed to perform the
77-
further search
78-
"""
79-
8043
try:
8144
conn = self.opener.open("http://www.google.com/ncr")
82-
_ = conn.info()
45+
_ = conn.info() # retrieve session cookie
8346
except urllib2.HTTPError, e:
8447
_ = e.info()
8548
except urllib2.URLError:
8649
errMsg = "unable to connect to Google"
8750
raise sqlmapConnectionException, errMsg
8851

89-
def search(self, googleDork):
52+
def search(self, dork):
9053
"""
9154
This method performs the effective search on Google providing
9255
the google dork and the Google session cookie
@@ -95,11 +58,11 @@ def search(self, googleDork):
9558
gpage = conf.googlePage if conf.googlePage > 1 else 1
9659
logger.info("using Google result page #%d" % gpage)
9760

98-
if not googleDork:
61+
if not dork:
9962
return None
10063

10164
url = "http://www.google.com/search?"
102-
url += "q=%s&" % urlencode(googleDork, convall=True)
65+
url += "q=%s&" % urlencode(dork, convall=True)
10366
url += "num=100&hl=en&complete=0&safe=off&filter=0&btnG=Search"
10467
url += "&start=%d" % ((gpage-1) * 100)
10568

@@ -136,11 +99,11 @@ def search(self, googleDork):
13699
errMsg = "unable to connect to Google"
137100
raise sqlmapConnectionException, errMsg
138101

139-
self._matches = self._parsePage(page)
102+
retVal = [urllib.unquote(match.group(1)) for match in re.finditer(GOOGLE_REGEX, page, re.I | re.S)]
140103

141-
if not self._matches and "detected unusual traffic" in page:
104+
if not retVal and "detected unusual traffic" in page:
142105
warnMsg = "Google has detected 'unusual' traffic from "
143106
warnMsg += "this computer disabling further searches"
144107
raise sqlmapGenericException, warnMsg
145108

146-
return self._matches
109+
return retVal

0 commit comments

Comments
 (0)