courses

Crackmes: A Ghidra Static-Analysis Walkthrough

Why do this without a debugger

Running a sample tells you what it did once, on one input. Reading it tells you what it will do on every input. Static analysis is also the only option when the sample refuses to run — wrong architecture, missing C2, a licence dongle you do not have, or anti-analysis checks that detect your debugger and take a different path.

This page works two small crackmes end to end in Ghidra, with no debugger and no execution. They are deliberately not malware: they are the same control-flow patterns you will meet in a real licence check, with the payload removed.

Both binaries are x86-64 ELF, dynamically linked, and built non-PIE so the addresses in this page match what you will see. Download them onto whichever machine you run Ghidra on — crackme1 and crackme2, or in a shell there:

$ curl -O "https://web.cecs.pdx.edu/~dmcgrath/courses/F26/malware/crackmes/{crackme1,crackme2}"
$ chmod +x crackme1 crackme2
$ file crackme1 crackme2
crackme1: ELF 64-bit LSB executable, x86-64, ... not stripped
crackme2: ELF 64-bit LSB executable, x86-64, ... stripped

⚠️ Resist running them. Both are harmless, and ltrace ./crackme1 AAAA would shortcut most of part one. The point of the exercise is the reading. Everything below was produced without executing either binary.

Setting up the project

# Ghidra 12.x
$ ghidra            # or: /opt/ghidra/ghidraRun

File → New Project → Non-Shared Project, then File → Import File and pick crackme1. When Ghidra offers to analyze, say yes and take the defaults. For a binary this size analysis finishes in seconds.

The three windows you will live in:

Window Shows Opened by
Symbol Tree functions, labels, imports left panel, default
Listing disassembly, byte values, data centre panel, default
Decompile Ghidra’s reconstructed C Ctrl+E on a function

See the Ghidra cheatsheet for the full key map.

crackme1: triage before you decompile

Start with the cheap tools. They cost nothing and they aim your reading.

$ strings crackme1 | grep -vE '^(_|GLIBC|\.)' | head -20
usage: %s <password>
Nope.
Good Job!
Right school, wrong department.

Four strings, and they already sketch the control flow: a usage message, a generic failure, the win, and something oddly specific. In Ghidra, select the Good Job! string in the Listing and press Ctrl+Shift+F (References → Show References To) to find who uses it. One hit, in main.

Because this binary kept its symbols, the Symbol Tree hands you the map for free:

check_body      0x0040119f
check_prefix    0x0040116a
fix_table       0x004011d6
main            0x004011ed
scramble        0x00401160
sum_bytes       0x00401186
expected        0x00404028     (data)

⚠️ Symbols are a courtesy, not a guarantee. crackme1 keeps them so the walkthrough can name things. crackme2 does not, and real samples rarely do. Part two is the same job without the map.

Reading main

Press Ctrl+E on main. Ghidra 12.1.2 produces this — lightly reflowed, otherwise verbatim:

undefined8 main(int param_1,undefined8 *param_2)
{
  char *__s;
  int iVar1;
  size_t sVar2;
  undefined8 uVar3;

  if (param_1 == 2) {
    __s = (char *)param_2[1];
    sVar2 = strlen(__s);
    if (sVar2 == 0xb) {
      if (((*__s == 'P') && (__s[1] == 'D')) && (__s[2] == 'X')) {
        puts("Right school, wrong department.");
      }
      else {
        iVar1 = check_prefix(__s);
        if (iVar1 == 0) { puts("Nope."); }
        else {
          iVar1 = sum_bytes(__s);
          if (iVar1 == 0x3c2) {
            iVar1 = check_body(__s);
            if (iVar1 != 0) {
              puts("Good Job!");
              return 0;
            }
            puts("Nope.");
          }
          else { puts("Nope."); }
        }
      }
    }
    else { puts("Nope."); }
    uVar3 = 1;
  }
  else {
    fprintf(stderr,"usage: %s <password>\n",*param_2);
    uVar3 = 2;
  }
  return uVar3;
}

