Lab12-01
I haven’t done dynamic analysis for a while but there’s nothing we can do, hopping on my xp machine I did a quick look to the files’ strings.
Looking at the imports of the exe file in PE Bear we can see some interesting functions indicating process injection, the dll has similar info so it will be confirmed in the advanced static analysis.

So let’s get everything ready to run the malware
- Procmon
- Process Hacker
- Process Explorer
- Procdot
- Regshot
The program started by sending a messageBox that “says press ok to reboot” and then it kept spamming it

I first checked regshot, I found this interesting added value
HKU\S-1-5-21-842925246-1715567821-682003330-1003\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{75048700-EF1F-11D0-9888-006097DEACF9}\Count\HRZR_EHACNGU:P:\Qbphzragf naq Frggvatf\znatn\Qrfxgbc\CenpgvpnyZnyjnerNanylfvf-Ynof-znfgre\CenpgvpnyZnyjnerNanylfvf-Ynof-znfgre\Cenpgvpny Znyjner Nanylfvf Ynof\OvanelPbyyrpgvba\Puncgre_12Y\Yno12-01.rkr: 01 00 00 00 06 00 00 00 50 8E 90 22 81 64 DC 01It has the path of the file encoded using ROT13, we will see how this is used in our advanced static analysis.
Looking at procmon this is all the events done by the sample

But it looks ugly so I will use procdot to generate a nice graph.
ok, I expected more events but here we are, so we need to do more analysis

The graph didn’t give much info; it injects a thread in Explorer.exe but we don’t know how it happens. so I looked more into procmon logs. the sample dropped a file named turns out it’s a system dll, the create function can be also used to read files.psapi.dll in C:/windows/system32, the file had the following exports

So we can consider this file as an IoC.
Ok, let’s start with Advanced Static Analysis, fire up IDA with Lab12-01.exe and we get jump scared with these indirect calls

After enumProcesses is done, an injection routine is being performed using VirtualAllocEx, WriteProcessMemory, GetModuleHandleA, GetProcAddress and CreateRemoteThread, it writes 260 bytes to the process.

The other branch calls a function named sub_401000

Which searches for the process explorer.exe, that we know from dynamic analysis is the injection target.

it then opens the process gets a handle and the process is repeated

So let’s look at the dll file

It creates a thread with a StartAddress that contains the code we see after running the malware, which gets injected in explorer.exe.

After confirming the analysis from the graph using static analysis, I decided to get back and check the injected code.
First in process explorer as the book explained in the solution(I tend to check the solution to see if I missed anything)

It shows that Lab12-01.dll is injected into the process.
Moving to a more modern solution Process Hacker we can check all the threads of the process by right click -> properties -> threads tab.

With that we can easily terminate the injected threads and stop the malware from spamming pop-ups.
Quetsions
-
What happens when you run the malware executable? The malware starts spamming pop-ups(
MessageBoxW) every minute. -
What process is being injected?
explorer.exeis being injected with a code that spams pop-ups every minute. -
How can you make the malware stop the pop-ups? By restarting the process or the system so the process starts again without the injected code as the malware doesn’t achieve persistence. Another modern alternative is terminating the injected threads using
Process Hackerno need to restart the process. -
How does this malware operate? The malware starts by looking for
explorer.exe, it injects its payload in it which sends a pop-up every minute, then it kills itself and the code keeps executing until the process or the system are restarted.
Lab12-02
For this lab we are provided with an exe file

Looking at the imports it seems to be importing resources-related APIs and performs process injection.
Since it deals with resources, I loaded up Resource Hacker but it wasn’t that helpful a blob of data so probably advanced static analysis will give away some decryption routine

Before I deep dive into Advanced Static Analysis, I took a look using floss.exe

We have host.exe and \svchost.exe as encoded strings, which might be the injection target.
Starting with advanced static analysis, a few functions are called in main

Starting with sub_40149D, the code is simple it calls GetSystemDirectoryA function which usually returns C:\Windows\System32, the function takes \svchost.exe as an argument and _strncat is called which indicates the function is returning the path C:\Windows\System32\svchost.exe.

The next function sub_40132C is responsible for loading data from resources section.

After loading the data, the program checks if it starts with MZ if not; it’s sent a the function sub_401000

Looking at function sub_401000 it looks like a decryption routine judging from the xor operation and the fact it’s a loop.

Looking at the decompilation since it’s easier to understand

So the function takes an argument a3, which is used an XOR decryption key for every bit in a1 which is the payload.

Looking at the function call we find the key is 65 or 41 in hex, now jumping into طباخ الزايبر

The decryption resulted in a PE, will leave it for now and check the last function in our program.
After loading and decrypting the resource the return value is passed to the function sub_4010EA so let’s inspect it

The function starts by creating a suspended process of svchost.exe

Then memory of size 716 bytes is allocated with MEM_COMMIT Allocation type and PAGE_READWRITE protect.
Then a thread is fetched using GetThreadContext

