我正在通过此链接在线阅读以下代码:http : //www.cse.scu.edu/~tschwarz/coen152_05/Lectures/BufferOverflow.html
我对%p在此行中的用法感到困惑:
printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
摘录自以下代码段:
/*
StackOverrun.c
This program shows an example of how a stack-based
buffer overrun can be used to execute arbitrary code. Its
objective is to find an input string that executes the function bar.
*/
#pragma check_stack(off)
#include <string.h>
#include <stdio.h>
void foo(const char* input)
{
char buf[10];
printf("My stack looks like:\n%p\n%p\n%p\n%p\n%p\n% p\n\n");
strcpy(buf, input);
printf("%s\n", buf);
printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}
void bar(void)
{
printf("Augh! I've been hacked!\n");
}
int main(int argc, char* argv[])
{
//Blatant cheating to make life easier on myself
printf("Address of foo = %p\n", foo);
printf("Address of bar = %p\n", bar);
if (argc != 2)
{
printf("Please supply a string as an argument!\n");
return -1;
}
foo(argv[1]);
return 0;
}
我知道%p是指针的格式化程序,但是为什么在格式化程序后面没有值?实际在这里打印什么值?如果正在打印提供给foo函数的参数的地址,那么为什么有5个'%p',为什么不是所有'%p'格式都相同?
非常感谢。
这利用了未定义的行为。
通过故意不向printf提供值,va_arg
将要从要打印的堆栈中拉出任意值。这是不正确的代码。实际上,此代码段似乎试图解释黑客技术,这些技术通常利用未定义行为发生时的故障。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句