学逆向论坛

找回密码
立即注册

只需一步,快速开始

发新帖

2万

积分

41

好友

1157

主题

[Pwn] firstbin_dup

发表于 2020-9-2 09:49:52 | 查看: 1551| 回复: 0

相关题目:

源码
#include 
  #include 
  int main()
  {
  fprintf(stderr, "This file demonstrates a simple double-free attack with fastbins.\n");
  fprintf(stderr, "Allocating 3 buffers.\n");
  int *a = malloc(8);
  int *b = malloc(8);
  int *c = malloc(8);
  fprintf(stderr, "1st malloc(8): %p\n", a);
  fprintf(stderr, "2nd malloc(8): %p\n", b);
  fprintf(stderr, "3rd malloc(8): %p\n", c);
  fprintf(stderr, "Freeing the first one...\n");
  free(a);
  fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
  // free(a);
  fprintf(stderr, "So, instead, we'll free %p.\n", b);
  free(b);
  fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a);
  free(a);
  fprintf(stderr, "Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
  fprintf(stderr, "1st malloc(8): %p\n", malloc(8));
  fprintf(stderr, "2nd malloc(8): %p\n", malloc(8));
  fprintf(stderr, "3rd malloc(8): %p\n", malloc(8));
  }
  

输出结果

firstbin_dup

firstbin_dup
分析&调试
流程很简单,malloc(8)分别给a,b,c
然后free(a)->free(b)->free(a)
(若是直接free(a)*2会检测到并报错)
之后再malloc就会得到a、b、a chunk
两次malloc的指针指向了同一块chunk
调试:
  三次malloc之后:

firstbin_dup

firstbin_dup
第一次free:
可以看到free的chunk被链入了fastbin中,但是prev_size低位还是1(防止被合并)

firstbin_dup

firstbin_dup

firstbin_dup

firstbin_dup
第二次free:
可以看到第二次free的chunk从头进入了fastbin,fd指针指向了第一个chunk

firstbin_dup

firstbin_dup
第三次free:
可以看到a的chunk又一次从头部链入fastbin,并将其fd改成了chunk2的地址

firstbin_dup

firstbin_dup
之后三次malloc就依次从链表头取chunk(先进后出),第一次和第三次取出的chunk一样
总结
  • 依旧需要防止和top chunk合并
  • fastbin的use位一直是1
  • fastbin插入从头部插入
  • fastbin是先进后出的


温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
论坛交流群:672619046

小黑屋|手机版|站务邮箱|学逆向论坛 ( 粤ICP备2021023307号 )|网站地图

GMT+8, 2024-3-29 17:04 , Processed in 0.090494 second(s), 37 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表