Κοινά API που χρησιμοποιούνται σε Malware

Reading time: 9 minutes

tip

Μάθετε & εξασκηθείτε στο AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Μάθετε & εξασκηθείτε στο Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Υποστηρίξτε το HackTricks

Γενικά

Δικτύωση

Raw SocketsWinAPI Sockets
socket()WSAStratup()
bind()bind()
listen()listen()
accept()accept()
connect()connect()
read()/recv()recv()
write()send()
shutdown()WSACleanup()

TLS pinning και chunked transport

Πολλοί loaders τυλίγουν το TCP stream τους σε SslStream και κάνουν pin το server’s leaf certificate σε ένα ενσωματωμένο αντίγραφο (certificate pinning). Οι πληροφορίες/εντολές του bot συμπιέζονται (π.χ., GZip). Όταν οι απαντήσεις υπερβαίνουν ένα όριο (~1 MB), τα δεδομένα τεμαχίζονται σε μικρά κομμάτια (π.χ., τμήματα των 16 KB) για να αποφευχθούν έλεγχοι με βάση το μέγεθος και να μειωθούν τα αιχμές μνήμης κατά την απο-σειριοποίηση.

Persistence

RegistryFileService
RegCreateKeyEx()GetTempPath()OpenSCManager
RegOpenKeyEx()CopyFile()CreateService()
RegSetValueEx()CreateFile()StartServiceCtrlDispatcher()
RegDeleteKeyEx()WriteFile()
RegGetValue()ReadFile()

Κρυπτογράφηση

Όνομα
WinCrypt
CryptAcquireContext()
CryptGenKey()
CryptDeriveKey()
CryptDecrypt()
CryptReleaseContext()

Anti-Analysis/VM

Όνομα ΣυνάρτησηςΕντολές Assembly
IsDebuggerPresent()CPUID()
GetSystemInfo()IN()
GlobalMemoryStatusEx()
GetVersion()
CreateToolhelp32Snapshot [Έλεγχος εάν τρέχει μια διεργασία]
CreateFileW/A [Έλεγχος εάν υπάρχει ένα αρχείο]

Έλεγχος εκτέλεσης βάσει locale/πληκτρολογίου

Πολλοί stealers/loaders τερματίζουν σε ορισμένα locales για να αποφύγουν ερευνητές και να συμμορφωθούν με περιορισμούς των threat actors. Τυπικοί έλεγχοι:

  • GetKeyboardLayout για την απαρίθμηση των εγκατεστημένων layouts (ανά thread/user)
  • GetLocaleInfoA/W για την ανάλυση κωδικών χώρας/περιοχής
  • GetSystemDefaultLangID / GetUserDefaultLangID

Εάν κάποιο από αυτά ταιριάξει με μια μαύρη λίστα (συνήθως χώρες της CIS), ο loader τερματίζει αμέσως πριν από οποιαδήποτε network IOCs ή injection.

Άμυνα / ανίχνευση

  • Σημειώστε διεργασίες που καλούν πολλαπλά locale/keyboard APIs νωρίς στην εκτέλεση και τερματίζονται χωρίς παρατηρήσιμη δραστηριότητα.
  • Συσχετίστε με anti-VM ελέγχους (BIOS strings, PnP devices, μοντέλο δίσκου, services) που επαναχρησιμοποιούνται από open-source projects (π.χ., VMDetector) για να εντοπίσετε gated execution.

Emulator API fingerprinting & sleep evasion

Το malware συχνά fingerprint-άρει sandbox emulators αναζητώντας τις virtualised exports του Defender (παρατηρούμενες στο Malware Protection Emulator). Εάν κάποιο από αυτά τα σύμβολα υπάρχει (ανίχνευση case-insensitive στη διεργασία), η εκτέλεση καθυστερεί για 10–30 λεπτά και επανελέγχεται για να σπαταληθεί χρόνος ανάλυσης.

Παραδείγματα ονομάτων API που χρησιμοποιούνται ως canaries:

  • MpVmp32Entry, MpVmp32FastEnter, MpCallPreEntryPointCode, MpCallPostEntryPointCode, MpFinalize, MpReportEvent*, MpSwitchToNextThread*
  • VFS_* family: VFS_Open, VFS_Read, VFS_MapViewOfFile, VFS_UnmapViewOfFile, VFS_FindFirstFile/FindNextFile, VFS_CopyFile, VFS_DeleteFile, VFS_MoveFile
  • ThrdMgr_*: ThrdMgr_GetCurrentThreadHandle, ThrdMgr_SaveTEB, ThrdMgr_SwitchThreads

