linux中iptables防火牆設定學習筆記

來源:互聯網
上載者:User

以下內容總結自鳥哥的 Linux 私房菜 -- 伺服器(第三版),同時推薦喜歡Linux的同學們學習閱讀。

For Linux Kernel 2.6+

I. 圖解防火牆

圖解防火牆

上面的圖示很複雜喔!不過基本上你依舊可以看出來,我們的 iptables 可以控制三種封包的流向:

封包進入 Linux 主機使用資源 (路徑 A): 在路由判斷後確定是向 Linux 主機要求資料的封包,主要就會透過 filter 的 INPUT 鏈來進行控管;

封包經由 Linux 主機的轉遞,沒有使用主機資源,而是向後端主機流動 (路徑 B): 在路由判斷之前進行封包表頭的修訂作業後,發現到封包主要是要透過防火牆而去後端,此時封包就會透過路徑 B 來跑動。 也就是說,該封包的目標並非我們的 Linux 本機。主要經過的鏈是 filter 的 FORWARD 以及 nat 的 POSTROUTING, PREROUTING。 這路徑 B 的封包流向使用方式,我們會在本章的 9.5 小節來跟大家作個簡單的介紹。

封包由 Linux 本機發送出去 (路徑 C): 例如響應用戶端的要求,或者是 Linux 本機主動送出的封包,都是透過路徑 C 來跑的。先是透過路由判斷, 決定了輸出的路徑後,再透過 filter 的 OUTPUT 鏈來傳送的!當然,最終還是會經過 nat 的 POSTROUTING 鏈。

II. 圖解防火牆(隱藏不常用的mangle)

III. 防火牆操作

IV. 防火牆執行個體1

[root@www ~]# vim bin/firewall.sh
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH

# 1. 清除規則
iptables -F
iptables -X
iptables -Z

# 2. 設定政策
iptables -P   INPUT DROP
iptables -P  OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

# 3~5. 制訂各項規則
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -A INPUT -i eth0 -s 192.168.1.0/24 -j ACCEPT

# 6. 寫入防火牆規則設定檔


/etc/init.d/iptables save

[root@www ~]# sh bin/firewall.sh
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
V. 防火牆執行個體2


[root@www ~]# mkdir -p /usr/local/virus/iptables
[root@www ~]# cd /usr/local/virus/iptables
[root@www iptables]# vim iptables.rule
#!/bin/bash

# 請先輸入您的相關參數,不要輸入錯誤了!
  EXTIF="eth0"             # 這個是可以連上 Public IP 的網路介面
  INIF="eth1"              # 內部 LAN 的串連介面;若無則寫成 INIF=""
  INNET="192.168.100.0/24" # 若無內部網域介面,請填寫成 INNET=""
  export EXTIF INIF INNET

# 第一部份,針對原生防火牆設定!##########################################
# 1. 先設定好核心的網路功能:
  echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do
        echo "1" > $i
  done
  for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,\
send_redirects}; do
        echo "0" > $i
  done

實際應用VPS安全之防火牆設定

iptables是基於核心的防火牆,功能非常強大,iptables內建了filter,nat和mangle三張表。

filter負責過濾資料包,包括的規則鏈有,input,output和forward;

nat則涉及到網路位址轉譯,包括的規則鏈有,prerouting,postrouting和output;

mangle表則主要應用在修改資料包內容上,用來做流量整形的,預設的規則鏈有:INPUT,OUTPUT,NAT,POSTROUTING,PREROUTING;

input匹配目的IP是原生資料包;
forward匹配流經原生資料包;
prerouting用來修改目的地址用來做DNAT;
postrouting用來修改源地址用來做SNAT。

I. iptables的文法

iptables -A INPUT -j ACCEPT
-A --append # 向規則鏈中添加一條規則,預設被添加到末尾
iptables -D INPUT -j ACCEPT
-D --delete # 刪條規則鏈


-A 向規則鏈中添加一條規則,預設被添加到末尾
-T 指定要操作的表,預設是filter
-D 從規則鏈中刪除規則,可以指定序號或者匹配的規則來刪除
-R 進行規則替換
-I 插入一條規則,預設被插入到首部
-F 清空所選的鏈,重啟後恢複
-N 建立使用者自訂的規則鏈
-X 刪除使用者自訂的規則鏈
-p 用來指定協議可以是tcp,udp,icmp等也可以是數位協議號,
-s 指定源地址
-d 指定目的地址
-i 進入介面
-o 流出介面
-j 採取的動作,accept,drop,snat,dnat,masquerade
--sport 源連接埠
--dport 目的連接埠,連接埠必須和協議一起來配合使用

-P 定義預設規則(Policy)

文法: iptables -P [INPUT,OUTPUT,FORWARD] [ACCEPT,DROP]

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

II. iptables常用操作

iptables -L  #列出iptables規則
iptables -F  #清除iptables內建規則
iptables -X  #清除iptables自訂規則
# 可以加入-n查看


/etc/rc.d/init.d/iptables save  # 儲存規則,只有儲存規則後重啟機器才不會生效
/etc/init.d/iptables status     # 查看規則
/etc/init.d/iptables stop       # 停用防火牆
chkconfig –level 35 iptables off  # 停止防火牆服務
III. 檢查iptables是否生效

# 檢測是否開啟了22連接埠
telnet www.phpgao.com 22
# 返回telnet: connect to address 104.224.144.xxx: Connection refused
IV. iptables規則的匯出和還原

iptables-save > somefile
iptables-restore < somefile
V. 推薦的規則

*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
#-A OUTPUT -o lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
#-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  DNS(not need this rule, as we only accept the input with [ESTABLISHED,RELATED])
#-A INPUT  -p udp --dport 53 -j ACCEPT
#  DNS
#-A OUTPUT -p udp --dport 53 -j ACCEPT

#  Allow ports for testing
#-A INPUT -p tcp --dport 8080:8090 -j ACCEPT

#  Allow ports for MOSH (mobile shell)
#-A INPUT -p udp --dport 60000:61000 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport xxx -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
#-A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
#-A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT

#  DDoS
-A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

#  Log
-A INPUT  -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A OUTPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Log iptables denied calls
# -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Allow shadowsocks connections
#  The -dport number should be the same port number you set in config.json
-A INPUT -p tcp --dport xxx -j ACCEPT

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT
#-A OUTPUT -j DROP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.