linux系统入侵检测

linux系统入侵检测

一:系统日志分析

1、/var/log/secure查看登陆事件

1> 查看登陆失败事件

    # grep -i Failed /var/log/secure
                
2> 查看登陆成功事件

    # grep -i Accepted /var/log/secure
      
3> 查看sshd服务登陆事件

    # grep -i sshd /var/log/secure
                
 4> 统计登陆成功、失败的IP,去重降序排列

    # grep -i Accepted /var/log/secure |awk '{print $(NF-3)}' |grep '^[0-9]' |sort |uniq -c |sort -rn
    # grep -i Failed /var/log/secure |awk '{print $(NF-3)}' |egrep '^[0-9]' |sort |uniq -c |sort -rn

2、last(查看历史用户登陆信息)

 1> 查看最后5条登陆记录

    #  last -a -5
                 
  2> 查看指定时间之前的登陆记录(2018年04月12日34分56秒)

     # last -a -t 20180429123456
                 
  3> 查看登陆系统的用户相关信息

      # last -a -f /var/log/btmp

3、lastlog(查看记录每个用户最后的登入信息)

    # lastlog

4、w (统计当前在线状态)

   # w

5、查看系统主日志

    # less /var/log/messages

6、查看计划任务

    # less /var/log/cron
    # cat /var/spool/cron/*
    # less /etc/crontab
    # ls /etc/cron.*

二:查看异常流量

1、iftop(-i指定网卡名)

  # yum -y install iftop
  # iftop -i eth0

2、tcpdump(数据包抓取)

基本用法  

  1> 抓10个包放大到/tmp/test.tcpdump文件,读取抓包内容

      #tcpdump  -nnv -i eth0 -c 10 -w /tmp/test.tcpdump
      #tcpdump  -nnv -r /tmp/test.tcpdump
        
      基于条件(Port,Host,Net)

   2> 抓取所有80端口、非80端口的数据包、抓取172.16.129.0/24网段和非172.16.129.0/24所有数据包、抓取某个172.16.129.79的数据包、取源端口80的数据包和目标端口22的数据包

      # tcpdump -nnv -i eth0 port 80
      # tcpdump -nnv -i eth0 not port 80
      # tcpdump -nnv -i eth0 net 172.16.129.0/24
      # tcpdump -nnv -i eth0 not net 172.16.129.0/24
      # tcpdump -nnv -i eth0 host 172.16.129.79
      # tcpdump -nnv -i eth0 src port 80
      # tcpdump -nnv -i eth0 dst port 22
        
        基于协议(arp、icmp、tcp、udp、ip)

3、抓取arp、icmp、tcp、udp数据包

      #  tcpdump -nnv -i eth0 arp
      #  tcpdump -nnv -i eth0 icmp
      #  tcpdump -nnv -i eth0 tcp
      #  tcpdump -nnv -i eth0 udp
      #  tcpdump -nnv -i eth0 ip
       
       基于多个条件:与、或、非(and、or、not)

4、多方式匹配

    1> 抓取非80端口的数据包

      # tcpdump -nnv -i eth0 not port 80
     
    2> 抓取172.16.129.79主机和80端口的数据包

      # tcpdump -nnv -i eth0 host 172.16.129.79 and port 80
            
    3> 抓取和172.16.129.79或者172.16.129.80通讯的数据包

      # tcpdump -nnv -i ech0 host 172.16.129.79 or 172.16.129.80
           
    4> 抓取和172.16.129.79的22端口通讯或者172.16.129.80的80端口通讯的数据包

      # tcpdump -nnv -i eth0 ‘host 172.16.129.79 and port 22’ or ‘host 172.16.129.80 port 80’          
     
     5> 抓取和172.16.129.79 22端口通讯或者和其他主机80端口通讯的数据包

      # tcpdump -nnv -i eth0 host 172.16.129.79 port 22 or port 80
            
     6> 抓取 和172.16.129.79 22端口和80端口通讯的数据包

      # tcpdump -nnv -i eth0 host 172.16.129.79 and \(port 22 or port 80\)               
     
     7> 抓取TCP仅有SYN标记的数据包

      #  tcpdump -nnv -i eth0 tcp[13]==2
                    
     8> 抓取TCP仅有SYN/ACK标记的数据包

      #  tcpdump -nnv -i eth0 tcp[13]==18

三、查看可疑进程

1、基本工具(ps、pstree、top、netstat、ss)

  1> ps(系统进程一般带 “[]” )

      #  ps -aux |less
             
   2> pstree(显示每个程序的完整指令,包含路径、参数或是常驻服务标识、列出树状图时特别标注现在执行的程序)

      # pstree -a
      # pstree -h
              
  3> top(按cpu、内存排序)

      # top -d 1 按P是按cpu排序,按M是按内存排序
                
  4> netstat

      # netstat -anlp
                
  5> ss(查看某个协议或者端口的监听状态)

       # ss -an |grep tcp
       # ss -an |grep 22

2、根据文件或者端口找进程

  1> 根据某文件查看正在被某些进程使用

        # lsoft /usr/local/php/sbin/php-fpm
        # fuser /usr/local/nginx/sbin/nginx
                   
  2> 根据某个端口查看对应进程

        # lsoft -i TCP:22
        #  fuser -v 22/tcp

四:Audit系统审计

作用:

【1】将系统中所有的行为记录下来(或者有选择性的记录):如:什么时间,哪个用户,执行了哪个程序,操作了哪个文件,成功与否, 例如执行过su命令,/tmp目录是否被写入过