Read the path to return 0 and you have the whole specification:

  1. param_1 == 2 — exactly one argument.
  2. strlen(__s) == 0xb — 11 characters.
  3. The input must not start with PDX.
  4. check_prefix must pass.
  5. sum_bytes must return 0x3c2 — 962.
  6. check_body must return non-zero.

Point 3 is a decoy. It is the only branch with a distinctive message, which is exactly why it attracts attention; it leads to return 1 like every other failure. Noticing that a conspicuous branch is a dead end is part of the job.

check_prefix and sum_bytes

bool check_prefix(char *param_1)
{
  if ((*param_1 == 'P') && (param_1[1] == 'S')) {
    return param_1[2] == 'U';
  }
  return false;
}

The password starts with PSU. Three characters down, eight to go.

int sum_bytes(byte *param_1)
{
  byte *pbVar1;
  int iVar2;

  pbVar1 = param_1 + 0xb;
  iVar2 = 0;
  do {
    iVar2 = iVar2 + (uint)*param_1;
    param_1 = param_1 + 1;
  } while (param_1 != pbVar1);
  return iVar2;
}

A plain byte sum over all 11 characters, which must equal 962. This is a consistency check, not a constraint that identifies the password on its own — treat it as the arithmetic that confirms your answer at the end.

check_body and the decompiler’s arithmetic

undefined8 check_body(long param_1)
{
  char cVar1;
  ulong uVar2;

  uVar2 = 0;
  do {
    cVar1 = scramble(*(undefined1 *)(param_1 + 3 + uVar2),uVar2 & 0xffffffff);
    if (cVar1 != (&expected)[uVar2]) {
      return 0;
    }
    uVar2 = uVar2 + 1;
  } while (uVar2 != 8);
  return 1;
}

For each of the eight body characters: run it through scramble with the index, and compare against expected[i]. So scramble(pw[3+i], i) == expected[i], and if scramble is reversible the password falls straight out.

uint scramble(uint param_1,int param_2)
{
  return -param_2 - 0x5bU ^ param_1;
}

Do not skim that. -param_2 - 0x5bU is Ghidra normalising a subtraction into an addition of a negative, and it is easy to misread. Evaluate it in eight-bit arithmetic:

-0x5b mod 0x100 = 0x100 - 0x5b = 0xa5

so   -i - 0x5b   ==   0xa5 - i      (mod 256)

scramble(c, i) = c XOR (0xa5 - i). XOR is its own inverse, so:

pw[3+i] = expected[i] XOR (0xa5 - i)

Now read expected out of the Listing. Navigate to 0x00404028 (g, then the address) and Ghidra shows eight bytes in .data:

00404028  8d cd 8f cb 89 89 f6 a0

