2012年2月9日 星期四

iptables技術分享



這里以一條iptabls的命令來分析Iptables的代碼,該條規則盡量設計比較多的iptables的命令行參數。
假設要下的規則為:
iptabls –A INPUT –i eth0 –p tcp --syn –s 10.0.0.0/8 –d 10.1.28.184 –j ACCEPT 
該規則是將源地址段在10.0.0.0/8範圍之內的主機發送的SYN包,接受的網口為eth0,且目的地址為10.1.28.184的,執行ACCEPT
以下是執行該條命令時,iptables的流程分析。這裡比較不注重分析代碼,而注重執行的流程及結果。
一、命令行的入口
int main(int argc, char *argv[]) (iptables-standalone.c) 
整個iptables命令行的入口。該函數設置了默認的table=”filter”,即當命令行中沒有-t選項是,就使用默認的filter表。同時還初始化了該應用程序的名字,版本號等。
#ifdef NO_SHARED_LIBS 
init_extensions(); 
#endif 
以上代碼為沒有定義共享庫的話,要執行init_extensions()。這裡我們假設不使用共享庫,所以調用該函數。該函數實在執行make的時候extensions/自動生成的initext.c中的函數。在該函數里調用了所有擴展模塊的init函數。
註冊所有的match,以及標準和擴展的target。
所有的match和target都加入到iptables.c中對應的全局鍊錶之中。以後find_match和find_target是就是搜索的這兩個鍊錶。
/* Keeping track of external matches and targets: linked lists. */ 
struct iptables_match *iptables_matches = NULL; 
struct iptables_target *iptables_targets = NULL; 
這樣在不使用共享庫的情況下,每次下命令之前都要初始化一下全局的鍊錶,當然已經存在的話,就不會再次register的。

