Python - 设置/获取环境变量和地址

时间:2021-07-29 11:53:45

I need to set an environment variable in Python and find the address in memory where it is located. Since it's on Linux, I don't mind about using libraries that only work consistently on Linux (if that's the only way). How would you do this?

我需要在Python中设置一个环境变量,并在内存中找到它所在的地址。因为它在Linux上,我不介意使用只在Linux上一致工作的库(如果这是唯一的方法)。你会怎么做?

Edit: The scope of the problem is as follows: I'm trying to hack a program for class, and essentially I'm putting my shellcode into an environment variable and then overwriting one byte on the victim code with the address of my environment variable. I need to find a way to automate this in Python, so my question is two-fold:

编辑:问题的范围如下:我正在尝试破解类的程序,基本上我将我的shellcode放入环境变量,然后用我的环境变量的地址覆盖受害者代码上的一个字节。我需要找到一种在Python中自动化的方法,所以我的问题有两个:

  • Is there a way to get the address in memory of an environment variable?

    有没有办法在环境变量的内存中获取地址?

  • Can this only be done in bash/C or can I do it purely in Python?

    这只能在bash / C中完成,还是纯粹用Python完成?

3 个解决方案

#1


1  

The built in function id() returns a unique id for any object, which just happens to be it's memory address.

内置函数id()为任何对象返回一个唯一的id,恰好是它的内存地址。

http://docs.python.org/library/functions.html#id

#2


4  

For accessing and setting environment variables, read up on the os.environ dictionary. You can also use os.putenv to set an environment variable.

要访问和设置环境变量,请阅读os.environ字典。您还可以使用os.putenv设置环境变量。

#3


0  

Pass the address itself in an environment variable, and just read it with os.getenv().

将地址本身传递给环境变量,然后使用os.getenv()读取它。

#1


1  

The built in function id() returns a unique id for any object, which just happens to be it's memory address.

内置函数id()为任何对象返回一个唯一的id,恰好是它的内存地址。

http://docs.python.org/library/functions.html#id

#2


4  

For accessing and setting environment variables, read up on the os.environ dictionary. You can also use os.putenv to set an environment variable.

要访问和设置环境变量,请阅读os.environ字典。您还可以使用os.putenv设置环境变量。

#3


0  

Pass the address itself in an environment variable, and just read it with os.getenv().

将地址本身传递给环境变量,然后使用os.getenv()读取它。