iptables cheat sheet

41 篇文章 2 订阅
9 篇文章 0 订阅

iptables

四表五链及其支持的target

tablePREROUTINGINPUTFORWARDOUTPUTPOSTROUTING
rawNOTRACK<NA><NA>NOTRACK<NA>
mangle<all targets><all targets><all targets><all targets><all targets>
natDNATSNAT<NA>DNATSNAT
filter<NA><all targets><all targets><all targets><NA>

标准的 target

项目描述
DROP报文丢弃
ACCEPT报文通过
RETURN不检查当前链中后续的规则。返回到上一层的链,继续检查。

基础例子

项目语法例子
Help for all-hiptables -h
Help for match-m match --helpiptables -m mark --help
Help for target-m target --helpiptables -j DNAT --help
Create a new user-defined chain-N chainiptables -N MY_CHAIN
Delete all rules in chain or all chains-F chainiptables -F FORWARD
Delete a user-defined chain-X [chain]iptables -X MY_CHAIN
Change chain name, (moving any references)-E old-chain new-chainiptables -E OLD_CHAIN NEW_CHAIN
Change policy on chain to target-P chain targetiptables -P FORWARD drop
Append to chain-A chainiptables -A INPUT -j ACCEPT
Insert in chain as rulenum (default 1=first)-I chain [rulenum]iptables -I INPUT 2 -j ACCEPT
Check for the existence of a rule-C chainiptables -C INPUT -j ACCEPT
Delete matching rule from chain-D chainiptables -D INPUT -j ACCEPT
Delete rule rulenum (1 = first) from chain-D chain rulenumiptables -D INPUT 1
Replace rule rulenum (1 = first) in chain-R chain rulenumiptables -R INPUT 3 -j ACCEPT
List the rules in a chain or all chains-L [chain [rulenum]]iptables -nvL FORWARD
List the rules in a chain or all chains(with line number)–line-numbersiptables --line-numbers -nvL FORWARD
Zero counters in chain or all chains-Z [chain [rulenum]]iptables -Z FORWARD
Print the rules in a chain or all chains-S [chain [rulenum]]iptables -S ROSTROUTING -t nat
table to manipulate (default: `filter’)-t tableiptables -t nat -I PREROUTING -d 8.8.8.8 -j DNAT --to 192.168.0.1
extended match (may load extension)-m matchiptables -I FORWARD -m mark --mark 0x123/0xffff -j ACCEPT
by number or name[!] -p proto protocoliptables -I INPUT -p icmp -j DROP
source specification[!] -s address[/mask][…]iptables -I FORWARD -s 192.168.0.0/24 -j DROP
destination specification[!] -d address[/mask][…]iptables -I FORWARD -d 192.168.1.0/24 -j DROP
network interface name ([+] for wildcard)[!] --in-interface -i input name[+]iptables -I FORWARD -i eth0 -j DROP
network interface name ([+] for wildcard)[!] --out-interface -o output name[+]iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
target for rule (may load target extension)-j targetiptables -I INPUT -s 192.168.0.0/24 -j MY_CHAIN
jump to chain with no return-g chainiptables -I INPUT -s 192.168.0.0/24 -g MY_CHAIN

nf_conntrack 模块

nf_conntrack 模块 默认是使用 知名协议端口号的,表中的 port_number

l7_protocolnf_conntrack 模块nf_nat 模块l4_protocolport_numberport_name
ftpnf_conntack_ftpnf_nat_ftptcp21FTP_PORT
tftpnf_conntack_tftpnf_nat_tftpudp69TFTP_PORT
h323nf_conntack_h323nf_nat_h323tcp1719RAS_PORT
h323nf_conntack_h323nf_nat_h323tcp1720Q931_PORT
sipnf_conntack_sipnf_nat_siptcp5060SIP_PORT
rtspnf_conntack_rtspnf_nat_rtsptcp554RTSP_PORT

如果要使用其他的端口号,那么需要重新加载 nf_conntrack 模块,并使用 ports= 来参数来指定。
注意一般可配置的个数,上限是 8个。

项目例子
查看模块lsmod | grep ftp
卸载 nf_nat 模块rmmod nf_nat_ftp
卸载 nf_conntrack 模块rmmod nf_conntack_ftp
加载 nf_conntrack 模块(一般)modprobe nf_conntack_ftp
加载 nf_conntrack 模块(指定其他端口)modprobe nf_conntack_ftp ports=21,2125,2135
加载 nf_nat 模块modprobe nf_nat_ftp

常用 match and target

源码文件夹

/net/netfilter

state match options:

[!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]
			State(s) to match

conntrack match options:

[!] --ctstate {INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED|SNAT|DNAT}[,...]
							   State(s) to match
[!] --ctproto proto            Protocol to match; by number or name, e.g. "tcp"
[!] --ctorigsrc address[/mask]
[!] --ctorigdst address[/mask]
[!] --ctreplsrc address[/mask]
[!] --ctrepldst address[/mask]
							   Original/Reply source/destination address
[!] --ctorigsrcport port
[!] --ctorigdstport port
[!] --ctreplsrcport port
[!] --ctrepldstport port
							   TCP/UDP/SCTP orig./reply source/destination port
[!] --ctstatus {NONE|EXPECTED|SEEN_REPLY|ASSURED|CONFIRMED}[,...]
							   Status(es) to match
[!] --ctexpire time[:time]     Match remaining lifetime in seconds against
							   value or range of values (inclusive)
	--ctdir {ORIGINAL|REPLY}   Flow direction of packet

connmark match options:

[!] --mark value[/mask]    Match ctmark value with optional mask
[root@localhost proc]## 

statistic match options:

 --mode mode                    Match mode (random, nth)
 random mode:
[!] --probability p		 Probability
 nth mode:
[!] --every n			 Match every nth packet
 --packet p			 Initial counter value (0 <= p <= n-1, default 0)

tcp match options:

[!] --tcp-flags mask comp	match when TCP flags & mask == comp
				(Flags: SYN ACK FIN RST URG PSH ALL NONE)
[!] --syn			match when only SYN flag set
				(equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)
[!] --source-port port[:port]
--sport ...
					match source port(s)
	[!] --destination-port port[:port]
--dport ...
					match destination port(s)
	[!] --tcp-option number        match if TCP option set

udp match options:

[!] --source-port port[:port]
 --sport ...
				match source port(s)
[!] --destination-port port[:port]
 --dport ...
				match destination port(s)

icmp match options:

[!] --icmp-type typename	match icmp type
[!] --icmp-type type[/code]	(or numeric type or type/code)
Valid ICMP Types:
any
echo-reply (pong)
destination-unreachable
   network-unreachable
   host-unreachable
   protocol-unreachable
   port-unreachable
   fragmentation-needed
   source-route-failed
   network-unknown
   host-unknown
   network-prohibited
   host-prohibited
   TOS-network-unreachable
   TOS-host-unreachable
   communication-prohibited
   host-precedence-violation
   precedence-cutoff
source-quench
redirect
   network-redirect
   host-redirect
   TOS-network-redirect
   TOS-host-redirect
echo-request (ping)
router-advertisement
router-solicitation
time-exceeded (ttl-exceeded)
   ttl-zero-during-transit
   ttl-zero-during-reassembly
parameter-problem
   ip-header-bad
   required-option-missing
timestamp-request
timestamp-reply
address-mask-request
address-mask-reply

hashlimit match options:

--hashlimit-upto <avg>           max average match rate
							   [Packets per second unless followed by 
							   /sec /minute /hour /day postfixes]
--hashlimit-above <avg>          min average match rate
--hashlimit-mode <mode>          mode is a comma-separated list of
							   dstip,srcip,dstport,srcport (or none)
--hashlimit-srcmask <length>     source address grouping prefix length
--hashlimit-dstmask <length>     destination address grouping prefix length
--hashlimit-name <name>          name for /proc/net/ipt_hashlimit
--hashlimit-burst <num>	    number to match in a burst, default 5
--hashlimit-htable-size <num>    number of hashtable buckets
--hashlimit-htable-max <num>     number of hashtable entries
--hashlimit-htable-gcinterval    interval between garbage collection runs
--hashlimit-htable-expire        after which time are idle entries expired?

limit match options:

--limit avg			max average match rate: default 3/hour
								[Packets per second unless followed by 
								/sec /minute /hour /day postfixes]
--limit-burst number		number to match in a burst, default 5

helper match options:

[!] --helper string        Match helper identified by string

SNAT target options:

 --to-source [<ipaddr>[-<ipaddr>]][:port[-port]]
				Address to map source to.
[--random] [--persistent]

DNAT target options:

 --to-destination [<ipaddr>[-<ipaddr>]][:port[-port]]
				Address to map destination to.
[--random] [--persistent]

MASQUERADE target options:

--to-ports <port>[-<port>]
			Port (range) to map to.
--random
			Randomize source port.

REDIRECT target options:

 --to-ports <port>[-<port>]
				Port (range) to map to.

MARK target options:

--set-xmark value[/mask]  Clear bits in mask and XOR value into nfmark
--set-mark value[/mask]   Clear bits in mask and OR value into nfmark
--and-mark bits           Binary AND the nfmark with bits
--or-mark bits            Binary OR the nfmark with bits
--xor-mask bits           Binary XOR the nfmark with bits

CT target options:

--notrack			Don't track connection
--helper name			Use conntrack helper 'name' for connection
--timeout name 		Use timeout policy 'name' for connection
--ctevents event[,event...]	Generate specified conntrack events for connection
--expevents event[,event...]	Generate specified expectation events for connection
--zone ID			Assign/Lookup connection in zone ID

CONNMARK target options:

--set-xmark value[/ctmask]    Zero mask bits and XOR ctmark with value
--save-mark [--ctmask mask] [--nfmask mask]
							Copy nfmark to ctmark using masks
--restore-mark [--ctmask mask] [--nfmask mask]
							Copy ctmark to nfmark using masks                                
--set-mark value[/mask]       Set conntrack mark value
--save-mark [--mask mask]     Save the packet nfmark in the connection
--restore-mark [--mask mask]  Restore saved nfmark value
--and-mark value              Binary AND the ctmark with bits
--or-mark value               Binary OR  the ctmark with bits
--xor-mark value              Binary XOR the ctmark with bits

LOG target options:

--log-level level		Level of logging (numeric or see syslog.conf)
--log-prefix prefix		Prefix log messages with this prefix.
--log-tcp-sequence		Log TCP sequence numbers.
--log-tcp-options		Log TCP options.
--log-ip-options		Log IP options.
--log-uid			Log UID owning the local socket.
--log-macdecode		Decode MAC addresses and protocol.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值