Assembly Assortment
You have a working knowledge of assembly from your journey thus far. Let's broaden it!
This module will explore the effects of a number of different assembly instructions, teaching you to recognize them and not panic in their presence. In most of these challenges, you'll reverse-engineer a binary to understand an instruction's effect on data and feed it input it will accept. In a few others, you'll put an instruction to work yourself, writing a small function around it. Either way, if you understand what the instruction does, you will get flags!
(review) Data
Reverse the Calculation
Reverse the Reverse
Dealing with Bitwise Operations
Even or Odd
Masking Bits
Lowercase a String
Uppercase a String
Swap Case
Bit Shifting
A bit shift moves every bit in a fixed-width value the same number of positions. A left shift moves bits toward the high end, drops the bits that leave that end, and inserts zeros at the low end. Here is a two-position left shift shown in one byte:
before: 1011 0010 (178)
dropped from high end: 10.. ....
inserted at low end: .... ..00
after shifting left by 2: 1100 1000 (200)
A logical right shift does the mirror image: it moves bits toward the low end, drops the low bits, and inserts zeros at the high end.
before: 1101 1011 (219)
dropped from low end: .... ..11
inserted at high end: 00.. ....
after shifting right by 2: 0011 0110 (54)
Each binary position is worth twice the position to its right.
Moving every bit left by one therefore doubles an unsigned value, and shifting left by n multiplies by 2^n as long as no 1 bit is lost past the high end.
Moving every bit right by one halves an unsigned value, discarding any remainder, so shifting right by n divides by 2^n.
On 64-bit x86, shl shifts left and shr performs the zero-filling right shift.
They use the same destination-first form as instructions such as add:
shl rax, 2
shr rax, 2
These examples use a byte so every bit fits on screen, but the same movement happens across all 64 bits of rax.
Shifting Left
Shifting Right
Guide: Reading Stored Data in GDB
Back in Opening the Flag, with RIP, you used a label and lea to pass the address of stored bytes to open.
In Examining Memory with GDB, you used x/s to display the string at an address.
The next challenge combines those ideas in a binary you are reversing.
The binary has already done the lea-style work for you: one of its registers will hold the runtime address of a stored string.
Instead of looking for every secret byte as an immediate inside a cmp, step through the code until a register points at the stored string, then examine that address as a string:
(gdb) x/s $rsi
Here, $rsi is just an example register.
Use the register that the binary loads with the stored string's address, then run /challenge/reverse-me directly with the string you found.