Linux Centos 78 计划任务 开机自动启动 查杀木马过程-使用 rootkit 隐藏踪迹



15章:Linux Centos 7/8 计划任务 开机自动启动 查杀木马过程-使用 rootkit 隐藏踪迹

1、计划任务

1.1用户级别计划任务

使用计划任务 crontab 让木马自动运行

[root@localhost ~]# crontab -e
    #1 2 * * * /usr/bin/fregonnzkq &
#注:分 时 天 月 周 ,表示每天 2 点 1 分,执行命令/usr/bin/fregonnzkq &。* 表每X,比如: 每天,每月,每周

用户计划任务排查方法

[root@localhost ~]# crontab -l
* * * * *  /bin/ls >/tmp/aaa.txt
#查看root用户计划任务 只能查看到当前用户的计划任务

其他用户建立计划任务

[root@localhost ~]# crontab -u bin -e  # -u 指定用户为bin 最好使用系统已经存在的系统用户 

排查查看所有用户建立的计划任务

排查:如何查看所有用户的计划任务? 做黑客要有一个很扎实的基础,还要有一个很好的思维。我 们不用遍历/etc/passwd 中所有的文件,这太 复杂了。因为每个用户的计划任务都有独立的文件。

[root@localhost ~l# ll /var/spool/cron #这个目录下存放着所有用户级别的计划任务
#注:所有用户的计划任务,都会在/var/spool/cron/下产生对应的文件。只要看一下这个目录下的文件,就知道哪些用户生成了计划任务。

1.2系统级别计划任务

查看系统级别计划任务开机自动启动

黑客在系统级别的计划任务中追加木马

[root@localhost tmp]# ls /etc/cron    #按两下tab
cron.d/       cron.daily/   cron.deny     cron.hourly/  cron.monthly/ crontab  
 注:
 crontab #写具体时间的系统级别的定时任务
 cron.d/ #系统级别的定时任务 
 cron.daily/ #系统每天要执行的计划任务
 cron.hourly/ #系统每小时要执行的计划任务 
 cron.monthly/ #系统每月要执行的计划任务
 cron.weekly/ #系统每周要执行的计划任务

[root@localhost ~]# find /etc/cron*    #查看可以添加系统级别的计划任务
/etc/cron.d
/etc/cron.d/0hourly
/etc/cron.d/raid-check
/etc/cron.d/sysstat
/etc/cron.daily
/etc/cron.daily/logrotate
/etc/cron.daily/man-db.cron
/etc/cron.daily/mlocate
/etc/cron.daily/certwatch
/etc/cron.deny
/etc/cron.hourly
/etc/cron.hourly/0anacron
/etc/cron.monthly
/etc/crontab
/etc/cron.weekly

例1:系统级别的计划任务

[root@localhost ~]# vim /etc/crontab #写具体时间的系统级别的定时任务
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  * * * * * root /bin/ls >>/tmp/rootxt.txt
~                                                         

例 2:添加系统级别的木马程序。可以追加到系统自带的脚本中,也可以自己新创建一个脚本,放到 对应的目录下

[root@localhost /]# vim /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
/usr/bin/fregonnzkq &    #这一行是添加的
exit 0
~          

系统计划任务排查方法:利用 md5sum ,来检验文件的完整性

[root@localhost /]# md5sum /etc/cron.daily/logrotate 
fdc63b7057623f10b657cdaf998222e6  /etc/cron.daily/logrotate
#修改一下文件的内容
[root@localhost /]# md5sum /etc/cron.daily/logrotate 
1c39a6fd2b4590b1cbd071bc4febef3b  /etc/cron.daily/logrotate
# MD5sum的值就发生了改变

注:所以通过对比 MD5 值,可以发现文件内容发生了变化。另外,系统级别的计划任务,从安装好 系统后,就固定下来,一般情况不会发生改变。

如何排查/etc/cron下所有文件有没有被黑客修改或追加? 思路:对所有文件都生成一个 md5 值库,后期进行对比,可以快速找出被修改的文件。 例: 对/etc/cron下所有文件都生成 md5 值,并存一个 md5 值

[root@localhost /]# find /etc/cron* -type f -exec md5sum {} \; > /usr/share/file_md5.v1
[root@localhost /]# cat /usr/share/file_md5.v1 
1638f7fe39f7f52c412e1705a0bc52d1  /etc/cron.d/0hourly
367636170f3ac44df6a117e3cbf7e4ba  /etc/cron.d/raid-check
b904bdae184e1d37d52b04dd28bd4ea6  /etc/cron.d/sysstat
16e73be8fe46a83f7525b59f921e9bab  /etc/cron.daily/man-db.cron
5eacfc6f1e56958d60e0ae8dbe65c63b  /etc/cron.daily/mlocate
b2140452ba36c5ddaa51b758ad5e6405  /etc/cron.daily/certwatch
1c39a6fd2b4590b1cbd071bc4febef3b  /etc/cron.daily/logrotate
d41d8cd98f00b204e9800998ecf8427e  /etc/cron.deny
8675eb4a3dba8e20bd6b82c626304556  /etc/cron.hourly/0anacron
fc0c335fab67574cd0e4ff0a404007b4  /etc/crontab

模拟黑客在文件的最后添加以下选中 的内容后,保存退出。

[root@localhost /]# vim /etc/cron.daily/logrotate

#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
/usr/bin/fregonnzkq &
echo "aaaa"   #这段代码是模拟添加的
exit 0

生成MD5sum 然后对比之前的MD5值是否相同

[root@localhost /]# find /etc/cron* -type f -exec md5sum {} \; > /usr/share/file_md51.v1
[root@localhost /]# diff /usr/share/file_md51.v1 /usr/share/file_md5.v1   #对比两个是否相同
7c7
< 08c6cef09a46402d46a26b6a2d438a8d  /etc/cron.daily/logrotate
---
> 1c39a6fd2b4590b1cbd071bc4febef3b  /etc/cron.daily/logrotate

注:如果忘提前生成/usr/share/file_md5.v1 怎么办?

找一台正常和你服务器系统版本号一样的虚拟机,在虚拟机中生成 md5 值数据库文件。复制到咱们 服务器上,再对比。

总结:通过计划任务运行木马程序

1、普通计划任务: crontab

2、高级 crontab ,篡改一个系统级别的计划任务

2、开机自动启动

/etc/rc.local 这个是开机自动启动脚本

特例:很多人 vim 打开一个文件后,看到文档的后面是一片空白,就认为是达到文件的最后了。所 以黑客在/etc/rc.local 中添加很多空行,然后在文档最后添加木马程序, 再把光标移动到行首,保存退 出。也可以躲避一些运维人员的视线,达到隐藏木马的目的。

排查:过滤掉空行 [root@xuegod63 ~]# grep -v ^$ /etc/rc.local

参数说明: ^$ 表示找出以$结束符开头的行,即空行; -v 表示取反显示

[root@localhost tmp]# vim /etc/rc.local #!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
echo "aaaa"
#测试
[root@localhost tmp]# bash /etc/rc.local 
aaaa

2.1 把木马追加到开机启动服务脚本中

把木马程序脚本写到已经存在的开机启动服务中,例如: httpd (apache web 服务器启动脚本

/etc/init.d/ #这个目录下的脚本文件,都是可以开机启动的脚本文件

[root@localhost tmp]# ls /etc/init.d 
functions  netconsole  network  README

2.2 自己写一个开机启动程序

可以编辑network 文件添加自动启动的命令,也可以自己新增开机启动程序

[root@localhost init.d]# vim /etc/init.d/fregonnzkq

#!/bin/sh
# chkconfig: 12345 90 90
# description: fregonnzkq
### END INIT INFO
case $1 in
start)
/usr/bin/fregonnzkq &
;;
stop)
;;
*)
/usr/bin/fregonnzkq &
;;
esac
# 赋予启动脚本执行权限,并启动
[root@xuegod63 ~]# chmod +x /etc/init.d/fregonnzkq 
[root@xuegod63 ~]# service fregonnzkq start
# 添加到开机自动启动服务列表
[root@xuegod63 ~]# chkconfig --add fregonnzkq
[root@xuegod63 ~]# chkconfig --list fregonnzkq

