Linux防火墻iptables

2017年2月25日00:46:31 3 5,912 ℃

Linux防火墻

netfilter:frame

iptables:數(shù)據(jù)報文過濾,NAT、mangle等規(guī)則生成的工具

網(wǎng)絡(luò):IP報文首部,TCP報文首部

Linux防火墻iptables

防火墻:硬件、軟件:規(guī)則(匹配標(biāo)準(zhǔn),處理辦法)

Framework: 

默認(rèn)規(guī)則:

開放:堵

關(guān)閉:通

規(guī)則:匹配標(biāo)準(zhǔn)

IP:源IP,目標(biāo)IP

TCP:源端口(sport),目標(biāo)端口(dport),SYN=1,F(xiàn)IN=0,RST=0,ACK=0 ; SYN=1,ACK=1,F(xiàn)IN=0,RST=0 ;ACK=1,SYN=0,RST=0,F(xiàn)IN=0(ESTABLISHED)

UDP:源端口(sport),目標(biāo)端口(dport)

ICMP:icmp-type

數(shù)據(jù)報文過濾:

OpenDBS

Linux2.0

ipfw/firewall

Linux2.2

ipchain/firewall

Linux2.4

iptables/netfilter

hook function:鉤子函數(shù)

prerouting

input

output

forward

postrouting

規(guī)則鏈:

PREROUTING

INPUT

FORWARD

OUTPUT

POSTROUTING

filter(過濾):表

input 進

output 出

forward 轉(zhuǎn)發(fā)

nat(地址轉(zhuǎn)換):表

prerouting

output

postrouting

mangle(拆開、修改、封裝):表

prerouting

input

forward

output

postrouting

raw():

prerouting

output

數(shù)據(jù)包過濾匹配流程

Linux防火墻iptables

可以使用自定義鏈,但只在被調(diào)用時才能發(fā)揮作用,而且如果沒有自定義鏈中的任何規(guī)則匹配,還應(yīng)該有返回機制

用戶可以刪除自定義的空鏈

默認(rèn)鏈無法刪除

每個規(guī)則都有兩個內(nèi)置的計數(shù)器

被匹配的報文個數(shù)

被匹配的報文大小之和

規(guī)則:匹配標(biāo)準(zhǔn),處理動作

iptables [ -t TABLE] COMMAND CHAIN [num]  匹配標(biāo)準(zhǔn) -j 處理辦法

匹配標(biāo)準(zhǔn):

通用匹配

-s, --src :指定源地址

-d, --dst:指定目標(biāo)地址

-p {tcp|udp|icmp}:指定協(xié)議

-i INTERFACE :指定數(shù)據(jù)報文流入的接口

可用于定義標(biāo)準(zhǔn)的鏈:PREOUTING,INPUT,FORWARD

-o INTERFACE:指定數(shù)據(jù)報文流出的接口

可用于標(biāo)準(zhǔn)定義的鏈:OUTPUT ,POSTROUTING,FORWARD

擴展匹配

隱含擴展:不用特別指明由哪個模塊進行的擴展,因為此時使用 -p {tcp|udp|icmp}

--sport PORT[-PORT]: 源端口

--dport PORT[-PORT]: 目標(biāo)端口

--tcp-flags mask comp: 只檢查mask指定的標(biāo)志位,是逗號分隔的標(biāo)志位列表;comp:此列表中出現(xiàn)的標(biāo)記位必須為1,comp中沒出現(xiàn),而mask中出現(xiàn)的,必須為0;

--tcp-flags SYN,FIN,ACK,RST SYN = --syn

--syn:匹配tcp三次握手的第一次

-p icmp

--icmp-type 

0: echo-reply 響應(yīng)報文

8: echo-request 請求報文

iptables -A INPUT -d 172.16.100.7 -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -s 172.16.100.7 -p icmp --icmp-type 0 -m state --state ESTABLISHED -j ACCEPT

-p udp

--sport

--dport

