HSCTF 9 - OTP
Poorly generated random number that allows for a small range to check
Description
“the one-time pad (OTP) is an encryption technique that cannot be cracked” - Wikipedia
Solution
Looking at the source code we see a couple of interesting things.
| |
The code encrypts the flag by generating random bits and does a bitwise XOR, the prints out the length of the flag in binary and the encrypted flag. We also get this as output.txt
| |
The “randomly” generated bits is gotten by using the random.seed() method. By default, this method uses the system time to set the starting number, but if you specify the seed value you actually get the same random number sequence. So what we need to do is to find this seed number, generate the same random bits and XOR the cipher to get the original flag.
In this challenge the seed value is generated with the secure_seed() method. Fortunately for us, this method is quite predictable. It loops ten billion times, generating a random integer each time and adding them together. The more you do this, the closer to the average you get, and so the seed number will basically be close to 10000000000 * 2.5.
All we have to do is a for loop to test possible seed numbers and to be on the safe side start just just below 25000000000 and iterate up. For each number we use it to start a new seed, generate 328 (the length we got from the output) random bits to XOR with the cipther, and if the result contains “flag” we break out of the loop. I added a timer to show how long the script takes to find the flag, as well as printing out the seed number used to get the random bits.
| |
Which generated the output
| |