Linux iptables使用

1.《计算机网络》把网络分为以下几个层次,实际用的是TCP/IP四层协议,iptables是工作在运输层和网际层,属于网络防火墙配置。

iptables的是作用于内核层,对应ip_tables和Nefilter内核模块

2.iptables的规则主要由“四表五链”构成

  四表包括以下:其中nat表和filter表最常用,其他两个表少用

  filter表主要的功能:禁止/允许 某些IP或IP段访问、端口的访问、协议(TCP/ICMP/IGMP)的访问、限制包数与并发包数

  nat表主要功能:共享上网、端口转发/映射、IP映射(比较少用)

filter表:     过滤数据包(根据给定规则过滤)
nat表:        用于网络地址转换(IP地址与端口)
mangle表:     修改数据包的服务类型、TTL、配置路由、实现QOS
raw表:        决定数据包是否被状态跟踪机制处理

五链包括以下:原则选择是最靠近的链处理,不要等数据包流入下一个链再处理。

INPUT链:       进来的数据包
OUTPUT链:      外出的数据包
FORWARD链:     转发数据包
PREROUTING链:  路由选择前(所有入口数据包由这个链处理)
POSTROUTING链: 路由选择后(所有的出口数据包由这个链处理)

3.iptable的命令,输入iptables -h就有以下的帮助信息

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

 Commands:
    Either long or short options are allowed.
    --append -A chain Append to chain
    --check -C chain Check for the existence of a rule
    --delete -D chain Delete matching rule from chain
    --delete -D chain rulenum Delete rule rulenum (1 = first) from chain
    --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first)
    --replace -R chain rulenum Replace rule rulenum (1 = first) in chain
    --list -L [chain [rulenum]] List the rules in a chain or all chains
    --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains
    --flush -F [chain] Delete all rules in chain or all chains
    --zero -Z [chain [rulenum]] Zero counters in chain or all chains
    --new -N chain Create a new user-defined chain
    --delete-chain -X [chain] Delete a user-defined chain
    --policy -P chain target Change policy on chain to target
    --rename-chain -E old-chain new-chain Change chain name, (moving any references)

Options:
    --ipv4 -4 Nothing (line is ignored by ip6tables-restore)
    --ipv6 -6 Error (line is ignored by iptables-restore)
    [!] --protocol -p proto protocol: by number or name, eg. `tcp'
    [!] --source -s address[/mask][...] source specification
    [!] --destination -d address[/mask][...] destination specification
    [!] --in-interface -i input name[+] network interface name ([+] for wildcard)
    --jump -j target target for rule (may load target extension)
    --goto -g chain jump to chain with no return
    --match -m match extended match (may load extension)
    --numeric -n numeric output of addresses and ports
    [!] --out-interface -o output name[+] network interface name ([+] for wildcard)
    --table -t table table to manipulate (default: `filter')
    --verbose -v verbose mode
    --wait -w [seconds] wait for the xtables lock
    --line-numbers print line numbers when listing
    --exact -x expand numbers (display exact values)
    [!] --fragment -f match second or further fragments only
    --modprobe=<command> try to insert modules using this command
    --set-counters PKTS BYTES set the counter during insert/append
    [!] --version -V print package version.

 

 

 

 

4.iptables的常用示例命令

1. 允许/禁止某IP或IP段访问(filter)
iptables -t filter -P INPUT  DROP                                       #在filter表的INPUT链丢弃所有数据,白名单机制
iptables -P INPUT  DROP                                    #默认处理filter表
iptables -t nat -P PREROUTING ACCEPT           #在net表不是用来过滤的,一般不使用DROP、ACCEPT、REJECT。  
iptables -t filter -I INPUT -s 192.168.1.43 -j DROP #插入规则到filter表的INPUT链-丢弃源地址为192.168.1.43所有数据
iptables -I INPUT -s 192.168.1.43 -j DROP   #一般拒绝的规则使用-I、允许的规则使用-A。
iptables -I INPUT -s 192.168.1.0/24 -j DROP #插入规则到filter表的INPUT链-丢弃源地址为192.168.1.0网段所有数据
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT   #允许访问lo网卡127.0.0.1
iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT   #禁止访问lo网卡 127.0.0.1
iptables -I INPUT -d  192.168.1.43 -j DROP   #丢弃目的地址为192.168.1.43所有数据iptables -I INPUT -s 192.168.1.1  -d  192.168.1.43 -j DROP   #丢弃源地址为192.168.1.1且目的地址为192.168.1.43所有数据
2. 允许/禁止某端口访问(filter)
iptables -t filter -A INPUT -p tcp --dport 22  -j ACCEPT                                       #允许目的端口为22的TCP数据进入
iptables -t filter -I INPUT -p tcp --dport 8080  -j DROP                                       #丢弃目的8080端口的TCP数据
iptables -t filter -I INPUT -s 192.168.1.43 -p tcp --dport 8080  -j DROP   #丢弃源地址为192.168.1.43的目的端口8080的TCP数据
iptables -I INPUT -p tcp --dport 1024:8080 -j DROP  #丢弃1024-8080 端口的TCP数据
Iptables –I INPUT -p tcp  -m multiport ! –dport 22,80,443 -j DROP   #仅接受22,80,443端口的TCP数据
3.禁止/允许 某协议(TCP/ICMP/UDP)的访问(filter)
Iptables –t filter -A INPUT -p tcp –dport 22  -j ACCEPT                                       #允许目的端口为22的TCP数据进入
Iptables –t filter -I INPUT -p icmp --icmp-type 8   -j DROP                                   #丢弃所有icmp type数据包(也就是ping包)
iptables -t filter -I INPUT -p icmp -j DROP                                                   #丢弃所有icmp数据包
iptables -t filter -I INPUT -p tcp -j REJECT #拒绝所有TCP包
iptables -t filter -I INPUT -p udp -j REJECT #拒绝所有UDP包
-p tcp --tcp-flags LIST1 LIST2
 #匹配指定的TCP标记,有两个参数列表,列表内部用逗号为分隔符,两个列表之间用空格分开, LIST1用作参数检查,LIST2用作参数匹配。可用标志有SYN(同步; 表示开始会话请求), ACK(应答), FIN(结束; 结束会话),RST(复位;中断一个连接),PSH(推送; 数据包立即发送),URG(紧急),ALL(指选定所有的标记),NONE(指未选定任何标记)
