mcpField
A
analyzing-linux-elf-malware

analyzing-linux-elf-malware

github.com/mukul975安全35.2k460 次安装8 周前更新
代码仓库

关于 analyzing-linux-elf-malware

供安全分析人员调查 Linux ELF 恶意二进制文件的技能,涉及静态检查、动态追踪、反汇编和持久化机制分析。流程适用于多种 Linux 架构,不用于 Windows PE 分析。

主要功能

  1. 01检查 ELF 头部与二进制属性
  2. 02提取字符串和威胁指标
  3. 03追踪系统调用与库调用
  4. 04使用 GDB 进行动态调试
  5. 05分析代码与 Linux 持久化机制

适用场景

  1. 01调查受感染 Linux 服务器上的挖矿程序
  2. 02识别僵尸网络的网络指标和行为
  3. 03检查容器或云环境中的可疑二进制文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill analyzing-linux-elf-malware

安装后在 Claude Code 中输入/analyzing-linux-elf-malware确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

分析 Linux ELF 恶意软件

适用场景

  • Linux 服务器或容器遭到入侵,并发现可疑的 ELF 二进制文件
  • 分析 Linux 僵尸网络(Mirai、Gafgyt、XorDDoS)、加密货币挖矿程序或勒索软件
  • 调查针对云基础设施、Docker 容器或 Kubernetes Pod 的恶意软件
  • 对 Linux Rootkit 和内核模块进行逆向工程
  • 分析为 Linux x86_64、ARM 或 MIPS 架构编译的跨平台恶意软件

请勿用于 Windows PE 二进制文件分析;分析 Windows 恶意软件请使用 PEStudio、Ghidra 或 IDA。

前置条件

  • 支持 Linux ELF 的 Ghidra 或 IDA,用于反汇编和反编译
  • 安装了开发工具的 Linux 分析虚拟机(推荐 Ubuntu 22.04)
  • 用于动态分析和调试的 strace、ltrace 和 GDB
  • 用于静态检查的 GNU binutils 工具 readelf、objdump 和 nm
  • 用于快速二进制初步分析和脚本化分析的 Radare2
  • 用于隔离式容器恶意软件执行的 Docker

工作流

步骤 1:识别 ELF 二进制文件属性

检查 ELF 文件头和基本属性:

# File type identification
file suspect_binary

# Detailed ELF header analysis
readelf -h suspect_binary

# Section headers
readelf -S suspect_binary

# Program headers (segments)
readelf -l suspect_binary

# Symbol table (if not stripped)
readelf -s suspect_binary
nm suspect_binary 2>/dev/null

# Dynamic linking information
readelf -d suspect_binary
ldd suspect_binary 2>/dev/null  # Only on matching architecture!

# Compute hashes
md5sum suspect_binary
sha256sum suspect_binary

# Check for packing/UPX
upx -t suspect_binary
# Python-based ELF analysis
from elftools.elf.elffile import ELFFile
import hashlib

with open("suspect_binary", "rb") as f:
    data = f.read()
    sha256 = hashlib.sha256(data).hexdigest()

with open("suspect_binary", "rb") as f:
    elf = ELFFile(f)

    print(f"SHA-256:      {sha256}")
    print(f"Class:        {elf.elfclass}-bit")
    print(f"Endian:       {elf.little_endian and 'Little' or 'Big'}")
    print(f"Machine:      {elf.header.e_machine}")
    print(f"Type:         {elf.header.e_type}")
    print(f"Entry Point:  0x{elf.header.e_entry:X}")

    # Check if stripped
    symtab = elf.get_section_by_name('.symtab')
    print(f"Stripped:     {'Yes' if symtab is None else 'No'}")

    # Section entropy analysis
    import math
    from collections import Counter
    for section in elf.iter_sections():
        data = section.data()
        if len(data) > 0:
            entropy = -sum((c/len(data)) * math.log2(c/len(data))
                          for c in Counter(data).values() if c > 0)
            if entropy > 7.0:
                print(f"  [!] High entropy section: {section.name} ({entropy:.2f})")
步骤 2:提取字符串和指标

搜索嵌入的 IOC 和功能线索:

# ASCII strings
strings suspect_binary > strings_output.txt

# Search for network indicators
grep -iE "(http|https|ftp)://" strings_output.txt
grep -iE "([0-9]{1,3}\.){3}[0-9]{1,3}" strings_output.txt
grep -iE "[a-zA-Z0-9.-]+\.(com|net|org|io|ru|cn)" strings_output.txt

# Search for shell commands
grep -iE "(bash|sh|wget|curl|chmod|/tmp/|/dev/)" strings_output.txt

# Search for crypto mining indicators
grep -iE "(stratum|xmr|monero|pool\.|mining)" strings_output.txt

# Search for SSH/credential theft
grep -iE "(ssh|authorized_keys|id_rsa|shadow|passwd)" strings_output.txt

# Search for persistence mechanisms
grep -iE "(crontab|systemd|init\.d|rc\.local|ld\.so\.preload)" strings_output.txt

# FLOSS for obfuscated strings (if available)
floss suspect_binary
步骤 3:分析系统调用和库的使用情况

识别恶意软件使用的系统调用和库:

# List imported functions (dynamically linked)
readelf -r suspect_binary | grep -E "socket|connect|exec|fork|open|write|bind|listen"

# Trace system calls during execution (in isolated VM only)
strace -f -e trace=network,process,file -o strace_output.txt ./suspect_binary

# Trace library calls
ltrace -f -o ltrace_output.txt ./suspect_binary

# Key system calls to watch:
# Network: socket, connect, bind, listen, accept, sendto, recvfrom
# Process: fork, execve, clone, kill, ptrace
# File:    open, read, write, unlink, rename, chmod
# Persistence: inotify_add_watch (file monitoring)
步骤 4:使用 GDB 进行动态分析

调试恶意软件以观察运行时行为:

# Start GDB with the binary
gdb ./suspect_binary

# Set breakpoints on key functions
(gdb) break main
(gdb) break socket
(gdb) break connect
(gdb) break execve
(gdb) break fork

# Run and analyze
(gdb) run
(gdb) info registers    # View register state
(gdb) x/20s $rdi        # Examine string argument
(gdb) bt                # Backtrace
(gdb) continue

# For stripped binaries, break on entry point
(gdb) break *0x400580   # Entry point from readelf
(gdb) run

# Monitor network connections during execution
# In another terminal:
ss -tlnp  # List listening sockets
ss -tnp   # List established connections
步骤 5:使用 Ghidra 进行逆向工程

对 ELF 二进制文件进行深入的代码分析:

Ghidra Analysis for Linux ELF:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. Import: File -> Import -> Select ELF binary
   - Ghidra auto-detects ELF format and architecture
   - Accept default analysis options

2. Key analysis targets:
   - main() function (or entry point if stripped)
   - Socket creation and connection functions
   - Command dispatch logic (switch/case on received data)
   - Encryption/encoding routines
   - Persistence installation code
   - Self-propagation/scanning functions

3. For Mirai-like botnets, look for:
   - Credential list for brute-forcing (telnet/SSH)
   - Attack module selection (UDP flood, SYN flood, ACK flood)
   - Scanner module (port scanning for vulnerable devices)
   - Killer module (killing competing botnets)

4. For cryptominers, look for:
   - Mining pool connection (stratum protocol)
   - Wallet address strings
   - CPU/GPU utilization functions
   - Process hiding techniques
步骤 6:分析 Linux 特有的持久化机制

检查持久化机制:

# Check for LD_PRELOAD rootkit
strings suspect_binary | grep "ld.so.preload"
# Malware writing to /etc/ld.so.preload can hook all dynamic library calls

# Check for crontab persistence
strings suspect_binary | grep -i "cron"

# Check for systemd service creation
strings suspect_binary | grep -iE "systemd|\.service|systemctl"

# Check for init script creation
strings suspect_binary | grep -iE "init\.d|rc\.local|update-rc"

# Check for SSH key injection
strings suspect_binary | grep -i "authorized_keys"

# Check for kernel module (rootkit) loading
strings suspect_binary | grep -iE "insmod|modprobe|init_module"

# Check for process hiding
strings suspect_binary | grep -iE "proc|readdir|getdents"

核心概念

术语定义
ELF(可执行与可链接格式)Linux 可执行文件、共享库和核心转储的标准二进制格式,包含文件头、节和段
已剥离符号的二进制文件移除了调试符号的 ELF 二进制文件;由于函数名称丢失,逆向工程会更加困难
LD_PRELOAD用于指定优先于其他库加载的共享库的 Linux 环境变量;Rootkit 会滥用它来拦截系统库调用
straceLinux 系统调用跟踪器,可记录进程发出的所有系统调用和信号,揭示文件、网络和进程操作
GOT/PLT全局偏移表和过程链接表;用于动态链接的 ELF 结构,可被劫持以钩取函数
静态链接编译时包含所有库代码的二进制文件;常见于 IoT 恶意软件,可在缺少匹配共享库的系统上运行
Mirai一种影响广泛的 Linux 僵尸网络,通过 Telnet 暴力破解攻击 IoT 设备;其源代码泄露后催生了许多变种

工具与系统

  • Ghidra:NSA 的逆向工程工具,全面支持 x86、x86_64、ARM、MIPS 和其他 Linux 架构的 ELF 文件
  • Radare2:开源逆向工程框架,提供命令行界面,可用于快速二进制分析和脚本编写
  • strace:Linux 系统调用跟踪工具,用于观察二进制文件的行为,包括文件、网络和进程操作
  • GDB:GNU 调试器,可用于设置断点、检查内存以及单步跟踪 Linux 二进制文件的执行
  • pyelftools:用于以编程方式解析 ELF 文件的 Python 库,可用于构建自动化分析流水线

常见场景

场景:分析在遭入侵的 Linux 服务器上发现的挖矿程序

背景:某云服务器的 CPU 使用率达到 100%。调查发现,/tmp 中有一个名称可疑的未知二进制文件正在运行。需要分析该文件,以确认它是挖矿程序,并找出攻击者的钱包和矿池。

方法:

  1. 将二进制文件复制到分析虚拟机,并计算 SHA-256 哈希值
  2. 运行 file 和 readelf,识别架构和链接类型
  3. 提取字符串,并搜索挖矿矿池地址(stratum+tcp://)和钱包地址
  4. 在沙箱中使用 strace 运行程序,观察网络连接(与挖矿矿池的连接)
  5. 将文件导入 Ghidra,识别挖矿算法和配置提取方式
  6. 检查持久化机制(crontab、systemd 服务、SSH 密钥)
  7. 记录所有 IOC,包括矿池地址、钱包、用于更新的 C2,以及持久化痕迹

注意事项:

  • 在沙箱外对恶意软件运行 ldd(ldd 可能会执行二进制文件中的代码)
  • 尝试以 x86_64 架构运行前,未检查其是否为 ARM/MIPS 架构
  • 遗漏可能负责持久化和清理工作的配套脚本(.sh 文件)
  • 忽略初始入侵途径(矿工程序的部署方式:SSH 暴力破解、Web 漏洞利用、容器逃逸)

输出格式

LINUX ELF MALWARE ANALYSIS REPORT
====================================
File:             /tmp/.X11-unix/.rsync
SHA-256:          e3b0c44298fc1c149afbf4c8996fb924...
Type:             ELF 64-bit LSB executable, x86-64
Linking:          Statically linked (all libraries embedded)
Stripped:         Yes
Size:             2,847,232 bytes
Packer:           UPX 3.96 (unpacked for analysis)

CLASSIFICATION
Family:           XMRig Cryptominer (modified)
Variant:          Custom build with C2 update mechanism

FUNCTIONALITY
[*] XMR (Monero) mining via RandomX algorithm
[*] Stratum pool connection for work submission
[*] C2 check-in for configuration updates
[*] Process name masquerading (argv[0] = "[kworker/0:0]")
[*] Competitor process killing (kills other miners)
[*] SSH key injection for re-access

NETWORK INDICATORS
Mining Pool:      stratum+tcp://pool.minexmr[.]com:4444
C2 Server:        hxxp://update.malicious[.]com/config
Wallet:           49jZ5Q3b...Monero_Wallet_Address...

PERSISTENCE
[1] Crontab entry: */5 * * * * /tmp/.X11-unix/.rsync
[2] SSH key added to /root/.ssh/authorized_keys
[3] Systemd service: /etc/systemd/system/rsync-daemon.service
[4] Modified /etc/ld.so.preload for process hiding

PROCESS HIDING
LD_PRELOAD:       /usr/lib/.libsystem.so
Hook:             readdir() to hide /tmp/.X11-unix/.rsync from ls
Hook:             fopen() to hide from /proc/*/maps reading

评论

支持 Markdown