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
| class CSRFSecurityTester { constructor(targetUrl) { this.targetUrl = targetUrl; this.testResults = []; this.attackVectors = []; }
async runFullTestSuite() { console.log('开始CSRF安全测试...'); await this.testBasicCSRF(); await this.testTokenBypass(); await this.testOriginBypass(); await this.testSameSiteBypass(); await this.testDoubleCookieBypass(); await this.testConcurrentAttacks(); return this.generateTestReport(); }
async testBasicCSRF() { console.log('测试基础CSRF漏洞...'); const testCases = [ { name: '无防护的POST请求', method: 'POST', endpoint: '/rest/user/update', payload: { email: '[email protected]' }, expected: 'should_be_blocked' }, { name: '无防护的PUT请求', method: 'PUT', endpoint: '/rest/user/update', payload: { newPassword: 'test123' }, expected: 'should_be_blocked' }, { name: '无防护的DELETE请求', method: 'DELETE', endpoint: '/rest/orders/1', payload: {}, expected: 'should_be_blocked' } ]; for (const testCase of testCases) { const result = await this.executeCSRFAttack(testCase); this.testResults.push({ test: testCase.name, status: result.success ? 'VULNERABLE' : 'PROTECTED', details: result }); } }
async testTokenBypass() { console.log('测试Token绕过技术...'); const bypassTechniques = [ { name: '空Token绕过', token: '', description: '提交空的CSRF Token' }, { name: '无效Token绕过', token: 'invalid_token_12345', description: '提交无效的CSRF Token' }, { name: '重复Token绕过', token: 'reused_token', description: '重复使用已消费的Token' }, { name: '过期Token绕过', token: 'expired_token', description: '使用已过期的Token' } ]; for (const technique of bypassTechniques) { const result = await this.testTokenBypassTechnique(technique); this.testResults.push({ test: technique.name, status: result.bypassed ? 'VULNERABLE' : 'PROTECTED', details: result }); } }
async testOriginBypass() { console.log('测试Origin/Referer绕过...'); const bypassTests = [ { name: '移除Origin头', headers: { origin: null }, description: '完全移除Origin头' }, { name: '伪造Origin头', headers: { origin: 'https://trusted-site.com' }, description: '伪造受信任的Origin' }, { name: '空Referer头', headers: { referer: '' }, description: '提交空的Referer' }, { name: '伪造Referer头', headers: { referer: 'https://trusted-site.com/page' }, description: '伪造受信任的Referer' } ]; for (const test of bypassTests) { const result = await this.testOriginBypassTechnique(test); this.testResults.push({ test: test.name, status: result.bypassed ? 'VULNERABLE' : 'PROTECTED', details: result }); } }
async testSameSiteBypass() { console.log('测试SameSite绕过技术...'); const sameSiteTests = [ { name: '子域攻击绕过', setup: 'subdomain_attack', description: '利用子域共享Cookie' }, { name: 'DNS重绑定绕过', setup: 'dns_rebinding', description: 'DNS重绑定攻击' }, { name: 'CORS预检绕过', setup: 'cors_preflight', description: '利用CORS预检机制' } ]; for (const test of sameSiteTests) { const result = await this.testSameSiteBypassTechnique(test); this.testResults.push({ test: test.name, status: result.bypassed ? 'VULNERABLE' : 'PROTECTED', details: result }); } }
async testConcurrentAttacks() { console.log('测试并发CSRF攻击...'); const concurrentRequests = 50; const attackPromises = []; for (let i = 0; i < concurrentRequests; i++) { attackPromises.push(this.executeCSRFAttack({ name: `并发攻击 ${i + 1}`, method: 'POST', endpoint: '/rest/user/update', payload: { email: `concurrent${i}@attack.com` }, expected: 'should_be_blocked' })); } const results = await Promise.allSettled(attackPromises); const successfulAttacks = results.filter(r => r.status === 'fulfilled' && r.value.success ).length; this.testResults.push({ test: '并发CSRF攻击', status: successfulAttacks > 0 ? 'VULNERABLE' : 'PROTECTED', details: { totalRequests: concurrentRequests, successfulAttacks, attackRate: (successfulAttacks / concurrentRequests * 100).toFixed(2) + '%' } }); }
async executeCSRFAttack(testCase) { try { const response = await fetch(`${this.targetUrl}${testCase.endpoint}`, { method: testCase.method, headers: { 'Content-Type': 'application/json', ...testCase.headers }, body: JSON.stringify(testCase.payload), credentials: 'include' }); return { success: response.ok, status: response.status, statusText: response.statusText, expected: testCase.expected }; } catch (error) { return { success: false, error: error.message, expected: testCase.expected }; } }
generateTestReport() { const vulnerable = this.testResults.filter(r => r.status === 'VULNERABLE').length; const protected = this.testResults.filter(r => r.status === 'PROTECTED').length; const total = this.testResults.length; return { summary: { total, vulnerable, protected, securityScore: ((protected / total) * 100).toFixed(2) + '%' }, details: this.testResults, recommendations: this.generateRecommendations() }; }
generateRecommendations() { const recommendations = []; const vulnerableTests = this.testResults.filter(r => r.status === 'VULNERABLE'); if (vulnerableTests.some(t => t.test.includes('基础CSRF'))) { recommendations.push('实施CSRF Token防护机制'); } if (vulnerableTests.some(t => t.test.includes('Token绕过'))) { recommendations.push('加强Token校验逻辑,确保Token的唯一性和时效性'); } if (vulnerableTests.some(t => t.test.includes('Origin绕过'))) { recommendations.push('实施严格的Origin/Referer白名单校验'); } if (vulnerableTests.some(t => t.test.includes('SameSite绕过'))) { recommendations.push('配置适当的SameSite策略,考虑使用双重Cookie提交'); } if (vulnerableTests.some(t => t.test.includes('并发攻击'))) { recommendations.push('实施请求频率限制和IP黑名单机制'); } return recommendations; } }
|