[英]Simple Buffer Overflow Exploit
我正在嘗試編寫一個非常簡單的程序,重點介紹如何使用緩沖區溢出漏洞繞過受密碼保護的系統。 代碼如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[15];
char tempbuff[15];
int pass = 0;
printf("\n Enter a password of length between 1 and 15 characters : \n");
gets(buff);
//strcpy("%s",buff);
printf("\n Enter your password : \n");
gets(tempbuff);
//strcpy("%s",tempbuff);
if(strcmp(tempbuff, buff))
{
printf ("\n Wrong Password \n");
}
else
{
printf ("\n Correct Password \n");
pass = 1;
}
if(pass)
{
/* Now Give root or admin rights to user*/
printf ("\n Root privileges given to the user \n");
}
return 0;
}
本質上,當第二次被要求輸入密碼時,我試圖通過輸入大於15個字符的字符串來將pass變量的值從0更改為1。 但是,到目前為止,我還無法做到這一點。 任何幫助將不勝感激!
我對代碼進行了一次更改,就可以在OS X中利用您的程序。 那是在tempbuff
之前定義pass
。 在tempbuff
之前聲明pass
意味着將pass
放在tempbuff
之后的堆棧上,因此溢出的tempbuff
將覆蓋pass
。 我能夠檢查lldb
(或gdb
)中pass
和tempbuff
的地址。
我還使用-fno-stack-protector
選項對其進行了編譯。
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[15];
int pass = 0;
char tempbuff[15];
printf("\n Enter a password of length between 1 and 15 characters : \n");
gets(buff);
printf("\n Enter your password : \n");
gets(tempbuff);
if(strcmp(tempbuff, buff))
{
printf ("\n Wrong Password \n");
}
else
{
printf ("\n Correct Password \n");
pass = 1;
}
if(pass)
printf ("\n Root privileges given to the user \n");
return 0;
}
編譯: gcc -Wall -Wextra -O0 -g -fno-stack-protector buf.c -o buf
這是輸入序列:
safepassword
1234567890123456
這是輸出:
$ ./buf < over
Enter a password of length between 1 and 15 characters :
warning: this program uses gets(), which is unsafe.
Enter your password :
Wrong Password
Root privileges given to the user
不能保證為局部變量分配內存的順序,也不能保證它們將位於連續的位置。 以下修改的代碼應在大多數系統中都可以使用。 它利用為結構元素分配連續的內存位置這一事實(還請注意,已更改了數組大小以避免填充。)
#include <stdio.h>
#include <string.h>
struct app {
char buff[16];
char tempbuff[16];
int pass;
};
int main(void)
{
struct app app;
app.pass = 0;
printf("\n Enter a password of length between 1 and 15 characters : \n");
gets(app.buff);
//strcpy("%s",buff);
printf("\n Enter your password : \n");
gets(app.tempbuff);
//strcpy("%s",tempbuff);
if(strcmp(app.tempbuff, app.buff))
{
printf ("\n Wrong Password \n");
}
else
{
printf ("\n Correct Password \n");
app.pass = 1;
}
if(app.pass)
{
/* Now Give root or admin rights to user*/
printf ("\n Root privileges given to the user \n");
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.