简体   繁体   English

缓冲区溢出c(gets函数)

[英]buffer overflow c(gets function)

There is the following code, I need to return an access level that's under 0x30 and not equal to 0 or 2: 有以下代码,我需要返回访问级别,该访问级别在0x30以下且不等于0或2:

int login() {
int accessLevel = 0xff;
char username[16];
char password[32];
printf("Username (max 15 characters): ");
gets(username);
printf("Password (max 31 characters): ");
gets(password);

if (!strcmp(username, "admin") && !strcmp(password, "{{ create_long_password() }}")) {
    accessLevel = 2;
} else if (!strcmp(username, "root") && !strcmp(password, "{{ create_long_password() }}")) {
    accessLevel = 0;
} else if (!strcmp(username, "artist") && !strcmp(password, "my-password-is-secret")) {
    accessLevel = 0x80;
}

return accessLevel;
}

I entered into the user name 16 'a' which reset the accessLevel to 0(and then added space which set access to 20 and gives me the desired output). 我输入了用户名16'a',将accessLevel重置为0(然后添加了将access设置为20并提供所需输出的空间)。 But, I would expect the buffer to overwrite password, not accessLevel since it's the "following memory". 但是,我希望缓冲区会覆盖密码,而不是accessLevel,因为它是“后续内存”。 I imagine I'm misunderstanding how the buffer works and would like an explanation. 我想我误解了缓冲区的工作原理,并希望得到一个解释。 Also, why did the 16th char reset to 0? 另外,为什么第16个字符重置为0?

Thanks in advance! 提前致谢!

Strcmp will match an arbitrary amount of chars, given by the first instance of null termination char. Strcmp将匹配空终止char的第一个实例给出的任意数量的char。 All string literals must be null terminated with \\0 所有字符串文字都必须为null并以\\ 0结尾

Using strncmp will only compare and amount of chars given in the arguements 使用strncmp只会比较争论中给出的字符数和数量

The ordering of local variables on the stack (assuming there is a stack) is not guaranteed to be in any particular order. 局部变量栈上(假设一个堆栈)的顺序不是保证在任何特定的顺序。 It depends largely on the types of the variables and the required alignment of each of them, and can change depending on the compiler or on optimization settings. 它在很大程度上取决于变量的类型以及每个变量的所需对齐方式,并且可以根据编译器或优化设置进行更改。

In this case, it sounds like the variables were pushed onto the stack in the order they appear in code, which would mean that their addresses would progressively decrease. 在这种情况下,听起来好像变量按照它们在代码中出现的顺序被压入堆栈,这意味着它们的地址将逐渐减少。

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

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