【发布时间】:2020-03-30 15:44:32
【问题描述】:
我正在尝试了解格式字符串漏洞,我正在尝试通过修改易受攻击的代码打印出secret[1] 值,但我的试用版不起作用,我只看到secret[0]。我无法打印secret[1]。
这是控制台输出:
$ ./vul_prog
The variable secret's address is 0xbffff2f8 (on stack)
The variable secret's value is 0x 804fa88 (on heap)
secret[0]'s address is 0x 804fa88 (on heap)
secret[1]'s address is 0x 804fa8c (on heap)
Please enter a decimal integer
1
Please enter a string
%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x
bffff2fc.b7fd6b48.0.b7fff000.1.804fa88.252e7825.78252e78.2e78252e.252e7825.78252e78.2e78252e
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x55
$ ./vul_prog
The variable secret's address is 0xbffff2f8 (on stack)
The variable secret's value is 0x 804fa88 (on heap)
secret[0]'s address is 0x 804fa88 (on heap)
secret[1]'s address is 0x 804fa8c (on heap)
Please enter a decimal integer
1234567
Please enter a string
%x.%x.%x.%x.%x.%x.%s
Segmentation fault
代码:
/* vul_prog.c */
#include<stdio.h>
#include<stdlib.h>
#define SECRET1 0x44
#define SECRET2 0x55
int main(int argc, char *argv[])
{
char user_input[100];
int *secret;
int int_input;
int a, b, c, d; /* other variables, not used here.*/
/* The secret value is stored on the heap */
secret = (int *) malloc(2*sizeof(int));
/* getting the secret */
secret[0] = SECRET1; secret[1] = SECRET2;
printf("The variable secret's address is 0x%8x (on stack)\n",
(unsigned int)&secret);
printf("The variable secret's value is 0x%8x (on heap)\n",
(unsigned int)secret);
printf("secret[0]'s address is 0x%8x (on heap)\n",
(unsigned int)&secret[0]);
printf("secret[1]'s address is 0x%8x (on heap)\n",
(unsigned int)&secret[1]);
printf("Please enter a decimal integer\n");
scanf("%d", &int_input); /* getting an input from user */
printf("Please enter a string\n");
scanf("%s", user_input); /* getting a string from user */
/* Vulnerable place */
printf(user_input);
printf("\n");
/* Verify whether your attack is successful */
printf("The original secrets: 0x%x -- 0x%x\n", SECRET1, SECRET2);
printf("The new secrets: 0x%x -- 0x%x\n", secret[0], secret[1]);
return 0;
}
有什么问题吗?
【问题讨论】:
-
使用
%p打印指针值,而不是%8x。 -
%s将导致printf(user_input)访问内存,直到找到'\0'。看起来它在找到 nul 之前就出现了段错误。 -
感谢您的提醒,但我正在使用 printf(user_input) 来访问内存。
标签: c format-string