![](/img/trans.png)
[英]exploiting Buffer Overflow using gets() in a simple C program
[英]C program prevent the buffer overflow using the gets for Password
我有以下 c 程序。 当我以字节字节输入输入时,由于缓冲区溢出,它给出了错误的输入。
这是程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
// Use a struct to force local variable memory ordering
struct {
char buff[5];
char pass;
} localinfo;
localinfo.pass = 0;
printf("\n Enter the password:\n");
gets(localinfo.buff);
if(strcmp(localinfo.buff, "byte")){
printf ("\n Wrong Password \n");
}
else {
printf ("\n Correct Password\n");
localinfo.pass = 1; // Set a flag denoting correct password
}
//IF password matches
// GIVE root or admin rights to user
if(localinfo.pass){
printf ("\n Congratulations! Root privileges given to the user!\n");
}
return 0;
}
正确的密码是字节,如果输入字节就可以了。 如果我由于缓冲区溢出而输入 bytebyte,则将 pass 修改为 1。并且用户正在获得管理员权限。
如果输入 bytebyte 作为输入 output 是
密码错误
恭喜! 授予用户的根权限
但如果密码错误,用户不应获得管理员权限。 目前他因为缓冲区溢出而得到如何解决这个问题?
我试图通过 fgets 和 strncmp 解决这个问题,但没有用。
永远不要使用gets
function,它既危险又过时。
改用fgets
fgets(localinfo.buff, sizeof(localinfo.buff), stdin);
为确保已读取整行,请检查最后一个字符是否为'\n'
。 如果不是,则假设出现问题并且输入了错误的密码。
尝试这个
#include <stdio.h>
#include <string.h>
int main(void) {
struct {
char buff[10];
char pass;
}
localinfo;
localinfo.pass = 0;
printf("\n Enter the password:\n");
scanf("%[^\n]s", localinfo.buff);
if (strcmp(localinfo.buff, "byte")) {
printf("\n Wrong Password \n");
} else {
printf("\n Correct Password\n");
localinfo.pass = 1;
}
if (localinfo.pass) {
printf("\n Congratulations! Root privileges given to the user!\n");
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.