Malware, maybe... 1
Malware Maybe… - 1
Learning how to write malware to upgrade my Red Team journey. Notes also contains several concepts for the windows internals and APIs and how to work with them. These notes are created from the MaldevAcademy malware development course.
Allocating space in memory and writing it
We can allocate space in memory using the HeapAlloc(), this is implementation of heapalloc functions in windows API there are serveral other methods to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
PVOID pheapaddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
CHAR* cstring = "This is some string";
memcpy(pheapaddress, cstring, strlen(cstring));
printf("[+] This is the base address of the heapalloc function: 0x%p \n", pheapaddress);
printf("We have successfully filled up the memory space with a string\n");
HeapFree(GetProcessHeap(), NULL, pheapaddress);
printf("Press enter to quit!");
getchar();
return EXIT_SUCCESS;
}
Creating a file using Windows API
We can create a file using the CreateFileW command the W here stands for Unicode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
HANDLE hfile = INVALID_HANDLE_VALUE;
LPCWSTR filepath = L"c:\\users\\sshuser\\desktop\\temp.txt";
hfile = CreateFileW(filepath, GENERIC_ALL, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile == INVALID_HANDLE_VALUE) {
printf("[-] The CreateFileW API failed with error : %d\n",GetLastError());
return -1;
}
return EXIT_SUCCESS;
}
We need to create handles since they act as the reference number to the operation we are doing, suppose now we need to write to the file that we just created we gonna use the WritefileW API with hfile (which is the handle) to write the file temp.txt.
1
2
3
4
5
6
7
WriteFile(
hFile,
"Hello Windows!",
14,
&bytesWritten,
NULL
);
Similarly to read the file too.
1
2
3
4
5
6
7
ReadFile(
hFile,
buffer,
sizeof(buffer),
&bytesRead,
NULL
);
DLLs (Dynamic Link Library)
DLL Entry Point
DLL_PROCESS_ATTACHED- A process is loading the DLL.DLL_THREAD_ATTACHED- A process is creating a new thread.DLL_THREAD_DETACH- A thread exits normally.DLL_PROCESS_DETACH- A process unloads the DLL.
Exporting a function
DLLs can export functions that can be used by the calling application or process. To export a function it needs to be defined using the keywords extern and __declspec(dllexport). An example exported function HelloWorld is shown below.
1
2
3
4
5
////// sampleDLL.dll //////
extern __declspec(dllexport) void HelloWorld(){
// Function code here
}
Dynamic Linking
It’s possible to use the LoadLibrary, GetModuleHandle and GetProcAddress WinAPIs to import a function from a DLL. This is referred to as dynamic linking. This is a method of loading and linking code (DLLs) at runtime rather than linking them at compile time using the linker and import address table.
There are several advantages of using dynamic linking, these are documented by Microsoft here.
This section walks through the steps of loading a DLL, retrieving the DLL’s handle, retrieving the exported function’s address and then invoking the function.
Loading a DLL
Calling a function such as MessageBoxA in an application will force the Windows OS to load the DLL exporting the MessageBoxA function into the calling process’s memory address space, which in this case is user32.dll. Loading user32.dll was done automatically by the OS when the process started and not by the code.
However, in some cases such as the HelloWorld function in sampleDLL.dll, the DLL may not be loaded into memory. For the application to call the HelloWorld function, it first needs to retrieve the DLL’s handle that is exporting the function. If the application doesn’t have sampleDLL.dll loaded into memory, it would require the usage of the LoadLibrary WinAPI, as shown below.
1
HMODULE hModule = LoadLibraryA("sampleDLL.dll"); // hModule now contain sampleDLL.dll's handle
Retrieving a DLL’s handle
If sampleDLL.dll is already loaded into the application’s memory, one can retrieve its handle via the GetModuleHandle WinAPI function without leveraging the LoadLibrary function.
1
HMODULE hModule = GetModuleHandleA("sampleDLL.dll");
Retrieving a function’s address
Once the DLL is loaded into memory and the handle is retrieved, the next step is to retrieve the function’s address. This is dont using the GetProcAddress WinAPI which takes the handle of the DLL that exports the functions and the function name.
1
PVOID pHelloWorld = GetProcAddress(hModule, "HelloWorld");
Invoking The Function
Once HelloWorld’s address is saved into the pHelloWorld variable, the next step is to perform a type-cast on this address to HelloWorld’s function pointer. This function pointer is required in order to invoke the function.
1
2
3
4
5
6
7
8
9
10
// Constructing a new data type that represents HelloWorld's function pointer
typedef void (WINAPI* HelloWorldFunctionPointer)();
void call(){
HMODULE hModule = LoadLibraryA("sampleDLL.dll");
PVOID pHelloWorld = GetProcAddress(hModule, "HelloWorld");
// Type-casting the 'pHelloWorld' variable to be of type 'HelloWorldFunctionPointer'
HelloWorldFunctionPointer HelloWorld = (HelloWorldFunctionPointer)pHelloWorld;
HelloWorld(); // Calling the 'HelloWorld' function via its function pointer
}
Dynamic Linking Example
The code below demonstrates another simple example of dynamic linking where MessageBoxA is called. The code assumes that user32.dll, the DLL that exports that function, isn’t loaded into memory. Recall that if a DLL isn’t loaded into memory the usage of LoadLibrary is required to load that DLL into the process’s address space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef int (WINAPI* MessageBoxAFunctionPointer)( // Constructing a new data type, that will represent MessageBoxA's function pointer
HWND hWnd,
LPCSTR lpText,
LPCSTR lpCaption,
UINT uType
);
void call(){
// Retrieving MessageBox's address, and saving it to 'pMessageBoxA' (MessageBoxA's function pointer)
MessageBoxAFunctionPointer pMessageBoxA = (MessageBoxAFunctionPointer)GetProcAddress(LoadLibraryA("user32.dll"), "MessageBoxA");
if (pMessageBoxA != NULL){
// Calling MessageBox via its function pointer if not null
pMessageBoxA(NULL, "MessageBox's Text", "MessageBox's Caption", MB_OK);
}
}
Rundll32.exe
There are a couple of ways to run exported functions without using a programmatical method. One common technique is to use the rundll32.exe binary. Rundll32.exe is a built-in Windows binary that is used to run an exported function of a DLL file. To run an exported function use the following command:
1
rundll32.exe <dllname>, <function exported to run>
For example, User32.dll exports the function LockWorkStation which locks the machine. To run the function, use the following command:
1
rundll32.exe user32.dll,LockWorkStation
