writeups / rev / flag-checkerNo.04

Flag Checker

EventUIUCTF 2025Date2025.07.27

Category: rev. Tags: none.

Strings Analysis

The usual first step with a binary is strings. Here, strings ./flag_checker turns up nothing better than sigpwny{}, so the flag isn’t stored in plaintext and we need a decompiler. I used Ghidra.

Initial Ghidra Analysis

Ghidra decompiles main as follows.

undefined8 main(void)

{
  char cVar1;
  long in_FS_OFFSET;
  undefined1 local_38 [40];
  long local_10;
  
  local_10 = *(long *)(in_FS_OFFSET + 0x28);
  get_input(local_38);
  cVar1 = check_input(local_38);
  if (cVar1 != '\0') {
    puts("PRINTING FLAG: ");
    print_flag(local_38);
  }
  if (local_10 != *(long *)(in_FS_OFFSET + 0x28)) {
                    /* WARNING: Subroutine does not return */
    __stack_chk_fail();
  }
  return 0;
}

main reads input with get_input, validates it with check_input, and calls print_flag only if the check returns a nonzero value. print_flag takes our input as its argument, which suggests the flag is derived from the correct input rather than stored in the binary. That would explain the strings result.

get_input

void get_input(long param_1)

{
  int local_10;
  int local_c;
  
  for (local_10 = 0; local_10 < 8; local_10 = local_10 + 1) {
    printf("> ");
    __isoc99_scanf(&DAT_001020a3,param_1 + (long)local_10 * 4);
  }
  for (local_c = 0; local_c < 8; local_c = local_c + 1) {
    *(uint *)((long)local_c * 4 + param_1) = *(uint *)(param_1 + (long)local_c * 4) % 0xffffff2f;
  }
  return;
}

param_1 points to an array of eight 4-byte integers (local_38 in main). The first loop prints > and reads one integer into each slot; param_1 + (long)local_10 * 4 is the address of slot local_10. The second loop reduces every entry modulo 0xffffff2f (4294967087).

check_input

This is the core of the challenge.

undefined8 check_input(long param_1)

{
  int iVar1;
  int local_10;
  
  local_10 = 0;
  while( true ) {
    if (7 < local_10) {
      return 1;
    }
    iVar1 = F(*(undefined4 *)(test_pt + (long)local_10 * 4),
              *(undefined4 *)(param_1 + (long)local_10 * 4),0xffffff2f);
    if (iVar1 != *(int *)(test_ct + (long)local_10 * 4)) break;
    local_10 = local_10 + 1;
  }
  return 0;
}

The loop runs over indices 0 through 7 and returns 1 only once all eight have passed. For each index i, it computes F(test_pt[i], input[i], 0xffffff2f) and compares the result with test_ct[i]; any mismatch breaks out of the loop and returns 0. test_pt and test_ct are global arrays stored in the binary.

So we need inputs that make F reproduce test_ct exactly. What is F?

F

Here param_1 is test_pt[i], param_2 is our input, and param_3 is 0xffffff2f.

ulong F(long param_1,ulong param_2,ulong param_3)

{
  undefined8 local_28;
  undefined8 local_18;
  undefined8 local_10;
  
  local_18 = 1;
  local_10 = param_1 % (long)param_3;
  for (local_28 = param_2; 0 < (long)local_28; local_28 = (long)local_28 >> 1) {
    if ((local_28 & 1) != 0) {
      local_18 = (local_18 * local_10) % param_3;
    }
    local_10 = (local_10 * local_10) % param_3;
  }
  return local_18;
}

F(base, exp, n) is right-to-left square-and-multiply modular exponentiation: it returns base^exp mod n.

  • local_10 holds x2k mod nx^{2^k} \bmod n and is squared every iteration.
  • local_28 holds the remaining bits of the exponent and is shifted right every iteration.
  • Whenever the low bit of local_28 is 1, the current power local_10 is multiplied into the result local_18.

For example, take param_1 = x, param_2 = 13, and param_3 = n. Since 13=1101213 = 1101_2,

x23+22+20≡x23⋅x22⋅x20(modn).x^{2^3 + 2^2 + 2^0} \equiv x^{2^3}\cdot x^{2^2}\cdot x^{2^0} \pmod{n}.

On the first iteration, 1101 & 1 = 1, so local_18 becomes x20(modn)x^{2^0} \pmod{n}; then local_10 is squared to x21x^{2^1} and local_28 shifts to 0110.

On the second, 0110 & 1 = 0, so local_18 is unchanged, local_10 becomes x22x^{2^2}, and local_28 becomes 0011. On the third, the low bit is 1 again, so local_18 becomes x22⋅x20(modn)x^{2^2}\cdot x^{2^0} \pmod{n}, and so on.

Discrete Log Problem

check_input therefore passes exactly when, for every i,

test_pt[i] input[i]≡test_ct[i](mod0xffffff2f),\texttt{test\_pt}[i]^{\,\texttt{input}[i]} \equiv \texttt{test\_ct}[i] \pmod{\texttt{0xffffff2f}},

so each input is a discrete logarithm. Ghidra shows the two arrays:

test pt in binary test ct in binary

Each entry is a 4-byte little-endian integer (the bytes f5 b1 65 22 are 0x2265b1f5), which gives:

test_pt = [0x2265b1f5, 0x91b7584a, 0xd8f16adf, 0xcd613e30, 0xc386bbc4, 0x1027c4d1, 0x414c343c, 0x1e2feb89]
test_ct = [0xdc44bf5e, 0x5aff1cec, 0xe1e9b4c2, 0x01329b92, 0x8f9ca92a, 0x0e45c5b4, 0x604a4b91, 0x7081eb59]

Sage Script

0xffffff2f is prime, so we can work in GF(p)\mathrm{GF}(p). With a 32-bit modulus, Sage’s built-in discrete_log solves each instance instantly.

modulus = 0xffffff2f
F = GF(modulus)
test_pt = [F(x) for x in [0x2265b1f5, 0x91b7584a, 0xd8f16adf, 0xcd613e30, 0xc386bbc4, 0x1027c4d1, 0x414c343c, 0x1e2feb89]]
test_ct = [F(x) for x in [0xdc44bf5e, 0x5aff1cec, 0xe1e9b4c2, 0x01329b92, 0x8f9ca92a, 0x0e45c5b4, 0x604a4b91, 0x7081eb59]]

for i in range(len(test_pt)):
    print(discrete_log(test_ct[i], test_pt[i]))

The eight logarithms are:

2127877499
1930549411
2028277857
2798570523
901749037
1674216077
3273968005
3294916953

All of them are below the modulus, so the reduction in get_input leaves them unchanged. Entering them into the binary prints the flag:

> 2127877499
> 1930549411
...
PRINTING FLAG: 
sigpwny{CrackingDiscreteLogs4TheFun/Lols}

Download flag-checker

805 words↑ Top