www.SecurityXploded.com
Disclaimer
The Content, Demonstration, Source Code and Programs presented here is "AS IS"
without any warranty or conditio...
Acknowledgement
 Special thanks to null & Garage4Hackers community for their extended support and
cooperation.
 Thanks t...
Reversing & Malware Analysis Training
This presentation is part of our Reverse Engineering & Malware Analysis Training
pro...
Who am I #1
Amit Malik (sometimes DouBle_Zer0,DZZ)
 Member SecurityXploded
 Security Researcher @ McAfee Labs
 RE, Expl...
Who am I #2
Swapnil Pathak
 Member SecurityXploded
 Security Researcher @ McAfee Labs
 RE, Malware Analysis, Network Se...
Course Q&A
 Keep yourself up to date with latest security news
 http://www.securityphresh.com
 For Q&A, join our mailin...
PE File Format
 PE – Portable Executable
 PE is the native Win32 file format.
 32-bit DLL, COM, OCX, Control Panel Appl...
Why PE File Format
 How windowsloaderloads theexecutablein memory.
 How loaderbuild theimport and export tablefor a modu...
Basic Structure
www.SecurityXploded.com
Basic Structure Cont.
 Most common sections found in executable are
 Executable Code section (.text , CODE)
 Data Secti...
Headers – DOS Header
 All PE files start with DOS header
 First 64 bytes of the file.
 Run program in DOS.
 Runs the D...
Header- DOS header cont.
e_magic = 4D, 5A (MZ)
e_lfanew is a DWORD which contains the offset of the PE header
www.Security...
Headers – PE header
 Begins with signature (DWORD) 50h, 45h, 00h, 00h
 Letters “PE” followed by two terminating zeros
 ...
Headers – PE –> File header
 Machine
 NumberOfSections
 SizeOfOptionalHeader
 Characteristics
www.SecurityXploded.com
Header – PE –> Optional Header
www.SecurityXploded.com
Optional Header Cont.
 AddressOfEntryPoint
 ImageBase
 SectionAlignment
 FileAlignment
 SizeOfImage
 SizeOfHeaders
...
Header – PE –> Optional –> Data
Directory
 Last 128 bytes of OptionalHeader
 Array of 16 Image_Data_Directory structures...
Data Directories
www.SecurityXploded.com
 IMAGE_DIRECTORY_ENTRY_EXPORT
 IMAGE_DIRECTORY_ENTRY_IMPORT
 IMAGE_DIRECTORY_ENTRY_RESOURCE
 IMAGE_DIRECTORY_ENTRY_TLS...
Headers - Section Header
 Array of IMAGE_SECTION_HEADER
 Equal to the numberofsections – FileHeader member.
 Each struc...
Section Header cont.
 Name – Virtually can be anything in text
 VirtualSize – Size of section in memory
 VirtualAddress...
 The structure of PE file on disk is exactly the same as when it is loaded into
memory.
 The windows loader maps the req...
