237 lines
9.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 恶意软件分析
{{#include ../../banners/hacktricks-training.md}}
## 取证备忘单
[https://www.jaiminton.com/cheatsheet/DFIR/#](https://www.jaiminton.com/cheatsheet/DFIR/)
## 在线服务
- [VirusTotal](https://www.virustotal.com/gui/home/upload)
- [HybridAnalysis](https://www.hybrid-analysis.com)
- [Koodous](https://koodous.com)
- [Intezer](https://analyze.intezer.com)
- [Any.Run](https://any.run/)
## 离线杀毒和检测工具
### Yara
#### 安装
```bash
sudo apt-get install -y yara
```
#### 准备规则
使用此脚本从github下载并合并所有yara恶意软件规则: [https://gist.github.com/andreafortuna/29c6ea48adf3d45a979a78763cdc7ce9](https://gist.github.com/andreafortuna/29c6ea48adf3d45a979a78763cdc7ce9)\
创建_**rules**_目录并执行它。这将创建一个名为_**malware_rules.yar**_的文件其中包含所有恶意软件的yara规则。
```bash
wget https://gist.githubusercontent.com/andreafortuna/29c6ea48adf3d45a979a78763cdc7ce9/raw/4ec711d37f1b428b63bed1f786b26a0654aa2f31/malware_yara_rules.py
mkdir rules
python malware_yara_rules.py
```
#### 扫描
```bash
yara -w malware_rules.yar image #Scan 1 file
yara -w malware_rules.yar folder #Scan the whole folder
```
#### YaraGen: 检查恶意软件并创建规则
您可以使用工具 [**YaraGen**](https://github.com/Neo23x0/yarGen) 从二进制文件生成 yara 规则。查看这些教程: [**第 1 部分**](https://www.nextron-systems.com/2015/02/16/write-simple-sound-yara-rules/), [**第 2 部分**](https://www.nextron-systems.com/2015/10/17/how-to-write-simple-but-sound-yara-rules-part-2/), [**第 3 部分**](https://www.nextron-systems.com/2016/04/15/how-to-write-simple-but-sound-yara-rules-part-3/)
```bash
python3 yarGen.py --update
python3.exe yarGen.py --excludegood -m ../../mals/
```
### ClamAV
#### 安装
```
sudo apt-get install -y clamav
```
#### 扫描
```bash
sudo freshclam #Update rules
clamscan filepath #Scan 1 file
clamscan folderpath #Scan the whole folder
```
### [Capa](https://github.com/mandiant/capa)
**Capa** 检测可执行文件中的潜在恶意 **能力**PE、ELF、.NET。因此它会找到诸如 Att\&ck 策略或可疑能力,例如:
- 检查 OutputDebugString 错误
- 作为服务运行
- 创建进程
在 [**Github repo**](https://github.com/mandiant/capa) 中获取它。
### IOCs
IOC 代表妥协指标。IOC 是一组 **条件,用于识别** 一些潜在的不需要的软件或确认的 **恶意软件**。蓝队使用这种定义来 **搜索这种恶意文件** 在他们的 **系统****网络** 中。\
共享这些定义非常有用,因为当恶意软件在计算机中被识别并为该恶意软件创建 IOC 时,其他蓝队可以使用它更快地识别恶意软件。
创建或修改 IOCs 的工具是 [**IOC Editor**](https://www.fireeye.com/services/freeware/ioc-editor.html)**.**\
您可以使用 [**Redline**](https://www.fireeye.com/services/freeware/redline.html) 等工具来 **搜索设备中的定义 IOC**
### Loki
[**Loki**](https://github.com/Neo23x0/Loki) 是一个简单妥协指标的扫描器。\
检测基于四种检测方法:
```
1. File Name IOC
Regex match on full file path/name
2. Yara Rule Check
Yara signature matches on file data and process memory
3. Hash Check
Compares known malicious hashes (MD5, SHA1, SHA256) with scanned files
4. C2 Back Connect Check
Compares process connection endpoints with C2 IOCs (new since version v.10)
```
### Linux Malware Detect
[**Linux Malware Detect (LMD)**](https://www.rfxn.com/projects/linux-malware-detect/) 是一个针对Linux的恶意软件扫描器发布于GNU GPLv2许可证旨在应对共享托管环境中面临的威胁。它使用来自网络边缘入侵检测系统的威胁数据提取正在攻击中积极使用的恶意软件并生成检测签名。此外威胁数据还来自用户提交的LMD结账功能和恶意软件社区资源。
### rkhunter
像[**rkhunter**](http://rkhunter.sourceforge.net)这样的工具可以用来检查文件系统中可能存在的**rootkits**和恶意软件。
```bash
sudo ./rkhunter --check -r / -l /tmp/rkhunter.log [--report-warnings-only] [--skip-keypress]
```
### FLOSS
[**FLOSS**](https://github.com/mandiant/flare-floss) 是一个工具,尝试使用不同的技术在可执行文件中查找混淆字符串。
### PEpper
[PEpper](https://github.com/Th3Hurrican3/PEpper) 检查可执行文件中的一些基本内容如二进制数据、熵、URLs 和 IPs以及一些 yara 规则)。
### PEstudio
[PEstudio](https://www.winitor.com/download) 是一个工具,可以获取 Windows 可执行文件的信息,如导入、导出、头部信息,同时还会检查病毒总数并找到潜在的 Att\&ck 技术。
### Detect It Easy(DiE)
[**DiE**](https://github.com/horsicq/Detect-It-Easy/) 是一个工具,用于检测文件是否 **加密**,并找到 **打包器**
### NeoPI
[**NeoPI**](https://github.com/CiscoCXSecurity/NeoPI) 是一个 Python 脚本,使用多种 **统计方法** 来检测文本/脚本文件中的 **混淆****加密** 内容。NeoPI 的预期目的是帮助 **检测隐藏的 web shell 代码**
### **php-malware-finder**
[**PHP-malware-finder**](https://github.com/nbs-system/php-malware-finder) 尽力检测 **混淆**/**可疑代码** 以及使用 **PHP** 函数的文件,这些函数通常用于 **恶意软件**/webshells。
### Apple Binary Signatures
在检查某些 **恶意软件样本** 时,您应该始终 **检查二进制文件的签名**,因为签名的 **开发者** 可能已经与 **恶意软件** **相关**
```bash
#Get signer
codesign -vv -d /bin/ls 2>&1 | grep -E "Authority|TeamIdentifier"
#Check if the apps contents have been modified
codesign --verify --verbose /Applications/Safari.app
#Check if the signature is valid
spctl --assess --verbose /Applications/Safari.app
```
## 检测技术
### 文件堆叠
如果你知道某个包含**文件**的文件夹**最后更新于某个日期**。**检查**所有**文件**在**web 服务器**中的**创建和修改日期**,如果有任何日期是**可疑的**,请检查该文件。
### 基线
如果一个文件夹的文件**不应该被修改**,你可以计算该文件夹**原始文件**的**哈希**并与**当前**文件进行**比较**。任何被修改的文件都将是**可疑的**。
### 统计分析
当信息保存在日志中时,你可以**检查统计数据比如每个web服务器的文件被访问的次数因为web shell可能是其中之一**。
---
## 反混淆动态控制流 (JMP/CALL RAX 调度器)
现代恶意软件家族严重滥用控制流图 (CFG) 混淆:它们不是直接跳转/调用,而是在运行时计算目标并执行`jmp rax``call rax`。一个小型*调度器*通常九条指令根据CPU的`ZF`/`CF`标志设置最终目标完全破坏静态CFG恢复。
该技术 - 由SLOW#TEMPEST加载器展示 - 可以通过一个仅依赖于IDAPython和Unicorn CPU模拟器的三步工作流程来击败。
### 1. 定位每个间接跳转/调用
```python
import idautils, idc
for ea in idautils.FunctionItems(idc.here()):
mnem = idc.print_insn_mnem(ea)
if mnem in ("jmp", "call") and idc.print_operand(ea, 0) == "rax":
print(f"[+] Dispatcher found @ {ea:X}")
```
### 2. 提取调度程序字节码
```python
import idc
def get_dispatcher_start(jmp_ea, count=9):
s = jmp_ea
for _ in range(count):
s = idc.prev_head(s, 0)
return s
start = get_dispatcher_start(jmp_ea)
size = jmp_ea + idc.get_item_size(jmp_ea) - start
code = idc.get_bytes(start, size)
open(f"{start:X}.bin", "wb").write(code)
```
### 3. 使用Unicorn模拟两次
```python
from unicorn import *
from unicorn.x86_const import *
import struct
def run(code, zf=0, cf=0):
BASE = 0x1000
mu = Uc(UC_ARCH_X86, UC_MODE_64)
mu.mem_map(BASE, 0x1000)
mu.mem_write(BASE, code)
mu.reg_write(UC_X86_REG_RFLAGS, (zf << 6) | cf)
mu.reg_write(UC_X86_REG_RAX, 0)
mu.emu_start(BASE, BASE+len(code))
return mu.reg_read(UC_X86_REG_RAX)
```
运行 `run(code,0,0)``run(code,1,1)` 以获取 *false**true* 分支目标。
### 4. 修补直接跳转 / 调用
```python
import struct, ida_bytes
def patch_direct(ea, target, is_call=False):
op = 0xE8 if is_call else 0xE9 # CALL rel32 or JMP rel32
disp = target - (ea + 5) & 0xFFFFFFFF
ida_bytes.patch_bytes(ea, bytes([op]) + struct.pack('<I', disp))
```
在打补丁后,强制 IDA 重新分析该函数,以恢复完整的 CFG 和 Hex-Rays 输出:
```python
import ida_auto, idaapi
idaapi.reanalyze_function(idc.get_func_attr(ea, idc.FUNCATTR_START))
```
### 5. 标记间接 API 调用
一旦每个 `call rax` 的真实目标被确定,你可以告诉 IDA 它是什么,以便参数类型和变量名称能够自动恢复:
```python
idc.set_callee_name(call_ea, resolved_addr, 0) # IDA 8.3+
```
### 实际好处
* 恢复真实的CFG → 反编译从*10*行变为数千行。
* 使字符串交叉引用和xrefs成为可能行为重建变得简单。
* 脚本可重用:将它们放入任何受相同技巧保护的加载器中。
---
## 参考文献
- [Unit42 Evolving Tactics of SLOW#TEMPEST: A Deep Dive Into Advanced Malware Techniques](https://unit42.paloaltonetworks.com/slow-tempest-malware-obfuscation/)
{{#include ../../banners/hacktricks-training.md}}