♧ Godown 安全知识树

IDA Hex-Rays ctree 宏还原:把 LIST_ENTRY 自指初始化折叠为 InitializeListHead(&x)

tools/ida · verified

一句话结论

Windows 驱动反编译中常见的 x.Flink = &x; x.Blink = &x; 是 InitializeListHead(&x) 的宏展开;给变量套上 LIST_ENTRY 类型后,可用 Hex-Rays ctree visitor 匹配这两条相邻赋值,把第一条替换成 InitializeListHead(&x) 调用并删除第二条,再通过 open_pseudocode/refresh_pseudocode 钩子在反编译完成后刷新文本,实现自动还原。

原理

  • 宏展开形态:InitializeListHead(ListHead) 的实现是 ListHead->Flink = ListHead->Blink = ListHead,编译后反编译常显示为两条相邻赋值:qwordA = &qwordA; qwordB = &qwordB;(或 stru.Blink = &stru; stru.Flink = &stru;),两个字段分别位于 LIST_ENTRY 的偏移 0(Flink)和 8(Blink,64 位)。
  • 类型恢复是前提:把该地址定义成 LIST_ENTRY 后,IDA 会把 qword 显示为 stru.Flink/stru.Blink;若没有符号文件,需要手动应用类型或用脚本按字段偏移识别。
  • ctree 模式匹配:Hex-Rays 的 ctree 由 cexpr_t 组成,赋值是 cot_asg,字段访问是 cot_memref/cot_memptr,取地址是 cot_ref。匹配器检查连续两条语句是否分别为偏移 0 和 8 的字段赋值,且左右基址相同、右值是同一基址的取地址;命中后构造 cot_call + cot_helper("InitializeListHead") + cot_ref(base) 参数,替换第一条并删除第二条。
  • 表达式指纹:为可靠比较基址表达式,可对 ctree 做递归 key(num/var/obj/ref/ptr/cast/memref/add 等),避免直接比较指针或受 cast 包装影响;匹配前剥离 cot_cast。
  • 钩子时机是关键:不能在 maturity(CMAT_FINAL) 回调里改 ctree——IDA 在回调返回后还会做最终序列化/验证,改动会破坏内部一致性,导致 decompile() 返回 None。应使用 open_pseudocode/refresh_pseudocode:它们在 decompile() 完全返回、调用方拿到 cfunc 后触发;refresh_ctext() 只刷新文本渲染,不重新反编译,因此不会递归触发钩子。
  • 批处理:提供 process_cfunc(cfunc) 对已反编译完成的函数直接应用 visitor,便于脚本批量处理驱动中的初始化代码。

触发条件

  • 使用 IDA Pro + Hex-Rays 反编译 Windows 驱动/内核模块,目标变量是 LIST_ENTRY 类型或可按偏移识别为 Flink/Blink。
  • IDA 版本提供 ida_hexrays、ctree_visitor_t、carglist_t 等 API(IDA 7.x/8.x/9.x 常见)。
  • 反编译结果中两条自指赋值相邻出现,且没有其他语句插入其间;被赋值的基址是同一个对象。
  • 允许加载/运行 IDAPython 脚本,且 Hex-Rays 插件已初始化。

利用步骤

  1. 在 IDA 中确认目标地址是 LIST_ENTRY:应用类型或按 Flink=+0、Blink=+8 识别;没有符号时手动定义结构。
  2. 运行 IDAPython 脚本,注册 Hexrays_Hooks 的 open_pseudocode 与 refresh_pseudocode 回调。
  3. 打开目标函数的伪代码(或按 F5):脚本在 process_cfunc 中遍历 ctree,匹配连续两条 Flink/Blink 自指赋值。
  4. 命中后构造 InitializeListHead(&base) 调用替换第一条语句,删除第二条;vu.refresh_ctext() 刷新显示。
  5. 对批处理场景直接调用 process_cfunc(cfunc);对未命中/误匹配的样本调整类型、cast 剥离和基址比较逻辑。
  6. 回归验证:确认还原后的调用语义与原两条赋值等价,且没有破坏其他语句/变量。