Type of PE file sections
 Executable code
 Data
 Resources
 Export section
 Import section
 Thread Local Storage (TL...
Export Section
 Relevant to DLLs
 Export functions in two ways
- By name
- By ordinal only
 Ordinal – 16 bit value that...
nName
nBase
NumberOfFunctions
NumberOfNames
AddressOfFunctions
AddressOfNames
AddressOfNameOrdinals
www.SecurityXploded.com
Export by Ordinal only
Export Forwarding
www.SecurityXploded.com
Import Section
 Contains information about all functions imported by executable from
DLLs
 Loader maps all the DLLs used...
Import Directory
 20 byte structure IMAGE_IMPORT_DESCRIPTOR
 Number of structures = Number of DLLs imported
 Last struc...
 OriginalFirstThunk
 Name1
 FirstThunk
Hint
Name1
www.SecurityXploded.com
 Each IMAGE_THUNK_DATA str corresponds to one imported function from
the dll.
 Arrays pointed by OriginalFirstThunk and ...
Functions exported by ordinal only
- No IMAGE_IMPORT_BY_NAME structure
-IMAGE_THUNK_DATA contains the ordinal of the funct...
DEMO
www.SecurityXploded.com
Reference
 Complete Reference Guide for Reversing & Malware Analysis Training
www.SecurityXploded.com
PE file format test
 Write a program in “C” or “ASM” that will modify the Address of Entry point of
an Executable (.exe) ...
Thank You !
www.SecurityXploded.com
Upcoming SlideShare
Loading in …5
×

Reversing & malware analysis training part 3 windows pe file format basics

958 views
767 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
958
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Reversing & malware analysis training part 3 windows pe file format basics

  1. 1. www.SecurityXploded.com
  2. 2. Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions of any kind. Also the views/ideas/knowledge expressed here are solely of the trainer’s only and nothing to do with the company or the organization in which the trainer is currently working. However in no circumstances neither the trainer nor SecurityXploded is responsible for any damage or loss caused due to use or misuse of the information presented here. www.SecurityXploded.com
  3. 3. Acknowledgement  Special thanks to null & Garage4Hackers community for their extended support and cooperation.  Thanks to all the trainers who have devoted their precious time and countless hours to make it happen. www.SecurityXploded.com
  4. 4. Reversing & Malware Analysis Training This presentation is part of our Reverse Engineering & Malware Analysis Training program. Currently it is delivered only during our local meet for FREE of cost. For complete details of this course, visit our Security Training page. www.SecurityXploded.com
  5. 5. Who am I #1 Amit Malik (sometimes DouBle_Zer0,DZZ)  Member SecurityXploded  Security Researcher @ McAfee Labs  RE, Exploit Analysis/Development, Malware Analysis  Email: m.amit30@gmail.com www.SecurityXploded.com
  6. 6. Who am I #2 Swapnil Pathak  Member SecurityXploded  Security Researcher @ McAfee Labs  RE, Malware Analysis, Network Security  Email: swapnilpathak101@gmail.com www.SecurityXploded.com
  7. 7. Course Q&A  Keep yourself up to date with latest security news  http://www.securityphresh.com  For Q&A, join our mailing list.  http://groups.google.com/group/securityxploded www.SecurityXploded.com
  8. 8. PE File Format  PE – Portable Executable  PE is the native Win32 file format.  32-bit DLL, COM, OCX, Control Panel Applets(.CPL), .NET, NT kernel mode drivers are all PE File Format. www.SecurityXploded.com
  9. 9. Why PE File Format  How windowsloaderloads theexecutablein memory.  How loaderbuild theimport and export tablefor a modulein memory  Fromwhere to start the executionorAddress ofentry point  Answerofthe question “how binary compiled on a version of windowsworks on anotherversion of windows?”  Whereshould attackerattack   Also today’smalwares aregenerally encrypted, packed. In order to rebuild the original binarywe need to know how the binary is structured. www.SecurityXploded.com
  10. 10. Basic Structure www.SecurityXploded.com
  11. 11. Basic Structure Cont.  Most common sections found in executable are  Executable Code section (.text , CODE)  Data Sections (.data, .rdata, .bss, DATA)  Resources section (.rsrc)  Export Section (.edata)  Import Section (.idata)  Debug Information Section (.debug) www.SecurityXploded.com
  12. 12. Headers – DOS Header  All PE files start with DOS header  First 64 bytes of the file.  Run program in DOS.  Runs the DOS stub  Usually the string “This program must be run under Microsoft Windows”  e_lfanew is the pointer to PE or NT header  Structure defined in windows.inc or winnt.h www.SecurityXploded.com
  13. 13. Header- DOS header cont. e_magic = 4D, 5A (MZ) e_lfanew is a DWORD which contains the offset of the PE header www.SecurityXploded.com
  14. 14. Headers – PE header  Begins with signature (DWORD) 50h, 45h, 00h, 00h  Letters “PE” followed by two terminating zeros  File Header- 20 Bytes – contains info about physical layout and properties of the file  Optional Header- 224 Bytes – contains info about the logical layout of the PE file – size given by member of File header www.SecurityXploded.com
  15. 15. Headers – PE –> File header  Machine  NumberOfSections  SizeOfOptionalHeader  Characteristics www.SecurityXploded.com
  16. 16. Header – PE –> Optional Header www.SecurityXploded.com
  17. 17. Optional Header Cont.  AddressOfEntryPoint  ImageBase  SectionAlignment  FileAlignment  SizeOfImage  SizeOfHeaders  Subsystem  DataDirectory www.SecurityXploded.com
  18. 18. Header – PE –> Optional –> Data Directory  Last 128 bytes of OptionalHeader  Array of 16 Image_Data_Directory structures  Each relating to an important data structure like the Import Table  Members  Virtual Address : RVA of the data structure  iSize : size in bytes of the data structure www.SecurityXploded.com
  19. 19. Data Directories www.SecurityXploded.com
  20. 20.  IMAGE_DIRECTORY_ENTRY_EXPORT  IMAGE_DIRECTORY_ENTRY_IMPORT  IMAGE_DIRECTORY_ENTRY_RESOURCE  IMAGE_DIRECTORY_ENTRY_TLS  IMAGE_DIRECTORY_ENTRY_IAT www.SecurityXploded.com
  21. 21. Headers - Section Header  Array of IMAGE_SECTION_HEADER  Equal to the numberofsections – FileHeader member.  Each structure size = 40 bytes www.SecurityXploded.com
  22. 22. Section Header cont.  Name – Virtually can be anything in text  VirtualSize – Size of section in memory  VirtualAddress – section entry offset in memory (RVA)  SizeOfRawData – Size of section on disk  PointerToRawData – section entry offset on disk  Characteristics – Type of section (execuatble, data etc.)  Section Alignment and File Alignment are two important values from optional header that control the entry point of next section. www.SecurityXploded.com
  23. 23.  The structure of PE file on disk is exactly the same as when it is loaded into memory.  The windows loader maps the required sections in memory.  When sections are loaded into memory they are aligned to fit 4KB memory pages (Section Alignment), each section starting on a new page. www.SecurityXploded.com
  24. 24. Type of PE file sections  Executable code  Data  Resources  Export section  Import section  Thread Local Storage (TLS)  Base Relocations (reloc.) www.SecurityXploded.com
  25. 25. Export Section  Relevant to DLLs  Export functions in two ways - By name - By ordinal only  Ordinal – 16 bit value that uniquely defines a function in particular DLL www.SecurityXploded.com
  26. 26. nName nBase NumberOfFunctions NumberOfNames AddressOfFunctions AddressOfNames AddressOfNameOrdinals www.SecurityXploded.com
  27. 27. Export by Ordinal only Export Forwarding www.SecurityXploded.com
  28. 28. Import Section  Contains information about all functions imported by executable from DLLs  Loader maps all the DLLs used by the application into its address space  Finds the addresses of all the imported functions and makes them available to the executable being loaded. www.SecurityXploded.com
  29. 29. Import Directory  20 byte structure IMAGE_IMPORT_DESCRIPTOR  Number of structures = Number of DLLs imported  Last structure filed with zeros www.SecurityXploded.com
  30. 30.  OriginalFirstThunk  Name1  FirstThunk Hint Name1 www.SecurityXploded.com
  31. 31.  Each IMAGE_THUNK_DATA str corresponds to one imported function from the dll.  Arrays pointed by OriginalFirstThunk and FirstThunk run parallelly.  OriginalFirstThunk – Import Name Table – Never modified  FirstThunk – Import Address Table – Contain actual function addresses www.SecurityXploded.com
  32. 32. Functions exported by ordinal only - No IMAGE_IMPORT_BY_NAME structure -IMAGE_THUNK_DATA contains the ordinal of the function -MSB used to identify the same - MSB is set, rest 31 bits are treated as an ordinal value. -Bound Imports www.SecurityXploded.com
  33. 33. DEMO www.SecurityXploded.com
  34. 34. Reference  Complete Reference Guide for Reversing & Malware Analysis Training www.SecurityXploded.com
  35. 35. PE file format test  Write a program in “C” or “ASM” that will modify the Address of Entry point of an Executable (.exe) file with any random address.  Write a program in “C” or “ASM” that will add a new section into an executable (.exe)  For hints shoot us an email  www.SecurityXploded.com
  36. 36. Thank You ! www.SecurityXploded.com

×