-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
465 lines (411 loc) · 14.6 KB
/
validator.js
File metadata and controls
465 lines (411 loc) · 14.6 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Token Validator - OFJAAAH Hardcoded Token Detector
// Valida tokens críticos para alertas de segurança em ambientes autorizados
const TOKEN_VALIDATORS = {
// Firebase API Key Validation
FIREBASE: async (token) => {
try {
// Tentar fazer uma requisição simples à API do Firebase
const response = await fetch(`https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${token}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ returnSecureToken: true })
});
const data = await response.json();
// Se retornar erro específico de API key inválida
if (data.error && data.error.message === 'API key not valid') {
return { valid: false, status: 'Token inválido ou expirado' };
}
// Se retornar qualquer outra resposta, a API key é válida
if (response.status === 400 && data.error && data.error.message.includes('MISSING')) {
return { valid: true, status: 'Token válido e ativo', severity: 'CRITICAL' };
}
return { valid: true, status: 'Token válido', severity: 'CRITICAL' };
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// GitHub Token Validation
GITHUB: async (token) => {
try {
const response = await fetch('https://api.github.com/user', {
headers: {
'Authorization': `token ${token}`,
'User-Agent': 'Security-Monitor'
}
});
if (response.status === 200) {
const data = await response.json();
return {
valid: true,
status: `Token válido - Usuário: ${data.login}`,
severity: 'CRITICAL',
metadata: { username: data.login, email: data.email }
};
} else if (response.status === 401) {
return { valid: false, status: 'Token inválido ou expirado' };
} else {
return { valid: null, status: `Status HTTP: ${response.status}` };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// GitLab Token Validation
GITLAB: async (token) => {
try {
const response = await fetch('https://gitlab.com/api/v4/user', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.status === 200) {
const data = await response.json();
return {
valid: true,
status: `Token válido - Usuário: ${data.username}`,
severity: 'CRITICAL',
metadata: { username: data.username, email: data.email }
};
} else if (response.status === 401) {
return { valid: false, status: 'Token inválido ou expirado' };
} else {
return { valid: null, status: `Status HTTP: ${response.status}` };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// Vercel Token Validation (Expandida para Bug Bounty)
VERCEL: async (token) => {
try {
// Testar endpoint /v2/user
const userResponse = await fetch('https://api.vercel.com/v2/user', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (userResponse.status === 200) {
const userData = await userResponse.json();
// Testar permissões adicionais
const teamsResponse = await fetch('https://api.vercel.com/v2/teams', {
headers: { 'Authorization': `Bearer ${token}` }
});
const projectsResponse = await fetch('https://api.vercel.com/v9/projects', {
headers: { 'Authorization': `Bearer ${token}` }
});
const teams = teamsResponse.ok ? await teamsResponse.json() : null;
const projects = projectsResponse.ok ? await projectsResponse.json() : null;
return {
valid: true,
status: `Token VERCEL válido - Usuário: ${userData.user.username || userData.user.email}`,
severity: 'CRITICAL',
metadata: {
username: userData.user.username,
email: userData.user.email,
teams: teams?.teams?.length || 0,
projects: projects?.projects?.length || 0,
scope: 'Full API Access'
}
};
} else if (userResponse.status === 403 || userResponse.status === 401) {
return { valid: false, status: 'Token inválido ou expirado' };
} else {
return { valid: null, status: `Status HTTP: ${userResponse.status}` };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// Supabase Token Validation (Expandida - Bug Bounty)
SUPABASE: async (token, projectUrl = null) => {
try {
// Supabase API keys são JWTs
if (token.startsWith('eyJ') && token.includes('.')) {
// Decodificar JWT
try {
const payload = JSON.parse(atob(token.split('.')[1]));
const now = Math.floor(Date.now() / 1000);
// Determinar tipo de key
const role = payload.role || 'unknown';
const isServiceRole = role === 'service_role';
const isAnonKey = role === 'anon';
// Verificar expiração
if (payload.exp && payload.exp < now) {
return { valid: false, status: 'JWT Supabase expirado' };
}
// Se temos URL do projeto, testar acesso real
if (projectUrl || payload.iss) {
const baseUrl = projectUrl || payload.iss;
try {
// Testar endpoint REST
const testResponse = await fetch(`${baseUrl}/rest/v1/`, {
headers: {
'apikey': token,
'Authorization': `Bearer ${token}`
}
});
// Testar permissões de escrita (apenas para service_role)
let writeAccess = false;
if (isServiceRole) {
try {
const writeTest = await fetch(`${baseUrl}/rest/v1/rpc/`, {
method: 'POST',
headers: {
'apikey': token,
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
writeAccess = writeTest.status !== 401 && writeTest.status !== 403;
} catch (e) {
// Ignorar erro
}
}
return {
valid: true,
status: `SUPABASE ${role.toUpperCase()} Key válida - Projeto: ${baseUrl}`,
severity: isServiceRole ? 'CRITICAL' : 'HIGH',
metadata: {
role: role,
projectUrl: baseUrl,
expires: payload.exp ? new Date(payload.exp * 1000).toISOString() : 'never',
writeAccess: isServiceRole ? writeAccess : 'N/A',
issuer: payload.iss
}
};
} catch (fetchError) {
// Key é válida mas não conseguimos testar acesso
return {
valid: true,
status: `SUPABASE ${role.toUpperCase()} Key válida (formato JWT correto)`,
severity: isServiceRole ? 'CRITICAL' : 'HIGH',
metadata: {
role: role,
expires: payload.exp ? new Date(payload.exp * 1000).toISOString() : 'never',
note: 'Não foi possível testar acesso real'
}
};
}
}
// Sem URL, apenas validar JWT
return {
valid: true,
status: `SUPABASE ${role.toUpperCase()} Key válida (JWT não expirado)`,
severity: isServiceRole ? 'CRITICAL' : 'HIGH',
metadata: {
role: role,
expires: payload.exp ? new Date(payload.exp * 1000).toISOString() : 'never',
note: 'URL do projeto não fornecida - validação parcial'
}
};
} catch (decodeError) {
return { valid: false, status: 'Formato de JWT Supabase inválido' };
}
}
return { valid: null, status: 'Token não parece ser uma Supabase key válida' };
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// AWS Credentials Validation
AWS: async (token) => {
// AWS requer access key ID + secret, não podemos validar apenas com um
return {
valid: null,
status: 'Validação AWS requer Access Key ID + Secret Access Key',
severity: 'CRITICAL'
};
},
// Slack Token Validation
SLACK: async (token) => {
try {
const response = await fetch('https://slack.com/api/auth.test', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const data = await response.json();
if (data.ok) {
return {
valid: true,
status: `Token válido - Team: ${data.team}`,
severity: 'HIGH',
metadata: { user: data.user, team: data.team }
};
} else {
return { valid: false, status: data.error || 'Token inválido' };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// Stripe Key Validation
STRIPE: async (token) => {
try {
// Tentar listar customers (operação read-only)
const response = await fetch('https://api.stripe.com/v1/customers?limit=1', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.status === 200) {
return {
valid: true,
status: 'Token Stripe válido (acesso à conta)',
severity: 'CRITICAL'
};
} else if (response.status === 401) {
return { valid: false, status: 'Token inválido ou expirado' };
} else {
return { valid: null, status: `Status HTTP: ${response.status}` };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// Google API Key Validation
GOOGLE: async (token) => {
try {
// Tentar uma API pública do Google
const response = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=test&key=${token}&maxResults=1`);
const data = await response.json();
if (response.status === 200) {
return {
valid: true,
status: 'Google API Key válida',
severity: 'HIGH'
};
} else if (data.error && data.error.message.includes('API key not valid')) {
return { valid: false, status: 'API Key inválida' };
} else if (data.error && data.error.message.includes('has not been used')) {
return {
valid: true,
status: 'API Key válida (não foi usada ainda)',
severity: 'HIGH'
};
} else {
return { valid: null, status: data.error?.message || 'Erro ao validar' };
}
} catch (error) {
return { valid: null, status: 'Erro ao validar: ' + error.message };
}
},
// JWT Token Validation (genérico)
JWT: async (token) => {
try {
const parts = token.split('.');
if (parts.length !== 3) {
return { valid: false, status: 'Formato JWT inválido' };
}
const payload = JSON.parse(atob(parts[1]));
const now = Math.floor(Date.now() / 1000);
if (payload.exp) {
if (payload.exp > now) {
return {
valid: true,
status: 'JWT válido e não expirado',
severity: 'MEDIUM',
metadata: {
expires: new Date(payload.exp * 1000).toISOString(),
issuer: payload.iss,
subject: payload.sub
}
};
} else {
return { valid: false, status: 'JWT expirado' };
}
} else {
return {
valid: null,
status: 'JWT sem data de expiração (verificar manualmente)',
severity: 'MEDIUM'
};
}
} catch (error) {
return { valid: false, status: 'Erro ao decodificar JWT: ' + error.message };
}
}
};
// Verificar se valor parece ser um falso positivo antes de validar
function isLikelyFalsePositive(value) {
// Verificar se é apenas palavras comuns separadas por underscore
if (/^[a-z]+(_[a-z]+){2,}$/.test(value)) {
return true;
}
// Se não tem maiúsculas nem números, provavelmente é falso positivo
const hasUpperCase = /[A-Z]/.test(value);
const hasNumbers = /[0-9]/.test(value);
if (!hasUpperCase && !hasNumbers && value.length < 40) {
return true;
}
return false;
}
// Função principal de validação
async function validateToken(type, value) {
console.log(`🔍 Validando token do tipo: ${type}`);
// Pre-validação: detectar falsos positivos antes de fazer requisições
if (isLikelyFalsePositive(value)) {
console.log(`⚠️ Token parece ser um falso positivo: ${value}`);
return {
valid: false,
status: 'Provável falso positivo (nome de variável ou feature flag)',
severity: 'LOW'
};
}
// Mapear tipos para validadores
const validatorMap = {
'FIREBASE': 'FIREBASE',
'GITHUB': 'GITHUB',
'GITLAB': 'GITLAB',
'VERCEL': 'VERCEL',
'SUPABASE': 'SUPABASE',
'AWS': 'AWS',
'SLACK': 'SLACK',
'STRIPE': 'STRIPE',
'GOOGLE': 'GOOGLE',
'JWT': 'JWT',
'API_KEY': null, // Genérico, não validamos
'TOKEN': null,
'SECRET': null,
'PASSWORD': null,
'PRIVATE_KEY': null
};
const validatorType = validatorMap[type];
if (!validatorType || !TOKEN_VALIDATORS[validatorType]) {
return {
valid: null,
status: 'Validação não disponível para este tipo',
severity: 'MEDIUM'
};
}
try {
const result = await TOKEN_VALIDATORS[validatorType](value);
console.log(`✅ Resultado da validação:`, result);
return result;
} catch (error) {
console.error(`❌ Erro ao validar token:`, error);
return {
valid: null,
status: 'Erro durante validação: ' + error.message
};
}
}
// Validar múltiplos tokens em lote
async function validateTokensBatch(tokens) {
const results = [];
for (const token of tokens) {
const validation = await validateToken(token.type, token.value);
results.push({
...token,
validation
});
// Delay pequeno entre requisições para evitar rate limiting
await new Promise(resolve => setTimeout(resolve, 500));
}
return results;
}
// Exportar para uso em outros scripts (ES6 module)
export { validateToken, validateTokensBatch };