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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/310118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:52:35  来源:igfitidea点击:

Python - Setting / Getting Environment Variables and Addrs

pythonlinuxenvironment-variables

提问by Chris Bunch

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 中完成?

采纳答案by JimB

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

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

回答by Bryan Oakley

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 来设置环境变量。

回答by Ali Afshar

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

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