PE Headers DLLs EXEs revision| Malware Analysis from Malware Development Part 1.

Mahmoud NourEldin
9 min readFeb 1, 2023

--

مراجعه سريعه على البي إي هيدرز وبإذن الله لما أكتب السلسله التالته هنستخدم البي إي هيدرز بشكل متعمق أكثر لكن السلسلة دي مش هيتم استخدام ال بي إي هيدرز إلا نوادر لأنها ابتدائيه وليست احترافيه.

PE format is a specification that describes the structure of executable (image) files and object files under the Windows family of operating systems.[MDSN].

هو شكل وصفي للملفات التنفيذيه :)

Assalamu-Alikum السلام عليكم ورحمة الله وبركاته

If you want to study physics, read that book and if you need reading poems, take this book. But if you need to study malware dev and analysis, you should learn PE headers.

اعتبر نفسك عندك مكتبه منظمه وعارف أماكن الكتب الخاصه بالتاريخ مثلًا موجودين فالرف اللي فوق فبتروح للرف الخاص بالتاريخ وتأخذ كتاب ومن الكتاب فيه فهرس بيعرفك الكتاب دا بيتكلم عن تاريخ مين وحصل ايه وكل الحوارات المتسجله فبالتالي من الفهرس بتعرف الكتاب دا بيتكلم عن تاريخ ايه.

كذلك ويندوز محتفظ بكل أنواع الملفات بأشكال معينه فحيث كل نوع ملف ولنفرض ملفذ تنفيذي ( كتاب التاريخ ) وخدنا الملف التنفيذي دا ونشوف البي إي هيدرز ( الفهرس ).

لازم أكتب انجليزي لوحده وعربي لوحده علشان ميديم مش بيدعم الاتنين مع بعض فاعذروني.

PE headers are a container for file data like it’s DLL or EXE, CUI or GUI, it’s x86 x64, or x86 x32, When was this file compiled[ maybe fake]? All data of the file which include imports, exports APIs, sections, entropy, the entry point of the program, machine type, resource, and a lot of info you can find here:

PE format from MDSN:

يعني فهمنا إن البي إي هيدرز عباره عن فهرس لأشكال الملف التنفيذي.

Also here is my static analysis and how we as a malware analyst use it for analyzing:

Here is an infographic for the PE headers by corkami:

Figure(1): PE headers infographic by corkami

So What’s PE File Format?

PE refers to the Portable Executable format of Windows which some derived from COFF [ Common Object File Format ].

ركز في الانجليزي والعربي علشان بكتبلك الاتنين ومش بترجم يعني الجمله الانجليزي مش بترجمها لعربي فأنت ركز في الاتنين بحيث الاجنبي يفهم والعربي يفهم

This format specifies executable, object code, DLLs, and others that Windows use for loading files and running it on the CPU.So PE format is a data structure for windows loader.

The figure above and below shows you the detailed PE File format outline.

Figure(2): PE Headers by Wikimedia

And here if we open any executable file in the hex editor:

دا هنا شكل الملف التنفيذي اللي بيقرأه ويندوز علشان ينفذه ومنه بيعرف يعمل ايه بالضبط بخصوص التعليمات الموجوده.

Figure(3): PowerLoaderV2 Malware in the hex editor tool.

Here are PE headers in PE-bear [ mostly used by writers ]:

Figure(4): PowerLoaderV2 Malware in PE-bear tool.

Here are PE headers in PEStudio[ mostly used by analysts ]:

Figure(5): PowerLoaderV2 Malware in pestudio tool.

So the PE format includes:

DOS Header, _IMAGE_DOS_HEADER

DOS Stub,

Rich Header,

PE File Header,

Image Optional Header,

Section Table,

Data Dictionaries,

and Sections.

Figure(6): PE Format by tech-zealots.

NOTE: This article is for revision not for explaining what PE Headers are, My goal is to take your knowledge to Dev and analysis.

يعني المقال دا يعتبر مراجعه مش شرح فياريت تشوف المصادر اللي هقولها بإذن الله تعرف منها ايه هو بالضبط البي إي هيدرز وتعالى هنا نشوف ايه المهم.

DOS Headers[MS-DOS Header]:

is the first 64-byte in the PE which begins with the “MZ”{ Mark Zbikowski, one of the developers of MS-DOS } header called e_magic. This Magic number [0x5A4D -> “MZ”]can be used for checking if it’s a PE file format or not [50% checking].

وهنا بيتم عمل تشيك هل الرقم دا موجود فعلًا ولا لأ نتأكد منه الملف دا تنفيذي ولا لا ومستخدمه بكثره في اليارا رولز.

Yara Rules Writers uses it a lot.

The last e_lfanew header which exists in 0x3C of _IMAGE_DOS_HEADER contains the offset of the PE Header. So from this value, we can reach PE File Header.

من قيمة الهيدر دا بنقدر نوصل لعنوان البي إي هيدر ودا مستخدم بكثره في المالويرز بيعمل تشيك لإم زاد ومن ثم يزود 3سي على الهيدر علشان يوصل للبي إي هيدر ويعمل تشيك لبي إي فاليو.

علشان يتأكد الملف دا تنفيذي ولا لأ.

زي هنا في تحليلنا في السلسلة الأولى الغير مكتمله شوفنا المالوير بيعمل تشيك لقيمة إم زاد ومن ثم يزود 3سي ويعمل تشيك لقيمة بي إي وطبعًا هما بالعكس لأنهم في الميموري.

