-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfind_expected_packet_counts.py
More file actions
executable file
·73 lines (56 loc) · 2.11 KB
/
find_expected_packet_counts.py
File metadata and controls
executable file
·73 lines (56 loc) · 2.11 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
#!/usr/bin/env python3
import sour16
import time
import generate_packets
import tempfile
import os
def attempt_attack(block_size_bytes: int, packet_count: int):
filename = 'sour16_packets'
cookie_text = "00000000000000000000000000000000"
directory = tempfile.gettempdir()
filepath = os.path.join(directory, filename)
generate_packets.main(packet_count, filepath, cookie_text, block_size_bytes)
decrypted_cookie_blocks = sour16.main(filepath, block_size_bytes)
percent_left = decrypted_cookie_blocks.count(None)/len(decrypted_cookie_blocks)
os.remove(filepath)
if percent_left == 0:
decrypted_cookie = "".join(decrypted_cookie_blocks)
is_solved = cookie_text in decrypted_cookie
else:
is_solved = False
return (is_solved, percent_left)
def repeat_multiple_attacks(block_size_bytes: int, packet_count: int, runs: int):
percent_lefts = []
start = time.time()
for i in range(runs):
(solved, percent) = attempt_attack(block_size_bytes, packet_count)
percent_lefts.append(percent)
return (sum(percent_lefts)/runs, (time.time()-start)/runs)
def find_expected_packet_count(block_size_bytes: int):
print("Trying to find number of packets needed to attack block_size of ",
block_size_bytes*8, " bits")
runs = 10
packet_count = 2
solved = False
runtime = None
while not solved:
print("Trying packet count: ", packet_count)
(avg_percent_left, runtime) = repeat_multiple_attacks(
block_size_bytes, packet_count, runs)
print(avg_percent_left)
solved = avg_percent_left < 0.01
if not solved:
multiplier = 5
if avg_percent_left < 0.9:
multiplier = 2
packet_count = packet_count*multiplier
return (packet_count, runtime)
def main():
results = []
results.append(find_expected_packet_count(2))
results.append(find_expected_packet_count(3))
results.append(find_expected_packet_count(4))
# results.append(find_expected_packet_count(5))
print(results)
if __name__ == "__main__":
main()