fregonnzkq 0:关 1:开 2:开 3:开 4:开 5:开 6:关
拓展:0-6 为运行级别
0-系统停机状态,系统默认运行级别不能设置为 0,否则不能正常启动,机器关闭。
1-单用户工作状态,root 权限,用于系统维护,禁止远程登陆,就像 Windows 下的安全模式登
录。
2-多用户状态,没有 NFS 支持。
3-完整的多用户模式,有 NFS,登陆后进入控制台命令行模式。
4-系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电
池用尽时,可以切换到这个模式来做一些设置。
5-X11 控制台,登陆后进入图形 GUI 模式,X Window 系统。
6-系统正常关闭并重启,默认运行级别不能设为 6,否则不能正常启动。运行 init 6 机器就会重
启。

2.3删除木马方法

 [root@xuegod63 rc3.d]# chkconfig --del fregonnzkq 
 [root@xuegod63 rc3.d]# rm -rf /etc/init.d/fregonnzkq

2.4排查: 对比法

将/etc/init.d/目录下的所有启动脚本进行 MD5 计算并保存到文件,如果文件被黑客篡改修改则 MD5 值发生改变

 [root@xuegod63 ~]# find /etc/init.d/ -type f -exec md5sum {} \; >  /usr/share/init.d.v1
 [root@xuegod63 ~]# find /etc/init.d/ -type f -exec md5sum {} \; > /usr/share/init.d.v2
 [root@xuegod63 ~]# diff /usr/share/init.d.v1 /usr/share/init.d.v2

