-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsfp.py
executable file
·193 lines (160 loc) · 4.35 KB
/
wsfp.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
#!/usr/bin/python
import socket, sys, os, base64, re, glob
from pprint import *
class WSFP:
def __init__(self, host, port):
self.host = host
self.port = port
self.kdb = {}
self.badrequests = {
'bad_content-length': 'POST / HTTP/1.0\nConnection: close\nContent-Length: x\n\n',
'bad_host': 'GET / HTTP/1.0\nHost: x]x:-\nConnection: close\n\n',
'bad_method': 'GARBLARGH / HTTP/1.0\nConnection: close\n\n',
'bad_proto': 'GET / GARBLARGH/1.0\nConnection: close\n\n',
'bad_proto_version': 'GET / HTTP/x.y\nConnection: close\n\n',
'bad_uri': 'GET x HTTP/1.0\nConnection: close\n\n',
'options': 'OPTIONS * HTTP/1.0\nConnection: close\n\n'
}
self.results = {}
self.keys = ['code', 'proto', 'msg', 'error']
# def load_requests(self):
# ret = {}
# for fn in glob.glob('./rq/*.rq'):
# rname = os.path.basename(fn)[:-3]
# f = open(fn)
# fc = ''.join(f.readlines())
# self.requests[rname] = fc.rstrip() + "\n\n"
# self.requests[rname].replace("\n", "\r\n")
# print "%s %s %s | %s" % (ret['proto'], ret['code'], ret['msg'], ret['error'])
def run(self):
# self.load_requests()
self.run_tests()
# pprint(self.results)
self.analyze()
# self.save_kdb()
def run_tests(self):
for rqn in self.badrequests:
self.results[rqn] = self.req(rqn)
def analyze(self):
self.load_kdb()
self.analyze_server_header()
self.analyze_match()
#
# proxy check
# CVE check
def load_kdb(self):
files = glob.glob('./kdb/*.dat')
for f in files:
fh = open(f)
lines = fh.readlines()
for line in lines:
s, v, b64 = line.split(' ', 2)
sv = "%s %s" % (s, v)
self.kdb[sv] = eval(base64.b64decode(b64))
# pprint(server)
# pprint(version)
# pprint(b64)
pprint(self.kdb)
def analyze_match(self):
t_stat, stat = {}, {}
kdbsize = len(self.kdb)
for server in self.kdb:
for rqn in self.kdb[server]:
for var in self.kdb[server][rqn]:
obj = "%s.%s=%s" % (rqn, var, self.kdb[server][rqn][var])
try:
t_stat[obj] += 1
except KeyError:
t_stat[obj] = 1
for k in t_stat:
stat[k] = "%.02f" % long(t_stat[k]) / kdbsize
pprint(stat)
def save_kdb(self):
res = {}
for r in self.results:
res[r] = self.results[r]
res[r].pop('headers')
rr = pformat(res).lstrip().split("\n")
ret = ''
for l in rr: ret += re.sub(r'^\s+', '', l)
# print 'Result:', ret
ret = base64.b64encode(ret)
# print 'Result:', ret
a = '-'
while a not in ['y', 'n']:
print 'do you know what is it? wanna save?'
a = sys.stdin.readline().strip()
if a == 'y':
print 'server?'
server = sys.stdin.readline().strip()
print 'version?'
version = sys.stdin.readline().strip()
f = open('./kdb/'+server+'_'+version+'.dat', 'w')
f.write("%s %s %s" % (server, version, ret))
def analyze_server_header(self):
ret = []
for r in self.results:
try:
server = self.results[r]['headers']['server']
except KeyError as e:
pass
else:
if server not in ret:
ret.append(server)
print "Server headers found:\n\t%s" % "\n\t".join(ret)
for h in ret:
self.server_version(h)
def server_version(self, ver):
ret = {}
# TODO, this implementation is pretty poor... imean crappy..
try:
name, ver = ver.split("/")
except:
pass
else:
print "Server type: %s\nServer version: %s" % (name, ver)
def req(self, rqname):
# print ":: %s" % rqname
ret = {}
s = self.conn()
s.send(self.badrequests[rqname])
f = s.makefile()
r = f.readline().strip()
ret = {
'proto': '',
'code': '',
'msg': '',
'error': '',
'headers': {}
}
# pprint(r)
try:
ret['proto'], ret['code'], ret['msg'] = r.split(' ', 2)
except ValueError as e:
ret['error'] = 1 # e
return ret
# print "0: %s | %s | %s" % (proto, code, msg)
while 1:
r = f.readline().strip()
if r == '':
break
hdr, val = r.split(':', 1)
hdr = hdr.strip().lower()
val = val.strip()
# print "1: %s | %s " % (hdr, val)
if hdr in ret['headers']:
print 'DUPLICATE HEADER: %s: %s' % (hdr, val)
ret['headers'][hdr] = val
s.close()
return ret
def conn(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
except socket.error as e:
print "err: %s\n" % e
sys.exit(1)
return s
if __name__ == '__main__':
wfp = WSFP(sys.argv[1], int(sys.argv[2]))
wfp.run()