二、命令行的核心處理
do_command()是處理Iptables命令的核心部分。
int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle) 
(iptables.c)
1.函數首先對一些結構、變量進行初始化。基本上涵蓋了一條規則可能出現的大部分基本參數。其中,重要的結構體有struct ipt_entry fw, *e,這兩個應該是存儲一條防火牆規則的;struct iptables_match *m,struct iptables_target *target,*t; 
分別用於存儲match和target. 
並將全局的match和target鍊錶的對應flags和used為初始化為0.
2.命令行解析。初始化完畢後,進入while循環,分析用戶輸入的命令,設置相關的標誌變量,然後根據相應標誌,調用對應的處理函數。這裡是我們要進行詳細分析的地方。
我們要分析的命令為:
iptabls –A INPUT –i eth0 –p tcp --syn –s 10.0.0.0/8 –d 10.1.28.184 –j ACCEPT 
以下開始命令行解析:
(1)處理–A選項
調用add_command函數,主要是對command變量進行邏輯處理。該函數的輸入參數
newcmd = CMD_APPEND=0x0010,othercmds= CMD_NONE=0x0000,invert = 0;
輸出參數command(初始值為0),
執行完該函數之後,
command = CMD_APPEND=0x0010,
chain = “INPUT” 
然後程序break跳出switch語句,並將invert = FALSE,進行while的下一個循環,及處理下一個選項。
(2)處理–i選項
調用check_inverse函數。該函數主要是檢查-i對應的參數中是否使用了取反標誌”!”,因此這裡我們使用的是”eth0”,因此該函數直接返回FALSE。invert=0。
調用set_option函數是指對應的選項。該函數的輸入參數option= OPT_VIANAMEOUT(0x0080), invert=0,輸出參數options(初始化為0),fw.ip.invflags(初始化為0)。
經過該函數處理之後,
options = OPT_VIANAMEIN=0x0080; 
fw.ip.invflags=0. 
調用parse_interface函數進行網絡接口的解析。該函數的參數輸入參數argv[optind-1]=”eth0”,輸出參數fw.ip.iniface[IFNAMSIZ]={“”}, fw.ip.iniface_mask[IFNAMSIZ]={“”},其中IFNAMSIZ= 15。
處理之後,
fw.ip.iniface[IFNAMSIZ]={“eth0”}; 
fw.ip.iniface_mask[IFNAMSIZ]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
fw.nfcache = NFC_IP_IF_IN = 0x0004;
(3)處理–p選項
調用check_inverse函數,檢查這裡設置的協議參數是否使用了取反標誌”!”。我們這裡使用的是tcp,因此該函數直接返回FALSE。invert仍舊為0。
調用set_option函數是指對應的選項。可以參照(2)中該函數的處理方法。options的初始值為0x0080,最終處理完畢之後
options |= OPT_PROTOCOL(0x0008) = 0x0088; 
fw.ip.invflags=0 
然後協議字串tcp全部轉化為小寫,protocol = “tcp”。調用parse_protocol函數將該協議字符串轉換為TCP對應的協議號6, 
fw.ip.proto = 6; 
fw.nfcache |= NFC_IP_PROTO = 0x0004 | 0x0020 = 0x0024;
(4)處理--syn選項
由於該選項並不在全局變量opts裡面,所以程序會進入switch語句的default分支執行。
進行該分支處理時一些變量的初始值:
target==NULL, m==NULL,
全局鍊錶iptables_matches的used選項都為0 
protocol = “tcp” ,options = 0x0088, proto_used = 0 
根據以上參數的值,這裡要執行的動作為加載該協議(m->init(),即libipt_tcp.c中init()),並且將協議的一些選項擴展到全局變量opts裡面。具體代碼執行是進入該分支的if(m==NULL& amp;&…)裡面,具體執行的結果如下:
m指向全局match鍊錶中tcp的match。(libipt_tcp.c) 
proto_used = 1; 
size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size; 
m->m = fw_calloc(1, size); (m->m為內核總的match部分) 
m ->m->u.match_size = size; 
m->m->u.user.name = “tcp”; 
m->used = 1; 
調用m->init(m->m, &fw.nfcache),執行init()(libipt_tcp.c)。僅是將match中的tcp的sport和dport的最大值置為0xFFFF. struct ipt_entry_match中的data主要用於表示真正數據部分的開始,是通過fw_calloc為整個結構體以及data申請的內存,以後對數據的操作都可以通過data來索引。
init(struct ipt_entry_match *m, unsigned int *nfcache) 
{ 
struct ipt_tcp *tcpinfo = (struct ipt_tcp *)m->data; 
tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF; 
} 
merge_options將tcp的一些選項擴展到全局變量opts裡面。這樣就可以解析--syn選項了。opts包含了tcp協議的選項參數。
並且全局變量global_option_offset += OPTION_OFFSET = 256; 
m->option_offset= global_option_offset =256; 
這裡有點需要注意,就是新加到opts全局結構體中的部分,即tcp對應的option結構中所有的val成員都被賦值為:
merge[num_old + i].val += *option_offset; (見merge_options函數) 
這裡主要是為了區別和原先已有option中的val成員。而且在以後添加更多match模塊的時候,都要做這樣的動作。因此在命令行解析的時候,通過getopt_long得到的這些match中的命令行參數值的時候,需要先減去對應的m->option_offset,然後才能正確的parse. 
然後兩行代碼: optind--; 
continue ; 
是讓程序進入下一個while循環,並再次處理--syn參數。因為以上的處理只是將tcp協議的match進行了初始化工作,並沒有處理該參數。
因此,程序再次進入switch的default分支,不過這次是在if(!target&&…),由於對應tcp的match結構體的used被置1,即m->used =1;因此程序要進行m-> parse進行libipt_tcp中的parse()函數對命令行參數進行處理。該函數的第一個參數的使用方法上面已經解釋過了。以下就是parse()函數對--syn的處理結果:
((struct ipt_tcp *)m->data)->flg_mask = 0x16(SYN,ACK,RST); 
((struct ipt_tcp *)m->data)- >flg_cmp = 0x02(SYN) 
m->flags |= TCP_FLAGS = 0x04; 
fw.nfcache |= NF_IP_TCP_FLAGS(0x0100) = 0x0024 | 0x0100=0x0124 
至此,iptables對—syn選項的解析已經完成。
(5)處理-s選項
同樣還是check_inverse,set_option的處理,只有options變量改變;
options |= OPT_SOURCE(0x0002) = 0x0088 | 0x0002 = 0x008a; 
shostnetworkmask= “10.0.0.0/24”; 
fw.nfcache |= NFC_IP_SRC(0x0001)= 0x0124 | 0x0001 = 0x0125;
(6)處理-d選項
同樣還是check_inverse,set_option的處理,只有options變量改變;
options |= OPT_DESTINATION (0x0004) = 0x008a | 0x0004 = 0x008e; 
dhostnetworkmask= “10.1.28.184”; 
fw.nfcache |= NFC_IP_DST ( 0x0002)= 0x0125 | 0x0002 = 0x0127;
(7)處理-j選項
首先還是set_option函數的處理:
options |= OPT_JUMP(0x0010) = 0x008e | 0x0010 = 0x009e; 
然後jumpto =parse_target(“ACCEPT”),主要是檢查一下該字符串是否合法;
接著target =find_target(jumpto, TRY_LOAD);該函數返回target為”standard”(包含了ACCEPT, DROP, QUEUE,RETURN等目標)的結構體指針。
並且: target->loaded = 1; target->used = 1 
jumpto = “ACCEPT” 
size = IPT_ALIGN(sizeof(struct ipt_entry_target))+ target->size; 
target->t = fw_calloc(1, size); //分配內存給struct ipt_entry_target 
target->t->u.target_size = size; 
target->t->u.user.name = “ACCEPT” 
這裡的target->init指向了libipt_standard.c中的init(),該函數並未執行任何動作。
merge_options又是將該libipt_standard.c的opts加入到全局的opts中。由於ACCEPT目標是標準的,這裡實際上並未往全局的opts中添加任何內容。
僅修改瞭如下變量
global_option_offset += OPTION_OFFSET = 256+256=512; 
target->option_offset= global_option_offset =512; 
至此,命令行已經解析完畢,下面要接著對解析出來的各個參數進行進一步處理。
3. 相關參數的檢查
/*執行libipt_tcp.c中的final_check*/ 
m->final_check(m->mflags); 
/*執行libipt_standard.c中的final_check*/ 
target->final_check(target->tflags); 
隨後是對optind,invert ,command的檢查,接著的if (command &…)對我們的參數沒有產生任何影響。
分別對shostnetworkmask,dhostnetworkmask調用parse_hostnetworkmask函數,執行的結果如下:
saddrs[0]->s_addr = 0x0a000000 (10.0.0.0); 
fw.ip.smsk.s_addr = 0xff000000 (255.0.0.0); 
nsaddrs = 1; 
daddrs[ 0]->s_addr = 0x0a011cb8 (10.1.28.184); 
fw.ip.dmsk.s_addr = 0xffffffff (255.255.255.255); 
ndaddrs = 1; 
最後調用generic_opt_check(command, options)對command和options進行檢查。主要是檢查iptables中的command和options是否搭配。所有的搭配情況保存在全局的數組
static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT]. 
下面則要從內核表裡取出對應表的全部信息。
4.取出內核中相應表的全部信息
因為在main函數中iptc_handle_t handle = NULL;所以這裡要執行的代碼為:
*handle = iptc_init(*table) 
這裡返回一個iptc_handle_t *handle的結構體指針,該指針指向從內核中去中的”filter”(默認)表對應的所有信息。先取出info結構,獲取表的一些大概信息,然後再獲取整個表的規則。
隨後的兩個if (!*handle)都是判斷*handle是否為NULL,如果是的話則說明有錯誤,獲取不到對應表的相關信息。我們這裡應該指向了獲取的”filter”的信息的指針。因此這兩個if判斷都為FALSE。
5.檢查chain與相關options的搭配,target的合法等問題
整個檢查都在if (command == CMD_APPEND…){…}裡面進行。
首先檢查chain和options的匹配情況,這裡chain=”INPUT”,對於本例中設置的規則,應該都沒有問題。
然後是if (target && iptc_is_chain(jumpto, *handle))。其中target為”standard”(包含了ACCEPT, DROP, QUEUE,RETURN等目標)的結構體指針。
(libiptc.c)。由於我們這裡的jumpto為目標ACCEPT,而並非chain,所以該if為FALSE. 
因此,程序真正執行的部分是
e = generate_entry(&fw, iptables_matches, target->t); 
用來生成一個struct ipt_entry e。e中包括了struct ipt_entry,並利用最後一個元素unsigned char elems[0]申請了一塊內存,該內存里首先是若干個match的結構體,然後是一個target結構體,因此該塊內存的大小為n *match + target.
6.具體命令的執行
這部分也是do_command函數的最後。用一個swicth(command)來判斷具體執行什麼動作。我們這裡command=CMD_APPEND,因此調用append_entry函數來將該條iptables規則加入進去。大致介紹一下append_entry函數的功能:
源碼中具體對應的函數名為TC_APPEND_ENTRY()。該函數首先調用find_label找到整個規則中指定chain的struct chain_cache結構,然後做一下target的映射,我們這裡是標準的target。真正執行添加規則的是insert_rules函數。該函數找到插入規則的entry點,並將該規則插入。
調整後的所有的規則都保存在結構體指針handle之中。
三、iptables規則的提交
do_command函數執行完畢,接著調用iptc_commit(&handle)將filter調整後的所有規則提交給內核。
這裡只分析了ipt_init(),append_entry和iptc_commit的功能,沒有具體分析其源代碼。因為這三個函數的源代碼都比較複雜。這裡作為Iptables的流程分析,不具體討論。
整個iptables的工作流程解析完畢。這樣,對iptables的整個命令行的核心代碼從已經有了相當的掌握

