# Basic Malware Reverse Engineering

## BASIC MALWARE RE WRITEUP

*By Jacvbtaylor*

```
       Used Ghidra and Kali Linux 
                 https://tryhackme.com/basicmalwarere
         completed April 10, 2023
```

### OBJECTIVE

Using static analysis, the task at hand is to discover which string in each .exe file is going to be hashed using MD5 once it is executed. The usefulness provided by these challenges extends to real world scenarios where malware may be encrypting compromised data. Being able to discover how it is encrypted can lead to recovering the data.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FsMREk8iEn47Daf72EK70%2Fimage.png?alt=media&#x26;token=8ac7dae8-5c40-4315-bde1-ef6d5f797ced" alt=""><figcaption></figcaption></figure>

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2F0c3v2FWcqd774Cy6DBW6%2Fimage.png?alt=media&#x26;token=21b7996b-b9cc-4adf-8488-71952ddeace6" alt=""><figcaption></figcaption></figure>

### Strings1.exe\_

After importing all three .exe files into our project using [Ghidra](https://ghidra-sre.org/), the first step is to analyze the first file, *strings1.exe\_*.

Starting with the symbol tree, open the functions select *entry.*&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2Fmm4HEjBV7kV6kboc5M7B%2Fimage.png?alt=media&#x26;token=e80bfc4c-5e39-4744-9420-54cf2fbe2c7e" alt=""><figcaption><p>Symbol Tree</p></figcaption></figure>

Selecting *entry* should have kick-started code population in the *Listing* and *Decompile* window. First looking at *Decompile*, a character pointer is revealed in C code that is instantiated and set to equal a defined string set as *md5\_hash.*

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FZR7cqLKzCEDznduA4Ovv%2Fimage.png?alt=media&#x26;token=552c3507-5bac-487f-a929-a2f6748246fd" alt=""><figcaption><p>entry</p></figcaption></figure>

To uncover the full string, first double click *md5\_hash* in the *Decompile* window next moving to the *Listing* window where the ID *00424828* is shown.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FzyfUFVKlAMumwMNYmEos%2Fimage.png?alt=media&#x26;token=8a3fdf7b-ca1e-4d66-85c7-05047fb602f3" alt=""><figcaption><p>Listing</p></figcaption></figure>

Double click that ID to move the Listing to the line that reveals the flag in its entirety as the string that will be hashed upon file execution.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FucATR2Ri3piYyYutuw4P%2Fimage.png?alt=media&#x26;token=d8831ad0-dd1c-4d0f-83bc-37513806aae0" alt=""><figcaption><p>flag</p></figcaption></figure>

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FkQ3kjS3NntWgSqbxiFkG%2Fimage.png?alt=media&#x26;token=c5b7649d-61ff-46dd-8803-502702a3d746" alt=""><figcaption></figcaption></figure>

### Strings2.exe\_

To begin the analysis of *strings2.exe\_*, begin in the *Symbol Tree* and select *entry*.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FyARUrz5HCxYPUylCKN82%2Fimage.png?alt=media&#x26;token=31a9a2b2-e2e6-4e91-8d62-a41f4bc24503" alt=""><figcaption></figcaption></figure>

&#x20;Decompile should now reveal the following C code, listing mostly variables, defined and undefined. It is easy to assume these variables will be added in the stack from how they are assigned. additionally, a character pointer is assigned to *md5\_hash* and *local\_2c*.&#x20;

<div><figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FdN2vzZndX5cfLvBIpqCN%2Fimage.png?alt=media&#x26;token=66042668-7377-4c2e-9f54-023e6e50b8e1" alt=""><figcaption><p>undefined variables</p></figcaption></figure> <figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FUgXNyehEfkOBREoHCePS%2FScreenshot%202023-04-10%20174329.png?alt=media&#x26;token=af5913a2-c95a-4825-b43d-ff36e4fc9eb7" alt=""><figcaption><p>defined variables</p></figcaption></figure></div>

Studying the local variables, it is clear that each definition is set to a hex value, excluding *local\_2c* which is defined as "*F*". Taking these values and assembling them through bash scripting allows for converting these hex values with ease.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FjunWPlQYqCTW1DZWcyu1%2Fimage.png?alt=media&#x26;token=b1860d05-85b0-4a4e-b47c-d264cc151894" alt=""><figcaption><p>bassh script</p></figcaption></figure>

Using rapid tables, all of the hex values were converted to ASCII characters which revealed our flag - the only thing missing was the defined value of *local\_2c* which was "*F*".

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2F4QDKbzS65MvBWYKSLt27%2Fimage.png?alt=media&#x26;token=b653b3bf-add7-4a44-ba43-0be0a6e3cb56" alt=""><figcaption><p>hexadecimal - ASCII</p></figcaption></figure>

Following the steps described above discloses the string that will be hashed upon file execution.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FdQ3qoqHnazIIlvs10VWa%2Fimage.png?alt=media&#x26;token=d77396ff-886d-465f-b45b-9b2814e93108" alt=""><figcaption></figcaption></figure>

### Strings3.exe\_

Lastly, *strings3.exe\_* can be analysed the same way by opening *functions* and *entry* in *Symbol Tree*.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FeNepDVYM3l11q0UDUUu4%2Fimage.png?alt=media&#x26;token=2747fc8a-9461-4ae4-9f55-6981f4451cfc" alt=""><figcaption><p>entry</p></figcaption></figure>

The *Listing* window doesn't show enough information for our task at hand to be resolved, so we move to the *Decompile* window.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FpOUmaKCpIBb1QyJGGlV3%2Fimage.png?alt=media&#x26;token=5e5993c2-8e37-4853-90a3-b55936ad430f" alt=""><figcaption><p>Listing</p></figcaption></figure>

From this view, a character pointer is described to be equal to *local\_a0*, or *MD5*. This is a good implication that we are on track for the string we are looking for. Examining this entry further, two interesting fields stand out: *FindResourceA* and *LoadStringA*.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FHDdQp15c8iTGiV3WNvON%2Fimage.png?alt=media&#x26;token=4a8cd2cb-8055-4e0a-aae5-bddfcabd8951" alt=""><figcaption><p>Decomplie</p></figcaption></figure>

Double Clicking *FindResourceA* changes thge *Listing* window to the following lines of the code. As shown, *FindResourceA* appears to be from the *Kernel32 library* which is loaded into protected memory upon system boot-up. In other words, this library is used by Windows OS in part of the shared dynamically-linked libraries. It will be used by multiple processes while one copy is stored in memory.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FSmPEUgoPRJjjfiWbNjuy%2Fimage.png?alt=media&#x26;token=d966258a-3b32-44a1-8bb9-a7e31a2c1f27" alt=""><figcaption><p>Kernel32</p></figcaption></figure>

With this understanding established, the new step is to locate *LoadStringA*. Double clicking this field in *Decompile* should change the *Listing* window to the following external function.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FzMVETf6EFnW6HF2ZpCHC%2Fimage.png?alt=media&#x26;token=96ce12f2-4179-458c-a807-1ce344404d87" alt=""><figcaption><p>User32</p></figcaption></figure>

Knowing that this exe file is compiled with C code, these two linked libraries are critical for the execution inside of the Windows OS. Without loading or linking the Kernel and User DLL, the program will not be able to receive input, or produce output. Specifically for *User32* we want to focus on the *hInstance* parameter which is more clearly set inside the *Decompile* window.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2F9H6J3vuZVtSJxe4XIO7m%2Fimage.png?alt=media&#x26;token=e4a6c785-74d3-461a-9313-694d32f40822" alt=""><figcaption><p>HINSTANCE</p></figcaption></figure>

Analyzing line 18, or *LoadStringA*, we can presume the string is being loaded from *0x110* and being stored in variable *local\_4a4*. Double clicking 0x110 loads the following into the Listing window, which provides a hint to the string we are looking for, but we need to be more precise.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2F1HOMPKGRX4OTSA72PpEA%2Fimage.png?alt=media&#x26;token=ff6691a7-224c-4291-8266-ca6848fa6c97" alt=""><figcaption><p>Listing</p></figcaption></figure>

in Ghidra, hovering over the string identifier provides insight that can be used - the decimal value of 0x110 is converted to 272.

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FcdcTycg2Wr5tDeyYl90z%2Fimage.png?alt=media&#x26;token=200a6fee-bff4-4157-b122-1e525976f6fc" alt=""><figcaption><p>Decimal</p></figcaption></figure>

In the Listing window, we can locate the Hex values and scroll until Rsrc String ID 272 is presented, which provides the string that will be hashed upon file execution.&#x20;

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FYySDKjfP30EKsbk5tfAE%2Fimage.png?alt=media&#x26;token=0b13ad26-da8a-41be-bef6-4099ac5d3106" alt=""><figcaption></figcaption></figure>

<figure><img src="https://638157146-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmCRN3OXOB1Xor2mgrH7f%2Fuploads%2FxuapLz8VWEYF2roVTvAD%2Fimage.png?alt=media&#x26;token=15df6758-4dfc-4166-8e9a-d3c902ebfca6" alt=""><figcaption></figcaption></figure>