排查方法总结 方法 1:对比其他服务器好的配置文件 ,利用 MD5 值做对比 方法 2:查看被黑当天生成或被修改的文件

[root@localhost init.d]# find /etc/init.d/ -mtime -1  #查看前一天被修改的文件
/etc/init.d/

2.5使用 rpm 检查文件的完整性

语法:rpm -V 软件包的名字 #使用 rpm 命令检查软件包安装后,生产的所有的文件的完整 性 语法:rpm -Vf 命令的绝对路径 #使用 rpm 检查命令的完整性

例 1、检查安装完 httpd 软件包后,哪些文件被人修改了
[root@xuegod63 ~]# yum install -y httpd 
[root@xuegod63 ~]# echo aaaa >> /usr/sbin/httpd 
[root@xuegod63 ~]# rpm -V httpd #可以看到 httpd 被修改了 S.5....T. /etc/rc.d/init.d/httpd

rpm -V 弹出的每列消息含意如下:
S file Size 大小不一致
MMode 模式不一致 (包括许可和文件类型)
5 MD5 sum 校验和不一致
DDevice 主从设备号不匹配
L readLink(2) 路径不匹配
UUser 属主不一致
GGroup 所属组不一致
T mTime 修改时间不一致

例 2、查看命令有没有被修改
[root@localhost init.d]# which time   #查看命令位置
/usr/bin/time
[root@localhost init.d]# rpm -Vf /usr/bin/time      #检查命令是否被修改
[root@localhost init.d]# echo aaa >> /usr/bin/time  #模拟修改命令
[root@localhost init.d]# rpm -Vf /usr/bin/time      #再次检查命令是否被修改
S.5....T.    /usr/bin/time                          # S大小不一致 5 md5sum 不一致 T 修改时间不一致
[root@localhost init.d]# 
例 3:系统中有很多命令和软件包,如何校对所有的命令和包?

实战:查看所有 rpm 安装的软件包,生成命令或文件是否被改过? 例:rpm -Va > rpm_check.txt

[root@localhost init.d]# rpm -Va > /root/rpm_check.txt
[root@localhost init.d]# cat /root/rpm_check.txt 
S.5....T.  c /etc/sysconfig/authconfig
遗漏     /var/run/pulse
S.5....T.    /usr/bin/time
S.5....T.  c /etc/php.ini
S.5....T.  c /etc/crontab
S.5....T.  c /etc/yum/pluginconf.d/langpacks.conf
S.5....T.  c /etc/httpd/conf/httpd.conf
.....UG..    /var/www/html
遗漏     /var/run/libgpod
未满足的依赖关系 Nessus-8.15.0-es8.x86_64:
	libc.so.6(GLIBC_2.25)(64bit)(已安裝) Nessus-8.15.0-es8.x86_64 需要
	libc.so.6(GLIBC_2.28)(64bit)(已安裝) Nessus-8.15.0-es8.x86_64 需要
	libstdc++.so.6(CXXABI_1.3.9)(64bit)(已安裝) Nessus-8.15.0-es8.x86_64 需要
	libstdc++.so.6(GLIBCXX_3.4.21)(64bit)(已安裝) Nessus-8.15.0-es8.x86_64 需要