link from:http://www.cnblogs.com/500ju/archive/2011/12/28/2305209.html

2012年2月7日 星期二

iptables rule script


##################################################################################

#!/bin/bash
# Program:
#     Iptables Firewall
# 
# History
# 2007/02/01/PM 13:57        TMeng        First release
# 2009/05/25/PM 16:56        TMeng        Second release
# 2009/11/11/AM 10:14        TMeng        Third release
# The IPTABLES part Re-write by "rc.firewall" Scripts from http://iptables-tutorial.frozentux.net/

# I just to integrate it ,and test it ,it can works
#
#*****************************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#

#*****************************************************************************
#
# 1. Configuration options.
#
MY_IP="192.168.10.100"
#
# 1.1 Internet Configuration.
#

#
# 1.1.1 DHCP
#

#
# 1.1.2 PPPoE
#
INET_IFACE="ppp0"
#INET_IFACE="eth0"
INET_IP=`ifconfig $INET_IFACE|grep inet|cut -d : -f 2|cut -d ' ' -f 1`

#
# 1.2 Local Area Network configuration.
#

LAN_IP="192.168.10.1"
LAN_IP_RANGE="192.168.10.0/24"
LAN_IFACE="eth1"

#
# 1.3 DMZ Configuration.
#

#
# 1.4 Localhost Configuration.
#

