-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttps_request.py
More file actions
209 lines (162 loc) · 6.8 KB
/
Copy pathhttps_request.py
File metadata and controls
209 lines (162 loc) · 6.8 KB
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
209
from __future__ import print_function
from scapy.layers.ssl_tls import *
import scapy.layers.ssl_tls as scapy_ssl
import argparse
import sys
import helpers
CONTEXT = False
def tls_client(ip, request, tls_version, ciphers, extensions):
resp = None
with TLSSocket(client=True) as tls_socket:
try:
tls_socket.connect(ip)
print("Connected to server: %s" % (ip,))
except socket.timeout:
print("Failed to open connection to server: %s" % (ip,), file=sys.stderr)
else:
try:
server_hello, server_kex = tls_socket.do_handshake(tls_version, ciphers, extensions)
except TLSProtocolError as tpe:
print("Got TLS error: %s" % tpe, file=sys.stderr)
else:
resp = tls_socket.do_round_trip(TLSPlaintext(data=request))
tls_socket.close()
if CONTEXT:
print(tls_socket.tls_ctx)
return resp
def run(server, req, tls_version, ciphers, extensions, resp_file):
resp = tls_client((host, port), req, tls_version, ciphers, extensions)
if not resp:
sys.exit(-1)
if not resp.fields:
resp.show()
else:
for record in resp.fields['records']:
if TLSPlaintext in record:
data = record[TLSPlaintext].data
with open(resp_file, 'a+') as f:
f.write(data)
def versions_action():
class customAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
print_versions()
setattr(args, self.dest, values)
parser.exit()
return customAction
def extensions_action():
class customAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
print_extensions()
setattr(args, self.dest, values)
parser.exit()
return customAction
def ciphersuites_action():
class customAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
print_ciphersuites()
setattr(args, self.dest, values)
parser.exit()
return customAction
def all_action():
class customAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
print_versions()
print_extensions()
print_ciphersuites()
setattr(args, self.dest, values)
parser.exit()
return customAction
def print_versions():
print('Versions')
for i in range(len(helpers.versions)):
print (str(i) + ': ' + helpers.versions[i])
def print_extensions():
print('Extensions')
for i in range(len(helpers.extensions)):
print (str(i) + ': ' + helpers.extensions[i])
def print_ciphersuites():
print('Ciphersuites')
for i in range(len(helpers.ciphersuites)):
print (str(i) + ': ' + helpers.ciphersuites[i])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('host', type=str, nargs=1)
parser.add_argument('port', type=int, nargs=1)
parser.add_argument('-r', '--request', type=str, nargs='?', help='Filename contaiting request header')
parser.add_argument('-o', '--response', type=str, nargs=1, help='Filename to write response')
parser.add_argument('-u', '--url', type=str, nargs='?', help='GET request url')
parser.add_argument('--list_versions', action=versions_action(), nargs=0)
parser.add_argument('--list_extensions', action=extensions_action(), nargs=0)
parser.add_argument('--list_ciphersuites', action=ciphersuites_action(), nargs=0)
parser.add_argument('-a', '--list_all', action=all_action(), nargs=0)
parser.add_argument('-v', '--version', type=int, nargs='*', help='Version number to be used. \
Use --list_versions options to find the corresponding number')
parser.add_argument('-c', '--ciphersuites', type=int, nargs='*', help='Ciphersuite to be used. \
Use --list_ciphersuites options to find the corresponding number')
parser.add_argument('-e', '--extensions', type=int, nargs='*', help='Extensions to be used. \
Use --list_extensions options to find the corresponding number')
parser.add_argument('--version_name', type=str, nargs='?', help='Version number to be used.')
parser.add_argument('--ciphersuites_name', type=str, nargs='*', help='Ciphersuite to be used.')
parser.add_argument('--extensions_name', type=str, nargs='*', help='Extensions to be used.')
parser.add_argument('--cipher_type', type=str, nargs=1, help='Type of ciphersuites to be used.')
parser.add_argument('--context', help='Print TLS context.', action='store_true')
args = parser.parse_args()
if args.context:
CONTEXT = True
if args.version:
tls_version = helpers.get_version(args.version[0])
elif args.version_name:
tls_version = getattr(TLSVersion, args.version_name)
else:
tls_version = getattr(TLSVersion, 'TLS_1_2')
extensions = []
if args.extensions:
for e in args.extensions:
tmp_ext = helpers.get_ext(e)
extensions.append(TLSExtension()/tmp_ext())
elif args.extensions_name:
for name in args.extensions_name:
tmp_ext = getattr(scapy_ssl, name)
extensions.append(TLSExtension()/tmp_ext())
ciphers = []
if args.cipher_type:
ciphers = helpers.get_all_ciphers(args.cipher_type[0])
if len(ciphers) == 0:
print('No such ciphersuites')
sys.exit(0)
elif args.ciphersuites:
for c in args.ciphersuites:
ciphers.append(helpers.get_cs(c))
elif args.ciphersuites_name:
for c in args.ciphersuites_name:
cs = getattr(TLSCipherSuite, c)
ciphers.append(cs)
else:
ciphers.append(helpers.get_cs(179))
tmp_ext = helpers.get_ext(4)
extensions.append(TLSExtension()/tmp_ext())
tmp_ext = helpers.get_ext(18)
extensions.append(TLSExtension()/tmp_ext())
host = args.host[0]
port = args.port[0]
resp_file = args.response[0]
if args.request:
req = args.request
with open(req, 'r') as f:
req = f.read()
req = req.replace('\n', '\r\n')
elif args.url:
url = args.url
print(url)
req = 'GET' + ' /' + url.split('://')[1].split('/', 1)[1] + ' HTTP/1.1\r\n' + 'HOST: ' + host + '\r\n\r\n'
else:
print('Give one of request file or URL')
sys.exit(-1)
if req[-4:] != '\r\n\r\n':
req += '\r\n'
if req[-4:] != '\r\n\r\n':
req += '\r\n'
print('Request:\n', repr(req))
with open(resp_file, 'w') as f:
f.write("")
run((host, port), req, tls_version, ciphers, extensions, resp_file)