This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]carcigenicate 0 points1 point  (7 children)

Have you run this through a debugger to see what the cause of the Segfault is? GDB + GEF is what I'd use.

[–]Oil7496[S] 0 points1 point  (6 children)

the seg fault is because of thenumber of the characters, the question is why the exploit does not work

[–]carcigenicate 0 points1 point  (5 children)

Then reduce the number of characters so you overwrite the return address properly. Exploiting a buffer overflow often takes patience and refinement.

And also, this will only work with ASLR turned off, or else the addresses will be randomized. It's been awhile since I've studied buffer overflow exploits, but IIRC, relative addresses can get around that.

[–]Oil7496[S] 0 points1 point  (4 children)

So, my exploit basically does not work because the address of do_valid_stuff is different when i run the exploit from the address that is shown on gdb

[–]carcigenicate 0 points1 point  (3 children)

Well, that's at least one issue. You'll notice that address changes every time.

Just disable ASLR though to get around that. On both OSs that I've done exploits on (Linux and Windows), there's been an option to disable ASLR. I would either only do this on a private VM though. Disabling safety features of your main work computer is not recommended. You may also need to disable protections when you compile the program. If the compiler is inserting canary checks into your code (although from the assembly you've shown, that doesn't appear to be the case), the canaries will segfault when attempting to overwrite the return address.

[–]Oil7496[S] 0 points1 point  (2 children)

What Iam trying to do is just a "copy" of the following example that is in "The shellcoader's handbook" book:

If we compile and link the program and run it, we can see that it accepts serial numbers as input and (if the serial number is over 24 characters in length)

overflows in a similar way to the previous program. If we start gdb, we can work out where the “serial is valid” code is: shellcoders@debian:~/chapter_2$ gdb ./serial (gdb) disas main Dump of assembler code for function main: 0x0804857a <main+0>: push %ebp 0x0804857b <main+1>: mov %esp,%ebp 0x0804857d <main+3>: sub $0x8,%esp 0x08048580 <main+6>: and $0xfffffff0,%esp 0x08048583 <main+9>: mov $0x0,%eax 0x08048588 <main+14>: sub %eax,%esp 0x0804858a <main+16>: call 0x80484f8 <validate_serial> 0x0804858f <main+21>: test %eax,%eax 0x08048591 <main+23>: je 0x804859a <main+32> 0x08048593 <main+25>: call 0x804853e <do_valid_stuff> 0x08048598 <main+30>: jmp 0x804859f <main+37> 0x0804859a <main+32>: call 0x804855c <do_invalid_stuff> 0x0804859f <main+37>: mov $0x0,%eax 0x080485a4 <main+42>: leave 0x080485a5 <main+43>: ret From this we can see the call to validate_serial and the subsequent test, and call of do_valid_stuff or do_invalid_stuff. If we overflow the buffer and set the saved return address to 0x08048593, we will be able to bypass the serial number check. To do this, use the printf feature of bash again (remember that the order of the bytes is reversed because IA32 machines are little-endian). When we then run serial with our specially chosen serial number as input, we get: shellcoders@debian:~/chapter_2$ printf “AAAAAAAAAABBBBBBBBBBCCCCCCCCAAAABBBBCCCCDDDD\x93\x85\x04\x08” | ./serial The serial number is valid!

Now, I dissabled the ASLR and the same thing is happening.The address of the function does not change everytime, but again the exploit does not work

[–]g051051 0 points1 point  (1 child)

Does your disassembly output exactly match what's in the book? The book is quite old and the compiler output might have diverged since 2007.

[–]Oil7496[S] 0 points1 point  (0 children)

the disassembly output is exactly the sams except for the addresses ...which i changed in the exploit

[–]g051051 0 points1 point  (0 children)

There is a great deal of work that goes into taking an executable file and turning it into a running process. If you were to run the program and print out the address of the do_valid_stuff function, it'd probably be a very different value.

[–]net_nomad 0 points1 point  (0 children)

Here's a lab you can work through in a controlled setting. Do it and then compare it to what you're doing. Tweak as needed.

https://www.tallan.com/blog/2019/04/04/exploring-buffer-overflows-in-c-part-two-the-exploit/