1. iptables的使用

# 1. 安装iptables
yum install iptables*

# 2. 启动iptables
systemctl start iptables

iptables -i -v -L

# 3. 关闭firewalld
systemctl disable --now firewalld

systemctl status ...

"格式:"iptables -t 表名 选项 链名 条件 动作
# 链下面有规则
	-t  指定操作的表
	-v  显示数据包和数据包的大小
	-n  不反解地址

	-A,--append    追加一条规则到链中
	-I,--insert    插入一条规则,插入到顶部
	-F,--flush     清空规则
	-Z,--zero      清空计数器(包数量,包大小)
	
	-L,--list      列出当前规则
	-D,--delete    删除链中的规则
	-R,--replace   修改
	-N,--new--chain创建一个自定义 链
	-X,--delete-chain删除一个自定义 链
	-S,--list-rules列出所有的规则
	-P,--policy    指定链的默认策略

2. iptables动作

"ACCEPT"
将数据包放行,进行完此动作后,将不再对比其他规则,之间跳往下一个规则链。

"REJECT"
拦阻该数据包,并传送数据包通知对方。

"DROP"
丢弃包不予处理,进行完此处理动作后,将不再对比其他规则,直接中断过滤程序。

"REDIRECT"
将包重新导向到另一个端口,进行完处理动作后,将会继续对比其他规则。

3. iptables基本的匹配条件

# 协议
	TCP(http)
	UDP
	ICMP(ping)
	ALL

"知识储备:"
    查看本机端口占用的命令:netstat -nutlp

# 地址
	-s源地址:   发送请求的地址
	-d目标地址: 访问的地址

# 端口
	--sport源端口:   发送请求的端口
	--dport目标端口: 访问的端口

# 动作
	-i    进来的网卡
	-o    出去的网卡
	-m    指定模块
	-j    转发模式
	-p    关联协议

案例

"案例1:"只允许22号端口可以访问,其他端口全部无法访问
# 用于过滤,所以是filter表
# 开放22端口
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
# 拒绝其他端口
iptables -t filter -A INPUT -p TCP -j DROP


"案例2:"只允许22,80,443端口可以访问,其他端口不可以访问
# 先清空原规则
iptables -F
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80 -j ACCETP
iptables -t filter -A INPUT -p TCP --dport 443 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP


"案例3:"只允许22,80,443端口可以访问,其他端口全部无法访问,但是本机可以访问百度



"案例4:"要求使用192.168.15.81能够通过22端口访问链接,但是其他的不行
iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP


"案例5:"只允许192.168.15.71能够通过22端口连接,其他的不行
iptables -F
iptables -t filter -A INPUT -p TCP -s 192.168.15.71 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP


"案例6:"要求192.168.15.71对外不可见
iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DRPO


"案例7:"要求使用eth0网卡的所有请求全部拒绝
iptables -F
iptables -t filter -A INPUT -p TCP -i eth0 -j DROP

# 使用172.16.1.71登录进来的窗口,不允许访问百度
iptables -t filter -A INPUT -p TCP -o eth1 -j DROP


"案例8:"要求访问服务器的8080端口,转发至80端口
iptables -F
iptables -t nat FORWARD -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80


"案例9:"要求只允许Windows通过ssh连接192.168.15.81,其他的拒绝
iptables -F
iptables -t filter -A INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 22 -j DROP

4. 模块

拓展iptables的功能

-m   指定模块

# 1. multiport模块(连续匹配多个端口)
    --dports   指定多个端口
    (不同端口之间以逗号,分割,连续的端口之间以冒号:分割)

# 2. iprange模块(指定一段连续的ip地址范围)
    --src-range from[-to]  源地址范围
    --dst-range from[-to]  目标地址范围

# 3. string模块
    --string pattern  指定要匹配的字符串
    --algo {bm|kmp}   匹配的查询算法

# 4. time模块(根据时间段匹配报文)
"UTC时间"
    --timestart hh:mm[:ss]  开始时间
    --timestop  hh:mm[:ss]  结束时间
    --monthdays day[,day..] 指定一个月的某一天
	--weekdays  day[,day..] 指定一周的某一天

# 5. icmp模块(ping)
🈲ping,默认本机无法ping别人,别人无法ping自己
    --icmp-type {type[/code]|typename}
        echo-request (8)  请求
        echo-reply   (0)  回应

# 6. connlimit(限制连接数,并发连接数)
    --connlimit-upto   n  如果现有连接数小于或等于 n 则匹配
    --connlimit-above  n  如果现有连接数大于 n 则匹配

# 7. limit(针对报文速率进行限制,秒,分,时,日)
    --limit rate[/second|/minute|/hour|/day]  报文速率
    --limit-burst number   报文数量(默认5)

案例

1. 要求将22,80,443以及30000-50000之间的所有端口向外暴露,其他端口拒绝
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DRPO


2. 要求访问呢数据包中,包含helloworld的数据不允许通过
iptables -t filter -A INPUT -p TCP -m string --string "helloworld" --algo kmp -j DRPO


3. 要求192.168.15.1 - 192.168.15.10之间的所有ip能够连接192.168.15.81,其他拒绝
iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DRPO


4. 要求每天的12点到13点,不允许访问
# 必须是UTC时间
iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DRPO


5. 要求别人不能ping本机,本机可以ping别人
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DRPO


6. 要求主机连接最多有2个
iptables -t filter -A INPUT -p TCP -m connlimit --connlimit-above 2 -j DROP


7. 要求限制速率在500k/s左右
# 关于速率限制,非常非常非常不准确
iptables -t filter -A INPUT -p TCP -m limit --limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP