当前位置: 代码迷 >> 综合 >> SELinux及网络监控
  详细解决方案

SELinux及网络监控

热度:98   发布时间:2024-02-11 16:14:59.0

1、编写脚本selinux.sh,实现开启或禁用SELinux功能

#!/bin/bash
#The script boots selinux.
status=$(getenforce) &> /dev/null
if [[ $status == Disabled ]];thensed -i "s@^\(\<SELINUX\>\)=[a-z]\+@\1=enforcing\$@g" /etc/selinux/config    #启用selinux
fi
if [[ $status == Enforcing ]];thensed -i "s@^\(\<SELINUX\>\)=[a-z]\+@\1=disabled\$@g" /etc/selinux/config    #禁用selinux
fi

注:SELinux功能如果处于禁用状态,开启SELinux功能必须重启系统让内核为系统中所有的文件重新标记才能生效。

2、统计/etc/fstab文件中每个文件系统类型出现的次数

[root@wjl ~]# sed -n “/^[^#]/p” /etc/fstab | tr -s “ ” @ | cut -d’@’ -f3 | uniq -c
2 xfs
1 swap
1 iso9660
1 ext4

3、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字

[root@wjl ~]# echo “Yd$C@M05MB%9&Bdh7dq+YVixp3vpw” | grep -o “[[:digit:]]”
0
5
9
7
3

4、解决DOS攻击生产案例:根据web日志或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔5分钟。防火墙命令为:iptables -A INPUT -s IP -j REJECT。

编辑监控脚本并测试:

#查看所有连接此机器的主机
[root@wjl ~]# ss -net | grep "^ESTAB"
ESTAB      0      0             192.168.10.10:22            192.168.10.1:52274  timer:(keepalive,34min,0) ino:31973 sk:ffff880079308780 <->
ESTAB      0      0             192.168.10.10:22            192.168.10.1:52495  timer:(keepalive,40min,0) ino:32116 sk:ffff880079309e00 <->
ESTAB      0      0             192.168.10.10:22            192.168.10.1:52478  timer:(keepalive,39min,0) ino:30535 sk:ffff880079308f00 <->
...[root@wjl ~]# vim /scripts/monitor.sh
#!/bin/bash
#Monitoring network concurrent connections.
num=$(netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' | grep -o "[[:digit:]]")    #统计主机的并发连接数
if [ $num -ge 100 ];theniptables -A INPUT -s 192.168.10.10/24 -j REJECT &> /dev/nullecho "Warning:Concurrent connection of remote host is alrealdy more than threshold,disabld connected"exit 1
elif [ $num -lt 100 ];thenexit 0
fi[root@wjl ~]# bash /scripts/monitor.sh
Warning:Concurrent connection of remote host is alrealdy more than threshold,disabld connected.

编辑计划任务定时监控主机的并发连接数: 

[root@wjl ~]# crontab -e
*/5 * * * * /bin/bash /scripts/monitor.sh