LO_IFACE="lo"
LO_IP="127.0.0.1"

#
# 1.5 IPTables Configuration.
#

IPTABLES="/sbin/iptables"

#
# 1.6 Other Configuration.
#

###########################################################################
#
# 2. Module loading.
#

#
# Needed to initially load modules
#

/sbin/depmod -a

#
# 2.1 Required modules
#

/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_TTL
/sbin/modprobe ipt_state

#
# 2.2 Non-Required modules
#

/sbin/modprobe ipt_REJECT
/sbin/modprobe ipt_mac
/sbin/modprobe ipt_multiport
/sbin/modprobe ipt_MASQUERADE
/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc

###########################################################################
#
# 3. /proc set up.
#

#
# 3.1 Required proc configuration
#

#Enable packet forward
echo "1" > /proc/sys/net/ipv4/ip_forward
#Modify tcp conntrack timeout established
echo "300" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established

#
# 3.2 Non-Required proc configuration
#

#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr

#
# 3.3 flush iptables and set default policy
#
# reset the default policies in the filter table.
#
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT

#
# reset the default policies in the nat table.
#
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT

#
# reset the default policies in the mangle table.
#
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P POSTROUTING ACCEPT
$IPTABLES -t mangle -P INPUT ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
$IPTABLES -t mangle -P FORWARD ACCEPT

#
# flush all the rules in the filter and nat tables.
#
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
#
# erase all chains that's not default in filter and nat table.
#
$IPTABLES -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X


###########################################################################
#
# 4. rules set up.
#

######
# 4.1 Filter table
#

#
# 4.1.1 Set policies
#

$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP

#
# 4.1.2 Create userspecified chains
#

#
# Create chain for bad tcp packets
#

$IPTABLES -N bad_tcp_packets

#
# Create separate chains for ICMP, TCP and UDP to traverse
#

$IPTABLES -N tcp_packets
$IPTABLES -N udp_packets
$IPTABLES -N icmp_packets

#
# 4.1.3 Create content in userspecified chains
#

#
# bad_tcp_packets chain
#


$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP

$IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset 
#$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
#--log-prefix "New not syn:"


