|
21 | 21 | from lib.core.common import getUnicode |
22 | 22 | from lib.core.common import isWindowsDriveLetterPath |
23 | 23 | from lib.core.common import posixToNtSlashes |
| 24 | +from lib.core.common import readInput |
24 | 25 | from lib.core.common import sanitizeAsciiString |
25 | 26 | from lib.core.common import singleTimeLogMessage |
26 | 27 | from lib.core.data import conf |
27 | 28 | from lib.core.data import kb |
28 | 29 | from lib.core.data import logger |
29 | 30 | from lib.core.enums import HTTPHEADER |
| 31 | +from lib.core.enums import PLACE |
30 | 32 | from lib.core.exception import sqlmapDataException |
| 33 | +from lib.core.settings import DEFAULT_COOKIE_DELIMITER |
31 | 34 | from lib.core.settings import ML |
32 | 35 | from lib.core.settings import META_CHARSET_REGEX |
33 | 36 | from lib.core.settings import PARSE_HEADERS_LIMIT |
34 | 37 | from lib.core.settings import UNICODE_ENCODING |
35 | 38 | from lib.parse.headers import headersParser |
36 | 39 | from lib.parse.html import htmlParser |
37 | 40 |
|
38 | | -def forgeHeaders(cookie, ua, referer): |
| 41 | +def forgeHeaders(items=None): |
39 | 42 | """ |
40 | 43 | Prepare HTTP Cookie, HTTP User-Agent and HTTP Referer headers to use when performing |
41 | 44 | the HTTP requests |
42 | 45 | """ |
43 | 46 |
|
44 | | - headers = {} |
| 47 | + headers = dict(conf.httpHeaders) |
| 48 | + headers.update(items or {}) |
45 | 49 |
|
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() |
55 | 79 |
|
56 | 80 | if kb.redirectSetCookie and not conf.dropSetCookie: |
57 | 81 | 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) |
59 | 83 | else: |
60 | 84 | headers[HTTPHEADER.COOKIE] = kb.redirectSetCookie |
61 | 85 |
|
|
0 commit comments