2011-06-20 15:32:30 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2012-07-12 21:38:03 +04:00
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
|
2011-06-20 15:32:30 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2011-07-11 12:54:39 +04:00
|
|
|
import httplib
|
2011-06-20 15:32:30 +04:00
|
|
|
import re
|
|
|
|
import urlparse
|
2011-06-20 18:27:24 +04:00
|
|
|
import time
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2011-06-20 18:27:24 +04:00
|
|
|
from lib.core.common import clearConsoleLine
|
2011-06-20 15:32:30 +04:00
|
|
|
from lib.core.common import dataToStdout
|
2011-10-29 13:32:20 +04:00
|
|
|
from lib.core.common import findPageForms
|
2011-06-24 23:50:13 +04:00
|
|
|
from lib.core.common import singleTimeWarnMessage
|
2011-06-20 15:32:30 +04:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapConnectionException
|
2011-06-21 02:41:38 +04:00
|
|
|
from lib.core.settings import CRAWL_EXCLUDE_EXTENSIONS
|
2011-06-20 15:32:30 +04:00
|
|
|
from lib.core.threads import getCurrentThreadData
|
|
|
|
from lib.core.threads import runThreads
|
|
|
|
from lib.request.connect import Connect as Request
|
2012-07-14 19:01:04 +04:00
|
|
|
from thirdparty.beautifulsoup.beautifulsoup import BeautifulSoup
|
|
|
|
from thirdparty.oset.pyoset import oset
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
def crawl(target):
|
|
|
|
try:
|
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
threadData.shared.value = oset()
|
|
|
|
|
|
|
|
def crawlThread():
|
2011-06-20 15:32:30 +04:00
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
while kb.threadContinue:
|
|
|
|
with kb.locks.limit:
|
|
|
|
if threadData.shared.unprocessed:
|
|
|
|
current = threadData.shared.unprocessed.pop()
|
|
|
|
else:
|
|
|
|
break
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
content = None
|
|
|
|
try:
|
|
|
|
if current:
|
|
|
|
content = Request.getPage(url=current, crawling=True, raise404=False)[0]
|
|
|
|
except SqlmapConnectionException, e:
|
|
|
|
errMsg = "connection exception detected (%s). skipping " % e
|
|
|
|
errMsg += "url '%s'" % current
|
|
|
|
logger.critical(errMsg)
|
|
|
|
except httplib.InvalidURL, e:
|
|
|
|
errMsg = "invalid url detected (%s). skipping " % e
|
|
|
|
errMsg += "url '%s'" % current
|
|
|
|
logger.critical(errMsg)
|
|
|
|
|
|
|
|
if not kb.threadContinue:
|
|
|
|
break
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
if isinstance(content, unicode):
|
2011-06-21 01:47:03 +04:00
|
|
|
try:
|
2013-01-09 18:22:21 +04:00
|
|
|
match = re.search(r"(?si)<html[^>]*>(.+)</html>", content)
|
|
|
|
if match:
|
|
|
|
content = "<html>%s</html>" % match.group(1)
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
soup = BeautifulSoup(content)
|
|
|
|
tags = soup('a')
|
2012-12-27 23:55:37 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
if not tags:
|
|
|
|
tags = re.finditer(r'(?si)<a[^>]+href="(?P<href>[^>"]+)"', content)
|
2012-12-27 23:55:37 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
for tag in tags:
|
|
|
|
href = tag.get("href") if hasattr(tag, "get") else tag.group("href")
|
2012-12-27 23:55:37 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
if href:
|
|
|
|
url = urlparse.urljoin(target, href)
|
2012-12-27 23:55:37 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
# flag to know if we are dealing with the same target host
|
|
|
|
_ = reduce(lambda x, y: x == y, map(lambda x: urlparse.urlparse(x).netloc.split(':')[0], (url, target)))
|
2012-02-22 14:40:11 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
if conf.scope:
|
|
|
|
if not re.search(conf.scope, url, re.I):
|
2011-06-21 01:18:12 +04:00
|
|
|
continue
|
2013-01-09 18:22:21 +04:00
|
|
|
elif not _:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if url.split('.')[-1].lower() not in CRAWL_EXCLUDE_EXTENSIONS:
|
|
|
|
with kb.locks.value:
|
|
|
|
threadData.shared.deeper.add(url)
|
|
|
|
if re.search(r"(.*?)\?(.+)", url):
|
|
|
|
threadData.shared.value.add(url)
|
|
|
|
except UnicodeEncodeError: # for non-HTML files
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
if conf.forms:
|
|
|
|
findPageForms(content, current, False, True)
|
|
|
|
|
|
|
|
if conf.verbose in (1, 2):
|
|
|
|
threadData.shared.count += 1
|
2013-01-09 19:10:26 +04:00
|
|
|
status = '%d/%d links visited (%d%%)' % (threadData.shared.count, threadData.shared.length, round(100.0 * threadData.shared.count / threadData.shared.length))
|
2013-01-09 18:22:21 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] %s" % (time.strftime("%X"), status), True)
|
|
|
|
|
|
|
|
threadData.shared.deeper = set()
|
|
|
|
threadData.shared.unprocessed = set([target])
|
|
|
|
|
|
|
|
logger.info("starting crawler")
|
|
|
|
|
|
|
|
for i in xrange(conf.crawlDepth):
|
|
|
|
if i > 0 and conf.threads == 1:
|
|
|
|
singleTimeWarnMessage("running in a single-thread mode. This could take a while.")
|
|
|
|
threadData.shared.count = 0
|
|
|
|
threadData.shared.length = len(threadData.shared.unprocessed)
|
|
|
|
numThreads = min(conf.threads, len(threadData.shared.unprocessed))
|
|
|
|
logger.info("searching for links with depth %d" % (i + 1))
|
|
|
|
runThreads(numThreads, crawlThread)
|
|
|
|
clearConsoleLine(True)
|
|
|
|
if threadData.shared.deeper:
|
|
|
|
threadData.shared.unprocessed = set(threadData.shared.deeper)
|
|
|
|
else:
|
|
|
|
break
|
2012-02-22 14:40:11 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
warnMsg = "user aborted during crawling. sqlmap "
|
|
|
|
warnMsg += "will use partial list"
|
|
|
|
logger.warn(warnMsg)
|
2011-06-20 15:32:30 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
finally:
|
|
|
|
clearConsoleLine(True)
|
2011-06-20 18:27:24 +04:00
|
|
|
|
2013-01-09 18:22:21 +04:00
|
|
|
if not threadData.shared.value:
|
|
|
|
warnMsg = "no usable links found (with GET parameters)"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
else:
|
|
|
|
for url in threadData.shared.value:
|
|
|
|
kb.targets.add((url, None, None, None))
|