64位Linux栈粉碎教程:第1部分使用Get environment variable address gist获取环境变量地址。先决条件是首先通过echo 0 > /proc/sys/kernel/randomize_va_space
.
要点的内容是:
/*
* 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);
}
为什么*2
用来调整节目名称?
我的猜测是程序名称在堆栈上方保存了两次。
来自https://lwn.net/Articles/631631/的下图提供了更多详细信息:
------------------------------------------------------------- 0x7fff6c845000
0x7fff6c844ff8: 0x0000000000000000
_ 4fec: './stackdump\0' <------+
env / 4fe2: 'ENVVAR2=2\0' | <----+
\_ 4fd8: 'ENVVAR1=1\0' | <---+ |
/ 4fd4: 'two\0' | | | <----+
args | 4fd0: 'one\0' | | | <---+ |
\_ 4fcb: 'zero\0' | | | <--+ | |
3020: random gap padded to 16B boundary | | | | | |
在此图中,./stackdump
用于执行程序。所以我可以看到程序名称./stackdump
在环境字符串上方保存一次。如果./stackdump
从 Bash shell 启动,Bashell 会将其保存在带有 key 的环境字符串中_
:
_
(下划线。)在 shell 启动时,设置为用于调用在环境或参数列表中传递的 shell 或正在执行的 shell 脚本的绝对路径名。随后,在展开后展开到前一个命令的最后一个参数。还设置为用于调用执行的每个命令并放置在导出到该命令的环境中的完整路径名。检查邮件时,此参数保存邮件文件的名称。
环境字符串位于堆栈之上。所以程序名称在堆栈上方再次保存。