File size: 14,947 Bytes
4c1e4ec |
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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
/**
* Sistema de Segurança Avançado
* Implementa múltiplas camadas de proteção e monitoramento
*/
interface SecurityConfig {
enableCSP: boolean;
enableXSSProtection: boolean;
enableRateLimiting: boolean;
enableSecurityHeaders: boolean;
enableIntegrityChecks: boolean;
}
interface SecurityThreat {
type: string;
severity: 'low' | 'medium' | 'high' | 'critical';
details: any;
timestamp: number;
mitigated: boolean;
}
export class SecurityEnhanced {
private static instance: SecurityEnhanced;
private config: SecurityConfig;
private threats: SecurityThreat[] = [];
private isEnabled = true;
private constructor() {
this.config = {
enableCSP: true,
enableXSSProtection: true,
enableRateLimiting: true,
enableSecurityHeaders: true,
enableIntegrityChecks: true
};
}
static getInstance(): SecurityEnhanced {
if (!SecurityEnhanced.instance) {
SecurityEnhanced.instance = new SecurityEnhanced();
}
return SecurityEnhanced.instance;
}
/**
* Inicializar todas as proteções de segurança
*/
initialize(): void {
if (!this.isEnabled) return;
console.log('🛡️ Inicializando sistema de segurança avançado...');
this.setupContentSecurityPolicy();
this.setupSecurityHeaders();
this.setupXSSProtection();
this.setupIntegrityChecks();
this.setupThreatMonitoring();
this.setupSecureStorage();
}
private setupContentSecurityPolicy(): void {
if (!this.config.enableCSP) return;
const csp = {
'default-src': ["'self'"],
'script-src': [
"'self'",
"'unsafe-inline'", // Necessário para React em desenvolvimento
"'unsafe-eval'", // Necessário para desenvolvimento
'https://servicebus2.caixa.gov.br',
'https://brasilapi.com.br'
],
'style-src': [
"'self'",
"'unsafe-inline'", // Necessário para Tailwind CSS
'https://fonts.googleapis.com'
],
'font-src': [
"'self'",
'https://fonts.gstatic.com'
],
'connect-src': [
"'self'",
'https://servicebus2.caixa.gov.br',
'https://brasilapi.com.br',
'https://api.loterias-caixa.com'
],
'img-src': [
"'self'",
'data:',
'https:'
],
'frame-ancestors': ["'none'"],
'base-uri': ["'self'"],
'form-action': ["'self'"]
};
const cspString = Object.entries(csp)
.map(([directive, sources]) => `${directive} ${sources.join(' ')}`)
.join('; ');
// Em produção, seria configurado no servidor
if (process.env.NODE_ENV === 'development') {
console.log('🛡️ CSP configurado:', cspString);
}
}
private setupSecurityHeaders(): void {
if (!this.config.enableSecurityHeaders) return;
// Headers que seriam configurados no servidor
const securityHeaders = {
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'Permissions-Policy': 'camera=(), microphone=(), geolocation=()',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
};
console.log('🛡️ Security headers configurados:', securityHeaders);
}
private setupXSSProtection(): void {
if (!this.config.enableXSSProtection) return;
// Monitorar tentativas de XSS
this.monitorDOMChanges();
this.validateUserInputs();
this.sanitizeURLParameters();
}
private monitorDOMChanges(): void {
if (typeof window === 'undefined') return;
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element;
// Verificar scripts suspeitos
if (element.tagName === 'SCRIPT') {
const src = element.getAttribute('src');
const inline = element.textContent;
if (src && !this.isAllowedScriptSource(src)) {
this.recordThreat('unauthorized-script', 'high', { src });
element.remove();
}
if (inline && this.containsSuspiciousCode(inline)) {
this.recordThreat('suspicious-inline-script', 'high', { code: inline.substring(0, 100) });
element.remove();
}
}
// Verificar iframes suspeitos
if (element.tagName === 'IFRAME') {
const src = element.getAttribute('src');
if (src && !this.isAllowedFrameSource(src)) {
this.recordThreat('unauthorized-iframe', 'high', { src });
element.remove();
}
}
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
console.log('🛡️ Monitoramento de DOM ativo para XSS');
}
private validateUserInputs(): void {
// Interceptar inputs do usuário
document.addEventListener('input', (event) => {
const target = event.target as HTMLInputElement;
if (target && target.value) {
const sanitized = this.sanitizeInput(target.value);
if (sanitized !== target.value) {
this.recordThreat('xss-attempt', 'medium', {
original: target.value.substring(0, 100),
sanitized: sanitized.substring(0, 100)
});
target.value = sanitized;
}
}
});
console.log('🛡️ Validação de inputs ativa');
}
private sanitizeURLParameters(): void {
if (typeof window === 'undefined') return;
const params = new URLSearchParams(window.location.search);
let hasThreats = false;
params.forEach((value, key) => {
const sanitized = this.sanitizeInput(value);
if (sanitized !== value) {
this.recordThreat('url-xss-attempt', 'medium', {
parameter: key,
original: value.substring(0, 100)
});
params.set(key, sanitized);
hasThreats = true;
}
});
if (hasThreats) {
const newUrl = `${window.location.pathname}?${params.toString()}`;
window.history.replaceState({}, '', newUrl);
console.log('🛡️ Parâmetros URL sanitizados');
}
}
private setupIntegrityChecks(): void {
if (!this.config.enableIntegrityChecks) return;
// Verificar integridade de recursos externos
const externalScripts = document.querySelectorAll('script[src]');
const externalStyles = document.querySelectorAll('link[rel="stylesheet"][href]');
Array.from(externalScripts).concat(Array.from(externalStyles)).forEach(element => {
if (!element.hasAttribute('integrity')) {
const src = element.getAttribute('src') || element.getAttribute('href');
if (src && this.isExternalResource(src)) {
this.recordThreat('missing-integrity', 'low', { src });
}
}
});
console.log('🛡️ Verificação de integridade ativa');
}
private setupThreatMonitoring(): void {
// Monitorar atividades suspeitas
this.monitorConsoleAccess();
this.monitorDevToolsUsage();
this.monitorSuspiciousNetworkActivity();
}
private monitorConsoleAccess(): void {
// Detectar tentativas de acesso ao console em produção
if (process.env.NODE_ENV === 'production') {
const originalConsole = { ...console };
['log', 'warn', 'error', 'debug'].forEach(method => {
(console as any)[method] = (...args: any[]) => {
// Em produção, log suspeito
if (args.some(arg =>
typeof arg === 'string' &&
(arg.includes('eval') || arg.includes('Function') || arg.includes('script'))
)) {
this.recordThreat('suspicious-console-access', 'low', { args: args.slice(0, 3) });
}
return (originalConsole as any)[method](...args);
};
});
}
}
private monitorDevToolsUsage(): void {
// Detectar abertura do DevTools
let devtools = { open: false };
setInterval(() => {
const start = performance.now();
debugger; // eslint-disable-line no-debugger
const end = performance.now();
if (end - start > 100) { // DevTools likely open
if (!devtools.open) {
devtools.open = true;
this.recordThreat('devtools-opened', 'low', { timestamp: Date.now() });
}
} else {
devtools.open = false;
}
}, 1000);
}
private monitorSuspiciousNetworkActivity(): void {
// Interceptar fetch para monitorar requisições suspeitas
const originalFetch = window.fetch;
window.fetch = async (...args) => {
const url = args[0].toString();
// Verificar URLs suspeitas
if (this.isSuspiciousURL(url)) {
this.recordThreat('suspicious-network-request', 'medium', { url });
throw new Error('Requisição bloqueada por segurança');
}
return originalFetch(...args);
};
}
private setupSecureStorage(): void {
// Implementar armazenamento seguro
const originalSetItem = localStorage.setItem;
const originalGetItem = localStorage.getItem;
localStorage.setItem = (key: string, value: string) => {
// Criptografar dados sensíveis
if (this.isSensitiveData(key)) {
value = this.encryptData(value);
}
return originalSetItem.call(localStorage, key, value);
};
localStorage.getItem = (key: string) => {
const value = originalGetItem.call(localStorage, key);
if (value && this.isSensitiveData(key)) {
return this.decryptData(value);
}
return value;
};
console.log('🛡️ Armazenamento seguro configurado');
}
private sanitizeInput(input: string): string {
return input
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
.replace(/javascript:/gi, '')
.replace(/on\w+=/gi, '')
.replace(/eval\(/gi, '')
.replace(/Function\(/gi, '')
.replace(/setTimeout\(/gi, '')
.replace(/setInterval\(/gi, '');
}
private containsSuspiciousCode(code: string): boolean {
const suspiciousPatterns = [
/eval\s*\(/,
/Function\s*\(/,
/document\.write/,
/innerHTML\s*=/,
/outerHTML\s*=/,
/javascript:/,
/data:text\/html/,
/execScript/,
/setInterval.*eval/,
/setTimeout.*eval/
];
return suspiciousPatterns.some(pattern => pattern.test(code));
}
private isAllowedScriptSource(src: string): boolean {
const allowedDomains = [
window.location.origin,
'https://servicebus2.caixa.gov.br',
'https://brasilapi.com.br',
'https://fonts.googleapis.com',
'https://fonts.gstatic.com'
];
return allowedDomains.some(domain => src.startsWith(domain));
}
private isAllowedFrameSource(src: string): boolean {
// Por padrão, não permitir iframes externos
return src.startsWith(window.location.origin);
}
private isExternalResource(src: string): boolean {
return !src.startsWith(window.location.origin) &&
(src.startsWith('http://') || src.startsWith('https://'));
}
private isSuspiciousURL(url: string): boolean {
const suspiciousPatterns = [
/bit\.ly/,
/tinyurl/,
/malware/,
/phishing/,
/\.tk$/,
/\.ml$/
];
return suspiciousPatterns.some(pattern => pattern.test(url));
}
private isSensitiveData(key: string): boolean {
const sensitiveKeys = ['token', 'password', 'secret', 'key', 'auth'];
return sensitiveKeys.some(sensitive => key.toLowerCase().includes(sensitive));
}
private encryptData(data: string): string {
// Implementação simples de encoding (em produção, usar crypto real)
return btoa(encodeURIComponent(data));
}
private decryptData(data: string): string {
try {
return decodeURIComponent(atob(data));
} catch {
return data; // Retornar original se não conseguir decodificar
}
}
private recordThreat(type: string, severity: SecurityThreat['severity'], details: any): void {
const threat: SecurityThreat = {
type,
severity,
details,
timestamp: Date.now(),
mitigated: false
};
this.threats.push(threat);
// Log baseado na severidade
const emoji = severity === 'critical' ? '🚨' : severity === 'high' ? '⚠️' : '⚡';
console.warn(`${emoji} Ameaça ${severity}: ${type}`, details);
// Auto-mitigar ameaças conhecidas
this.mitigateThreat(threat);
}
private mitigateThreat(threat: SecurityThreat): void {
switch (threat.type) {
case 'unauthorized-script':
case 'unauthorized-iframe':
// Já removido no monitoramento
threat.mitigated = true;
break;
case 'xss-attempt':
case 'url-xss-attempt':
// Já sanitizado
threat.mitigated = true;
break;
default:
// Log para investigação manual
break;
}
}
/**
* Obter relatório de segurança
*/
getSecurityReport(): {
threatsDetected: number;
threatsMitigated: number;
recentThreats: SecurityThreat[];
recommendations: string[];
} {
const recent = this.threats.filter(t => Date.now() - t.timestamp < 24 * 60 * 60 * 1000); // Last 24h
const mitigated = this.threats.filter(t => t.mitigated).length;
return {
threatsDetected: this.threats.length,
threatsMitigated: mitigated,
recentThreats: recent,
recommendations: this.generateSecurityRecommendations()
};
}
private generateSecurityRecommendations(): string[] {
const recommendations = [];
if (this.threats.some(t => t.type.includes('xss'))) {
recommendations.push('🛡️ Implementar sanitização mais rigorosa de inputs');
}
if (this.threats.some(t => t.type.includes('script'))) {
recommendations.push('🔒 Revisar Content Security Policy');
}
if (this.threats.some(t => t.severity === 'high' || t.severity === 'critical')) {
recommendations.push('🚨 Revisar logs de segurança imediatamente');
}
if (recommendations.length === 0) {
recommendations.push('✅ Sistema seguro - nenhuma ameaça significativa detectada');
}
return recommendations;
}
/**
* Limpar dados de segurança
*/
cleanup(): void {
this.threats = [];
console.log('🧹 Dados de segurança limpos');
}
/**
* Ativar/desativar sistema de segurança
*/
setEnabled(enabled: boolean): void {
this.isEnabled = enabled;
if (enabled) {
this.initialize();
}
}
}
// Singleton instance
export const securityEnhanced = SecurityEnhanced.getInstance();
|