forked from dytttf/antispider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautohome.py
427 lines (395 loc) · 13.7 KB
/
autohome.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
#coding:utf8
import re
import string
import urllib
import requests
import traceback
from collections import defaultdict
def get_char(js, replace_count):
all_var = {}
# 判断混淆 无参数 返回常量 函数
if_else_no_args_return_constant_function_functions = []
"""
function zX_() {
function _z() {
return '09';
};
if (_z() == '09,') {
return 'zX_';
} else {
return _z();
}
}
"""
constant_function_regex4 = re.compile("""
function\s+\w+\(\)\s*\{\s*
function\s+\w+\(\)\s*\{\s*
return\s+[\'\"][^\'\"]+[\'\"];\s*
\};\s*
if\s*\(\w+\(\)\s*==\s*[\'\"][^\'\"]+[\'\"]\)\s*\{\s*
return\s*[\'\"][^\'\"]+[\'\"];\s*
\}\s*else\s*\{\s*
return\s*\w+\(\);\s*
\}\s*
\}
""",
re.X)
l = constant_function_regex4.findall(js)
for i in l:
function_name = re.search("""
function\s+(\w+)\(\)\s*\{\s*
function\s+\w+\(\)\s*\{\s*
return\s+[\'\"]([^\'\"]+)[\'\"];\s*
\};\s*
if\s*\(\w+\(\)\s*==\s*[\'\"]([^\'\"]+)[\'\"]\)\s*\{\s*
return\s*[\'\"]([^\'\"]+)[\'\"];\s*
\}\s*else\s*\{\s*
return\s*\w+\(\);\s*
\}\s*
\}
""", i,
re.X)
if_else_no_args_return_constant_function_functions.append(function_name.groups())
js = js.replace(i, "")
# 替换全文
a,b,c,d = function_name.groups()
all_var["%s()"%a] = d if b == c else b
# 判断混淆 无参数 返回函数 常量
if_else_no_args_return_function_constant_functions = []
"""
function wu_() {
function _w() {
return 'wu_';
};
if (_w() == 'wu__') {
return _w();
} else {
return '5%';
}
}
"""
constant_function_regex5 = re.compile("""
function\s+\w+\(\)\s*\{\s*
function\s+\w+\(\)\s*\{\s*
return\s+[\'\"][^\'\"]+[\'\"];\s*
\};\s*
if\s*\(\w+\(\)\s*==\s*[\'\"][^\'\"]+[\'\"]\)\s*\{\s*
return\s*\w+\(\);\s*
\}\s*else\s*\{\s*
return\s*[\'\"][^\'\"]+[\'\"];\s*
\}\s*
\}
""",
re.X)
l = constant_function_regex5.findall(js)
for i in l:
function_name = re.search("""
function\s+(\w+)\(\)\s*\{\s*
function\s+\w+\(\)\s*\{\s*
return\s+[\'\"]([^\'\"]+)[\'\"];\s*
\};\s*
if\s*\(\w+\(\)\s*==\s*[\'\"]([^\'\"]+)[\'\"]\)\s*\{\s*
return\s*\w+\(\);\s*
\}\s*else\s*\{\s*
return\s*[\'\"]([^\'\"]+)[\'\"];\s*
\}\s*
\}
""", i,
re.X)
if_else_no_args_return_function_constant_functions.append(function_name.groups())
js = js.replace(i, "")
# 替换全文
a,b,c,d = function_name.groups()
all_var["%s()"%a] = b if b == c else d
# var 参数等于返回值函数
var_args_equal_value_functions = []
"""
var ZA_ = function(ZA__) {
'return ZA_';
return ZA__;
};
"""
constant_function_regex1 = re.compile("var\s+[^=]+=\s*function\(\w+\)\{\s*[\'\"]return\s*\w+\s*[\'\"];\s*return\s+\w+;\s*\};")
l = constant_function_regex1.findall(js)
for i in l:
function_name = re.search("var\s+([^=]+)", i).group(1)
var_args_equal_value_functions.append(function_name)
js = js.replace(i, "")
# 替换全文
a = function_name
js = re.sub("%s\(([^\)]+)\)"%a, r"\1", js)
# var 无参数 返回常量 函数
var_no_args_return_constant_functions = []
"""
var Qh_ = function() {
'return Qh_';
return ';';
};
"""
constant_function_regex2 = re.compile("""
var\s+[^=]+=\s*function\(\)\{\s*
[\'\"]return\s*\w+\s*[\'\"];\s*
return\s+[\'\"][^\'\"]+[\'\"];\s*
\};
""",
re.X)
l = constant_function_regex2.findall(js)
for i in l:
function_name = re.search("""
var\s+([^=]+)=\s*function\(\)\{\s*
[\'\"]return\s*\w+\s*[\'\"];\s*
return\s+[\'\"]([^\'\"]+)[\'\"];\s*
\};
""",
i,
re.X)
var_no_args_return_constant_functions.append(function_name.groups())
js = js.replace(i, "")
# 替换全文
a,b = function_name.groups()
all_var["%s()"%a] = b
# 无参数 返回常量 函数
no_args_return_constant_functions = []
"""
function ZP_() {
'return ZP_';
return 'E';
}
"""
constant_function_regex3 = re.compile("""
function\s*\w+\(\)\s*\{\s*
[\'\"]return\s*[^\'\"]+[\'\"];\s*
return\s*[\'\"][^\'\"]+[\'\"];\s*
\}\s*
""",
re.X)
l = constant_function_regex3.findall(js)
for i in l:
function_name = re.search("""
function\s*(\w+)\(\)\s*\{\s*
[\'\"]return\s*[^\'\"]+[\'\"];\s*
return\s*[\'\"]([^\'\"]+)[\'\"];\s*
\}\s*
""",
i,
re.X)
no_args_return_constant_functions.append(function_name.groups())
js = js.replace(i, "")
# 替换全文
a,b = function_name.groups()
all_var["%s()"%a] = b
# 无参数 返回常量 函数 中间无混淆代码
no_args_return_constant_sample_functions = []
"""
function do_() {
return '';
}
"""
constant_function_regex3 = re.compile("""
function\s*\w+\(\)\s*\{\s*
return\s*[\'\"][^\'\"]*[\'\"];\s*
\}\s*
""",
re.X)
l = constant_function_regex3.findall(js)
for i in l:
function_name = re.search("""
function\s*(\w+)\(\)\s*\{\s*
return\s*[\'\"]([^\'\"]*)[\'\"];\s*
\}\s*
""",
i,
re.X)
no_args_return_constant_sample_functions.append(function_name.groups())
js = js.replace(i, "")
# 替换全文
a,b = function_name.groups()
all_var["%s()"%a] = b
# 字符串拼接时使无参常量函数
"""
(function() {
'return sZ_';
return '1'
})()
"""
constant_function_regex6 = re.compile("""
\(function\(\)\s*\{\s*
[\'\"]return[^\'\"]+[\'\"];\s*
return\s*[\'\"][^\'\"]*[\'\"];?
\}\)\(\)
""",
re.X)
l = constant_function_regex6.findall(js)
for i in l:
function_name = re.search("""
\(function\(\)\s*\{\s*
[\'\"]return[^\'\"]+[\'\"];\s*
return\s*([\'\"][^\'\"]*[\'\"]);?
\}\)\(\)
""",
i,
re.X)
js = js.replace(i, function_name.group(1))
# 字符串拼接时使用返回参数的函数
"""
(function(iU__) {
'return iU_';
return iU__;
})('9F')
"""
constant_function_regex6 = re.compile("""
\(function\(\w+\)\s*\{\s*
[\'\"]return[^\'\"]+[\'\"];\s*
return\s*\w+;
\}\)\([\'\"][^\'\"]*[\'\"]\)
""",
re.X)
l = constant_function_regex6.findall(js)
for i in l:
function_name = re.search("""
\(function\(\w+\)\s*\{\s*
[\'\"]return[^\'\"]+[\'\"];\s*
return\s*\w+;
\}\)\(([\'\"][^\'\"]*[\'\"])\)
""",
i,
re.X)
js = js.replace(i, function_name.group(1))
# 获取所有变量
var_regex = "var\s+(\w+)\s*=\s*([\'\"].*?[\'\"]);\s"
for var_name, var_value in re.findall(var_regex, js):
var_value = re.sub("\s", "", var_value).strip("\'\" ")
if "(" in var_value:
var_value = ";"
all_var[var_name] = var_value
# 注释掉 此正则可能会把关键js语句删除掉
#js = re.sub(var_regex, "", js)
for var_name, var_value in all_var.items():
js = js.replace(var_name, var_value)
js = re.sub("[\s+']", "", js)
# 寻找%E4%B8%AD%E5%80%92%E 密集区域
#string_region = re.findall("((?:%\w\w)+)", js)
string_region = re.findall("((?:%\w\w|[A-Za-z\d])+)", js)
# 去重
string_region = set(string_region)
# 判断是否存在汉字
chinese_flag = 0
for string_ in string_region:
if re.search("%\w\w", string_):
chinese_flag = 1
if not chinese_flag:
# 可能混淆字符为纯英文 。。。尚未解决
return []
string_str = ""
for string_ in string_region:
if not re.search("%\w\w", string_):
continue
# 过滤
utf8_string = urllib.unquote(string_)
if not utf8_string.isalpha():
# 去掉可能匹配到的多余字符 \w+ 建立在混淆字符串是排好序的 字母在汉字前
utf8_string = utf8_string.rstrip(string.letters + string.digits + "_")
try:
unicode_string = utf8_string.decode("utf8")
except:
continue
if len(unicode_string) < replace_count:
continue
if len(string_) > len(string_str):
string_str = string_
utf8_string = urllib.unquote(string_str)
if not utf8_string.isalpha():
# 去掉可能匹配到的多余字符 \w+ 建立在混淆字符串是排好序的 字母在汉字前
utf8_string = utf8_string.rstrip(string.letters + string.digits + "_")
unicode_string = utf8_string.decode("utf8")
# 当只有一个替换字符时 下面正则寻找失败 此时也不用寻找了
if len(unicode_string) == 1:
return [unicode_string]
# 从 字符串密集区域后面开始寻找索引区域
index_m = re.search("([\d,]+(;[\d,]+)+)", js[js.find(string_str) + len(string_str):])
string_list = list(unicode_string)
index_list = index_m.group(1).split(";")
_word_list = []
for word_index_list in index_list:
_word = ""
if "," in word_index_list:
word_index_list = word_index_list.split(",")
word_index_list = [int(x) for x in word_index_list]
else:
word_index_list = [int(word_index_list)]
for word_index in word_index_list:
_word += string_list[word_index]
_word_list.append(_word)
return _word_list
def get_complete_text_autohome(text):
_types_info = defaultdict(list)
types = re.findall('hs_kw(\d+_[^\'\"]+)', text)
for item in types:
idx, type = item.split("_")
_types_info[type].append(idx)
# 获取混淆字符个数
types = {type: len(set(value)) for type, value in _types_info.items()}
js_list = re.findall("<script>(\(function[\s\S]+?)\(document\);</script>", text.encode("utf8"))
type_charlist = {}
for js in js_list:
for _type in types:
if _type in js:
break
else:
continue
if not js:
continue
try:
char_list = get_char(js, types[_type])
except Exception as e:
traceback.print_exc()
continue
type_charlist.update({_type: char_list})
def char_replace(m):
index = int(m.group(1))
typ = m.group(2)
char_list = type_charlist.get(typ, [])
if not char_list:
return m.group()
char = char_list[index]
return char
text = re.sub("<span\s*class=[\'\"]hs_kw(\d+)_([^\'\"]+)[\'\"]></span>", char_replace, text)
return text
debug_flag = 1
if 0 or debug_flag:
# 论坛
url = "http://club.autohome.com.cn/bbs/thread-c-3788-62403429-1.html"
url = "http://club.autohome.com.cn/bbs/thread-c-2561-64686945-1.html"
headers = {"User-Agent": "Mozilla/6.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
resp = requests.get(url, headers=headers)
text = resp.content.decode("gbk")
text = get_complete_text_autohome(text)
data = re.search("<div\s*class=[\'\"]tz-paragraph[^\'\"]*?[\'\"]>([\s\S]+?)</div>", text).group(1)
if debug_flag:
if "hs_kw" not in data:
print("%s ok !!" % url)
if 0 or debug_flag:
# 口碑
url = "http://k.autohome.com.cn/spec/27507/view_1524661_1.html?st=2&piap=1|27507|0|0|1|0|0|0|0|0|1"
headers = {"User-Agent": "Mozilla/6.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
resp = requests.get(url, headers=headers)
text = resp.content.decode("gbk")
text = get_complete_text_autohome(text)
text = re.sub("<style[^>]+?>[\s\S]+?</style>", "", text)
text = re.sub("<script[^>]?>[\s\S]+?</script>", "", text)
data = re.search("<div\s*class=[\'\"]text-con[\'\"][^>]*>([\s\S]+?)</div>", text).group(1)
if debug_flag:
if "hs_kw" not in data:
print("%s ok !!" % url)
if 0 or debug_flag:
# 参数配置
url = "http://car.autohome.com.cn/config/spec/1001360.html"
url = "http://car.autohome.com.cn/config/spec/1646.html" # 混淆字符存在英文
headers = {"User-Agent": "Mozilla/6.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
resp = requests.get(url, headers=headers)
text = resp.content.decode("gbk")
text = get_complete_text_autohome(text)
data = re.search('var config = (.*?);\r', text, re.DOTALL).group(1)
if debug_flag:
if "hs_kw" not in data:
print("%s ok !!" % url)