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

Skip to content

Commit 1d0b43b

Browse files
committed
implemented mechanism for merging cookies by request
1 parent f114703 commit 1d0b43b

5 files changed

Lines changed: 42 additions & 54 deletions

File tree

lib/controller/controller.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from lib.core.exception import sqlmapValueException
5050
from lib.core.exception import sqlmapUserQuitException
5151
from lib.core.session import setInjection
52+
from lib.core.settings import DEFAULT_COOKIE_DELIMITER
5253
from lib.core.settings import DEFAULT_GET_POST_DELIMITER
5354
from lib.core.settings import EMPTY_FORM_FIELDS_REGEX
5455
from lib.core.settings import IGNORE_PARAMETERS
@@ -241,7 +242,6 @@ def start():
241242

242243
hostCount = 0
243244
cookieStr = ""
244-
setCookieAsInjectable = True
245245

246246
for targetUrl, targetMethod, targetData, targetCookie in kb.targetUrls:
247247
try:
@@ -345,31 +345,6 @@ def start():
345345
if conf.nullConnection:
346346
checkNullConnection()
347347

348-
if not conf.dropSetCookie and conf.cj:
349-
cookieStr = ";".join("%s=%s" % (getUnicode(cookie.name), getUnicode(cookie.value)) for _, cookie in enumerate(conf.cj))
350-
351-
if cookieStr:
352-
if PLACE.COOKIE in conf.parameters:
353-
message = "you provided an HTTP Cookie header value. "
354-
message += "The target url provided its own Cookie within "
355-
message += "the HTTP Set-Cookie header. Do you want to "
356-
message += "continue using the HTTP Cookie values that "
357-
message += "you provided? [Y/n] "
358-
test = readInput(message, default="Y")
359-
360-
if not test or test[0] in ("y", "Y"):
361-
setCookieAsInjectable = False
362-
363-
if setCookieAsInjectable:
364-
conf.httpHeaders.append((HTTPHEADER.COOKIE, cookieStr))
365-
conf.parameters[PLACE.COOKIE] = cookieStr
366-
__paramDict = paramToDict(PLACE.COOKIE, cookieStr)
367-
368-
if __paramDict:
369-
conf.paramDict[PLACE.COOKIE] = __paramDict
370-
# TODO: consider the following line in __setRequestParams()
371-
# __testableParameters = True
372-
373348
if (len(kb.injections) == 0 or (len(kb.injections) == 1 and kb.injections[0].place is None)) \
374349
and (kb.injection.place is None or kb.injection.parameter is None):
375350

lib/core/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class HTTPHEADER:
109109
CONTENT_RANGE = "Content-Range"
110110
CONTENT_TYPE = "Content-Type"
111111
COOKIE = "Cookie"
112+
SET_COOKIE = "Set-Cookie"
112113
HOST = "Host"
113114
PRAGMA = "Pragma"
114115
PROXY_AUTHORIZATION = "Proxy-Authorization"

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,7 @@ def __setKnowledgeBaseAttributes(flushAll=True):
14421442
kb.locks[_] = threading.Lock()
14431443

14441444
kb.matchRatio = None
1445+
kb.mergeCookies = None
14451446
kb.multiThreadMode = False
14461447
kb.nullConnection = None
14471448
kb.pageTemplate = None

lib/request/basic.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,65 @@
2121
from lib.core.common import getUnicode
2222
from lib.core.common import isWindowsDriveLetterPath
2323
from lib.core.common import posixToNtSlashes
24+
from lib.core.common import readInput
2425
from lib.core.common import sanitizeAsciiString
2526
from lib.core.common import singleTimeLogMessage
2627
from lib.core.data import conf
2728
from lib.core.data import kb
2829
from lib.core.data import logger
2930
from lib.core.enums import HTTPHEADER
31+
from lib.core.enums import PLACE
3032
from lib.core.exception import sqlmapDataException
33+
from lib.core.settings import DEFAULT_COOKIE_DELIMITER
3134
from lib.core.settings import ML
3235
from lib.core.settings import META_CHARSET_REGEX
3336
from lib.core.settings import PARSE_HEADERS_LIMIT
3437
from lib.core.settings import UNICODE_ENCODING
3538
from lib.parse.headers import headersParser
3639
from lib.parse.html import htmlParser
3740

38-
def forgeHeaders(cookie, ua, referer):
41+
def forgeHeaders(items=None):
3942
"""
4043
Prepare HTTP Cookie, HTTP User-Agent and HTTP Referer headers to use when performing
4144
the HTTP requests
4245
"""
4346

44-
headers = {}
47+
headers = dict(conf.httpHeaders)
48+
headers.update(items or {})
4549

46-
for header, value in conf.httpHeaders:
47-
if cookie and header == HTTPHEADER.COOKIE:
48-
headers[header] = cookie
49-
elif ua and header == HTTPHEADER.USER_AGENT:
50-
headers[header] = ua
51-
elif referer and header == HTTPHEADER.REFERER:
52-
headers[header] = referer
53-
else:
54-
headers[header] = value
50+
for _ in headers.keys():
51+
if headers[_] is None:
52+
del headers[_]
53+
54+
if conf.cj:
55+
if HTTPHEADER.COOKIE in headers:
56+
for cookie in conf.cj:
57+
if ("%s=" % cookie.name) in headers[HTTPHEADER.COOKIE]:
58+
if kb.mergeCookies is None:
59+
message = "you provided a HTTP %s header value. " % HTTPHEADER.COOKIE
60+
message += "The target url provided it's own cookies within "
61+
message += "the HTTP %s header which intersect with yours. " % HTTPHEADER.SET_COOKIE
62+
message += "Do you want to merge them in futher requests? [Y/n] "
63+
test = readInput(message, default="Y")
64+
kb.mergeCookies = not test or test[0] in ("y", "Y")
65+
66+
if kb.mergeCookies:
67+
_ = lambda x: re.sub("%s=[^%s]+" % (cookie.name, DEFAULT_COOKIE_DELIMITER), "%s=%s" % (cookie.name, cookie.value), x, re.I)
68+
headers[HTTPHEADER.COOKIE] = _(headers[HTTPHEADER.COOKIE])
69+
70+
if PLACE.COOKIE in conf.parameters:
71+
conf.parameters[PLACE.COOKIE] = _(conf.parameters[PLACE.COOKIE])
72+
conf.httpHeaders = [(item[0], item[1] if item[0] != HTTPHEADER.COOKIE else _(item[1])) for item in conf.httpHeaders]
73+
74+
elif not kb.testMode:
75+
headers[HTTPHEADER.COOKIE] += "%s %s=%s" % (DEFAULT_COOKIE_DELIMITER, cookie.name, cookie.value)
76+
77+
if kb.testMode:
78+
conf.cj.clear()
5579

5680
if kb.redirectSetCookie and not conf.dropSetCookie:
5781
if HTTPHEADER.COOKIE in headers:
58-
headers[HTTPHEADER.COOKIE] = "%s; %s" % (headers[HTTPHEADER.COOKIE], kb.redirectSetCookie)
82+
headers[HTTPHEADER.COOKIE] += "%s %s" % (DEFAULT_COOKIE_DELIMITER, kb.redirectSetCookie)
5983
else:
6084
headers[HTTPHEADER.COOKIE] = kb.redirectSetCookie
6185

lib/request/connect.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def getPage(**kwargs):
240240
requestMsg += " %s" % httplib.HTTPConnection._http_vsn_str
241241

242242
# Prepare HTTP headers
243-
headers = forgeHeaders(cookie, ua, referer)
243+
headers = forgeHeaders({ HTTPHEADER.COOKIE: cookie, HTTPHEADER.USER_AGENT: ua, HTTPHEADER.REFERER: referer })
244244

245245
if conf.realTest:
246246
headers[HTTPHEADER.REFERER] = "%s://%s" % (conf.scheme, conf.hostname)
@@ -271,23 +271,13 @@ def getPage(**kwargs):
271271
else:
272272
req = urllib2.Request(url, post, headers)
273273

274-
if not conf.dropSetCookie and conf.cj:
275-
for _, cookie in enumerate(conf.cj):
276-
if not cookieStr:
277-
cookieStr = "Cookie: "
278-
279-
cookie = getUnicode(cookie)
280-
index = cookie.index(" for ")
281-
282-
cookieStr += "%s; " % cookie[8:index]
283-
284274
if not req.has_header(HTTPHEADER.ACCEPT_ENCODING):
285275
requestHeaders += "%s: identity\n" % HTTPHEADER.ACCEPT_ENCODING
286276

287277
requestHeaders += "\n".join("%s: %s" % (key.capitalize() if isinstance(key, basestring) else key, getUnicode(value)) for (key, value) in req.header_items())
288278

289-
if not req.has_header(HTTPHEADER.COOKIE) and cookieStr:
290-
requestHeaders += "\n%s" % cookieStr[:-2]
279+
if not req.has_header(HTTPHEADER.COOKIE) and conf.cj:
280+
requestHeaders += "\n%s" % ("Cookie: %s" % ";".join("%s=%s" % (getUnicode(cookie.name), getUnicode(cookie.value)) for cookie in conf.cj))
291281

292282
if not req.has_header(HTTPHEADER.CONNECTION):
293283
requestHeaders += "\n%s: close" % HTTPHEADER.CONNECTION
@@ -728,9 +718,6 @@ def _randomizeParameter(paramString, randomParameter):
728718
if kb.testMode:
729719
kb.testQueryCount += 1
730720

731-
if conf.cj:
732-
conf.cj.clear()
733-
734721
if timeBasedCompare:
735722
return wasLastRequestDelayed()
736723
elif noteResponseTime:

0 commit comments

Comments
 (0)