Misc - Not really random

495 points | 25 solves

Description

Do you really think random numbers generated by computers are random?

Downloads

779B
Open

Solution

We are given 2 files:

  1. rand.py

import random 
import time 
import hashlib

seed = round(time.time())

random.seed(seed, version=2)

while True:
    rand = random.random()
    has = hashlib.sha256(str(rand).encode()).hexdigest()
    flag = f"CTF{{{has}}}"
    if "7a2" in has:
        with open("./flag", "w") as f:
            f.write(flag)
            break
    else:
        print(f"Bad random value: {rand}")

2. log.txt

The weakness in the randomization is in line 5 of the rand.py script. According to documentations,

When the seed is a floating point number, it would be very difficult to guess which seed results in the random value in log.txt. However in this case, because the seed is rounded to an integer, brute-forcing becomes viable.

First I converted the random values in log.txt into a python list using Sublime so that the script will be able to compare and know when the brute-forced seed is correct.

Using seed = round(time.time()), I get the seed at the current time so that I know the last number to brute-force and the number was 1646540228 when I wrote the script. The starting seed should be a feasible number and in my case, I used 1646430228. If the number is too small the brute-force will take forever while if the number is too big, you risk missing the number (now to think about it I should probably have brute-forced it in the reverse direction.)

Flag: CTF{a13a806d175841731b24a01e9af240bc81750967542550a4b3bb77a29a9d291b}

Last updated