一、編寫腳本selinux.sh,實(shí)現(xiàn)開啟或禁用SELinux功能;
1、臨時(shí)關(guān)閉;
目前 SELinux 支持三種模式,分別如下:
enforcing :強(qiáng)制模式,代表 SELinux 運(yùn)作中,且已經(jīng)正確的開始限制 domain/type 了;
permissive:寬容模式:代表 SELinux 運(yùn)作中,不過僅會(huì)有警告訊息并不會(huì)實(shí)際限制domain/type 的存取。這種模式可以用來作為 SELinux 的 debug 之用;
disabled :關(guān)閉,SELinux 并沒有實(shí)際運(yùn)作
[root@localhost ~]# cat selinux_temp.sh
#!/bin/bash
echo 'current selinux status!'
getenforce
PS3="enable-selinux or disable-selinux?"
select menu in enable_selinux disable_selinux quit
do
if [ $menu == "enable_selinux" ] ; then
setenforce 1;
echo "selinux is up!"
break;
elif [ $menu == "disable_selinux" ] ; then
setenforce 0;
echo "selinux is down!"
break;
elif [ $menu == "quit" ] ; then
exit
fi
done
[root@localhost ~]# chmod +x selinux_temp.sh
測試:
1)編輯退出;
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# ./selinux_temp.sh
current selinux status!
Enforcing
1) enable_selinux
2) disable_selinux
3) quit
enable-selinux or disable-selinux?3
[root@localhost ~]# getenforce
Enforcing
2)關(guān)閉selinux;
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# ./selinux_temp.sh
current selinux status!
Enforcing
1) enable_selinux
2) disable_selinux
3) quit
enable-selinux or disable-selinux?2
selinux is down!
[root@localhost ~]# getenforce
Permissive
3)開啟selinux;
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# ./selinux_temp.sh
current selinux status!
Permissive
1) enable_selinux
2) disable_selinux
3) quit
enable-selinux or disable-selinux?1
selinux is up!
[root@localhost ~]# getenforce
Enforcing
2、永久關(guān)閉(要重啟才會(huì)生效);
[root@localhost ~]# cat selinux_perm.sh
#!/bin/bash
selinux_cnf="/etc/selinux/config"
case "$1" in
on)
sed -ir 's@^SELINUX=.*@SELINUX=enforcing@' $selinux_cnf
;;
off)
sed -ir 's@^SELINUX=.*@SELINUX=disabled@' $selinux_cnf
;;
*)
echo "Usage: $0 on|off"
;;
esac
[root@localhost ~]# chmod +x selinux_perm.sh
測試:
1)無關(guān)字符或空;
[root@localhost ~]# egrep '^SELINUX=' /etc/selinux/config
SELINUX=enforcing
[root@localhost ~]# ./selinux_perm.sh
Usage: ./selinux_perm.sh on|off
[root@localhost ~]# ./selinux_perm.sh dsfert
Usage: ./selinux_perm.sh on|off
2)關(guān)閉selinux;
[root@localhost ~]# egrep '^SELINUX=' /etc/selinux/config
SELINUX=enforcing
[root@localhost ~]# ./selinux_perm.sh off
[root@localhost ~]# egrep '^SELINUX=' /etc/selinux/config
SELINUX=disabled
3)開啟selinux;
[root@localhost ~]# egrep '^SELINUX=' /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# ./selinux_perm.sh on
[root@localhost ~]# egrep '^SELINUX=' /etc/selinux/config
SELINUX=enforcing
二、統(tǒng)計(jì)/etc/fstab文件中每個(gè)文件系統(tǒng)類型出現(xiàn)的次數(shù);
[root@localhost ~]# egrep -i '^uuid|^\/dev' /etc/fstab
/dev/mapper/centos-root / xfs defaults 0 0
UUID=61bfe580-8947-41c1-91c0-0e3422e7d468 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=e598b458-c2ba-4bbe-a2b7-7791be1278fb /test ext4 acl 0 0
UUID=94464a8b-758d-474c-bf56-55b76c425b19 /users ext4 defaults 0 0
[root@localhost ~]# egrep -i '^uuid|^\/dev' /etc/fstab | awk '{print $3}'
xfs
xfs
swap
ext4
ext4
[root@localhost ~]# egrep -i '^uuid|^\/dev' /etc/fstab | awk '{print $3}' | uniq -c
2 xfs
1 swap
2 ext4
三、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有數(shù)字;
[root@localhost ~]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw" | awk '{gsub(/[^0-9]/,"",$0);print $0}'
05973
[root@localhost ~]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw" | awk '{gsub(/[^0-9]/,"");print $0}'
05973
### 將字符串打印出來,然后利用awk的自帶函數(shù)gsub,對字符串進(jìn)行處理,對匹配到的非數(shù)字部分替換為空,awk默認(rèn)行為是打印$0,即最后只輸出剩下的數(shù)字。
四、解決DOS攻擊生產(chǎn)案例:根據(jù)web日志或者或者網(wǎng)絡(luò)連接數(shù),監(jiān)控當(dāng)某個(gè)IP 并發(fā)連接數(shù)或者短時(shí)內(nèi)PV達(dá)到100,即調(diào)用防火墻命令封掉對應(yīng)的IP,監(jiān)控頻 率每隔5分鐘。防火墻命令為:iptables -A INPUT -s IP -j REJECT。
[root@localhost ~]# cat steal_ip.sh
#!/bin/bash
iplist=`netstat -ant |awk -F'[ :]+' '/ESTABLISHED/{print $6}' | sort | uniq -c | awk '{if($1>100) print $2}' `
for ip in $iplist;
do
iptables -A INPUT -s $ip REJECT
echo "$ip is dangerous ,was rejected!"
done
[root@localhost ~]#chmod +x steal_ip.sh
[root@localhost ~]# crontab -l
*/5 * * * * /usr/bin/bash /steal_ip.sh
---