ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Pwndbg `decomp` 命令详解:在 GDB/LLDB 中就近反编译指定地址

2026/9/15 11:15:49 拓冰建站 浏览量
Pwndbg `decomp` 命令详解:在 GDB/LLDB 中就近反编译指定地址 Pwndbgdecomp命令详解在 GDB/LLDB 中就近反编译指定地址【免费下载链接】pwndbgExploit Development and Reverse Engineering with GDB LLDB Made Easy项目地址: https://gitcode.com/GitHub_Trending/pw/pwndbg导读decomp是 Pwndbg 反编译器集成decompiler integration体系中的核心命令之一它借助当前已连接的反编译器IDA、Binary Ninja、Ghidra 或 angr-management把某个内存地址附近的函数反编译结果直接渲染在调试器中方便在 GDB/LLDB 的会话里对照反汇编阅读高级语言级别的代码。读完本文你将掌握decomp的完整参数语义、底层调用链从命令入口到反编译渲染的每一层、前置环境要求以及它与di系列命令和 context 窗口的协作方式。命令概览decomp在 Pwndbg 源码中定义于 pwndbg/commands/decompiler_integration.py其命令行帮助文档位于 docs/commands/integrations/decomp.md。命令帮助信息如下usage: decomp [-h] [addr] [lines] Use the current integration to decompile code near an address.它的语义非常直接使用当前集成即已连接的反编译器反编译某个地址附近的代码。注意该命令必须与di connect建立的反编译器连接配合使用否则命令会提示尝试连接并失败。位置参数位置参数帮助说明默认值addr需要反编译的地址pc当前程序计数器lines显示的反编译行数14addr反编译的定位地址。不指定时默认使用当前$pc即停在哪儿就反编译哪儿适合单步调试时随手查看当前函数的 C 级源码。lines显示行数。默认值14与 Pwndbg context 窗口中源码行数的默认值保持一致——源码中注释明确写了# Same as the default for context-code-lines见 pwndbg/commands/decompiler_integration.py。可选参数短参数长参数帮助说明-h--help显示帮助信息并退出参数解析与命令注册decomp命令的解析器与decompiler-integration别名di命令在同一个模块中注册。与di使用子命令分发不同decomp是独立的顶层命令parser argparse.ArgumentParser( descriptionUse the current integration to decompile code near an address. ) parser.add_argument( addr, typeint, nargs?, defaultNone, helpAddress to decompile near. (default: pc), ) parser.add_argument( lines, typeint, nargs?, default14, helpNumber of lines of decompilation to show., ) pwndbg.commands.Command(parser, categoryCommandCategory.INTEGRATIONS) pwndbg.commands.OnlyWhenRunning def decomp(addr: int | None, lines: int) - None: ...其中pwndbg.commands.OnlyWhenRunning装饰器限定了该命令只能在被调试进程存活时使用这是decomp要求有真实运行进程的原因。CommandCategory.INTEGRATIONS则将它归类到集成类命令下与di一致。核心执行流程decomp的实际执行逻辑非常简短但信息量很大pwndbg/commands/decompiler_integration.pydef decomp(addr: int | None, lines: int) - None: if addr is None: if pwndbg.aglib.regs.pc is None: print(Address not specified, and could not find PC.) return addr pwndbg.aglib.regs.pc if not soft_connection_check(also_syncTrue): return decomp pwndbg.dintegration.manager.decompile_pretty(addr, lines) if decomp is None: print(Could not retrieve decompilation.) else: print(\n.join(decomp))整个流程分四步地址兜底addr为None时回退到$pc若连$pc都取不到例如寄存器状态异常则直接报错退出。软连接检查调用soft_connection_check(also_syncTrue)。若尚未连接反编译器它会先尝试di connect并顺带同步一次符号表连接失败则返回False并中止命令。这意味着即使你忘记手动di connect首次执行decomp也会自动发起连接尝试。获取反编译调用pwndbg.dintegration.manager.decompile_pretty(addr, lines)获取“美化后”的反编译结果。输出逐行打印结果失败时给出 Could not retrieve decompilation. 提示。底层实现decompile_pretty 的加工逻辑decomp的深层逻辑位于 pwndbg/dintegration/init.py 的IntegrationManager.decompile_pretty()。它并不是简单地透传反编译器返回的文本而是做了三层加工语法高亮当pwndbg.config.syntax_highlight开启时会把反编译文本按decompiled_函数名.c这个伪文件名交给语法高亮器处理见 pwndbg/color/syntax_highlight.py让反编译代码像源码一样有颜色区分。当前行指示在$pc所在的行前标注►前缀方便在反编译窗口里一眼定位当前执行点。行数裁剪通过pretty_print.format_source只保留以curr_line为中心的nlines行lines传-1时返回全部行。其裁剪策略与 context 窗口的源码显示逻辑一致源码注释中说明“Logic similar to pwndbg.commands.context.get_filename_and_formatted_source()”。此外还包含针对 Ghidra 的两个兼容性处理源码注释中有明确说明Ghidra 返回的反编译文本首行常为空字符串decompile_pretty会删掉这行空行并相应修正当前行号避免行号错位。Ghidra 有时返回curr_line -1对应上游 decomp2dbg issue #131实现中会按函数名缓存上次有效的行号作为回退保证►指示不失效。与上下文context窗口的联动除了手动执行decompPwndbg 还允许把反编译集成进 context 窗口。在 pwndbg/commands/context.py 中should_decompile pwndbg.config.add_param( context-integration-decompile, True, whether context should fall back to decompilation with no source code, )当没有源码可用例如调试 stripped 的二进制时context_code()会回退调用manager.decompile_pretty(pc, nlines)在 context 中渲染一个Decomp区域并且默认开启True。可以通过set context-integration-decompile off关闭这一行为。也就是说decomp命令本身就是这块 context 集成能力的“手动模式”。使用前提完整的反编译器集成链路decomp依赖反编译器集成环境完整前置步骤如下详细教程见 docs/tutorials/decompiler-integration.md安装反编译器插件在 Pwndbg 内执行di install ida|binja|ghidra|angr安装对应插件。Pwndbg 要求特定的 decomp2dbg 版本源码中固定为3.14.0起始的3.14.*见 pwndbg/commands/decompiler_integration.pydi install会自动处理版本匹配并建立符号链接。不要在~/.gdbinit中手动sourcedecomp2dbg 的d2d.py会与 Pwndbg 自带的调试器侧逻辑冲突。在反编译器中启动服务打开 IDA/Binary Ninja/Ghidra/angr-management按CtrlShiftD并确认启动插件自带的 XML-RPC 服务端。连接在 Pwndbg 中执行di connect连接参数由decompiler-host默认localhost与decompiler-port默认3662两个配置项决定见 pwndbg/commands/decompiler_integration.py。连接成功且进程存活时还会自动执行一次di sync。执行此时运行decomp [addr] [lines]即可。值得注意的约束来自 docs/tutorials/decompiler-integration.mdIDA 与 Binary Ninja 需要商业授权才能加载插件而连接协议基于 XML-RPC且 XML-RPC 的整数上限为 32 位因此 pwndbg/dintegration/init.py 在地址转换时会检查相对地址是否超出MININT/MAXINT范围越界时直接返回None打印 Could not retrieve decompilation.。地址映射与共享库支持decomp传入的是被调试进程地址空间中的“映射地址”而反编译器习惯用镜像基址的相对偏移。这一转换由DecompilerConnection.addr_to_relative()/addr_to_mapped()完成pwndbg/dintegration/init.py。二进制基址默认通过反编译器上报的文件路径与/proc/pid/maps即vmmap比对自动检测支持共享库若文件名不一致或二进制未出现在内存映射中典型场景是调试内核模块可以手动指定di setbase addr手动设置二进制基址恢复自动检测传-1。di setpath path手动指定二进制在内存中的路径恢复自动检测传。两者互斥设置其中一个会自动清除另一个源码见 pwndbg/commands/decompiler_integration.py。与di decomp的关系decompiler-integration别名di命令也暴露了一个di decomp子命令但它的实现只是打印一条提示Just use thedecompcommand.即di decomp并不是真正的反编译入口真正的入口就是本文讲解的decomp命令。完整的di子命令集合connect/c、disconnect/d、sync/s、jump/j、install、decomp、list/l、setbase、setpath参见 docs/commands/integrations/decompiler-integration.md。典型使用示例在已连接反编译器并加载目标进程后decomp # 反编译当前 $pc 所在函数显示 14 行 decomp 0x401234 # 反编译 0x401234 地址所在函数显示 14 行 decomp $pc 30 # 反编译当前函数显示 30 行 decomp 0x401234 -1 # 反编译 0x401234 所在函数并输出全部行输出示例可参考 Pwndbg 文档中的集成效果图 docs/assets/caps/decomp_integration_ex.png图中展示了 context 窗口内Decomp区域与反汇编并排渲染、当前行以►标记的效果。小结decomp是 Pwndbg 反编译器集成中“低门槛、高频使用”的命令两个可选参数地址 行数即可把反编译结果带入调试会话配合►当前行标记与语法高亮能够显著提升逆向调试效率。理解其背后soft_connection_check的自动连接、decompile_pretty的渲染管线、地址映射机制以及di系列命令的配合是发挥这套集成完整能力的关键。【免费下载链接】pwndbgExploit Development and Reverse Engineering with GDB LLDB Made Easy项目地址: https://gitcode.com/GitHub_Trending/pw/pwndbg创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考