关于缓冲区溢出的练习

Exercise about Buffer Overflow

我试图在下面的代码中找到漏洞,我只是猜测它可以通过缓冲区溢出被利用,但不幸的是我不知道从哪里开始。

#include <stdio.h>
#include <stdlib.h>

#define TABLELEN 7
int table[] = {2, 3, 5, 7, 11, 13, 17};

void loadTable(int *hashtable) {
int i;
for (i = 0; i < TABLELEN; i++) {
hashtable[i] = table[i];
  } 
}

int main(int argc, char *argv[])
{
 int array[8];
 int index;
 int value;
 if (argc < 3) {
    fprintf(stderr, "Not enough args\n");
    return -1;
 }
 loadTable(array);
 index = (int) strtol(argv[1], NULL, 10);
 value = (int) strtoul(argv[2], NULL, 16);
 printf("Updating table value at index %d with %d: previous value was %d\n",
 index, value, array[index]); 
 array[index] = value;
 printf("The updated table is:\n");
 for (index = 0; index < TABLELEN; index++) {
    printf("%d: %d\n", index, array[index]);
 }
  return 0;
 }

我正在尝试寻找方法来利用数组大小​​为 8 但仅声明了 7 个元素的部分。我不是在寻找确切的解决方案,但我们将不胜感激

在这种情况下可能会导致缓冲区溢出,因为在将 index 变量转换为 long 后,您没有检查它的值是什么。稍后您使用语句 :

array[index] = value;

但您已将 array 声明为 :

int array[8];

这意味着您的 array's 索引将从 0 开始并达到 7。所以如果index大于7,就会造成缓冲区溢出