【Python學習】操作防火牆


Windows防火牆cmd
os.system("netsh firewall set opmode mode=disable")

命令:netsh firewall 

  參數: 

  ? // 顯示命令列表 

  add // 添加防火牆配置 

  delete // 刪除防火牆配置 

  dump // 顯示一個配置腳本 

  help // 顯示命令列表 

  reset // 將防火牆配置重置為默認值。 

  set // 設置防火牆配置 

  show // 顯示防火牆配置 

  add allowedprogram // 添加防火牆允許的程序配置。 

  add portopening // 添加防火牆端口配置 

  delete allowedprogram // 刪除防火牆允許的程序配置 

  delete portopening // 刪除防火牆端口配置 

  set allowedprogram // 設置防火牆允許的程序配置 

  set icmpsetting // 設置防火牆 ICMP 配置 

  set logging // 設置防火牆記錄配置 

  set multicastbroadcastresponse // 設置防火牆多播/廣播響應配置 

  set notifications // 設置防火牆通知配置 

  set opmode // 設置防火牆操作配置 

  set portopening // 設置防火牆端口配置 

  set service // 設置防火牆服務配置 

  show allowedprogram // 顯示防火牆允許的程序配置 

  show config // 顯示防火牆配置。 

  show currentprofile // 顯示當前防火牆配置文件 

  show icmpsetting // 顯示防火牆 ICMP 配置 

  show logging // 顯示防火牆記錄配置 

  show multicastbroadcastresponse // 顯示防火牆多播/廣播響應配置 
  show notifications // 顯示防火牆操作配置 

  show opmode // 顯示防火牆端口配置 

  show portopening // 顯示防火牆端口配置 

  show service // 顯示防火牆服務配置 

  show state // 顯示當前防火牆狀態   

  例如: 

  命令:netsh firewall show allowedprogram //查看防火牆放行的程序 

  netsh firewall set portopening TCP 445 ENABLE //打開445端口 

  netsh firewall set portopening TCP 3389 ENABLE // 

  netsh firewall delete allowedprogram C:\A.exe //刪除放行程序A.exe 

  netsh firewall set allowedprogram C:\A.exe A ENABLE //添加程序C盤下的A.exe並放行 

  netsh firewall add allowedprogram C:\A.exe A ENABLE //添加程序C盤下的A.exe並放行

    netsh firewall set icmpsettting type=ALL mode=enable //開啟ICMP協議 

    netsh firewall set icmpsettting type=2 mode=enable  //允許出站數據包太大

 

Linux防火牆
查看:iptables -nvL

如上圖所示,防火牆策略內容如下:
出入站放開mgr的ip(172.16.202.190)和本地ip(127.0.0.1)tcp、udp、icmp的所有端口
拒絕所有ip的tcp的8080端口的入站請求

# 禁止PING
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j DROP
# 禁止UDP的80端口
# 禁止TCP的25端口
iptables -A INPUT -p udp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 25 -j DROP
iptables-save > /etc/iptables.rules
iptables -nvL

單個IP的命令是
iptables -I INPUT -s 211.1.0.0 -j DROP


封IP段的命令是 iptables -I INPUT -s 211.1.0.0/16 -j DROP iptables -I INPUT -s 211.2.0.0/16 -j DROP iptables -I INPUT -s 211.3.0.0/16 -j DROP
封整個段的命令是 iptables -I INPUT -s 211.0.0.0/8 -j DROP
封幾個段的命令是 iptables -I INPUT -s 61.37.80.0/24 -j DROP iptables -I INPUT -s 61.37.81.0/24 -j DROP
查看iptables列表iptables -L

刪除iptables
https://blog.csdn.net/wang0112233/article/details/93063542
iptables -L -n --line-number
iptables -D INPUT 2
INPUT大寫,2表示序號

https://www.cnblogs.com/-mo-/p/11542320.html

永久生效
當你刪除、添加規則后,這些更改並不能永久生效,這些規則很有可能在系統重啟后恢復原樣。為了讓配置永久生效,根據平台的不同,具體操作也不同。下面進行簡單介紹:
1.Ubuntu

首先,保存現有的規則:
iptables-save > /etc/iptables.rules
然后新建一個bash腳本,並保存到/etc/network/if-pre-up.d/目錄下:
#!/bin/bash
iptables-restore < /etc/iptables.rules
這樣,每次系統重啟后iptables規則都會被自動加載。
/!\注意:不要嘗試在.bashrc或者.profile中執行以上命令,因為用戶通常不是root,而且這只能在登錄時加載iptables規則。
2.CentOS, RedHat

# 保存iptables規則
service iptables save

# 重啟iptables服務
service iptables stop
service iptables start
查看當前規則:
cat  /etc/sysconfig/iptables

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM