-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattack_1.py
124 lines (108 loc) · 3.35 KB
/
attack_1.py
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
from prng_1 import prng
from ecff.finitefield.finitefield import FiniteField
import socket
import random
import json
import ssl
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
class Guesser:
def __init__(self, output):
"""
given the initial output,
init out guesser so we can guess
all remaining outputs
output: some output from the prng_1 prng
"""
F = FiniteField(104729,1) #use the same finite field at the prng
P = F(7) #same parameters
Q = F(2)
PQ_inverse = (P*Q).inverse() #the trick! we have multiplicative inverses!
T = F(output)
oldstate = T*PQ_inverse
curstate = oldstate*P*P
self.p = prng(seed=curstate.n)
def guess(self):
"""
by the nature of level 1, we
don't have to 'modify' our guesses.
we're guaranteed to get precisely
the right prng
"""
return self.p.get_num()
# we know the order, P, Q, and the algorithm.
# let's see if we can determine state from just
# a single output
#output = state * P * Q
def get_state(p):
"""
UTILITY/TEST FUNCTION
Given the (assumed default) prng
with various known public constants,
recover the prng's current state
"""
#TODO: pull these from prng object. that would be much cleaner
F = FiniteField(104729,1)
P = F(7)
Q = F(2)
PQ_inverse = (P*Q).inverse()
t = p.get_num()
T = F(t)
oldstate = T*PQ_inverse
curstate = oldstate*P*P
return curstate.n
def test():
"""
UTILITY/TEST FUNCTION
"""
print("randomly generating/recovering 5 prngs")
seeds = [random.randint(1,104729) for i in range(0,5)]
print("seeds: ",seeds)
prngs = [prng(seed=s) for s in seeds]
curstates = [get_state(p) for p in prngs]
print("recovered states: ",curstates)
synced_prngs = [prng(seed=s) for s in curstates]
good_output = [p.get_num() for p in prngs]
synced_output = [p.get_num() for p in synced_prngs]
print("good output: ",good_output)
print("test output: ",synced_output)
if good_output == synced_output:
return True
else:
return False
def attack(ip, port):
"""
attack the service at ip:port
this method uses our guesser to do the heavy
lifting, and instead focuses on the
boilerplate socket communication/json parsing
"""
key = None
msg={"team": "cobra"}
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl_context.wrap_socket(sock)
print("connecting")
s.connect((ip, port))
guess_machine = None
while key is None:
buf = str(s.recv(1024), "UTF-8")
print("recvd:",buf)
try:
response = json.loads(buf)
if "error" in response.keys():
# print error, quit
print("Error:",response["error"])
return
if guess_machine is None:
guess_machine = Guesser(response["num"])
key = response["key"]
except ValueError as e:
print("bad json from server:",e)
return
msg["guess"] = guess_machine.guess()
print("guessing:",msg["guess"])
s.sendall(bytes(json.dumps(msg)+"\n","UTF-8"))
print("key:",key)
if __name__ == "__main__":
attack("127.0.0.1", 1337)