Jump to content
Sign in to follow this  
Ancient One

Kuiz Win32

Recommended Posts

Memandangkan tak lama lagi nak raye, meh kiter sama2 jawab kuiz raya ni..

Adakah korang pernah terfikir brape banyak yang korang tau mengenai aplikasi korang sendiri. Kenalkah korang dengan program yang korang tulis dan bina sendiri? Kuiz ini akan membantu korang mendalami program korang.

[1] [NT only] Program 1.exe ni telah dienkrip bermula dari entrypoint (disassembly akan menghasilkan kod sampah). Namun demikian, program ni tetap dilaksanakan dengan betul. Nampaknya ia telah didekrip secara automatik (tanpa bantuan kod program yang lain) semasa program loading. Explain how it is done.

[2] [NT only] Seperti yang kita semer tau, execution begins at entrypoint. Nyatakan apakah feature yang membenarkan cebisan kod di dalam aplikasi korang dilaksanakan sebelum entrypoint program korang. Berikan contoh program dan kod.

[3] Program 3.exe run tanpa masalah under NT tapi takleh run under Win9x. Explain why and how to fix it.

[4] The program 4.exe cannot be bind by bind.exe. That is, the imported function entrypoint cannot be patch into the executable file at link time. Explain why and how to fix them. (Use M$ PSDK bind.exe with -v and -u switch and it will show you the error.) [Hint : Program for Question 3 can be bind].

[5] Without using any Win32 API related to Windows version (e.g GetVersion), juga tanpa bantuan daripada aper2 HLL libraries/wrappers/framework libraries, write a code to differentiate between 9x and NT.

[6] Next Week..

Jawapan akan menyusul dengan soalan yang seterusnya..

Selamat Menjawab!!!

Share this post


Link to post
Share on other sites

A'kum.. teringin nak jawab, tapi aku hapa pon tatau pasal win32. hehehehe

Tak apa, nara pun tak faham kebanyakan soalan yang ditanyakan. Kita ambil soalan.. dan nantikan jawapan.. kita cuba fahami.. kalau tak faham.. kita cuba belajar.. dan satu hari nanti.. kita Insya-Allah boleh faham soalan yang ditanyakan dahulu tu..

Yang penting, jangan biarkan soalan itu terbiar tak berjawab... smile.gif

Share this post


Link to post
Share on other sites

[1] - takde encryption sebenarnya..mungkin sebab relocation table yg agak mengarut