It loads the Ebx register in the context obtained by GetThreadContext and adds 8 to it, then the malware reads process’s memory and unmap the memory using an NT function NtUnmapViewOfSection, EBX of a suspended fresh process always contains a pointer to PEB so we have a pointer to EBX+8 I looked into the PEB structure and I found that the offset EBX+8 contains mutant which didn’t make sense why would you access a Mutex handle(or that’s what GPT told me) so I checked the PEB32 structure and it made more sense that it refers to ImageBaseAddress, just to make sure I checked the app again using PE Studio and I found it’s a 32bit app.
This routine was done to remove the suspended process from memory.

The next routine is allocating memory in the process which we read its memory using VirtualAllocEx. The function starts by allocating 50h size of memory with the protection READ_EXECUTE_READWRITE which allows read, write, and execution operation on the allocated space.
Then the function takes lpAddress which is the start of IMAGE_BASE of the IMAGE_OPTIONAL_HEADER located in the offset 34h from IMAGE_OPTIONAL_HEADER defined by the value in lpBuffer + 3C which is e_lfanew so if the value in e_lfanew is 80h then lpBuffer + 80h is the start of IMAGE_OPTIONAL_HEADER.
Finally the data is written in the process we created earlier as suspended
hope I’m not overanalysing

NOTEI noticed later that there’s a call for
[ebp+var_64]which containsNtUnmapViewOfSectionreturned byGetProcAddressfrom the previous routine, I should’ve renamed it in the screenshot but I was too lazy to redraw the annotation.
Next the data is written using WriteProcessMemory function, the size of the written data matches the size of the following:
- e_lfanew member of IMAGE_DOS_HEADER
- 4 byte signature
- size of IMAGE_FILE_HEADER
- size of optional header
- size of all section headers
which is contained in the SizeOfHeaders property in IMAGE_OPTIONAL_HEADER.
The function starts writing in lpBaseAddress of the allocated data returned from the previous routine, and it writes the MZ file we obtained from the decryption stored in [ebp+lpBuffer].

IMAGE_OPTIONAL_HEADER SectionsLooking at the structure of
IMAGE_OPTIONAL_HEADERwe can see it’s a bit different from our code, that’s because the COFF file header ends at the offset 18, so given thatImageBaseis at0x1Cwe have0x1C + 0x18 = 0x34and the offset ofSizeOfHeadersis0x3Cso we have0x3C + 0x18 = 0x54.
The next routine is a loop that starts by reading IMAGE_NT_HEADER + 6 which is NumberOfSections property, it iterates over the PE sections which we can deduce from the imul edx, 28h instruction as each section is 28h sized, it rewrites all sections(in our case: ) with data from the loaded resource using WriteProcessMemory. At the final iteration(edx == numberOfSections) the sample writes the loaded PE’s ImageBase into PEB.ImageBaseADdress of the hallowed process then it resumes the thread.

Verifying calculationAs the code starts by going to offset
0x3Cwe can see it’s the offset ofe_lfanew
So we have
e_lfanewcontains the valueE0which is the start ofNT_IMAGE_HEADERe.g: real code of the program.As the file header ends at
F6and it has the size ofWORDor 2-bytes we conclude it ends atF8
To recreate the operation we add the value of
e_lfanewwhich isE0toF8which lands on offset1D8the start oftextsection.
Then the function returns 1 on success indicating the hallowing routine was done successfully.

Phew what a journey, this sample was a loader that’s sole purpose is creating a process svchost.exe and hallow it, I tried recreating the procedure in this small project, give project’s references a read and watch the last playlist(especially this it’s a must).
Checking the injected code which I won’t spend much time in.
From basic analysis we can tell it’s a keylogger


Questions
- What is the purpose of this program?
This program is a loader that loads a PE from it’s rsrc section and performs process hallowing on a suspended svchost process it creates.
- How does the launcher program hide execution?
It does hide execution by performing process hallowing as it creates a new svchost process and replace the content of the process with decoded content from rsrc section.
- Where is the malicious payload stored?
It’s stored in rsrc section.
- How is the malicious payload protected?
The malicious payload is encoded with XOR using 41h as a key.
I couldn’t find the answer for the last question as floss decoded the strings for me, and when I checked the book’s answers sub_401000 is a general XOR function that takes the key and the content as args and it’s only called during the loading of the resource.
Lab12-03
For this lab we are provided with an .exe file, let’s start by looking at strings using floss

We can see some functions that are used in user-space keyloggers.

Another indications of user-space keyloggers.
I gave the sample a quick look on PE Bear didn’t find more info so we can skip this part, let’s jump straight into IDA.

The sample starts by creating a console for its own process using AllocConsole, then it retrieves the window using FindWindow with ConsoleWindowClass parameter and hides the window using ShowWindow.
Then a handle to the current process is obtained using GetModuleHandle and a hook with WH_KEYBOARD_LL is set, the hook uses a function named fn to receive hook’s events.

In fn the function sub_4010C7 is called with a buffer parameter then the next hook is called using CallNextHookEx.

Looking at sub_4010C7 we can see it’s has keylogging logic as it creates a file named practicalmalwareanalysis.log and calls GetForegroundWindow and then GetWindowText then it calls WriteFile to write the obtained info. The function ends with a jump table that handles many inputs such as [DEL] and [CAPSLOCK].


