努力挣扎的生活 努力挣扎的生活
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)

yangfk

瑟瑟发抖的小运维
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)
  • 运维基础

    • nginx 安装
    • nginx-conf常用示例
    • rsync 服务pull与push
    • linux时间同步
    • 系统参数优化
    • ruby版本升级
    • fpm定制rpm
    • php7_X安装
    • iptables规则
      • 常用参数
      • iptables基础防护命令
      • iptables常用的几个规则
      • 扩展
        • 配置网关路由
        • 网关服务器gw01
        • app服务器app02
        • 端口映射
    • tcpdump抓包
    • 公司部署Pritunl-VPN
    • 需求shell脚本记录
    • openldap安装配置
    • Ldap集成常用开源服务示例图
    • ansible基础
    • ansible进阶playbook与Roles
    • centos安装python3
    • Firewalld防火墙
    • Linux配置jdk
    • ubuntu安装ftp
    • minio分布式文件存储
    • windows安装openssh
    • centos7安装系统检测不到网卡
    • docker运行一个bind9和常用的网络命令
    • nginx禁用真实IP
  • 监控

  • 日志系统

  • 安全记录

//
  • 运维
  • 运维基础
yangfk
2019-09-08

iptables规则

//

从物理上讲,防火墙可以分为硬件防火墙和软件防火墙。

硬件防火墙:在硬件级别实现部分防火墙功能,另一部分功能基于软件实现,性能高,成本高。

软件防火墙:应用软件处理逻辑运行于通用硬件平台之上的防火墙,性能低,成本低。

  • 安装iptables,启动
yum install -y iptables-services

systemctl enable iptables.service

systemctl start iptables.service
1
2
3
4
5

iptables-logo.png

iptables 用户空间,netfilter 内核空间 ,5表tables ,5链 chains,规则policy。5表:filter(主机防火墙,),nat(IP路由,端口映射),mangle,raw,security;5链:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING

# 常用参数

参数 说明 示例
-A 追加规则 iptables -A INPUT
-D 删除规则 iptables -D INPUT 1
-F 清空规则链 iptables -F
-I 插入规则到第一行 iptables -I INPUT 1 --dport 80 -j ACCEPT
-L 查看规则链 iptables -L
-L 查看规则 iptables -L INPUT
-N 新的规则 iptables -N allowed
-R 修改规则 iptable -R INPUT 1 -s 192.168.1.0 -j DROP
-V 查看iptables版本 iptables -V
-d 匹配目的地址 iptables -A INPUT -d 192.168.1.1
-i 匹配入口网卡流入的数据 iptables -A INPUT -i eth0
-j 要进行的处理动作:DROP(丢弃),REJECT(拒绝),ACCEPT(接受),SANT(基于原地址的转换) iptable -A INPUT 1 -s 192.168.1.0 -j DROP
-m 使用扩展模块来进行数据包的匹配(multiport/tcp/state/addrtype) iptables -m multiport
-o 匹配出口网卡流出的数据 iptables -A FORWARD -o eth0
-p 协议(tcp/udp/icmp) iptables -A INPUT -p tcp
-s 匹配原地址,加" ! "表示除这个IP外 iptables -A INPUT -s 192.168.1.1
-t 表名(raw、mangle、nat、filter) iptables -t nat
--dport 匹配目的端口流出的数据 iptables -A INPUT -p tcp --dport 22
--sport 匹配源端口流入的数据 iptables -A INPUT -p tcp --sport 22
--to-source 指定SANT转换后的地址 iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SANT --to-source 172.16.1.1
--line-numbers 查看表链数,通过 -D 参数删除 iptables -nL --line-numbers

处理规则

规则匹配从上往下,匹配到则返回,先放白名单,再加拒绝规则

ACCEPT:允许数据包通过。

DROP:直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求泥牛入海了,过了超时时间才会有反应。推荐

REJECT:拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息。

SNAT:源地址转换,解决内网用户用同一个公网地址上网的问题。

MASQUERADE:是SNAT的一种特殊形式,适用于动态的、临时会变的ip上。

DNAT:目标地址转换。

REDIRECT:在本机做端口映射。

加载模块到linux内核

modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state

lsmod |grep -E "nat|filter|ipt"
1
2
3
4
5
6
7
8
9

# iptables基础防护命令

