2016-09-25 51 views
3

我试图在下面的代码中发现漏洞,我只是猜测它可以被缓冲区溢出利用,但不幸的是我不知道从哪里开始。关于缓冲区溢出的练习

#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个元素已被声明的部分。我不是在寻找确切的解决方案,但任何帮助将不胜感激

+0

由于在使用之前没有检查'index'的值,可以调用一个高于7的值来在一个意外的位置写入'value'来破坏堆栈。 [这是一篇关于基于堆栈的缓冲区溢出的文章](https://en.wikipedia.org/wiki/Stack_buffer_overflow)。 –

回答

1

在这种情况下,可能会导致缓冲区溢出,因为在将其转换为long后,您不检查index变量的值。后来你使用的语句:

array[index] = value; 

,但你已经声明array为:

int array[8]; 

这意味着你的array's指数将从0开始,可达7。因此,如果index大于7,则可能导致缓冲区溢出。