-p tcp --dport

顯式擴展:必須指明由哪個模塊進行的擴展,在iptables中使用-m選項可完成此功能

-m EXTESTION --spe-opt

state: 狀態(tài)擴展

結(jié)合 追蹤會話的狀態(tài)

NEW: 新連接請求

ESTABLISHED:已建立的連接

INVALID:非法連接

RELATED:相關(guān)聯(lián)的

-m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A INPUT -d 172.16.100.7 -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -s 172.16.100.7 -m state --state RELATED,ESTABLISHED -j ACCEPT

ftp服務(wù)首先要裝載ip_conntrack_ftp和ip_nat_ftp模塊

#vim /etc/sysconfig/iptables-config 

IPTABLES_MODULES="ip_conntrack_ftp ip_nat_ftp"

iptables -A INPUT -d 172.16.100.7 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

-m multiport: 離散的多端口匹配擴展

--source-ports

--destination-ports

--ports

-m multiport --destination-ports 21,22,80 -j ACCEPT

條件取反:!,-s ! 172.16.100.6

-m iprange

--src-range

--dst-range

-s,-d

-s IP ,NET

172.16.0.0/16 , 172.16.100.3-172.16.100.100

iptables -A INPUT -p tcp -m iprange --src-range 172.16.100.3-172.16.100.100 --dport 22 -m state --state NEW,ESTABLISHED -j ECCEPT

-m connlimit :鏈接數(shù)限制

! --connlimit-above n

iptables -A INPUT -d 172.16.100.7 -p tcp --dport 80 -m connlimit --connlimit-above 2 -j ACCEPT

-m limit

--limit RATE:#/minute  #/second

--limit-burst  :#最多同時幾個

iptables -A INPUT -d 172.16.100.7 -p icmp --icmp-type 8 -m limit --limit 5/minute --limit-burst 6  -j ACCEPT

-m string

--algo {bm|kmp}

--sting "STRING"

iptables -A OUTPUT -s 172.16.100.7 -p tcp --drort 80 -m sting --algo kmp --sting "hehe"  -j REJECT

處理動作:

-j  TARGET :

ACCEPT:通過

DROP:丟棄

REJECT:拒絕

LOG

--log-prefix "SRTING"

命令:

管理規(guī)則

-A:附加一條規(guī)則,添加在鏈的尾部

-I CHAIN [num]: 插入一條規(guī)則,插入為對應(yīng)CHAIN上的第num條;

-D CHAIN [num]: 刪除指定鏈中的第num條規(guī)則;

-R CHAIN [num]: 替換指定的規(guī)則;

管理鏈:

-F [CHAIN]:flush,清空指定規(guī)則鏈,如果省略CHAIN,則可以實現(xiàn)刪除對應(yīng)表中的所有鏈

-P CHAIN: 設(shè)定指定鏈的默認(rèn)策略;

-N:自定義一個新的空鏈

-X: 刪除一個自定義的空鏈

-Z:置零指定鏈中所有規(guī)則的計數(shù)器;

-E: 重命名自定義的鏈;

查看類:

-L: 顯示指定表中的規(guī)則;

-n: 以數(shù)字格式顯示主機地址和端口號;

-v: 顯示鏈及規(guī)則的詳細(xì)信息

-vv: 

-x: 顯示計數(shù)器的精確值

--line-numbers: 顯示規(guī)則號碼

動作(target):

ACCEPT:放行

DROP:丟棄

REJECT:拒絕

DNAT:目標(biāo)地址轉(zhuǎn)換

SNAT:源地址轉(zhuǎn)換

REDIRECT:端口重定向

MASQUERADE:地址偽裝

LOG:日志

MARK:打標(biāo)記

RETURN:返回,在自定義鏈執(zhí)行完畢后使用返回,來返回原規(guī)則鏈

iptables -t filter -A INPUT -s 172.16.0.0/16 -j DROP

例:允許172.16.100.7,sshd:22/tcp被訪問

iptables -t filter -A INPUT -s 172.16.0.0/16 -d 172.16.100.7 -p tcp --dport 22 -j ACCEPT

iptables -t filter -A OUTPUT -s 172.16.0.7 -d 172.16.0.0/16 -p tcp --sport 22 -j ACCEPT

iptables不是服務(wù),但有服務(wù)腳本;服務(wù)腳本的主要作用在于管理保存的規(guī)則

裝載及移除iptables/netfilter相關(guān)的內(nèi)核模塊;

iptables_nat, iptables_filter, iptables_mangle, iptables_raw, ip_nat, nf_conntrack

nf_conntrack:cat /proc/net/nf_conntrack

/proc/sys/net/nf_conntrack_max 記錄數(shù)量

/proc/sys/net/netfilter/

保存規(guī)則:

#service iptables save

/etc/sysconfig/iptables

#iptables -save > /etc/sysconfig/iptables.2016

#iptables -restore < /etc/sysconfig/iptables.2016

默認(rèn)規(guī)則

iptables -A INPUT -d 192.168.108.130 -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT 

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -d 192.168.108.130 -p tcp -m multiport --destination-ports 21,22,80,3306 -m state --state NEW -j ACCEPT

iptables -A INPUT -d 192.168.108.130 -p icmp --icmp-type 8 -m limit --limit 1/second --limit-burst 5 -j ACCEPT

iptables -A OUTPUT -s 192.168.108.130/32 -m state --state RELATED,ESTABLISHED -j ACCEPT 

iptables -P INPUT DROP

iptables -P OUTPUT DROP

service iptables save

練習(xí):判斷下述規(guī)則的意義:

# iptables -N clean_in

# iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP

# iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP

# iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP

# iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP

# iptables -A clean_in -p tcp --tcp-flags ALL NONE -j DROP

# iptables -A clean_in -d 172.16.100.7 -j RETURN 

# iptables -A INPUT -d 172.16.100.7 -j clean_in

# iptables -A INPUT  -i lo -j ACCEPT

# iptables -A OUTPUT -o lo -j ACCEPT

# iptables -A INPUT  -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP

# iptables -A INPUT  -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP

# iptables -A INPUT  -i eth0 -p udp --dport 1026 -j DROP

# iptables -A INPUT  -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP

# iptables -A INPUT  -p icmp -m limit --limit 10/second -j ACCEPT

利用iptables的recent模塊來抵御DOS攻擊

ssh: 遠程連接,

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP

iptables -I INPUT  -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH

iptables -I INPUT  -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j DROP

1.利用connlimit模塊將單IP的并發(fā)設(shè)置為3;會誤殺使用NAT上網(wǎng)的用戶,可以根據(jù)實際情況增大該值;

2.利用recent和state模塊限制單IP在300s內(nèi)只能與本機建立3個新連接。被限制五分鐘后即可恢復(fù)訪問。

下面對最后兩句做一個說明:

1.第二句是記錄訪問tcp 22端口的新連接,記錄名稱為SSH

--set 記錄數(shù)據(jù)包的來源IP,如果IP已經(jīng)存在將更新已經(jīng)存在的條目

2.第三句是指SSH記錄中的IP,300s內(nèi)發(fā)起超過3次連接則拒絕此IP的連接。

--update 是指每次建立連接都更新列表;

--seconds必須與--rcheck或者--update同時使用

--hitcount必須與--rcheck或者--update同時使用

3.iptables的記錄:/proc/net/ipt_recent/SSH

也可以使用下面的這句記錄日志:

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --name SSH --second 300 --hitcount 3 -j LOG --log-prefix "SSH Attack"

NAT :Network address Translation

DNAT:目標(biāo)地址轉(zhuǎn)換

SNAT:源地址轉(zhuǎn)換(POTROUTING, OUTPUT)

-j SNAT

--to-source

-j MASQUERADE 自動識別外網(wǎng)網(wǎng)卡

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 123.2.13.2

-j DNAT

--to-destination IP[:port]

iptables -t nat -A PREROUTING -d 172.16.100.7 -p tcp --dport 80 -j DNAT --to-destination 192.168.100.2

PNAT:port NAT

layer7 -- l7

應(yīng)用:xunlei, qq, netfilter<--patch

-m layer7 --l7proto xunlei -j DROP

1、給內(nèi)核打補丁,并重新編譯內(nèi)核

2、給iptables源碼打補丁,并重新編譯iptables

3、安裝l7proto

kernel, patch

iptables, patch

Kernel Patch

# tar zxvf  linux-2.6.28.10.tar.gz  -C  /usr/src

# tar zxvf  netfilter-layer7-v2.22.tar.gz  -C  /usr/src

# cd /usr/src

# ln –s  linux-2.6.28.10  linux

# cd /usr/src/linux/

# patch -p1  <  ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch 

# cp /boot/config-2.6.18-164.el5  /usr/src/linux/.config

# make  menuconfig

Networking support → Networking Options →Network packet filtering framework →Core Netfilter Configuration

<M>  Netfilter connection tracking support 

<M>  “l(fā)ayer7” match support

<M>  “string” match support

<M>  “time”  match support

<M>  “iprange”  match support

<M>  “connlimit”  match support

<M>  “state”  match support

<M>  “conntrack”  connection  match support

<M>  “mac”  address  match support

<M>   "multiport" Multiple port match support

Networking support → Networking Options →Network packet filtering framework → IP: Netfilter Configuration

<M> IPv4 connection tracking support (required for NAT)

<M>   Full NAT

<M>     MASQUERADE target support                                                                                   

<M>     NETMAP target support                                                                               

<M>     REDIRECT target support 

# make 

# make modules_install

# make install

Compiles iptables :

# cp /etc/init.d/iptables ~/iptables

# cp /etc/sysconfig/iptables-config ~/

# rpm  -e  iptables-ipv6  iptables  iptstate  --nodeps

# tar jxvf iptables-1.4.6.tar.bz2 –C  /usr/src

# cd /usr/src/iptables-1.4.6

# cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.*   ./extensions/

# ./configure  --prefix=/usr  --with-ksource=/usr/src/linux

# make

# make install

# tar zxvf l7-protocols-2009-05-28.tar.gz

# cd l7-protocols-2009-05-28

# make install

# mv ~/iptables  /etc/rc.d/init.d/

# service iptables start

l7-filter uses the standard iptables extension syntax 

# iptables [specify table & chain] -m layer7 --l7proto [protocol name] -j [action] 

iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 172.16.100.7

iptables -A FORWARD -s 192.168.10.0/24 -m layer7 --l7proto qq -j REJECT

-m time

--datestart  --datestop

--timestart   --timestop

iptables -A FORWARD -s 192.168.10.0/24 -m time --timestart 08:10:00 --timestop 12:00:00 -j DROP

iptables -A FORWARD -s 192.168.10.0/24 -m time --timestart 14:30:00 --timestop 18:00:00 -j DROP

iptables腳本:

#!/bin/bash

ipt=/usr/sbin/iptables

einterface=eth1

linterface=eth0

eip=172.16.100.7

lip=192.168.10.6

$ipt -t nat -F

$ipt -t filter -F

$ipt -t mangle -F

$ipt -N clean_up

$ipt -A clean_up -d 255.255.255.255 -p icmp -j DROP

$ipt -A clean_up -j RETURN

IDS:

nids:snort + iptables =NIPS

hids:

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評論取消回復(fù)

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前評論:3   其中:訪客  0   博主  0

    • avatar Feeey個人博客 1

      博主最近沒怎么更新啊,我來轉(zhuǎn)轉(zhuǎn)。

      • avatar 三五豪俠傳 0

        拜讀大俠博客,感悟人生道理!

        • avatar 增達網(wǎng) 0

          就是喜歡看你博客!