#清理防火墙规则,并不会改变默认规则
iptables-save  > ./iptables_$(date +%F-%H_%M)   #备份
iptables -F
iptables -X
iptables -Z

#放开ssh端口,白名单网段
iptables -A INPUT -s 192.168.108.0/24 -j ACCEPT
iptables -A INPUT -s 183.10.10.234/32 --dport 22 -j ACCEPT 

#iptables -A INPUT -p tcp --dport 22 -j ACCEPT  #放开所有网段22端口


#设置默认规则,注意是否开启白名单或者ssh端口
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

#设置其他规则
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p icmp -m imcp --icmp-type any -j ACCEPT


#临时
systemctl reload iptables.service

#永久生效
/usr/sbin/iptables-save > /etc/sysconfig/iptables

#iptables 开机自启动
chkconfig iptables on


#内核优化参数
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# iptables常用的几个规则

拒绝连接3306端口

iptables -A INPUT -p tcp --dport 3306 -j DROP

插入到第一条拒绝网段:`iptables -I INPUT -p tcp -s 192.168.108.105 -i eth0 -j DROP

往后添加:iptables -A INPUT -p tcp -s 192.168.108.0/24 -i eth0 -j DROP

允许SSH的连接请求

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A INPUT -i eth0 -p tcp -s 192.168.100.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

使用multiport 将多个规则结合在一起

iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT

允许关联的协议包(ftp协议)

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

限制指定时间的允许通过数量

-m limit --limit n/{second/minute/hour}

iptables -I INPUT -p icmp --icmp-type 8 -m limit --limit 6/min -j DROP

ICMP 类型

iptables -A INPUT -p icmp --icmp-type 8 -j DROP #拒绝所有

接受某个网段,icmp-type 类型有很多,可以使用any

iptables -I INPUT -p icmp -s 192.168.108.0/24 --icmp-type 8 -j ACCEPT

内核参数:net.ipv4.icmp_echo_ignore_all=0

允许出站的DNS连接

iptables -A INPUT -p udp -i eth0 --sport 53 -j ACCEPT

允许内部网络域外部网络的通信

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

查看INPUT 有哪些编号

iptables --line -nvL FORWARD ,iptables -nvL --line-numbers

删除规则

iptables -D INPUT pkts编号

#匹配规则从上往下,可以通过保存文件,修改再次导入修改匹配规则顺序

规则保存

iptables-save > iptables-file

规则导入

iptables-restore < iptables-file

清除表规则,"慎用"

iptables-save  > ./iptables_$(date +%F-%H_%M)   #备份
iptables -F
iptables -X
iptables -Z
1
2
3
4

# 扩展

# 配置网关路由

# 网关服务器gw01

  • 查看网卡配置
[root@gw01 network-scripts]# cat ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.108.100
PREFIX=24
GATEWAY=192.168.108.2
DNS1=114.114.114.114

[root@gw01 network-scripts]# cat ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=172.16.0.100
PREFIX=24

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 配置防火墙规则
iptables -F
iptables -X
iptables -Z

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT

#----添加SNAT 
#方式一:SNAT
iptables -t nat -A POSTROUTING -o eth0 -s 172.16.0.0/24 -j SNAT  --to-source 192.168.108.100

#方式二:伪装,适合IP经常变换
iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -j MASQUERADE 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# app服务器app02

[root@app02 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=172.16.0.102
PREFIX=24
DEFROUTE=yes

#route add default gw 172.16.0.100
1
2
3
4
5
6
7
8
9
10
11

# 端口映射

访问 52223 转发到 172.16.0.102端口22

iptables -A INPUT  -p tcp --dport 52223 -j ACCEPT
iptables -t nat -A PREROUTING -d 192.168.108.100 -p tcp --dport 52223 -j DNAT --to-destination 172.16.0.102:22
1
2
//
如果此文章对您有帮助,点击 -->> 请博主喝咖啡 (opens new window)
上次更新: 2023/05/25, 16:16:09
php7_X安装
tcpdump抓包

← php7_X安装 tcpdump抓包→

最近更新
01
Linux Polkit 权限提升漏洞(CVE-2021-4034)
03-28
02
postgreSQL维护
03-17
03
trivy系统漏洞扫描
02-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 yangfk | 湘ICP备2021014415号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×
//