Showing posts with label winapi. Show all posts
Showing posts with label winapi. Show all posts

04 July 2012

jelajah PEB

1) struktur: PEB_LDR_DATA
typedef struct _PEB_LDR_DATA
{
0x0      ULONG Length;
0x4      BOOLEAN Initialized;
0x8      PVOID SsHandle;
0xc      LIST_ENTRY InLoadOrderModuleList;
0x14     LIST_ENTRY InMemoryOrderModuleList;
0x1c     LIST_ENTRY InInitializationOrderModuleList;
} PEB_LDR_DATA, *PPEB_LDR_DATA;


Cara nak dapatkan struktur ini adalah:
mov eax, fs:[30]  ; -> eax adalah lokasi PEB
mov eax, [ eax + 0xc] ; ->  eax adalah lokasi PEB_LDR_DATA berada

[PEB_LDR_DATA.InInitializationOrderModuleList] -> First LDR_DATA_TABLE_ENTRY struktur


2) struktur: LDR_DATA_TABLE_ENTRY
typedef struct _LDR_DATA_TABLE_ENTRY
{
0x0    LIST_ENTRY InLoadOrderLinks;
0x8    LIST_ENTRY InMemoryOrderLinks;
0x10   LIST_ENTRY InInitializationOrderLinks;
0x18   PVOID DllBase;
0x1c   PVOID EntryPoint;
0x20   ULONG SizeOfImage;
0x24   UNICODE_STRING FullDllName;
0x2c   UNICODE_STRING BaseDllName;
       ULONG Flags;
       WORD LoadCount;
       WORD TlsIndex;
       union
       {
            LIST_ENTRY HashLinks;
            struct
            {
                 PVOID SectionPointer;
                 ULONG CheckSum;
            };
       };
       union
       {
            ULONG TimeDateStamp;
            PVOID LoadedImports;
       };
       _ACTIVATION_CONTEXT * EntryPointActivationContext;
       PVOID PatchInformation;
       LIST_ENTRY ForwarderLinks;
       LIST_ENTRY ServiceTagLinks;
       LIST_ENTRY StaticLinks;
} LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;

FullDllName -> nama dll beserta lokasi file (UNICODE "C:\Windows\system32\kernel32.dll")
BaseDllName -> hanya nama dll   (UNICODE "kernel32.dll")

*note: dlm libemu, FullDllName adalah empty, so kalau shellcode guna FullDllName instead of BaseDllName, libemu tak dapat simulasikan shellcode. Boleh consider sebagai anti-libemu features


3) UNICODE_STRING (8 byte)

typedef struct _UNICODE_STRING
{
     WORD Length;                2 byte
     WORD MaximumLength;         2 byte
     WORD * Buffer;              4 byte (pointer to word)
} UNICODE_STRING, *PUNICODE_STRING;

Maka, offset utk BaseDLLName : 0x2c + 4 = 0x30


ref  http://undocumented.ntinternals.net/UserMode/Structures/PEB_LDR_DATA.html
ref >> http://www.nirsoft.net/kernel_struct/vista/LDR_DATA_TABLE_ENTRY.html

IMAGE_EXPORT_DIRECTORY (struktur)

public struct IMAGE_EXPORT_DIRECTORY
{
0x00    public UInt32 Characteristics;
0x04    public UInt32 TimeDateStamp;
0x08    public UInt16 MajorVersion;
0x0a    public UInt16 MinorVersion;
0x0c    public UInt32 Name;
0x10    public UInt32 Base;
0x14    public UInt32 NumberOfFunctions;
0x18    public UInt32 NumberOfNames;
0x1c    public UInt32 AddressOfFunctions; // RVA from base of image
0x20    public UInt32 AddressOfNames; // RVA from base of image
0x24    public UInt32 AddressOfNameOrdinals; // RVA from base of image
}


imageBase + DataDirectories[ExportDir RVA]  = adalah pointer kepada struktur kat atas


28 June 2012

Bagaimana data disimpan dalam memori

x = 0x12345678
Andaikan x adalah DWORD variable, dan data x di simpan di alamat 0x904F000

Jika dilihat dalam Virtual Memory(blh guna immunity debugger, windbg), yang disimpan adalah
terbalik, iaitu 78563412
atau lebih terperinci spt dibawah:

alamatdata
0x904F000 78
0x904F001 56
0x904F002 34
0x904F003 12

Bila guna winapi ReadProcessMemory

Syntax



BOOL WINAPI ReadProcessMemory(
  __in   HANDLE hProcess,
  __in   LPCVOID lpBaseAddress,
  __out  LPVOID lpBuffer,
  __in   SIZE_T nSize,
  __out  SIZE_T *lpNumberOfBytesRead
);



Lihat parameter ke-3, lpBuffer berjenis LPVOID,  maksudnya boleh guna char, word, dword etc.

REadProcessMemory(-1, 0x904f000, [array of dword], 4, 0)
hasil >>
dword[0] = 305419896 ; yg mana hex(dword[0]) akan dapat semula nilai > 0x12345678


Tapi kalau lpBuffer berjenis word(array)
REadProcessMemory(-1, 0x904f000, [array of word], 4, 0)
hasil >>
word[0] = 22136 ; yg mana hex(word[0]) akan dapat semula nilai > 0x5678
dan
word[1] = 4660 ; yg mana hex(word[1]) akan dapat semula nilai > 0x1234


RUMUSAN:
word[0] bersaiz 2 byte,(ambil data dari 2 alamat)
jadi word[0] diambil dari alamat 0x904F000 (0x78) dan 0x904F001  (0x56)

tapi sebab cpu ni litte endian, kena baca dari alamat secara terbalik
0x904F001 (0x56)
0x904F000 (0x78) >> 0x5678

dan word[1] akan ambil data dari
0x904F003 (0x12) dan
0x904F002 (0x34) >> 0x1234

Manakala
dword pulak bersaiz 4 byte. (ambil data dari 4 alamat), so dword[0] akan ambil data dari
0x904F003 (0x12) dan
0x904F002 (0x34) dan
0x904F001 (0x56) dan
0x904F000 (0x78) >> 0x12345678


Bagaimana pula kalau char/byte ??
hint: char saiz 1 byte. (ambil data dari 1 alamat)


12 June 2012

System Call


Ada 2.
1) INT 2E > Lama (Win 2000 dan sebelum)

ntdll!NtWriteFile:
77f8c552   mov  eax, 0xed
77f8c557   lea  edx, [esp+0x4]
77f8c55b   int  2e
77f8c55d   ret 0x24

eax: index dalam array  KiServiceTable
edx: pointer kpd parameter pertama utk function kernel-mode

interupt akan buat os baca IDT(interupt descriptor table). int 2e akan point kepada  code KiSystemService dalam NTOSKRNL(kernel-mode).
KiSystemService akan baca KiServiceTable base dari nilai eax.

Dalam contoh ni, sebab eax ialah 0xed, function NtWriteFile akan dipanggil.












11 April 2011

VirtualProtect

Dalam Immunity Debugger-> Memory Map

Asal
Memory map, item 13
 Address=00401000
 Size=0000F000 (61440.)
 Owner=setup279 00400000
 Section=.text
 Contains=code
 Type=Imag 01001002
 Access=R E
 Initial access=RWE



Call Virtual Protect API
0040F253  |. 50             PUSH EAX                                          ; /pOldProtect
0040F254  |. 6A 40          PUSH 40                                           ; |NewProtect = PAGE_EXECUTE_READWRITE
0040F256  |. 52             PUSH EDX                                          ; |Size
0040F257  |. 68 00104000    PUSH setup279.00401000                            ; |Address = setup279.00401000
0040F25C  |. E8 1F000000    CALL <JMP.&kernel32.VirtualProtect>               ; \VirtualProtect


 Result

Memory map, item 13
 Address=00401000
 Size=0000F000 (61440.)
 Owner=setup279 00400000
 Section=.text
 Contains=code
 Type=Imag 01001002
 Access=RWE CopyOnWr
 Initial access=RWE


Perubahan pada field Access, dari [R E]  kepada [RWE CopyOnWr]