shell 安全腳本的實現(xiàn)
題目:
將密碼輸入錯誤超過4次的IP地址通過firewalld防火墻阻止訪問
1.初始配置
首先使用systemctl工具啟用firewalld服務(wù):
?[root@localhost ~]# systemctl enable firewalld
如果已經(jīng)啟用了,我們現(xiàn)在可以通過執(zhí)行以下命令啟動firewalld:
[root@localhost ~]# systemctl start firewalld
且可以通過運行以下命令驗證firewalld的狀態(tài)并且以下輸出確認了firewalld啟動了并且在運行:
[root@localhost ~]# systemctl status firewalld
2.分析
1、需要知道ssh遠程訪問記錄在哪個文件中/var/log/secure
2、模擬遠程訪問輸錯密碼,查看日志文件:
cat /var/log/secure
3、了解firewalld添加富規(guī)則的知識:
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject' success [root@localhost ~]# firewall-cmd --reload success [root@localhost ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: no masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject
4、回到192.168.210.133上進行測試:
[root@localhost ~]# ssh root@192.168.210.128 ssh: connect to host 192.168.210.128 port 22: Connection refused
5、測試后將添加的富規(guī)則刪除:
[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject' success [root@localhost ~]# firewall-cmd --reload success [root@localhost ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: no masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
3.編寫腳本
[root@localhost shell]# vim deny_ip.sh #!/bin/bash #************************************************************* #Author:czc #Date: 2023-01-09 #FileName: deny_ip.sh #************************************************************* serc_log=/avr/log/secure ip_list=`awk '/Failed password/ {IP[$(NF-3)]++} END{for (k in IP){if (IP[k]>=4) {print k} }}' /var/log/secure` for ip in `echo $ip_list` do denyed_ip=`firewall-cmd --list-all | awk -F'[= ]' '/rule family/ && $NF="reject" && $(NF-1)~/ssh/ {print $6}' | awk -F'["/]' '{print $2}'` echo $denyed_ip | grep -q $ip [ $? -ne 0 ] && firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="$ip" service name="ssh" reject" done firewall-cmd --reload [root@localhost shell]# chmod a+rx deny_ip.sh
4.測試
由于此時192.168.210.133只輸錯密碼3次,因此執(zhí)行腳本后,并未添加富規(guī)則。
此時再到192.168.210.133上對本機進行訪問,累計達到4次錯誤及以上:[root@localhost ~]# ssh
root@192.168.210.128 root@192.168.210.128's password: Permission denied, please try again. root@192.168.210.128's password: Permission denied, please try again. root@192.168.210.128's password:
可以看到現(xiàn)在的輸入密碼錯誤次數(shù)累計達到5次:
#此時在192.168.210.128上執(zhí)行腳本 [root@localhost shell]# ./deny_ip.sh success success [root@localhost shell]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: no masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.210.133" service name="ssh" reject #再回到192.168.210.133上進行測試 [root@localhost ~]# ssh root@192.168.210.128 ssh: connect to host 192.168.210.128 port 22: Connection refused
至此,腳本的編寫和測試就完成了。更多相關(guān)shell 安全腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux shell 實現(xiàn)用for循環(huán)100次的方法
今天小編就為大家分享一篇Linux shell 實現(xiàn)用for循環(huán)100次的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06linux?shell字符串截取的詳細總結(jié)(實用!)
在開發(fā)的時候經(jīng)常會自行寫一些小的腳本,其中就用到截取字符串的操作,這篇文章主要給大家介紹了關(guān)于linux?shell字符串截取的詳細方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07Linux命令ifconfig報錯command not found的解決方法
最近在安裝Vmware CentOS,輸入ifconfig查看VM的IP地址,提示command not found,發(fā)現(xiàn)沒安裝命令包,此篇文章記錄整個問題解決方法,有和小編遇到一樣的問題的小伙伴可以參考閱讀本文2023-08-08在linux shell腳本中root切換到普通用戶執(zhí)行腳本或命令的方法
今天小編就為大家分享一篇在linux shell腳本中root切換到普通用戶執(zhí)行腳本或命令的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06高級開發(fā)運維測試必須掌握的envsubst命令使用詳解
這篇文章主要為大家介紹了高級開發(fā)運維測試必須掌握的envsubst命令使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04Shell中的for和while循環(huán)詳細總結(jié)
這篇文章主要介紹了Shell中的for和while循環(huán)詳細總結(jié),本文講解了for循環(huán)的數(shù)字段形式、詳細列出、對文件進行循環(huán),while循環(huán)的三種使用場合等內(nèi)容,需要的朋友可以參考下2015-05-05linux下數(shù)據(jù)壓縮的幾種方法與查看方式(示例代碼)
這篇文章主要介紹了linux下數(shù)據(jù)壓縮的幾種方法與查看方式,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10