2016-09-25 163 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,則可能導致緩衝區溢出。