在利用這段代碼并發現你是否輸入了 16 個不同的數字 輸出后, 我試圖理解為什么會這樣。是因為記憶體位置嗎?
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(int argc, char *argv[]) {
int entry[6];
int results[6];
int i = 0, tmp = 0;
/* Generate power balls */
srand(time(NULL));
for (int i = 0; i < 6; i ) {
results[i] = rand() % 99;
}
printf("RULE: You are to enter a sequence of six two-digit numbers between 10 and 99. \n");
printf(" - The numbers should be separated by a single space. \n");
printf(" - The seventh number should be -1, indicating the completion of the sequence \n");
printf("Enter the numbers: \n");
while(tmp != -1) {
scanf("%d", &tmp);
if (tmp == -1) break;
entry[i] = tmp;
i ;
}
/* Check results */
int match = 0;
for (int i = 0; i < 6; i ) {
printf("The lottery number is: %d\n", results[i]);
printf("Your guess is: %d\n", entry[i]);
if (results[i] == entry[i]) {
match ;
}
}
if (match != 6){
printf("Unfortunately, there has been a mismatch! Better luck next time!\n");
}
else {
printf("Congratulations, all the numbers match! You have won a gazillion dollars \n");
}
return 0;
}
我試著找到每個陣列的記憶體位置:Entry / Results
但我不明白它們是如何相關的,即使它們是漏洞利用的一部分。
uj5u.com熱心網友回復:
在我的機器上,哪里sizeof (int)是 4,這些陣列間隔 32 個位元組對齊
(gdb) print &entry
$1 = (int (*)[6]) 0x7fffffffe7b0
(gdb) print &results
$2 = (int (*)[6]) 0x7fffffffe7d0
withresults被放置在 之后entry,并且它們之間有 8 個位元組的填充。
|entry |padding|results |stack
| 0 | 1 | 2 | 3 | 4 | 5 | P | P | 0 | 1 | 2 | 3 | 4 | 5 | ? | ? | ? |
i = 0 1 ... 6 7 8 9 ... 12 13 14 15 16
tmp = 10 12 ... 16 17 18 10 12 ... 16 17 18 -1
Until tmpis -1,i將繼續遞增,將 的值tmp寫入entry[i]。這最終會溢位entry,并開始寫入填充位元組,然后寫入results陣列。
在我的機器上,i, tmp, 和match放在陣列之前,陣列后面可能是金絲雀,當它們的值改變時會觸發堆疊保護。
只輸入 14 個值,而不是 16 個,然后-1,會導致results陣列被完全覆寫,而不會觸發任何堆疊保護。
不過,這確實都屬于Undefined Behavior的范圍。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533853.html
標籤:C部件数据库缓冲区溢出
上一篇:SSL不安全舊版重新協商已禁用
下一篇:無法理解此匯編代碼
