【发布时间】:2019-05-05 02:39:20
【问题描述】:
我有这段代码,它有一些漏洞,但我似乎无法利用它。
目前,这是我注意到的:
1) 如果 argv[1] = 3 且 argc = 3,则它会溢出并将 argv[2] 写入 "place_int_array" 函数中的 array[3] 的内存中。
2) 如果 argv[1]
3) 我们在 printf 函数中编写了 argv[0],它可以以某种方式被利用(根本没有设法利用它)。
这里是代码。 我放了一些 cmets,希望它是可读的。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int secretCode = 123;
void print_secret_code(){
//TODO - call this from somewhere...
printf("You get better at this stuff, ah?! Go get your treasure, the code is %d\n", secretCode);
}
// array, 3
void fill_array(int array[], size_t len){
unsigned int i;
for (i = 0; i < len; i++){
array[i] = i * 10;
printf("array[i]: %d, i: %d\n", array[i], i);
}
}
void place_int_array(int slot,int value){
//buffer size = 3*4=12 bytes?
int array[3];
//sizeof(array)=4*SAFES ( = 12 here), sizeof(array[0]) = 4 ==> fill_array(array, 12/4=3)
fill_array(array, sizeof(array)/sizeof(array[0]));
//vuln - when slot = 3.
if(slot>3) //we stop bad guys here
printf("safe number is greater than 3, out of bounds.\n");
else{
//vuln?
array[slot]=value;
printf("filled safe %d with %d.\n",slot,value);
}
return;
}
int main(int argc,char **argv){
if(argc!=3){
printf("Welcome to Alladin's magic cave!\n");
printf("Enter the secret number into the right safe and get the treasure cave entrance code!\n");
printf("syntax: %s [SAFE NUMBER] [SECRET NUMBER]\n",argv[0]);
//TEMP TEMP - for debugging only
printf("print_secret_code function = %p\n", print_secret_code);
}
else
//atoi("14s56")=>14, atoi("a14s56")=>0
place_int_array(atoi(argv[1]), atoi(argv[2]));
exit(0);
}
我希望以某种方式设法控制程序流以执行“print_secret_code”。我知道如何找到它的地址,只是找不到一种方法来利用该程序以使其进入该内存。
注意:我知道如何调试并让它打印变量的值。我在问如何利用代码本身来跳转到该函数。
【问题讨论】:
-
对我来说看起来像是一个破坏漏洞的堆栈,因为在 argv2 中需要这个负数。或许试试 argv3 中 print_secret_code 的地址
-
是的,它显然是一个整数溢出。我尝试将大于最大大小的参数传递给 argv[1] 和 argv[2],并将它们打印出来,但由于某种原因它们不会溢出。小于 int 最小值的值相同
-
尝试-2^63或-2^31左右的值
-
好吧,我注意到堆栈中place_int_array函数的返回地址距离堆栈中数组的地址仅6个字节(上面6个字节)。所以我设法覆盖了堆栈的一些地址,但是因为堆栈每次都在不同的地址中,所以我无法真正计算负 argv[1] 应该是多少(尽管它在 -2^31 左右)以便它覆盖array[6] 的地址。
-
您可以访问源代码,因此没有必要利用任何东西。数字是 123。
标签: c overflow integer-overflow exploit stack-smash