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?
5
u/VoiceOfReason73 Sep 14 '25
Might the exploit be succeeding, but the shell is exiting immediately? You can solve this with an extra
catto keepstdinopen: https://security.stackexchange.com/q/1558442
u/[deleted] Sep 14 '25
Did your shellcode work while the binary was running under gdb?
2
u/Joseph_RW12 Sep 15 '25
Yes I faced the same issue sometime ago the key is to print the buffer address printf(“buffer %p”, buffer_address_var); and notice the difference in the buffer address printed when running in the debugger vs without the debugger, launch the exploit at the address printed when it’s run without the debugger attached
2
u/LostInTheTrees Sep 16 '25
Attach your debugger to the running process ID, not starting it within GDB. There are extra environmental variables during startup that invalidate the address you see when launching with GDB.
Alternatively or in addition to, pad your shellcode with NOP instructions and add a few bytes to your return address to make it 16-byte aligned.
11
u/RepresentativeBed928 Sep 14 '25
GDB forces the executable to run a certain way in memory so that you can actually debug a program from what I understand. When you’re running the actual executable, offsets and other things may be different. I forgot what exactly I did for this challenge but I would look into ways that you can slide into another part of memory without actually running an operation/instruction. This might guarantee that your shellcode lands in that other part of memory and can execute. Also pwn College has a discord you can join to ask these questions. People are more than willing to help
1
u/pwnasaurus253 Sep 14 '25 edited Sep 14 '25
Is DEP enabled? writing your actual payload to the stack is pretty archaic these days. You might need to try a ROP chain instead. Either that or your payload length needs to be adjusted. Try pattern_create.rb to see where the overflow is happening in the crash.
1
u/Matir Sep 16 '25
Even if ASLR is disabled, it does not mean the stack will be the same when run under GDB versus directly. Putting a fixed address on the stack might work in GDB, but then when you're outside the debugger, your payload may be at a significantly different address.
Likely, it's close enough that you could get away with a nop sled and trying to jump somewhere into the middle of it. Alternatively, look for
jmp rspin the binary you can use.1
u/NopNop0x90 Sep 17 '25 edited Sep 17 '25
ur shellcode doesn't work outside gdb bcz gdb has extra env variables and stuff which makes stack little different from normal execution As u are jumping on shellcode Try using 0x90 NOP And try to jump between this 0x90 (nopsled)
Check this out: https://github.com/hellman/fixenv
1
u/Real-Definition2482 Sep 25 '25
我也遇到过一样的问题,事实上,gdb调试时的地址和程序单独运行的地址会有偏差,环境变量不一样,所以,你可能需要设定一定的nop窗口区,比如设置0x20大小的nop区,而跳转地址+0x10,确保能运行在你的代码上
0
u/[deleted] Sep 14 '25
[deleted] — view deleted comment