在以下代码中:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_authentication(char *password) {
char password_buffer[16];
int auth_flag = 0;
strcpy(password_buffer, password);
if(strcmp(password_buffer, "brillig") == 0)
auth_flag = 1;
if(strcmp(password_buffer, "outgrabe") == 0)
auth_flag = 1;
return auth_flag;
}
int main (int argc, char *argv[]){
if(argc < 2){
printf("Usage: %s <password>\n", argv[0]);
exit(0);
}
if(check_authentication(argv[1])){
printf("\n-=-=-=-=-=-=-==-=-=-\n");
printf(" Access Granted\n");
printf("-=-=-=-=-=-=-==-=-=-\n");
}
else {
printf("Access Denied\n");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行诸如"AAAAAAAAAAAAAAAAAAAA"之类的东西,某些东西会溢出并导致程序在授予访问权限时运行.我很困惑,因为当我运行gdb调试器时,auth_flag在内存中的password_buffer之前,它从未溢出.
编辑:我知道文本不适合缓冲区,但我正在试验缓冲区溢出以及如何以受控方式利用它们.是的,我们可以让阵列更大,但这不是重点
我想知道是否有人能够告诉我为什么会发生这种情况/什么是溢出导致这种情况.
AAAAAAAAAAAAAAAAAAAA的大小为20.您将使用strcpy它将其复制到大小为16的字符数组.尝试增加password_buffer大小.
为避免溢出,目标指向的数组大小应足够长,以包含与源相同的C字符串(包括终止空字符),并且不应在内存中与源重叠.
http://www.cplusplus.com/reference/cstring/strcpy/