iptables -A INPUT -p tcp --dport 23 --tcp-flags ALL SYN -j REJECT  #拒绝第一次握手
(更多--tcp-flags 参考:https://blog.csdn.net/u014721096/article/details/78626729)
4.匹配TCP/IP连接状态(filter)
-m state –state
NEW:已经或将启动新连接
ESTABLISHED:已经建立的连接
RELATED:正在启动的新链接,等待
INVALID:非法或无法识别的
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    #允许进入的包建立连接,等待启动新链接
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许出去的包建立连接,等待启动新链接
5. 限制数据包数量(filter)
-m limit 限制模块
-m limit --limit n/(second/minute/hour) #每(秒/分钟/小时)只有n个数据包被允许。(速度)
-m limit --limit-burst n #在单位时间内最多n个包。(总量)
-m limit --limit 10/minute --limit-burst 5 #每分钟只允许10个包,每秒钟6个包,最多允许5个包同时并发。前面5个数据包很快发出,从第六个数据包开始每6秒允许一个数据包
Iptables -P INPUT DROP #默认是丢弃所有包
iptables -I INPUT -p icmp -m limit --limit 10/minute --limit-burst 5 -j ACCEPT  #每分钟只允许10个包,每秒钟6个包
6.共享上网(nat)

开启内核转发:
echo "1" > /proc/sys/net/ipv4/ip_forward
或者在/etc/sysctl.conf文件增加 net.ipv4.ip_forward = 1
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE  
#公网不固定,自动更换源地址为获取的公网IP
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT  --to-source 191.4.4.1    
#公网固定,自动更换源地址为固定公网IP
7.端口映射
主机A(192.4.6.1)<->家庭路由器独立公网ip(10.9.2.1,192.168.1.1)<->主机C(个人主机192.168.1.100)
    主机A(192.4.6.1:22)->S:191.4.6.1 D:10.9.2.1:9001 -> 主机C 192.168.1.100:22

家庭路由器独立公网配置
iptables -t nat -A PREROUTING -d 10.9.2.1 -p tcp --dport 9001 -j DNAT --to-destination 192.168.1.100:22  #主机A访问10.9.2.1:9001相当于访问主机C的22端口
8.其他命令
iptables -F #清除所有规则 iptables -t filter -F #清除filter表所有规则 iptables -t filter -F INPUT #在filter表的INPUT链丢弃所有数据,白名单机制 iptables -Z #把 chain 或者所有 chain(当未指定 chain 名称时)的包及字节的计数器清空 iptables -X #删除用户自定义 chain 或者所有用户自定义 chain(当未指定 chain 名称时)。该指令不影响预设规则 (如 INPUT、OUTPUT、FORWARD 等)。 iptables -n -L --line-numbers #列出所有规则,并显示序号 iptables -P chain target #改变 chain 的策略为 target iptables -D chain firewall-rule #表示从 chain 中删除对应规则 firewall-rule 的那一条目。这种规则比较麻烦 iptables -D chain rulenum #删除 chain 中编号为 rulenum 的那条规则。1 表示第一条 iptables-save > iptables.bak #保存所有规则。 iptables-save -t filter > iptables.bak #指定要保存的表的名称。 iptables-restore < iptables.bak #还原iptables配置 iptables-restore -c < iptables.bak #还原iptables表时候,还原当前的数据包计数器和字节计数器的值 iptables-restore -t filter < iptables.bak #还原iptables表时候,指定要还原的表的名称。

 

posted @ 2023-03-06 23:38  jest549  阅读(240)  评论(0编辑  收藏  举报