【2】audit: Linux系统中用于记录用户底层调用情况的系统,如记录用户执行的open,exit等系统调用,并将记录写到日志文件中,可以通过使用auditctl命令来添加或者删除audit规则。设置针对某个用户进行记录,或针对某个进程的进行记录。

   auditd   audit服务进程 

   auditctl audit规则设置工具

   auditd是audit系统的用户空间程序。主要作用是将audit记录信息写到磁盘上,auditd在启动时会读取两个配置文件:

        /etc/audit/auditd.conf audit配置文件

       /etc/audit/audit.rules audit规则文件

 查看audit日志

     1> ausearch

     2> aureport -l

1、审计对/tmp目录的写入事件

  1> auditctl 添加审计规则(审计tmp目录内写入事件,关键字是tmp_w)(临时添加重启失效)

      # auditctl -w /tmp -p w -k "tmp_w"
            
  2> 查看审计结果

      # ausearch -k "tmp_w" |less
             
  3> 查看规则

      # auditctl -l
               
  4>删除规则

      # auditctl -D
               
  5> 注意:

       auid=0 auid是登录时用户的UID,即初始的ID

       uid=503         uid是当前用户的UID

       uid=0 有效UID(执行命令的用户id) 

       suid=0 当前文件set uid,并属主为root 

2、身份切换审计

  1> 初始UID不为0,后来成为0,即从普通用户切换到root

        # auditctl -a exit,always -S all -F auid!=0 -F uid=0 -k "SU-ROOT"	
                
   2> root切普通用户

        # auditctl -a exit,always -S all -F auid=0 -F uid!=0 -k "SU-USER"	
                 
   3> 测试

             1. 先登录成普通用户,su成root

             2. 先登录成root,su成普通用户

     4> 查看审计结果

        # ausearch -k "SU-ROOT"
        # ausearch -k "SU-USER"

3、审计/bin/su被执行

      1> 添加规则

        # auditctl -w /bin/su -p x -k "EXEC-SU"
                
      2> 测试

           1、root用户登陆切换用户duan

           2、duan用户登陆切换用户root

       3> 查看审计结果

        # ausearch -k "EXEC-SU"

五:文件完整性检查

1、校验RPM包完整性(bash、kernel、sshd和/etc/passwd)没有显示说明包没有被修改

      # rpm -V bash
      # rpm -V kernel
      # rpm -V vsftpd
      # rpm -Vf /etc/ssh/sshd_config
   
   from the package header, followed by the file name.  Each of  the  9  characters  denotes  the
   result of a comparison of attribute(s) of the file to the value of those attribute(s) recorded
   in the database.  A single "." (period) means the test passed, while a  single  "?"  (question
   mark)  indicates the test could not be performed (e.g. file permissions prevent reading). Oth‐
   erwise, the (mnemonically emBoldened) character denotes failure of the corresponding  --verify
   test:
   
   S file Size differs
   M Mode differs (includes permissions and file type)
   5 digest (formerly MD5 sum) differs
   D Device major/minor number mismatch
   L readLink(2) path mismatch
   U User ownership differs
   G Group ownership differs
   T mTime differs
   P caPabilities differ

2、md5sum/sha1sum检测

   1> 获取当前/etc的md5值

        #  find /etc -type f -exec md5sum {} \; >/tmp/`date +%F%H%M`-md5.tx
               
   2> 修改文件、删除文件,添加文件

   3> 重新获取/etc的md5值

        #  find /etc -type f -exec md5sum {} \; >/tmp/`date +%F%H%M`-md5.tx
                
   4> 对比两个md5值获取操作过的文件

        # diff /tmp/2018-05-051830-md5.txt /tmp/2018-05-051832-md5.txt

六:AIDE

1、安装

    # yum -y install aide

2、拷贝并修改配置文件(获取当前状态/etc和/var/www/html状态信息)

    # cp /etc/aide.conf /etc/aide.conf.default
    # vi /etc/aide.conf
    # Next decide what directories/files you want in the database. Aide
    # uses a first match system. Put file specific instructions before generic
    # matches. e.g. Put file matches before directories.

    /boot/   CONTENT_EX
    /bin/    CONTENT_EX
    /sbin/   CONTENT_EX
    /lib/    CONTENT_EX
    /lib64/  CONTENT_EX
    /opt/    CONTENT
   
   上面的配置是系统默认的检测项,可以获取一下默认目录的状态信息,由于更多时候关注的是配置文件或者网站代码的变更,因此需要获取/etc和/var/www/html目录配置信息,NORMAL是一种策略,配置文件中有更多策略选择,这个可以根据实际需求修改。

    修改后的配置文件:

     # Next decide what directories/files you want in the database. Aide
     # uses a first match system. Put file specific instructions before generic
     # matches. e.g. Put file matches before directories.
     /etc NORMAL
     /var/www/html NORMAL

3、保存原始数据状态,默认原始数据状态保存在/var/lib/aide/aide.db.new.gz,这个文件建议离线保存

     # aide -i

4、修改原始数据状态的文件名为var/lib/aide/aide.db.gz

     # mv /var/lib/aide/aide.db.new.gz var/lib/aide/aide.db.gz

5、在/etc和/var/www/html修改文件,进行增删改等操作

6、用aide检测现在当前状态和保存数据状态是对比,结果保存在/tmp/

     # aide -C >/tmp/`date +%F%H%M`-aide.txt

7、查看tmp下的校验文件

     # cat /tmp/*-aide.txt

8、验证没有异常的返回结果

    AIDE, version 0.15.1

    ### All files match AIDE database. Looks okay!

9、如果校验没问题更新数据状态文件

    # aide -u
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值