-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidentifyingHidden.py
399 lines (367 loc) · 15.7 KB
/
identifyingHidden.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
#对存在的URL返回结果进行过滤
import re
import time
import xml.sax
import nltk
import sqlite3
import collections
import os
import base64
import requests
from lxml import etree
from urllib.parse import urljoin
from dbAssistant import list2file
#input:vendor,model
#output:可能存在信息泄露的URL返回包
def filterInfoLeak(vendor, model, firmadyne):
normal_key = ['URLBase','deviceType','friendlyName','serialNumber','UDN','presentationURL','webaccess','<firmware>','Model','WAN','wlan1_security','wpa2auto_psk','wlan1_wps_enable','wlan1_psk_cipher_type','wlan1_psk_pass_phrase','rid','appname','appsign','fw_ver','author','mode','question','pwdSet','USRegionTag','router_name_div','ssid0','Brand','LANG','DefaultIP','LAN_MAC','WAN_MAC','specVersion', 'serviceStateTable', 'webfile_images','<wlan1_ssid>','stamac','fw_version','SOAPVersion','question0','mydlink_triggedevent_history','mydlink_logdnsquery','Message:1','Router Firmware Version','controlling','macaddr','External Version','<diagnostic>','<havenewfirmware/>','bind_bssid','functions','Recovered']
if firmadyne:
dbFile = "dbs/{}_{}_firmadyne.db3".format(vendor, model)
else:
dbFile = "dbs/{}_{}.db3".format(vendor, model)
if os.path.isfile(dbFile):
conn = sqlite3.connect(dbFile)
# print("Connect to DB file: " + dbFile)
else:
return -1
cursor = conn.cursor()
conts = cursor.execute("select url, content from potential_exist").fetchall()
outFile = "infoLeakPages.csv"
fp = open(outFile, "a")
fp.write("{},{}\nrank,url,keys\n".format(vendor, model))
num = 0
for cont in conts:
url = cont[0]
rank = 0
bStr = base64.b64decode(cont[1].encode()).decode("utf-8","ignore")
keys = []
for key in normal_key:
if key in bStr:
rank += 1
keys.append(key)
if rank > 2:
fp.write("{},{},{}\n".format(rank, url, keys))
num += 1
fp.write("Filtered Number: {}\n".format(num))
fp.write("-"*150 + "\n")
fp.close()
def get_file_path(root_path, file_list):
#获取该目录下所有的文件名称和目录名称
dir_or_files = os.listdir(root_path)
for dir_file in dir_or_files:
#获取目录或者文件的路径
dir_file_path = os.path.join(root_path,dir_file)
#判断该路径为文件还是路径
if os.path.isdir(dir_file_path):
#递归获取所有文件和目录的路径
get_file_path(dir_file_path, file_list)
elif dir_file_path.endswith(".html") or dir_file_path.endswith(".js") \
or dir_file_path.endswith(".htm") or dir_file_path.endswith(".php"):
file_list.append(dir_file_path)
def mkTables(cursor, conn):
cursor.execute("drop table if exists cgis")
cursor.execute("create table cgis (ID integer primary key, cgi varchar(100) not null)")
cursor.execute("drop table if exists params")
cursor.execute("create table params (ID integer primary key, cgiID integer, name varchar(100) not null, " +
"defaultValue varchar(100), type varchar(100), class varchar(100)," +
"maxlength varchar(100))")
cursor.execute("drop table if exists requests")
cursor.execute(
"create table requests (ID integer primary key, url varchar(200), payload varchar(500), response varchar(1000), diff varchar(10))")
conn.commit()
def ajaxCgiParams(vendor, model):
rules = ["\$\.post\(([^,]+,[^\)]+),\s*function\s*\(data\)\s*\{",
# "$.post( path +\"auth_info_failure.cgi\",{\"mac\":macStr},showMessage);",
'srouter.init.common.ajax\(([^,]+,[^\)]+),\s*function\s*\(data\)\s*\{',
# '$.post("/app/webauth_example/webs/auth_info_check.cgi",obj,function(data){',
'ajaxObj\.sendRequest\(([^,]+,[^;]+)\);'
]
firmPath = ".\\{}\\{}\\firmware".format(vendor, model)
file_list = []
get_file_path(firmPath, file_list)
#get cgis
conn = sqlite3.connect('dbs/{}_{}.db3'.format(vendor, model))
cursor = conn.cursor()
urlcur = cursor.execute("select url from potential_exist")
urls = urlcur.fetchall()
cgis = set([])
for u in urls:
url = u[0]
if url.endswith(".cgi") or url.endswith(".php"):
cgis.add(u[0].split("/")[-1])
conn.close()
#matching rules
keywords = set()
resList = []
for f in file_list:
for cgi in cgis:
try:
cont = open(f, encoding='gbk', errors='ignore').read()
except:
cont = ""
if cont and cgi in cont:
for rule in rules:
res = re.findall(rule, cont)
for r in res:
pRes = re.sub("[^(\x21-\x7e)]","",r)
cgi = pRes[:pRes.index(",")]
params = pRes[pRes.index(",")+1:]
if re.match("[\w\.]+", params): #参数是一个变量,去上下文中寻找
typ, postParams = extractCgiParams(params, cont)
if typ == "dict":
for k in postParams.keys():
keywords.add(k)
elif typ == "list":
for k in postParams:
keywords.add(k)
else:#参数是字典格式的
postParams = {}
params = re.sub("[\"\+]","",params)
for p in params.split("&"):
if "=" in p:
key, value = p.split("=")
postParams[key] = value
keywords.add(key)
pRes = "{}, {}".format(cgi, postParams)
if not pRes in resList:
resList.append(pRes)
#save match results to files
resFile = "./{}/{}/cgiParams.csv".format(vendor, model)
if resList:
list2file(resList, resFile)
print("CGI params has been writen to {}".format(resFile))
else:
print("No cgi is found!")
return 0
with open(resFile, "a") as f:
f.write("\nkeywords:\n{}".format(list(keywords)))
#save match results to database
conn = sqlite3.connect("./{}/{}/cgiFilter.db3".format(vendor, model))
cursor = conn.cursor()
mkTables(cursor, conn)
cgiId = 0
paramID = 0
for res in resList:
cgi = res[:res.index(",")]
params = res[res.index(",") + 1:]
#deal with CGI
cgi = re.sub("['\"]","",cgi)
if cgi.startswith("path+"):
cgi = cgi.replace("path+", "/app/safety_wireless/webs/")# fix for G1 and F5C
cgiId += 1
cursor.execute("insert into cgis values ({}, '{}')".format(cgiId, cgi))
#deal with params, output is dictory
if "{" in params:
params = re.sub("[\\{\\}\\s]", "", params)
if not params:#params is {}
continue
params = re.sub("\(.*\)","\(\)", params)
for key_val in params.split(","):
key, val = key_val.split(":")
if "'" in val or "\"" in val or val.isdigit():
value = re.sub("['\"]", "", val)
cls = ""
elif val:
value = ""
cls = val
paramID += 1
cursor.execute("insert into params values ({}, {}, '{}', '{}', '', '{}', '')".format(paramID, cgiId, re.sub("['\"]", "", key), value, cls))
elif "[" in params:
#we need to get param from context
params = re.sub("[\\[\\]\\s']", "", params)
for p in params.split(","):
paramID += 1
cursor.execute(
"insert into params values ({}, {}, '{}', '', '', '', '')".format(paramID, cgiId, p))
conn.commit()
conn.close()
def extractCgiParams(key, text):
pattern1 = "\\b"+key+"\\s*=\\s*\{\\s*\};"
pattern2 = "\\b"+key+"\\s*=\\s*new\\sObject;"
pattern3 = "\\b"+key+"\\s*=\\s*\\{([^\\}]+)\\};"
if re.findall(pattern1, text) or re.findall(pattern2, text):
values = re.findall("\\b{}\\.(\\w+)\\b".format(key), text)
res = set()
for v in values:
res.add(v)
return "list", list(res)
elif re.findall(pattern3, text):
dicts = re.findall(pattern3, text)
if dicts:
res_dict = {}
for d in dicts:
dict = re.sub("\\s", "", d)
for key_value in dict.split(","):
if ":" in key_value:
key, value = key_value.split(":")
res_dict[key] = value
# res_dict.update(dict)
return "dict", res_dict
return "str", key
def getParams4Cgi(html):
try:
html_content = open(html, errors="ignore").read()
except Exception as e:
print("Error {} in open {}".format(e, html))
return [], []
try:
xhtml = etree.HTML(html_content)
except Exception as e:
print("Error {} in parsing {}".format(e, html))
xhtml = None
if xhtml is None:
return [], []
form = xhtml.xpath('//form')
action = []
input_list = []
if len(form):
for f in form:
action.append(f.attrib.get('action',''))
inputs = xhtml.xpath('//input')
for i in inputs:
input_list.append((i.attrib.get('name',''), i.attrib.get('value',''), i.attrib.get('type',''), i.attrib.get('class',''), i.attrib.get('maxlength','')))
return action, input_list
#inputs: [(name, value, type, class, maxlength)]
def diffRequest(url, inputs):
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"}
proxies = {"http": "socks5://127.0.0.1:1088",'https': 'socks5://127.0.0.1:1088'}
# proxies = {}
try:
r = requests.get(url, headers=headers, timeout=3, proxies=proxies)
unparaRsps = r.content
except Exception as e:
unparaRsps = b'Error: ' + str(e).encode()
try:
payload = {}
for input in inputs:
key = input[0]
cls = input[3]
if not key:
continue
if input[1] and not re.findall("[\.<\(]", input[1]):
value = input[1]
elif cls == 'num':
value = '0'
elif re.match("addr", key):
value = "192.168.0.100"
elif re.match("mask", key):
value = "255.255.255.0"
elif re.match("gateway", key):
value = "192.168.0.1"
elif re.match("mac", key, re.I):
value = "24:41:8C:01:2F:7E"
elif re.match("username", key, re.I):
value = "usertest"
elif re.match("pass", key, re.I):
value = "usertest123"
elif re.match("dns", key, re.I):
value = "8.8.8.8"
elif re.match("dst", key, re.I):
value = "127.0.0.1"
else:
value = 'some'
payload[key] = value
r = requests.post(url, data=payload, headers=headers, timeout=3)
paraRsps = r.content
except Exception as e:
paraRsps = b'null'
return base64.b64encode(unparaRsps), base64.b64encode(paraRsps), payload
def htmlCgiParams(vendor, model):
print("Finding post cgi from html : {} {}".format(vendor, model))
dbFile = "./{}/{}/cgiFilter.db3".format(vendor, model)
conn = sqlite3.connect(dbFile)
cursor = conn.cursor()
mkTables(cursor, conn)
scanDir = "./{}/{}/firmware/www".format(vendor, model)
htmls = []
actionID = 0
inputID = 0
resList = []
for root, dirs, files in os.walk(scanDir):
for name in files:
if name.endswith(".htm"):
htmls.append(os.path.join(root, name))
keywords = set()
for html in htmls:
action, input_list = getParams4Cgi(html)
for a in action:
if not a or "<" in a:
continue
actionID += 1
cursor.execute("insert into cgis values ({}, '{}')".format(actionID, a))
params = {}
for i in input_list:
inputID += 1
cursor.execute("insert into params values ({}, {}, '{}', '{}', '{}', '{}', '{}')".
format(inputID, actionID, i[0], i[1], i[2], i[3], i[4]))
params[i[0]] = i[1]
keywords.add((i[0]))
resStr = "{}, {}".format(a, str(params))
if not resStr in resList:
resList.append(resStr)
conn.commit()
conn.close()
#save data in csv
resFile = "./{}/{}/cgiParams.csv".format(vendor, model)
if resList:
list2file(resList, resFile)
print("CGI params has been writen to {}".format(resFile))
else:
print("No form is found!")
with open(resFile, "a") as f:
f.write("\nkeywords:\n{}".format(list(keywords)))
def mkDiffRequests(vendor, model, base_url):
dbFile = "./{}/{}/cgiFilter.db3".format(vendor, model)
conn = sqlite3.connect(dbFile)
cursor = conn.cursor()
cgis = cursor.execute("select * from cgis").fetchall()
reqID = 0
for c in cgis:
cgiID = c[0]
cgi = c[1]
url = urljoin(base_url, cgi)
print(url)
params = cursor.execute("select name, defaultValue, type, class, maxlength from params where cgiID={}".format(cgiID)).fetchall()
unparaRsps, paraRsps, payload = diffRequest(url, params)
if unparaRsps == paraRsps:
diff = "False"
else:
diff = "True"
cursor.execute("insert into requests values ({}, '{}', '', '{}', '{}')".format(reqID + 1, url, unparaRsps.decode(), diff))
cursor.execute(
"insert into requests values ({}, '{}', \"{}\", '{}', '{}')".format(reqID + 2, url, payload, paraRsps.decode(), diff))
reqID += 2
conn.commit()
conn.close()
def listDiffCgi(vendor, model):
dbFile = "./{}/{}/cgiFilter.db3".format(vendor, model)
conn = sqlite3.connect(dbFile)
cursor = conn.cursor()
requests = cursor.execute("select url, payload, response from requests where diff='True'").fetchall()
with open("./{}/{}/unauthSetting.log.txt".format(vendor, model), "w") as f:
for req in requests:
url = req[0]
payload = req[1]
resp = base64.b64decode(req[2]).decode()
f.write("Url:\t{}\nPayload:\t{}\nresponse:\t{}\n".format(url, payload, resp))
f.write("-"*100 + "\n")
print("param and unparam reps hav been saved in {}".format(dbFile))
if __name__ == "__main__":
start = time.time()
vms = ['Amcrest IP2M841', 'ASUS AC55U', 'D-Link DIR-868L', 'D-Link DIR-412', 'D-Link DIR-816',
'D-Link DAP-1320', 'H3C MAGIC', 'Mercury MIPC372-4', 'Mercury MNVR408', 'Nettcore G1',
'Netgear PLW1000', 'Netgear W104', 'Netgear WNDR4000', 'Qihoo360 F5C', 'Tenda G103',
'TP-Link GP110', 'Wavlink AC1200']
urlDIct = {"G1":"http://192.168.1.1", "F5C":"http://192.168.0.1", "WNDR4000": "http://192.168.1.1",
"DIR-412":"http://31.170.175.40:3390/", "G103": "http://192.168.1.11/", "WNDR4000": "http://192.168.1.1/"}
firmadyne = False
vms = ["Netgear WNDR4000"]
for vm in vms:
vendor, model = vm.split()
filterInfoLeak(vendor, model, False)
ajaxCgiParams(vendor, model)
htmlCgiParams(vendor, model)
base_url = urlDIct[model]
mkDiffRequests(vendor, model, base_url)
listDiffCgi(vendor, model)
print("Time cost: {}s".format(time.time()-start))