-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattack_mitigations.py
More file actions
557 lines (436 loc) · 17.7 KB
/
attack_mitigations.py
File metadata and controls
557 lines (436 loc) · 17.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
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
"""
Advanced Attack Mitigations
============================
Implements mitigations for attack vectors discovered in Track 13.
Addresses:
1. Demurrage bypass via self-transfer → Track original acquisition time
2. Demurrage flash loans → Apply decay on every transfer
3. Trust Oracle cache poisoning → Context-dependent cache TTL
4. ATP budget fragmentation → Aggregate limits across delegations
5. Delegation chain amplification → Monotonic scope narrowing
6. Witness shopping → Require consensus from category
7. Reputation washing → Identity cost and social proof
8. Reputation inflation → Require diverse attestation sources
Author: Legion Autonomous Web4 Research
Date: 2025-12-05
Session: Track 15
"""
from dataclasses import dataclass
from typing import List, Set, Dict, Optional
from datetime import datetime, timezone
from enum import Enum
# ============================================================================
# 1. Demurrage Bypass Mitigation: Track Original Acquisition Time
# ============================================================================
@dataclass
class ATPHoldingWithLineage:
"""
ATP holding that tracks original acquisition time through transfers.
Mitigation: Self-transfer cannot reset decay age.
"""
entity_lct: str
amount: int
acquired_at: datetime # When THIS entity received it
original_acquisition: datetime # When ATP was FIRST created
transfer_count: int = 0 # How many times transferred
metadata: Dict[str, any] = None
def effective_age_days(self, now: Optional[datetime] = None) -> float:
"""
Calculate age based on ORIGINAL acquisition, not current holder's time.
This prevents self-transfer attacks:
- Agent holds ATP for 30 days
- Transfers to alt account (fresh acquired_at)
- Transfer back
- Effective age is still 30 days (from original_acquisition)
"""
if now is None:
now = datetime.now(timezone.utc)
age_seconds = (now - self.original_acquisition).total_seconds()
return age_seconds / 86400 # Convert to days
# ============================================================================
# 2. Demurrage Flash Loan Mitigation: Decay on Transfer
# ============================================================================
class DemurrageOnTransferEngine:
"""
Apply demurrage decay immediately on every ATP transfer.
Mitigation: Flash loans cannot avoid decay by timing transfers
around scheduled demurrage calculations.
"""
def transfer_atp(
self,
from_entity: str,
to_entity: str,
amount: int,
now: Optional[datetime] = None
) -> tuple[int, int]:
"""
Transfer ATP with immediate decay calculation.
Returns:
(amount_transferred, amount_decayed) tuple
"""
if now is None:
now = datetime.now(timezone.utc)
# Get source holding
from_holding = self.get_holding(from_entity)
# Apply decay BEFORE transfer
decayed, remaining = self.apply_decay(from_entity, now)
# Check if enough ATP after decay
if remaining < amount:
raise ValueError(f"Insufficient ATP after decay: {remaining} < {amount}")
# Transfer (preserve original acquisition time!)
self.deduct_atp(from_entity, amount)
self.add_atp(
to_entity,
amount,
original_acquisition=from_holding.original_acquisition, # Preserve!
transfer_count=from_holding.transfer_count + 1
)
return amount, decayed
# ============================================================================
# 3. Trust Oracle Cache Poisoning Mitigation
# ============================================================================
class ContextDependentCachePolicy:
"""
Context-dependent cache TTL to prevent trust score staleness attacks.
High-risk contexts use shorter TTL or no cache.
"""
# Context sensitivity levels
LOW_RISK = "low" # Read-only, non-sensitive
MEDIUM_RISK = "medium" # Standard operations
HIGH_RISK = "high" # Authorization decisions, ATP spending
CRITICAL = "critical" # Root key operations, high-value transactions
# Cache TTLs by risk level (seconds)
CACHE_TTL = {
LOW_RISK: 300, # 5 minutes
MEDIUM_RISK: 60, # 1 minute
HIGH_RISK: 10, # 10 seconds
CRITICAL: 0 # No cache
}
def get_cache_ttl(self, context: Dict) -> int:
"""
Determine cache TTL based on operation context.
Args:
context: Operation context including:
- operation_type: What operation is being performed
- atp_cost: How much ATP is involved
- resource_sensitivity: How sensitive is the resource
"""
# Critical operations: no cache
if context.get("operation_type") in ["root_key_sign", "revoke_delegation"]:
return self.CACHE_TTL[self.CRITICAL]
# High ATP cost: short cache
atp_cost = context.get("atp_cost", 0)
if atp_cost > 1000:
return self.CACHE_TTL[self.HIGH_RISK]
# Sensitive resource: short cache
if context.get("resource_sensitivity") == "high":
return self.CACHE_TTL[self.HIGH_RISK]
# Default: medium risk
return self.CACHE_TTL[self.MEDIUM_RISK]
class TrustOracleWithContextualCache:
"""Trust Oracle with context-dependent caching"""
def __init__(self):
self.cache_policy = ContextDependentCachePolicy()
self.cache: Dict = {}
def get_trust_score(
self,
lct_id: str,
organization_id: str,
context: Dict
) -> float:
"""
Get trust score with context-appropriate caching.
High-risk operations use fresh data, low-risk can use cached.
"""
cache_ttl = self.cache_policy.get_cache_ttl(context)
# Check cache
cache_key = (lct_id, organization_id)
if cache_key in self.cache and cache_ttl > 0:
score, timestamp = self.cache[cache_key]
age = (datetime.now(timezone.utc) - timestamp).total_seconds()
if age < cache_ttl:
return score # Cache hit
# Cache miss or expired - query fresh
fresh_score = self._query_trust_database(lct_id, organization_id)
# Cache if TTL > 0
if cache_ttl > 0:
self.cache[cache_key] = (fresh_score, datetime.now(timezone.utc))
return fresh_score
# ============================================================================
# 4. ATP Budget Fragmentation Mitigation
# ============================================================================
class AggregateBudgetEnforcement:
"""
Enforce ATP limits across ALL delegations for an entity.
Mitigation: Cannot bypass per-delegation limits by creating
many tiny delegations.
"""
def __init__(self, max_total_budget_per_entity: int = 10000):
self.max_total_budget = max_total_budget_per_entity
# Track: entity → total budget across all delegations
self.entity_budgets: Dict[str, int] = {}
def create_delegation(
self,
delegator_lct: str,
delegatee_lct: str,
atp_budget: int
) -> bool:
"""
Create delegation with aggregate budget check.
Returns:
True if allowed, False if would exceed aggregate limit
"""
# Get current total budget for delegatee
current_total = self.entity_budgets.get(delegatee_lct, 0)
# Check if new delegation would exceed aggregate limit
if current_total + atp_budget > self.max_total_budget:
return False # Deny: Would exceed aggregate limit
# Allow delegation, update aggregate
self.entity_budgets[delegatee_lct] = current_total + atp_budget
return True
def spend_atp(self, entity_lct: str, amount: int) -> bool:
"""
Spend ATP, checking against aggregate budget.
"""
current_total = self.entity_budgets.get(entity_lct, 0)
if amount > current_total:
return False # Insufficient aggregate budget
self.entity_budgets[entity_lct] -= amount
return True
# ============================================================================
# 5. Delegation Chain Amplification Mitigation
# ============================================================================
class MonotonicScopeEnforcement:
"""
Enforce monotonic scope narrowing in delegation chains.
Mitigation: Sub-delegations can only NARROW scopes, never broaden.
"""
@staticmethod
def is_scope_subset(sub_scopes: List[str], parent_scopes: List[str]) -> bool:
"""
Check if sub_scopes is a subset of parent_scopes.
Returns:
True if sub_scopes ⊆ parent_scopes (monotonic)
"""
sub_set = set(sub_scopes)
parent_set = set(parent_scopes)
return sub_set.issubset(parent_set)
@staticmethod
def validate_sub_delegation(
parent_scopes: List[str],
sub_scopes: List[str]
) -> tuple[bool, str]:
"""
Validate that sub-delegation doesn't amplify privileges.
Returns:
(is_valid, error_message) tuple
"""
if MonotonicScopeEnforcement.is_scope_subset(sub_scopes, parent_scopes):
return True, ""
# Find scopes that were added illegally
parent_set = set(parent_scopes)
sub_set = set(sub_scopes)
illegal_scopes = sub_set - parent_set
return False, f"Illegal scope amplification: {illegal_scopes}"
# ============================================================================
# 6. Witness Shopping Mitigation
# ============================================================================
class WitnessConsensusRequirement:
"""
Require consensus from witness category, not cherry-picked witnesses.
Mitigation: Agent cannot shop for favorable attestations.
"""
def __init__(self, consensus_threshold: float = 0.67):
"""
Args:
consensus_threshold: Fraction of category that must agree (2/3 default)
"""
self.consensus_threshold = consensus_threshold
def validate_attestations(
self,
claim: Dict,
attestations: List[Dict],
witness_category: str,
all_witnesses_in_category: List[str]
) -> tuple[bool, str]:
"""
Validate that sufficient witnesses from category attested.
Args:
claim: The claim being attested
attestations: Attestations provided by agent
witness_category: Category of witnesses (e.g., "time", "oracle")
all_witnesses_in_category: All witnesses in this category
Returns:
(is_valid, reason) tuple
"""
# How many witnesses are in this category?
category_size = len(all_witnesses_in_category)
# How many attestations were provided?
attestation_count = len(attestations)
# Calculate consensus fraction
consensus_fraction = attestation_count / category_size
# Check threshold
if consensus_fraction < self.consensus_threshold:
required = int(self.consensus_threshold * category_size)
return False, (
f"Insufficient consensus: {attestation_count}/{category_size} "
f"(need {required})"
)
# Verify attestations are from category witnesses
attesting_witnesses = {a["witness_did"] for a in attestations}
category_witnesses = set(all_witnesses_in_category)
if not attesting_witnesses.issubset(category_witnesses):
invalid = attesting_witnesses - category_witnesses
return False, f"Invalid witnesses (not in category): {invalid}"
return True, ""
# ============================================================================
# 7. Reputation Washing Mitigation
# ============================================================================
class IdentityCreationCost:
"""
Make identity creation costly to prevent reputation washing.
Mitigations:
1. ATP cost to mint new LCT
2. Social proof requirement (existing entities vouch)
3. Time delay before full reputation
"""
def __init__(
self,
mint_cost_atp: int = 1000,
required_vouches: int = 3,
probation_days: int = 30
):
self.mint_cost = mint_cost_atp
self.required_vouches = required_vouches
self.probation_days = probation_days
def can_mint_lct(
self,
requester_lct: str,
vouchers: List[str],
atp_available: int
) -> tuple[bool, str]:
"""
Check if entity can mint new LCT.
Returns:
(allowed, reason) tuple
"""
# Check ATP cost
if atp_available < self.mint_cost:
return False, f"Insufficient ATP: {atp_available} < {self.mint_cost}"
# Check social proof
if len(vouchers) < self.required_vouches:
return False, (
f"Insufficient vouchers: {len(vouchers)} < {self.required_vouches}"
)
return True, ""
def get_reputation_multiplier(
self,
lct_age_days: int
) -> float:
"""
Get reputation multiplier based on LCT age.
New identities have reduced reputation during probation period.
"""
if lct_age_days >= self.probation_days:
return 1.0 # Full reputation
# Linear ramp during probation
return lct_age_days / self.probation_days
# ============================================================================
# 8. Reputation Inflation Mitigation
# ============================================================================
class DiverseAttestationRequirement:
"""
Require attestations from diverse sources to build reputation.
Mitigation: Prevents collusion ring from inflating each other's scores.
"""
def __init__(self, min_diversity: int = 5):
"""
Args:
min_diversity: Minimum number of independent attestation sources
"""
self.min_diversity = min_diversity
def validate_reputation_sources(
self,
attestations: List[Dict],
graph_analysis: Dict
) -> tuple[bool, str]:
"""
Validate that reputation comes from diverse, independent sources.
Args:
attestations: All attestations contributing to reputation
graph_analysis: Trust graph analysis showing relationships
Returns:
(is_valid, reason) tuple
"""
# Extract unique attesters
attesters = set(a["attester_lct"] for a in attestations)
# Check minimum diversity
if len(attesters) < self.min_diversity:
return False, (
f"Insufficient diversity: {len(attesters)} < {self.min_diversity}"
)
# Check for collusion rings (mutual attestation patterns)
mutual_attestation_count = self._detect_mutual_attestation(
attestations,
graph_analysis
)
mutual_fraction = mutual_attestation_count / len(attestations)
if mutual_fraction > 0.5:
return False, (
f"Excessive mutual attestation: {mutual_fraction:.1%} "
"(possible collusion ring)"
)
return True, ""
def _detect_mutual_attestation(
self,
attestations: List[Dict],
graph_analysis: Dict
) -> int:
"""
Count attestations that are part of mutual attestation patterns.
Mutual pattern: A attests for B, B attests for A (suspicious)
"""
mutual_count = 0
# Build attestation graph
attestation_graph = {}
for att in attestations:
attester = att["attester_lct"]
subject = att["subject_lct"]
if attester not in attestation_graph:
attestation_graph[attester] = set()
attestation_graph[attester].add(subject)
# Check for mutual relationships
for att in attestations:
attester = att["attester_lct"]
subject = att["subject_lct"]
# Is there a reverse attestation?
if subject in attestation_graph and attester in attestation_graph[subject]:
mutual_count += 1
return mutual_count
# ============================================================================
# Mitigation Summary
# ============================================================================
def print_mitigation_summary():
"""Print summary of all implemented mitigations"""
print("=" * 70)
print("ATTACK MITIGATION IMPLEMENTATION SUMMARY")
print("=" * 70)
mitigations = [
("Demurrage self-transfer bypass", "Track original acquisition time", "✅"),
("Demurrage flash loans", "Apply decay on every transfer", "✅"),
("Trust Oracle cache poisoning", "Context-dependent cache TTL", "✅"),
("ATP budget fragmentation", "Aggregate limits across delegations", "✅"),
("Delegation chain amplification", "Monotonic scope narrowing", "✅"),
("Witness shopping", "Require category consensus", "✅"),
("Reputation washing", "Identity creation cost + probation", "✅"),
("Reputation inflation", "Diverse attestation sources", "✅"),
]
print("\n📋 Mitigations Implemented:")
for i, (attack, mitigation, status) in enumerate(mitigations, 1):
print(f" {i}. {status} {attack}")
print(f" → {mitigation}")
print("\n" + "=" * 70)
print(f"Total: {len(mitigations)} attack mitigations implemented")
print("=" * 70)
if __name__ == "__main__":
print_mitigation_summary()