00011025   68 00100100      PUSH 1.00011000                        ; ASCII "Question 1"
0001102A   90               NOP
0001102B   90               NOP
0001102C   90               NOP
0001102D   68 0B100100      PUSH 1.0001100B                        ; ASCII "Hello Malaysia!!!"
00011032   40               INC EAX
00011033   31C0             XOR EAX,EAX
00011035   53               PUSH EBX
00011036   29DB             SUB EBX,EBX
00011038   43               INC EBX
00011039   FF15 7B100100    CALL DWORD PTR DS:[<&USER32.MessageBoxA>>; USER32.MessageBoxA

http://www.windowsitlibrary.com/Content/356/11/5.html

The relocation information helps the operating system load an executable file and apply fixups for absolute addresses. The relocation data is needed by the loader if the image cannot be loaded to the preferred load address ImageBase mentioned in the Optional Header. In this case, the fixed addresses supplied by the linker are no longer valid, and the loader has to apply fixups for absolute addresses used for locations of static variables, string literals, etc. On the other hand, if the loader was able to load the file at the preferred base address, the relocation data is not needed and is ignored.

http://www.heaventools.com/remove_relocations.htm

[3] -

For example, say you built an EXE for Windows NT and based the EXE at 0x10000. If you told the linker to strip the relocations, the EXE wouldn't run under Chicago, where the address 0x10000 is already in use
- http://www.csn.ul.ie/~caolan/publink/winre..._peeringpe.html

tukar nilai imagebase kpd contoh nya 0x0000000

Edited by zeph

Share this post


Link to post
Share on other sites

[1] - Actually relocation table tu takla agak mengarut smile.gif. Aku buat secara manual so biler loader apply fixups info pada address yang dinyatakan, ia akan hasilkan kod yang betul, yang aku nak. Since executable biasanya adalah aplikasi pertama yang ader dalam process address space (along with kernel32, ntdll.dll), so untuk force relocation berlaku, aku setkan image basenya kepada 0, yang menyebabkan sistem akan relocates semula 1.exe ke base 0x10000 (NT), iaitu start of user-mode partition dalam address spacenya. So, jawapan zeph bleh kire betul laaa smile.gif.

[3] - tak lengkap.

Share this post


Link to post
Share on other sites

[1] - Actually relocation table tu takla agak mengarut smile.gif. Aku buat secara manual so biler loader apply fixups info pada address yang dinyatakan, ia akan hasilkan kod yang betul, yang aku nak. Since executable biasanya adalah aplikasi pertama yang ader dalam process address space (along with kernel32, ntdll.dll), so untuk force relocation berlaku, aku setkan image basenya kepada 0, yang menyebabkan sistem akan relocates semula 1.exe ke base 0x10000 (NT), iaitu start of user-mode partition dalam address spacenya. So, jawapan zeph bleh kire betul laaa smile.gif.

[3] - tak lengkap.

[3]

tukar imagebase ke 0x0000 and set flag ke 'relocation strip'

p/s: mcm error yg dibuat oleh zydon

[2]

http://my.execpc.com/~geezer/osd/exec/pe.txt

soalan lain2 bila nak bagi jawapan? aku malasss smile.gif

Edited by zeph

Share this post


Link to post
Share on other sites

Jawapan kepada Kuiz Pertama :

[1] Dengan menggunakan base relocation info (pointed to by index 5 of PE data directory array). Melihat disassembly 1.exe kiter tidak dapat melihat kod sebenar program tersebut. Kod sebenar yang dilaksanakan adalah seperti berikut (output from ollyDbg) :

<ModuleEntryPoint>  31DB           XOR EBX, EBX	;1101D
0001101F            90             NOP
00011020            90             NOP
00011021            53             PUSH EBX
00011022            EB 00          JMP SHORT 00011024
00011024            49             DEC ECX
00011025            68 00100100    PUSH 00011000  ; ASCII "Question 1"
0001102A            90             NOP
0001102B            90             NOP
0001102C            90             NOP
0001102D            68 0B100100    PUSH 0001100B  ; ASCII "Hello Malaysia!!!"
00011032            40             INC EAX
00011033            31C0           XOR EAX, EAX
00011035            53             PUSH EBX
00011036            29DB           SUB EBX, EBX
00011038            43             INC EBX
00011039            FF15 7B100100  CALL [<&USER32.MessageBoxA>]
0001103F            EB 00          JMP SHORT 00011041
00011041            C3             RETN
Meh kiter tengok fixups info untuk 1.exe (using dumpbin atau aper2 PE dumping tools) :
D:\Out\Q>dumpbin -relocations 1.exe
Microsoft (R) COFF/PE Dumper Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file 1.exe

File Type: EXECUTABLE IMAGE

BASE RELOCATIONS #1
    101D RVA,       22 SizeOfBlock
       0  HIGHLOW            908FDB31
       4  HIGHLOW            48FFEB53
       8  HIGHLOW            000F0068
       C  HIGHLOW            908F9000
      10  HIGHLOW            000F0B68
      14  HIGHLOW            C0304000
      18  HIGHLOW            43DA2953
      1C  HIGHLOW            107A15FF
      20  HIGHLOW            00EA0000
      24  HIGHLOW            9002CDC3
       9  HIGHLOW            00000F00
      11  HIGHLOW            00000F0B
      1E  HIGHLOW            0000107A

  Summary

        1000 <Achik>

D:\Out\Q>
First block of relocation info bermula pada RVA 0x101D. First fixup to be applied is at offset 0 from RVA 0x101D. Dword (encrypted code) pada 0x101D adalah
dd 0x908FDB31
Note that, in order to force system untuk rebase semula 1.exe, aku setkan preferred image base 1.exe kepada 0, yang sudah tentu akan direlokasikan semula oleh sistem kepada 0x10000 (start of user mode partition in a process virtual address space) under NT. Apabila program ini perlu dipindahkan, maka sistem akan pastikan program tersebut mempunyai relocation info (File characteristic, IMAGE_FILE_RELOCS_STRIPPED tidak akan dihiraukan oleh sistem as long as ia ader base relocation info, ia akan digunakan). So.. dalam kes 1.exe di atas, the fixup applied at offset 0 is 0x908FDB31+0x10000=0x9090DB31, menjadi
xor	ebx, ebx
nop
nop
dan begitulah seterusnya. Kalau korang tengok reloc info kat atas, terdapat overlapped pada address yang tertentu. Fixup info bleh diapply beberapa kali pada lokasi yang sama. [2] Feature tersebut adalah Thread Local Storage (TLS). TLS, secara ringkasnya, blehla dikatakan global variable yang local kepada setiap thread dalam suatu process (they're not allocated from the thread stack!!). Dalam program Win32, kite bleh manipulate tls melalui 2 teknik: pertama, secara dinamik, dengan Tls* functions, i.e TlsAlloc, TlsFree, TlsSetValue dan TlsGetValue. Yang kedua, dengan static tls, yang disokong dalam format PE. Direktori maklumat tls ringkas jer :
typedef struct _IMAGE_TLS_DIRECTORY32 {
    DWORD   StartAddressOfRawData;
    DWORD   EndAddressOfRawData;
    DWORD   AddressOfIndex;
    DWORD   AddressOfCallBacks;
    DWORD   SizeOfZeroFill;
    DWORD   Characteristics;
} IMAGE_TLS_DIRECTORY32;
Untuk menggunakan static tls, kiter bleh gunakan __declspec(thread) dalam M$ VC++ compiler. Field yang menjawab persoalan kiter adalah AddressOfCallBacks, di mana ia menunjuk kepada array of callback functions yang membolehkan kiter melakukan initialization. Callback functions ni dipanggil sebelum program kiter bermula! Note that callback function ni tak disokong dalam Windows 9x. Most compilers pun tak support definition of tls callback functions. Prototype callback function ni serupa (tapi tak sama) dengan DllMain :
typedef VOID
(NTAPI *PIMAGE_TLS_CALLBACK) (PVOID DllHandle, DWORD Reason, PVOID Reserved);
di mana DllHandle dan Reason parameter adalah sama dengan DllMain. Kod fasm di bawah memberikan contoh ringkas mengenai callback function nih (tested on XP):
tls fix 9

format PE GUI
entry Mula

section ".ancient" code data readable writeable executable

Caption     db "Tls Callback Test", 0
EntryTitle  db "Program Entrypoint", 0
Cb1Title    db "TLS Callback 1", 0
Cb2Title    db "TLS Callback 2 chained", 0

Mula:
    push    0
    push    Caption
    push    EntryTitle
    push    0
    call    [MessageBox]
    ret

TlsCallback1:
   ;make sure it's called only when DLL_PROCESS_ATTACH
    mov     eax, [esp+8]
    dec     eax
    jnz     @f
    push    0
    push    Caption
    push    Cb1Title
    push    0
    call    [MessageBox]
    @@:
    ret     3*4

TlsCallback2:
   ;make sure it's called only when DLL_PROCESS_ATTACH
    mov     eax, [esp+8]
    dec     eax
    jnz     @f
    push    0
    push    Caption
    push    Cb2Title
    push    0
    call    [MessageBox]
    @@:
    ret     3*4

data import
dd  0, 0, 0, rva user32.dll, rva user32
dd  0, 0, 0, 0, 0
user32 :
MessageBox dd rva fMessageBox
dd  0
fMessageBox dw 0
            db "MessageBoxA", 0
user32.dll  db "user32.dll", 0

end data

data tls
dd 0, 0, tlsIndex, tlsCallback, 0, 0
tlsCallback dd TlsCallback1, TlsCallback2, 0
end data

tlsIndex dd ?
[3] Ader beberapa fields dalam PE headers yang kiter kena modify untuk menjadikannya valid dalam Win9x. Pertama sekali adalah subsystem version. Win9x takleh run program yang mempunyai versi subsistem yang lebih tinggi dari yang sepatutnya. 3.exe nyer subsystem version = 5.0, which are supported only under NT 5.0+, but not Win9x/ME. So kiter kena ubah field ni kepada e.g 4.0. Kemudian kiter kena modify field image base. 3.exe nyer image base diberikan 0x10000, yang tak valid under Win9x. Win9x nyer user app address start at 0x400000, so kiter kena modify field ni kepada e.g 0x400000 laaa.. Tu jer.. tapi problemnya kod program ni dah tak valid dah since base asal semasa ia dikompil adalah 0x10000, so kalau kiter run program ni under Win9x ia akan crash. So, kiter kena modify kod yang refer kepada virtual address laa (dalam 3.exe kiter kena patch 3 asm lines, 2 pushes, 1 call). [4] Struktur import table adalah seperti berikut :
typedef struct {
    DWORD   Characteristics;
    DWORD   OriginalFirstThunk;
    DWORD   TimeDateStamp;
    DWORD   ForwarderChain;
    DWORD   Name;
    DWORD   FirstThunk;
} IMAGE_IMPORT_DESCRIPTOR;
typedef IMAGE_IMPORT_DESCRIPTOR UNALIGNED *PIMAGE_IMPORT_DESCRIPTOR;
Untuk membolehkan kiter bind 4.exe, ia mestilah ader OriginalFirstThunk, iaitu satu lagi penunjuk kepada salinan array of IMAGE_THUNK_DATA (2nd copy ditunjuk oleh FirstThunk). Second copy digunakan oleh sistem untuk patch entrypoint function at load time, dan kod program kiter menggunakan second copy of IMAGE_THUNK_DATA untuk memanggil fungsi yang diimport daripada DLL lain. Manakala first copy plak tidak akan diganggu oleh sistem. Biler kiter bind suatu program, second copy mengandungi entrypoint sebenar function tersebut jadi sistem tak perlulah lakukan search and replace setiap kali program dijalankan. Tapi ni tertakluk kepada beberapa peraturan, e.g TimeDateStamp yang ader kat struktur di atas mestilah sama ngan DLL nyer time date stamp. Kalau tak, sistem akan assume entrypoint ni dah tak valid dan ia terpaksa resolve semer import function semula. Tapi memandangkan 2nd copy dah takde information about import, ia akan gunakan first copy yang ditunjuk oleh OriginalFirstThunk. Dalam kes 4.exe, kiter dah takde first copy since OriginalFirstThunk was set to 0. So, bind.exe refuse untuk bind 4.exe sebab takde original information yang bleh digunakan semasa runtime. Kat bawah aderlah information untuk import directory bagi 4.exe apabila dilihat ngan bind.exe. OriginalFirstThunk dirujuk sebagai Import Name Table, manakala FirstThunk dirujuk sebagai Import Address Table. Bandingkan dengan info untuk 3.exe di bawahnya :
D:\Junks\Q>dumpbin -IMPORTS 4.exe
Microsoft (R) COFF/PE Dumper Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file 4.exe

File Type: EXECUTABLE IMAGE

  Section contains the following imports:

    KERNEL32.DLL
                40106E Import Address Table
                     0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                    0 ExitProcess

    USER32.DLL
                401076 Import Address Table
                     0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                    0 MessageBoxA

  Summary

        1000 ANCIENT

D:\Junks\Q>dumpbin -IMPORTS 3.exe
Microsoft (R) COFF/PE Dumper Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file 3.exe

File Type: EXECUTABLE IMAGE

  Section contains the following imports:

    USER32
                 1106A Import Address Table
                 11072 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                    0 MessageBoxA

  Summary

        1000 <Achik>

D:\Junks\Q>
[5] Sample code below show how this is done using the Thread Environment Block. Ia berasaskan kepada fakta bahawa PEB yang ditunjuk oleh field ProcessEnvironmentBlock (offset 48 from start of TEB) sentiasa berada di bawah address 0x80000000 (i.e 0x7FFDF000 - 0x7FFDFFFF) dalam NT manakala sebaliknya dalam Win9x (in shared memory mapped file area). Selector fs points to the start of TEB, dan code C di bawah menunjukkan camne ia dilakukan :
#include <windows.h>

char Title[] = "Win9x Vs WinNT";

void main() {
    __asm {
        mov     eax, dword ptr fs:[0x30]   ;Peb
        test    eax, eax
        js      Win9x
    }
    MessageBox(0, "Running Under WinNT", Title, 0);
    return;
    Win9x:
    MessageBox(0, "Running Under Win9x", Title, 0);
}
Satu lagi cara adalah dengan tls callback. Read [2]. (fasm code) :
tls fix 9

format PE GUI
entry Mula

section ".ancient" code data readable writeable executable
IsNT        dd UnderWin9x
Caption     db "Win9x Vs WinNT", 0
UnderNT     db "Running Under WinNT", 0
UnderWin9x  db "Running Under Win9x", 0

Mula:
    push    0
    push    Caption
    push    [IsNT]
    push    0
    call    [MessageBox]
    ret

TlsCallback1:
    mov     [IsNT], UnderNT
    ret     3*4



data import
dd  0, 0, 0, rva user32.dll, rva user32
dd  0, 0, 0, 0, 0
user32 :
MessageBox dd rva fMessageBox
dd  0
fMessageBox dw 0
            db "MessageBoxA", 0
user32.dll  db "user32.dll", 0

end data

data tls
dd 0, 0, tlsIndex, tlsCallback, 0, 0
tlsCallback dd TlsCallback1, 0
end data

tlsIndex dd ?

Actually ader lagi cara lain... tu pandai2 korang laaa..

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...