Linux安全工具

本节介绍常用的Linux安全工具和加固方法。

系统安全检查

检查开放端口和服务

# 查看监听端口
ss -tlnp
netstat -tlnp
 
# 查看所有连接
ss -tunap
 
# 检查开放端口
nmap localhost
nmap -sT -O localhost          # TCP扫描+系统识别

检查用户和权限

# 查看所有用户
cat /etc/passwd | grep -v nologin
 
# 查看sudo用户
grep -Po '^sudo.+:\K.*$' /etc/group
 
# 查看空密码账户
sudo awk -F: '($2 == "") {print $1}' /etc/shadow
 
# 查看最近登录
last
lastb                           # 失败的登录
 
# 查看当前登录用户
w
who

检查异常进程

# 查看所有进程
ps aux
ps aux --sort=-%cpu            # 按CPU排序
ps aux --sort=-%mem            # 按内存排序
 
# 查看进程树
pstree -p
 
# 查找异常进程
ps aux | grep -E "python|perl|nc|ncat"
 
# 检查隐藏进程
ls -la /proc/*/exe 2>/dev/null | grep deleted

检查定时任务

# 查看系统定时任务
cat /etc/crontab
ls -la /etc/cron.*
 
# 查看用户定时任务
crontab -l
for user in $(cut -f1 -d: /etc/passwd); do
    echo "=== $user ===" && crontab -u $user -l 2>/dev/null
done

Fail2ban 防暴力破解

安装配置

# 安装
sudo apt install fail2ban      # Debian/Ubuntu
sudo dnf install fail2ban      # RHEL/CentOS
 
# 创建本地配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vim /etc/fail2ban/jail.local

基本配置

# /etc/fail2ban/jail.local
 
[DEFAULT]
bantime = 1h                   # 封禁时长
findtime = 10m                 # 检测时间窗口
maxretry = 5                   # 最大重试次数
ignoreip = 127.0.0.1/8 192.168.1.0/24  # 白名单
 
# 邮件通知
destemail = admin@example.com
sendername = Fail2Ban
mta = sendmail
 
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h
 
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
 
[recidive]
enabled = true
bantime = 1w                   # 重复违规封禁一周

管理命令

# 启动服务
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
 
# 查看状态
sudo fail2ban-client status
sudo fail2ban-client status sshd
 
# 手动解封
sudo fail2ban-client set sshd unbanip 192.168.1.100
 
# 手动封禁
sudo fail2ban-client set sshd banip 192.168.1.100
 
# 测试规则
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

Rootkit检测

rkhunter

# 安装
sudo apt install rkhunter
 
# 更新数据库
sudo rkhunter --update
 
# 检查系统
sudo rkhunter --check
 
# 快速检查
sudo rkhunter --check --skip-keypress
 
# 查看日志
cat /var/log/rkhunter.log

chkrootkit

# 安装
sudo apt install chkrootkit
 
# 运行检查
sudo chkrootkit
 
# 指定目录
sudo chkrootkit -r /mnt/suspect

文件完整性检查

AIDE

# 安装
sudo apt install aide
 
# 初始化数据库
sudo aideinit
 
# 检查
sudo aide --check
 
# 更新数据库
sudo aide --update
sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db

配置示例

# /etc/aide/aide.conf
 
# 定义检查规则
NORMAL = fuid+fgid+mode+size+mtime+md5+sha256
 
# 监控目录
/etc NORMAL
/bin NORMAL
/sbin NORMAL
/usr/bin NORMAL
/usr/sbin NORMAL
 
# 排除目录
!/tmp
!/var/log
!/var/cache

端口扫描与网络检测

nmap

# 基本扫描
nmap target
 
# 常用选项
nmap -sS target                # SYN扫描(半开)
nmap -sT target                # TCP连接扫描
nmap -sU target                # UDP扫描
nmap -sV target                # 版本检测
nmap -O target                 # 系统检测
 
# 组合扫描
nmap -sS -sV -O target        # SYN+版本+系统
 
# 端口指定
nmap -p 22,80,443 target      # 指定端口
nmap -p 1-1000 target         # 端口范围
nmap -p- target               # 全端口
 
# 扫描网段
nmap -sP 192.168.1.0/24       # Ping扫描
nmap 192.168.1.1-100          # IP范围
 
# 输出
nmap -oN scan.txt target      # 普通格式
nmap -oX scan.xml target      # XML格式
nmap -oA scan target          # 所有格式

网络流量监控

# iftop - 带宽使用
sudo apt install iftop
sudo iftop
 
# nethogs - 按进程监控
sudo apt install nethogs
sudo nethogs
 
# tcpdump - 抓包
sudo tcpdump -i eth0
sudo tcpdump -i eth0 port 80
sudo tcpdump -i eth0 host 192.168.1.100
sudo tcpdump -w capture.pcap -i eth0   # 保存
 
# 查看抓包文件
tcpdump -r capture.pcap

密码安全

密码策略

# 安装密码策略工具
sudo apt install libpam-pwquality
 
# 配置 /etc/security/pwquality.conf
minlen = 12                    # 最小长度
minclass = 4                   # 至少包含4类字符
dcredit = -1                   # 至少1个数字
ucredit = -1                   # 至少1个大写
lcredit = -1                   # 至少1个小写
ocredit = -1                   # 至少1个特殊字符

密码过期

# /etc/login.defs
PASS_MAX_DAYS 90               # 密码最长使用天数
PASS_MIN_DAYS 0                # 密码最短使用天数
PASS_WARN_AGE 7                # 提前警告天数
 
# 查看用户密码信息
chage -l username
 
# 设置密码过期
sudo chage -M 90 username      # 90天后过期
sudo chage -E 2026-12-31 user  # 指定过期日期

检查弱密码

# 检查shadow文件权限
ls -la /etc/shadow
 
# 确保权限正确
sudo chmod 640 /etc/shadow
sudo chown root:shadow /etc/shadow
 
# 检查空密码
sudo awk -F: '($2 == "" || $2 == "!") {print $1}' /etc/shadow

安全加固清单

SSH加固

# /etc/ssh/sshd_config
Port 2222                      # 更改默认端口
PermitRootLogin no             # 禁止root登录
PasswordAuthentication no      # 禁用密码认证
PubkeyAuthentication yes       # 启用密钥认证
MaxAuthTries 3                 # 最大尝试次数
ClientAliveInterval 300        # 会话超时

禁用不必要服务

# 查看运行的服务
systemctl list-unit-files --type=service --state=enabled
 
# 禁用服务
sudo systemctl disable bluetooth
sudo systemctl disable cups
sudo systemctl disable avahi-daemon

内核安全

# /etc/sysctl.conf
# 禁用IP转发
net.ipv4.ip_forward = 0
 
# 禁用源路由
net.ipv4.conf.all.accept_source_route = 0
 
# 启用反向路径过滤
net.ipv4.conf.all.rp_filter = 1
 
# 禁用ICMP重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
 
# 防止SYN洪水
net.ipv4.tcp_syncookies = 1
 
# 应用配置
sudo sysctl -p

文件权限加固

# 关键文件权限
sudo chmod 600 /etc/shadow
sudo chmod 600 /etc/gshadow
sudo chmod 644 /etc/passwd
sudo chmod 644 /etc/group
sudo chmod 600 /etc/ssh/sshd_config
sudo chmod 700 /root
 
# 查找SUID文件
find / -perm -4000 -type f 2>/dev/null
 
# 查找SGID文件
find / -perm -2000 -type f 2>/dev/null
 
# 查找无主文件
find / -nouser -o -nogroup 2>/dev/null

安全审计

auditd

# 安装
sudo apt install auditd
 
# 添加监控规则
# 监控文件访问
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
 
# 监控目录
sudo auditctl -w /etc/ -p wa -k etc_changes
 
# 监控系统调用
sudo auditctl -a always,exit -F arch=b64 -S execve -k commands
 
# 查看日志
sudo ausearch -k passwd_changes
sudo aureport
 
# 持久化规则 /etc/audit/rules.d/audit.rules
-w /etc/passwd -p wa -k passwd_changes
-w /etc/shadow -p wa -k shadow_changes
-w /etc/ssh/sshd_config -p wa -k ssh_config

快速参考

工具用途
nmap端口扫描
ss -tlnp查看监听端口
fail2ban防暴力破解
rkhunterRootkit检测
aide文件完整性检查
auditd安全审计
tcpdump网络抓包
iftop带宽监控