iptablesとnetfilter
20376 ワード
iptables( ) netfilter
=================================================
:
http://www.360doc.com/content/11/0506/09/706976_114731108.shtml#
http://drops.wooyun.org/tips/1424
netfilter/iptables :
(1)IP netfilter iptables 。
(2)netfilter , 、 ,
(3)iptables , , 。
(4)netfilter Linux , (tables) (chains) , (rules) 。
:
:
(1) :PREROUTING --> INPUT ==> 。
(2) : ==> OUTPUT --> POSTROUTING
(3) :PREROUTING --> FORWARD --> POSTROUTING
:==> , --> 。
( --> ):
(1):raw: nat 。
:PREROUTING -> OUTPUT
(2):mangle: , 。
:PREROUTING -> INPUT -> FORWARD -> OUTPUT -> POSTROUTING
(3):nat:network address translation (ip , )。
:PREROUTING -> INPUT -> OUTPUT -> POSTROUTING
(4):filter: , 。
:INPUT -> FORWARD -> OUTPUT
(netfilter): 。
(1):PREROUTING:
(2):INPUT:
(3):FORWARD: ,
(4):OUTPUT: ,
(5):POSTROUTIONG:
=================================================
iptables :
:
CentOS 7
~]# systemctl stop firewalld.service
~]# systemctl disable firewalld.service
CentOS 6
~]# service iptables stop
~]# chkconfig iptables off
...........................................................................................................................................................................
(1)
:iptables [-t table] SUBCOMMAND chain [matches...] [target]
iptables [-t ] [ ] [ ] [-j ]
...........................................................................................................................................................................
-t table: raw, mangle, nat, [filter]
~]# iptables -t raw -nvL
~]# iptables -t mangle -nvL
~]# iptables -t nat -nvL
~]# iptables -nvL
...........................................................................................................................................................................
(2)SUBCOMMAND:
(2.1) :
-N: new: 。
~]# iptables -N test
-X:delete: 。
~]# iptables -X mytest
-P:policy: 。
ACCEPT:
DROP:
REJECT: : REJECT。
~]# iptables -t filter -P FORWARD DROP
~]# iptables -t filter -P INPUT DROP
~]# iptables -t filter -P OUTPUT DROP
: filter FORWARD,INPUT,OUTPUT DROP REJECT
-E:rename: ( 0) 。
~]# iptables -E test mytest
(2.2) :
-A:append: ( )。
-I:insert: ( )。
-D:delete: 。
1:rule specification
2:rule number
~]# iptables -D OUTPUT 1
-R:replace: 。
-F:flush: , 。
-Z:zero: 0。
iptables 。
1: packets。
2: bytes。
S:selected, iptables-save 。
(2.3) :
-L: list,
-n:numeric, ;
-v:verbose, ;-vv, -vvv
-x:exactly,
--line-numbers:
~]# iptables -nvL --line-numbers
:-nvL
~]# iptables -nvL
...........................................................................................................................................................................
(3)matches :
1: :netfilter
[!] -s, --source address[/mask][,...]:
~]# iptables -A INPUT -s 172.18.0.0/16 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.0.0/16 -j ACCEPT
~]# iptables -A FORWARD -s 172.18.0.0/16 -j ACCEPT
[!] -d, --destination address[/mask][,...]:
~]# iptables -A INPUT -s 172.18.21.72 -d 172.18.21.62 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -d 172.18.21.72 -j ACCEPT
[!] -i, --in-interface name: , PREROUTING,INPUT FORWARD。
[!] -o, --out-interface name: , OUTPUT,FORWARD POSTROUTING。
[!] -p {tcp|udp|icmp}:
~]# iptables -I INPUT -s 172.18.21.72 -d 172.18.21.62 -p tcp -j ACCEPT
~]# iptables -I OUTPUT -s 172.18.21.62 -d 172.18.21.72 -p tcp -j ACCEPT
2: : ,-m matchname
:
: -m ; -p 。
: -m 。
(1): :
[!] -p, --protocol PROTOCOL PROTOCOL:
:tcp, udp, icmp, icmpv6, esp, ah, sctp, mh or "all"
tcp: “-m tcp”, :
[!] --source-port,--sport port[:port]: tcp ; 。
[!] --destination-port,--dport port[:port]: tcp ; 。
~]# iptables -A INPUT -s 0/0 -d 172.18.21.62 -p tcp --dport 22 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -d 0/0 -p tcp --sport 22 -j ACCEPT
: ssh 。
~]# iptables -A INPUT -s 0/0 -d 172.18.21.62 -p tcp --dport 80 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -d 0/0 -p tcp --sport 80 -j ACCEPT
: 80
[!] --tcp-flags mask comp: mask tcp , comp 1;
--tcp-flags syn,fin,ack,rst syn
--tcp-flags syn,fin,ack,rst ack,fin
[!] --syn:
--syn “--tcp-flags syn,fin,ack,rst syn”;tcp ;
udp: “-m udp”, :
[!] --source-port,--sport port[:port]: udp ; ;
[!] --destination-port,--dport port[:port]: udp ; ;
icmp: “-m icmp”, :
[!] --icmp-type {type[/code]|typename}
type/code:
0/0:echo reply:
8/0:echo request :
~]# iptables -A OUTPUT -s 172.18.21.62 -d 0/0 -p icmp --icmp-type 8 -j ACCEPT
~]# iptables -A INPUT -s 0/0 -d 172.18.21.62 -p icmp --icmp-type 0 -j ACCEPT
~]# iptables -A INPUT -d 172.18.21.62 -p icmp --icmp-type 8 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -p icmp --icmp-type 0 -j ACCEPT
:icmp type:http://baike.baidu.com/link?url=rph8HhXWXtekVwXLMz2FcUHaPru6DgG7-fDXFJOUt4qGann-4pwVaCTnF0JiGNiCzeiOg51fQzhWY8XEP_tyE_#7
(2): :
(1):multiport: : , 15 ;
[!] --source-ports,--sports port[,port|,port:port]...
[!] --destination-ports,--dports port[,port|,port:port]...
[!] --ports port[,port|,port:port]...
~]# iptables -I INPUT -s 0/0 -d 172.18.21.62 -p tcp -m multiport --dports 22,80 -j ACCEPT
~]# iptables -I OUTPUT -d 0/0 -s 172.18.21.62 -p tcp -m multiport --sports 22,80 -j ACCEPT
(2):iprange: ip ;
[!] --src-range from[-to]:
[!] --dst-range from[-to]:
~]# iptables -A INPUT -d 172.18.21.62 -p tcp --dport 23 -m iprange --src-range 172.18.21.1-172.18.21.100 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -p tcp --sport 23 -m iprange --dst-range 172.18.21.1-172.18.21.100
(3):string: ;
--algo {bm|kmp}:(bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)
[!] --string pattern: ;
[!] --hex-string pattern: ;
~]# iptables -I OUTPUT -s 172.18.100.6 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j REJECT
~]# iptables -I OUTPUT -s 172.18.21.62 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j REJECT
(4):time: / / 。
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]: 。
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]: 。
--timestart hh:mm[:ss]: 。
--timestop hh:mm[:ss]: 。
[!] --monthdays day[,day...]: 。
[!] --weekdays day[,day...]: 。
~]# iptables -R INPUT 4 -d 172.18.100.6 -p tcp --dport 23 -m iprange --src-range 172.18.100.1-172.18.100.100 -m time --timestart 09:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 -j ACCEPT
~]# iptables -I INPUT -d 172.18.21.62 -p tcp --dport 23 -m iprange --src-range 172.18.21.1-172.18.21.100 -m time --timestart 09:00:00 --timestop 18:00:00 -j ACCEPT
(5):connlimit: , ;
--connlimit-upto n: n ;
--connlimit-above n: n ;
~]# iptables -A INPUT -s 0/0 -d 172.18.100.6 -p tcp --dport 23 -m connlimit --connlimit-upto 2 -j ACCEPT
~]# iptables -A INPUT -s 0/0 -d 172.18.21.62 -p tcp --dport 23 -m connlimit --connlimit-upto 2 -j ACCEPT
~]# iptables -A INPUT -d 172.18.21.62 -p tcp --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT
(6):limit: ;
--limit rate[/second|/minute|/hour|/day]
--limit-burst number
~]# iptables -A INPUT -d 172.18.21.62 -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 3 -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -p icmp --icmp-type 0 -j ACCEPT
(7):state: conntrack ,
[!] --state state
INVALID: 。
ESTABLISHED: 。
NEW: 。
RELATED: 。
UNTRACKED: 。
1: :cat /proc/net/nf_conntrack
2: ( ): /proc/sys/net/nf_conntrack_max
~]# sysctl -w net.nf_conntrack_max=300000 #
net.nf_conntrack_max = 300000
~]# echo 200000 > /proc/sys/net/nf_conntrack_max #
~]# cat /proc/sys/net/nf_conntrack_max
200000
conntrack /proc/sys/net/nf_conntrack_max ; /proc/net/nf_conntrack , ; , ; :
(1) nf_conntrack_max ;
(2) nf_conntrack ;
:/proc/sys/net/netfilter/
1:
~]# iptables -A INPUT -s 172.18.0.0/16 -d 172.18.21.62 -p tcp -m multiport --dports 22,23,80 -m state --state NEW,ESTABLISHED -j ACCEPT
~]# iptables -A OUTPUT -d 172.18.0.0/16 -s 172.18.21.62 -p tcp -m multiport --sports 22,23,80 -m state --state ESTABLISHED -j ACCEPT
~]# iptables -A INPUT -d 172.18.21.62 -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED -j ACCEPT
~]# iptables -A OUTPUT -s 172.18.21.62 -p icmp --icmp-type 0 -m state --state ESTABLISHED -j ACCEPT
2:
~]# iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
~]# iptables -A INPUT -d 172.18.21.62 -p tcp -m multiport --dports 22,23,80 -m state --state NEW -j ACCEPT
~]# iptables -A INPUT -d 172.18.21.62 -p icmp --icmp-type 8 -m state --state NEW -j ACCEPT
~]# iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT
3:
~]# modinfo nf_conntrack_ftp
~]# modprobe nf_conntrack_ftp
~]# lsmod | grep ftp
nf_conntrack_ftp 11953 0
nf_conntrack 79206 7 nf_conntrack_ftp,xt_connlimit,iptable_nat,nf_nat,nf_conntrack_ipv4,nf_conntrack_ipv6,xt_state
~]# service vsftpd restart
~]# iptables -A INPUT -d 172.18.21.62 -p tcp --dport 21 -m state --state NEW -j ACCEPT
~]# iptables -I INPUT -d 172.18.21.62 -m state --state ESTABLISHED -j ACCEPT
...........................................................................................................................................................................
(4):
1: :
CentOS 6:
~]# service iptables save
~]# iptables-save > /etc/sysconfig/iptables
~]# iptables-save > /PATH/TO/SOME_RULE_FILE
CentOS 7:
~]# iptables -S > /PATH/TO/SOME_RULE_FILE
~]# iptables-save > /PATH/TO/SOME_RULE_FILE
2: :
:
~]# iptables-restore < /PATH/FROM/SOME_RULE_FILE
CentOS 6:service iptables restart
: /etc/sysconfig/iptables 。
:
(1):/etc/rc.d/rc.local
~]# vi /etc/rc.d/rc.local
iptables-restore < /PATH/FROM/SOME_RULE_FILE
(2):/usr/bin/iptables.sh
~]# vi /usr/bin/iptables.sh
iptables-restore < /PATH/FROM/SOME_RULE_FILE
...........................................................................................................................................................................
(4) :
-j targetname [per-target-options]
1:ACCEPT, DROP, REJECT
2:RETURN:
:
~]# iptables -N web
~]# iptables -A web -s 10.0.1.0/24 -p tcp --dport 80 -j ACCEPT
~]# iptables -I web 1 -m string --alog kmp --string "old" -j REJECT
~]# iptables -I web 2 -p tcp -m state --state ESTABLISHED -j ACCEPT
~]# iptables -A FORWARD -p tcp -j web
~]# iptables -A FORWARD -s 10.0.1.0/24 -p tcp --dport 22 -m state --state NEW -J ACCEPT
~]# iptables -A web -j RETURN
: ( )。
3:REDIRECT: , : PREROUTING OUTPUT 。
--to-ports port[-port]
:
~]# 8080 。
~]# iptables -t nat -A PREROUTING -d 172.18.21.72 -p tcp --dport 80 -j REDIRECT --to-ports 8080
:
http://172.18.21.72:8080
http://172.18.21.72
4:LOG:
--log-level level: ( 4):emerg, alert, crit, error, warning, notice, info or debug.
--log-prefix prefix:
:
~]# iptables -I FORWARD 2 -s 10.0.1.0/24 -p tcp -m multiport --dports 80,21,22,23 -m state --state NEW -j LOG --log-prefix "(new connctions)"
:
~]# cat /var/log/messages
5:MARK:
6:SNAT: : IP IP :POSTROUTING INPUT (CentOS 7 )
--to-source [ipaddr[-ipaddr]]
:
~]# iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -j SNAT --to-source 172.18.21.71
~]# iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -j SNAT --to-source 172.18.21.70-172.18.21.100
: ,
: , IP,
: IP, nat
7:DNAT: : IP IP :RPEROUTING OUTPUT (CentOS 7)
--to-destination [ipaddr[-ipaddr]][:port[-port]]
1:
~]# iptables -t nat -A PREROUTING -d 172.18.21.71 -p tcp --dport 80 -j DNAT --to-destination 10.0.1.2
~]# curl http://172.18.21.71/
2:
~]# 10.0.1.2 Listen 8090
~]# iptables -t nat -A PREROUTING -d 172.18.21.71 -p tcp --dport 80 -j DNAT --to-destination 10.0.1.2:8090
~]# curl http://172.18.21.71/
: ( ), 。
: , , 。
: , nat 。
8:MASQERADE: ,
:
~]# iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -j MASQUERADE
9:recent:
...........................................................................................................................................................................
:
:nmtui
:
CentOS6 iptables CentOS7 firewalld。
:
~]# iptables -F
:172.18.21.71
:
~]# cat /proc/sys/net/ipv4/ip_forward
:
~]# echo 1 > /proc/sys/net/ipv4/ip_forward
: 。
:
1:10.0.1.1 ping 172.18.21.71, ping 10.0.1.1
2:172.18.21.71 ping 172.18.21.72, ping 。
:10.0.1.1
~]# ping 172.18.21.72
:172.18.21.72: 。
~]# tcpdump -i eno16777736 icmp
:172.18.21.72 。
:
:172.18.21.72
~]# route add -net 10.0.1.0/24 gw 172.18.21.71
~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 100 0 0 eno16777736
10.0.1.0 172.18.21.71 255.255.255.0 UG 0 0 0 eno16777736
172.18.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eno16777736
: 10.0.1.2 ping 172.18.21.72 。
:172.18.21.71
~]# iptables -P FORWARD DROP
~]# tcpdump -i eno16777736 -nn icmp -vv
:
ping
~]# iptables -A FORWARD -s 10.0.1.0/24 -d 0/0 -p icmp --icmp-type 8 -j ACCEPT
~]# iptables -A FORWARD -s 0/0 -d 10.0.1.0/24 -p icmp --icmp-type 0 -j ACCEPT
ping
~]# iptables -A FORWARD -s 10.0.1.0/24 -d 0/0 -p icmp --icmp-type 0 -j ACCEPT
~]# iptables -A FORWARD -s 0/0 -d 10.0.1.0/24 -p icmp --icmp-type 8 -j ACCEPT
:
~]# iptables -A FORWARD -m state --state ESTABLISHED -j ACCEPT
ping
~]# iptables -A FORWARD -s 10.0.1.0/24 -p icmp --icmp-type 8 -m state --state NEW -j ACCEPT
ping
~]# iptables -A FORWARD -s 0/0 -d 10.0.1.0/24 -p icmp --icmp-type 8 -m state --state NEW -j ACCEPT
ping
~]# iptables -A FORWARD -p icmp -m state --state NEW -j ACCEPT
~]# lsmod | grep ftp
80 21
~]# iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
~]# iptables -A FORWARD -s 10.0.1.0/24 -p tcp --dport 80 -m state --state NEW -j ACCEPT
~]# iptables -A FORWARD -s 10.0.1.0/24 -p tcp --dport 21 -m state --state NEW -j ACCEPT
~]# modprobe nf_conntrack_ftp
:
~]# iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
~]# iptables -A FORWARD -s 10.0.1.0/24 -p tcp -m multiport --dports 80,21 -m state --state NEW -j ACCEPT