PoC

以下为最小骨架(按文中脚本精简,未在本机 IDA 运行):

import ida_hexrays, ida_typeinf

def strip_cast(e):
    while e.op == ida_hexrays.cot_cast:
        e = e.x
    return e

def match_field_asg(expr, off):
    if expr.op != ida_hexrays.cot_asg:
        return None
    lhs = expr.x
    if lhs.op in (ida_hexrays.cot_memref, ida_hexrays.cot_memptr) and lhs.m == off:
        return lhs.x, expr.y
    return None

def try_match(s1, s2):
    for off_a, off_b in ((8, 0), (0, 8)):
        m1, m2 = match_field_asg(s1.cexpr, off_a), match_field_asg(s2.cexpr, off_b)
        if not m1 or not m2:
            continue
        base1, rhs1 = m1; base2, rhs2 = m2
        if key(base1) != key(base2):
            continue
        if strip_cast(rhs1).op == ida_hexrays.cot_ref and strip_cast(rhs2).op == ida_hexrays.cot_ref:
            return base1
    return None

class V(ida_hexrays.ctree_visitor_t):
    def __init__(self):
        super().__init__(ida_hexrays.CV_FAST)
    def visit_insn(self, insn):
        if insn.op == ida_hexrays.cit_block:
            stmts = list(insn.cblock)
            for a, b in zip(stmts, stmts[1:]):
                base = try_match(a, b)
                if base:
                    a.cexpr.swap(build_initialize_list_head_call(base))
                    insn.cblock.erase(insn.cblock.find(b))
        return 0

class Hook(ida_hexrays.Hexrays_Hooks):
    def open_pseudocode(self, vu):
        V().apply_to(vu.cfunc.body, None)
        vu.refresh_ctext()
        return 0
    def refresh_pseudocode(self, vu):
        self.open_pseudocode(vu)
        return 0

build_initialize_list_head_call 需按 cot_call(cot_helper("InitializeListHead"), carglist[cot_ref(base)]) 构造;key() 为递归表达式指纹函数。

变体与关联

  • 变体:同法可还原 RemoveEntryList、InsertHeadList、InsertTailList 等 LIST_ENTRY 宏,只要为其建立对应的 ctree 语句模式。
  • 变体:无符号/类型未知时,先用脚本按“两个相邻 qword 互指/自指”识别候选,再批量应用 LIST_ENTRY 类型,最后做宏折叠。
  • 变体:Ghidra 侧可用 P-code/Decompiler API 做类似的语句模式替换,但 AST/接口不同。
  • 关联:[[tools-ida-xor-string-call-arg-backtrack-decrypt]](另一 IDAPython 自动注释/解密脚本)、Windows 驱动逆向相关卡片。
  • 边界:两条赋值被编译器重排/插入其他语句、基址表达式不一致、宏被内联成其他形态、或 ctree 在 maturity(CMAT_FINAL) 中被修改时,匹配或稳定性会失效。

检测与防御

  • 这不是攻击技术,而是反编译辅助;重点是正确性与稳定性:只在 decompile() 完成后改 ctree,不在 maturity 回调里改;构造表达式时注意 tinfo_t.create_ptr(base_expr.type) 的方向和 thisown 归属,避免悬空/重复释放。
  • 误匹配:必须校验两个字段偏移(0/8)、基址表达式完全一致、右值确为同一基址的取地址;对 cast、括号、常量偏移做归一化。
  • 验证:对每个被替换的函数做前后语义对比(原两条赋值 vs 调用),在样本集上回归;保留原始反编译文本/日志以便回滚。
  • 兼容性:不同 IDA/Hex-Rays 版本的 ctree API 可能变化(carglist_t、cot_helper、refresh_ctext),脚本应做版本检测和异常捕获。