Τυπικό primitive καθυστέρησης (user-land):

cmd
cmd /c timeout /t %RANDOM_IN_[600,1800]% > nul

Argument gatekeeping

  • Οι operators μερικές φορές απαιτούν να υπάρχει ένα εμφανιζόμενο ως benign CLI switch πριν εκτελέσουν το payload (π.χ., /i:--type=renderer για να μιμηθούν Chromium child processes). Εάν το switch λείπει, ο loader τερματίζει άμεσα, εμποδίζοντας την αφελή εκτέλεση σε sandbox.

Απόκρυψη

Όνομα
VirtualAllocAlloc memory (packers)
VirtualProtectChange memory permission (packer giving execution permission to a section)
ReadProcessMemoryInjection into external processes
WriteProcessMemoryA/WInjection into external processes
NtWriteVirtualMemory
CreateRemoteThreadDLL/Process injection...
NtUnmapViewOfSection
QueueUserAPC
CreateProcessInternalA/W

Εκτέλεση

Function Name
CreateProcessA/W
ShellExecute
WinExec
ResumeThread
NtResumeThread

Διάφορα

  • GetAsyncKeyState() -- Key logging
  • SetWindowsHookEx -- Key logging
  • GetForeGroundWindow -- Get running window name (or the website from a browser)
  • LoadLibrary() -- Import library
  • GetProcAddress() -- Import library
  • CreateToolhelp32Snapshot() -- List running processes
  • GetDC() -- Screenshot
  • BitBlt() -- Screenshot
  • InternetOpen(), InternetOpenUrl(), InternetReadFile(), InternetWriteFile() -- Access the Internet
  • FindResource(), LoadResource(), LockResource() -- Access resources of the executable

Τεχνικές Malware

DLL Injection

Execute an arbitrary DLL inside another process

  1. Locate the process to inject the malicious DLL: CreateToolhelp32Snapshot, Process32First, Process32Next
  2. Open the process: GetModuleHandle, GetProcAddress, OpenProcess
  3. Write the path to the DLL inside the process: VirtualAllocEx, WriteProcessMemory
  4. Create a thread in the process that will load the malicious DLL: CreateRemoteThread, LoadLibrary

Other functions to use: NTCreateThreadEx, RtlCreateUserThread

Reflective DLL Injection

Load a malicious DLL without calling normal Windows API calls.
The DLL is mapped inside a process, it will resolve the import addresses, fix the relocations and call the DllMain function.

Thread Hijacking

Find a thread from a process and make it load a malicious DLL

  1. Find a target thread: CreateToolhelp32Snapshot, Thread32First, Thread32Next
  2. Open the thread: OpenThread
  3. Suspend the thread: SuspendThread
  4. Write the path to the malicious DLL inside the victim process: VirtualAllocEx, WriteProcessMemory
  5. Resume the thread loading the library: ResumeThread

PE Injection

Portable Execution Injection: Το εκτελέσιμο θα γραφτεί στη μνήμη της θύματος διεργασίας και θα εκτελεστεί από εκεί.

Process Hollowing (a.k.a RunPE)

Process Hollowing is one of the favourite defence-evasion / execution tricks used by Windows malware. Η ιδέα είναι να ξεκινήσει μια legitimate διεργασία σε suspended κατάσταση, να αφαιρεθεί (hollow) η αρχική εικόνα της από τη μνήμη και να αντιγραφεί ένα arbitrary PE στη θέση της. Όταν το primary thread επαναληφθεί, το malicious entry-point εκτελείται υπό το προσωπείο ενός αξιόπιστου binary (συχνά signed by Microsoft).

Τυπική ροή εργασίας:

  1. Spawn a benign host (e.g. RegAsm.exe, rundll32.exe, msbuild.exe) suspended έτσι ώστε να μην τρέξουν ακόμα εντολές.
