Easy Path to the Grail - Detailed Writeup
Overview
Binary Name: grail.knight Architecture: ELF 64-bit Symbols: Not stripped
This writeup explains how to reverse the binary and recover the correct password without revealing the actual flag value.
Recon
First, inspect the binary:
file grail.knight
This confirms:
- 64-bit ELF
- Dynamically linked
- Not stripped (function names available)
Run the binary:
./grail.knight
It prompts for input and prints “Wrong password!” if incorrect.
This proves:
- The program performs a check against user input.
- The flag is likely validated through transformation rather than direct comparison.
Dynamic Analysis
Use ltrace to monitor library calls:
ltrace ./grail.knight
Observation:
- Each character is passed through sprintf with "%02X"
- The program performs strcmp against a long hexadecimal string
This proves:
- Input is transformed before comparison.
- The comparison is made against a hardcoded hex string.
- The transformation produces byte-level hex output.
Static Analysis
Open in radare2:
r2 grail.knight
aaa
afl
Important functions discovered:
- main
- transform_input
- do_fight
This confirms that input processing is separated into transformation logic.
Transformation Logic
transform_input
Disassembly shows:
- For each input character:
- Call do_fight
- Convert returned byte to hex using sprintf("%02X")
- Append to output buffer
So the algorithm is:
transformed_byte = do_fight(original_byte)
output += HEX(transformed_byte)
do_fight
The assembly loop:
shift left accumulator
extract lowest bit from input
append bit to accumulator
shift input right
repeat 8 times
This proves:
- The function performs bit reversal on each byte.
- Each byte of input is reversed bitwise.
Example (demonstration only):
If a byte is: 01100001
Bit-reversed: 10000110
Reversing the Comparison
The binary compares against a fixed hexadecimal string.
Since the transformation is bit reversal:
To recover the original input:
original_byte = bit_reverse(transformed_byte)
Steps to recover password:
- Take the hardcoded hex string.
- Split into byte pairs.
- Convert each hex pair into a byte.
- Reverse the bits of each byte.
- Convert resulting bytes to ASCII.
Proof of Work (Verification Method)
To confirm correctness:
- Enter the reconstructed string into the binary.
- If correct:
- The program prints a success message.
- It reveals the flag format using the input provided.
The proof is the success message, not the flag itself.
Tools Used
- file
- ltrace
- radare2
References
- Radare2 Documentation: https://rada.re/n/
- Linux man pages: sprintf(3), strcmp(3)
- Bitwise operations reference: https://en.wikipedia.org/wiki/Bitwise_operation
This writeup is original and explains the full reasoning process without revealing the actual flag value.