Snippets
Created by
grimhacker
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | '''
. .1111... | Title: raw2proxy.py
.10000000000011. .. | Author: Oliver Morton (Sec-1 Ltd)
.00 000... | Email: oliverm-tools@sec-1.com
1 01.. | Description:
.. | Parse files containing raw HTTP requests and
.. | make these requests via a proxy.
GrimHacker .. |
.. |
grimhacker.com .. |
@grimhacker .. |
----------------------------------------------------------------------------
Raw2Proxy - Send Raw HTTP Requests to a Proxy
Copyright (C) 2015 Oliver Morton (Sec-1 Ltd)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'''
__version__ = "$Revision: 1.0 $"
# $Source$
import os
import sys
import argparse
import logging
import urllib2
import ssl
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, raw_request):
self.rfile = StringIO(raw_request)
self.raw_requestline = self.rfile.readline()
self.error_code = None
self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
def make_request(raw_request, protocol="http", proxy={'http': '127.0.0.1:8080', 'https': '127.0.0.1:8080'}, verify=True):
try:
logging.debug("Parsing raw_request")
request = HTTPRequest(raw_request) #Parse the raw request to an object.
except Exception as e:
raise Exception("Failed to parse raw request. {0}".format(e))
try:
# Extract the information we need from the request object:
logging.debug("Extracting required information")
host = request.headers.get('host')
url = "{protocol}://{host}{path}".format(protocol=protocol, host=host, path=request.path)
body = request.rfile.read()
args = {'headers': request.headers}
if body:
args['data'] = body # Add body parameters if they exist.
except Exception as e:
raise Exception("Failed to extract fields from parsed request. {0}".format(e))
try:
# Create the Proxy Handler so we go through the proxy
logging.debug("Creating ProxyHandler")
proxy_handler = urllib2.ProxyHandler(proxy)
if not verify:
logging.warning("Not verifying TLS/SSL connection!")
logging.debug("Creating insecure TLS context")
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
https_handler = urllib2.HTTPSHandler(context=ctx)
logging.debug("Creating Opener")
opener = urllib2.build_opener(https_handler, proxy_handler)
else:
logging.debug("Creating Opener")
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
# Build the HTTP Request
logging.debug("Building HTTP request")
url_request = urllib2.Request(url, **args)
url_request.get_method = lambda: request.command # Handle methods (PUT, DELETE, GET, etc)
except Exception as e:
raise Exception("Failed to create opener. {0}".format(e))
try:
# Send the HTTP Request
logging.debug("Sending HTTP request")
response = opener.open(url_request)
logging.debug("Response = {0}".format(response.read())) # Print the response
except urllib2.HTTPError, error:
contents = error.read()
logging.debug("Error response = {0}".format(contents)) # Print the error response
except Exception as e:
raise Exception("Failed to send request. {0}".format(e))
def main(files, protocol, proxy, verify):
failed = []
try:
for file_ in files:
logging.info("Handling: {0}".format(file_))
try:
with open(file_, "r") as f:
raw_request = f.read()
try:
make_request(raw_request, protocol=protocol, proxy={'https': proxy, 'http': proxy}, verify=verify)
except Exception as e:
failed.append((file_, e))
logging.warning("Error handling: {0} - {1}".format(file_, e))
except Exception as e:
failed.append((file_, e))
logging.warning("Error reading: {0} - {1}".format(file_, e))
except Exception as e:
for file_ in files:
failed.append((file_, e))
logging.critical("Error: {0}".format(e))
if failed:
print
logging.warning("The following requests were not successfully handled and may not be in your proxy:\n{0}".format("\n".join(str(file_) for file_, reason in failed)))
logging.info("Done. Check your proxy.")
def print_version():
"""Print command line version banner."""
print """
. .1111... | Title: raw2proxy.py {0}
.10000000000011. .. | Author: Oliver Morton (Sec-1 Ltd)
.00 000... | Email: oliverm-tools@sec-1.com
1 01.. | Description:
.. | Parse files containing raw HTTP requests and
.. | make these requests via a proxy.
GrimHacker .. |
.. |
grimhacker.com .. |
@grimhacker .. |
----------------------------------------------------------------------------
""".format(__version__)
if __name__ == "__main__":
print """
Raw2Proxy - Send Raw HTTP Requests to a Proxy {0}
Copyright (C) 2015 Oliver Morton (Sec-1 Ltd)
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See GPLv2 License.
""".format(__version__)
parser = argparse.ArgumentParser(description="Send Raw HTTP Requests to a Proxy")
parser.add_argument("-V", "--version", help="Print version banner", action="store_true")
parser.add_argument("-v", "--verbose", help="Debug logging", action="store_true")
parser.add_argument("-d", "--directory", help="Directory containing raw HTTP requests in files")
parser.add_argument("-f", "--files", help="Files containing raw HTTP requests", nargs="+")
parser.add_argument("-p", "--proxy", help="HTTP Proxy to send requests via. DEFAULT=127.0.0.1:8080", default="127.0.0.1:8080")
parser.add_argument("--insecure", help="Do not verify SSL/TLS certificates", action="store_true")
parser.add_argument("--https", help="Use HTTPS", action="store_true")
args = parser.parse_args()
if args.version:
print_version()
exit()
if args.verbose:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(level=level,
format="%(levelname)s: %(message)s")
if not (args.files or args.directory):
logging.critical("Specify directory and/or files!")
parser.print_usage()
exit()
files = []
if args.files:
logging.debug("Getting file names from command line")
files += args.files
if args.directory:
logging.debug("Getting file names from directory '{0}'".format(args.directory))
try:
for (dirpath, dirnames, filenames) in os.walk(args.directory):
for filename in filenames:
files.append(os.path.join(dirpath, filename))
break
except Exception as e:
logging.critical("Failed to get file names from directory '{0}'".format(args.directory))
exit()
protocol = "http"
if args.https:
protocol = "https"
if args.insecure:
verify = False
else:
verify = True
main(files, protocol, args.proxy, verify)
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
This worked great for me, but I needed it to do HTTPS and handle invalid certificates. That turned out to be fairly straight forward. Here's a
diff -u
of the change: