繁体   English   中英

如何从缓冲区溢出的角度编写一个字符串,然后是 null 字节,然后使用 scanf() 获取更多数据?

[英]How can I write a string, then a null byte, then more data using scanf() in the perspective of a buffer overflow?

从使用缓冲区溢出攻击更复杂的程序的角度来看,我尝试通过以下方式使用scanf()调用:

int main(int agrc, char * argv[]) {
    int len = 32;
    char username[32];
   
    printf("Please enter your username: ");
    scanf("%s", username); //scanf that interests me

  
    if (strcmp(username, "admin") == 0) {
        printf("Ok %s! Here is the desired data: ", username);
    } else {
        printf("Sorry you don't have access\n");
    } 
    
    return 0;
}

目标是能够用“admin”写入缓冲区username ,然后是 null 字节,然后是一些数据在len变量上溢出。 这样strcmp(username, "admin")将成功,我们将输入条件,同时能够在len上溢出。

我怎样才能为scanf()形成一个输入来做到这一点? 我的主要问题是我不知道如何使用scanf("%s", username) function 输入 null 字节,即使我知道scanf("%s", username)不会停止在 Z27BA6259CC0C89DFFE字节并将继续扫描。

您可以使用 shell 中的echoprintf创建输入,并将其作为输入发送到您的程序。 您可以在 Linux 或任何其他基于 Unix 的系统中执行以下操作。

$ printf  'admin\x00abcdefgh' | ./a.out # \x00 is the null byte
$ echo -e 'admin\x00abcdefgh' | ./a.out

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM