醋醋百科网

Good Luck To You!

Day 4:安全分析与威胁检测

目标:利用 tcpdump 识别网络攻击行为,检测异常流量,保护系统安全。


一、端口扫描检测

1. SYN 扫描(半开扫描)

# 检测 SYN 包无后续 ACK 的流量(高频 SYN 请求)
tcpdump -nn 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack = 0' | \
awk '{print $3}' | cut -d. -f1-4 | sort | uniq -c | sort -nr

输出示例:

45 203.0.113.5      # 来自 203.0.113.5 的 45 次 SYN 请求(疑似扫描)

2. NULL/Xmas/Fin 扫描

# 检测非常规标志位组合(NULL:无标志位,Xmas: FIN/URG/PUSH)
tcpdump 'tcp[tcpflags] & (tcp-fin|tcp-urg|tcp-push) != 0'

二、DDoS 攻击检测

1. SYN 洪水攻击

# 统计 SYN 包速率(每秒超过 1000 个则告警)
tcpdump -l -n 'tcp[tcpflags] & tcp-syn != 0' | \
pv -l -r -t -i 1 | \
awk '{if ($1 > 1000) print "SYN Flood Alert: "$1" packets/s"}'

2. ICMP 洪水攻击(Ping Flood)

# 统计 ICMP 请求速率(按源 IP 分组)
tcpdump -nn icmp | awk '{print $3}' | cut -d. -f1-4 | sort | uniq -c | sort -nr

三、敏感数据泄露监控

1. HTTP 明文密码

# 抓取 HTTP 明文传输的敏感字段
tcpdump -A -s0 'tcp port 80' | \
grep -Ei 'password=|passwd=|token=|auth=' --color=auto

2. FTP 明文凭据

# 捕获 FTP 登录用户名和密码
tcpdump -A -s0 'tcp port 21' | \
grep -Ei 'USER |PASS ' --color=auto

输出示例:

USER admin
PASS MySecretPassword123

四、恶意流量识别

1. 检测 DNS 隧道(隐蔽通信)

# 捕获异常 DNS 载荷(长域名或非常规类型)
tcpdump -nn -X 'udp port 53 and (udp[14:2] > 512)' | \
grep -Eo '[a-zA-Z0-9]{20,}\.'  # 匹配超长子域名

2. 识别 C2 服务器通信

# 检测高频定时心跳包(如每 10 秒一次)
tcpdump -nn 'tcp and (tcp[8:2] > 0)' | \
awk '{print $3}' | cut -d. -f1-4 | uniq -c | \
awk '{if ($1 > 60) print "C2 Suspicion: "$2" - "$1" connections"}'

五、自动化安全告警

1. 实时 SSH 登录监控

#!/bin/bash
tcpdump -l -i eth0 'tcp port 22 and (tcp[13] & 0x03 != 0)' | \
while read line; do
    ip=$(echo $line | awk '{print $3}' | cut -d. -f1-4)
    echo "[$(date)] SSH login attempt from $ip" >> /var/log/ssh_monitor.log
    # 发送邮件告警(需配置 mail 命令)
    echo "SSH attempt from $ip at $(date)" | mail -s "SSH Alert" admin@example.com
done

2. 异常流量自动阻断(需结合 iptables)

# 检测 SYN 洪水并动态封禁 IP
tcpdump -l -i eth0 'tcp[tcpflags] & tcp-syn != 0' | \
awk '{print $3}' | cut -d. -f1-4 | \
sort | uniq -c | \
awk '{if ($1 > 100) system("iptables -A INPUT -s " $2 " -j DROP")}'

六、实战案例

场景 1:内网横向渗透检测

# 检测内网主机扫描(同一源 IP 访问多个端口)
tcpdump -nn 'src 192.168.1.100 and tcp' | \
awk '{print $3}' | cut -d. -f5 | sort | uniq | \
awk '{if (NR > 10) print "Port Scan Alert from 192.168.1.100"}'

场景 2:勒索软件通信识别

# 检测 Tor 网络通信(常见于勒索软件)
tcpdump -nn 'tcp and (dst port 9001 or dst port 9030)' -v
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言