S-Dreamer commited on
Commit
ded0382
·
verified ·
1 Parent(s): 6bc2f51

Create osint_core/drift.py

Browse files
Files changed (1) hide show
  1. osint_core/drift.py +292 -0
osint_core/drift.py ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ osint_core/drift.py
3
+
4
+ Purpose:
5
+ Convert execution telemetry into a drift vector.
6
+
7
+ Principles:
8
+ - Drift is a vector, not a scalar.
9
+ - Detection does not mutate state.
10
+ - Correction is separate.
11
+ - Policy drift outranks statistical drift.
12
+ - Adversarial drift constrains before adapting.
13
+ """
14
+
15
+ DEFINE DriftType:
16
+ statistical
17
+ behavioral
18
+ structural
19
+ adversarial
20
+ operational
21
+ policy
22
+
23
+
24
+ DEFINE DriftVector:
25
+ statistical: float 0.0 to 1.0
26
+ behavioral: float 0.0 to 1.0
27
+ structural: float 0.0 to 1.0
28
+ adversarial: float 0.0 to 1.0
29
+ operational: float 0.0 to 1.0
30
+ policy: float 0.0 to 1.0
31
+
32
+
33
+ DEFINE DriftSignal:
34
+ name
35
+ drift_type
36
+ score
37
+ reason
38
+ tier
39
+ evidence
40
+
41
+
42
+ DEFINE DriftAssessment:
43
+ drift_vector
44
+ signals
45
+ dominant_type
46
+ recommended_correction
47
+ confidence
48
+
49
+ FUNCTION assess_drift(telemetry, baseline, manifest, policy_result):
50
+
51
+ signals = []
52
+
53
+ signals += check_policy_drift(policy_result)
54
+
55
+ signals += check_adversarial_drift(telemetry)
56
+
57
+ signals += check_operational_drift(telemetry, baseline)
58
+
59
+ signals += check_structural_drift(telemetry, manifest)
60
+
61
+ signals += check_behavioral_drift(telemetry, baseline)
62
+
63
+ signals += check_statistical_drift(telemetry, baseline)
64
+
65
+ drift_vector = aggregate_signals(signals)
66
+
67
+ dominant_type = choose_dominant_drift_type(drift_vector)
68
+
69
+ correction = recommend_correction(drift_vector, signals)
70
+
71
+ confidence = estimate_confidence(signals)
72
+
73
+ RETURN DriftAssessment(
74
+ drift_vector=drift_vector,
75
+ signals=signals,
76
+ dominant_type=dominant_type,
77
+ recommended_correction=correction,
78
+ confidence=confidence
79
+ )
80
+
81
+ FUNCTION check_adversarial_drift(telemetry):
82
+
83
+ suspicious_patterns = [
84
+ "../",
85
+ "%2e%2e",
86
+ "<script",
87
+ "javascript:",
88
+ "file:",
89
+ "localhost",
90
+ "127.0.0.1",
91
+ "169.254.169.254",
92
+ "$(",
93
+ "`",
94
+ ";",
95
+ "|"
96
+ ]
97
+
98
+ FOR pattern IN suspicious_patterns:
99
+ IF pattern appears in rejected_input_reason OR sanitized_input_trace:
100
+ ADD signal(
101
+ type=adversarial,
102
+ score=0.7,
103
+ tier=T2,
104
+ reason="Suspicious input pattern detected"
105
+ )
106
+
107
+ IF repeated rejected inputs exceed baseline:
108
+ ADD signal(
109
+ type=adversarial,
110
+ score=0.5,
111
+ tier=T2,
112
+ reason="Rejected input rate elevated"
113
+ )
114
+
115
+ RETURN signals
116
+
117
+ FUNCTION check_operational_drift(telemetry, baseline):
118
+
119
+ IF runtime_ms > baseline.runtime_p95 * 2:
120
+ ADD signal(
121
+ type=operational,
122
+ score=0.5,
123
+ tier=T3,
124
+ reason="Runtime exceeded expected boundary"
125
+ )
126
+
127
+ IF error_count > baseline.error_rate_threshold:
128
+ ADD signal(
129
+ type=operational,
130
+ score=0.6,
131
+ tier=T3,
132
+ reason="Error rate exceeded baseline"
133
+ )
134
+
135
+ IF timeout_count increased:
136
+ ADD signal(
137
+ type=operational,
138
+ score=0.4,
139
+ tier=T3,
140
+ reason="Timeout rate elevated"
141
+ )
142
+
143
+ RETURN signals
144
+
145
+ FUNCTION check_structural_drift(telemetry, manifest):
146
+
147
+ IF telemetry.manifest_hash != manifest.hash:
148
+ RETURN signal(
149
+ type=structural,
150
+ score=1.0,
151
+ tier=T1,
152
+ reason="Execution manifest mismatch"
153
+ )
154
+
155
+ IF dependency_hash changed without approved manifest:
156
+ RETURN signal(
157
+ type=structural,
158
+ score=0.9,
159
+ tier=T1,
160
+ reason="Dependency graph changed"
161
+ )
162
+
163
+ IF runtime_python_version changed:
164
+ RETURN signal(
165
+ type=structural,
166
+ score=0.6,
167
+ tier=T2,
168
+ reason="Runtime version changed"
169
+ )
170
+
171
+ RETURN []
172
+
173
+ FUNCTION check_behavioral_drift(telemetry, baseline):
174
+
175
+ IF same_input_hash existed before:
176
+ previous_output_hash = baseline.output_hash_for(input_hash)
177
+
178
+ IF current_output_hash != previous_output_hash:
179
+ ADD signal(
180
+ type=behavioral,
181
+ score=0.9,
182
+ tier=T1,
183
+ reason="Same input produced different output"
184
+ )
185
+
186
+ IF output_schema_invalid:
187
+ ADD signal(
188
+ type=behavioral,
189
+ score=0.8,
190
+ tier=T1,
191
+ reason="Output schema invalid"
192
+ )
193
+
194
+ RETURN signals
195
+
196
+ FUNCTION check_statistical_drift(telemetry, baseline):
197
+
198
+ IF input_type_distribution changed:
199
+ ADD signal(
200
+ type=statistical,
201
+ score=0.4,
202
+ tier=T4,
203
+ reason="Input type distribution shifted"
204
+ )
205
+
206
+ IF module_usage_distribution changed:
207
+ ADD signal(
208
+ type=statistical,
209
+ score=0.3,
210
+ tier=T4,
211
+ reason="Module usage distribution shifted"
212
+ )
213
+
214
+ IF average_input_entropy changed:
215
+ ADD signal(
216
+ type=statistical,
217
+ score=0.4,
218
+ tier=T4,
219
+ reason="Input entropy shifted"
220
+ )
221
+
222
+ RETURN signals
223
+
224
+ FUNCTION aggregate_signals(signals):
225
+
226
+ vector = DriftVector(all zeros)
227
+
228
+ FOR each drift_type:
229
+ matching = signals where signal.type == drift_type
230
+
231
+ IF no matching:
232
+ vector[drift_type] = 0.0
233
+ ELSE:
234
+ vector[drift_type] = max(signal.score for matching)
235
+
236
+ RETURN vector
237
+
238
+ FUNCTION choose_dominant_drift_type(vector):
239
+
240
+ priority_order = [
241
+ policy,
242
+ structural,
243
+ behavioral,
244
+ adversarial,
245
+ operational,
246
+ statistical
247
+ ]
248
+
249
+ FOR type IN priority_order:
250
+ IF vector[type] > 0:
251
+ RETURN type
252
+
253
+ RETURN none
254
+
255
+ FUNCTION recommend_correction(vector, signals):
256
+
257
+ IF vector.policy >= 0.6:
258
+ RETURN REVERT
259
+
260
+ IF vector.structural >= 0.5:
261
+ RETURN REVERT
262
+
263
+ IF vector.behavioral >= 0.7:
264
+ RETURN REVERT
265
+
266
+ IF vector.adversarial >= 0.3:
267
+ RETURN CONSTRAIN
268
+
269
+ IF vector.operational >= 0.7:
270
+ RETURN CONSTRAIN
271
+
272
+ IF vector.statistical >= 0.5:
273
+ RETURN ADAPT
274
+
275
+ RETURN OBSERVE
276
+
277
+ FUNCTION passes_noise_filter(signal, history):
278
+
279
+ IF signal.tier == T1:
280
+ RETURN True
281
+
282
+ persistence = signal appears N times across M windows
283
+
284
+ ensemble_agreement = at least two detectors agree
285
+
286
+ causal_hypothesis = signal.reason is not empty
287
+
288
+ IF persistence AND ensemble_agreement AND causal_hypothesis:
289
+ RETURN True
290
+
291
+ RETURN False
292
+