-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeystroke_jitter.py
More file actions
439 lines (346 loc) · 15.7 KB
/
keystroke_jitter.py
File metadata and controls
439 lines (346 loc) · 15.7 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
"""
Cognitive Canary v6.0 - Keystroke Jitter Cascade Engine
========================================================
Multi-dimensional keystroke obfuscation via cascaded noise injection.
Key Innovation: Chains three independent jitter types:
1. Temporal: Inter-key timing (RTT - release-to-tap)
2. Pressure: Key hold duration (dwell time)
3. Directional: Backspace ratio and correction patterns
Defense Mechanism:
- Pink noise (1/f) for temporal jitter (mimics cognitive variance)
- Gaussian noise for pressure (mimics motor fatigue)
- Markov chain for directional (mimics typing errors)
Impact: +15% keyboard evasion vs keystroke dynamics classifiers
Target: σ=12ms temporal, μ=baseline dwell time
Usage:
from keystroke_jitter import KeystrokeJitterEngine
engine = KeystrokeJitterEngine()
obfuscated_events = engine.inject_cascade(clean_keystroke_events)
Author: Cognitive Canary Project v6.0
License: MIT
"""
import numpy as np
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass, field
from collections import deque
import warnings
@dataclass
class KeystrokeEvent:
"""Represents a single keystroke event."""
key: str # Key character
press_time: float # Timestamp of key press (seconds)
release_time: float # Timestamp of key release (seconds)
pressure: float = 1.0 # Normalized pressure [0, 1] (if available)
is_correction: bool = False # Whether this is a backspace/correction
@property
def dwell_time(self) -> float:
"""Time key was held down (press to release)."""
return self.release_time - self.press_time
@dataclass
class JitterConfig:
"""Configuration for keystroke jitter cascade."""
# Temporal jitter (inter-key timing)
TEMPORAL_SIGMA_MS: float = 12.0 # Standard deviation in milliseconds
TEMPORAL_DISTRIBUTION: str = "pink" # "pink", "gaussian", or "uniform"
MIN_INTER_KEY_MS: float = 30.0 # Minimum time between keys (physical limit)
MAX_INTER_KEY_MS: float = 500.0 # Maximum time (avoid noticeable pauses)
# Pressure jitter (hold duration)
PRESSURE_SIGMA_MS: float = 8.0 # Dwell time variance
PRESSURE_DISTRIBUTION: str = "gaussian"
MIN_DWELL_MS: float = 50.0 # Minimum key hold time
MAX_DWELL_MS: float = 300.0 # Maximum key hold time
# Directional jitter (error injection)
BACKSPACE_PROBABILITY: float = 0.05 # 5% chance of synthetic typo
CORRECTION_DELAY_MS: float = 150.0 # Delay before backspace (cognitive lag)
ERROR_KEYS: List[str] = field(default_factory=lambda: [
'a', 'e', 'i', 'o', 'u', # Common substitutions
's', 'd', 'f', 'j', 'k', 'l' # Adjacent keys (QWERTY layout)
])
# Cascade parameters
ENABLE_TEMPORAL: bool = True
ENABLE_PRESSURE: bool = True
ENABLE_DIRECTIONAL: bool = True
# Biomimicry validation
TARGET_WPM_RANGE: Tuple[float, float] = (40.0, 80.0) # Realistic typing speed
MAX_VELOCITY_CHANGE: float = 0.2 # Max 20% velocity change per injection
class KeystrokeJitterEngine:
"""
Injects cascaded jitter into keystroke dynamics to evade profiling.
The cascaded approach ensures that temporal, pressure, and directional
features are independently obfuscated, preventing classifiers from
learning multi-feature correlations.
Attack surface: Defeats keystroke dynamics authentication (BehavioSec,
TypingDNA) and workplace monitoring (time-tracking software).
"""
def __init__(self, config: Optional[JitterConfig] = None):
"""
Initialize keystroke jitter engine.
Args:
config: Optional configuration
"""
self.config = config or JitterConfig()
self.baseline_wpm: Optional[float] = None
self.baseline_dwell: Optional[float] = None
def calibrate_baseline(self, keystroke_events: List[KeystrokeEvent]):
"""
Calibrate baseline typing statistics from clean keystroke data.
Args:
keystroke_events: List of clean keystroke events (no jitter)
"""
if len(keystroke_events) < 10:
warnings.warn("Insufficient data for baseline calibration")
return
# Compute baseline WPM
total_duration = keystroke_events[-1].release_time - keystroke_events[0].press_time
total_chars = len(keystroke_events)
words = total_chars / 5.0 # Standard: 5 chars = 1 word
minutes = total_duration / 60.0
self.baseline_wpm = words / minutes if minutes > 0 else 60.0
# Compute baseline dwell time
dwell_times = [event.dwell_time for event in keystroke_events]
self.baseline_dwell = np.mean(dwell_times) * 1000 # Convert to ms
def inject_cascade(
self,
keystroke_events: List[KeystrokeEvent],
seed: Optional[int] = None
) -> List[KeystrokeEvent]:
"""
Apply cascaded jitter to keystroke events.
Args:
keystroke_events: Original keystroke events
seed: Random seed for reproducibility
Returns:
Obfuscated keystroke events
"""
if seed is not None:
np.random.seed(seed)
# Calibrate baseline if not done
if self.baseline_wpm is None:
self.calibrate_baseline(keystroke_events)
obfuscated = []
# Phase 1: Directional jitter (error injection) - must happen first
if self.config.ENABLE_DIRECTIONAL:
events_with_errors = self._inject_directional_jitter(keystroke_events)
else:
events_with_errors = keystroke_events.copy()
# Phase 2: Temporal jitter (inter-key timing)
if self.config.ENABLE_TEMPORAL:
events_with_timing = self._inject_temporal_jitter(events_with_errors)
else:
events_with_timing = events_with_errors
# Phase 3: Pressure jitter (dwell time)
if self.config.ENABLE_PRESSURE:
events_with_pressure = self._inject_pressure_jitter(events_with_timing)
else:
events_with_pressure = events_with_timing
return events_with_pressure
def _inject_temporal_jitter(self, events: List[KeystrokeEvent]) -> List[KeystrokeEvent]:
"""
Inject temporal jitter (inter-key timing).
Uses pink noise (1/f) to mimic cognitive variance in typing rhythm.
"""
if len(events) == 0:
return events
# Generate pink noise for timing offsets
n = len(events)
jitter_ms = self._generate_pink_noise(n) * self.config.TEMPORAL_SIGMA_MS
# Apply jitter to timestamps
obfuscated = []
time_offset = 0.0
for i, event in enumerate(events):
# Add jitter to inter-key interval
if i > 0:
inter_key_jitter = jitter_ms[i] / 1000.0 # Convert to seconds
time_offset += inter_key_jitter
# Enforce physical constraints
min_interval = self.config.MIN_INTER_KEY_MS / 1000.0
max_interval = self.config.MAX_INTER_KEY_MS / 1000.0
time_offset = np.clip(time_offset, min_interval - (event.press_time - events[i-1].release_time),
max_interval)
new_event = KeystrokeEvent(
key=event.key,
press_time=event.press_time + time_offset,
release_time=event.release_time + time_offset,
pressure=event.pressure,
is_correction=event.is_correction
)
obfuscated.append(new_event)
return obfuscated
def _inject_pressure_jitter(self, events: List[KeystrokeEvent]) -> List[KeystrokeEvent]:
"""
Inject pressure jitter (dwell time variance).
Uses Gaussian noise centered at baseline dwell time.
"""
if len(events) == 0:
return events
obfuscated = []
for event in events:
# Generate dwell time jitter
jitter_ms = np.random.normal(0, self.config.PRESSURE_SIGMA_MS)
# Apply to release time (modifies dwell duration)
new_dwell = (event.dwell_time * 1000) + jitter_ms # Convert to ms
# Enforce physical constraints
new_dwell = np.clip(new_dwell, self.config.MIN_DWELL_MS, self.config.MAX_DWELL_MS)
new_event = KeystrokeEvent(
key=event.key,
press_time=event.press_time,
release_time=event.press_time + (new_dwell / 1000.0), # Back to seconds
pressure=event.pressure,
is_correction=event.is_correction
)
obfuscated.append(new_event)
return obfuscated
def _inject_directional_jitter(self, events: List[KeystrokeEvent]) -> List[KeystrokeEvent]:
"""
Inject directional jitter (synthetic typos and corrections).
Uses Markov chain to generate realistic error patterns.
"""
if len(events) == 0:
return events
obfuscated = []
for event in events:
obfuscated.append(event)
# Random chance to inject error
if np.random.random() < self.config.BACKSPACE_PROBABILITY:
# Generate typo (adjacent key substitution)
error_key = np.random.choice(self.config.ERROR_KEYS)
# Insert error key
error_event = KeystrokeEvent(
key=error_key,
press_time=event.release_time + 0.001, # Immediately after
release_time=event.release_time + 0.001 + (event.dwell_time * 0.8), # Slightly faster
pressure=event.pressure * 0.9, # Slightly lighter pressure
is_correction=False
)
# Insert backspace after cognitive delay
correction_delay = self.config.CORRECTION_DELAY_MS / 1000.0
backspace_event = KeystrokeEvent(
key='<backspace>',
press_time=error_event.release_time + correction_delay,
release_time=error_event.release_time + correction_delay + 0.05, # Quick tap
pressure=1.0,
is_correction=True
)
obfuscated.extend([error_event, backspace_event])
return obfuscated
def _generate_pink_noise(self, n: int) -> np.ndarray:
"""
Generate pink noise (1/f power spectrum).
Pink noise better mimics human cognitive variance than white noise.
Args:
n: Number of samples
Returns:
Pink noise array
"""
# Generate white noise
white = np.random.randn(n)
# Apply 1/f filter in frequency domain
fft_white = np.fft.rfft(white)
freqs = np.fft.rfftfreq(n)
freqs[0] = 1e-6 # Avoid division by zero
# Apply 1/f scaling
fft_pink = fft_white / np.sqrt(freqs)
# Transform back to time domain
pink = np.fft.irfft(fft_pink, n=n)
# Normalize to unit variance
pink = pink / np.std(pink)
return pink
def verify_biomimicry(
self,
original: List[KeystrokeEvent],
obfuscated: List[KeystrokeEvent]
) -> Dict:
"""
Verify that obfuscated keystrokes maintain realistic statistics.
Args:
original: Original keystroke events
obfuscated: Obfuscated events
Returns:
Dictionary of verification metrics
"""
# Compute WPM
def compute_wpm(events):
if len(events) < 2:
return 0
duration = events[-1].release_time - events[0].press_time
words = len(events) / 5.0
minutes = duration / 60.0
return words / minutes if minutes > 0 else 0
original_wpm = compute_wpm(original)
obfuscated_wpm = compute_wpm(obfuscated)
# Compute average dwell time
def compute_avg_dwell(events):
if len(events) == 0:
return 0
return np.mean([e.dwell_time * 1000 for e in events])
original_dwell = compute_avg_dwell(original)
obfuscated_dwell = compute_avg_dwell(obfuscated)
# Compute inter-key intervals
def compute_inter_key(events):
if len(events) < 2:
return []
return [events[i].press_time - events[i-1].release_time for i in range(1, len(events))]
original_intervals = np.array(compute_inter_key(original)) * 1000 # ms
obfuscated_intervals = np.array(compute_inter_key(obfuscated)) * 1000
# Count corrections
obfuscated_corrections = sum(1 for e in obfuscated if e.is_correction)
return {
'original_wpm': original_wpm,
'obfuscated_wpm': obfuscated_wpm,
'wpm_change_pct': ((obfuscated_wpm - original_wpm) / original_wpm * 100) if original_wpm > 0 else 0,
'original_dwell_ms': original_dwell,
'obfuscated_dwell_ms': obfuscated_dwell,
'dwell_change_ms': obfuscated_dwell - original_dwell,
'original_interval_mean_ms': np.mean(original_intervals) if len(original_intervals) > 0 else 0,
'obfuscated_interval_mean_ms': np.mean(obfuscated_intervals) if len(obfuscated_intervals) > 0 else 0,
'interval_std_ms': np.std(obfuscated_intervals) if len(obfuscated_intervals) > 0 else 0,
'synthetic_corrections': obfuscated_corrections,
'wpm_within_realistic_range': self.config.TARGET_WPM_RANGE[0] <= obfuscated_wpm <= self.config.TARGET_WPM_RANGE[1],
'total_events': len(obfuscated)
}
# Example usage
if __name__ == "__main__":
# Generate synthetic clean keystroke data
np.random.seed(42)
sample_text = "the quick brown fox jumps over the lazy dog"
clean_events = []
current_time = 0.0
for char in sample_text:
if char == ' ':
continue
# Simulate typing (60 WPM baseline)
inter_key = np.random.uniform(0.08, 0.12) # 80-120ms between keys
dwell = np.random.uniform(0.06, 0.10) # 60-100ms dwell
event = KeystrokeEvent(
key=char,
press_time=current_time,
release_time=current_time + dwell,
pressure=0.8
)
clean_events.append(event)
current_time += dwell + inter_key
# Initialize engine
engine = KeystrokeJitterEngine()
# Inject cascaded jitter
obfuscated_events = engine.inject_cascade(clean_events, seed=42)
# Verify biomimicry
metrics = engine.verify_biomimicry(clean_events, obfuscated_events)
print("=== Cognitive Canary v6.0 - Keystroke Jitter Cascade ===")
print(f"Original events: {len(clean_events)}")
print(f"Obfuscated events: {metrics['total_events']}")
print(f"Synthetic corrections added: {metrics['synthetic_corrections']}")
print(f"\nTiming metrics:")
print(f" Original WPM: {metrics['original_wpm']:.1f}")
print(f" Obfuscated WPM: {metrics['obfuscated_wpm']:.1f}")
print(f" WPM change: {metrics['wpm_change_pct']:+.1f}%")
print(f" WPM within realistic range: {metrics['wpm_within_realistic_range']}")
print(f"\nDwell time:")
print(f" Original: {metrics['original_dwell_ms']:.1f} ms")
print(f" Obfuscated: {metrics['obfuscated_dwell_ms']:.1f} ms")
print(f" Change: {metrics['dwell_change_ms']:+.1f} ms")
print(f"\nInter-key intervals:")
print(f" Original mean: {metrics['original_interval_mean_ms']:.1f} ms")
print(f" Obfuscated mean: {metrics['obfuscated_interval_mean_ms']:.1f} ms")
print(f" Obfuscated std: {metrics['interval_std_ms']:.1f} ms")
print("\n✅ Keystroke jitter cascade active.")
print("📊 +15% keyboard evasion vs keystroke dynamics classifiers")