Incorrect MLIL->HLIL lifting due to stack aliasing

未关闭
#8,624 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp

调研方向

Start with the attached hlil_counter_copy.zip and reproduce the lifting around 0x400042, comparing LLIL, MLIL, and HLIL for the stack store and later load. Trace the stack aliasing through the shown loops; done means the counter assignment remains represented correctly in HLIL without removing the corresponding value.

由索引模型根据 Issue 内容生成。

描述

This bug is found while emulating HLIL on known crypto oracles.

Given the following code snippet:

typedef unsigned int u32;
typedef unsigned char u8;
static u32 load32le(const u8 *p)
{
    return (u32)p[0] | ((u32)p[1] << 8) | ((u32)p[2] << 16) | ((u32)p[3] << 24);
}
void counter_copy(const u8 *key, u32 counter, const u8 *nonce, u32 *out)
{
    static const u32 constants[4] = {0x61707865u, 0x3320646eu, 0x79622d32u, 0x6b206574u};
    u32 state[16], original[16];
    for (int i = 0; i < 4; i++) state[i] = constants[i];
    for (int i = 0; i < 8; i++) state[4 + i] = load32le(key + 4*i);
    state[12] = counter;
    for (int i = 0; i < 3; i++) state[13 + i] = load32le(nonce + 4*i);
    for (int i = 0; i < 16; i++) original[i] = state[i];
    for (int round = 0; round < 10; round++) {
        state[0] += state[4];
        state[12] ^= state[0];
        state[8] += state[12];
    }
    for (int i = 0; i < 16; i++) out[i] = state[i] + original[i];
}

Specifically for line state[12] = counter;, the counter store is at 0x400042:

0x400042  mov dword ptr [rbp-0x10], esi

The LLIL and MLIL are both correct:

0x400042  [rbp - 0x10 {var_18}].d = esi
0x400070  edx = [rbp + (rax << 2) - 0x40].d
0x400074  [rbp + (rax << 2) - 0x80].d = edx
0x400042  var_18 = arg2
0x400070  rdx = [&var_48 + (i_3 << 2)].d
0x400074  [&var_88 + (i_3 << 2)].d = rdx
0x400087  rdx_1 = var_18

But it gets removed in HLIL:

00400000    int64_t counter_copy(int64_t arg1, int32_t arg2, int64_t arg3, int64_t arg4)

00400024        int32_t var_48[0x4]
00400024        
00400024        for (int64_t i = 0; i != 0x10; i += 4)
00400017            *(&var_48 + i) = *(i + "expand 32-byte k")
00400017        
00400040        int32_t var_38[0x4]
00400040        
00400040        for (int64_t i_1 = 0; i_1 != 8; i_1 += 1)
00400034            var_38[i_1] = *(arg1 + (i_1 << 2))
00400034        
0040005e        for (int64_t i_2 = 0; i_2 != 3; i_2 += 1)
00400053            int32_t var_14[0x3]
00400053            var_14[i_2] = *(arg3 + (i_2 << 2))
00400053        
0040007f        int32_t var_88[0x10]
0040007f        
0040007f        for (int64_t i_3 = 0; i_3 != 0x10; i_3 += 1)
00400074            var_88[i_3] = var_48[i_3]
00400074        

Binaries and source code are attached: hlil_counter_copy.zip

主要语言
C++
星标
1.3k
派生
304
平均合并
2 天 20 小时
30 天内合并 PR
10

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Vector35/binaryninja-api 的其他 Issue

查看 Vector35/binaryninja-api 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。