這項計劃與我的機器上的root權限運行的一個基本的堆棧溢出,我需要執行以下代碼堆棧溢出攻擊,並獲取root權限:創建使用IDA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/sha.h>
void sha256(char *string, char outputBuffer[65])
{
unsigned char hash[SHA256_DIGEST_LENGTH];
int i = 0;
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(hash, &sha256);
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
int password_check(char *userpass)
{
char text[20] = "thisisasalt";
unsigned int password_match = 0;
char output[65] = { 0, };
// >>> hashlib.sha256("Hello, world!").hexdigest()
char pass[] = "315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3";
text[0] = 'a';
text[1] = 't';
text[2] = 'j';
text[3] = '5';
text[4] = '3';
text[5] = 'k';
text[6] = '$';
text[7] = 'g';
text[8] = 'f';
text[9] = '[';
text[10] = ']';
text[11] = '\0';
strcat(text, userpass);
sha256(text, output);
if (strcmp(output, pass) == 0)
{
password_match = 1;
}
return (password_match == 1);
}
int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Usage: %s <pass> <command>\n", argv[0]);
exit(1);
}
if (strlen((const char *) argv[1]) > 10)
{
printf("Error: pasword too long\n");
exit(1);
}
if (password_check(argv[1]))
{
printf("Running command as root: %s\n", argv[2]);
setuid(0);
setgid(0);
system(argv[2]);
}
else
{
printf("Authentication failed! This activity will be logged!\n");
}
return 0;
}
所以我試着分析使用IDA的程序看到文本段從低位地址到高位地址,高於我看到的數據,然後是bss,最後是外部命令。
現在據我所知,堆棧應該剛好在上面,但我不確定如何查看它,我應該如何查看堆棧才能知道我在寫什麼? (難道我甚至需要它還是我完全無能?)
第二個問題是考慮輸入的長度,我該如何解決這個檢查代碼得到:
if (strlen((const char *) argv[1]) > 10)
{
printf("Error: pasword too long\n");
exit(1);
}
我可以以某種方式給字符串通過引用到程序中?如果是這樣,我該怎麼做? (同樣,希望我沒有完全無能)
哇男人太感謝你了,我想通了一些出來單獨包括溢出是哪裏,但我相信我在寫在寄信人地址,而不是過度的密碼!它的偉大工程,謝謝! – 2013-04-09 10:32:40
@MehMeh:不客氣。 – DCoder 2013-04-09 19:29:39