Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
heaplens — # CMPT733 网络安全实验室 II 项目:受 CVE-2021-3156 启发的堆利用 GDB 插件 | Kitploit
工具/GitHubGitHub/ypl6/heaplens
漏洞分析漏洞利用调试器二进制分析学习与教育
GitHubypl6/heaplens

heaplens

# CMPT733 网络安全实验室 II 项目:受 CVE-2021-3156 启发的堆利用 GDB 插件

查看仓库
24年前尚未审核

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享

heaplens

🔎 Heaplens

CMPT733 网络安全实验室 II 项目

📂 仓库结构

root@kitploit:~
.
├── README.md
├── heaplens.png
├── heaplens.py
├── logs
│   ├── sudoedit-#.txt
│   └── ...
└── tests
    ├── Makefile
    ├── env-in-heap
    └── ...
  • README.md:就是本文件!
  • heaplens.py:主脚本
  • logs/:包含我们在开发和测试过程中捕获的一些示例日志
  • tests/:包含一些用于测试的二进制文件(及其源代码)

📦 环境

以下是我们用于测试该漏洞利用的工具(及版本)列表。它们应该已经安装在虚拟机中。如果缺少任何内容,请按照下面的说明操作。

依赖项

  • Ubuntu 20.04.4(64 位)
  • python 3.8.10
  • glibc 2.34
  • gdb 9.2
  • gef 2022.01
    • 如果 gef 在 root 下未启用,请查看 /root/.gdbinit
    • 如果该文件为空,只需复制 ~/.gdbinit 中的内容即可
  • sudo 依赖项(用于在 sudo 上运行 heaplens)

    需要带有调试功能的 sudo 二进制文件。虚拟机中已安装带有调试符号的预编译二进制文件。

    root@kitploit:~
    cyberlab@ubuntu:~$ which sudo
    /usr/local/bin/sudo
    
    cyberlab@ubuntu:~$ sudo --version
    Sudo version 1.8.31
    Sudoers policy plugin version 1.8.31
    Sudoers file grammar version 46
    Sudoers I/O plugin version 1.8.31
    
    cyberlab@ubuntu:~$ sudoedit -s /
    sudoedit: /: not a regular file # sudo 存在漏洞
    

    📝 加载插件

    此操作已在虚拟机中完成。如果配置有误,您可以按照以下说明加载 Heaplens。

    选项 1

    启动 gef 并编辑配置:

    root@kitploit:~
    gef➤ gef config gef.extra_plugins_dir <路径>/heaplens
    gef➤ gef save
    gef➤ q
    

    这将更新文件 ~/.gef.rc 或 /root/.gef.rc(在 root 下)。

    选项 2

    在 ~/.gdbinit 或 /root/.gdbinit 中添加以下行:

    root@kitploit:~
    source <路径>/heaplens/heaplens.py
    

    💡 使用方法

    建议在调试特权程序时以 root 身份运行 gdb:

    root@kitploit:~
    $ sudo su
    # gdb
    

    所有命令均提供帮助信息。您可以在 GDB 中通过 <command> -h 访问它们。

    heaplens

    从内存分配和释放函数(包括 malloc、realloc、calloc 和 free)收集堆信息。

    其核心思想是:通过挂钩这些函数,我们可以自动化检查和更新调用栈以及内存分配的返回地址的过程。通过检查跟踪记录,我们可以查看哪个块是由某个 C 文件中的 foo() 分配的,并进一步调查(例如检查相邻块)以找到适合堆漏洞利用的目标。

    它还支持在用户对中间堆布局感兴趣时添加自定义断点。

    该命令本身输出不多,您需要使用 heaplens-dump 来打印结果。

    root@kitploit:~
    heaplens -h
    usage: [-h] [-b BREAKPOINT] [-v]
    
    Collect heap info from memory (de)allocation functions.
    
    optional arguments:
      -h, --help            show this help message and exit
      -b BREAKPOINT, --breakpoint BREAKPOINT
                            stop the executions here (execute br {breakpoint} in gdb) (default: None)
      -v, --verbose         increase output verbosity (default: False)
    

    示例输出:

    root@kitploit:~
    gef➤  file sudoedit
    gef➤  heaplens -b set_cmnd -- -s '\\' $(python3 -c 'print("A"*65535)')
    ----------------------------
    Initializing Heaplens
    ----------------------------
    Temporary breakpoint 1 at 0x5840: file ../../src/src/sudo.c, line 136.
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    usage: sudoedit [-AknS] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ...
    [Inferior 1 (process 82934) exited with code 01]
    Setting breakpoint at set_cmnd...
    Function "set_cmnd" not defined.
    Breakpoint 2 (set_cmnd) pending.
    Hooking free function...
    Breakpoint 3 at 0x7f25cc163700: free. (2 locations)
    Hooking malloc function...
    Breakpoint 4 at 0x7f25cc163110: malloc. (2 locations)
    Hooking realloc function...
    Breakpoint 5 at 0x7f25cc163eb0: realloc. (2 locations)
    Hooking calloc function...
    Breakpoint 6 at 0x7f25cc164b40: calloc. (2 locations)
    Running -s '\' $(python3 -c 'print("A"*65535)')...
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    
    Breakpoint 2, set_cmnd () at ../../../src/plugins/sudoers/sudoers.c:804
    804	../../../src/plugins/sudoers/sudoers.c: No such file or directory.
    Removing breakpoints from mem_bkps...
    

    heaplens-dump

    转储 Heaplens 日志。我们提供将结果写入文件、以 JSON 格式输出以及按地址对块进行排序的选项。在转储中,每个块都会以更易读的方式记录其地址、大小、回溯和相关的内存分配函数。

    root@kitploit:~
    heaplens-dump -h
    usage: [-h] [-o OUTPUT] [--json] [-s]
    
    Dump Heaplens logs. Writes to stdout by default.
    
    optional arguments:
      -h, --help            show this help message and exit
      -o OUTPUT, --output OUTPUT
                            write to file at path {output} (default: None)
      --json                dump in json (default: False)
      -s, --sort            sort the chunks by their addresses (default: False)
    

    示例输出:

    root@kitploit:~
    gef➤  heaplens-dump
    ----------------------------
    Dumping...
    ----------------------------
    
    ...
    
    [malloc] Chunk 3 @ 0x55f3908ed2a0 | size 0xc
    Trace:
    #0  __GI___strdup (s=0x7fff6fb3b8dc "en_US.UTF-8") at strdup.c:44
    #1  0x00007f285df363c5 in _nl_load_locale_from_archive (category=category@entry=0xc, namep=namep@entry=0x7fff6fb29f50) at loadarchive.c:464
    #2  0x00007f285df351fe in _nl_find_locale (locale_path=0x0, locale_path_len=0x0, category=category@entry=0xc, name=name@entry=0x7fff6fb29f50) at findlocale.c:152
    #3  0x00007f285df34925 in __GI_setlocale (locale=<optimized out>, category=<optimized out>) at setlocale.c:337
    #4  __GI_setlocale (category=<optimized out>, locale=<optimized out>) at setlocale.c:217
    #5  0x000055f3901d5965 in main (argc=0x4, argv=0x7fff6fb2a298, envp=0x7fff6fb2a2c0) at ../../src/src/sudo.c:1430
    
    ...
    
    Dump complete.
    

    heaplens-chunks

    输出 gef 中 heap chunks 的略微修改版本。

    此命令通过整合 heap bins(列出 glibc bins 中先前分配和释放的块)中关于空闲块的信息,扩展了 GEF 的 heap chunks(显示块的地址、标志、大小和元数据)。GEF 提供了有助于堆检查的功能(如我们提到的两个),但将两者结合起来很繁琐。从高层来看,此命令从后者收集空闲块的地址,并在前者的结果中标记它们。

    root@kitploit:~
    heaplens-chunks -h
    usage: [-h] [--nocolor]
    
    A modified `heap chunks` with info about free chunks.
    
    optional arguments:
      -h, --help  show this help message and exit
      --nocolor   disable ANSI color codes
    

    示例输出:

    root@kitploit:~
    gef➤  heaplens-chunks
    Showing current heap info with freed chunks:
    
    ...
    
    Chunk(addr=0x55dd7f91ddb0, size=0xed0, flags=PREV_INUSE)  ←  free chunk
        [0x000055dd7f91ddb0     e0 fb ba e1 f8 7f 00 00 e0 fb ba e1 f8 7f 00 00    ...............]
    Chunk(addr=0x55dd7f91ec80, size=0x50, flags=! PREV_INUSE)
        [0x000055dd7f91ec80     2f 75 73 72 2f 6c 6f 63 61 6c 2f 73 62 69 6e 3a    /usr/local/sbin]
    Chunk(addr=0x55dd7f91ecd0, size=0x4010, flags=PREV_INUSE)  ←  free chunk
        [0x000055dd7f91ecd0     f0 02 bb e1 f8 7f 00 00 f0 02 bb e1 f8 7f 00 00    ...............]
    Chunk(addr=0x55dd7f922ce0, size=0x50, flags=! PREV_INUSE)
        [0x000055dd7f922ce0     90 02 91 7f dd 55 00 00 00 00 00 00 00 00 00 00    .....U.........]
    Chunk(addr=0x55dd7f922d30, size=0x2da0, flags=PREV_INUSE)  ←  free chunk
        [0x000055dd7f922d30     d0 02 bb e1 f8 7f 00 00 d0 02 bb e1 f8 7f 00 00    ...............]
    Chunk(addr=0x55dd7f925ad0, size=0x110, flags=! PREV_INUSE)  ←  free chunk
        [0x000055dd7f925ad0     00 00 00 00 00 00 00 00 10 f0 90 7f dd 55 00 00    .............U.]
    Chunk(addr=0x55dd7f925be0, size=0x30, flags=PREV_INUSE)
        [0x000055dd7f925be0     00 00 00 00 00 00 00 00 d2 78 b7 7e dd 55 00 00    .........x.~.U.]
    Chunk(addr=0x55dd7f925c10, size=0x20, flags=PREV_INUSE)
        [0x000055dd7f925c10     65 6e 5f 55 53 2e 55 54 46 2d 38 00 00 00 00 00    en_US.UTF-8....]
    Chunk(addr=0x55dd7f925c30, size=0xa3e0, flags=PREV_INUSE)  ←  top chunk
    

    heaplens-clear

    清除 heaplens 收集和使用的所有内部日志/数据。

    root@kitploit:~
    heaplens-clear -h
    usage: [-h] [-v]
    
    Clear Heaplens logs.
    
    optional arguments:
      -h, --help     show this help message and exit
      -v, --verbose  increase output verbosity
    

    heaplens-list-env

    列出存储在堆中并被释放的环境变量。当您想要进行堆整理(heap grooming)时,此命令特别有用,因为这些变量可能会影响堆布局。

    root@kitploit:~
    heaplens-list-env -h
    usage: [-h] [-v] [--prefix PREFIX] [--suffix SUFFIX] [-b BREAKPOINT] [-s SKIP]
    
    List environment variables that might affect the heap layout.
    
    optional arguments:
      -h, --help            show this help message and exit
      -v, --verbose         increase output verbosity
      --prefix PREFIX       environment variable value prefix
      --suffix SUFFIX       environment variable value suffix
      -b BREAKPOINT, --breakpoint BREAKPOINT
                            stop the executions here (execute br {breakpoint} in gdb)
      -s SKIP, --skip SKIP  skip this environment variable
    

    示例输出:

    root@kitploit:~
    gef➤  file sudoedit
    Reading symbols from sudoedit...
    gef➤  heaplens-list-env -s LC_ALL -b set_cmnd --prefix C.UTF-8@ -- -s \\ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    
    ...
    
    1st execution. Found following environment variable:
    ['LOCPATH', 'LC_ALL', 'LC_IDENTIFICATION', 'LANG', 'LC_MEASUREMENT', 'LC_TELEPHONE', 'LC_ADDRESS', 'LC_NAME', 'LC_PAPER', 'LC_MESSAGES', 'LC_MONETARY', 'LC_COLLATE', 'LC_TIME', 'LC_NUMERIC', 'LC_CTYPE', 'TZ', 'SHELL', 'LANGUAGE']
      
    ...
    
    2nd execution. Possible environment variables for heap grooming:
    ['LC_IDENTIFICATION', 'LC_COLLATE', 'TZ', 'LC_TIME', 'LANGUAGE', 'LC_NAME', 'LOCPATH', 'LC_MESSAGES', 'LC_NUMERIC', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MONETARY', 'LC_MEASUREMENT', 'LC_CTYPE', 'LC_PAPER']
    

    🛠 测试用例

    构建测试用例脚本(已在虚拟机中构建)

    root@kitploit:~
    $ cd tests
    $ make all
    

    转储 tests/heap-dump 堆布局

    root@kitploit:~
    gef➤  file tests/heap-dump
    gef➤  heaplens -b breakme
    gef➤  heaplens-dump
    

    将 sudoedit 堆布局转储到 output.txt

    root@kitploit:~
    gef➤  file sudoedit
    gef➤  heaplens -b set_cmnd -- -s \\ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    gef➤  heaplens-dump -o output.txt
    

    列出 tests/env-in-heap 中用于堆整理的环境变量

    root@kitploit:~
    gef➤  file tests/env-in-heap
    gef➤  heaplens-list-env
    
    ...
    
    ----------------------------
    2nd execution. Possible environment variables for heap grooming:
    ['ENV_IN_HEAP']
    ----------------------------
    

    列出 sudoedit 中用于堆整理的环境变量

    root@kitploit:~
    gef➤  file sudoedit
    gef➤  heaplens-list-env -s LC_ALL -b set_cmnd --prefix C.UTF-8@ -- -s \\ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    
    ...
    
    ----------------------------
    1st execution. Found following environment variable:
    ['LOCPATH', 'LC_ALL', 'LC_IDENTIFICATION', 'LANG', 'LC_MEASUREMENT', 'LC_TELEPHONE', 'LC_ADDRESS', 'LC_NAME', 'LC_PAPER', 'LC_MESSAGES', 'LC_MONETARY', 'LC_COLLATE', 'LC_TIME', 'LC_NUMERIC', 'LC_CTYPE', 'TZ', 'SHELL', 'LANGUAGE']    
    ----------------------------
    
    ...
    
    ----------------------------
    2nd execution. Possible environment variables for heap grooming:
    ['LC_CTYPE', 'LC_PAPER', 'LC_MONETARY', 'TZ', 'LC_ADDRESS', 'LC_MEASUREMENT', 'LC_IDENTIFICATION', 'LC_COLLATE', 'LC_NUMERIC', 'LC_MESSAGES', 'LC_TIME', 'LANGUAGE', 'LC_NAME', 'LOCPATH', 'LC_TELEPHONE']
    ---------------------------- 
    

    🚨 已知问题

    请参阅 Issues 页面了解更多详情。

    下载工具