Giter Club home page Giter Club logo

python-random-module-cracker's Introduction

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 3.5.2, 3.6.2. and 3.7.0. 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 and predict_choice

Note: Cracker does not implement prediction of random() function since it is based on the os.urandom module which is based on /dev/urandom.

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

Accuracy

Cracker is not absolutely accurate. It is able to perform close to 100% accurate on first 624 32-bit generations, ~99.5% on the first 1 000, ~95% on the first 10 000 and then figures drop to ~50% accurate to generation 50 000.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.