# Function call obfuscation

## <mark style="color:red;">Calling External Functions using WIN32 API</mark>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FFciy0J8MEDnQGwdgHECy%2Fimage.png?alt=media&#x26;token=fac60730-395e-4a6f-b30c-d8b32f1843de" alt=""><figcaption></figcaption></figure></div>

every PE file like EXE and DLL usually uses external files that means that it will call functions implemented in external DLLs which will be mapped into the process memory to make this functions available for the process code

AV industry realized by analyzing what kind of external DLLs and functions used by the binary it can be a good indicator if this binary is malicious or not, and it's all done before running the binary (Static Analysis) so AV Engines analyzes a PE file on disk by taking a look at its import address table Section which is a dedicated section in a PE file and reviews the functions and compares them to the list of functions that is known to be used by malware developers

this method generates false positives

function call obfuscation is a method to hide DLLs and external functions that will be called during runtime to do that we can use **GetModuleHandle** and **GetProcAddress**

* <mark style="color:green;">**GetModuleHandle :**</mark> handle = GetModuleHandle("Windows32.h") returns a handle to a DLL
* <mark style="color:green;">**GetProcAddress :**</mark> GetProcAddress(handle, "VirtualAlloc") allows you to get a memory address of the function you need which is exported by the dll

### <mark style="color:blue;">List Import Address Table using DumpBin</mark>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FgE4wgvsMESnUcUTZiQRd%2Fimage.png?alt=media&#x26;token=c83af64f-4e76-40ee-a01f-47705798135b" alt=""><figcaption></figcaption></figure></div>

### <mark style="color:blue;">Let's Get rid of VirtualProtect from the import address table using function call obfuscation</mark>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F5XOMytLsaNZrkrUW6y2S%2Fimage.png?alt=media&#x26;token=eded5868-f27e-4593-8a44-c8bbde5540e9" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fe7h9MJaKUiWNi2QZ5WmT%2Fimage.png?alt=media&#x26;token=27e5a9ac-46e3-47c7-b44a-f61edc1f35f5" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fq3NrWfyjzVtBbp8esRQt%2Fimage.png?alt=media&#x26;token=597f70fa-2249-4679-bdeb-1505dd097a82" alt=""><figcaption></figcaption></figure></div>

this is function pointer declaration in C/C++ programming language. Specifically, it declares a function pointer named pVirtualProtect that points to the VirtualProtect function the pointer pVirtualProtect stores the address of the VirtualProtect function

by doing this you can indirectly call the VirtualProtect function through the function pointer pVirtualProtect. This provides flexibility and allows for dynamic function invocation based on runtime conditions or function indirection

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F53QEPtKjKjAjEGJq6SEI%2Fimage.png?alt=media&#x26;token=d1118a72-da60-40db-9626-b0a0d945a490" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FDSka8gaG4uVq1dJEs4uC%2Fimage.png?alt=media&#x26;token=c0b77a0c-bc88-41d0-a7ab-ff03ea6e2ccd" alt=""><figcaption></figcaption></figure></div>

now if we compile the program and list the import address table we will not find the VirtualProtect function in the list

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FxG5IAROiaFlShhtJdT20%2Fimage.png?alt=media&#x26;token=bf63f515-8fa0-4ce0-b93f-1ad9438e4b96" alt=""><figcaption></figcaption></figure></div>

let's make sure by using `findstr`

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FgN5cwC7MJUfd2JDUN2uz%2Fimage.png?alt=media&#x26;token=fba76ef3-4877-409f-812b-7a2d9affb105" alt=""><figcaption></figcaption></figure></div>

but if we use strings we will find that VirtualProtect string is still present in the binary and this is because when we have called GetProcAddress function we passed "VirtualProtect" string in clear text and we can go around this using XOR

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fp9ffWCQPhSDXz0t4Jstp%2Fimage.png?alt=media&#x26;token=600cb088-1420-445f-b4bd-db3b9b6d0a98" alt=""><figcaption></figcaption></figure></div>

let's set the key to

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F5rOEFqAe7YYIFMLsVU7s%2Fimage.png?alt=media&#x26;token=dca9a7b6-bcb2-4d97-8830-e0ec76da26e4" alt=""><figcaption></figcaption></figure></div>

now let's encrypt the "VirtualProtect" string with the key using a python script

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fbu6WEX5IVwfZczLhxhpB%2Fimage.png?alt=media&#x26;token=0b20784f-3369-407d-9869-f3173dd65569" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FkeyNxnDnvHu0UVyeSDNm%2Fimage.png?alt=media&#x26;token=70ea552c-8eb8-442a-bc3a-9026d2c84fd2" alt=""><figcaption></figcaption></figure></div>

and now if search for the string "VirtualProtect" in the binary strings we will not find anything

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FoeMTmIDKA5SmTJLg6xCD%2Fimage.png?alt=media&#x26;token=ddb949c8-b9a1-493e-9473-047e39b83858" alt=""><figcaption></figcaption></figure></div>

so we have fully obfuscated the function call of the function VirtualProtect

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

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FK1bTSL40cAMJqhtD5YeG%2Fpasted_image026.png?alt=media&#x26;token=41ba97b2-6d23-4104-8790-d402f859c67b" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FetherwpNmeERRvJ6pCzr%2Fimage.png?alt=media&#x26;token=df6a0df8-61c1-4cff-bcbf-ebc5fe0b253f" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Ficv1C1PhqGF1pfiA8M0p%2Fimage.png?alt=media&#x26;token=27b04236-d854-439a-b460-dd5745d6a224" alt=""><figcaption></figcaption></figure></div>
