Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address Space Layout Randomization in C Compilers

If I am not mistaken, ASLR will make the local variables in C compilers have a different address each time I run the program. But when I tried it in Turbo C++ and Dev-CPP IDE, it just returns a similar address for local variables. The code i tried:

#include <stdio.h>
#include <conio.h>

int main()
{
 int x = 10;
 int *ptr = &x;
 printf("%d", ptr);
 getch();
 return 0;
}

Before, I thought the address of the local variables are the same because it is allocated in the same stack area and thus the same memory address. But when i found a thread here in stackoverflow about ASLR, it made me did these. I guess this is because of the compilers. Can anyone shed a light on this?

Edit:

Im using Windows 7.

like image 792
paul Avatar asked Aug 04 '14 07:08

paul


People also ask

What is Address Space Layout Randomization what is its purpose?

Address space layout randomization (ASLR) is a technique that is used to increase the difficulty of performing a buffer overflow attack that requires the attacker to know the location of an executable in memory.

How does address randomization work?

Address space layout randomization (ASLR) is a memory-protection process for operating systems (OSes) that guards against buffer-overflow attacks by randomizing the location where system executables are loaded into memory.

What is kernel address space layout randomization?

Address Space Layout Randomization (ASLR) has nothing to do with system I/O, but the internals of the Linux kernel itself. ASLR is a security feature which randomizes where various parts of a Linux application are loaded into memory. One of the things it can do is to change the load address of the C library.

Is ASLR enabled by default?

it's Off by default, when you turn it on, you will have to restart your device. Address space layout randomization (ASLR) is a computer security technique involved in preventing exploitation of memory corruption vulnerabilities.


2 Answers

It appears you are using windows.

Quoting from wikipedia

Microsoft's Windows Vista (released January 2007) and later have ASLR enabled for only those executables and dynamic link libraries specifically linked to be ASLR-enabled. For compatibility, it is not enabled by default for other applications. Typically, only older software is incompatible and ASLR can be fully enabled by editing a registry entry "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\MoveImages".

and

Host-based intrusion prevention systems such as WehnTrust and Ozone also offer ASLR for Windows XP and Windows Server 2003 operating systems. WehnTrust is open-source Complete details of Ozone's implementation is not available

Make sure you enable the ASLR to observe the expected behaviour.

like image 81
Mohit Jain Avatar answered Oct 06 '22 01:10

Mohit Jain


There are a few things that can influence whether your program uses ASLR or not. In most cases, your compiler/linker needs to ensure that the executable is relocatable and mark the executable as being ASLR-compatible. Then your OS needs to actually relocate it at load time.

Do your compilers support ASLR? If not, and you're using Windows (I'm guessing that you are based on the compilers that you're using), then you can try to force the OS to apply ASLR using EMET, which you can download from Microsoft.

like image 33
user3553031 Avatar answered Oct 06 '22 01:10

user3553031