#
# TCP_packets chain rules
#
$IPTABLES -A tcp_packets -p TCP --syn -j ACCEPT
$IPTABLES -A tcp_packets -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
#For PORT mode FTP ,TCP20/21 port
#TCP53 for DNS,TCP2022 for SSH
$IPTABLES -A tcp_packets -p TCP -s 0/0 -m multiport --dports 20,21,53,\
2022 -j ACCEPT
#For PASV mode FTP
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 65400:65420 -j ACCEPT
$IPTABLES -A tcp_packets -p TCP -j DROP



#
# UDP_packets chain rules
#
# Allow the Paopao tang  port
$IPTABLES -A udp_packets -p UDP -s 0/0 -m multiport --dports 4849,5859,6869,\
7879,9278,9520,9521,9522,9523,9963,9756 -j ACCEPT


# Allow the Games ( QQ paopaotang UC neteasePaopao )
$IPTABLES -A udp_packets -p UDP -s 0/0 -m multiport --dports 2022,3001,3002,\
4000,4001,8000,8191,8192,9889,9977,29851,29853 -j ACCEPT

# Allow the Games ( [rexue]chuanqi[shijie] and other )
$IPTABLES -A udp_packets -p UDP -s 0/0 -m multiport --dports 1429,1547,6000,\
6004,6111,6112,7000,7050,7100,7200,7300,7400,7705 -j ACCEPT

# Allow the default port
$IPTABLES -A udp_packets -p UDP -s 0/0 -m multiport --dports 53,443,123,2032,\
2047,2402,3952,8002,9314 -j ACCEPT

#
# ICMP_packets chain rules
#

$IPTABLES -A icmp_packets -p ICMP --icmp-type echo-reply -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP --icmp-type echo-request -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -j DROP

#
# 4.1.4 INPUT chain
#

#
# Bad TCP packets we don't want.
#

$IPTABLES -t filter -A INPUT -p tcp -j bad_tcp_packets

#
# Rules for special networks not part of the Internet
#
$IPTABLES -t filter -A INPUT -p ALL -i $LAN_IFACE -s $LAN_IP_RANGE -j ACCEPT
$IPTABLES -t filter -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -t filter -A INPUT -p ALL -i $LO_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -t filter -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT


#
# Rules for incoming packets from the internet.
#

$IPTABLES -t filter -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPTABLES -t filter -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
$IPTABLES -t filter -A INPUT -p UDP -i $INET_IFACE -j udp_packets
$IPTABLES -t filter -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets

#
# If you have a Microsoft Network on the outside of your firewall, you may 
# also get flooded by Multicasts. We drop them so we do not get flooded by 
# logs
#

#$IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP




# 4.1.5 OUTPUT chain
#

#
# Bad TCP packets we don't want.
#
$IPTABLES -t nat -A OUTPUT --dst $INET_IP -p tcp --dport 21 -j DNAT --to-destination $LAN_IP


$IPTABLES -t filter -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -t filter -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -t filter -A OUTPUT -p ALL -s $INET_IP -j ACCEPT






#
# 4.1.6 FORWARD chain
#

#
# Bad TCP packets we don't want
#

$IPTABLES -t filter -A FORWARD -p tcp -j bad_tcp_packets


# Drop some client connect to internet

$IPTABLES -t filter -A FORWARD -m ipp2p --ipp2p -j DROP
$IPTABLES -t filter -A FORWARD -m ipp2p --xunlei -j DROP
$IPTABLES -t filter -A FORWARD -p tcp -m conntrack --ctstate INVALID -j REJECT
#$IPTABLES -t filter -A FORWARD -p tcp --syn -m connlimit --connlimit-above 35\
--connlimit-mask 32 -j LOG --log-ip-options
$IPTABLES -t filter -A FORWARD -p tcp --syn -m connlimit --connlimit-above 40\
--connlimit-mask 32 -j REJECT

#
# Accept the packets we actually want to forward
#

$IPTABLES -t filter -A FORWARD -p tcp -i $LAN_IFACE -j ACCEPT
#Just allow some DST UDP port ,for filter P2P UDP upload
$IPTABLES -t filter -A FORWARD -p udp -i $LAN_IFACE -j udp_packets
$IPTABLES -t filter -A FORWARD -p icmp -i $LAN_IFACE -j icmp_packets

$IPTABLES -t filter -A FORWARD -p tcp -i $INET_IFACE -j ACCEPT
$IPTABLES -t filter -A FORWARD -p udp -i $INET_IFACE -j udp_packets
$IPTABLES -t filter -A FORWARD -p icmp -i $INET_IFACE -j icmp_packets

#
######
# 4.2 nat table
#

