Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python print environment variable memory address

Is it possible to print my environment variable memory address ?

With gdb-peda i have a memory address looking like 0xbffffcd6 with searchmem and i know it's the right form. (0xbfff????) but gdb moved the stack with some other environment variable.

I would like with my python script to get this address and then do my trick and include my shellcode.

i tried (with Python):

print hex(id(os.environ["ENVVAR"]))
print memoryview(os.environ["ENVVAR"])

# output :
# 0xb7b205c0L
# <memory at 0xb7b4dd9c>

With Ruby :

puts (ENV['PATH'].object_id << 1).to_s(16)
# output :
# -4836c38c

If anyone have an idea, with python or ruby.

like image 982
albttx Avatar asked Jun 09 '16 12:06

albttx


People also ask

How do you print the memory address of a variable in Python?

It can be done in these ways:Using id() function. Using addressof() function. Using hex() function.

How do I print a memory address?

To print the memory address, we use '%p' format specifier in C. To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator.

How do I find the address of a variable?

To access the address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us the address of variable x.

How do you check the memory size of a variable in Python?

In python, the usage of sys. getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes.


1 Answers

The cpython built in function id() returns a unique id for any object, which is not exactly it's memory address but is as close as you can get to such.

For example, we have variable x. id(x) does not return the memory address of the variable x, rather it returns the memory address of the object that x points to.

There's a strict separation between 'variables' and 'memory objects'. In the standard implementation, python allocates a set of locals and a stack for the virtual machine to operate on. All local slots are disjoint, so if you load an object from local slot x onto the stack and modify that object, the "location" of the x slot doesn't change.

enter image description here http://docs.python.org/library/functions.html#id

like image 129
davidcondrey Avatar answered Sep 19 '22 12:09

davidcondrey