對於連接到網絡上的 Linux 系統來說,防火牆是必不可少的防禦機制, 它只允許合法的網絡流量進出系統,而禁止其它任何網絡流量。 netfilter/iptables IP 信息包過濾系統是一種功能強大的工具, 可用於添加、編輯和除去規則,這些規則是在做信息包過濾決定時,防火牆所遵循和組成的規則。 這些規則存儲在專用的信息包過濾表中, 而這些表集成在 Linux 內核中。在信息包過濾表中,規則被分組放在我們所謂的 chain)中。 雖然 netfilter/iptables IP 信息包過濾系統被稱為單個實體,但它實際上由兩個組件 netfilteriptables 組成。 netfilter 組件也稱為 內核空間(kernelspace),是內核的一部分,由一些信息包過濾表組成, 這些表包含內核用來控制信息包過濾處理的規則集。 iptables 組件是一種工具,也稱為 用戶空間(userspace),它使插入、修改和除去信息包過濾表中的規則變得容易。 通過使用用戶空間,可以構建自己的定制規則,這些規則存儲在內核空間的信息包過濾表中。這些規則具有 目標,它們告訴內核對來自某些源、前往某些目的地或具有某些協議類型的信息包做些什麼。如果某個信息包與規則匹配,那麼使用目標 ACCEPT 允許該信息包通過。還可以使用目標 DROPREJECT 來阻塞並殺死信息包。對於可對信息包執行的其它操作,還有許多其它目標。


shell> iptables

Circumvent MTU issues

shell> iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

/etc/ppp/ip-up.d/0clampmss

#!/bin/sh
# Enable MSS clamping (autogenerated by pppoeconf)

iptables -t mangle -o "$PPP_IFACE" --insert FORWARD 1 -p tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1400:65495 -j TCPMSS --clamp-mss-to-pmtu

:books: 參考網站:


multiport

iptables -A INPUT -i 10.10.10.200 -m state --state NEW -m multiport -p tcp -s 10.10.10.0/24 -d 10.10.10.0/24 --dports 50006,50008,50009 -j ACCEPT

:books: 參考網站:

Masquerade everything out ppp0.

shell> iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
shell> iptables -L
shell> iptables -L -v
shell> iptables -nvL --line-numbers

shell> iptables -nvL INPUT --line-numbers
shell> iptables -F
shell> iptables -Z
shell> iptables-save
shell> iptables-restore
shell> iptables -P INPUT DROP
shell> iptables -P FORWARD DROP
shell> iptables -P OUTPUT ACCEPT
shell> iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
shell> iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 69 -j ACCEPT

iprange

shell> iptables -A INPUT -m iprange --src-range 10.50.10.20-10.50.10.80 -j ACCEPT

:books: 參考網站:


shell> iptables -A INPUT -p tcp --dport ssh -j ACCEPT
shell> iptables -A INPUT -p tcp --dport 80 -j DROP
INPUT|OUTPUT|FORWARD
ACCEPT|DROP|REJECT 
INVALID|ESTABLISHED|NEW|RELATED|SNAT|DNAT
PREROUTING|POSTROUTING
mangle|nat
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

iptables -F -t nat
iptables -F -t mangle
iptables -F

iptables -X -t nat 
iptables -X -t mangle 
iptables -X

iptables -Z

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT

#iptables -A INPUT -p icmp -j ACCEPT 
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 11 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/second -j ACCEPT

iptables -A INPUT --protocol tcp --tcp-flags ALL SYN,ACK -j DROP

iptables -A INPUT -m conntrack --ctstate NEW -p udp --dport 5353 -j ACCEPT

iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 1723 -j ACCEPT

iptables -A INPUT -p udp -m conntrack --ctstate NEW -m udp --dport 5353 -j ACCEPT

iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited
#! /bin/bash

# Private interface
IF_PRV=eth0
IP_PRV=192.168.1.1
NET_PRV=192.168.1.0/24

# Public interface
IF_PUB=eth1
IP_PUB=10.0.0.1
NET_PUB=10.0.0.0/24

# Others
ANYWHERE=0.0.0.0/0

SetDefaultPolicy() {
    # Drop everything
    iptables -P INPUT DROP
    iptables -P OUTPUT DROP
    iptables -P FORWARD DROP
}

FlushTables() {
    iptables -F -t nat
    iptables -F -t mangle
    iptables -F -t filter
    iptables -X
}

SetForwardingRules() {
    iptables -A FORWARD -i $IF_PUB -o $IF_PRV -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -A FORWARD -i $IF_PRV -o $IF_PUB -j ACCEPT
}

SetLoopbackRules() {
    # Allow everything
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A OUTPUT -o lo -j ACCEPT
}

SetPrivateInterfaceRules() {
    # Allow everything
    iptables -A INPUT -i $IF_PRV -s $NET_PRV -j ACCEPT
    iptables -A OUTPUT -o $IF_PRV -d $NET_PRV -j ACCEPT
}

SetPublicInterfaceRules() {
    iptables -A INPUT -i $IF_PUB -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -A OUTPUT -o $IF_PUB -j ACCEPT
}

EnableSourceNAT() {
    # Then source NAT everything else
    iptables -t nat -A POSTROUTING -s $NET_PRV -o $IF_PUB -j SNAT --to $IP_PUB
}

SetICMP_Open() {
    iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
    iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
    iptables -A INPUT -p icmp --icmp-type 11 -j ACCEPT
    iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/second -j ACCEPT
}

SetSSH_Open() {
    iptables -A INPUT -i $IF_PUB -p tcp -d $IP_PUB --dport 2202 -j ACCEPT
}

SetSMTP_DNAT() {
    iptables -t nat -A PREROUTING -i $IF_PUB -d $IP_PUB -p tcp --dport smtp -j DNAT --to 192.168.1.254
    iptables -A FORWARD -m state --state NEW,ESTABLISHED,RELATED -i $IF_PUB -p tcp --dport smtp -j ACCEPT
}

SetPOP3_DNAT() {
    iptables -t nat -A PREROUTING -i $IF_PUB -d $IP_PUB -p tcp --dport pop3 -j DNAT --to 192.168.10.254
    iptables -A FORWARD -m state --state NEW,ESTABLISHED,RELATED -i $IF_PUB -p tcp --dport pop3 -j ACCEPT
}

SetHTTP_DNAT() {
    iptables -t nat -A PREROUTING -i $IF_PUB -d $IP_PUB -p tcp --dport http -j DNAT --to 192.168.10.253
    iptables -A FORWARD -m state --state NEW,ESTABLISHED,RELATED -i $IF_PUB -p tcp --dport http -j ACCEPT
}

SetBlockedHosts() {
    iptables -A INPUT -i $IF_PUB -s 10.220.231.236 -j REJECT --reject-with icmp-host-prohibited
    iptables -A FORWARD -i $IF_PUB -s 10.220.231.236 -j REJECT --reject-with icmp-host-prohibited
}

SetBlockedNetworks() {
    iptables -A INPUT -i $IF_PUB -s 10.220.232.0/24 -j REJECT --reject-with icmp-net-prohibited
    iptables -A FORWARD -i $IF_PUB -d $IP_PUB -s 10.220.232.0/24 -j REJECT --reject-with icmp-net-prohibited
}

:books: 參考網站:

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

iptables -F -t nat
iptables -F -t mangle
iptables -F

iptables -X -t nat 
iptables -X -t mangle 
iptables -X

iptables -Z

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i ppp+ -j ACCEPT

#iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 11 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/second -j ACCEPT


iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 1723 -j ACCEPT

iptables -A FORWARD -i eth0 -j ACCEPT
iptables -A FORWARD -o eth0 -j ACCEPT

iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited
shell> modprobe nf_conntrack_ftp
shell> modprobe nf_conntrack_pptp
shell> modprobe nf_nat_pptp

/etc/modules

nf_conntrack_ftp
nf_conntrack_pptp
nf_nat_pptp
shell> ip6tables -L
shell> ip6tables -L -v
shell> ip6tables -nvL --line-numbers

shell> ip6tables -nvL INPUT --line-numbers

shell> ip6tables -A INPUT -i eth0 -p tcp -s 3ffe:ffff:100::1/128 --dport 22 -j ACCEPT

netstat-nat

:books: 參考網站:


shell> sudo apt-get install netfilter-persistent
shell> service netfilter-persistent save

shell> sudo apt-get install ipset
shell> ipset help
shell> ipset list
Name: foo
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 448
References: 0
Members:
192.168.0.0/24
shell> ipset create test hash:net family inet hashsize 131072 maxelem 236294
shell> ipset create test hash:net
shell> ipset add test 192.168.0.0/24
shell> ipset add test 10.1.0.0/16
shell> ipset add test 192.168.0/24

shell> ipset test test 192.168.0/24

shell> iptables -A INPUT -m set --match-set foo src -j DROP
shell> iptables -A INPUT -m set --match-set foo src -p tcp --dport 80 -j DROP
shell> ipset create test hash:ip timeout 300
shell> ipset add test 192.168.0.1 timeout 60
shell> ipset -exist add test 192.168.0.1 timeout 600
shell> ipset create foo hash:mac
shell> ipset add foo 01:02:03:04:05:06
shell> ipset test foo 01:02:03:04:05:06
shell> ipset create foo hash:ip,port
shell> ipset add foo 192.168.1.0/24,80-82
shell> ipset add foo 192.168.1.1,udp:53
shell> ipset add foo 192.168.1.1,vrrp:0
shell> ipset test foo 192.168.1.1,80
shell> sudo ipset save foo -f /etc/ipset.conf 
shell> sudo ipset save -f /etc/ipset.conf

shell> sudo ipset restore -f /etc/ipset.conf
shell> ipset flush foo 
shell> ipset flush
shell> ipset destroy foo 
shell> ipset destroy
ipset v6.29: Timeout cannot be used: set was created without timeout support
ipset v6.29: Set cannot be created: set with the same name already exists
ipset v6.20.1: Set cannot be destroyed: it is in use by a kernel component
shell> apt install python-pip
shell> pip install --upgrade pip
shell> pip install iblocklist2ipset

shell> iblocklist2ipset generate --ipset=foo "http://list.iblocklist.com/?list=ydxerpxkpcfqjaybcssw&fileformat=p2p&archiveformat=" > foo.txt
shell> sudo ipset restore -f foo.txt
http://list.iblocklist.com/?list=ydxerpxkpcfqjaybcssw&fileformat=p2p&archiveformat=gz

:books: 參考網站:


mturoute

shell> mturoute host

:books: 參考網站:


shell> apt install iputils-tracepath
shell> tracepath host
shell> iptables-save > /opt/bitnami/iptables-rules
shell> crontab -e
@reboot /sbin/iptables-restore < /opt/bitnami/iptables-rules

TSO (TCP Segmentation Offload) 是一种利用网卡分割大数据包,减小 CPU 负荷的一种技术,也被叫做 LSO (Large segment offload) ,如果数据包的类型只能是 TCP,则被称之为 TSO,如果硬件支持 TSO 功能的话,也需要同时支持硬件的 TCP 校验计算和分散 - 聚集 (Scatter Gather) 功能。

GSO (Generic Segmentation Offload)

TSO 是使得网络协议栈能够将大块 buffer 推送至网卡,然后网卡执行分片工作,这样减轻了 CPU 的负荷,但 TSO 需要硬件来实现分片功能;而性能上的提高,主要是因为延缓分片而减轻了 CPU 的负载,因此,可以考虑将 TSO 技术一般化,因为其本质实际是延缓分片,这种技术,在 Linux 中被叫做 GSO(Generic Segmentation Offload),它比 TSO 更通用,原因在于它不需要硬件的支持分片就可使用,对于支持 TSO 功能的硬件,则先经过 GSO 功能,然后使用网卡的硬件分片能力执行分片;而对于不支持 TSO 功能的网卡,将分片的执行,放在了将数据推送的网卡的前一刻,也就是在调用驱动的 xmit 函数前。

:books: 參考網站:


:books: 參考網站:


conntrack

shell> apt install conntrack
shell> conntrack -L
shell> conntrack -L -o extended
shell> conntrack -L --src-nat

:books: 參考網站:

- http://manpages.ubuntu.com/manpages/trusty/man8/conntrack.8.html

iptables -N DOS_PROTECT
iptables -A INPUT -j DOS_PROTECT
iptables -I DOS_PROTECT -i bond0 -p tcp --syn -j DROP
iptables -I DOS_PROTECT -i bond0 -p tcp --syn -m limit --limit 10000/s --limit-burst 100 -j RETURN
iptables -I DOS_PROTECT -i bond0 -p tcp --tcp-flags SYN,ACK,FIN,RST RST -j DROP
iptables -I DOS_PROTECT -i bond0 -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j RETURN
iptables -I DOS_PROTECT -i bond0 -p icmp --icmp-type echo-request -j DROP
iptables -I DOS_PROTECT -i bond0 -p icmp --icmp-type echo-request -m limit --limit 1/s -j RETURN


iptables -t nat -A PREROUTING -m iprange --src-range 192.168.1.10-192.168.1.150 -p tcp --dport 80 -j REDIRECT --to-port 3128

:books: 參考網站:

powered by Gitbook最後更新: 2017-11-21 03:07:22

results matching ""

    No results matching ""