最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

shell 安全腳本的實現(xiàn)

 更新時間:2023年01月12日 09:01:31   作者:THE FOOL295  
本文主要介紹了shell 安全腳本的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

題目:

將密碼輸入錯誤超過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)并且以下輸出確認(rèn)了firewalld啟動了并且在運行:

[root@localhost ~]# systemctl status firewalld

在這里插入圖片描述

2.分析

1、需要知道ssh遠(yuǎn)程訪問記錄在哪個文件中/var/log/secure
2、模擬遠(yuǎn)程訪問輸錯密碼,查看日志文件:

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上進(jìn)行測試:

[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上對本機進(jìn)行訪問,累計達(dá)到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ù)累計達(dá)到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上進(jìn)行測試

[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)文章

  • 處理JSON最強命令jq使用場景

    處理JSON最強命令jq使用場景

    jq命令是處理json字符串的神器,?主要用于獲取JSON屬性/簡單重組JSON字符串,本章詳細(xì)介紹jq的主要應(yīng)用場景,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • 最新評論

    蕲春县| 耒阳市| 深泽县| 定结县| 周至县| 乌鲁木齐市| 吉隆县| 海安县| 武鸣县| 江津市| 安阳市| 修水县| 南投县| 启东市| 波密县| 乡城县| 莒南县| 河北省| 海丰县| 武冈市| 本溪| 日喀则市| 曲周县| 惠水县| 遵化市| 临安市| 宝山区| 安福县| 富民县| 循化| 和平区| 招远市| 五指山市| 永宁县| 田东县| 贵定县| 昆山市| 巴林右旗| 东莞市| 张掖市| 巨鹿县|