64-bit

file-archive
3KB

The difference between 32-bit and 64-bit binary lies in the Calling Conventionarrow-up-right. It's basically how arguments/parameters are passed to the function. In x64 architecture, the first 6 arguments are passed on to the registers (RDI, RSI, RDX, RCX, R8, R9) and the rest get pushed onto the stack. Whereas, in x32, all of the arguments are ultimately passed to the stack.

Enough yapping, let's begin

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void rdi_func() {
    asm("pop %rdi; ret");
}

void flag(int check) {
    if(check == 0xFACEB00C) {
        puts("How are you here??");
    } else {
    	puts("So close, yet so far...");
    }
}

void vuln() {
    char buffer[40];

    printf("There's nothing to see here right?\n> ");
    gets(buffer);
}



int main() {
    vuln();
    return 0;
}

I modified the source a little bit, but it should be the same but we will be passing only one parameter due to me having trouble with adding more gadgets (lol)


1. Find offset

We can see this time the Instruction Pointer (RIP) is not populated by the string sequence that we input, this is because in 64-bit programs don't allow invalid memory address in the Instruction Pointer (which the cyclic pattern obviously is) so we read from RSP.

But we only read the starting 8 bytes, since the overflow starts from there .

Another thing to mention is 32-bit addresses tend to be smaller (4 bytes) instead of 64-bit which are 8 bytes. That is why the offset is much larger due to stack alignment (larger saved base pointer (RBP))


2. Grab the address of flag() using gdb

Notice how larger the address is compared to the 32-bit version?


3. Find gadget

circle-info
  • Gadgets are small snippets of code followed by a ret instruction, e.g. pop rdi; ret. We can manipulate the ret of these gadgets in such a way as to string together a large chain of them to do what we want.

  • ROP gadgets are sequences of CPU instructions that are already present in the program being exploited or its loaded shared libraries and can be used to execute almost any arbitrary code;

We can use ropper to find our gadget. Since our binary only loads one argument, we need to find the pop rdi gadget

Great, we have all the components we need to craft our exploit


4. Exploit

Last updated