#
# 4.2.1 Set policies
#

#
# 4.2.2 Create user specified chains
#

#
# 4.2.3 Create content in user specified chains
#

#
# 4.2.4 PREROUTING chain
#
# Redirect http request to squid
#$IPTABLES -t nat -A PREROUTING -p tcp -s 192.168.10.0/24  --dport 80 -j REDIRECT --to-ports 3128

# Map emule port to localhost
#$IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p tcp --dport 4662 -j DNAT --to $MY_IP:4662
#$IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p udp --dport 4672 -j DNAT --to $MY_IP:4672

#
# 4.2.5 POSTROUTING chain
#

#
# Enable simple IP Forwarding and Network Address Translation
#
# Added by TMeng ,for ADSL Connection

$IPTABLES -t nat -A POSTROUTING -s $LAN_IP_RANGE -o $INET_IFACE -j MASQUERADE
#$IPTABLES -t nat -A POSTROUTING -s $LAN_IP_RANGE -o $INET_IFACE -j SNAT --to-source $INET_IP





######
# 4.3 mangle table
#

#
# 4.3.1 Set policies
#

#
# 4.3.2 Create user specified chains
#

#
# 4.3.3 Create content in user specified chains
#

#
# 4.3.4 PREROUTING chain
#

#
# 4.3.5 INPUT chain
#

#
# 4.3.6 FORWARD chain
#

#
# 4.3.7 OUTPUT chain
#
#

#
# 4.3.8 POSTROUTING chain
#


# Increase the IPID
$IPTABLES -t mangle -A POSTROUTING -o $INET_IFACE -j IPID --ipid-pace 2
$IPTABLES -t mangle -A POSTROUTING -o $INET_IFACE -j TTL --ttl-set 128
$IPTABLES -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o $INET_IFACE -j TCPMSS --clamp-mss-to-pmtu


##################################################################################

說明:
1.腳本的框架來自於"rc.firewall script"  ,在這裏
http://www.frozentux.net/iptables-tutorial/iptables-tutorial.html#INCLUDERCFIREWALL

2.在框架的基礎上,去掉了原來的allowed鏈,只保留四個自定義的鏈 bad_tcp_packets,tcp_packets,udp_packets,icmp_packets;
框架具有很好的可擴展性,添加和刪除規則基本上都在自身的鏈中完成,對其它規則的影響極小

3.基本流程是這樣的
先對進入的packets作合法性檢查,即選進入bad_tcp_packets,非法的丟棄;
合法的packets,TCP報文進入tcp_packets作進一步匹配,合法通過;
合法的packets,UDP報文進入udp_packets進行匹配,合法通過
余下的icmp報文匹配icmp_packets規則

4.對需要進行packets濾的鏈,input ,output ,forward的默認規則是DROP

5.由於網絡環境為ADSL鏈路,上傳帶寬有限,對UDP報文作了--dport限制,符合要求的放行,對於回流的UDP報文,對Firewall來說其 --dport端口為送出時的--sport,因此在自定義的udp_packets鏈中,將其統一寫入--dport中,使用--multiport的 好處是減少規則的條目,提高匹配效率。

6.對TCP的限制使用connlimit,另,對P2P已知的協議進過相關過濾(如果你也需要此功能,你需要自己編譯相關模塊)。

7.在Firewall上提供vsftp,ssh-server服務。

8.為更好理解相關過濾規則在iptables表、鏈中的相對位置,用顏色表示規則和其對應的鏈。對於自定義的鏈(上述2)規則由於圖表的篇幅,沒有在圖 表中列出,你需要對照腳本4.1.3項中的相關鏈的規則。

Netfilter Extensions HOWTO


Next Previous Contents


Fabrice MARIE , mailing list netfilter-devel@lists.samba.org

$Revision: 3822 $ $Date: 2005-04-03 11:03:46 +0200 (Sun, 03 Apr 2005) $
This document describes how to install and use current iptables extensions for netfilter.

1. Introduction

2. Patch-O-Matic

3. New netfilter matches

4. New netfilter targets

5. New connection tracking patches

6. New IPv6 netfilter matches

7. New IPv6 netfilter targets

8. New IPv6 connection tracking patches

9. Contributing


Next Previous Contents

How to repair and clone disk with ddrescue

  ddrescue  is a tool that can be used to repair and clone disks on a  Linux system . This includes hard drives, partitions, DVD discs, flas...