iptables命令

man幫助

  1. man iptables
  2. man iptables-extensions

規(guī)則格式

image.png
iptables [-t table] SUBCOMMAND chain [-m matchname [per-match-options] ] -j targetname [per-target-options]

-t table

raw, mangle, nat, [filter]默認(rèn)

SUBCOMMAND

  1. 鏈管理
  • -N:new 自定義一條新的規(guī)則鏈
  • -X:delete,刪除自定義的空的規(guī)則鏈
  • -P:policy,設(shè)置默認(rèn)策略;對(duì)filter表而言,默認(rèn)策略有ACCEPT和DROP
  • -E:重命名自定義鏈
  1. 查看
  • -L list,列出指定鏈上的所有規(guī)則,本選項(xiàng)須置后
  • -n numberic,以數(shù)字格式顯示地址和端口號(hào)
  • -v verbose,詳細(xì)信息,-vv
  • -x exactly,顯示計(jì)數(shù)器結(jié)果的精確值,而非單位轉(zhuǎn)換后的易讀值
    --line-numbers:顯示規(guī)則序號(hào)
iptables -vnL
iptables --vvnxL --line_numbers
  1. 規(guī)則管理
  • -A append,追加
  • -I insert,插入,要指明插入至的規(guī)則編號(hào),默認(rèn)為第一條
  • -D delete,刪除,(1)指明規(guī)則序號(hào),指明規(guī)則本身
  • -R replace,替換指定鏈上的指定規(guī)則編號(hào)
  • -F flush,清空指定規(guī)則鏈
  • -Z zero,置零
    iptables每條規(guī)則都有2個(gè)計(jì)數(shù)器,(1)匹配到的報(bào)文個(gè)數(shù)(2)匹配到的所有報(bào)文大小之和

chain

PREROUTING, INPUT, FORWARD,OUPUT, POSTROUTING

匹配條件

基本匹配條件
  • -s address[/mask] 源IP地址或范圍
  • -d address[/mask] 目標(biāo)IP地址或范圍
  • -p protocol tcp,udp, icmp, ...
  • -i 報(bào)文流入接口,INPUT,F(xiàn)ORWARD,PREROUTING
  • -o 報(bào)文流出接口,OUTPUT,F(xiàn)ORWARD,POSTROUTING
擴(kuò)展匹配條件

需要加載擴(kuò)展模塊(/usr/lib64/xtables/*.so),方可生效

  1. 隱式擴(kuò)展
  • tcp
  • udp
  • icmp
  1. 顯式擴(kuò)展
  • multiport
    允許多個(gè)端口
  • iprange
  • mac
  • string
  • time
  • connlimit
  • limit
  • stat

處理動(dòng)作

-j targetname [per-target-options]
target
ACCEPT, DROP, REJECT, RETURN
LOG, SNAT, DNAT, REDIRECT, MASQUERADE
LOG, 將日志記錄在/var/log/messages

規(guī)則優(yōu)化

  1. 安全放行所有入站和出站的狀態(tài)為ESTABLISHED狀態(tài)連接
  2. 謹(jǐn)慎放行入站的新請(qǐng)求
  3. 有特殊目的限制訪問(wèn)功能,要在放行規(guī)則之前加以拒絕
  4. 同類(lèi)規(guī)則(訪問(wèn)同一應(yīng)用),匹配范圍小的放在前面,用于特殊處理
  5. 不同類(lèi)的規(guī)則(訪問(wèn)不同應(yīng)用),匹配范圍大的放在前面
  6. 應(yīng)該將那些可由一條規(guī)則能夠描述的多個(gè)規(guī)則合并為一條
  7. 設(shè)置默認(rèn)策略,建議白名單(只放行特定連接)
  • iptables -P,不建議
  • 建議在規(guī)則的最后定義規(guī)則做為默認(rèn)策略

保存恢復(fù)iptables rule

iptables-save > /data/my.rules
iptables-restore < /data/my.rules

vim /etc/rc.d/rc.local
iptables-restore < /data/my.rules

實(shí)踐

# 刪除rule
iptables -D INPUT 2
# 
iptables -A INPUT -s 192.168.80.3 -j ACCEPT
iptables -A INPUT -s 192.168.80.0/24 -j REJECT

# 插入規(guī)則
iptables -I INPUT -s 192.168.80.13 -j ACCEPT
iptables -I INPUT 2 -s 192.168.80.13 -j ACCEPT
# 替代規(guī)則
iptables -R INPUT 2 -s 192.168.80.14 -j ACCEPT
# INPUT計(jì)數(shù)清零
iptables -Z INPUT

# 一次添加多個(gè)規(guī)則
iptables -I INPUT 2 -s 192.168.80.14,192.168.80.15 -j ACCEPT
# 拒絕所有輸入
iptables -A INPUT -j REJECT
# 允許網(wǎng)卡lo
iptables -I INPUT 5 -i lo -j ACCEPT

# INPUT默認(rèn)policy為DROP
# iptebales -P INPUT DROP

# 允許http訪問(wèn)
iptables -I INPUT 2 -s 192.168.80.12 -p tcp --dport 80 -j ACCEPT

# 允許mysql
iptables -I INPUT 2 -s 192.168.80.12 -p tcp --dport 3306 -j ACCEPT

# 拒絕握手
iptables -I INPUT 3 -s 192.168.80.13 -p tcp --syn -j REJECT

# 允許icmp響應(yīng)報(bào)文
iptables -I INPUT 4 -p icmp --icmp-type 0 -j ACCEPT

# 允許多個(gè)端口
iptables -R INPUT 4 -p tcp -m multiport --dport 139,445 -j ACCEPT

# 設(shè)置mac
iptables -I INPUT 3 -m mac --mac-source 00:0c:29:00:a1:17 -j ACCEPT

# 設(shè)置過(guò)濾string
iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT

# 設(shè)置訪問(wèn)時(shí)間
iptables -I INPUT 3 -m time --timestart 1:00 --timestop 10:00 -j ACCEPT

# 并發(fā)連接超過(guò)100時(shí)拒絕連接
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 100 -j REJECT

# 超過(guò)10個(gè)連接時(shí)每分鐘只能連接20次
iptables -I INPUT -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 10 -j ACCEPT

# 老用戶不受影響,新用戶拒絕
iptables -I INPUT 3 -p tcp --dport 22 -m state --state ESTABLISHED,RELATED -j ACCEPT

# 日志記錄
iptables -I INPUT -s 10.0.1.0/24 -p tcp -m multiport --dports 80,21,22,23 
-m state --state NEW -j LOG --log-prefix "new connections: "
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容