Wednesday, September 16, 2009

සිංහලෙන් win32 Portable Executable format - part 2

ගිය සතියේ මෙතුලින් අප කතා කලේ IMAGE_DOS_HEADER එක ගැනයි. මීලගට අප හට ඇත්තේ IMAGE_NT_HEADERS යන කොටසයි. මෙම කොටස තුල program එකක ‍වැදගත්ම කොටස තිබේ. මෙහි file offset එක සොයා ගැනීම ඉතා පහසුය. IMAGE_DOS_HEADER යටතේ ඇති e_lfanew මගින් මෙහි file offset එක සොයා ගත හැක. ‍මෙම කොටසේ විශාලත්වය 248 bytes වේ. එය PE32 application එකක් සදහා නියත අගයකි.

එය ප්‍රධාන කොටස් 3 කට වෙන් කළ හැක.

1. IMAGE_NT_SIGNATURE
2. IMAGE_FILE_HEADER
3. IMAGE_OPTIONAL_HEADER

IMAGE_NT_SIGNATURE

මෙහි IMAGE_NT_SIGNATURE යනු 32 bits විශාලත්වයක් ඇති Dword එකකි. එහි 0x00004550 (PE) යන අගය පවතී.

IMAGE_FILE_HEADER

මීලගට තිබෙන්නේ IMAGE_FILE_HEADER යන කොටසයි. මෙය තවත් කොටස් 7 කට වෙන් කල හැක.

