Python - Setting / Getting Environment Variables and Addrs

10,481

Solution 1

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

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

Solution 2

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

Share:
10,481
tkokoszka
Author by

tkokoszka

I'm a software engineer at Google in Mountain View, California. I love programming and learning new programming languages. Some open source projects I've been involved in: AppScale - an open source implementation of the Google App Engine APIs. Runs App Engine apps written in Python, Java, Go, or PHP over Amazon EC2, Google Compute Engine, Eucalyptus, Xen, or KVM. Active Cloud DB - a software-as-a-service that exposes a REST API to any of the databases that AppScale supports (e.g., HBase, Cassandra, MongoDB) or the Datastore that App Engine supports. Neptune - a domain specific language that automatically configures and deploys high performance computing apps over AppScale. Run your MPI, MapReduce, X10, and other codes automatically over EC2 without needing to know how to start them and configure them!

Updated on June 04, 2022

Comments

  • tkokoszka
    tkokoszka almost 2 years

    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?

    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:

    • 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?

  • Russell Smith
    Russell Smith over 15 years
    I realize this question was accepted but I don't see how it answers the root problem, namely "I need to set an environment variable in python". How will depending on the id returning a memory address solve the problem?
  • JimB
    JimB over 15 years
    I assumed he was referring to python variables, only because the "the memory address" of a shell environment variable doesn't really make sense.
  • tkokoszka
    tkokoszka over 15 years
    I actually needed a way to get the memory address of where the shell environment variable is so I can feed it to a different program, which you can do with getenv() in C.
  • JimB
    JimB over 15 years
    I'm a little confused - even in c, getenv only returns a string. char * getenv (const char *name)
  • Ali Afshar
    Ali Afshar over 15 years
    Can't you just send a copy of the string to the "different program", do you really need the address?
  • tkokoszka
    tkokoszka over 15 years
    Yes, 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. But I need to find a way to automate this in Python, hence this question.
  • tkokoszka
    tkokoszka over 15 years
    @Jim: getenv returns a char*, which you can cast to void* and then print to see the memory location where the env var is.
  • JimB
    JimB over 15 years
    Ok, I just thought there was a strcopy going on (I don't do a lot of C though). I wouldn't have guessed that the string in python's os.environ dict points to the actual char * in the environment though.
  • tkokoszka
    tkokoszka over 15 years
    From looking it over, it seems like it actually doesn't. It points to the Python representation, but I had accepted your answer because it looks like I can use it for a different purpose and it is technically correct.