c
STARTUPINFOA  si = { sizeof(si) };
PROCESS_INFORMATION pi;
CreateProcessA("C:\\Windows\\Microsoft.NET\\Framework32\\v4.0.30319\\RegAsm.exe",
NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
  1. Read the malicious payload into memory and parse its PE headers to obtain SizeOfImage, sections and the new EntryPoint.
  2. NtUnmapViewOfSection / ZwUnmapViewOfSection – unmap the original image base of the suspended process.
  3. VirtualAllocEx – reserve RWX memory of SizeOfImage inside the remote process.
  4. WriteProcessMemory – copy the Headers first, then iterate over sections copying their raw data.
  5. SetThreadContext – patch the value of EAX/RAX (RCX on x64) or Rip in the context structure so that EIP points to the payload’s EntryPoint.
  6. ResumeThread – the thread continues, executing the attacker-supplied code.

Ελάχιστο proof-of-concept (x86) σκελετό:

c
void RunPE(LPCSTR host, LPVOID payload, DWORD payloadSize){
// 1. create suspended process
STARTUPINFOA si = {sizeof(si)}; PROCESS_INFORMATION pi;
CreateProcessA(host, NULL,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi);

// 2. read remote PEB to get ImageBaseAddress
CONTEXT ctx; ctx.ContextFlags = CONTEXT_FULL;
GetThreadContext(pi.hThread,&ctx);
PVOID baseAddr;
ReadProcessMemory(pi.hProcess,(PVOID)(ctx.Ebx+8),&baseAddr,4,NULL);

// 3. unmap original image & allocate new region at same base
NtUnmapViewOfSection(pi.hProcess,baseAddr);
PVOID newBase = VirtualAllocEx(pi.hProcess,baseAddr,pHdr->OptionalHeader.SizeOfImage,
MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
// 4-5. copy headers & sections …
// 6. write new image base into PEB and set Eip
WriteProcessMemory(pi.hProcess,(PVOID)(ctx.Ebx+8),&baseAddr,4,NULL);
ctx.Eax = (DWORD)(newBase) + pHdr->OptionalHeader.AddressOfEntryPoint;
SetThreadContext(pi.hThread,&ctx);
// 7. run!
ResumeThread(pi.hThread);
}

Practical notes observed in the DarkCloud Stealer campaign:

  • The loader picked RegAsm.exe (part of the .NET Framework) as host – a signed binary unlikely to draw attention.
  • The decrypted VB6 stealer (holographies.exe) is not dropped on disk; it only ever exists inside the hollowed process making static detection harder.
  • Sensitive strings (regexes, paths, Telegram credentials) are RC4-encrypted per-string and only decrypted at runtime, further complicating memory scanning.

Detection ideas:

  • Alert on CREATE_SUSPENDED processes that never create GUI/console windows before a memory region is allocated as RWX (rare for benign code).
  • Look for a call sequence NtUnmapViewOfSection ➜ VirtualAllocEx ➜ WriteProcessMemory across different processes.
  • Unusual use of trusted developer utilities as hollowing hosts, especially MSBuild.exe, RegAsm.exe, rundll32.exe, parented by short-lived loaders.
  • Hunt for msbuild.exe spawned from user-writable paths or without corresponding .sln/.proj context then making outbound connections (ATT&CK T1127.001 + T1055.012).

Common host processes and path resolution

  • MSBuild.exe is frequently chosen as a hollowing host to blend with developer tooling. Loaders often search architecture-appropriate locations:
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
  • C:\Windows\System32\MSBuild.exe
  • C:\Windows\SysWOW64\MSBuild.exe
  • Select the host matching the current payload/OS architecture before calling CreateProcess(..., CREATE_SUSPENDED, ...).

Hooking

  • Το SSDT (System Service Descriptor Table) δείχνει σε kernel functions (ntoskrnl.exe) ή GUI driver (win32k.sys) ώστε οι user processes να μπορούν να καλούν αυτές τις λειτουργίες.
  • Ένα rootkit μπορεί να τροποποιήσει αυτούς τους δείκτες σε διευθύνσεις που ελέγχει ο επιτιθέμενος.
  • IRP (I/O Request Packets) μεταφέρουν κομμάτια δεδομένων μεταξύ components. Σχεδόν οτιδήποτε στον kernel χρησιμοποιεί IRPs και κάθε device object έχει τον δικό του πίνακα λειτουργιών που μπορεί να γίνει hook: DKOM (Direct Kernel Object Manipulation)
  • Η IAT (Import Address Table) είναι χρήσιμη για την επίλυση dependencies. Είναι δυνατό να γίνει hook σε αυτόν τον πίνακα για να καταληφθεί ο κώδικας που θα κληθεί.
  • EAT (Export Address Table) Hooks. Αυτά τα hooks μπορούν να γίνουν από userland. Στόχος είναι να γίνουν hook εξαγόμενων συναρτήσεων από DLLs.
  • Inline Hooks: Αυτού του τύπου τα hooks είναι δύσκολο να επιτευχθούν. Περιλαμβάνουν την τροποποίηση του ίδιου του κώδικα των συναρτήσεων, π.χ. βάζοντας ένα jump στην αρχή τους.

References

tip

Μάθετε & εξασκηθείτε στο AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Μάθετε & εξασκηθείτε στο Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Υποστηρίξτε το HackTricks