ファイアウォールの基礎知識の拡張マッチング


1.拡張マッチング:暗黙的拡張と明示的拡張に分けられる
暗黙的拡張:暗黙的拡張とは、特定のプロトコルに対する拡張です.

  
  
  
  
  1.  -p tcp 
  2.  
  3.            --sport PORT[PORT2] 
  4.  
  5.            --sport PORT[PORT2] 
  6.  
  7.            --tcp-flags SYN,ACK,RST,FIN,SYN  —syn,  
  8.  
  9.            --tcp-flags ACK,SYN,FIN,RST ACK,SYN 
  10.  
  11.        -p icmp     ping  
  12.  
  13.            --icmp-type 
  14.  
  15.               echo-reply 0   
  16.  
  17.               echo-reply 8   
  18.  
  19. eg:iptables –t filter –A INPUT –s 172.16.0.0/16 –p icmp-type 8 –j DROP  ping 172.16.0.0/16  
  20.  
  21. iptables -t filter -L -n   
  22.  
  23. iptables -t filter -L -n -v   
  24.  
  25. iptables -L –n –line-numbers   
  26.  
  27. iptables  -t filter -D INPUT 2  INPUT  
  28.  
  29. iptables -t filter -F INPUT  INPUT  
  30.  
  31. 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/ディレクトリの下で、いくつかの一般的なモジュールを例に挙げて説明します.

  
  
  
  
  1. :state 
  2.  
  3.     -m state 
  4.  
  5.        --state    
  6.  
  7.         NEW( ),ESTABLISHED( ),RELATED( ),INVALID( ) 
  8. eg: [root@ns ~]# iptables -t filter -A INPUT -d 172.16.7.1 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT 
  9.  
  10. // 172.16.7.1 tcp/22 NEW ESTABLISHED 
  11.  
  12. [root@ns ~]# iptables -t filter -A OUTPUT -s 172.16.7.1 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT 
  13.  
  14. // 172.16.7.1 tcp/22 ESTABLISHED 
  15.  
  16. [root@ns ~]# iptables -P INPUT DROP 
  17.  
  18. [root@ns ~]# iptables -P OUTPUT DROP 
  19.  
  20. [root@ns ~]# iptables -L -n -v 
  21.  
  22. Chain INPUT (policy DROP 9 packets, 945 bytes) 
  23.  
  24.  pkts bytes target     prot opt in     out     source               destination         
  25.  
  26.   372 25112 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.16.7.1          tcp dpt:22 state NEW,ESTABLISHED 
  27.  
  28.   
  29.  
  30. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 
  31.  
  32.  pkts bytes target     prot opt in     out     source               destination         
  33.  
  34.   
  35.  
  36. Chain OUTPUT (policy DROP 0 packets, 0 bytes) 
  37.  
  38.  pkts bytes target     prot opt in     out     source               destination         
  39.  
  40.    93  8400 ACCEPT     tcp  --  *      *       172.16.7.1           0.0.0.0/0           tcp spt:22 state ESTABLISHED 
  41.  
  42. : , , , NEW , , NEW ESTABELISED , ESTABLISED , 。 
 

  
  
  
  
  1. -m connlimit  [Allows  you  to  restrict  the  number of parallel TCP connections to a server per client IP address (or address block).] 
  2.  
  3.        [!] --connlimit-above #  # , , DROP , , 。[match if the number of existing tcp connections is (not) above #] 
  

  
  
  
  
  1. -m iprange  IP ,  
  2.  
  3.       --src-range ip-ip  IP  
  4.  
  5.       --dst-range ip-ip  IP  
  6.  
  7. g:-m iprange --src-range 172.16.7.1-172.16.100.1  IP  


  
  
  
  
  1.     -m mac  mac  
  2.  
  3.        --mac-source XX:XX:XX:XX:XX:XX mac PREROUTING,INPUT,FORWARD  
  4.  
  5.     -m string  ,  
  6.  
  7.        --algo bm|kmp   algo: algorithm  
  8.  
  9.        --string “pattern”   
  10.  
  11. eg:iptables -I OUTPUT 1 -o eth0 -s 172.16.7.1 -p tcp --sport 80 -m string --algo kmp --string “sex” -j DROP 
  12.  
  13. : sex  
  14.  
  15.     -m recent  ip , IP  
  16.  
  17.        --name NAME 
  18.  
  19.        --set   
  20.  
  21.        --rcheck  --set  
  22.  
  23.        --remove   
  24.  
  25.        --second  , --rcheck|--update  
  26.  
  27.        --hitcount hits 
  28.  
  29.     -m time   
  30.  
  31.        --datestart   
  32.  
  33.        --datestop   
  34.  
  35.        --timestart   
  36.  
  37.        --timestop   
  38.  
  39.        --weekdays   

2.iptablesを使用してnetfilterにルールを追加する

  
  
  
  
  1. :iptables [-t TABLE] COMMAND CHAIN [CRETIRIA ] –j ACTION 
  2.  
  3. Iptables ( ): 
  4.  
  5. /etc/sysconfig/iptables-config 
  6.  
  7. :/etc/rc.d/init.d/iptables-config 。 
  8.  
  9. iptables-config : 
  10.  
  11. Service iptables {status|start|stop|restart|save}, 
  12.  
  13. save , 。 
  14.  
  15. :/etc/sysconfig/iptables 

3.チェーンを新規作成し、メインチェーンを呼び出し、メインチェーンに戻る

  
  
  
  
  1. # iptables -A INPUT -d 172.16.7.1 -j clean_in 
  2. //  
  3. # iptables -N clean_in 
  4. //  
  5. # iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP 
  6. # iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP 
  7. # iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP 
  8. # iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP 
  9. # iptables -A clean_in -p tcp --tcp-flags ALL NONE -j DROP 
  10. # iptables -A clean_in -d 172.16.100.1 -j RETURN  
  11. // ,