在Ubuntu 13.04中用C演示缓冲区溢出

在Ubuntu 13.04中用C演示缓冲区溢出,c,gcc,ubuntu-13.04,C,Gcc,Ubuntu 13.04,作为我任务的一部分,我必须在我的linux机箱中演示stackoverflow 我的盒子配置: 操作系统:Ubuntu 13.04 GCC版本:4.6.3 我试图用标志-fno stack protector编译该程序,该程序成功地符合要求,但在触发堆栈溢出时出现分段错误。如何显示实际的o/p。 缓冲区过流Pgm: int main(int argc, char**argv) { int authentication=0; char cUsername[10], cPassword

作为我任务的一部分,我必须在我的linux机箱中演示stackoverflow

我的盒子配置: 操作系统:Ubuntu 13.04

GCC版本:4.6.3

我试图用标志-fno stack protector编译该程序,该程序成功地符合要求,但在触发堆栈溢出时出现分段错误。如何显示实际的o/p。 缓冲区过流Pgm:

int main(int argc, char**argv)
 {
   int authentication=0;
   char cUsername[10], cPassword[10];
   strcpy(cUsername, argv[1]);
   strcpy(cPassword, argv[2]);
   if(strcmp(cUsername, "admin") == 0 && strcmp(cPassword, "adminpass") == 0)
{
       authentication = 1;}
if(authentication)
{
       printf("Access granted");} 
else
{
       printf("Wrong username and password");
    }return 0;}
如果我给一个像aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
然后它应该显示已授予访问权限,但现在它显示分段错误

如果您使用以下参数启动程序,我的c编译器会发生这种情况:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

int main(int argc, char**argv)
{
  int authentication=0;
  char cUsername[10], cPassword[10];

  strcpy(cUsername, argv[1]);
  // now cUsername contains "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  // and authentication contains "0x41414141" because it has been overwritten because of the
  // buffer overflow of cUsername

  strcpy(cPassword, argv[2]);
  //now cPassword contains "B"

  if(strcmp(cUsername, "admin") == 0 && strcmp(cPassword, "adminpass") == 0)
  {
    // strings are different so we don't get here
    authentication = 1;
  }

  if (authentication)
  {
    // authentication still contains 0x41414141 therefore we get here
    printf("Access granted");
  } 
  else
  {
    printf("Wrong username and password");
  }

  // here we will get a segmentation fault, because the return adress which is on the
  // stack will have been overwritten with 0x41414141 which is most probably an
  // invalid address
  return 0;
}
顺便说一句,如果你的代码格式正确,它更容易阅读

重要


根据您的系统,“授权访问”可能不会打印出来,因为如果输出被缓冲,输出缓冲区通常在主功能返回后清空,并且由于程序seg之前出现故障,因此输出缓冲区永远不会清空,消息也永远不会显示。尝试在“已授予访问权限”的结尾添加\n字符串。

是否要使堆栈溢出并执行shell代码?是的,我想将长字符串复制到一个小字符数组中。这听起来更像是堆栈损坏而不是堆栈溢出。好的……那么我如何才能成功演示它呢?如果您有一个SIGSEGV,那么您已经演示过了:您在一个小缓冲区中写入了太多数据,操作系统向您抛出了一个段冲突。您想具体演示什么?始终可以使用
fflush(stdout)刷新输出缓冲区因此,当o/p出现时,我希望获得消息访问权限,但它会直接导致分段错误。使用调试器运行程序,您将看到seg错误发生的确切位置。