Skip to content

random.random() is not based on /dev/urandom #10

Closed
@oplik0

Description

@oplik0

The README states: Note: Cracker does not implement prediction of random() function since it is based on the os.urandom module which is based on /dev/urandom - however, at least in current versions of CPython, this is not true. You can find the implementation of this function for Python 3.10 here: https://github.com/python/cpython/blob/60adc4b92a8a6fe115a023c8f639a6de4730fac1/Modules/_randommodule.c#L153-L177
It's basically these two lines:

uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));

with genrand_uint32 being the MT random generator, the same as in getrandbits(). One needs to use SystemRandom to get the randomness from the os directly.

There is a slight loss of information about the state here, as in total 11 bits are zeroed, so I'm not sure if it can be used to crack the generator anyway, but it might be a good idea to correct the README at least, and I'll try to add a prediction generator at least.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions