Incorrect MLIL->HLIL lifting due to stack aliasing

Offen
#8,624 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
cpp

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
C++
Sterne
1.3k
Forks
304
Ø Merge
2 T. 20 Std.
Gemergte PRs (30 T.)
10

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Vector35/binaryninja-api

Alle Issues in Vector35/binaryninja-api

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.