[英]How to 'hack' with buffer overflow attack
#include <stdio.h>
char user_name[20] = " ";
char password[20] = " ";
char users[][2][20] =
{ { "root", "98765" },
{ "me", "hello" },
{ "abc", "password" },
{ "", "" }
};
int check_name()
{
int i;
gets(user_name);
gets(password);
for(i=0; users[i][0][0] != 0; i++)
{
if(strcmp(user_name, users[i][0]) == 0 &&
strcmp(password, users[i][1]) == 0)
return 1;
}
return 0;
}
void logon()
{
printf("Welcome! \n");
exit(1);
}
void reject()
{
printf("Connection closed !\n");
printf("Real username: \n");
printf(users[0][0]);
printf("\n");
printf("Real password: \n");
printf(users[0][1]);
printf("\n");
exit(0);
}
main()
{
if(check_name())
logon();
else
reject();
}
嘿伙計們,我有點理解緩沖區溢出背后的理論,但我似乎無法讓它在這里工作。
請注意,我將額外的printf
添加到 output 真實用戶名和密碼,以查看我在 memory 中覆蓋了多少。
我嘗試寫一個隨機字母x
。
首先我做了:用戶名 = 20 x
s,密碼 = 60 x
s,output 是:
Real user:
xxxxxxxxxxxxxxxxxxxxxxxxxxxx // 28 x's
Real pass:
xxxxxxxx // 8 x's
所以我在密碼中添加了 12 x
s 以使其最大大小為 20,因此密碼 = 72 x
s 並且 output 為:
// with input user = 20 x's and pass = 72 x's
Real user:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // 40
Real pass:
xxxxxxxxxxxxxxxxxxxx // 20
最終我認為通過將我的輸入用戶名更新為 40 x
s 它會起作用,但它沒有。 Output 與上一個相同(真實用戶為 40 x,真實密碼為 20 x),但仍然無法“破解”它。
我不確定此時該做什么。 提前致謝!
您必須了解什么構成了 C 中的字符串。像printf
這樣的東西將繼續從字符串中打印字節,直到它遇到NULL
字符。 按照您的方式在程序中嵌入字符串 ( foo="barbaz"
) 會自動包含一個 null 字符。
當程序將字符串讀入固定長度的緩沖區,但輸入大於緩沖區時,就會發生緩沖區溢出錯誤。 例如,如果我有char username[8]
,但用戶輸入supercalifragilisticexpalidocious
。
顯然,輸入大於緩沖區,如果程序允許所有輸入,它將繼續覆蓋 memory 中超出用戶名的內容。
在這種情況下,足夠聰明的黑客可以通過某種方式覆蓋 memory,使她能夠訪問她通常無法訪問的部分程序或數據。
因此,要回答您的問題,由於您是如何使用在代碼的編譯部分中完全定義的字符串構建程序的,因此您不會出現緩沖區溢出錯誤。
嘗試接受無限的用戶輸入,然后放入比緩沖區可以接受的更多的數據,看看結果 memory 塊會發生什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.