Questions
-
What is the purpose of this malicious payload?
The payload injects a hook into its own hidden console window, this hook intercepts messages and logs the text in the foreground window into a file named
practicalmawlareanalysis.logworking as a user-space keylogger. -
How does the malicious payload inject itself?
It creates a hidden window console using
AllocConsolethen usesSetWindowHookExto create aWH_KEYBOARD_LLhook. -
What filesystem residue does this program create?
The sample leaves its logged data into a file named
practicalmalwareanalysis.login the same folder the sample is called.
Lab12-04
For this lab we are provided with an .exe file, let’s start by looking at the sample’s strings using floss, I got no encoded strings however the static strings are interesting and give some clues

We can conclude from (1) that the sample is loading something from its resources, and recalling from Chapter 11 the combination of OpenProcessToken and LookupPrivilegeValue and AdjustTokenPrivileges is used to set SeDebugPrivilege for privilege escalation by giving the infected account a LocalSystem access(2).

Looking at this group of strings we can see a few IoCs \system32\wupdmgr.exe and \winup.exe. With URLDownoadToFile and the link at (3) we can tell the it does download an executable called updater.exe, we will learn the usage of all of these elements when we start advanced static analysis.
Now, let’s give it a look on PE Bear.

Looking at the imports, we don’t see anything new other than the ones floss gave out. However, I didn’t notice WinExec among the strings which is used along with URLDownloadFile to install a payload.

Looking at the resources we can see it has a PE file in the resources; will extract it using Resource Hacker and check it later.
Now let’s examine the sample in IDA.

Firstly, the program loads three functions from psapi.dll namely: EnumProcessModules, GetModulesBaseName, EnumProcesses.

Then EnumProcesses is called to grab the PIDs of all the running process in system, then a loop(1, 2) is preformed on the array of processes and each process id is passed to the function sub_401000.

Looking at sub_401000 it starts by opening the passed process and enumerate all modules, then getting the basename of a specific module that’s stored in String1.

Both String1 and String2 are defined in the function prologue, after defining the bytes as a string(by pressing A) we can see String1 is <not real> and String2 is winlogon.exe.

After renaming the two variables we can see the <not real> strings gets replaced with the value of GetModuleBaseName so the code looks if GetModuleBaseName returns winlogon.exe in other words, it looks for winlogon.exe process.

The selected process id is passed to sub_401174

The subroutine starts with sub_4010FC

Which performs privilege escalation by allowing the infected process to read data and perform operations that can be done by LocalSystem account; for more details refer to chapter 11.

The sample then loads sfc_os.dll, and gets the ordinal number 2 function which is SfcTerminateWatcherThread and it’s used to disable windows file protection.
NOTEI got this part from the books’ answers as the ordinal in the dll was different for me
Then it opens winlogon.exe and crates a thread in it.
NOTEIf the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.

The last part fetches the system directory C:\windows\ and then concatenate it with \system32\wupdmgr windows update manager program, it’s collected in a stack element named ExistingFileName, the same is done with a file in $TEMP\winup.exe by using GetTempPath. So it moves the file C:\windows\system\wupdmgr.exe to $TEMP\winup.exe.
The the sample continues into sub_4011FC

The subroutine loads the resource of type BIN named #101 and writes its content in-place of C:\windows\system32\wupdmgr.exe, then runs it using WinExec.

The dropped file does two things:
first, it runs the file in $TEMP\winup.exe.

Then it downloads a file from http://www.practicalmalwareanalysis.com/updater.exe and places it in C:\windows\system32\wupdmgrd.exe then run it with WinExec.
Questions
-
What does the code at 0x401000 accomplish?
The code at
0x401000receives a process, opens it and check if it’swinlogon.exe. -
Which process has code injected?
winlogon.exeas it’s stored in a public variable nameddwProcessIdand then it’s passed to a routine at0x401174which creates a thread usingCreateRemoteThreadfunction. -
What DLL is loaded using LoadLibraryA?
sfc_os.dll, it loads the function by ordinal #2 which isSfcTerminateWatcherThread. -
What is the fourth argument passed to the
CreateRemoteThreadcall?lpStartAddress; in our case it was a pointer to the functionSfcTerminateWatcherThread. -
What malware is dropped by the main executable?
The main sample drops a file from its resources which downloads a file named
updater.exefromhttps://practicalmalwareanalysis.comand places it inC:\windows\system32\wupdmgrd.exe. -
What is the purpose of this and the dropped malware?
The purpose of this sample is to find the process
winlogon.exe, perform privilege escalation by settingSeDebugPrivilege, then turn offWindows File Protectionby injecting the start address ofSfcTerminateWatcherThreadintowinlogon.exe. The sample copiesc:\windows\system32\wupdmgr.exeto$TEMP\winup.exeand then writes the file from resources inplace ofC:\windows\system32\wupdmgr.exeas we can see from the analysis ofsub_4011FCthen this file is run. The file dropped from resources runs$TEMP\winup.exeand downloads a file fromhttps://practicalmalwareanalysis.com/updater.exeintoC:\windows\system32\wupdmgrd.exe.
Some information may be outdated
So we have
To recreate the operation we add the value of 
