ファイアウォールの基礎知識の拡張マッチング
1.拡張マッチング:暗黙的拡張と明示的拡張に分けられる
暗黙的拡張:暗黙的拡張とは、特定のプロトコルに対する拡張です.
明示的拡張:明示的拡張はnetfilterの拡張モジュールによって導入する何らかの拡張であり、マッチング条件を指定するために使用され、通常は追加の専用オプションで定義する必要があるため、iptablesでモジュールを使用する場合は-m指定モジュールを使用する必要がある.モジュールの格納場所:/lib/iptables/ディレクトリの下で、いくつかの一般的なモジュールを例に挙げて説明します.
2.iptablesを使用してnetfilterにルールを追加する
3.チェーンを新規作成し、メインチェーンを呼び出し、メインチェーンに戻る
暗黙的拡張:暗黙的拡張とは、特定のプロトコルに対する拡張です.
- -p tcp
-
- --sport PORT[PORT2]
-
- --sport PORT[PORT2]
-
- --tcp-flags SYN,ACK,RST,FIN,SYN —syn,
-
- --tcp-flags ACK,SYN,FIN,RST ACK,SYN
-
- -p icmp ping
-
- --icmp-type
-
- echo-reply 0
-
- echo-reply 8
-
- eg:iptables –t filter –A INPUT –s 172.16.0.0/16 –p icmp-type 8 –j DROP ping 172.16.0.0/16
-
- iptables -t filter -L -n
-
- iptables -t filter -L -n -v
-
- iptables -L –n –line-numbers
-
- iptables -t filter -D INPUT 2 INPUT
-
- iptables -t filter -F INPUT INPUT
-
- iptables -t filter -A INPUT -s 0.0.0.0/0 -d 172.16.100.1 -p tcp --dport 22 -j ACCEPT 0.0.0.0 172.16.100.1 22 .
明示的拡張:明示的拡張はnetfilterの拡張モジュールによって導入する何らかの拡張であり、マッチング条件を指定するために使用され、通常は追加の専用オプションで定義する必要があるため、iptablesでモジュールを使用する場合は-m指定モジュールを使用する必要がある.モジュールの格納場所:/lib/iptables/ディレクトリの下で、いくつかの一般的なモジュールを例に挙げて説明します.
- :state
-
- -m state
-
- --state
-
- NEW( ),ESTABLISHED( ),RELATED( ),INVALID( )
- eg: [root@ns ~]# iptables -t filter -A INPUT -d 172.16.7.1 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
-
- // 172.16.7.1 tcp/22 NEW ESTABLISHED
-
- [root@ns ~]# iptables -t filter -A OUTPUT -s 172.16.7.1 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
-
- // 172.16.7.1 tcp/22 ESTABLISHED
-
- [root@ns ~]# iptables -P INPUT DROP
-
- [root@ns ~]# iptables -P OUTPUT DROP
-
- [root@ns ~]# iptables -L -n -v
-
- Chain INPUT (policy DROP 9 packets, 945 bytes)
-
- pkts bytes target prot opt in out source destination
-
- 372 25112 ACCEPT tcp -- * * 0.0.0.0/0 172.16.7.1 tcp dpt:22 state NEW,ESTABLISHED
-
-
-
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
-
- pkts bytes target prot opt in out source destination
-
-
-
- Chain OUTPUT (policy DROP 0 packets, 0 bytes)
-
- pkts bytes target prot opt in out source destination
-
- 93 8400 ACCEPT tcp -- * * 172.16.7.1 0.0.0.0/0 tcp spt:22 state ESTABLISHED
-
- : , , , NEW , , NEW ESTABELISED , ESTABLISED , 。
- -m connlimit [Allows you to restrict the number of parallel TCP connections to a server per client IP address (or address block).]
-
- [!] --connlimit-above # # , , DROP , , 。[match if the number of existing tcp connections is (not) above #]
- -m iprange IP ,
-
- --src-range ip-ip IP
-
- --dst-range ip-ip IP
-
- g:-m iprange --src-range 172.16.7.1-172.16.100.1 IP
- -m mac mac
-
- --mac-source XX:XX:XX:XX:XX:XX mac PREROUTING,INPUT,FORWARD
-
- -m string ,
-
- --algo bm|kmp algo: algorithm
-
- --string “pattern”
-
- eg:iptables -I OUTPUT 1 -o eth0 -s 172.16.7.1 -p tcp --sport 80 -m string --algo kmp --string “sex” -j DROP
-
- : sex
-
- -m recent ip , IP
-
- --name NAME
-
- --set
-
- --rcheck --set
-
- --remove
-
- --second , --rcheck|--update
-
- --hitcount hits
-
- -m time
-
- --datestart
-
- --datestop
-
- --timestart
-
- --timestop
-
- --weekdays
2.iptablesを使用してnetfilterにルールを追加する
- :iptables [-t TABLE] COMMAND CHAIN [CRETIRIA ] –j ACTION
-
- Iptables ( ):
-
- /etc/sysconfig/iptables-config
-
- :/etc/rc.d/init.d/iptables-config 。
-
- iptables-config :
-
- Service iptables {status|start|stop|restart|save},
-
- save , 。
-
- :/etc/sysconfig/iptables
3.チェーンを新規作成し、メインチェーンを呼び出し、メインチェーンに戻る
- # iptables -A INPUT -d 172.16.7.1 -j clean_in
- //
- # 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.1 -j RETURN
- // ,