You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

栈基缓冲区溢出实验中执行system('/bin/sh')触发segment fault core dumped的原因排查

栈基缓冲区溢出实验中执行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. Once gets finishes reading the payload, the file descriptor closes, leaving stdin in an EOF state. When /bin/sh starts up, it immediately tries to read from stdin—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 to secret_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. When secret_shell calls system, it relies on a valid stack frame to manage arguments and return addresses. A corrupted ebp can completely throw off the stack layout, leading to a segfault when system tries to execute.
    • If your payload is longer than necessary, you might have overwritten critical stack data like environment pointers or argument values that system needs to launch /bin/sh.
  • Missing null terminator causing unintended memory corruption
    The vulnerable function uses gets (which ignores bounds) and then runs printf("%s", buffer) to print your input. If your payload doesn't include a null terminator (\x00), printf will keep reading past the buffer until it hits a null byte somewhere in memory. This can overwrite nearby stack data (like saved ebp or return addresses) after the initial overflow, breaking execution right when system is about to run.

Quick tests to narrow it down:

  1. 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 with payload.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 like expect to keep the input stream alive.

  2. Inspect the stack with GDB
    Fire up GDB with gdb ./program, set a breakpoint at secret_shell, then run your payload. When hit, check the value of ebp (use info frame or print $ebp) to confirm it's a valid stack address. Step into the system call with stepi to see exactly where the segfault occurs—this will tell you if it's a stack layout problem or something else.

  3. Add a null terminator to your payload
    Append \x00 to the end of your payload. Even if you don't care about the printed output, this byte stops printf from reading past the buffer and causing unintended corruption.

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 07:34:36