.M.......  g /boot/initramfs-3.10.0-957.el7.x86_64.img
遗漏     /run/gluster
....L....  c /etc/pam.d/fingerprint-auth
....L....  c /etc/pam.d/password-auth
....L....  c /etc/pam.d/postlogin
....L....  c /etc/pam.d/smartcard-auth
....L....  c /etc/pam.d/system-auth
.M.......  g /var/lock/iscsi
.M.......  g /var/lock/iscsi/lock
S.5....T.  c /etc/rc.d/rc.local
.....UG..  g /var/run/avahi-daemon
S.5....T.  c /etc/cups/cups-browsed.conf
S.5....T.  c /var/lib/unbound/root.key
.......T.  c /etc/cron.daily/logrotate
遗漏     /var/run/pluto

3 系统命令被人替换

例 1:黑客修改/替换系统命令 , 查看命令有没有被修改
[root@localhost init.d]# which find
/usr/bin/find
[root@localhost init.d]# cp /usr/bin/find /usr/bin/ffind #拷贝一份find命令出来
[root@localhost init.d]# rm -rf /usr/bin/find            #删除原来的find命令
[root@localhost init.d]# vim /usr/bin/find               #新增一个find命令 内容如下: 
#!/bin/bash
/usr/bin/ffind
echo "aaaaa"
[root@localhost init.d]# chmod +x /usr/bin/find          #添加执行权限
[root@localhost init.d]# find / -name tt                 #测试一下
./README
./functions
./netconsole
./network
aaaaa
#输出了aaaa表示我们修改成功

总结: 黑客如何让脚本定时执行,有以下三种方法:

1、计划任务: crontab 和系统级别的计划任务

2、开机启动 rc.local 和开机启动脚本

3、系统命令被人替换,定一个触发事件

排查木马方法总结:
方法 1:通过生成 md5 值数据库,查询文件系统的完整性
方法 2: 利用 find 命令查找下最近被修改过的文件:一般情况下命令文件都很久之前被修改的。
方法 3: 被入侵后,检测所有 rpm -Va 生成的文件是否被改动过

3、创建一个让root用户删除不了的木马

一台服务器被黑后,找到了木马程序 hack.sh 文件,hack.sh 文件权限为-rw-r–r--。但 是使用 root 用户却删除不了此文件,也无法往此文件中写内容。

[root@localhost ~]# touch hack.sh aaa.sh   #创建两个文件
[root@localhost ~]# ll hack.sh aaa.sh      #查看文件类型
-rw-r--r-- 1 root root 0 7月   3 18:31 aaa.sh
-rw-r--r-- 1 root root 0 7月   3 18:31 hack.sh

黑客悄悄在后台添加 attr 扩展属性

[root@localhost ~]# chattr +i hack.sh    #添加特殊权限
[root@localhost ~]# rm -rf hack.sh       #现在已经删除不掉了
rm: 无法删除"hack.sh": 不允许的操作

chattr chattr 扩展权限说明
+i:即 Immutable,系统不允许对这个文件进行任何的修改。如果目录具有这个属性,那么任何的
进程只能修改目录之下的文件,不允许建立和删除文件。 注:immutable [ɪˈmju:təbl] 不可改变的
-i :移除 i 参数。
5、查看扩展权限
[root@localhost ~]# lsattr hack.sh
----i----------- hack.sh

解决方法

[root@localhost ~]# chattr -i hack.sh 
[root@localhost ~]# lsattr hack.sh 
---------------- hack.sh

总结:如果工作中发现某个木马程序不停的运行,可以利用扩展属性,让木马程序没有可执行权限。

 chmod 0000 /bin/workstat && chattr +i /bin/workstat 

4、生成木马程序父进程实时监控木马

4.1生成木马程序的父进程实现自我检测并运行

生成木马的父进程,自动检测子进程,如果子进程被删除,父进程可以自动启动子进程。 这就是 Linux 删除木马后,木马自动又生成的原因。

[root@localhost ~]# cp /usr/bin/fregonnzkq /usr/wke  #复制一份木马
#使用 pgrep 命令查看进程 pgrep fregonnzkq | wc -l 表示运行了几次
[root@localhost usr]# pgrep fregonnzkq
2937
[root@localhost usr]# pgrep fregonn | wc -l
1
#生成木马程序的父进程脚本
[root@localhost bin]# vim /bin/workstat
#!/bin/bash
while true
do
 a=`pgrep fregonnzkq | wc -l` #统计当前系统运行了几个木马进程
 if [ $a -le 1 ]; then #如查木马进程数小于等 1,那么再启动一个子进程,这样可以保障,最少有两个木马子进程在运行
/bin/cp /usr/wke /usr/bin/fregonnzkq #防止子进程文件被删除
 /usr/bin/fregonnzkq &
#service network restart #防止管理员关闭外网,让木马主动启动网络和外面联系。也可以加上
#清空防火墙规则的命令
fi
done
[root@localhost bin]# chmod +x workstat  #添加可执行权限
[root@localhost bin]# /bin/workstat &  #运行父进程
[3] 19216
#删除掉进程
[root@localhost bin]# ps -aux | grep fregon | grep -v grep
 root      2937  0.0  0.0 113412  1600 pts/1    S    15:39   0:05 /bin/bash /usr/bin/fregonnzkq
 root      5606  0.0  0.0 113412  1588 pts/1    S    15:54   0:03 /bin/bash /usr/bin/fregonnzkq
[root@localhost bin]# kill -9 2937 5606
#因为父进程没杀掉 所以还是会自动运行子进程
[root@localhost bin]# ps -aux | grep fregon | grep -v grep
root     11303  0.0  0.0 113284  1416 pts/1    S    17:42   0:00 /bin/bash /usr/bin/fregonnzkq
root     11304  0.0  0.0 113284  1416 pts/1    S    17:42   0:00 /bin/bash /usr/bin/fregonnzkq

4.1-0 排查方法

删除木马父进程 排查思路:这种情况下,你需要把找到木马的父进程,把父进程删除,再把病毒的原体删除

[root@localhost bin]# pstree | grep freg           # 查找父进程
        |-fregonnzkq---sleep
        |      |             `-workstat---fregonnzkq---sleep
        
[root@localhost bin]# ps axu | grep workstat       # 查看父进程路径
root      6050  2.4  0.0 113280  1408 pts/1    S    15:54   2:52 /bin/bash /usr/bin/workstat
root     21625  0.0  0.0 112824   984 pts/1    S+   17:51   0:00 grep --color=auto workstat

[root@localhost bin]# vim /bin/workstat     #可以查看一下父进程内容
[root@localhost bin]# rm -rf /bin/workstat   #删除父进程
[root@localhost bin]# rm -rf /usr/wke       #删除父进程中调用的病原体 

如果进程杀掉后,快速又生成更多进程,怎么办? 解决方法:不杀进程,但是让进程不工作

[root@localhost bin]# ps axu | grep freg        # 查看所有木马进程
root     11303  0.0  0.0 113284  1444 pts/1    S    17:42   0:00 /bin/bash /usr/bin/fregonnzkq
root     11304  0.0  0.0 113284  1444 pts/1    S    17:42   0:00 /bin/bash /usr/bin/fregonnzkq
root     23694  0.0  0.0 112824   984 pts/1    S+   17:55   0:00 grep --color=auto freg

[root@localhost bin]# top -p 11303              #查看进程状态
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                        
11303 root      20   0  113284   1444   1220 S   0.3  0.0   0:00.35 fregonnzkq  
#S 表示是 sleep 状态 R 表示正在运行
[root@localhost bin]# kill -STOP 11303  #停止进程
[root@localhost bin]# top -p 11303
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                        
11303 root      20   0  113284   1456   1220 T   0.0  0.0   0:00.52 fregonnzkq                                                   

4.2-0 不让木马程序和外网数据主动通信

我的木马需要对外发大量数据包,管理可以通过 iptables 在 output 链上做限制,比如,把从output 链出去 state 状态为 new 的全部 drop 掉。
[root@localhost bin]# iptables -t filter -A OUTPUT -m state --state NEW -j DROP
注:这是基于状态的防火墙规则。 由服务器自己自动发起的 TCP 链接状态为 NEW。木马程序一般都是主动发起连接的。
测试:
[root@localhost bin]# ping 192.168.1.1 #此服务器不能直接和外网通信了
[root@localhost bin]# ping www.baidu.com
ping: unknown host www.baidu.com
当然,黑客也可以在/bin/workstat 脚本中,添加放行木马通信的规则,如下:
iptables -F OUTPUT
iptables -t filter -A OUTPUT -m state --state NEW -j ACCEPT
这样木马就可以上网了。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值