Skip to content
Advertisement

C code to access environment variables

I created an environment variable SHELLCODE which contains a 200-byte long NOP sled and a shellcode. It is stored at 0x7fffffffe285, but I’ll try to access 0x7fffffffe2e5, which is around the middle of the NOP sled.

Then I wrote the following code to try to access the variable.

JavaScript

I used gdb to see the memory

JavaScript

The pointer was clearly pointing to the middle of the NOP sled, and gdb could access and see what was at that address. But I keep getting this Segmentation fault error.

Is this because C programs are not allowed to access memory where environment variables are stored? If so, is there a way to allow it to access the memory?

I’m using Ubuntu 16.04 and gcc 5.4.0. Thanks in advance.

Advertisement

Answer

The getenv function is used to retrieve the values of environment variables:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement