Skip to content

Instantly share code, notes, and snippets.

@kato-megumi
Created July 27, 2018 13:49
Show Gist options
  • Save kato-megumi/e1bc4cf7e63dfa11c655ec93c96bd363 to your computer and use it in GitHub Desktop.
Save kato-megumi/e1bc4cf7e63dfa11c655ec93c96bd363 to your computer and use it in GitHub Desktop.
import time,sys,math
from ctypes import c_int, c_uint
from ctypes import CDLL
# http://www.mscs.dal.ca/~selinger/random/
#implement glibc rand function
libc = CDLL('libc.so.6') # or just use libc :))
def srand(seed):
srand.r = [0 for _ in range(34)]
srand.r[0] = c_int(seed).value
for i in range(1, 31):
srand.r[i] = (16807 * srand.r[i - 1]) % 2147483647
for i in range(31, 34):
srand.r[i] = srand.r[i - 31]
srand.k = 0
for _ in range(34, 344):
rand()
def rand():
srand.r[srand.k] = srand.r[(srand.k - 31) % 34] + srand.r[(srand.k - 3) % 34]
r = c_uint(srand.r[srand.k]).value >> 1
srand.k = (srand.k + 1) % 34
return r
def search(b):
now = int(math.floor(time.time()))
for i in range(-256,256):
srand(now+i)
# libc.srand(now+i)
for x in range(256):
k = rand()
# k = libc.rand()
if k==b:
return now+i
if __name__ == '__main__':
print (search(eval(sys.argv[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment