hacktricks/reversing/common-api-used-in-malware.md

166 lines
8.5 KiB
Markdown
Raw Normal View History

# Common API used in Malware
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
<details>
2022-04-28 16:01:33 +00:00
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
## Generic
2023-09-03 01:48:41 +02:00
### Networking
2020-12-03 18:00:02 +00:00
| Raw Sockets | WinAPI Sockets |
| ------------- | -------------- |
| socket() | WSAStratup() |
| bind() | bind() |
| listen() | listen() |
| accept() | accept() |
| connect() | connect() |
| read()/recv() | recv() |
| write() | send() |
| shutdown() | WSACleanup() |
2020-12-03 18:00:02 +00:00
### Persistence
2020-12-03 18:00:02 +00:00
| Registry | File | Service |
| ---------------- | ------------- | ---------------------------- |
| RegCreateKeyEx() | GetTempPath() | OpenSCManager |
| RegOpenKeyEx() | CopyFile() | CreateService() |
| RegSetValueEx() | CreateFile() | StartServiceCtrlDispatcher() |
| RegDeleteKeyEx() | WriteFile() | |
| RegGetValue() | ReadFile() | |
2020-12-03 18:00:02 +00:00
### Encryption
2020-12-03 18:00:02 +00:00
| Name |
| --------------------- |
| WinCrypt |
| CryptAcquireContext() |
| CryptGenKey() |
| CryptDeriveKey() |
| CryptDecrypt() |
| CryptReleaseContext() |
2020-12-03 18:00:02 +00:00
### Anti-Analysis/VM
2020-12-03 18:00:02 +00:00
| Function Name | Assembly Instructions |
| --------------------------------------------------------- | --------------------- |
| IsDebuggerPresent() | CPUID() |
| GetSystemInfo() | IN() |
| GlobalMemoryStatusEx() | |
| GetVersion() | |
| CreateToolhelp32Snapshot \[Check if a process is running] | |
| CreateFileW/A \[Check if a file exist] | |
2020-12-03 18:00:02 +00:00
### Stealth
2020-12-03 18:00:02 +00:00
| Name | |
| ------------------------ | -------------------------------------------------------------------------- |
| VirtualAlloc | Alloc memory (packers) |
| VirtualProtect | Change memory permission (packer giving execution permission to a section) |
| ReadProcessMemory | Injection into external processes |
| WriteProcessMemoryA/W | Injection into external processes |
| NtWriteVirtualMemory | |
| CreateRemoteThread | DLL/Process injection... |
| NtUnmapViewOfSection | |
| QueueUserAPC | |
| CreateProcessInternalA/W | |
2020-12-03 18:00:02 +00:00
### Execution
2020-12-03 18:00:02 +00:00
| Function Name |
| ---------------- |
2020-12-09 00:31:50 +00:00
| CreateProcessA/W |
| ShellExecute |
| WinExec |
| ResumeThread |
| NtResumeThread |
2020-12-03 18:00:02 +00:00
### Miscellaneous
2020-12-03 18:00:02 +00:00
* GetAsyncKeyState() -- Key logging
2020-12-03 18:00:02 +00:00
* 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
2020-12-03 18:00:02 +00:00
## Malware Techniques
2021-09-07 00:15:14 +00:00
### DLL Injection
2021-09-07 00:15:14 +00:00
Eseguire un DLL arbitrario all'interno di un altro processo
2021-09-07 00:15:14 +00:00
1. Individuare il processo in cui iniettare il DLL malevolo: CreateToolhelp32Snapshot, Process32First, Process32Next
2. Aprire il processo: GetModuleHandle, GetProcAddress, OpenProcess
3. Scrivere il percorso del DLL all'interno del processo: VirtualAllocEx, WriteProcessMemory
4. Creare un thread nel processo che caricherà il DLL malevolo: CreateRemoteThread, LoadLibrary
2021-09-07 00:15:14 +00:00
2024-02-10 13:03:23 +00:00
Altre funzioni da utilizzare: NTCreateThreadEx, RtlCreateUserThread
2021-09-07 00:15:14 +00:00
### Reflective DLL Injection
2021-09-07 00:15:14 +00:00
Caricare un DLL malevolo senza chiamare le normali chiamate API di Windows.\
Il DLL è mappato all'interno di un processo, risolverà gli indirizzi di importazione, correggerà le rilocazioni e chiamerà la funzione DllMain.
2021-09-07 00:15:14 +00:00
### Thread Hijacking
2021-09-07 00:15:14 +00:00
Trovare un thread da un processo e farlo caricare un DLL malevolo
2021-09-07 00:15:14 +00:00
1. Trovare un thread target: CreateToolhelp32Snapshot, Thread32First, Thread32Next
2. Aprire il thread: OpenThread
3. Sospendere il thread: SuspendThread
4. Scrivere il percorso del DLL malevolo all'interno del processo vittima: VirtualAllocEx, WriteProcessMemory
5. Riprendere il thread caricando la libreria: ResumeThread
2021-09-07 00:15:14 +00:00
### PE Injection
2021-09-07 00:15:14 +00:00
Iniezione di Esecuzione Portatile: L'eseguibile sarà scritto nella memoria del processo vittima e verrà eseguito da lì.
2021-09-07 00:15:14 +00:00
### Process Hollowing
2021-09-07 00:15:14 +00:00
Il malware rimuoverà il codice legittimo dalla memoria del processo e caricherà un binario malevolo
2021-09-07 00:15:14 +00:00
1. Creare un nuovo processo: CreateProcess
2. Rimuovere la mappatura della memoria: ZwUnmapViewOfSection, NtUnmapViewOfSection
3. Scrivere il binario malevolo nella memoria del processo: VirtualAllocEc, WriteProcessMemory
4. Impostare il punto di ingresso ed eseguire: SetThreadContext, ResumeThread
2021-09-07 00:15:14 +00:00
## Hooking
* La **SSDT** (**System Service Descriptor Table**) punta a funzioni del kernel (ntoskrnl.exe) o driver GUI (win32k.sys) in modo che i processi utente possano chiamare queste funzioni.
* Un rootkit può modificare questi puntatori a indirizzi che controlla
* **IRP** (**I/O Request Packets**) trasmettono pezzi di dati da un componente a un altro. Quasi tutto nel kernel utilizza IRP e ogni oggetto dispositivo ha la propria tabella di funzioni che può essere hookata: DKOM (Direct Kernel Object Manipulation)
* La **IAT** (**Import Address Table**) è utile per risolvere le dipendenze. È possibile hookare questa tabella per dirottare il codice che verrà chiamato.
* **EAT** (**Export Address Table**) Hooks. Questi hook possono essere effettuati da **userland**. L'obiettivo è hookare le funzioni esportate dai DLL.
* **Inline Hooks**: Questo tipo è difficile da realizzare. Questo comporta la modifica del codice delle funzioni stesse. Forse mettendo un salto all'inizio di questo.
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Support HackTricks</summary>
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}