source: trunk/bin/bletchley-http2py @ 126

Last change on this file since 126 was 118, checked in by tim, 8 years ago

.

  • Property svn:executable set to *
File size: 10.2 KB
Line 
1#!/usr/bin/env python3
2#-*- mode: Python;-*-
3#
4# Requires Python 3+
5
6'''
7This script reads a raw HTTP request and writes to stdout a Python
8script.  The generated script sends the same (or a very similar)
9request using the Requests library, or optionally, the built-in
10http.client library.
11
12Certainly if you have a raw request, you could simply send it via TCP
13sockets, but if for some reason the server behaves oddly with flow control,
14insists on using gzip/deflate encoding, insists on using chunked encoding,
15or any number of other annoying things, then using an HTTP library is a
16lot more convenient.  This script attempts to make the conversion from a
17raw HTTP request to HTTP library calls easy for pentesting automation.
18
19
20Copyright (C) 2011-2013 Virtual Security Research, LLC
21Copyright (C) 2014-2016 Blindspot Security LLC
22Author: Timothy D. Morgan
23
24 This program is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Lesser General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program.  If not, see <http://www.gnu.org/licenses/>.
35'''
36
37import sys
38import argparse
39import pprint
40import urllib.parse
41
42bopen = lambda f: open(f, 'rb')
43
44parser = argparse.ArgumentParser(
45    description='A script which accepts an HTTP request and prints out a'
46    ' generated Python script which sends a similar request.  This is useful'
47    ' when one wants to automate sending a large number of requests to a'
48    ' particular page or application.'
49    ' For more information, see: http://code.google.com/p/bletchley/wiki/Overview')
50parser.add_argument(
51    'requestfile', type=bopen, nargs='?', default=sys.stdin.buffer, 
52    help='A file containing an HTTP request.  Defaults to stdin if omitted.')
53group = parser.add_mutually_exclusive_group()
54group.add_argument('--requests', action='store_true',
55                   help='Generate a script that uses the Requests module'
56                        ' rather than http.client (default).')
57group.add_argument('--native', action='store_false', dest='requests',
58                   help='Generate a script that uses Pythons built-in http.client'
59                        ' rather than the Requests module.')
60parser.set_defaults(requests=True)
61
62args = parser.parse_args()
63input_req = args.requestfile.read()
64
65
66if b'\r\n\r\n' in input_req:
67    raw_headers,body = input_req.split(b'\r\n\r\n', 1)
68elif b'\n\n' in input_req:
69    raw_headers,body = input_req.split(b'\n\n', 1)
70else:
71    raw_headers = input_req
72    body = b''
73
74raw_headers = raw_headers.decode('utf-8')
75
76header_lines = raw_headers.split('\n')
77method,path,version = header_lines[0].split(' ', 2)
78
79host = 'TODO'
80port = None
81protocol = None
82
83headers = []
84for l in header_lines[1:]:
85    if len(l) < 1: 
86        break
87    # Handle header line continuations
88    if l[0] in ' \t':
89        if len(headers) == 0:
90            continue
91        name,values = headers[-1]
92        values.append(l.lstrip('\t'))
93        headers[-1] = (name,values)
94        continue
95
96    name,value = l.split(':',1)
97    value = value.lstrip(' ').rstrip('\r')
98
99    # Skip headers that have to do with transfer encodings, connection longevity, and caching
100    # XXX: maybe add these back as commented-out headers to the output?
101    if name.lower() not in ['accept','accept-language',
102                            'accept-encoding','accept-charset',
103                            'connection', 'keep-alive', 'host', 
104                            'content-length', 'proxy-connection',
105                            'if-none-match']:
106        headers.append((name,[value]))
107
108    if name.lower() == 'host':
109        if ':' in value:
110            host,port = value.split(':',1)
111            port = int(port, 10)
112            if port == 443:
113                protocol = 'https'
114        else:
115            host = value
116
117    # Attempt to guess the port and protocol from the referer header, since
118    # often it is the same site.  Defer to the host header though, if the
119    # info is there.
120    elif name.lower() == 'referer':
121        rurl = urllib.parse.urlparse(value)
122        if rurl.netloc == host:
123            if rurl.scheme == 'https' and protocol == None:
124                protocol = 'https'
125            if rurl.port != None and port == None:
126                port = rurl.port
127
128if protocol == None:
129    protocol = 'http'
130if port == None:
131    if protocol == 'https':
132        port = 443
133    else:
134        port = 80
135
136
137# XXX: use pprint
138formatted_body = '\n            '.join([repr(body[i:i+40]) for i in range(0,len(body),40)])
139if formatted_body == '':
140    formatted_body = "b''"
141
142
143print('''#!/usr/bin/env python3
144# This script was generated by bletchley-http2py
145# See the "TODO" comments below for places to edit your request as needed for your situation.
146
147import sys
148from bletchley import blobtools,buffertools
149from bletchley import chosenct
150from bletchley.CBC import *
151
152# TODO: ensure the host, port, and protocol settings are correct.
153host = %s
154port = %s
155protocol = %s
156
157def decode(token):
158    # TODO: Perhaps you needs something like this?
159    #       (See 'bletchley-decode -e ?' for a list of encodings)
160    # return blobtools.decodeChain(['percent/mixed','base64/rfc3548'], token)
161    return token
162
163
164def encode(binary):
165    # TODO: Perhaps you needs something like this?
166    # return blobtools.encodeChain(['base64/rfc3548', 'percent/mixed'], binary)
167    return binary
168''' % (repr(host),repr(port),repr(protocol)))
169
170if args.requests:
171    print('''
172try:
173    import requests
174except:
175    sys.stderr.write('ERROR: Could not import requests module.  Ensure it is installed.\\n')
176    sys.stderr.write('       Under Debian, the package name is "python3-requests"\\n.')
177    sys.stderr.write('       Alternatively, re-generate this script using the --native option.\\n.')
178    sys.exit(1)
179''')
180
181    headers = dict(headers)
182    # XXX: We don't currently support exactly formatted header
183    #      continuations with python requests, but this should be
184    #      semantically equivalent.
185    for h in headers.keys():
186        headers[h] = ' '.join(headers[h])
187
188    print('''
189session = requests.Session()
190def sendRequest(session, data=None):
191    data = data.decode('utf-8')
192    # TODO: Replace the token you wish to target in this request with the "data" variable.
193    #       Then specify the starting value for that token at the end of this script.
194    method = %s
195    path = %s
196    headers = %s
197    url = "%%s://%%s:%%d%%s" %% (protocol,host,port,path)
198    body = (%s)
199
200    # Set verify=True if you want to validate the server cert
201    return session.request(method, url, headers=headers, data=body, allow_redirects=False, verify=False)
202    ''' % (repr(method), repr(path),
203           pprint.pformat(headers, width=80-14).replace('\n','\n'+' '*14),
204           formatted_body))
205
206    print('''   
207
208def processResponse(data, other=None):
209    global session
210    ret_val = None
211    response = sendRequest(session, encode(data))
212
213    # TODO: customize code here to retrieve what you need from the response(s)
214    # For information on the response object's interface, see:
215    #   http://docs.python-requests.org/en/latest/api/#requests.Response
216
217    # These are useful for debugging, but once your response processing is working,
218    # remove them so it isn't so verbose.
219    print(response.status_code)
220    print(response.headers)
221    print(repr(response.content))
222
223    return ret_val
224''')
225
226
227else:
228    print('''
229import http.client
230
231def sendRequest(connection, data=None):
232    data = data.decode('utf-8')
233    # TODO: use "data" below, wherever your token normally appears
234    method = %s
235    path = %s
236    body = (%s)
237   
238    connection.putrequest(method, path)
239    ''' % (repr(method), repr(path), formatted_body))
240
241    for name,values in headers:
242        if len(values) > 1:
243            continuations = ','.join([repr(v) for v in values[1:]])
244            print('''    connection.putheader(%s, %s, %s)''' % (repr(name),repr(values[0]),continuations))
245        else:
246            print('''    connection.putheader(%s, %s)''' % (repr(name),repr(values[0])))
247
248    print('''   
249    if len(body) > 0:
250        connection.putheader('Content-Length', len(body))
251    connection.endheaders()
252    connection.send(body)
253   
254    return connection.getresponse()
255
256
257def newConnection():
258    global protocol
259    if protocol == 'https':
260        return http.client.HTTPSConnection(host, port)
261    else:
262        return http.client.HTTPConnection(host, port)
263
264
265def processResponse(data, other=None):
266    ret_val = False
267    connection = newConnection()
268    response = sendRequest(connection, encode(data))
269
270    # TODO: customize code here to retrieve what you need from the response(s)
271    # For information on the response object's interface, see:
272    #   http://docs.python.org/library/httplib.html#httpresponse-objects
273
274    # These are useful for debugging, but once your response processing is working,
275    # remove them so it isn't so verbose.
276    print(response.status)
277    print(response.getheaders())
278    print(repr(response.read()))
279
280    connection.close()
281    return ret_val
282''')
283
284
285print('''
286token = b'TODO: paste your encoded ciphertext here (typically moved from the sendRequest function)'
287ciphertext = decode(token)
288
289# TODO: Use this to verify you get the response you expect. 
290#       Once everything is working, use the commented code below to conduct specific attacks.
291processResponse(ciphertext)
292
293
294# Padding Oracle Attacks
295# poa = POA(processResponse, {block size}, ciphertext, iv=None, threads=1, log_file=sys.stderr)
296# print(poa.probe_padding()) # sanity check
297# print(poa.decrypt())
298
299
300# Byte-by-byte probing of ciphertext
301#   Maybe start with this as a fast but gentle probe:
302# result = chosenct.probe_bytes(processResponse, ciphertext, [1,128], max_threads=2)
303#   This is more in-depth (every bit of each byte) and more threads
304# result = chosenct.probe_bytes(processResponse, ciphertext, [1,2,4,8,16,32,64,128], max_threads=5)
305#   Yet more intensive (every byte value against every byte):
306# result = chosenct.probe_bytes(processResponse, ciphertext, list(range(1,256)), max_threads=8)
307#
308# print(result.toHTML())
309''')
Note: See TracBrowser for help on using the repository browser.