Support for chunked requests (#3536)

*  Add the `--chunk` option to send requests in chunks

* solve the httplib&urllib2 content-legnth

* remove info

* Solve the error caused by the mix of get mode and chunk

* add CHUNKED_KEYWORDS `union`
This commit is contained in:
boyhack
2019-03-19 20:26:29 +08:00
committed by Miroslav Stampar
parent 3b3774abaa
commit 340e250fb1
6 changed files with 124 additions and 4 deletions

View File

@@ -61,6 +61,7 @@ from lib.core.common import unicodeencode
from lib.core.common import unsafeVariableNaming
from lib.core.common import urldecode
from lib.core.common import urlencode
from lib.core.common import generateChunkDdata
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
@@ -271,9 +272,13 @@ class Connect(object):
checking = kwargs.get("checking", False)
skipRead = kwargs.get("skipRead", False)
finalCode = kwargs.get("finalCode", False)
chunked = conf.chunk
if multipart:
post = multipart
if chunked:
post = urllib.unquote(post)
post = generateChunkDdata(post)
websocket_ = url.lower().startswith("ws")
@@ -396,6 +401,9 @@ class Connect(object):
if conf.keepAlive:
headers[HTTP_HEADER.CONNECTION] = "keep-alive"
if chunked:
headers[HTTP_HEADER.TRANSFER_ENCODING] = "Chunked"
if auxHeaders:
headers = forgeHeaders(auxHeaders, headers)
@@ -455,7 +463,7 @@ class Connect(object):
requestHeaders += "\r\n%s" % ("Cookie: %s" % ";".join("%s=%s" % (getUnicode(cookie.name), getUnicode(cookie.value)) for cookie in cookies))
if post is not None:
if not getRequestHeader(req, HTTP_HEADER.CONTENT_LENGTH):
if not getRequestHeader(req, HTTP_HEADER.CONTENT_LENGTH) and not chunked:
requestHeaders += "\r\n%s: %d" % (string.capwords(HTTP_HEADER.CONTENT_LENGTH), len(post))
if not getRequestHeader(req, HTTP_HEADER.CONNECTION):
@@ -466,7 +474,8 @@ class Connect(object):
if post is not None:
requestMsg += "\r\n\r\n%s" % getUnicode(post)
requestMsg += "\r\n"
if not chunked:
requestMsg += "\r\n"
if not multipart:
threadData.lastRequestMsg = requestMsg

View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""
import urllib2
import httplib
from lib.core.data import conf
class HTTPHandler(urllib2.HTTPHandler):
"""
The hook http_requests function ensures that the chunk function is working properly.
"""
def _hook(self, request):
host = request.get_host()
if not host:
raise urllib2.URLError('no host given')
if request.has_data(): # POST
data = request.get_data()
if not request.has_header('Content-type'):
request.add_unredirected_header(
'Content-type',
'application/x-www-form-urlencoded')
if not request.has_header('Content-length') and not conf.chunk:
request.add_unredirected_header(
'Content-length', '%d' % len(data))
sel_host = host
if request.has_proxy():
scheme, sel = urllib2.splittype(request.get_selector())
sel_host, sel_path = urllib2.splithost(sel)
if not request.has_header('Host'):
request.add_unredirected_header('Host', sel_host)
for name, value in self.parent.addheaders:
name = name.capitalize()
if not request.has_header(name):
request.add_unredirected_header(name, value)
return request
http_request = _hook