如何在Ubuntu系统中加固被挂马的服务器?
美国、香港服务器
如何在Ubuntu系统中加固被挂马的服务器?
03-27 来源:
一、先做最关键:清理后门痕迹
1. 查杀可疑进程
bash
运行
ps -ef --sort=-pcpu | head -20
netstat -antlp
发现陌生进程直接 kill:
bash
运行
kill -9 进程PID
2. 清理定时任务(木马最爱)
bash
运行
crontab -l
crontab -r # 清空所有定时任务
检查所有用户的定时任务:
bash
运行
ls /var/spool/cron/crontabs/
3. 清理异常自启动服务
bash
运行
systemctl list-unit-files | grep enabled
ls /etc/rc.d/rc.local
ls /etc/systemd/system/
4. 清理 /tmp/var/tmp
bash
运行
rm -rf /tmp/* /var/tmp/*
二、强制修改所有密码(必须做)
1. 修改 root 密码
bash
运行
sudo passwd root
2. 修改当前用户密码
bash
运行
passwd
3. 检查是否有可疑用户
bash
运行
cat /etc/passwd | grep "/bin/bash"
发现陌生用户直接删除:
bash
运行
userdel -r 可疑用户名
三、SSH 安全加固(防止再次被爆破)
编辑配置:
bash
运行
sudo nano /etc/ssh/sshd_config
修改或添加以下内容:
plaintext
Port 2222 # 改默认端口(可选但强烈建议)
PermitRootLogin no # 禁止 root 登录
PasswordAuthentication no # 关闭密码登录,只用密钥
PubkeyAuthentication yes
ChallengeResponseAuthentication no
UseDNS no
重启 SSH:
bash
运行
sudo systemctl restart sshd
四、防火墙严格放行(只开必要端口)
bash
运行
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp # 你改后的SSH端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status
五、网站目录权限加固(防上传木马)
1. 网站目录归属改为普通用户,禁止 www-data 写入
bash
运行
sudo chown -R 你的用户名:www-data /var/www/html
2. 目录 755,文件 644(最安全)
bash
运行
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;
3. 上传目录禁止执行 PHP(非常重要)
bash
运行
sudo chmod -R 444 /var/www/html/upload
或在 Nginx 里直接禁止 upload 目录解析 PHP。
六、系统更新 + 关闭危险函数
1. 更新系统补丁
bash
运行
sudo apt update && sudo apt upgrade -y
2. PHP 禁用危险函数(防一句话木马)
编辑 php.ini:
bash
运行
sudo nano /etc/php/8.3/fpm/php.ini
找到 disable_functions,添加:
plaintext
exec,system,passthru,shell_exec,proc_open,popen,pcntl_exec,eval
重启 PHP:
bash
运行
sudo systemctl restart php8.3-fpm
七、安装木马查杀工具
bash
运行
sudo apt install clamav clamav-daemon -y
sudo systemctl stop clamav-freshclam
sudo freshclam
sudo systemctl start clamav-freshclam
全盘扫描:
bash
运行
sudo clamscan -r --remove /var/www
八、检查是否被留隐藏后门
1. 查看隐藏文件
bash
运行
ls -la /var/www/html
ls -la /tmp
2. 搜索一句话木马特征
bash
运行
grep -r "eval\|base64\|assert\|system\|passthru" /var/www/html
九、最后一步:重启服务器
bash
reboot
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快