Linux防火墙

一、什么是防火墙?

即防止其他人恶意访问的工具。

image

二、防火墙种类

一般有三种,硬件防火墙、软件防火墙、安全组。

image

三、iptables防火墙

image

iptables四表五链

# 四表
filter表:过滤数据包

nat表:网络地址转换

mangle表:负责修改数据包内容

raw表:负责数据包跟踪


# 五链
PREROUTING链: 主机外报文进入位置,允许的表mangle, nat(目标地址转换,把本机地址转换为真正的目标机地址,通常指响应报文)

INPUT链:报文进入本机用户空间位置,允许的表filter, mangle

OUTPUT:报文从本机用户空间出去的位置,允许filter, mangle, nat

FOWARD:报文经过路由并且本机不是目标机,转发给目标机,允许filter, mangle

POSTROUTING:报文经过路由被转发出去,允许mangle,nat(源地址转换,把原始地址转换为转发主机出口网卡地址)

image

iptables流程

iptables流程图表现于三种网络模型。

1.流入本机

image

2.流出本机

image

3.经过本机

image

iptables使用

命令语法 : iptables -t [表名] [选项] [链名称] [条件] [动作]

选项:

-t:指定操作的表

image

-L:列出当前的规则

image

-v:显示数据包和数据包大小

image

-n:不反解地址

image

-A:追加规则到链尾

image

-I:插入规则到链头

image

-F:清空规则

image

-Z:清空计数器(包数量、包大小)

image

-D:删除链中规则

image

image

-R:修改规则

image

image

-S:列出所有规则

image

-p:协议

TCP (http)
UDP
ICMP (ping)
ALL

-s:(源地址) -d:(目标地址)

# 屏蔽ip 192.168.15.71
iptables -t filter -A INPUT -s 192.168.15.71 -j DROP

# 只能使用192.168.15.71端口号22连接本机
iptables -t filter -A INPUT -p TCP -d 192.168.15.71 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

# 只允许 192.168.15.71通过22端口号连接 192.168.15.81
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

-j: 动作

ACCEPT  数据包通过,完成此处理动作后不再比对其他规则,直接跳往下一个规则链。
REJECT  阻拦数据包,并传送数据包通知对方。
DROP  丢弃数据包,完成此处理动作后不再比对其他规则,直接终端过滤程序。
REDIRECT  将数据包转发到另一个端口,完成此处理动作后继续比对其他规则。

-i:数据包进入本机的网口

# 拒绝使用eth0网口的所有请求
iptables -t filter -A INPUT -p TCP -i etho -j DROP

-o:数据包离开本机使用的网口

# 使用eth1网口连接本机的窗口无法发送数据
iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP

-m:指定模块

-N, --new-chain:创建一个自定义链

-X, --delete-chain:删除一个自定义链

-P, --policy:指定链的默认策略

模块

iptables使用-m指定模块能够拓展iptables功能。
image

multiport(连续匹配多个端口)

--dports : 指定多个端口(不同端口之间以( , )逗号分割,连续的端口使用( : )冒号分割)

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

iprange(指定一段连续的ip地址)

--src-range from[-to]: 源地址范围
--dst-range from[-to]: 目标地址范围

# 要求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 DROP

string(匹配指定字符串)

--string pattern : 指定要匹配的字符串
--algo {bm|kmp} : 匹配的查询算法

# 要求访问数据包中包含hello的数据不允许通过
iptables -t filter -A INPUT -p tcp -m string "hello" --algo kmp -j DROP

time(根据时间段匹配报文)

--timestart hh:mm[:ss] : 开始时间 UTC时间
--timestop hh:mm[:ss] : 结束时间 UTC时间
--monthdays day[,day...] : 指定一个月的某一天
--weekdays day[,day...]: 指定周 还是 周天

# 要求每天的12到13之间,不允许访问
# 时间需要使用UTC时间,东八区-8
iptables -t filter -A INPUT -p tcp -m time --timestart 4:00 --timestop 5:00 -j DROP

icmp(ping设置)

--icmp-type
  • echo-request (8) 请求
  • echo-reply (0) 回应
# 要求别人ping本机不通,本机ping别人通
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP

connlimit

--connlimit-upto n : 如果现有连接数小于或等于 n 则 匹配
--connlimit-above n : 如果现有连接数大于n 则匹配

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

limit

--limit rate[/second|/minute|/hour|/day] # 报文数量
--limit-burst number # 报文数量(默认:5)

# 要求限制速率在500k/s左右(不是精准的)
iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

案例

1、要求访问服务器的8080端口转发至80端口

iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 80

2、只允许22,80,443端口可以访问,其他端口全部无法访问

iptables -t filter -A INPUT -p TCP --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 443  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
posted @ 2021-12-24 20:31  它叫鸮  阅读(454)  评论(0编辑  收藏  举报