beddel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +297 -0
- package/dist/agents/agentRegistry.d.ts +68 -0
- package/dist/agents/agentRegistry.d.ts.map +1 -0
- package/dist/agents/agentRegistry.js +222 -0
- package/dist/agents/agentRegistry.js.map +1 -0
- package/dist/agents/formatter-agent.d.ts +10 -0
- package/dist/agents/formatter-agent.d.ts.map +1 -0
- package/dist/agents/formatter-agent.js +49 -0
- package/dist/agents/formatter-agent.js.map +1 -0
- package/dist/agents/genkit-agent.d.ts +12 -0
- package/dist/agents/genkit-agent.d.ts.map +1 -0
- package/dist/agents/genkit-agent.js +119 -0
- package/dist/agents/genkit-agent.js.map +1 -0
- package/dist/agents/i18n-messages.d.ts +17 -0
- package/dist/agents/i18n-messages.d.ts.map +1 -0
- package/dist/agents/i18n-messages.js +92 -0
- package/dist/agents/i18n-messages.js.map +1 -0
- package/dist/agents/index.d.ts +10 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +26 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/pipeline.d.ts +15 -0
- package/dist/agents/pipeline.d.ts.map +1 -0
- package/dist/agents/pipeline.js +45 -0
- package/dist/agents/pipeline.js.map +1 -0
- package/dist/agents/schema-factory.d.ts +40 -0
- package/dist/agents/schema-factory.d.ts.map +1 -0
- package/dist/agents/schema-factory.js +121 -0
- package/dist/agents/schema-factory.js.map +1 -0
- package/dist/agents/translation-validators.d.ts +26 -0
- package/dist/agents/translation-validators.d.ts.map +1 -0
- package/dist/agents/translation-validators.js +77 -0
- package/dist/agents/translation-validators.js.map +1 -0
- package/dist/agents/translator-agents.d.ts +184 -0
- package/dist/agents/translator-agents.d.ts.map +1 -0
- package/dist/agents/translator-agents.js +613 -0
- package/dist/agents/translator-agents.js.map +1 -0
- package/dist/agents/types/translation.types.d.ts +100 -0
- package/dist/agents/types/translation.types.d.ts.map +1 -0
- package/dist/agents/types/translation.types.js +3 -0
- package/dist/agents/types/translation.types.js.map +1 -0
- package/dist/agents/validator-agent.d.ts +42 -0
- package/dist/agents/validator-agent.d.ts.map +1 -0
- package/dist/agents/validator-agent.js +122 -0
- package/dist/agents/validator-agent.js.map +1 -0
- package/dist/audit/auditTrail.d.ts +55 -0
- package/dist/audit/auditTrail.d.ts.map +1 -0
- package/dist/audit/auditTrail.js +93 -0
- package/dist/audit/auditTrail.js.map +1 -0
- package/dist/compliance/gdprEngine.d.ts +44 -0
- package/dist/compliance/gdprEngine.d.ts.map +1 -0
- package/dist/compliance/gdprEngine.js +178 -0
- package/dist/compliance/gdprEngine.js.map +1 -0
- package/dist/compliance/lgpdEngine.d.ts +51 -0
- package/dist/compliance/lgpdEngine.d.ts.map +1 -0
- package/dist/compliance/lgpdEngine.js +221 -0
- package/dist/compliance/lgpdEngine.js.map +1 -0
- package/dist/config.d.ts +78 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +77 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +40 -0
- package/dist/errors.js.map +1 -0
- package/dist/firebase/tenantManager.d.ts +84 -0
- package/dist/firebase/tenantManager.d.ts.map +1 -0
- package/dist/firebase/tenantManager.js +378 -0
- package/dist/firebase/tenantManager.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +118 -0
- package/dist/index.js.map +1 -0
- package/dist/integration/secure-yaml-runtime.d.ts +68 -0
- package/dist/integration/secure-yaml-runtime.d.ts.map +1 -0
- package/dist/integration/secure-yaml-runtime.js +245 -0
- package/dist/integration/secure-yaml-runtime.js.map +1 -0
- package/dist/parser/secure-yaml-parser.d.ts +62 -0
- package/dist/parser/secure-yaml-parser.d.ts.map +1 -0
- package/dist/parser/secure-yaml-parser.js +234 -0
- package/dist/parser/secure-yaml-parser.js.map +1 -0
- package/dist/performance/autoscaling.d.ts +100 -0
- package/dist/performance/autoscaling.d.ts.map +1 -0
- package/dist/performance/autoscaling.js +339 -0
- package/dist/performance/autoscaling.js.map +1 -0
- package/dist/performance/benchmark.d.ts +104 -0
- package/dist/performance/benchmark.d.ts.map +1 -0
- package/dist/performance/benchmark.js +514 -0
- package/dist/performance/benchmark.js.map +1 -0
- package/dist/performance/index.d.ts +14 -0
- package/dist/performance/index.d.ts.map +1 -0
- package/dist/performance/index.js +35 -0
- package/dist/performance/index.js.map +1 -0
- package/dist/performance/monitor.d.ts +126 -0
- package/dist/performance/monitor.d.ts.map +1 -0
- package/dist/performance/monitor.js +324 -0
- package/dist/performance/monitor.js.map +1 -0
- package/dist/performance/streaming.d.ts +82 -0
- package/dist/performance/streaming.d.ts.map +1 -0
- package/dist/performance/streaming.js +287 -0
- package/dist/performance/streaming.js.map +1 -0
- package/dist/runtime/audit.d.ts +240 -0
- package/dist/runtime/audit.d.ts.map +1 -0
- package/dist/runtime/audit.js +641 -0
- package/dist/runtime/audit.js.map +1 -0
- package/dist/runtime/declarativeAgentRuntime.d.ts +123 -0
- package/dist/runtime/declarativeAgentRuntime.d.ts.map +1 -0
- package/dist/runtime/declarativeAgentRuntime.js +576 -0
- package/dist/runtime/declarativeAgentRuntime.js.map +1 -0
- package/dist/runtime/isolatedRuntime.d.ts +119 -0
- package/dist/runtime/isolatedRuntime.d.ts.map +1 -0
- package/dist/runtime/isolatedRuntime.js +425 -0
- package/dist/runtime/isolatedRuntime.js.map +1 -0
- package/dist/runtime/schemaCompiler.d.ts +35 -0
- package/dist/runtime/schemaCompiler.d.ts.map +1 -0
- package/dist/runtime/schemaCompiler.js +151 -0
- package/dist/runtime/schemaCompiler.js.map +1 -0
- package/dist/runtime/simpleRuntime.d.ts +57 -0
- package/dist/runtime/simpleRuntime.d.ts.map +1 -0
- package/dist/runtime/simpleRuntime.js +187 -0
- package/dist/runtime/simpleRuntime.js.map +1 -0
- package/dist/security/dashboard.d.ts +89 -0
- package/dist/security/dashboard.d.ts.map +1 -0
- package/dist/security/dashboard.js +300 -0
- package/dist/security/dashboard.js.map +1 -0
- package/dist/security/hardening.d.ts +130 -0
- package/dist/security/hardening.d.ts.map +1 -0
- package/dist/security/hardening.js +414 -0
- package/dist/security/hardening.js.map +1 -0
- package/dist/security/index.d.ts +128 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +353 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/monitor.d.ts +88 -0
- package/dist/security/monitor.d.ts.map +1 -0
- package/dist/security/monitor.js +356 -0
- package/dist/security/monitor.js.map +1 -0
- package/dist/security/scanner.d.ts +104 -0
- package/dist/security/scanner.d.ts.map +1 -0
- package/dist/security/scanner.js +298 -0
- package/dist/security/scanner.js.map +1 -0
- package/dist/security/score.d.ts +150 -0
- package/dist/security/score.d.ts.map +1 -0
- package/dist/security/score.js +983 -0
- package/dist/security/score.js.map +1 -0
- package/dist/security/test-security.d.ts +22 -0
- package/dist/security/test-security.d.ts.map +1 -0
- package/dist/security/test-security.js +154 -0
- package/dist/security/test-security.js.map +1 -0
- package/dist/security/threatDetector.d.ts +39 -0
- package/dist/security/threatDetector.d.ts.map +1 -0
- package/dist/security/threatDetector.js +354 -0
- package/dist/security/threatDetector.js.map +1 -0
- package/dist/security/validation.d.ts +69 -0
- package/dist/security/validation.d.ts.map +1 -0
- package/dist/security/validation.js +286 -0
- package/dist/security/validation.js.map +1 -0
- package/dist/server/api/clientsRoute.d.ts +9 -0
- package/dist/server/api/clientsRoute.d.ts.map +1 -0
- package/dist/server/api/clientsRoute.js +71 -0
- package/dist/server/api/clientsRoute.js.map +1 -0
- package/dist/server/api/endpointsRoute.d.ts +8 -0
- package/dist/server/api/endpointsRoute.d.ts.map +1 -0
- package/dist/server/api/endpointsRoute.js +76 -0
- package/dist/server/api/endpointsRoute.js.map +1 -0
- package/dist/server/api/graphql.d.ts +9 -0
- package/dist/server/api/graphql.d.ts.map +1 -0
- package/dist/server/api/graphql.js +180 -0
- package/dist/server/api/graphql.js.map +1 -0
- package/dist/server/errors.d.ts +19 -0
- package/dist/server/errors.d.ts.map +1 -0
- package/dist/server/errors.js +42 -0
- package/dist/server/errors.js.map +1 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +24 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/kvStore.d.ts +27 -0
- package/dist/server/kvStore.d.ts.map +1 -0
- package/dist/server/kvStore.js +128 -0
- package/dist/server/kvStore.js.map +1 -0
- package/dist/server/runtimeSecurity.d.ts +28 -0
- package/dist/server/runtimeSecurity.d.ts.map +1 -0
- package/dist/server/runtimeSecurity.js +85 -0
- package/dist/server/runtimeSecurity.js.map +1 -0
- package/dist/server/types.d.ts +53 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/types.js +8 -0
- package/dist/server/types.js.map +1 -0
- package/dist/types/executionContext.d.ts +16 -0
- package/dist/types/executionContext.d.ts.map +1 -0
- package/dist/types/executionContext.js +3 -0
- package/dist/types/executionContext.js.map +1 -0
- package/package.json +77 -0
- package/src/agents/agentRegistry.ts +272 -0
- package/src/agents/image-agent.yaml +86 -0
- package/src/agents/joker-agent.yaml +47 -0
- package/src/agents/translator-agent.yaml +80 -0
- package/src/audit/auditTrail.ts +134 -0
- package/src/compliance/gdprEngine.ts +209 -0
- package/src/compliance/lgpdEngine.ts +268 -0
- package/src/config.ts +179 -0
- package/src/errors.ts +35 -0
- package/src/firebase/tenantManager.ts +443 -0
- package/src/index.ts +125 -0
- package/src/integration/secure-yaml-runtime.ts +341 -0
- package/src/parser/secure-yaml-parser.ts +273 -0
- package/src/performance/autoscaling.ts +495 -0
- package/src/performance/benchmark.ts +644 -0
- package/src/performance/index.ts +34 -0
- package/src/performance/monitor.ts +469 -0
- package/src/performance/streaming.ts +317 -0
- package/src/runtime/audit.ts +907 -0
- package/src/runtime/declarativeAgentRuntime.ts +836 -0
- package/src/runtime/isolatedRuntime.ts +572 -0
- package/src/runtime/schemaCompiler.ts +228 -0
- package/src/runtime/simpleRuntime.ts +201 -0
- package/src/security/dashboard.ts +462 -0
- package/src/security/hardening.ts +560 -0
- package/src/security/index.ts +439 -0
- package/src/security/monitor.ts +490 -0
- package/src/security/scanner.ts +368 -0
- package/src/security/score.ts +1138 -0
- package/src/security/threatDetector.ts +481 -0
- package/src/security/validation.ts +365 -0
- package/src/server/api/clientsRoute.ts +92 -0
- package/src/server/api/endpointsRoute.ts +97 -0
- package/src/server/api/graphql.ts +249 -0
- package/src/server/errors.ts +38 -0
- package/src/server/index.ts +6 -0
- package/src/server/kvStore.ts +152 -0
- package/src/server/runtimeSecurity.ts +102 -0
- package/src/server/types.ts +60 -0
- package/src/types/executionContext.ts +16 -0
- package/tools/seed.ts +365 -0
- package/tools/test-endpoints.ts +174 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { SecurityEvent, AlertLevel } from "./monitor";
|
|
2
|
+
|
|
3
|
+
export interface ThreatAnalysis {
|
|
4
|
+
riskScore: number;
|
|
5
|
+
threatType: string;
|
|
6
|
+
confidence: number;
|
|
7
|
+
recommendations: string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class ThreatDetectionEngine {
|
|
11
|
+
private patterns: Map<string, RegExp> = new Map();
|
|
12
|
+
private anomalyDetector: AnomalyDetector;
|
|
13
|
+
private mlModel: ThreatMLModel;
|
|
14
|
+
|
|
15
|
+
constructor() {
|
|
16
|
+
this.initializePatterns();
|
|
17
|
+
this.anomalyDetector = new AnomalyDetector();
|
|
18
|
+
this.mlModel = new ThreatMLModel();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private initializePatterns(): void {
|
|
22
|
+
this.patterns.set(
|
|
23
|
+
"brute_force",
|
|
24
|
+
/multiple_failed_attempts|rapid_login_sequence|authentication_failure|login_fail/i
|
|
25
|
+
);
|
|
26
|
+
this.patterns.set(
|
|
27
|
+
"sql_injection",
|
|
28
|
+
/union.*select|drop.*table|exec.*\(.*\)|';.*--|xp_cmdshell|information_schema/i
|
|
29
|
+
);
|
|
30
|
+
this.patterns.set(
|
|
31
|
+
"data_exfiltration",
|
|
32
|
+
/bulk.*export|mass.*download|unusual.*access|data.*leak|unauthorized.*download/i
|
|
33
|
+
);
|
|
34
|
+
this.patterns.set(
|
|
35
|
+
"cross_tenant",
|
|
36
|
+
/cross.*tenant|tenant.*injection|unauthorized.*access|tenant.*spoofing|bypass.*isolation/i
|
|
37
|
+
);
|
|
38
|
+
this.patterns.set(
|
|
39
|
+
"lgpd_violation",
|
|
40
|
+
/unauthorized.*data|consent.*violation|retention.*breach|right.*to.*be.*forgotten|data.*portability/i
|
|
41
|
+
);
|
|
42
|
+
this.patterns.set(
|
|
43
|
+
"ddos_attack",
|
|
44
|
+
/flood.*attack|denial.*of.*service|rate.*limit.*bypass|connection.*flood/i
|
|
45
|
+
);
|
|
46
|
+
this.patterns.set(
|
|
47
|
+
"privilege_escalation",
|
|
48
|
+
/privilege.*escalation|permission.*bypass|role.*elevation|admin.*bypass/i
|
|
49
|
+
);
|
|
50
|
+
this.patterns.set(
|
|
51
|
+
"malware_detection",
|
|
52
|
+
/malware|virus|trojan|ransomware|backdoor|rootkit/i
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public async analyze(
|
|
57
|
+
tenantId: string,
|
|
58
|
+
operation: string,
|
|
59
|
+
metadata: any
|
|
60
|
+
): Promise<ThreatAnalysis> {
|
|
61
|
+
let riskScore = 0.1; // Base risk score
|
|
62
|
+
let threatType = "low_risk";
|
|
63
|
+
let confidence = 0.9;
|
|
64
|
+
|
|
65
|
+
// Pattern matching detection
|
|
66
|
+
for (const [patternName, pattern] of this.patterns) {
|
|
67
|
+
if (pattern.test(operation) || pattern.test(JSON.stringify(metadata))) {
|
|
68
|
+
switch (patternName) {
|
|
69
|
+
case "brute_force":
|
|
70
|
+
riskScore += 0.4;
|
|
71
|
+
break;
|
|
72
|
+
case "sql_injection":
|
|
73
|
+
riskScore += 0.6;
|
|
74
|
+
break;
|
|
75
|
+
case "data_exfiltration":
|
|
76
|
+
riskScore += 0.8;
|
|
77
|
+
break;
|
|
78
|
+
case "cross_tenant":
|
|
79
|
+
riskScore += 0.9;
|
|
80
|
+
break;
|
|
81
|
+
case "lgpd_violation":
|
|
82
|
+
riskScore += 0.7;
|
|
83
|
+
break;
|
|
84
|
+
case "ddos_attack":
|
|
85
|
+
riskScore += 0.5;
|
|
86
|
+
break;
|
|
87
|
+
case "privilege_escalation":
|
|
88
|
+
riskScore += 0.7;
|
|
89
|
+
break;
|
|
90
|
+
case "malware_detection":
|
|
91
|
+
riskScore += 0.8;
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
riskScore += 0.3;
|
|
95
|
+
}
|
|
96
|
+
threatType = patternName;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Machine learning based anomaly detection
|
|
102
|
+
const mlScore = await this.mlModel.predict(tenantId, operation, metadata);
|
|
103
|
+
riskScore += mlScore;
|
|
104
|
+
|
|
105
|
+
// Time-based anomaly detection
|
|
106
|
+
const anomalyScore = await this.anomalyDetector.detectAnomaly(
|
|
107
|
+
tenantId,
|
|
108
|
+
operation,
|
|
109
|
+
metadata
|
|
110
|
+
);
|
|
111
|
+
riskScore += anomalyScore * 0.3;
|
|
112
|
+
|
|
113
|
+
// Apply additional risk factors
|
|
114
|
+
riskScore = this.applyRiskFactors(riskScore, tenantId, metadata);
|
|
115
|
+
|
|
116
|
+
// Cap risk score at maximum of 1.0
|
|
117
|
+
riskScore = Math.min(riskScore, 1.0);
|
|
118
|
+
|
|
119
|
+
const recommendations = this.generateRecommendations(
|
|
120
|
+
riskScore,
|
|
121
|
+
threatType,
|
|
122
|
+
tenantId
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
riskScore,
|
|
127
|
+
threatType,
|
|
128
|
+
confidence,
|
|
129
|
+
recommendations,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private applyRiskFactors(
|
|
134
|
+
riskScore: number,
|
|
135
|
+
tenantId: string,
|
|
136
|
+
metadata: any
|
|
137
|
+
): number {
|
|
138
|
+
// Increase risk for operations during unusual hours
|
|
139
|
+
const hour = new Date().getHours();
|
|
140
|
+
if (hour < 6 || hour > 22) {
|
|
141
|
+
riskScore += 0.15; // Night operations increase risk
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Increase risk for bulk operations
|
|
145
|
+
if (metadata && metadata.count && metadata.count > 1000) {
|
|
146
|
+
riskScore += 0.2;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Increase risk for unauthorized access attempts
|
|
150
|
+
if (metadata && metadata.isUnauthorized) {
|
|
151
|
+
riskScore += 0.25;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Increase risk for cross-tenant operations
|
|
155
|
+
if (
|
|
156
|
+
metadata &&
|
|
157
|
+
metadata.targetTenant &&
|
|
158
|
+
metadata.targetTenant !== tenantId
|
|
159
|
+
) {
|
|
160
|
+
riskScore += 0.3;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Increase risk for operations without proper consent
|
|
164
|
+
if (metadata && metadata.consentStatus === "missing") {
|
|
165
|
+
riskScore += 0.4;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return Math.min(riskScore, 1.0);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private generateRecommendations(
|
|
172
|
+
riskScore: number,
|
|
173
|
+
threatType: string,
|
|
174
|
+
tenantId: string
|
|
175
|
+
): string[] {
|
|
176
|
+
const recommendations: string[] = [];
|
|
177
|
+
|
|
178
|
+
// Critical threats (>0.8)
|
|
179
|
+
if (riskScore > 0.8) {
|
|
180
|
+
recommendations.push("🚨 EMERGENCY: Immediate action required");
|
|
181
|
+
recommendations.push(`Isolate tenant ${tenantId} immediately`);
|
|
182
|
+
recommendations.push("Contact security team NOW");
|
|
183
|
+
recommendations.push("Preserve audit logs");
|
|
184
|
+
recommendations.push(`Potential ${threatType} attack detected`);
|
|
185
|
+
}
|
|
186
|
+
// High threats (>0.6)
|
|
187
|
+
else if (riskScore > 0.6) {
|
|
188
|
+
recommendations.push("⚠️ CRITICAL: Investigate immediately");
|
|
189
|
+
recommendations.push(
|
|
190
|
+
`Block suspicious activities from tenant ${tenantId}`
|
|
191
|
+
);
|
|
192
|
+
recommendations.push("Monitor all operations from this tenant");
|
|
193
|
+
recommendations.push("Notify security team within 5 minutes");
|
|
194
|
+
recommendations.push(`Check for ${threatType} indicators`);
|
|
195
|
+
}
|
|
196
|
+
// Medium threats (>0.4)
|
|
197
|
+
else if (riskScore > 0.4) {
|
|
198
|
+
recommendations.push("⚠️ WARNING: Monitor closely");
|
|
199
|
+
recommendations.push(`Increase monitoring for tenant ${tenantId}`);
|
|
200
|
+
recommendations.push("Check access patterns");
|
|
201
|
+
recommendations.push("Review permissions");
|
|
202
|
+
recommendations.push(`Investigate ${threatType} patterns`);
|
|
203
|
+
}
|
|
204
|
+
// Low threats (>0.2)
|
|
205
|
+
else if (riskScore > 0.2) {
|
|
206
|
+
recommendations.push("ℹ️ INFO: Routine observation");
|
|
207
|
+
recommendations.push("Document the pattern");
|
|
208
|
+
recommendations.push("Check if pattern repeats");
|
|
209
|
+
recommendations.push(`Normal monitoring for ${threatType}`);
|
|
210
|
+
}
|
|
211
|
+
// Very low threats
|
|
212
|
+
else {
|
|
213
|
+
recommendations.push("✅ Normal activity");
|
|
214
|
+
recommendations.push("Continue regular monitoring");
|
|
215
|
+
recommendations.push("No special actions required");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return recommendations;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public getStatistics(): any {
|
|
222
|
+
return {
|
|
223
|
+
patternsLoaded: this.patterns.size,
|
|
224
|
+
lastUpdate: new Date().toISOString(),
|
|
225
|
+
mlModelVersion: "2025.1.0",
|
|
226
|
+
detectorVersion: "v3.0",
|
|
227
|
+
threatTypes: Array.from(this.patterns.keys()),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export class AnomalyDetector {
|
|
233
|
+
private normalPatterns: Map<string, any[]> = new Map();
|
|
234
|
+
private anomalyThreshold: number = 2.5;
|
|
235
|
+
private historicalData: Map<
|
|
236
|
+
string,
|
|
237
|
+
{ timestamps: number[]; operations: string[] }
|
|
238
|
+
> = new Map();
|
|
239
|
+
|
|
240
|
+
constructor() {
|
|
241
|
+
this.initializeHistoricalData();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private initializeHistoricalData(): void {
|
|
245
|
+
// Initialize with some baseline patterns
|
|
246
|
+
const baselineTenants = ["tenant-alpha", "tenant-beta", "tenant-gamma"];
|
|
247
|
+
baselineTenants.forEach((tenantId) => {
|
|
248
|
+
this.historicalData.set(tenantId, {
|
|
249
|
+
timestamps: [],
|
|
250
|
+
operations: [],
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public async detectAnomaly(
|
|
256
|
+
tenantId: string,
|
|
257
|
+
operation: string,
|
|
258
|
+
metadata: any
|
|
259
|
+
): Promise<number> {
|
|
260
|
+
const key = `${tenantId}:${operation}`;
|
|
261
|
+
const currentTime = new Date().getTime();
|
|
262
|
+
|
|
263
|
+
// Initialize if not exists
|
|
264
|
+
if (!this.normalPatterns.has(key)) {
|
|
265
|
+
this.normalPatterns.set(key, []);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const patterns = this.normalPatterns.get(key)!;
|
|
269
|
+
|
|
270
|
+
// Advanced anomaly detection
|
|
271
|
+
let anomalyScore = 0.0;
|
|
272
|
+
|
|
273
|
+
// 1. Velocity-based anomaly detection
|
|
274
|
+
if (patterns.length > 5) {
|
|
275
|
+
const recentTimestamps = patterns.slice(-5).map((p) => p.timestamp);
|
|
276
|
+
const avgTimeInterval = this.calculateAverageInterval(recentTimestamps);
|
|
277
|
+
|
|
278
|
+
if (avgTimeInterval > 0) {
|
|
279
|
+
const currentTimeInterval =
|
|
280
|
+
currentTime - recentTimestamps[recentTimestamps.length - 1];
|
|
281
|
+
const deviation =
|
|
282
|
+
Math.abs(currentTimeInterval - avgTimeInterval) / avgTimeInterval;
|
|
283
|
+
|
|
284
|
+
if (deviation > 3.0) {
|
|
285
|
+
anomalyScore += 0.4; // High velocity anomaly
|
|
286
|
+
} else if (deviation > 1.5) {
|
|
287
|
+
anomalyScore += 0.2; // Medium velocity anomaly
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 2. Pattern-based anomaly detection
|
|
293
|
+
if (metadata && metadata.size) {
|
|
294
|
+
const historicalData = this.historicalData.get(tenantId);
|
|
295
|
+
if (historicalData) {
|
|
296
|
+
const avgSize = this.getAverageSize(historicalData.operations);
|
|
297
|
+
const currentSize = metadata.size;
|
|
298
|
+
|
|
299
|
+
if (currentSize > avgSize * 100) {
|
|
300
|
+
anomalyScore += 0.3; // Unusual data size
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// 3. Time-based anomaly detection
|
|
306
|
+
const hour = new Date().getHours();
|
|
307
|
+
if (hour < 6 || hour > 22) {
|
|
308
|
+
anomalyScore += 0.15; // Night operations are more suspicious
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 4. Cross-tenant anomaly detection
|
|
312
|
+
if (
|
|
313
|
+
metadata &&
|
|
314
|
+
metadata.targetTenant &&
|
|
315
|
+
metadata.targetTenant !== tenantId
|
|
316
|
+
) {
|
|
317
|
+
anomalyScore += 0.25; // Cross-tenant operations are high risk
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Store current pattern for future comparison
|
|
321
|
+
patterns.push({
|
|
322
|
+
timestamp: currentTime,
|
|
323
|
+
operation,
|
|
324
|
+
metadata,
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// Keep only recent patterns (last 24 hours)
|
|
328
|
+
const cutoff = currentTime - 24 * 60 * 60 * 1000;
|
|
329
|
+
this.normalPatterns.set(
|
|
330
|
+
key,
|
|
331
|
+
patterns.filter((p) => p.timestamp > cutoff)
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
return Math.min(anomalyScore, 0.8);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private calculateAverageInterval(timestamps: number[]): number {
|
|
338
|
+
if (timestamps.length < 2) return 0;
|
|
339
|
+
|
|
340
|
+
let totalInterval = 0;
|
|
341
|
+
for (let i = 1; i < timestamps.length; i++) {
|
|
342
|
+
totalInterval += timestamps[i] - timestamps[i - 1];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return totalInterval / (timestamps.length - 1);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private getAverageSize(operations: string[]): number {
|
|
349
|
+
if (operations.length === 0) return 0;
|
|
350
|
+
return 1000; // Default average size
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export class ThreatMLModel {
|
|
355
|
+
private modelWeights: Map<string, number> = new Map();
|
|
356
|
+
private trainingData: Map<string, any[]> = new Map();
|
|
357
|
+
private modelVersion: string = "2025.1.0";
|
|
358
|
+
|
|
359
|
+
constructor() {
|
|
360
|
+
this.initializeModel();
|
|
361
|
+
this.loadTrainingData();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private initializeModel(): void {
|
|
365
|
+
// Advanced ML model weights based on tenant behavior patterns
|
|
366
|
+
this.modelWeights.set("tenant_historical_risk", 0.35);
|
|
367
|
+
this.modelWeights.set("operation_frequency_anomaly", 0.25);
|
|
368
|
+
this.modelWeights.set("metadata_complexity_risk", 0.2);
|
|
369
|
+
this.modelWeights.set("time_deviation_risk", 0.15);
|
|
370
|
+
this.modelWeights.set("cross_tenant_detection", 0.4);
|
|
371
|
+
this.modelWeights.set("lgpd_compliance_checker", 0.3);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private loadTrainingData(): void {
|
|
375
|
+
// Load historical training data
|
|
376
|
+
const sampleTrainingData = [
|
|
377
|
+
{
|
|
378
|
+
tenantId: "tenant-alpha",
|
|
379
|
+
operation: "normal_access",
|
|
380
|
+
metadata: { size: 100 },
|
|
381
|
+
riskScore: 0.1,
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
tenantId: "tenant-beta",
|
|
385
|
+
operation: "bulk_export",
|
|
386
|
+
metadata: { size: 10000 },
|
|
387
|
+
riskScore: 0.8,
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
tenantId: "tenant-gamma",
|
|
391
|
+
operation: "cross_tenant_access",
|
|
392
|
+
metadata: { targetTenant: "other" },
|
|
393
|
+
riskScore: 0.9,
|
|
394
|
+
},
|
|
395
|
+
];
|
|
396
|
+
|
|
397
|
+
sampleTrainingData.forEach((data) => {
|
|
398
|
+
const key = `${data.tenantId}:${data.operation}`;
|
|
399
|
+
this.trainingData.set(key, [data]);
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
public async predict(
|
|
404
|
+
tenantId: string,
|
|
405
|
+
operation: string,
|
|
406
|
+
metadata: any
|
|
407
|
+
): Promise<number> {
|
|
408
|
+
let riskScore = 0.0;
|
|
409
|
+
|
|
410
|
+
// 1. Historical tenant behavior analysis
|
|
411
|
+
const historicalKey = `${tenantId}:${operation}`;
|
|
412
|
+
if (this.trainingData.has(historicalKey)) {
|
|
413
|
+
const historicalData = this.trainingData.get(historicalKey)!;
|
|
414
|
+
const avgRisk =
|
|
415
|
+
historicalData.reduce((sum, data) => sum + data.riskScore, 0) /
|
|
416
|
+
historicalData.length;
|
|
417
|
+
riskScore += avgRisk * this.modelWeights.get("tenant_historical_risk")!;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// 2. Operation frequency analysis
|
|
421
|
+
const hour = new Date().getHours();
|
|
422
|
+
if (hour < 6 || hour > 22) {
|
|
423
|
+
riskScore += 0.3 * this.modelWeights.get("time_deviation_risk")!;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// 3. Metadata complexity analysis
|
|
427
|
+
if (metadata && JSON.stringify(metadata).length > 2000) {
|
|
428
|
+
riskScore += 0.2 * this.modelWeights.get("metadata_complexity_risk")!;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// 4. Cross-tenant operation detection
|
|
432
|
+
if (
|
|
433
|
+
metadata &&
|
|
434
|
+
metadata.targetTenant &&
|
|
435
|
+
metadata.targetTenant !== tenantId
|
|
436
|
+
) {
|
|
437
|
+
riskScore += 0.8 * this.modelWeights.get("cross_tenant_detection")!;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// 5. LGPD compliance violation detection
|
|
441
|
+
if (metadata && metadata.consentStatus === "missing") {
|
|
442
|
+
riskScore += 0.6 * this.modelWeights.get("lgpd_compliance_checker")!;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// 6. High-frequency operation detection
|
|
446
|
+
if (metadata && metadata.operationCount && metadata.operationCount > 1000) {
|
|
447
|
+
riskScore += 0.3 * this.modelWeights.get("operation_frequency_anomaly")!;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return Math.min(riskScore, 1.0);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
public getModelInfo(): any {
|
|
454
|
+
return {
|
|
455
|
+
version: this.modelVersion,
|
|
456
|
+
weights: Object.fromEntries(this.modelWeights),
|
|
457
|
+
trainingDataSize: this.trainingData.size,
|
|
458
|
+
features: [
|
|
459
|
+
"tenant_historical_risk",
|
|
460
|
+
"operation_frequency_anomaly",
|
|
461
|
+
"metadata_complexity_risk",
|
|
462
|
+
"time_deviation_risk",
|
|
463
|
+
"cross_tenant_detection",
|
|
464
|
+
"lgpd_compliance_checker",
|
|
465
|
+
],
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
public retrainModel(newData: any[]): void {
|
|
470
|
+
// Simplified retraining logic
|
|
471
|
+
newData.forEach((data) => {
|
|
472
|
+
const key = `${data.tenantId}:${data.operation}`;
|
|
473
|
+
if (!this.trainingData.has(key)) {
|
|
474
|
+
this.trainingData.set(key, []);
|
|
475
|
+
}
|
|
476
|
+
this.trainingData.get(key)!.push(data);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
this.modelVersion = `2025.${new Date().getMonth()}.${new Date().getDate()}`;
|
|
480
|
+
}
|
|
481
|
+
}
|