-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdetection_avoidance.py
More file actions
559 lines (457 loc) · 21.8 KB
/
detection_avoidance.py
File metadata and controls
559 lines (457 loc) · 21.8 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# Copyright (c) 2025 Jascha Wanger / Tarnover, LLC
# SPDX-License-Identifier: MIT
#
# This file is part of the VectorSmuggle project.
# You may obtain a copy of the license at https://opensource.org/licenses/MIT
"""Detection avoidance techniques for evading DLP, content analysis, and anomaly detection.
Note: This module uses the standard random module for content obfuscation and evasion techniques.
This is intentional and not for cryptographic purposes, hence the nosec comments.
"""
import hashlib
import logging
import random
import re
from typing import Any
import numpy as np
class DetectionAvoidance:
"""Implements techniques to avoid detection by DLP systems and content analysis."""
def __init__(
self,
dlp_keywords: list[str] | None = None,
transformation_strength: float = 0.3,
statistical_noise_level: float = 0.1,
content_obfuscation_ratio: float = 0.2,
random_state: np.random.RandomState | None = None
):
"""
Initialize detection avoidance system.
Args:
dlp_keywords: List of DLP keywords to avoid
transformation_strength: Strength of content transformations (0.0-1.0)
statistical_noise_level: Level of statistical noise to inject
content_obfuscation_ratio: Ratio of content to obfuscate
random_state: Seeded RandomState for deterministic operations
"""
if dlp_keywords is None:
dlp_keywords = self._get_default_dlp_keywords()
self.dlp_keywords = dlp_keywords
self.transformation_strength = transformation_strength
self.statistical_noise_level = statistical_noise_level
self.content_obfuscation_ratio = content_obfuscation_ratio
self.random_state = random_state or np.random.RandomState()
self.detection_patterns = self._compile_detection_patterns()
self.transformation_cache: dict[str, str] = {}
self.logger = logging.getLogger(__name__)
def _get_default_dlp_keywords(self) -> list[str]:
"""Get default DLP keywords to avoid."""
return [
# Financial
"credit card", "ssn", "social security", "bank account", "routing number",
"financial", "salary", "revenue", "profit", "budget", "invoice",
"payment", "transaction", "account number", "tax id", "ein",
"wire transfer", "ach", "swift", "iban", "cryptocurrency", "bitcoin",
# Personal Information
"personal", "confidential", "private", "sensitive", "classified",
"employee", "customer", "client", "patient", "medical",
"pii", "phi", "hipaa", "gdpr", "personal data", "biometric",
"driver license", "passport", "birth date", "maiden name",
# Technical
"password", "api key", "secret", "token", "credential", "authentication",
"database", "server", "network", "security", "vulnerability",
"encryption key", "private key", "certificate", "oauth", "jwt",
"access token", "session id", "hash", "salt", "cipher",
# Business
"proprietary", "trade secret", "intellectual property", "merger",
"acquisition", "strategic", "competitive", "internal", "restricted",
"board meeting", "executive", "c-level", "ceo", "cfo", "cto",
"insider", "material information", "earnings", "forecast",
# Legal/Compliance
"legal", "compliance", "regulation", "audit", "investigation",
"lawsuit", "contract", "agreement", "license", "copyright",
"subpoena", "litigation", "settlement", "nda", "non-disclosure",
"attorney", "counsel", "privileged", "discovery"
]
def _compile_detection_patterns(self) -> list[re.Pattern]:
"""Compile regex patterns for common detection signatures."""
patterns = []
# Common data patterns
pattern_strings = [
r'\b\d{3}-\d{2}-\d{4}\b', # SSN
r'\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b', # Credit card
r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', # Email
r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b', # IP address
r'\b[A-F0-9]{32}\b', # MD5 hash
r'\b[A-F0-9]{40}\b', # SHA1 hash
r'\b[A-F0-9]{64}\b', # SHA256 hash
r'\b\d{9}\b', # Tax ID/EIN
r'\b[A-Z]{2}\d{2}[A-Z0-9]{4}\d{7}([A-Z0-9]?){0,16}\b', # IBAN
r'\b[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?\b', # SWIFT/BIC
r'\b\d{10,12}\b', # Phone numbers
r'\b[A-Z]\d{8}\b', # Passport pattern
r'\b[A-Z]\d{7}\b', # Driver license pattern
r'\bmysql://[^\s]+\b', # Database connection strings
r'\bpostgres://[^\s]+\b',
r'\bmongodb://[^\s]+\b',
r'\bBearer\s+[A-Za-z0-9\-._~+/]+=*\b', # Bearer tokens
r'\bBasic\s+[A-Za-z0-9+/]+=*\b', # Basic auth
r'\bsk-[A-Za-z0-9]{48}\b', # OpenAI API keys
r'\bAKIA[0-9A-Z]{16}\b', # AWS access keys
]
for pattern_str in pattern_strings:
try:
patterns.append(re.compile(pattern_str, re.IGNORECASE))
except re.error as e:
self.logger.warning(f"Invalid regex pattern {pattern_str}: {e}")
return patterns
def avoid_dlp_keywords(self, text: str) -> str:
"""
Transform text to avoid DLP keyword detection.
Args:
text: Input text to transform
Returns:
Transformed text with DLP keywords avoided
"""
if not text:
return text
# Check cache first
text_hash = hashlib.md5(text.encode(), usedforsecurity=False).hexdigest()
if text_hash in self.transformation_cache:
return self.transformation_cache[text_hash]
transformed = text
for keyword in self.dlp_keywords:
if keyword.lower() in transformed.lower():
# Apply various transformation techniques
transformed = self._transform_keyword(transformed, keyword)
# Cache the result
self.transformation_cache[text_hash] = transformed
return transformed
def _transform_keyword(self, text: str, keyword: str) -> str:
"""Apply transformations to avoid specific keyword detection."""
transformations = [
self._character_substitution,
self._word_splitting,
self._synonym_replacement,
self._context_obfuscation,
self._unicode_normalization
]
# Apply random transformation based on strength
if random.random() < self.transformation_strength: # nosec B311
transformation = random.choice(transformations) # nosec B311
text = transformation(text, keyword)
return text
def _character_substitution(self, text: str, keyword: str) -> str:
"""Replace characters with similar-looking alternatives."""
substitutions = {
'a': ['@', 'α', 'а', 'ɑ', 'a', 'ä', 'à', 'á', 'â', 'ã'], # Cyrillic 'а', Greek alpha, etc.
'e': ['3', 'е', 'ε', 'e', 'é', 'è', 'ê', 'ë', '€', 'ē'], # Cyrillic 'е', Greek epsilon
'i': ['1', '!', 'і', 'ι', 'i', 'í', 'ì', 'î', 'ï', 'ī', '|'], # Cyrillic 'і', Greek iota
'o': ['0', 'о', 'ο', 'o', 'ó', 'ò', 'ô', 'õ', 'ö', 'ø', '°'], # Cyrillic 'о', Greek omicron
's': ['$', '5', 'ѕ', 's', 'š', 'ś', 'ş', 'ș', '§'], # Cyrillic 'ѕ'
't': ['7', '+', 'т', 'τ', 't', 'ť', 'ţ', 'ț', '†'], # Cyrillic 'т', Greek tau
'c': ['с', 'ç', 'ć', 'č', 'ĉ', 'ċ', 'c', '©'], # Cyrillic 'с'
'p': ['р', 'ρ', 'p', 'þ'], # Cyrillic 'р', Greek rho
'x': ['х', 'χ', 'x', '×'], # Cyrillic 'х', Greek chi
'y': ['у', 'γ', 'y', 'ý', 'ÿ', 'ŷ'], # Cyrillic 'у', Greek gamma
'n': ['ñ', 'ń', 'ň', 'ņ', 'n'],
'u': ['ü', 'ú', 'ù', 'û', 'ū', 'u'],
'l': ['ł', 'ľ', 'ļ', 'l', '|', '1'],
'r': ['ř', 'ŕ', 'r'],
'd': ['ď', 'đ', 'd'],
'g': ['ğ', 'ģ', 'g'],
'h': ['ħ', 'h'],
'j': ['ĵ', 'j'],
'k': ['ķ', 'k'],
'm': ['m'],
'v': ['v'],
'w': ['w'],
'z': ['ž', 'ź', 'ż', 'z']
}
# Find keyword occurrences (case insensitive)
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
def replace_func(match):
matched_text = match.group()
result = ""
for char in matched_text:
if char.lower() in substitutions and random.random() < 0.3:
# 30% chance to substitute each character
replacement = random.choice(substitutions[char.lower()])
result += replacement if char.islower() else replacement.upper()
else:
result += char
return result
return pattern.sub(replace_func, text)
def _word_splitting(self, text: str, keyword: str) -> str:
"""Split words with invisible characters or spaces."""
# Find keyword occurrences
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
def replace_func(match):
matched_text = match.group()
if len(matched_text) > 2:
# Enhanced zero-width character insertion
split_chars = [
'\u200B', # Zero-width space
'\u200C', # Zero-width non-joiner
'\u200D', # Zero-width joiner
'\u2009', # Thin space
'\u00A0', # Non-breaking space
'\u2060', # Word joiner
'\uFEFF', # Zero-width no-break space
]
# Multiple insertion strategy for better obfuscation
result = matched_text
if random.random() < 0.7: # 70% chance to apply splitting
# Insert 1-3 zero-width characters at different positions
num_insertions = random.randint(1, min(3, len(matched_text) - 1))
positions = sorted(random.sample(range(1, len(matched_text)), num_insertions))
# Insert from right to left to maintain position indices
for pos in reversed(positions):
split_char = random.choice(split_chars)
result = result[:pos] + split_char + result[pos:]
return result
return pattern.sub(replace_func, text)
def _synonym_replacement(self, text: str, keyword: str) -> str:
"""Replace keywords with synonyms or related terms."""
synonyms = {
'confidential': ['private', 'restricted', 'internal', 'sensitive'],
'secret': ['private', 'hidden', 'classified', 'covert'],
'password': ['passcode', 'credentials', 'access code', 'login'],
'financial': ['monetary', 'fiscal', 'economic', 'pecuniary'],
'customer': ['client', 'patron', 'consumer', 'user'],
'employee': ['worker', 'staff', 'personnel', 'team member'],
'database': ['data store', 'repository', 'data warehouse', 'records'],
'security': ['protection', 'safety', 'defense', 'safeguard']
}
keyword_lower = keyword.lower()
if keyword_lower in synonyms:
synonym = random.choice(synonyms[keyword_lower])
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
def replace_func(match):
matched_text = match.group()
if matched_text.isupper():
return synonym.upper()
elif matched_text.istitle():
return synonym.title()
else:
return synonym
return pattern.sub(replace_func, text)
return text
def _context_obfuscation(self, text: str, keyword: str) -> str:
"""Add context that changes the meaning of sensitive terms."""
obfuscation_prefixes = [
"example of ", "sample ", "test ", "demo ", "mock ", "fake ",
"hypothetical ", "simulated ", "training ", "educational "
]
pattern = re.compile(r'\b' + re.escape(keyword) + r'\b', re.IGNORECASE)
def replace_func(match):
if random.random() < 0.3: # 30% chance to add prefix
prefix = random.choice(obfuscation_prefixes)
return prefix + match.group()
return match.group()
return pattern.sub(replace_func, text)
def _unicode_normalization(self, text: str, keyword: str) -> str:
"""Use Unicode normalization to create visually similar text."""
# This is a simplified version - in practice, you'd use more sophisticated
# Unicode normalization techniques
unicode_alternatives = {
'a': 'а', # Cyrillic
'e': 'е', # Cyrillic
'o': 'о', # Cyrillic
'p': 'р', # Cyrillic
'c': 'с', # Cyrillic
'x': 'х', # Cyrillic
}
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
def replace_func(match):
matched_text = match.group()
result = ""
for char in matched_text:
if char.lower() in unicode_alternatives and random.random() < 0.2:
result += unicode_alternatives[char.lower()]
else:
result += char
return result
return pattern.sub(replace_func, text)
def transform_content_signatures(self, content: str) -> str:
"""Transform content to avoid signature-based detection."""
if not content:
return content
transformed = content
# Apply pattern-based transformations
for pattern in self.detection_patterns:
matches = pattern.findall(transformed)
for match in matches:
if random.random() < self.content_obfuscation_ratio:
# Obfuscate detected patterns
obfuscated = self._obfuscate_pattern(match)
transformed = transformed.replace(match, obfuscated, 1)
return transformed
def _obfuscate_pattern(self, pattern_match: str) -> str:
"""Obfuscate a detected pattern."""
# Simple obfuscation - replace some characters with similar ones
obfuscated = ""
for char in pattern_match:
if char.isdigit() and random.random() < 0.3:
# Replace some digits with letters
obfuscated += random.choice(['O', 'l', 'I', 'S', 'G'])
elif char == '@' and random.random() < 0.5:
obfuscated += '[at]'
elif char == '.' and random.random() < 0.3:
obfuscated += '[dot]'
else:
obfuscated += char
return obfuscated
def inject_statistical_noise(self, embeddings: np.ndarray) -> np.ndarray:
"""
Inject statistical noise to avoid anomaly detection.
Args:
embeddings: Input embeddings array
Returns:
Embeddings with statistical noise injected
"""
if embeddings.size == 0:
return embeddings
# Calculate statistics of original embeddings
std = np.std(embeddings, axis=0)
# Generate noise that maintains statistical properties
noise_scale = self.statistical_noise_level
# Add correlated noise to maintain embedding structure
noise = np.random.normal(0, noise_scale * std, embeddings.shape)
# Apply noise selectively to avoid detection
noise_mask = np.random.random(embeddings.shape) < self.content_obfuscation_ratio
noise = noise * noise_mask
noisy_embeddings = embeddings + noise
self.logger.debug(f"Injected statistical noise to {embeddings.shape[0]} embeddings")
return noisy_embeddings
def create_decoy_patterns(self, num_decoys: int = 10) -> list[str]:
"""
Create decoy patterns that look suspicious but are harmless.
Args:
num_decoys: Number of decoy patterns to create
Returns:
List of decoy pattern strings
"""
decoy_templates = [
"Test data for {purpose} validation",
"Sample {data_type} for training purposes",
"Mock {item} used in development",
"Dummy {content} for testing only",
"Placeholder {element} - not real data",
"Example {object} for documentation",
"Simulated {thing} for demo purposes",
"Training {material} - educational use only"
]
purposes = ["security", "compliance", "audit", "validation", "verification"]
data_types = ["records", "entries", "information", "content", "data"]
items = ["credentials", "accounts", "profiles", "documents", "files"]
decoys = []
for _ in range(num_decoys):
template = random.choice(decoy_templates)
# Fill in template variables
if "{purpose}" in template:
decoy = template.format(purpose=random.choice(purposes))
elif "{data_type}" in template:
decoy = template.format(data_type=random.choice(data_types))
elif "{item}" in template:
decoy = template.format(item=random.choice(items))
elif "{content}" in template:
decoy = template.format(content=random.choice(data_types))
elif "{element}" in template:
decoy = template.format(element=random.choice(items))
elif "{object}" in template:
decoy = template.format(object=random.choice(data_types))
elif "{thing}" in template:
decoy = template.format(thing=random.choice(items))
elif "{material}" in template:
decoy = template.format(material=random.choice(data_types))
else:
decoy = template
decoys.append(decoy)
return decoys
def evade_anomaly_detection(self, data_points: list[Any]) -> list[Any]:
"""
Modify data points to evade statistical anomaly detection.
Args:
data_points: List of data points to modify
Returns:
Modified data points that appear more normal
"""
if len(data_points) < 2:
return data_points
# Convert to numpy array for easier manipulation
if isinstance(data_points[0], int | float):
data_array = np.array(data_points)
# Calculate statistics
mean = np.mean(data_array)
std = np.std(data_array)
# Identify potential outliers (beyond 2 standard deviations)
outlier_threshold = 2 * std
outliers = np.abs(data_array - mean) > outlier_threshold
# Adjust outliers to be within normal range
adjusted_data = data_array.copy()
for i, is_outlier in enumerate(outliers):
if is_outlier and random.random() < 0.7: # 70% chance to adjust
# Move outlier closer to mean
if data_array[i] > mean:
adjusted_data[i] = mean + outlier_threshold * 0.8
else:
adjusted_data[i] = mean - outlier_threshold * 0.8
return adjusted_data.tolist()
# For non-numeric data, return as-is
return data_points
def assess_detection_risk(self, content: str) -> dict[str, Any]:
"""
Assess the detection risk of given content.
Args:
content: Content to assess
Returns:
Risk assessment dictionary
"""
risk_score = 0.0
detected_patterns = []
detected_keywords = []
# Check for DLP keywords
for keyword in self.dlp_keywords:
if keyword.lower() in content.lower():
detected_keywords.append(keyword)
risk_score += 0.1
# Check for detection patterns
for pattern in self.detection_patterns:
matches = pattern.findall(content)
if matches:
detected_patterns.extend(matches)
risk_score += 0.2 * len(matches)
# Normalize risk score
risk_score = min(1.0, risk_score)
# Categorize risk level
if risk_score < 0.2:
risk_level = "low"
elif risk_score < 0.5:
risk_level = "medium"
elif risk_score < 0.8:
risk_level = "high"
else:
risk_level = "critical"
assessment = {
"risk_score": risk_score,
"risk_level": risk_level,
"detected_keywords": detected_keywords,
"detected_patterns": detected_patterns,
"content_length": len(content),
"keyword_density": len(detected_keywords) / len(content.split()) if content.split() else 0
}
return assessment
def get_detection_statistics(self) -> dict[str, Any]:
"""Get statistics about detection avoidance activities."""
stats = {
"dlp_keywords_count": len(self.dlp_keywords),
"detection_patterns_count": len(self.detection_patterns),
"transformation_cache_size": len(self.transformation_cache),
"transformation_strength": self.transformation_strength,
"statistical_noise_level": self.statistical_noise_level,
"content_obfuscation_ratio": self.content_obfuscation_ratio
}
return stats
def clear_transformation_cache(self) -> None:
"""Clear the transformation cache."""
self.transformation_cache.clear()
self.logger.info("Cleared transformation cache")