forked from GeorgeArgyros/mt_derand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derand.py
293 lines (231 loc) · 7.63 KB
/
derand.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
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
#!/usr/bin/python
from socket import *
from struct import pack, unpack
import sys
bitMask = 0xffffffff
PHP_MT_RAND = True
MT_RAND = False
def bin(n, count=31):
"""
Thanks stack overflow!
"""
return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
class MTRand:
N = 624
M = 397
def __init__(self, php = False):
self.php = php
self.twist = self.phpTwist if php else self.mtTwist
self.seeded = False
self.state = []
self.next = self.N
def phpTwist(self, m, u, v):
"""
The invalid twist operation of the PHP generator
"""
return (m ^ (((u) & 0x80000000)|((v) & 0x7fffffff))>>1) ^ \
((-((u) & 0x00000001)) & 0x9908b0df)
def mtTwist(self, m, u, v):
"""
The original mt twist operation
"""
return (m ^ (((u) & 0x80000000)|((v) & 0x7fffffff))>>1) ^ \
((-((v) & 0x00000001)) & 0x9908b0df)
def mt_srand(self, seed):
"""
The default seeding procedure from the original MT code
"""
self.seeded = True
self.next = self.N
self.state = [seed & bitMask]
for i in range(1, self.N):
s = (1812433253 * (self.state[i-1] ^ (self.state[i-1] >> 30))+ i)
self.state.append(s & bitMask)
def setState(self, state):
"""
Replace existing state with another one and considers the
generator initialized
"""
self.next = self.N
self.state = state
self.seeded = True
def reload(self):
"""
Generate the next N words of the internal state
"""
N = self.N
M = self.M
for i in range(N - M):
self.state[i] = self.twist(self.state[M + i], self.state[i],
self.state[i+1])
for i in range(i+1, N-1):
self.state[i] = self.twist(self.state[i+(M-N)], self.state[i],
self.state[i+1])
self.state[N-1] = self.twist(self.state[M-1], self.state[N-1],
self.state[0])
self.next = 0
return
def mt_rand(self, rmin = None, rmax = None):
"""
Generate a 32 bit integer
"""
if not self.seeded:
self.mt_srand(0xdeadbeef)
if self.next == self.N:
self.reload()
num = self.state[ self.next ]
self.next += 1
num = (num ^ (num >> 11))
num = (num ^ ((num << 7) & 0x9d2c5680))
num = (num ^ ((num << 15) & 0xefc60000))
num = (num ^ (num >> 18))
if not rmin and not rmax:
return num
return (rmin + (num*(rmax - rmin + 1)) / (1<<32))
def php_mt_rand(self, rmin = None, rmax= None):
"""
as returned by PHP
"""
num = self.mt_rand() >> 1
#print bin(num)
if not rmin and not rmax:
return num
return rmin + (num*(rmax - rmin + 1) / (1 <<31))
class MTDerandClient:
cmdLeak = 'L'
cmdSkip = 'N'
cmdSolve = 'S'
cmdClear = 'C'
cmdShutdown = 'T'
rspOK = 'O'
rspError = 'E'
rspSolvable = 'S'
rspMore = 'M'
cmdSize = 2
respSize = 1
stateSize = 624
intSize = 4
def __init__(self, php=False):
"""
Initialize some internal variables.
"""
self.numLeaks = 0
self.gen = MTRand(php)
def __submitCmd(self, cmd, arg = None):
"""
Send a command to the MT Derandomization server.
"""
if cmd == self.cmdLeak or cmd == self.cmdSkip:
pcmd = pack('cc', cmd, chr(arg))
elif cmd == self.cmdSolve or cmd == self.cmdClear or cmd == self.cmdShutdown:
pcmd = pack('cx', cmd)
else:
return None
self.sock.send(pcmd)
resp = self.sock.recv(self.respSize)
if cmd != self.cmdSolve or resp == self.rspError:
return resp
state = self.sock.recv(self.stateSize * self.intSize)
fmt = '%dI' % self.stateSize
return (resp, unpack(fmt, state))
def __skipLeaks(self, num):
"""
Skip a number of leaks. Because the command header is 2 bytes we
can skip a maximum number of 255 bits with a single request.
"""
self.numLeaks += num
num = num * 32
for i in range(num / 255):
#print 'inside'
resp = self.__submitCmd(self.cmdSkip, 255)
if resp == self.rspError:
return False
last = num % 255
#print last
resp = self.__submitCmd(self.cmdSkip, last)
return resp
def __bitsFromLeak(self, leak, min = 0, max = 0x7fffffff, randMax=0x7fffffff):
"""
Collect all the bits that we can possible recollect from the leak given that
is was mapped to the min/max range.
"""
bits = []
l = ((leak-min)*(randMax+1))/(max-min+1)
u = (((leak+1-min)*(randMax+1))/(max-min+1)) - 1
lower = bin(l)
upper = bin(u)
i = 0
while i < 31:
if lower[i] == upper[i]:
bits.append(int(lower[i]))
else:
break
i += 1
return bits
def __skipLeaks(self, num):
"""
Skip a number of leaks. Because the command header is 2 bytes we
can skip a maximum number of 255 bits with a single request.
"""
self.numLeaks += num
num = num * 32
for i in range(num / 255):
#print 'inside'
resp = self.__submitCmd(self.cmdSkip, 255)
if resp == self.rspError:
return False
last = num % 255
#print last
resp = self.__submitCmd(self.cmdSkip, last)
return resp
def connectDerandServer(self, host='localhost', port=8080):
"""
Connect to the derandomization server in order to
"""
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.connect((host, port))
def addLeak(self, leak, rmin = None, rmax = None, randMax=0x7fffffff):
"""
Add a leak to the system.
"""
self.numLeaks += 1
solvable = False
if rmin == None or rmax == None:
bits = self.__bitsFromLeak(leak)
else:
bits = self.__bitsFromLeak(leak, rmin, rmax, randMax)
for bit in bits:
resp = self.__submitCmd(self.cmdLeak, bit)
if resp == self.rspSolvable:
solvable = True
self.__submitCmd(self.cmdSkip, 32 - len(bits))
return solvable
def recoverState(self):
"""
Recover the state from the server and set to it the
state of the internal MT generator. This function will fail
if we don't have enough leaks to recover the internal state.
"""
(resp, state) = self.__submitCmd(self.cmdSolve)
if resp == self.rspError:
return False
l = list(state)
self.gen.setState(list(state))
return True
def advanceToCurrentState(self):
"""
Advance the internal generator to the current position
skiping all the outputs we have received but send to
the derandomization system.
"""
for i in range(self.numLeaks):
self.gen.mt_rand()
return
def shutdownServer(self):
"""
Shutdown the server by submitting a termination command
"""
self.__submitCmd(self.cmdShutdown)
return
if __name__ == '__main__':
print 'MTDerand client and MTRand generator.'