![](/img/trans.png)
[英]Why aren't consecutively-defined variables in increasing memory locations?
[英]Why aren't memory addresses of C variables fixed?
我是一名CS本科生,剛剛了解虛擬內存。 我對以下程序進行了實驗。
#include<stdio.h>
int ready0;
int main(void) {
int ready;
printf("ready0 at %p, ready at %p. \n", (void*)&ready0, (void*)&ready);
}
我認為,由於該程序僅處理虛擬內存,因此在程序看來,它是計算機上唯一運行的進程。 我還看了看反匯編的代碼,它看起來很確定。 因此,如果我多次運行該程序,結果應該是相同的。 但是,實驗表明事實並非如此。 為什么實驗與我預期的不同? 是什么導致每次我運行該程序時結果都不同?
如果您有興趣,這里是Mac OS X Yosemite上的一些實驗結果。
$ ./sp
ready0 at 0x102b18018, ready at 0x7fff5d0e876c.
$ ./sp
ready0 at 0x107c09018, ready at 0x7fff57ff776c.
$ ./sp
ready0 at 0x10aa9c018, ready at 0x7fff5516476c.
$ ./sp
ready0 at 0x10d56d018, ready at 0x7fff5269376c.
$ ./sp
ready0 at 0x10da1c018, ready at 0x7fff521e476c.
$ ./sp
ready0 at 0x109aff018, ready at 0x7fff5610176c.
$ ./sp
ready0 at 0x107c31018, ready at 0x7fff57fcf76c.
$ ./sp
ready0 at 0x10fab1018, ready at 0x7fff5014f76c.
這是由於地址空間布局隨機化。
從Wiki:
*
地址空間布局隨機化(ASLR)是一種計算機安全方法,它涉及在進程的地址空間中隨機安排關鍵數據區域的位置,通常包括可執行文件的基礎以及庫,堆和堆棧的位置。
好處
地址空間隨機化使攻擊者更難預測目標地址,從而阻礙了某些類型的安全攻擊。 例如,嘗試執行返回libc攻擊的攻擊者必須找到要執行的代碼,而其他嘗試執行注入到堆棧中的shellcode的其他攻擊者則必須先找到堆棧。 在這兩種情況下,相關的內存地址都會被攻擊者遮蓋。 必須猜測這些值,並且由於應用程序崩潰,通常無法恢復錯誤的猜測。
*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.