栈基缓冲区溢出实验中执行system('/bin/sh')触发segment fault core dumped的原因排查
Hey there, let's break down why you're hitting that segfault when trying to spawn a shell in your buffer overflow lab. I've run into this exact scenario a bunch of times, so here are the most likely culprits and how to verify them:
Input stream closure (the GPT call wasn't wrong, but might not be the full story)
When you run./program < payload.txt, your program's standard input is tied to the payload file. Oncegetsfinishes reading the payload, the file descriptor closes, leavingstdinin an EOF state. When/bin/shstarts up, it immediately tries to read fromstdin—but since there's nothing left to read, it exits right away. This can sometimes look like a segfault if you're not monitoring closely, but let's rule out other issues first.Corrupted stack or register state
Even though you successfully jumped tosecret_shell, your overflow payload might have messed up more than just the return address:- You could have overwritten the base pointer (
ebp) with garbage values. Whensecret_shellcallssystem, it relies on a valid stack frame to manage arguments and return addresses. A corruptedebpcan completely throw off the stack layout, leading to a segfault whensystemtries to execute. - If your payload is longer than necessary, you might have overwritten critical stack data like environment pointers or argument values that
systemneeds to launch/bin/sh.
- You could have overwritten the base pointer (
Missing null terminator causing unintended memory corruption
Thevulnerablefunction usesgets(which ignores bounds) and then runsprintf("%s", buffer)to print your input. If your payload doesn't include a null terminator (\x00),printfwill keep reading past the buffer until it hits a null byte somewhere in memory. This can overwrite nearby stack data (like savedebpor return addresses) after the initial overflow, breaking execution right whensystemis about to run.
Quick tests to narrow it down:
Test interactively instead of file redirection
Run the program manually (./program), type your payload directly into the terminal, and see if the shell stays open. If it works here but not withpayload.txt, the input stream closure is definitely the issue. For this case, you can modify your payload to include extra newlines, launch the shell with/bin/sh -i(forces interactive mode), or use a tool likeexpectto keep the input stream alive.Inspect the stack with GDB
Fire up GDB withgdb ./program, set a breakpoint atsecret_shell, then run your payload. When hit, check the value ofebp(useinfo frameorprint $ebp) to confirm it's a valid stack address. Step into thesystemcall withstepito see exactly where the segfault occurs—this will tell you if it's a stack layout problem or something else.Add a null terminator to your payload
Append\x00to the end of your payload. Even if you don't care about the printed output, this byte stopsprintffrom reading past the buffer and causing unintended corruption.
内容来源于stack exchange