typedef struct _IMAGE_FILE_HEADER {
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

*(මෙය microsoft ආයතනයෙන් සකසන ලද winnt.h නැමැති source file එකෙහි හදුන්වා ඇති ආකාරය වේ.)

මෙහි Machine මගින් දක්වනුයේ අදාල program එක සුදුසු වන CPU platform එකයි. මේ සදහා පහත අගයන් තිබිය හැක.


0x01D3 - Matsushita AM33
0x8664 - x64
0x01C0 - ARM little endian
0x0EBC - EFI byte code
0x014C - Intel 386 or later processors and compatible processors
0x014D - Intel 486 or later processors and compatible processors
0x014E - Intel pentium or later processors and compatible processors
0x0020 - Intel Itanium processor family
0x9401 - Mitsubishi M32R little endian
0x0200 - Intel 64
0x0266 - MIPS16
0x0366 - MIPS with FPU
0x0466 - MIPS16 with FPU
0x0160 - R3000 (MIPS) processor, big endian
0x0162 - R3000 (MIPS) processor, little endian
0x0166 - R4000 (MIPS) processor, little endian
0x0168 - R10000 (MIPS) processor, little endian
0x0169 - MIPS little-endian WCE v2
0x0184 - DEC Alpha AXP processor
0x0284 - ALPHA64 or AXP64
0x01F0 - IBM Power PC, little endian
0x01F1 - Power PC with floating point support
0x01A2 - Hitachi SH3
0x01A3 - Hitachi SH3 DSP
0x01a4 - SH3E little-endian
0x01A6 - Hitachi SH4
0x01A8 - Hitachi SH5
0x01C2 - Thumb
0x0000 - Any machine type

NumberOfSections - මගින් දැක්වෙන්නේ අදාල program එක තුල ඇති sections ගනනයි. අප sections ගනන වෙනස් කරන්නේ නම් මෙහි ඇති අගයද වෙනස් කල යුතුය. sections පිළිබදව පසුව ලිවීමට බළාපොරොත්තු වෙනවා.

TimeDateStamp - මගින් දැක්වෙන්නේ program එ
ක නිර්මානය කල දිනය හා වේලාවයි. එහි සදහන්ව ඇත්තේ 1970 ජනවාරි මස 1 දින පෙ.ව. 12.00.00 සි‍ට ගත වූ තත්පර ගනනෙහි hex value එකයි.

උදා - 0x3B7D8410 ලෙස ඇත්නම්
998081552 seconds = 31:07:15 20:52:32

PointerToSymbolTable හා NumberOfSymbols සෑම අවස්ථාවකම 0x00000000 අගයෙහි පවතී. එය debug කිරීම් සදහා යොදා ගනී.

SizeOfOptionalHeader - මෙමගින් IMAGE_OPTIONAL_HEADER හි විශාලත්වය දක්වා ඇත. එය PE file එකෙහි නිරවද්‍යතාව පරීක්ෂා කිරීමට යොදා ගත හැක.




Characteristics මගින් අදාල program එක dll හෝ exe එකක්ද යන්න
බලා ගත හැක.
Characteristics ලබා ගැනීමට නම් පළමුව එය bit වශයෙන් පහත ආකාරයට වෙන් කර ගත යුතුය.



පහත දැක්වෙන bits, 1 ලෙස සටහන්ව ඇත්නම් ඒවාට පහත Characteristics තිබේ.

bit 0 (IMAGE_FILE_BYTES_REVERSED_HI) - Big endian: the MSB precedes the LSB in memory.
bit 1 (IMAGE_FILE_UP_SYSTEM_ONLY) - The file should be run only on a uniprocessor machine.
bit 2 (IMAGE_FILE_DLL) - The image file is a dynamic-link library (DLL).
bit 3 (IMAGE_FILE_SYSTEM) - The image file is a system file, not a user program. (ex: driver)
bit 4 (IMAGE_FILE_NET_RUN_FROM_SWAP) - If the image is on network media, fully load it and copy it to the swap file.
bit 5 (IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP) - If the image is on removable media, fully load it and copy it to the swap file.
bit 6 (IMAGE_FILE_DEBUG_STRIPPED) - Debugging information is removed from the image file.
bit 7 (IMAGE_FILE_32BIT_MACHINE) - Machine is based on a 32-bit-word architecture.
bit 8 (IMAGE_FILE_BYTES_REVERSED_LO) - Little endian: the least significant bit (LSB) precedes the most significant bit (MSB) in memory.
bit 9 - This flag is reserved for future use.
bit 10 (IMAGE_FILE_LARGE_ADDRESS_AWARE) - Application can handle > 2 GB addresses.
bit 11 (IMAGE_FILE_AGGRESIVE_WS_TRIM) - Obsolete. Aggressively trim working set.
bit 12 (IMAGE_FILE_LOCAL_SYMS_STRIPPED) - COFF symbol table entries for local symbols have been removed.
bit 13 (IMAGE_FILE_LINE_NUMS_STRIPPED) - COFF line numbers have been removed.
bit 14 (IMAGE_FILE_EXECUTABLE_IMAGE) - Image only.
bit 15 (IMAGE_FILE_RELOCS_STRIPPED) - Image only, Windows CE, and Windows NT® and later.

උදා:- 0x010F ලෙස ඇත්නම් එය 2 පාදයට ගත් විට 0000000100001111 ලෙස වේ.
එවිට මෙය - [Machine is based on a 32-bit-word architecture.],[COFF symbol table entries for local symbols have been removed.],[COFF line numbers have been removed.],[Image only.],[Image only, Windows CE, and Windows NT® and later.]

මෙහි දක්වා තිබෙන්නේ IMAGE_NT_HEADERS හි උප කොටස් වේ.


අදට මෙතනින් ලියන එක නවත්වනවා ලබන සතියේ IMAGE_OPTIONAL_HEADER ගැන විස්තර කරන්න බලාපොරොත්තු වෙනවා.

5 comments:

  1. මේව ඉගෙන ගන්න ආසයි. මෙවත් සමග උදාහරන දුන්නනම් ඉතා හොදයි

    ReplyDelete
  2. ස්තුතියි ඔබගේ වටිනා අදහස් වලට

    -------------------------------------------------
    www.sriweb.info

    ReplyDelete
  3. උදාහරන ලෙස ගෙන තිබෙන්නේ calc.exe හා shell32.dll වේ. ඒවා පින්තූර හා වගු ලෙස දක්වා ඇත. පින්තූර වලදී ඉහත පහදා ඇති කොටස් රතු වර්ණයෙන් දක්වා ඇත. වගුව තුලදී උදාහරනය ලෙස ගත් PE එකෙහි ඇති දත්තද දක්වා ඇත.

    ReplyDelete
  4. Nice work..
    remove your Word verification machan

    ReplyDelete