Apply the formula and you get… garbage. 0x8d ^ 0xa5 = 0x28, a (, and the rest is no better: (i,i()i>. It satisfies none of the other constraints, and the byte sum is wrong.

Nothing in the code read so far is incorrect. The data is.

The gotcha: .data is not what runs

The Symbol Tree listed a function that main never calls — fix_table. Decompile it:

void fix_table(void)
{
  byte *pbVar1;

  pbVar1 = &expected;
  do {
    *pbVar1 = *pbVar1 ^ 0x5a;
    pbVar1 = pbVar1 + 1;
  } while (pbVar1 != &__TMC_END__);
  return;
}

It XORs the whole expected table with 0x5a. Nothing calls it, so when does it run? Check the ELF’s initialisation array:

$ readelf -x .init_array crackme1
Hex dump of section '.init_array':
  0x00403de0 50114000 00000000 d6114000 00000000 P.@.......@.....

Two little-endian pointers: 0x00401150 and 0x004011d6. The second is fix_table. Anything listed in .init_array is executed by the loader, before main is entered. In Ghidra the same thing is visible by navigating to .init_array in the Listing, or in the Symbol Tree under Functions where fix_table appears with no incoming call references.

⚠️ The bytes in .data are the on-disk image, not the runtime state. Any constructor, any unpacking stub, any .init_array entry can rewrite them before the first line of main. A static read that ignores initialisation is reading a file that never existed at runtime. This is the single most common way a correct static analysis produces a wrong answer, and it is why packed malware defeats a naive strings.

So the table check_body actually compares against is:

on disk:   8d cd 8f cb 89 89 f6 a0
XOR 0x5a:  d7 97 d5 91 d3 d3 ac fa     <- what is in memory when main runs

Recovering the password

Two reversals, in order: undo the constructor, then undo scramble.

disk = [0x8d, 0xcd, 0x8f, 0xcb, 0x89, 0x89, 0xf6, 0xa0]   # Ghidra Listing, .data

runtime = [b ^ 0x5a for b in disk]                         # fix_table, .init_array
body    = bytes(runtime[i] ^ (0xa5 - i) for i in range(8)) # scramble, inverted

pw = "PSU" + body.decode()
print(pw, len(pw), sum(pw.encode()))
# -> PSUr3v3rs3d 11 962

Check it against every constraint from main before going near a shell:

Constraint Required PSUr3v3rs3d
argument count 1 1
strlen 11 11
not PDX prefix — starts PSU
check_prefix PSU PSU
sum_bytes 962 (0x3c2) 962
check_body 8/8 match 8/8

All six hold, derived entirely from reading. The password is PSUr3v3rs3d.

$ ./crackme1 PSUr3v3rs3d
Good Job!

crackme2: no symbols, and a file on disk

$ ./crackme2
cannot open key file

That is the only hint you get, and strings will not tell you which file to make. The binary is stripped, so there are no function names either.

Your job: recover the name of the file it wants, work out the exact contents it will accept, create it, and get Good Job! out of the binary — all by reading. Everything you need is in the decompilation. The one piece of scaffolding you get is below; the rest is the exercise.

Finding main in a stripped binary

With symbols gone the Symbol Tree offers only entry and a pile of FUN_... labels. The standard move on a stripped glibc ELF: open entry, and read the argument __libc_start_main is called with — the first pointer argument is main. Rename it to main (L on the function name) and the rest of the analysis has somewhere to hang.

Two habits worth carrying in from part one. Cross-reference every interesting string you find — select it in Defined Strings and press Ctrl+Shift+F — and when a function turns out to be worth a name, give it one. Ghidra propagates renames through every caller, and a stripped binary becomes readable surprisingly fast once ten FUN_... labels have become words.

What to hand in

  1. The filename, and the address and bytes you recovered it from.
  2. Every byte the file must contain, in hex, each with the address of the instruction that checks it.
  3. The xxd dump of your key file, and the Good Job! it produces.
  4. One paragraph on how the binary reports failure, and what that reporting hands an attacker who never reads the code at all. Real licence checks leak the same way.

⚠️ Build the file with a tool that writes bytes, not text, and confirm it with xxd before you blame the binary. If your first few attempts are rejected, check the file’s size before you start doubting your analysis.

What these two exercises are really teaching

Pattern here Where you meet it for real
.init_array rewriting .data unpacking stubs; a packed sample’s on-disk strings are meaningless
Decompiler arithmetic that hides a constant every optimised build you will ever read
Decoy branch with a loud message anti-analysis decoys and deliberate time-wasters
Obfuscated strings C2 domains, mutex names, registry paths hidden from strings
Stripped binary the normal case, not the exception

The last row is the real lesson. Nothing above needed a debugger, a sandbox, or the source. It needed the Listing, the Decompiler, readelf, and the discipline to distrust a byte you read out of .data without checking who writes to it.

Key takeaways

References


Related course pages: Static Analysis · Advanced Static Analysis · Ghidra Cheatsheet · Reverse Engineering · Anti-Analysis · Dynamic Analysis

🛠️ Maintenance note: every address, byte value, and block of decompiler output on this page came from the two published binaries, analysed with Ghidra 12.1.2 and built with GCC 16.2.1 at -O1 -fno-pie -no-pie. Rebuilding the crackmes will shift the addresses, and a different Ghidra version may render scramble differently — re-derive the quoted output if you rebuild, or ship the same binaries. Part two is deliberately unhinted apart from locating main; the worked answer is in the instructor key rather than on this page.