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
| import time
import random
import statistics
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
class VulnerableImplementation:
def __init__(self):
self.private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=1024
)
self.public_key = self.private_key.public_key()
def vulnerable_string_compare(self, a, b):
"""Comparación de cadenas vulnerable a timing attack"""
if len(a) != len(b):
return False
for i in range(len(a)):
if a[i] != b[i]:
return False
time.sleep(0.001) # Simular operación costosa
return True
def vulnerable_rsa_decrypt(self, ciphertext):
"""Descifrado RSA vulnerable a timing attack"""
start_time = time.time()
# Simular operación que toma más tiempo con bits en 1
result = 1
exponent = self.private_key.private_numbers().private_exponent
for bit in bin(exponent)[2:]:
if bit == '1':
time.sleep(0.001) # Simular operación costosa
result = (result * result) % self.private_key.private_numbers().public_numbers.n
end_time = time.time()
execution_time = end_time - start_time
return result, execution_time
class TimingAttack:
def __init__(self, target):
self.target = target
self.measurements = []
def measure_timing(self, input_data, num_samples=1000):
"""Medir tiempo de ejecución"""
times = []
for _ in range(num_samples):
start_time = time.time()
self.target.vulnerable_string_compare(input_data, "secret_password")
end_time = time.time()
times.append(end_time - start_time)
return times
def analyze_timing(self, times):
"""Analizar mediciones de tiempo"""
mean_time = statistics.mean(times)
std_time = statistics.stdev(times)
min_time = min(times)
max_time = max(times)
return {
'mean': mean_time,
'std': std_time,
'min': min_time,
'max': max_time,
'range': max_time - min_time
}
def attack_string_compare(self, known_prefix, max_length=20):
"""Atacar comparación de cadenas"""
result = known_prefix
for pos in range(len(known_prefix), max_length):
best_char = None
best_time = 0
for char in range(256):
test_string = result + chr(char)
times = self.measure_timing(test_string, 100)
avg_time = statistics.mean(times)
if avg_time > best_time:
best_time = avg_time
best_char = chr(char)
if best_char:
result += best_char
else:
break
return result
# Ejemplo de uso
vulnerable = VulnerableImplementation()
attacker = TimingAttack(vulnerable)
# Medir tiempo de comparación
times = attacker.measure_timing("test_password")
analysis = attacker.analyze_timing(times)
print(f"Análisis de tiempo: {analysis}")
# Atacar comparación de cadenas
known_prefix = "secret_"
result = attacker.attack_string_compare(known_prefix)
print(f"Cadena extraída: {result}")
|