# EXE vs DLL

## <mark style="color:red;">EXE</mark>

exe are seperate programs which can be loaded into memory as an independent process

## <mark style="color:red;">DLL</mark>

* dlls are PE modules that are loaded to existing processes and cannot live independentely in the memory
* dll's main purpose is to deliver some functionality a calling process needs

## <mark style="color:red;">Generating a PE file</mark>&#x20;

we need source code and a compiler to translate and compile the code into machine code which is then understandable by the CPU which is executing the program

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FeVJrkNbYgsJ6WUCYCBwR%2Fimage.png?alt=media&#x26;token=15bd97d6-452d-44ff-8cae-32f137ec8062" alt=""><figcaption></figcaption></figure></div>

## <mark style="color:red;">Difference between writing C program for EXE and DLL</mark>

the basic diffrence is how you call your code in your module or program

### <mark style="color:blue;">EXE</mark>

in EXE case there should be a function called main which is called by the OS Loader when it finishes all initialization of the new process

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FYDUuU4EFzm3waBCSRiyM%2Fimage.png?alt=media&#x26;token=fe6de78f-0e2c-4b60-9c69-0a327ccf3401" alt=""><figcaption></figcaption></figure></div>

### <mark style="color:blue;">Compiliing EXE</mark>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F2q3IPyLRFyRWa0NhEGid%2Fimage.png?alt=media&#x26;token=153c7865-1ade-4f78-9de0-16ff40f44a21" alt=""><figcaption></figcaption></figure></div>

### <mark style="color:blue;">DLL</mark>

* in DLL the process is already exist and then it's imported due to the funcionality it implants
* the loader reads a DLL from the disk reserve some space in the target process and loads the DLL into the space and then it calls a dll function called DLL main and this function initializes the library and then the loader hands off the control to the process and then the process an call functions from the loaded DLL
* DllMain is the entry point for DLLs so when the OS Loader wants to load the dll into a process it will call DllMain DLL\_PROCESS\_ATTACH and when it wants to unload the dll from the process it will also call DllMain DLL\_PROCESS\_DETACH

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F4segRLUNZFjEU5PhCVL6%2Fimage.png?alt=media&#x26;token=e52468d9-d89b-4af8-9cfe-d4b76f0008b2" alt=""><figcaption></figcaption></figure></div>

* This code snippet is an example of a Windows Dynamic Link Library (DLL) that exports a single function called "RunME" using the \_\_declspec(dllexport) directive. When this DLL is loaded into a process, the operating system will call the DllMain function to notify the DLL about various events such as process/thread attach and detach.
* In this case, the DllMain function simply switches on the ul\_reason\_for\_call parameter to determine the type of event and takes no further action. This is a common pattern for DllMain implementations where the developer is not interested in performing any initialization or cleanup work.
* The RunME function displays a message box using the MessageBox function with the "MB\_OK" flag, which indicates that the message box should contain an OK button. The message box contains the text "RT Operator, here I come!" and has the title "RTO". The function returns TRUE, indicating success.
* Overall, this code snippet is a basic example of a DLL with a single exported function that displays a message box when called.
* The DllMain function is an optional entry point function for Windows DLLs. It is called by the operating system when various events occur, such as when the DLL is loaded into a process or when a process or thread attaches or detaches from the DLL.

The function takes three parameters:&#x20;

* <mark style="color:green;">**hModule :**</mark> A handle to the DLL module.&#x20;
* <mark style="color:green;">**ul\_reason\_for\_call :**</mark> An indication of why the function was called. This parameter can be one of four values:&#x20;
* <mark style="color:yellow;">**DLL\_PROCESS\_ATTACH :**</mark> The DLL is being loaded into a process for the first time.&#x20;
* <mark style="color:yellow;">**DLL\_PROCESS\_DETACH :**</mark> The DLL is being unloaded from a process.&#x20;
* <mark style="color:yellow;">**DLL\_THREAD\_ATTACH :**</mark> A new thread is being created in the current process and the DLL is being attached to the thread.&#x20;
* <mark style="color:yellow;">**DLL\_THREAD\_DETACH :**</mark> A thread in the current process is exiting and the DLL is being detached from the thread.&#x20;
* <mark style="color:yellow;">**lpReserved :**</mark> Reserved for future use.

The function should return a BOOL value indicating success or failure. In the code snippet provided, the function simply switches on the value of ul\_reason\_for\_call to determine the reason for the function call and takes no further action. The function returns TRUE to indicate success.

### <mark style="color:blue;">Compiling DLL</mark>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FDTrx9wGtwogsc0AME4Gi%2Fimage.png?alt=media&#x26;token=96332994-6c0b-4d8f-83be-17dbb330de98" alt=""><figcaption></figcaption></figure></div>

## <mark style="color:red;">Using dumpbin to list all the functions exported by a DLL</mark>

```powershell
dumpbin /exports implant.dll
```

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FKkCtaMXVoWOhst4MTYx2%2Fimage.png?alt=media&#x26;token=a78dc640-d7a3-4f2b-afef-f0ce0a198edf" alt=""><figcaption></figcaption></figure></div>

**Running dll's exported functions :**&#x20;

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F0evdv3ADZaj2BG2uhllY%2Fimage.png?alt=media&#x26;token=29d53476-d487-46da-8510-549f71f65e2a" alt=""><figcaption></figcaption></figure></div>
