rand¶
rand lets you screw around with rand() and srand() in pure python (and a little bit of rust)
API¶
GlibcRand(seed: int = 1)¶
a pure-python implementation of glibc's random number generator. seed is the initial seed of the RNG, defaulting to 1 (same as glibc if you never call srand)
srand(seed: int) -> None¶
re-initialize the RNG with the seed seed
rand(seed: int) -> None¶
re-initialize the RNG with the seed seed
srand(seed: int) -> None¶
re-initialize the RNG of an already instanced GlibcRand class, so you don't have to
>>> from dog import *
>>> from ctypes import CDLL
>>> srand(67)
>>> rand()
1049489039
>>> x = CDLL('libc.so.6')
>>> x.srand(67)
>>> x.rand()
1049489039
rand() -> int¶
get the RNG output of an already instanced GlibcRand class, so you don't have to
>>> from dog import *
>>> from ctypes import CDLL
>>> rand()
1804289383
>>> CDLL('libc.so.6').rand()
1804289383
GlibcRandCrack()¶
glibc rand() cracker with a similar API to the well-known symbolic_mersenne_cracker. given a series of rand() outputs, clone the RNG state or crack the initial seed. comes with some rust code that can bruteforce the entire 2^32 seed keyspace in a few seconds.
submit(self, observation: int | str | list | tuple | None) -> None:¶
submit an observed RNG output. your input can be one of:
- an
intcontaining a complete output ofrand() - if you only have partial
rand()output (sayrand() & 0xff), astrholding the binary reprsentation of your known bits,?for unknown bits Noneif the RNG made arand()call that you couldn't observe the output of- if submitting multiple outputs at once, a
listortuplecontaining an arbitrary combination of the above if you need to submit partial output, seerand_mod,rand_rshift,rand_and, andrand_divide.
predict(self) -> GlibcRand¶
using the inputs you gave from submit(), attempt to clone the RNG state and return a GlibcRand. if enough outputs are given, smart math will be used, otherwise naive bruteforce.
>>> libc = CDLL('libc.so.6')
>>> libc.srand(590823498)
>>> out = [libc.rand() for _ in range(15)]
>>>
>>> x = GlibcRandCrack()
>>> x.submit(out)
>>> y = x.predict()
>>> y.rand()
500867484
>>> libc.rand()
500867484
get_seed(self, known: int | str | None = None) -> int¶
using the inputs you gave from submit(), attempt to crack the original RNG seed. to speed up the bruteforce, pass known, which can be:
- an
intof all the least-significant bits you know (useful ifsrand(pointer)was done) - a
strof all the bits you know, in the same format as.submit() Noneif you know nothing
get_seeds(self, known: int | str | None = None) -> list[int]¶
exact same as get_seed, but bruteforce the entire 2^32 keyspace and return every seed that matches your submitted data. useful if you've submitted very low-entropy data. only use this if you actually need all possible seeds!
rand_mod(value: int, n: int) -> str¶
given a rand() output value that was generated by rand() % n, correctly format it for GlibcRandCrack.submit().
>>> libc = CDLL('libc.so.6')
>>> libc.srand(73749362)
>>> out = [libc.rand() % 256 for _ in range(30)]
>>> x = GlibcRandCrack()
>>> x.submit([rand_mod(x,256) for x in out])
>>> x.get_seed()
73749362
rand_rshift(value: int, k: int) -> str¶
given a rand() output value that was generated by rand() >> k, correctly format it for GlibcRandCrack.submit().
>>> libc = CDLL('libc.so.6')
>>> libc.srand(73749362)
>>> out = [libc.rand() >> 8 for _ in range(30)]
>>> x = GlibcRandCrack()
>>> x.submit([rand_rshift(x, 8) for x in out])
>>> x.get_seed()
73749362
rand_and(value: int, mask: int) -> str¶
given a rand() output value that was generated by rand() & mask, correctly format it for GlibcRandCrack.submit().
>>> libc = CDLL('libc.so.6')
>>> libc.srand(73749362)
>>> out = [libc.rand() & 0xff for _ in range(30)]
>>> x = GlibcRandCrack()
>>> x.submit([rand_and(x,0xff) for x in out])
>>> x.get_seed()
73749362
rand_divide(value: int, n: int) -> str¶
given a rand() output value that was generated by rand() // n, correctly format it for GlibcRandCrack.submit().