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.

Jawaban

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. Di jendela Struktur ( View->Open subviews->Structures), impor KUSER_SHARED_DATA. Anda cukup menulis nama tipe di kotak dialog dan tekan OK, seperti pada gambar berikut:

  1. Di bawah Edit->Segments->Create segment, buat segmen baru dengan kisaran memori tersebut, seperti pada gambar berikut.

  1. Di awal segmen baru, gunakan Edit->Struct var, dan pilih KUSER_SHARED_DATA.

Itu dia. Sekarang daftar pembongkaran terlihat seperti ini:

Dan dekompilasinya terlihat seperti ini:

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])

hasil

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;
}

Metode 1)

Gunakan VirtualQuery untuk mendapatkan ukuran yang
ditunjukkan di bawah ini adalah poc python yang membandingkan hasilnya dengan (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

Metode 2)

gunakan windbg! vprot untuk mendapatkan hal yang sama

:\>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:

Metode 3)

gunakan alamat windbg! untuk Mendapatkan Detail Lebih Verbose dari Ruang Alamat yang sama

:\>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: