Skip to content

Instantly share code, notes, and snippets.

@superkojiman
Created March 26, 2015 15:09
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save superkojiman/6a6e44db390d6dfc329a to your computer and use it in GitHub Desktop.
Save superkojiman/6a6e44db390d6dfc329a to your computer and use it in GitHub Desktop.
Get environment variable address
/*
* I'm not the author of this code, and I'm not sure who is.
* There are several variants floating around on the Internet,
* but this is the one I use.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *ptr;
if(argc < 3) {
printf("Usage: %s <environment variable> <target program name>\n", argv[0]);
exit(0);
}
ptr = getenv(argv[1]); /* get env var location */
ptr += (strlen(argv[0]) - strlen(argv[2]))*2; /* adjust for program name */
printf("%s will be at %p\n", argv[1], ptr);
}
@manuelbua
Copy link

Credits for this goes to Jon Erickson :)

@yaojingguo
Copy link

The code is from Page 147 and 148 of Hacking: The Art of Exploitation, 2nd Edition .

@tomtastic
Copy link

Also on page 38 and 39 of Hacking: The Art of Exploitation, 1st Edition .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment