Skip to main content

Predict python's random module random generated values

Project description

randcrack – Python random module cracker / predictor

Build Status PyPI PyPI - Python Version PyPI - Implementation

This script is able to predict python's random module random generated values.

Script was tested against Python versions from 3.5 to 3.10. Should work against other versions of Python as well, since the generator is pretty much the same in 2.7.12. Enjoy!

Installation

To install randcrack, simply:

$ pip install randcrack

How it works

The generator is based upon Mersenne Twister, which is able to generate numbers with excellent statistical properties(indistinguishable from truly random). However, this generator was not designed to be cryptographycally secure. You should NEVER use in critical applications as a PRNG for your crypto scheme. You can learn more about this generator on Wikipedia.

This cracker works as the following way. It obtains first 624 32 bit numbers from the generator and obtains the most likely state of Mersenne Twister matrix, which is the internal state. From this point generator should be synchronized with the cracker.

How to use

It is important to feed cracker exactly 32-bit integers generated by the generator due to the fact that they will be generated anyway, but dropped if you don't request for them. As well, you must feed the cracker exactly after new seed is presented, or after 624*32 bits are generated since every 624 32-bit numbers generator shifts it's state and cracker is designed to be fed from the begining of some state.

Implemented methods

Cracker has one method for feeding: submit(n). After submitting 624 integers it won't take any more and will be ready for predicting new numbers.

Cracker can predict new numbers with following methods, which work exactly the same as their siblings from the random module but without predict_ prefix. These are: predict_getrandbits, predict_randbelow, predict_randrange, predict_randint, predict_choice and predict_random

Here's an example usage:

import random, time
from randcrack import RandCrack

random.seed(time.time())

rc = RandCrack()

for i in range(624):
	rc.submit(random.getrandbits(32))
	# Could be filled with random.randint(0,4294967294) or random.randrange(0,4294967294)

print("Random result: {}\nCracker result: {}"
	.format(random.randrange(0, 4294967295), rc.predict_randrange(0, 4294967295)))

Output

Random result: 127160928
Cracker result: 127160928

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

randcrack-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

randcrack-0.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file randcrack-0.2.0.tar.gz.

File metadata

  • Download URL: randcrack-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for randcrack-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cccf4ecdea147666530f0826d11a72bfc967ccd7ab36e2edf9a951de7c07021b
MD5 85ee1bd74d2d238161d94a6d71f36b8d
BLAKE2b-256 5bc44814fced55db4ca165dbf023513b5c0f217f9ef7261149c573ab9b756a98

See more details on using hashes here.

File details

Details for the file randcrack-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: randcrack-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for randcrack-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4575348c9a6db6c6c1c42ea57334ad636a7876b9a8c51b2f591a35eb15a8c4c
MD5 740d3770b2151477034b1568383336ba
BLAKE2b-256 20ba5874f3afb775877c6983a8776b4a8006c86477247636d4b1d3da33ef17d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page