如何排查香港VPS服务器日志中出现的可疑行为

 

在香港VPS服务器上排查可疑行为需要系统化的方法,以下是详细的排查流程和技术手段:

 

## 一、日志收集与初步分析

 

### 1. 关键日志位置

```bash

# 系统认证日志

/var/log/auth.log   # Ubuntu/Debian

/var/log/secure     # CentOS/RHEL

 

# 系统消息日志

/var/log/syslog

/var/log/messages

 

# 香港VPS上的Web服务日志

/var/log/apache2/access.log   # Apache

/var/log/nginx/access.log     # Nginx

 

# 数据库日志

/var/log/mysql/error.log      # MySQL

/var/log/postgresql.log       # PostgreSQL

 

# SSH登录记录

lastlog

last -f /var/log/wtmp

```

 

### 2. 日志收集命令

```bash

# 收集最近3天的关键日志

grep -i "fail\|error\|warning\|invalid\|refused" /var/log/{auth.log,secure,syslog,messages} --color=always | grep -E "$(date +'%b %e')|$(date +'%b %e' -d '1 day ago')|$(date +'%b %e' -d '2 days ago')"

 

# 检查异常登录尝试

grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr | head -20

```

 

## 二、用户与进程排查

 

### 1. 用户账户检查

```bash

# 检查异常用户

awk -F: '($3 < 1000) {print $1}' /etc/passwd

 

# 检查空密码账户

awk -F: '($2 == "") {print $1}' /etc/shadow

 

# 检查sudo权限用户

grep -Po '^sudo.+:\K.*$' /etc/group

```

 

### 2. 进程与服务分析

```bash

# 检查异常进程

ps auxf | grep -E "(httpd|apache|nginx|mysql|ssh|ftp|smtp)" | grep -v grep

 

# 检查隐藏进程

ps -ef | awk '{print $2}' | sort -n | uniq > /tmp/ps1.txt

ls /proc | sort -n | uniq > /tmp/ps2.txt

diff /tmp/ps1.txt /tmp/ps2.txt

 

# 检查异常定时任务

crontab -l

ls -la /etc/cron* /var/spool/cron/crontabs/

```

 

## 三、网络连接分析

 

### 1. 当前连接检查

```bash

# 检查异常外连

netstat -antup | grep ESTABLISHED

ss -tulnp | grep -E "(127.0.0.1|::1)" -v

 

# 检查DNS查询历史

journalctl -u systemd-resolved --no-pager -n 100

```

 

### 2. 历史连接分析

```bash

# 检查防火墙日志

journalctl -u firewalld --no-pager -n 100  # CentOS

journalctl -u ufw --no-pager -n 100       # Ubuntu

 

# 检查IP连接历史

grep -E "ACCEPT|DROP" /var/log/syslog* | grep -i "src="

```

 

## 四、文件系统排查

 

### 1. 文件完整性检查

```bash

# 查找最近修改的文件

find / -type f -mtime -3 -exec ls -la {} \; 2>/dev/null | grep -vE "/proc|/sys|/run|/dev"

 

# 查找隐藏的Web Shell

find /var/www/ -type f -name "*.php" -exec grep -l "eval(base64_decode" {} \;

```

 

### 2. SUID/SGID文件检查

```bash

# 查找异常SUID文件

find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null

 

# 查找异常SGID文件

find / -perm -2000 -type f -exec ls -la {} \; 2>/dev/null

```

 

## 五、自动化工具辅助

 

### 1. 使用RKHunter检查Rootkit

```bash

sudo apt install rkhunter  # Ubuntu/Debian

sudo yum install rkhunter  # CentOS/RHEL

 

sudo rkhunter --update

sudo rkhunter --propupd

sudo rkhunter --check --sk

```

 

### 2. 使用Lynis进行安全审计

```bash

sudo apt install lynis    # Ubuntu/Debian

sudo yum install lynis   # CentOS/RHEL

 

sudo lynis audit system

```

 

### 3. 使用ClamAV扫描恶意软件

```bash

sudo apt install clamav clamav-daemon  # Ubuntu/Debian

sudo freshclam

sudo clamscan -r -i --bell /

```

 

## 六、香港VPS特殊注意事项

 

1. **网络地理位置**:

   - 检查来自非亚太地区的异常连接

   ```bash

   whois $(netstat -tnp | awk '{print $5}' | cut -d: -f1 | sort | uniq) | grep -i "country"

   ```

 

2. **合规性要求**:

   - 确保日志不包含敏感个人信息(香港PDPO规定)

 

3. **网络延迟分析**:

   ```bash

   # 检查异常TCP延迟

   tcpdump -i eth0 -nn -tttt 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' and 'dst port 22'

   ```

 

## 七、可疑行为处置流程

 

1. **证据保存**:

   ```bash

   # 创建取证快照

   tar czvf /tmp/forensics_$(date +%Y%m%d).tar.gz /var/log/ /etc/passwd /etc/shadow /etc/group

   ```

 

2. **隔离措施**:

   ```bash

   # 立即阻断可疑IP

   iptables -A INPUT -s 可疑IP -j DROP

   ```

 

3. **服务恢复**:

   - 重置受影响服务密码

   - 更新所有软件包

   - 恢复干净备份

 

## 八、预防性措施建议

 

1. **日志集中管理**:

   ```bash

   # 配置rsyslog远程日志

   *.* @远程日志服务器IP:514

   ```

 

2. **实时监控设置**:

   ```bash

   # 安装并配置OSSEC

   sudo apt install ossec-hids

   sudo /var/ossec/bin/ossec-control start

   ```

 

3. **定期审计脚本**:

   ```bash

   # 每周安全检查脚本

   #!/bin/bash

   echo "===== $(date) =====" >> /var/log/security_audit.log

   rkhunter --check --sk >> /var/log/security_audit.log

   lynis audit system >> /var/log/security_audit.log

   ```

 

通过以上系统化的排查方法,您可以有效识别香港VPS服务器上的可疑活动,并采取相应措施保障服务器安全。

超过 50,000 人的信任 网硕互联期待你加入我们的会员。