$npx -y skills add killvxk/cybersecurity-skills-zh --skill analyzing-linux-elf-malware分析恶意 Linux ELF(可执行和可链接格式)二进制文件,包括针对 Linux 服务器、容器和云基础设施的僵尸网络、 挖矿程序、勒索软件和 rootkit。涵盖 x86_64 和 ARM ELF 样本的静态分析、动态追踪和逆向工程。 适用于 Linux 恶意软件分析、ELF 二进制文件调查、Linux 服务器被攻陷评估或容器恶意软件分析相关请求。
| 1 | # 分析 Linux ELF 恶意软件 |
| 2 | |
| 3 | ## 适用场景 |
| 4 | |
| 5 | - Linux 服务器或容器被攻陷,发现可疑 ELF 二进制文件 |
| 6 | - 分析 Linux 僵尸网络(Mirai、Gafgyt、XorDDoS)、挖矿程序或勒索软件 |
| 7 | - 调查针对云基础设施、Docker 容器或 Kubernetes Pod 的恶意软件 |
| 8 | - 逆向工程 Linux rootkit 和内核模块 |
| 9 | - 分析为 Linux x86_64、ARM 或 MIPS 架构编译的跨平台恶意软件 |
| 10 | |
| 11 | **不适用于** Windows PE 二进制文件分析;请使用 PEStudio、Ghidra 或 IDA 分析 Windows 恶意软件。 |
| 12 | |
| 13 | ## 前置条件 |
| 14 | |
| 15 | - Ghidra 或支持 Linux ELF 的 IDA,用于反汇编和反编译 |
| 16 | - Linux 分析虚拟机(推荐 Ubuntu 22.04),已安装开发工具 |
| 17 | - strace、ltrace 和 GDB,用于动态分析和调试 |
| 18 | - GNU binutils 中的 readelf、objdump 和 nm,用于静态检查 |
| 19 | - Radare2,用于快速二进制分类和脚本化分析 |
| 20 | - Docker,用于隔离的基于容器的恶意软件执行 |
| 21 | |
| 22 | ## 工作流程 |
| 23 | |
| 24 | ### 步骤 1:识别 ELF 二进制文件属性 |
| 25 | |
| 26 | 检查 ELF 头部和基本属性: |
| 27 | |
| 28 | ```bash |
| 29 | # 文件类型识别 |
| 30 | file suspect_binary |
| 31 | |
| 32 | # 详细 ELF 头部分析 |
| 33 | readelf -h suspect_binary |
| 34 | |
| 35 | # 节头部 |
| 36 | readelf -S suspect_binary |
| 37 | |
| 38 | # 程序头部(段) |
| 39 | readelf -l suspect_binary |
| 40 | |
| 41 | # 符号表(如未去符号表) |
| 42 | readelf -s suspect_binary |
| 43 | nm suspect_binary 2>/dev/null |
| 44 | |
| 45 | # 动态链接信息 |
| 46 | readelf -d suspect_binary |
| 47 | ldd suspect_binary 2>/dev/null # 仅在匹配架构上使用! |
| 48 | |
| 49 | # 计算哈希值 |
| 50 | md5sum suspect_binary |
| 51 | sha256sum suspect_binary |
| 52 | |
| 53 | # 检查打包/UPX |
| 54 | upx -t suspect_binary |
| 55 | ``` |
| 56 | |
| 57 | ```python |
| 58 | # 基于 Python 的 ELF 分析 |
| 59 | from elftools.elf.elffile import ELFFile |
| 60 | import hashlib |
| 61 | |
| 62 | with open("suspect_binary", "rb") as f: |
| 63 | data = f.read() |
| 64 | sha256 = hashlib.sha256(data).hexdigest() |
| 65 | |
| 66 | with open("suspect_binary", "rb") as f: |
| 67 | elf = ELFFile(f) |
| 68 | |
| 69 | print(f"SHA-256: {sha256}") |
| 70 | print(f"类型: {elf.elfclass}-bit") |
| 71 | print(f"字节序: {'小端' if elf.little_endian else '大端'}") |
| 72 | print(f"机器架构: {elf.header.e_machine}") |
| 73 | print(f"文件类型: {elf.header.e_type}") |
| 74 | print(f"入口点: 0x{elf.header.e_entry:X}") |
| 75 | |
| 76 | # 检查是否去符号表 |
| 77 | symtab = elf.get_section_by_name('.symtab') |
| 78 | print(f"已去符号表: {'是' if symtab is None else '否'}") |
| 79 | |
| 80 | # 节熵值分析 |
| 81 | import math |
| 82 | from collections import Counter |
| 83 | for section in elf.iter_sections(): |
| 84 | data = section.data() |
| 85 | if len(data) > 0: |
| 86 | entropy = -sum((c/len(data)) * math.log2(c/len(data)) |
| 87 | for c in Counter(data).values() if c > 0) |
| 88 | if entropy > 7.0: |
| 89 | print(f" [!] 高熵节:{section.name}({entropy:.2f})") |
| 90 | ``` |
| 91 | |
| 92 | ### 步骤 2:提取字符串和指标 |
| 93 | |
| 94 | 搜索嵌入的 IOC 和功能线索: |
| 95 | |
| 96 | ```bash |
| 97 | # ASCII 字符串 |
| 98 | strings suspect_binary > strings_output.txt |
| 99 | |
| 100 | # 搜索网络指标 |
| 101 | grep -iE "(http|https|ftp)://" strings_output.txt |
| 102 | grep -iE "([0-9]{1,3}\.){3}[0-9]{1,3}" strings_output.txt |
| 103 | grep -iE "[a-zA-Z0-9.-]+\.(com|net|org|io|ru|cn)" strings_output.txt |
| 104 | |
| 105 | # 搜索 shell 命令 |
| 106 | grep -iE "(bash|sh|wget|curl|chmod|/tmp/|/dev/)" strings_output.txt |
| 107 | |
| 108 | # 搜索挖矿指标 |
| 109 | grep -iE "(stratum|xmr|monero|pool\.|mining)" strings_output.txt |
| 110 | |
| 111 | # 搜索 SSH/凭据窃取 |
| 112 | grep -iE "(ssh|authorized_keys|id_rsa|shadow|passwd)" strings_output.txt |
| 113 | |
| 114 | # 搜索持久化机制 |
| 115 | grep -iE "(crontab|systemd|init\.d|rc\.local|ld\.so\.preload)" strings_output.txt |
| 116 | |
| 117 | # FLOSS 用于混淆字符串(如可用) |
| 118 | floss suspect_binary |
| 119 | ``` |
| 120 | |
| 121 | ### 步骤 3:分析系统调用和库使用 |
| 122 | |
| 123 | 识别恶意软件使用的系统调用和库: |
| 124 | |
| 125 | ```bash |
| 126 | # 列出导入函数(动态链接) |
| 127 | readelf -r suspect_binary | grep -E "socket|connect|exec|fork|open|write|bind|listen" |
| 128 | |
| 129 | # 在执行期间追踪系统调用(仅在隔离虚拟机中) |
| 130 | strace -f -e trace=network,process,file -o strace_output.txt ./suspect_binary |
| 131 | |
| 132 | # 追踪库调用 |
| 133 | ltrace -f -o ltrace_output.txt ./suspect_binary |
| 134 | |
| 135 | # 需要关注的关键系统调用: |
| 136 | # 网络:socket、connect、bind、listen、accept、sendto、recvfrom |
| 137 | # 进程:fork、execve、clone、kill、ptrace |
| 138 | # 文件:open、read、write、unlink、rename、chmod |
| 139 | # 持久化:inotify_add_watch(文件监控) |
| 140 | ``` |
| 141 | |
| 142 | ### 步骤 4:使用 GDB 进行动态分析 |
| 143 | |
| 144 | 调试恶意软件以观察运行时行为: |
| 145 | |
| 146 | ```bash |
| 147 | # 使用二进制文件启动 GDB |
| 148 | gdb ./suspect_binary |
| 149 | |
| 150 | # 在关键函数上设置断点 |
| 151 | (gdb) break main |
| 152 | (gdb) break socket |
| 153 | (gdb) break connect |
| 154 | (gdb) break execve |
| 155 | (gdb) break fork |
| 156 | |
| 157 | # 运行并分析 |
| 158 | (gdb) run |
| 159 | (gdb) info registers # 查看寄存器状态 |
| 160 | (gdb) x/20s $rdi # 检查字符串参数 |
| 161 | (gdb) bt # 调用栈回溯 |
| 162 | (gdb) continue |
| 163 | |
| 164 | # 对于去符号表的二进制文件,在入口点断点 |
| 165 | (gdb) break *0x400580 # 来自 readelf 的入口点 |
| 166 | (gdb) run |
| 167 | |
| 168 | # 在执行期间监控网络连接 |
| 169 | # 在另一个终端: |
| 170 | ss -tlnp # 列出监听套接字 |
| 171 | ss -tnp # 列出已建立的连接 |
| 172 | ``` |
| 173 | |
| 174 | ### 步骤 5:使用 Ghidra 逆向工程 |
| 175 | |
| 176 | 对 ELF 二进制文件进行深度代码分析: |
| 177 | |
| 178 | ``` |
| 179 | Ghidra 对 Linux ELF 的分析: |
| 180 | ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 181 | 1. 导入:文件 -> 导入 -> 选择 ELF 二进制文件 |
| 182 | - Ghidra 自动检测 ELF 格式和架构 |
| 183 | - 接受默认分析选项 |
| 184 | |
| 185 | 2. 关键分析目标: |
| 186 | - main() 函数(或去符号表时的入口点) |
| 187 | - 套接字创建和连接函数 |
| 188 | - 命令分发逻辑(对接收数据的 switch/case) |
| 189 | - 加密/编码例程 |
| 190 | - 持久化安装代码 |
| 191 | - 自传播/扫描函数 |
| 192 | |
| 193 | 3. 对于类 Mirai 僵尸网络,查找: |
| 194 | - 暴力破解凭据列表(telnet/SSH) |
| 195 | - 攻击模块选择(UDP 洪泛、SYN 洪泛、ACK 洪泛) |
| 196 | - 扫描模块(扫描易受攻击设备的端口) |
| 197 | - 清除模块(杀死竞争性僵尸网络) |
| 198 | |
| 199 | 4. 对于挖矿程序,查找: |
| 200 | - 挖矿池连接(stratum 协议) |
| 201 | - 钱包地址字符串 |
| 202 | - CPU/GPU 利用函数 |
| 203 | - 进程隐藏技术 |
| 204 | ``` |
| 205 | |
| 206 | ### 步骤 6:分析 Linux 特定持久化机制 |
| 207 | |
| 208 | 检查持久化机制: |
| 209 | |
| 210 | ```bash |
| 211 | # 检查 LD_PRELOAD rootkit |
| 212 | strings suspect_binary | grep "ld.so.preload" |
| 213 | # 写入 /etc/ld.so.preload 的恶意软件可钩住所有动态库调用 |
| 214 | |
| 215 | # 检查 crontab 持久化 |
| 216 | strings suspect_binary | grep -i "cron" |
| 217 | |
| 218 | # 检查 systemd 服务创建 |
| 219 | strings suspect_binary | grep -iE "systemd|\.service|systemctl" |
| 220 | |
| 221 | # 检查 init 脚本创建 |
| 222 | strings suspect_binary | grep -iE "init\.d|rc\.local|update-rc" |
| 223 | |
| 224 | # 检查 SSH 密钥注入 |
| 225 | strings suspect_binar |