【问题标题】:Simple Buffer Overflow Exploit简单的缓冲区溢出利用
【发布时间】:2016-02-07 03:08:17
【问题描述】:

我正在尝试编写一个非常简单的程序,重点介绍如何使用缓冲区溢出漏洞绕过受密码保护的系统。代码如下:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char buff[15];
    char tempbuff[15];
    int pass = 0;

    printf("\n Enter a password of length between 1 and 15 characters : \n");
    gets(buff);
    //strcpy("%s",buff);

    printf("\n Enter your password : \n");
    gets(tempbuff);
    //strcpy("%s",tempbuff);

    if(strcmp(tempbuff, buff))
    {
        printf ("\n Wrong Password \n");

    }
    else
    {
        printf ("\n Correct Password \n");
        pass = 1;
    }

    if(pass)
    {
       /* Now Give root or admin rights to user*/
        printf ("\n Root privileges given to the user \n");
    }

    return 0;
}

本质上,我试图通过在第二次被要求输入我的密码时输入一个大于 15 个字符的字符串来将 pass 变量的值从 0 更改为 1。但是,到目前为止,我还不能这样做。任何帮助将不胜感激!

【问题讨论】:

  • 你用 Google 搜索过“如何利用缓冲区溢出 C”吗?
  • 我已经将它编译成程序集,但似乎无法确定 pass 变量的位置。
  • 如果你使用 gcc,你可以使用-g 编译可执行文件,然后使用objdump -S executable 得到C 代码和汇编程序的混合
  • 你如何编译你的程序?

标签: c buffer-overflow


【解决方案1】:

只需对您的代码进行一次更改,我就能在 OS X 中利用您的程序。那就是在tempbuff 之前定义pass。在tempbuff 之前声明pass 意味着pass 在堆栈上位于tempbuff 之后,因此溢出的tempbuff 将覆盖pass。我能够在lldb(或gdb)中检查passtempbuff 的地址。

我还使用-fno-stack-protector 选项对其进行了编译。

#include <stdio.h>
#include <string.h>

int main(void)
{
    char buff[15];
    int pass = 0;
    char tempbuff[15];

    printf("\n Enter a password of length between 1 and 15 characters : \n");
    gets(buff);

    printf("\n Enter your password : \n");
    gets(tempbuff);

    if(strcmp(tempbuff, buff))
    {
        printf ("\n Wrong Password \n");
    }
    else
    {
        printf ("\n Correct Password \n");
        pass = 1;
    }

    if(pass)
        printf ("\n Root privileges given to the user \n");

    return 0;
}

编译:gcc -Wall -Wextra -O0 -g -fno-stack-protector buf.c -o buf

这是输入序列:

safepassword
1234567890123456

这是输出:

$ ./buf < over

 Enter a password of length between 1 and 15 characters :
warning: this program uses gets(), which is unsafe.

 Enter your password :

 Wrong Password

 Root privileges given to the user

【讨论】:

    【解决方案2】:

    不能保证为局部变量分配内存的顺序,也不能保证它们会在连续的位置。以下修改后的代码应该适用于大多数系统。它利用了结构元素被分配连续内存位置的事实(还请注意,数组大小已更改以避免填充。)

    #include <stdio.h>
    #include <string.h>
    
    struct app {
        char buff[16];
        char tempbuff[16];
        int pass;
    };
    
    int main(void)
    {
       struct app app;
       app.pass = 0;
    
        printf("\n Enter a password of length between 1 and 15 characters : \n");
        gets(app.buff);
        //strcpy("%s",buff);
    
        printf("\n Enter your password : \n");
        gets(app.tempbuff);
        //strcpy("%s",tempbuff);
    
        if(strcmp(app.tempbuff, app.buff))
        {
            printf ("\n Wrong Password \n");
    
        }
        else
        {
            printf ("\n Correct Password \n");
            app.pass = 1;
        }
    
        if(app.pass)
        {
           /* Now Give root or admin rights to user*/
            printf ("\n Root privileges given to the user \n");
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多