How to show KUSER_SHARED_DATA members in decompiled C code?

Oct 23 2020

Here is a sample C code which prints Windows version directly from address of KUSER_SHARED_DATA. Tested in Windows 10 only. The raw memory address differ in Windows version but that's not the point.

#include <stdio.h>

int main(void)
{
    wprintf(
        L"Version: %lu.%lu.%lu\n",
        *(unsigned int *)(0x7FFE0000 + 0x026C),
        *(unsigned int *)(0x7FFE0000 + 0x0270),
        *(unsigned int *)(0x7FFE0000 + 0x0260)
    );
}

Here are the decompiled code:

In GHIDRA:

int main(int _Argc,char **_Argv,char **_Env)

{
    wprintf(L"Version: %lu.%lu.%lu\n",
        (ulonglong)_DAT_7ffe026c,
        (ulonglong)_DAT_7ffe0270,
        (ulonglong)_DAT_7ffe0260);
  return 0;
}

In IDA Pro + Hex-Rays:

int __fastcall main()
{
    wprintf(L"Version: %lu.%lu.%lu\n",
        MEMORY[0x7FFE026C],
        MEMORY[0x7FFE0270],
        MEMORY[0x7FFE0260]);
  return 0;
}

My question: In decompiled code, is it possible to show the memory address as the member of KUSER_SHARED_DATA? For example, I want to show MEMORY[0x7FFE0260] as SharedData.NtBuildNumber or something similar to it.

Risposte

3 RolfRolles Oct 23 2020 at 11:58

For IDA/Hex-Rays:

  1. In the Loaded Type Libraries window (View->Open subviews->Type libraries), load ntddk_win10 (or whatever Windows version you want, back to Windows XP and Windows Server 2003).

  1. Nella finestra Strutture ( View->Open subviews->Structures), importa KUSER_SHARED_DATA. Puoi semplicemente scrivere il nome del tipo nella finestra di dialogo e premere OK, come nell'immagine seguente:

  1. Sotto Edit->Segments->Create segment, crea un nuovo segmento con quell'intervallo di memoria, come nell'immagine seguente.

  1. All'inizio del nuovo segmento, usa Edit->Struct vare seleziona KUSER_SHARED_DATA.

Questo è tutto. Ora l'elenco di smontaggio è simile a questo:

E la decompilazione è simile a questa:

2 blabb Oct 24 2020 at 04:06

ghidra

addr = toAddr(0x7ffe0000)
currentProgram.memory.createUninitializedBlock("KUSER_SHARED_PAGE",addr,0x1000,0)
createData(addr,getDataTypes("KUSER_SHARED_DATA")[0])

risultato

undefined8 main(void)

{
  wprintf((__crt_locale_pointers *)L"Version: %lu.%lu.%lu\n",
          (ulonglong)KUSER_SHARED_DATA_7ffe0000.NtMajorVersion,
          (ulonglong)KUSER_SHARED_DATA_7ffe0000.NtMinorVersion,
          (ulonglong)KUSER_SHARED_DATA_7ffe0000.NtBuildNumber);
  return 0;
}

Metodo 1)

Usa VirtualQuery per ottenere la dimensione
mostrata di seguito è un poc python confronta il risultato con (MEMORY_BASIC_INFO *) foo.RegionSize

:\>cat vq.py
from ctypes import *
meminfo =(c_ulong * 0x8)()
windll.kernel32.VirtualQuery(0x7ffe0000,byref(meminfo),sizeof(meminfo))
for i in meminfo:
    print (hex(i))


:\>python vq.py
0x7ffe0000
0x7ffe0000
0x2
0x1000
0x1000
0x2
0x20000
0x0

Metodo 2)

usa windbg! vprot per ottenere lo stesso

:\>cdb -c "!vprot 7ffe0000;q" cdb | awk "/Reading/,/quit/"
0:000> cdb: Reading initial command '!vprot 7ffe0000;q'
BaseAddress:       7ffe0000
AllocationBase:    7ffe0000
AllocationProtect: 00000002  PAGE_READONLY
RegionSize:        00001000
State:             00001000  MEM_COMMIT
Protect:           00000002  PAGE_READONLY
Type:              00020000  MEM_PRIVATE
quit:

Metodo 3)

usa windbg! address per ottenere dettagli più dettagliati dello stesso spazio indirizzo

:\>cdb -c "!address 7ffe0000;q" cdb | awk "/Usage:/,/quit/"
Usage:                  Other
Base Address:           7ffe0000
End Address:            7ffe1000
Region Size:            00001000 (   4.000 kB)
State:                  00001000          MEM_COMMIT
Protect:                00000002          PAGE_READONLY
Type:                   00020000          MEM_PRIVATE
Allocation Base:        7ffe0000
Allocation Protect:     00000002          PAGE_READONLY
Additional info:        User Shared Data


Content source: 1 (target), length: 1000
quit: