-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwafBypass.js
384 lines (322 loc) · 11 KB
/
wafBypass.js
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
class WAFBypassTechniques {
constructor() {
this.encodingMethods = {
base64: this.base64Encode.bind(this),
url: this.urlEncode.bind(this),
html: this.htmlEncode.bind(this),
js: this.jsEncode.bind(this),
unicode: this.unicodeEncode.bind(this),
hex: this.hexEncode.bind(this)
};
this.obfuscationMethods = {
string: this.obfuscateString.bind(this),
eval: this.obfuscateEval.bind(this),
concat: this.obfuscateConcat.bind(this),
template: this.obfuscateTemplate.bind(this)
};
this.wafSignatures = {
modSecurity: {
patterns: [/mod_security/, /NOYB/],
bypassTechniques: ['encoding', 'obfuscation', 'splitting']
},
cloudflare: {
patterns: [/cloudflare-nginx/, /cf-ray/],
bypassTechniques: ['encoding', 'obfuscation', 'splitting', 'chunked']
},
aws: {
patterns: [/x-amzn-RequestId/, /x-amz-cf-id/],
bypassTechniques: ['encoding', 'obfuscation', 'splitting', 'chunked']
},
akamai: {
patterns: [/AkamaiGHost/, /Akamai-Origin-Hop/],
bypassTechniques: ['encoding', 'obfuscation', 'splitting', 'chunked']
}
};
}
// 检测WAF类型
detectWAFType() {
const headers = this.getResponseHeaders();
for (const [name, waf] of Object.entries(this.wafSignatures)) {
for (const pattern of waf.patterns) {
if (this.checkHeaderPattern(headers, pattern)) {
return {
detected: true,
type: name,
bypassTechniques: waf.bypassTechniques
};
}
}
}
return { detected: false };
}
// 检查响应头模式
checkHeaderPattern(headers, pattern) {
for (const [name, value] of headers.entries()) {
if (pattern.test(value)) {
return true;
}
}
return false;
}
// 生成绕过变体
generateBypassVariants(payload, wafType) {
const variants = [];
const waf = this.wafSignatures[wafType];
if (!waf) {
return variants;
}
// 生成编码变体
if (waf.bypassTechniques.includes('encoding')) {
for (const [name, method] of Object.entries(this.encodingMethods)) {
variants.push({
type: 'encoding',
method: name,
payload: method(payload)
});
}
}
// 生成混淆变体
if (waf.bypassTechniques.includes('obfuscation')) {
for (const [name, method] of Object.entries(this.obfuscationMethods)) {
variants.push({
type: 'obfuscation',
method: name,
payload: method(payload)
});
}
}
// 生成分割变体
if (waf.bypassTechniques.includes('splitting')) {
variants.push(...this.generateSplitVariants(payload));
}
// 生成分块变体
if (waf.bypassTechniques.includes('chunked')) {
variants.push(...this.generateChunkedVariants(payload));
}
return variants;
}
// 生成分割变体
generateSplitVariants(payload) {
const variants = [];
// 字符串分割
variants.push({
type: 'splitting',
method: 'string',
payload: this.splitString(payload)
});
// 数组分割
variants.push({
type: 'splitting',
method: 'array',
payload: this.splitArray(payload)
});
// 对象分割
variants.push({
type: 'splitting',
method: 'object',
payload: this.splitObject(payload)
});
return variants;
}
// 生成分块变体
generateChunkedVariants(payload) {
const variants = [];
// 固定大小分块
variants.push({
type: 'chunked',
method: 'fixed',
payload: this.chunkFixed(payload)
});
// 动态大小分块
variants.push({
type: 'chunked',
method: 'dynamic',
payload: this.chunkDynamic(payload)
});
return variants;
}
// 字符串分割
splitString(payload) {
const chunks = [];
const chunkSize = 2;
for (let i = 0; i < payload.length; i += chunkSize) {
chunks.push(payload.slice(i, i + chunkSize));
}
return chunks.join('+');
}
// 数组分割
splitArray(payload) {
const chunks = [];
const chunkSize = 2;
for (let i = 0; i < payload.length; i += chunkSize) {
chunks.push(`"${payload.slice(i, i + chunkSize)}"`);
}
return `[${chunks.join(',')}].join('')`;
}
// 对象分割
splitObject(payload) {
const chunks = [];
const chunkSize = 2;
for (let i = 0; i < payload.length; i += chunkSize) {
chunks.push(`"${i}":"${payload.slice(i, i + chunkSize)}"`);
}
return `Object.values({${chunks.join(',')}}).join('')`;
}
// 固定大小分块
chunkFixed(payload) {
const chunks = [];
const chunkSize = 4;
for (let i = 0; i < payload.length; i += chunkSize) {
chunks.push(payload.slice(i, i + chunkSize));
}
return chunks.join('');
}
// 动态大小分块
chunkDynamic(payload) {
const chunks = [];
let currentSize = 2;
for (let i = 0; i < payload.length; i += currentSize) {
chunks.push(payload.slice(i, i + currentSize));
currentSize = (currentSize + 1) % 5 + 2;
}
return chunks.join('');
}
// 编码方法
base64Encode(payload) {
return btoa(payload);
}
urlEncode(payload) {
return encodeURIComponent(payload);
}
htmlEncode(payload) {
return payload
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/');
}
jsEncode(payload) {
return payload
.replace(/\\/g, '\\\\')
.replace(/'/g, '\\\'')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}
unicodeEncode(payload) {
return payload.split('').map(char =>
'\\u' + char.charCodeAt(0).toString(16).padStart(4, '0')
).join('');
}
hexEncode(payload) {
return payload.split('').map(char =>
'\\x' + char.charCodeAt(0).toString(16).padStart(2, '0')
).join('');
}
// 混淆方法
obfuscateString(payload) {
return payload.split('').map(char =>
`String.fromCharCode(${char.charCodeAt(0)})`
).join('+');
}
obfuscateEval(payload) {
return `eval(${this.obfuscateString(payload)})`;
}
obfuscateConcat(payload) {
return payload.split('').map(char =>
`"${char}"`
).join('+');
}
obfuscateTemplate(payload) {
return `\`${payload}\``;
}
// 验证绕过效果
validateBypass(payload, wafType) {
const validation = {
success: false,
evidence: [],
errors: []
};
try {
// 检查基本语法
if (payload.includes('<script>')) {
validation.evidence.push('Script tag detected');
}
// 检查事件处理器
if (payload.match(/on\w+\s*=/i)) {
validation.evidence.push('Event handler detected');
}
// 检查编码
if (payload.includes('\\u') || payload.includes('\\x')) {
validation.evidence.push('Encoded characters detected');
}
// 检查混淆
if (payload.includes('eval(') || payload.includes('Function(')) {
validation.evidence.push('Obfuscated code detected');
}
// 检查分割
if (payload.includes('+') || payload.includes('join(')) {
validation.evidence.push('Split payload detected');
}
// 检查分块
if (payload.includes('chunk') || payload.includes('slice')) {
validation.evidence.push('Chunked payload detected');
}
validation.success = true;
} catch (error) {
validation.errors.push(`Validation error: ${error.message}`);
}
return validation;
}
// 生成绕过报告
generateBypassReport(payload, wafType, validation) {
return {
timestamp: new Date().toISOString(),
payload: payload,
wafType: wafType,
validation: validation,
variants: this.generateBypassVariants(payload, wafType),
recommendations: this.generateBypassRecommendations(payload, wafType, validation)
};
}
// 生成绕过建议
generateBypassRecommendations(payload, wafType, validation) {
const recommendations = [];
// 基于验证结果的建议
if (!validation.success) {
recommendations.push({
type: 'error',
description: 'Fix payload validation errors',
priority: 'high'
});
}
// 基于WAF类型的建议
if (wafType) {
const waf = this.wafSignatures[wafType];
recommendations.push({
type: 'waf',
description: `Use appropriate bypass techniques for ${wafType}`,
priority: 'high'
});
}
// 基于证据的建议
for (const evidence of validation.evidence) {
recommendations.push({
type: 'evidence',
description: `Address evidence: ${evidence}`,
priority: 'medium'
});
}
return recommendations;
}
// 获取响应头
getResponseHeaders() {
// 这里需要实现获取响应头的逻辑
// 由于浏览器安全限制,可能需要通过其他方式获取
return new Headers();
}
}
export const wafBypassTechniques = new WAFBypassTechniques();