-
Notifications
You must be signed in to change notification settings - Fork 235
/
curlcontrol.py
518 lines (486 loc) · 19.9 KB
/
curlcontrol.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
# -*- coding: utf-8 -*-"
# vim: set expandtab tabstop=4 shiftwidth=4:
"""
This file is part of the XSSer project, https://xsser.03c8.net
Copyright (c) 2010/2019 | psy <epsylon@riseup.net>
xsser 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 version 3 of the License.
xsser 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 xsser; if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import os, urllib.request, urllib.parse, urllib.error, email, pycurl, re, time, random
from io import StringIO as StringIO
class Curl:
"""
Class to control curl on behalf of the application.
"""
cookie = None
dropcookie = None
referer = None
headers = None
proxy = None
ignoreproxy = None
tcp_nodelay = None
xforw = None
xclient = None
atype = None
acred = None
#acert = None
retries = 1
delay = 0
followred = 0
fli = None
agents = [] # user-agents
try:
f = open("core/fuzzing/user-agents.txt").readlines() # set path for user-agents
except:
f = open("fuzzing/user-agents.txt").readlines() # set path for user-agents when testing
for line in f:
agents.append(line)
agent = random.choice(agents).strip() # set random user-agent
def __init__(self, base_url="", fakeheaders=[ 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg', 'Connection: Keep-Alive', 'Content-type: application/x-www-form-urlencoded; charset=UTF-8']):
self.handle = pycurl.Curl()
self._closed = False
self.set_url(base_url)
self.verbosity = 0
self.signals = 1
self.payload = ""
self.header = StringIO()
self.fakeheaders = fakeheaders
self.headers = None
self.set_option(pycurl.SSL_VERIFYHOST, 0)
self.set_option(pycurl.SSL_VERIFYPEER, 0)
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_2) # max supported version by pycurl
except:
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_1)
except: # use vulnerable TLS/SSL versions (TLS1_0 -> weak enc | SSLv2 + SSLv3 -> deprecated)
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_0)
except:
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
except:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv2)
self.set_option(pycurl.FOLLOWLOCATION, 0)
self.set_option(pycurl.MAXREDIRS, 50)
# this is 'black magic'
self.set_option(pycurl.COOKIEFILE, '/dev/null')
self.set_option(pycurl.COOKIEJAR, '/dev/null')
self.set_timeout(30)
self.set_option(pycurl.NETRC, 1)
self.set_nosignals(1)
def payload_callback(x):
self.payload += str(x)
self.set_option(pycurl.WRITEFUNCTION, payload_callback)
def header_callback(x):
self.header.write(str(x))
self.set_option(pycurl.HEADERFUNCTION, header_callback)
def set_url(self, url):
"""
Set the base url.
"""
self.base_url = url
self.set_option(pycurl.URL, self.base_url)
return url
def set_cookie(self, cookie):
"""
Set the app cookie.
"""
self.cookie = cookie
self.dropcookie = dropcookie
if dropcookie:
self.set_option(pycurl.COOKIELIST, 'ALL')
self.set_option(pycurl.COOKIE, None)
else:
self.set_option(pycurl.COOKIELIST, '')
self.set_option(pycurl.COOKIE, self.cookie)
return cookie
def set_agent(self, agent):
"""
Set the user agent.
"""
self.agent = agent
self.set_option(pycurl.USERAGENT, self.agent)
return agent
def set_referer(self, referer):
"""
Set the referer.
"""
self.referer = referer
self.set_option(pycurl.REFERER, self.referer)
return referer
def set_headers(self, headers):
"""
Set extra headers.
"""
self.headers = headers
self.headers = self.headers.split("\n")
for headerValue in self.headers:
header, value = headerValue.split(": ")
if header and value:
self.set_option(pycurl.HTTPHEADER, (header, value))
return headers
def set_proxy(self, ignoreproxy, proxy):
"""
Set the proxy to use.
"""
self.proxy = proxy
self.ignoreproxy = ignoreproxy
if ignoreproxy:
self.set_option(pycurl.PROXY, "")
else:
self.set_option(pycurl.PROXY, self.proxy)
return proxy
def set_option(self, *args):
"""
Set the given option.
"""
self.handle.setopt(*args)
def set_verbosity(self, level):
"""
Set the verbosity level.
"""
self.set_option(pycurl.VERBOSE, level)
def set_nosignals(self, signals="1"):
"""
Disable signals.
curl will be using other means besides signals to timeout
"""
self.signals = signals
self.set_option(pycurl.NOSIGNAL, self.signals)
return signals
def set_tcp_nodelay(self, tcp_nodelay):
"""
Set the TCP_NODELAY option.
"""
self.tcp_nodelay = tcp_nodelay
self.set_option(pycurl.TCP_NODELAY, tcp_nodelay)
return tcp_nodelay
def set_timeout(self, timeout):
"""
Set timeout for requests.
"""
self.set_option(pycurl.CONNECTTIMEOUT,timeout)
self.set_option(pycurl.TIMEOUT, timeout)
return timeout
def set_follow_redirections(self, followred, fli):
"""
Set follow locations parameters to follow redirection pages (302)
"""
self.followred = followred
self.fli = fli
if followred:
self.set_option(pycurl.FOLLOWLOCATION , 1)
self.set_option(pycurl.MAXREDIRS, 50)
if fli:
self.set_option(pycurl.MAXREDIRS, fli)
else:
self.set_option(pycurl.FOLLOWLOCATION , 0)
return followred
def do_head_check(self, urls):
"""
Send a HEAD request before to start to inject to verify stability of the target
"""
for u in urls:
self.set_option(pycurl.URL, u)
self.set_option(pycurl.NOBODY,1)
self.set_option(pycurl.FOLLOWLOCATION, 1)
self.set_option(pycurl.MAXREDIRS, 50)
self.set_option(pycurl.SSL_VERIFYHOST, 0)
self.set_option(pycurl.SSL_VERIFYPEER, 0)
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_2) # max supported version by pycurl
except:
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_1)
except: # use vulnerable TLS/SSL versions (TLS1_0 -> weak enc | SSLv2 + SSLv3 -> deprecated)
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_TLSv1_0)
except:
try:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
except:
self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv2)
if self.fakeheaders:
from core.randomip import RandomIP
if self.xforw:
generate_random_xforw = RandomIP()
xforwip = generate_random_xforw._generateip('')
xforwfakevalue = ['X-Forwarded-For: ' + str(xforwip)]
if self.xclient:
generate_random_xclient = RandomIP()
xclientip = generate_random_xclient._generateip('')
xclientfakevalue = ['X-Client-IP: ' + str(xclientip)]
if self.xforw:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xforwfakevalue)
if self.xclient:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xforwfakevalue + xclientfakevalue)
elif self.xclient:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xclientfakevalue)
if self.headers:
self.fakeheaders = self.fakeheaders + self.headers
self.set_option(pycurl.HTTPHEADER, self.fakeheaders)
if self.agent:
self.set_option(pycurl.USERAGENT, self.agent)
if self.referer:
self.set_option(pycurl.REFERER, self.referer)
if self.proxy:
self.set_option(pycurl.PROXY, self.proxy)
if self.ignoreproxy:
self.set_option(pycurl.PROXY, "")
if self.timeout:
self.set_option(pycurl.CONNECTTIMEOUT, self.timeout)
self.set_option(pycurl.TIMEOUT, self.timeout)
if self.signals:
self.set_option(pycurl.NOSIGNAL, self.signals)
if self.tcp_nodelay:
self.set_option(pycurl.TCP_NODELAY, self.tcp_nodelay)
if self.cookie:
self.set_option(pycurl.COOKIE, self.cookie)
try:
self.handle.perform()
except:
return
if str(self.handle.getinfo(pycurl.HTTP_CODE)) in ["302", "301"]:
self.set_option(pycurl.FOLLOWLOCATION, 1)
def __request(self, relative_url=None):
"""
Perform a request and returns the payload.
"""
if self.fakeheaders:
from core.randomip import RandomIP
if self.xforw:
"""
Set the X-Forwarded-For to use.
"""
generate_random_xforw = RandomIP()
xforwip = generate_random_xforw._generateip('')
xforwfakevalue = ['X-Forwarded-For: ' + str(xforwip)]
if self.xclient:
"""
Set the X-Client-IP to use.
"""
generate_random_xclient = RandomIP()
xclientip = generate_random_xclient._generateip('')
xclientfakevalue = ['X-Client-IP: ' + str(xclientip)]
if self.xforw:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xforwfakevalue)
if self.xclient:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xforwfakevalue + xclientfakevalue)
elif self.xclient:
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + xclientfakevalue)
if self.headers:
self.fakeheaders = self.fakeheaders + self.headers
self.set_option(pycurl.HTTPHEADER, self.fakeheaders)
if self.agent:
self.set_option(pycurl.USERAGENT, self.agent)
if self.referer:
self.set_option(pycurl.REFERER, self.referer)
if self.proxy:
self.set_option(pycurl.PROXY, self.proxy)
if self.ignoreproxy:
self.set_option(pycurl.PROXY, "")
if relative_url:
self.set_option(pycurl.URL,os.path.join(self.base_url,relative_url))
if self.timeout:
self.set_option(pycurl.CONNECTTIMEOUT, self.timeout)
self.set_option(pycurl.TIMEOUT, self.timeout)
if self.signals:
self.set_option(pycurl.NOSIGNAL, self.signals)
if self.tcp_nodelay:
self.set_option(pycurl.TCP_NODELAY, self.tcp_nodelay)
if self.cookie:
self.set_option(pycurl.COOKIE, self.cookie)
if self.followred:
self.set_option(pycurl.FOLLOWLOCATION , 1)
self.set_option(pycurl.MAXREDIRS, 50)
if self.fli:
self.set_option(pycurl.MAXREDIRS, int(self.fli))
else:
self.set_option(pycurl.FOLLOWLOCATION , 0)
if self.fli:
print("\n[E] You must launch --follow-redirects command to set correctly this redirections limit\n")
return
"""
Set the HTTP authentication method: Basic, Digest, GSS, NTLM or Certificate
"""
if self.atype and self.acred:
atypelower = self.atype.lower()
if atypelower not in ( "basic", "digest", "ntlm", "gss" ):
print("\n[E] HTTP authentication type value must be: Basic, Digest, GSS or NTLM\n")
return
acredregexp = re.search("^(.*?)\:(.*?)$", self.acred)
if not acredregexp:
print("\n[E] HTTP authentication credentials value must be in format username:password\n")
return
user = acredregexp.group(1)
password = acredregexp.group(2)
self.set_option(pycurl.USERPWD, "%s:%s" % (user,password))
if atypelower == "basic":
self.set_option(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
elif atypelower == "digest":
self.set_option(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
elif atypelower == "ntlm":
self.set_option(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NTLM)
elif atypelower == "gss":
self.set_option(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE)
else:
self.set_option(pycurl.HTTPAUTH, None)
self.set_option(pycurl.HTTPHEADER, ["Accept:"])
elif self.atype and not self.acred:
print("\n[E] You specified the HTTP authentication type, but did not provide the credentials\n")
return
elif not self.atype and self.acred:
print("\n[E] You specified the HTTP authentication credentials, but did not provide the type\n")
return
#if self.acert:
# acertregexp = re.search("^(.+?),\s*(.+?)$", self.acert)
# if not acertregexp:
# print "\n[E] HTTP authentication certificate option must be 'key_file,cert_file'\n"
# return
# # os.path.expanduser for support of paths with ~
# key_file = os.path.expanduser(acertregexp.group(1))
# cert_file = os.path.expanduser(acertregexp.group(2))
# self.set_option(pycurl.SSL_VERIFYHOST, 0)
# self.set_option(pycurl.SSL_VERIFYPEER, 1)
# self.set_option(pycurl.SSH_PUBLIC_KEYFILE, key_file)
# self.set_option(pycurl.CAINFO, cert_file)
# self.set_option(pycurl.SSLCERT, cert_file)
# self.set_option(pycurl.SSLCERTTYPE, 'p12')
# self.set_option(pycurl.SSLCERTPASSWD, '1234')
# self.set_option(pycurl.SSLKEY, key_file)
# self.set_option(pycurl.SSLKEYPASSWD, '1234')
# for file in (key_file, cert_file):
# if not os.path.exists(file):
# print "\n[E] File '%s' doesn't exist\n" % file
# return
self.set_option(pycurl.SSL_VERIFYHOST, 0)
self.set_option(pycurl.SSL_VERIFYPEER, 0)
self.header.seek(0,0)
self.payload = ""
for count in range(0, self.retries):
time.sleep(self.delay)
if self.dropcookie:
self.set_option(pycurl.COOKIELIST, 'ALL')
nocookie = ['Set-Cookie: ', '']
self.set_option(pycurl.HTTPHEADER, self.fakeheaders + nocookie)
try:
self.handle.perform()
except:
return
return self.payload
def get(self, url="", params=None):
"""
Get a url.
"""
if params:
url += "?" + urllib.parse.urlencode(params)
self.set_option(pycurl.HTTPGET, 1)
return self.__request(url)
def post(self, cgi, params):
"""
Post a url.
"""
self.set_option(pycurl.POST, 1)
self.set_option(pycurl.POSTFIELDS, params)
return self.__request(cgi)
def body(self):
"""
Get the payload from the latest operation.
"""
return self.payload
def info(self):
"""
Get an info dictionary from the selected url.
"""
self.header.seek(0,0)
url = self.handle.getinfo(pycurl.EFFECTIVE_URL)
if url.startswith('http'):
self.header.readline()
m = email.message_from_string(str(self.header))
else:
m = email.message_from_string(str(StringIO()))
#m['effective-url'] = url
m['http-code'] = str(self.handle.getinfo(pycurl.HTTP_CODE))
m['total-time'] = str(self.handle.getinfo(pycurl.TOTAL_TIME))
m['namelookup-time'] = str(self.handle.getinfo(pycurl.NAMELOOKUP_TIME))
m['connect-time'] = str(self.handle.getinfo(pycurl.CONNECT_TIME))
#m['pretransfer-time'] = str(self.handle.getinfo(pycurl.PRETRANSFER_TIME))
#m['redirect-time'] = str(self.handle.getinfo(pycurl.REDIRECT_TIME))
#m['redirect-count'] = str(self.handle.getinfo(pycurl.REDIRECT_COUNT))
#m['size-upload'] = str(self.handle.getinfo(pycurl.SIZE_UPLOAD))
#m['size-download'] = str(self.handle.getinfo(pycurl.SIZE_DOWNLOAD))
#m['speed-upload'] = str(self.handle.getinfo(pycurl.SPEED_UPLOAD))
m['header-size'] = str(self.handle.getinfo(pycurl.HEADER_SIZE))
m['request-size'] = str(self.handle.getinfo(pycurl.REQUEST_SIZE))
m['response-code'] = str(self.handle.getinfo(pycurl.RESPONSE_CODE))
m['ssl-verifyresult'] = str(self.handle.getinfo(pycurl.SSL_VERIFYRESULT))
try:
m['content-type'] = (self.handle.getinfo(pycurl.CONTENT_TYPE) or '').strip(';')
except:
m['content-type'] = None
m['cookielist'] = str(self.handle.getinfo(pycurl.INFO_COOKIELIST))
#m['content-length-download'] = str(self.handle.getinfo(pycurl.CONTENT_LENGTH_DOWNLOAD))
#m['content-length-upload'] = str(self.handle.getinfo(pycurl.CONTENT_LENGTH_UPLOAD))
#m['encoding'] = str(self.handle.getinfo(pycurl.ENCODING))
return m
@classmethod
def print_options(cls):
"""
Print selected options.
"""
print("\nCookie:", cls.cookie)
print("User Agent:", cls.agent)
print("Referer:", cls.referer)
print("Extra Headers:", cls.headers)
if cls.xforw == True:
print("X-Forwarded-For:", "Random IP")
else:
print("X-Forwarded-For:", cls.xforw)
if cls.xclient == True:
print("X-Client-IP:", "Random IP")
else:
print("X-Client-IP:", cls.xclient)
print("Authentication Type:", cls.atype)
print("Authentication Credentials:", cls.acred)
if cls.ignoreproxy == True:
print("Proxy:", "Ignoring system default HTTP proxy")
else:
print("Proxy:", cls.proxy)
print("Timeout:", cls.timeout)
if cls.tcp_nodelay == True:
print("Delaying:", "TCP_NODELAY activate")
else:
print("Delaying:", cls.delay, "seconds")
if cls.followred == True:
print("Follow 302 code:", "active")
if cls.fli:
print("Limit to follow:", cls.fli)
else:
print("Delaying:", cls.delay, "seconds")
print("Retries:", cls.retries, "\n")
def answered(self, check):
"""
Check for occurence of a string in the payload from
the latest operation.
"""
return self.payload.find(check) >= 0
def close(self):
"""
Close the curl handle.
"""
self.handle.close()
self.header.close()
self._closed = True
def __del__(self):
if not self._closed:
self.close()