HSCTF 2020 Crypto - Randomization 2


Randomization 2


Again, we are provided a binary rand2 which runs on the server. On connecting to the server, we’re provided with 2 numbers and then we’re asked to guess the next numbers several(10) times.

Going ahead, let’s use Ghidra to decompile this binary, and then work our ass off to understand the terrible decompiled code. Just kidding…

undefined8 main(void)

  long lVar1;
  long in_FS_OFFSET;
  int local_1c;
  long local_18;
  long local_10;
  local_10 = *(in_FS_OFFSET + 40);
  local_18 = 0;
  puts("I had a bit too much coffee so this is in Java not C");
  puts("(Actually it\'s still in C because Java is a pain)");
  puts("Since I\'m so generous you get 2 free numbers");
  lVar1 = next();
  lVar1 = next();
  local_1c = 0;
  while (local_1c < 10) {
    printf("Guess my number: ");
    lVar1 = next();
    if (lVar1 != local_18) {
                    /* WARNING: Subroutine does not return */
    local_1c += 1;
  puts("You win!");
  printf("Have a flag: ");
  if (local_10 != *(in_FS_OFFSET + 40)) {
                    /* WARNING: Subroutine does not return */
  return 0;

Briefly, we can see an initRandom function and a next function which we’ll look at just in a moment, and the loop runs 10 times and we’re asked for a guess of the next number every time, and if all our guesses work out, we’ll have the flag.

Moving over to the initRandom function:

void initRandom(void)

  int iVar1;
  int local_14;
  long local_10;
  urandom = fopen("/dev/urandom","r");
  local_10 = 0;
  local_14 = 0;
  while (local_14 < 8) {
    iVar1 = fgetc(urandom);
    local_10 = local_10 * 256 + iVar1;
    local_14 += 1;
  curr = local_10;

Basically it initializes the seed to the PRNG, meh we needn’t worry too much about this probably.

Then comes the next function:

long next(void)

  curr = curr * 25214903917 + 11;
  return curr;

Voila, this equation is what we wanted! But a catch, long values can be negative, but we don’t see any negative numbers. Maybe it is unsigned long (64 bits, now we realize what the description probably meant).

Our linear PRNG’s state comprises of just 1 number, so we don’t even need the first number. Take the second number, and do the next function on it.

#include <bits/stdc++.h>
using namespace std;

int main() {
	unsigned long x;
	cin >> x;
	for (int i = 0; i < 10; i++) {
		x = x * 25214903917+11;
		cout << x << endl;

If you’re trying it out in Python, don’t forget to take numbers modulo 2**64.

I had a bit too much coffee so this is in Java not C
(Actually it's still in C because Java is a pain)
Since I'm so generous you get 2 free numbers
Guess my number: 16690969720309355830
Guess my number: Guess my number: Guess my number: Guess my number: Guess my number: Guess my number: Guess my number: Guess my number: Guess my number: You win!
Have a flag: flag{1n53cur3_r4nd0m_46b8861b}

Easy huh, turns out java.util.Random uses almost the same PRNG, DON’T USE THIS FOR CRYPTO lol :triumph:

jekyll.environment != "beta" -%}