# Common API used in Malware

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

***

## 通用

### 网络

| 原始套接字         | WinAPI套接字    |
| ------------- | ------------ |
| socket()      | WSAStratup() |
| bind()        | bind()       |
| listen()      | listen()     |
| accept()      | accept()     |
| connect()     | connect()    |
| read()/recv() | recv()       |
| write()       | send()       |
| shutdown()    | WSACleanup() |

### 持久性

| 注册表              | 文件            | 服务                           |
| ---------------- | ------------- | ---------------------------- |
| RegCreateKeyEx() | GetTempPath() | OpenSCManager                |
| RegOpenKeyEx()   | CopyFile()    | CreateService()              |
| RegSetValueEx()  | CreateFile()  | StartServiceCtrlDispatcher() |
| RegDeleteKeyEx() | WriteFile()   |                              |
| RegGetValue()    | ReadFile()    |                              |

### 加密

| 名称                    |
| --------------------- |
| WinCrypt              |
| CryptAcquireContext() |
| CryptGenKey()         |
| CryptDeriveKey()      |
| CryptDecrypt()        |
| CryptReleaseContext() |

### 反分析/虚拟机

| 函数名称                                   | 汇编指令    |
| -------------------------------------- | ------- |
| IsDebuggerPresent()                    | CPUID() |
| GetSystemInfo()                        | IN()    |
| GlobalMemoryStatusEx()                 |         |
| GetVersion()                           |         |
| CreateToolhelp32Snapshot \[检查进程是否正在运行] |         |
| CreateFileW/A \[检查文件是否存在]              |         |

### 隐蔽

| 名称                       |                        |
| ------------------------ | ---------------------- |
| VirtualAlloc             | 分配内存（压缩器）              |
| VirtualProtect           | 更改内存权限（使压缩器给予某个部分执行权限） |
| ReadProcessMemory        | 注入到外部进程                |
| WriteProcessMemoryA/W    | 注入到外部进程                |
| NtWriteVirtualMemory     |                        |
| CreateRemoteThread       | DLL/进程注入...            |
| NtUnmapViewOfSection     |                        |
| QueueUserAPC             |                        |
| CreateProcessInternalA/W |                        |

### 执行

| 函数名称             |
| ---------------- |
| CreateProcessA/W |
| ShellExecute     |
| WinExec          |
| ResumeThread     |
| NtResumeThread   |

### 其他

* GetAsyncKeyState() -- 键盘记录
* SetWindowsHookEx -- 键盘记录
* GetForeGroundWindow -- 获取运行窗口名称（或浏览器中的网站）
* LoadLibrary() -- 导入库
* GetProcAddress() -- 导入库
* CreateToolhelp32Snapshot() -- 列出运行中的进程
* GetDC() -- 截图
* BitBlt() -- 截图
* InternetOpen()、InternetOpenUrl()、InternetReadFile()、InternetWriteFile() -- 访问互联网
* FindResource()、LoadResource()、LockResource() -- 访问可执行文件的资源

## 恶意软件技术

### DLL注入

在另一个进程中执行任意DLL

1. 定位要注入恶意DLL的进程：CreateToolhelp32Snapshot、Process32First、Process32Next
2. 打开进程：GetModuleHandle、GetProcAddress、OpenProcess
3. 将DLL路径写入进程：VirtualAllocEx、WriteProcessMemory
4. 在进程中创建一个将加载恶意DLL的线程：CreateRemoteThread、LoadLibrary

其他要使用的函数：NTCreateThreadEx、RtlCreateUserThread

### 反射式DLL注入

加载恶意DLL而无需调用常规的Windows API调用。\
DLL被映射到进程内部，将解析导入地址，修复重定位并调用DllMain函数。

### 线程劫持

找到进程中的线程并使其加载恶意DLL

1. 找到目标线程：CreateToolhelp32Snapshot、Thread32First、Thread32Next
2. 打开线程：OpenThread
3. 暂停线程：SuspendThread
4. 将恶意DLL的路径写入受害进程：VirtualAllocEx、WriteProcessMemory
5. 恢复加载库的线程：ResumeThread

### PE注入

Portable Execution Injection：可执行文件将被写入受害进程的内存中，并从那里执行。

### 进程空壳化

恶意软件将从进程的内存中取消映射合法代码并加载恶意二进制文件

1. 创建一个新进程：CreateProcess
2. 取消映射内存：ZwUnmapViewOfSection、NtUnmapViewOfSection
3. 将恶意二进制文件写入进程内存：VirtualAllocEc、WriteProcessMemory
4. 设置入口点并执行：SetThreadContext、ResumeThread

## 钩子

* **SSDT**（系统服务描述符表）指向内核函数（ntoskrnl.exe）或GUI驱动程序（win32k.sys），因此用户进程可以调用这些函数。
* Rootkit可能会修改这些指针以指向他控制的地址
* **IRP**（I/O请求数据包）将数据片段从一个组件传输到另一个组件。几乎所有内核都使用IRP，每个设备对象都有自己的函数表，可以对其进行钩取：DKOM（直接内核对象操作）
* **IAT**（导入地址表）有助于解析依赖项。可以钩取此表以劫持将要调用的代码。
* **EAT**（导出地址表）钩取。这些钩可以从**用户空间**完成。目标是钩取DLL导出的函数。
* **内联钩子**：这种类型很难实现。这涉及修改函数本身的代码。也许是在函数开头放置一个跳转。

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) **和** [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) **的GitHub存储库提交PR来分享您的黑客技巧。**

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/reversing/common-api-used-in-malware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
