ramhost VPS 除虫记
笔者前段时间在ramhost VPS上购买了一款小型VPS,用于存放一些托管的企业网站,流量不大,用着也很舒服。今天做例行的网站数据备份,发现VPS上挂的两个网站都无法访问,被重定向到http://www.microsoft.com/en/us/default.aspx 这个网站。在确认了不是本机DNS劫持以及GFW没有拦截以后,笔者随即登录VPS的root查明原因。
查看安全日志
#cat /var/log/secure | grep ssh Oct 21 13:16:03 vps21 sshd[28598]: Accepted password for root from 96.9.176.149 port 53991 ssh2 Oct 21 13:16:03 vps21 sshd[28598]: pam_unix(sshd:session): session opened for user root by (uid=0) Oct 21 13:16:03 vps21 sshd[28598]: pam_unix(sshd:session): session closed for user root Oct 21 13:19:27 vps21 sshd[28625]: Accepted password for root from 24.7.235.27 port 3500 ssh2 Oct 21 13:19:27 vps21 sshd[28625]: pam_unix(sshd:session): session opened for user root by (uid=0) Oct 21 13:23:09 vps21 sshd[28625]: pam_unix(sshd:session): session closed for user root
果然发现异常登录的端倪,经查证这两个IP均来自国外,96.9.176.149 24.7.235.27 。笔者从来没有使用过此IP,因此可以判定VPS确实被黑客入侵过。
修改密码
既然确定VPS被入侵,密码肯定不保了,笔者立即登录ramhost的后台去修改密码,一面回忆着为什么密码会泄漏出去。VPS被入侵让笔者联想到早前一个星期笔者的gmail邮箱被一个来自比利时的IP地址登录过,gmail提醒过要修改密码,我改了gmail密码,却忘记了ramhost VPS的密码还躺在某封邮件里呢,黑客一定是通过这个gmail搞到VPS的密码的,汗=_=!
查看bash_history
既然有黑客光顾,此大虾必然在VPS上做过一些手脚,于是乎查看root下的.bash_history文件,看看命令历史
#cat /root/.bash_history gcc mkdir /usr/sbin/s ; cd /usr/sbin/s && wget http://sysoev.ru/nginx/nginx-0.8.52.tar.gz && tar pxfz nginx-0.8.52.tar.gz && cd nginx-0.8.52 && ./configure --without-http_rewrite_module --without-http-cache --without-http_gzip_module && make >2 && make install && rm -Rf /usr/sbin/s && echo "" > /usr/local/nginx/conf/nginx.conf && vi /usr/local/nginx/conf/nginx.conf yum install make yum --noplugins install make make >2 && make install && rm -Rf /usr/sbin/s && echo "" > /usr/local/nginx/conf/nginx.conf && vi /usr/local/nginx/conf/nginx.conf killall nginx ; /usr/local/nginx/sbin/nginx killall httpd ; killall apache2 ; /usr/local/nginx/sbin/nginx ps xa|grep htt killall lighttpd killall httpd ; killall apache2 ; /usr/local/nginx/sbin/nginx
这位黑客朋友把笔者网站安装的apache和lighthttpd进程全部关闭,安装了nginx,去看看nginx的配置文件就知道它在捣什么鬼了=_=!
#cat /usr/local/nginx/conf/nginx.conf worker_processes 2; worker_rlimit_nofile 80000; error_log /dev/null info; events { worker_connections 50000; } http { client_max_body_size 10m; include mime.types; default_type application/octet-stream; reset_timedout_connection on; access_log /dev/null; server { listen 80; location / { proxy_pass http://207.210.254.42:80/; proxy_set_header Host $host; proxy_set_header REMOTEADDR1 $remote_addr; }}}
竟然定向到了微软公司的主页,笔者开始对黑客的真正目的有些明了了,无非是利用笔者的VPS对微软公司主页进行洪水攻击。
关闭nginx并查看网络状态
#/usr/local/nginx/sbin/nginx stop
#service httpd start
#netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n
在netstat的统计中发现N多IP连接。依照常理不应该有这么多IP连接的,并且这些连接看着都很正常,不似DDOS和SYN攻击。不处理又不行,流量在哗哗地淌啊,每秒流量一直在7Mbps上下,用不了多久VPS的流量就耗光了。没有办法的情况下只好下载了一款防ddos的脚本,死马当作活马医了。
wget http://www.inetbase.com/scripts/ddos/install.sh chmod 0700 install.sh ./install.sh
配置文件说明
##### Paths of the script and other files PROGDIR="/usr/local/ddos" PROG="/usr/local/ddos/ddos.sh" IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list" //白名单 CRON="/etc/cron.d/ddos.cron" APF="/etc/apf/apf" IPT="/sbin/iptables" ---------------------------------------------------------------------------- ##### frequency in minutes for running the script ##### Caution: Every time this setting is changed, run the script with --cron ##### option so that the new frequency takes effect FREQ=1 //ddos.sh执行的频率,单位是分钟,搭配crontab来执行. 如果你更改了该设置,请执行 /usr/local/ddos/ddos.sh --cron ---------------------------------------------------------------------------- ##### How many connections define a bad IP? Indicate that below. NO_OF_CONNECTIONS=150 //当哪个IP的连接数达到150就加入黑名单;查看连接数你可以执行 #netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n ---------------------------------------------------------------------------- ##### APF_BAN=1 (Make sure your APF version is atleast 0.96) ##### APF_BAN=0 (Uses iptables for banning ips instead of APF) APF_BAN=0 //一般情况下你是使用iptables来做防火墙,所以这里你需要将APF_BAN的值改为0. ---------------------------------------------------------------------------- ##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script) ##### KILL=1 (Recommended setting) KILL=1 //将非法的的IP加入iptables的INPUT链中,DROP之. ---------------------------------------------------------------------------- ##### An email is sent to the following address when an IP is banned. ##### Blank would suppress sending of mails EMAIL_TO="root" //给root发一封邮件 ---------------------------------------------------------------------------- ##### Number of seconds the banned ip should remain in blacklist. BAN_PERIOD=600 //关押黑名单中的IP的时长
安装了以后观察发现流量从峰值的7Mbps降到了1Mbps以下,还可以接受,暂时就做了上面的处理,不知道有没有在VPS上留下backdoor,这一块笔者还不是很精通,只能摸着石头过河了。
IPtables的配置
痛定思痛,笔者由于懒惰一直没有配置这台VPS的iptables,结果导致密码丢失后VPS失守。和我一样有VPS的朋友还是早早配置iptables为妙把22端口的访问权限放到最低或使用key文件验证登录可以大大提高服务器的安全性。如果没法得到root shell,黑客入侵也无法成功了。
最后要说一句 Fuck YOU,hacker!
额,看名字还以为真的是除虫呢,呵呵
唉,比虫子还难根除
啊,你都研究这么深了,VPS俺买了1年多了,但还是入门级的。。
貌似还有后门没清除,比较麻烦=_=!
强人啊!!!
受教了,楼主写的真好,还是原创,很容易读懂,谢谢