وهنا السي كواد للبي إي هيدرز

typedef struct _IMAGE_DOS_HEADER {      // DOS .EXE header
WORD e_magic; // Magic number
WORD e_cblp; // Bytes on last page of file
WORD e_cp; // Pages in file
WORD e_crlc; // Relocations
WORD e_cparhdr; // Size of header in paragraphs
WORD e_minalloc; // Minimum extra paragraphs needed
WORD e_maxalloc; // Maximum extra paragraphs needed
WORD e_ss; // Initial (relative) SS value
WORD e_sp; // Initial SP value
WORD e_csum; // Checksum
WORD e_ip; // Initial IP value
WORD e_cs; // Initial (relative) CS value
WORD e_lfarlc; // File address of relocation table
WORD e_ovno; // Overlay number
WORD e_res[4]; // Reserved words
WORD e_oemid; // OEM identifier (for e_oeminfo)
WORD e_oeminfo; // OEM information; e_oemid specific
WORD e_res2[10]; // Reserved words
LONG e_lfanew; // File address of new exe header
} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;

And here is the value of e_lfanew:

In 0x3C place exists the D8 value which is the beginning of the PE header.

And the PE header starts with 0x5045 “PE” in memory.[ we will discuss this later ].

So Continue reading the details here:

ِAnd here is a good source for a rich header:

I prefer sharing articles not reinventing the wheel, continue reading these articles and come back.

لازم تقرأ المقالات اللي فوق لأن المقال دا يعتبر مراجعه مش شررررح

So now what know?

We can know from DOS Header the magic[0x5A4D] number and where is the PE Header[0x3C].

And from DOS Stub Header, we know it prints “ This program cannot be run in DOS mode” if it’s executable not for DOS.

And from Rich Header we can know the IDE of the malware and the build number of the Visual Studio, with this info, the compile time stamp, and the linker's path can help us in Threat Hunting and detection. Also, this info can help Microsoft for targeted attacks:

And from File Header, we can know the machine type, number of sections, timestamp of compiler [ may be fake ], 32 or 64, and others as we can see:

How does this tool know in the machine header as Intel? In the figure below, the value of the machine header is 14C.

So we can know the types of machines from this link, and this can help you a lot in analysis:

And for the optional header:

يعني بتطلع منه معلومات كتيره كل معلومه لها أهميه في موقف معين يعني مش كل المعلومات لها كل الاهميه في مالوير واحد فمثلًا مالوير ممكن يستخدم ال

checksum

علشان يتأكد فيه

debugger

شغال ولا لا أو في تعديل حصل في الكود ولا لأ وممكن مالوير بيستخدم حاجه تانيه بعيدًا عن أهمية الانتري بوينت والصب سيستم وكله دا مهم كذلك

And for sections headers

دا مهم جدًا لأنه منه بتعرف حاجات كتيره أوي
هل دا فايل عادي ولا مالوير من اسماء السيشكنز وكذلك لو اسم غريب زي ما انت شايف كدا دا مش معروف فالبتالي ممكن تحدد السيشكن دا للمالوير دا.

وبتعرف هل المالوير مشفر ولا لا

It’s packed or not by knowing the entropy value of every section and if it is above 7 to 8, then it’s packed.

وبرده الحجم بتاعه في الديسك أد ايه وفي الميموري اد ايه فبتعرف انه كام مضغوط ولا لا.

ومن السيكشنز بتعرف إن التيكست سيكشن هو الخاص بالكود والداتا سيكشن هو الخاص بالقلوبال

.text section for instrcutions and .data sections for global variables and data .rsrc section for recource.

وهنعرف المعلومات دي بشكل متعمق في استخدام البايلود لذلك عايزك تركز فيهم دول.

And exports section:

بتقدر تعرف الفنشكنز اللي بيتم تنفيذها لما المالوير دا يشتغل تحت

argument

معين

على سبيل المثال

sdropper32.exe DownloadRunExeId

وهنا عملت استدعاء لفنشكن من الاسبورتس علشان ينفذ دي بدل ما ينفذ المين فنكشن.

فأنا معتمد على معرفتك المسبقه عن ال بي إي هيدرز فلازم تشوف شرح لهم وتيجي هنا

وهنا الامبورتس بتاعتنا

والمقاله دي هيتم تحديثها في السلسله التالته بإذن الله لأننا مش هنستخدمها في السلسله دي إلا قليل أنا عايزك تعرف بس مفهوم البي إي هيدرز وازاي بنستخدمه في الاناليزيز بشكل بيزيك.

DLLs and EXEs revision

Executable files are a lot but the most famous are EXEs and DLLs.

EXE: refers to Execute and that means when you click on it, the windows loader will create an independent process then initialize memory space for this process, then load a PE file to execute some code.[ for all executable files also ].

DLLs: refers to Dynamic Link Library which is a complete PE file also but it doesn’t independent process and not running by double-clicking on it because you need another process for running it[ that’s why malware prefers DLLs ]. Also, DLLs have an export table for APIs you can use.

EXEs begin running the code from the main() function and DLLs if you not pushing an export API, will call DLLMain() function.

To Be Continued…يتبع

--

--

Mahmoud NourEldin
Mahmoud NourEldin

Written by Mahmoud NourEldin

Threat Researcher and Malware Analyst.

No responses yet