Linux系统——iptables网络安全服务

Iptables网络安全服务

  • iptables哲学

使用iptables的最高境界,永远都不使用哲学

1) 不给外网IP。

2) 大并发场景不开Iptables,硬件防火墙。

网站访问效率,网站安全性,永远是相悖

部署安全要把基础网络都弄明白尤其是基础网络知识这块

网络安全基础学习条件:

基础知识:网络基础 osi,tcp/ip,http,icmp协议。

三次握手,四次断开,11种状态。

常用的服务端口要了如指掌。

【tcp/ip协议卷1】

  • iptables防火墙概念说明

Iptables是什么?

开源的基于数据包过滤的网络安全策略控制工具。

centos6.9 --- 默认防火墙工具软件iptables

centos7 --- 默认防火墙工具软件firewalld(zone)

Iptables主要工作在OSI七层的二、三、四层,如果重新编译内核,

Iptables也可以支持7层控制(squid代理+iptables)。

iptables术语说明

iptables==tables(表)==chains(链)==rule(规则)

  • Iptables工作流程

iptables是采用数据包过滤机制工作的,所以它会对请求的数据包的包头数据进行分析,

并根据我们预先设定的规则进行匹配来决定是否可以进入主机。

iptables工作流程小结:

1、防火墙是一层层过滤的。实际是按照配置规则的顺序从上到下,从前到后进行过滤的。

2、如果匹配上了规则,即明确表明是阻止还是通过,此时数据包就不在向下匹配新规则了。

3、如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,向下进行匹配,

直到匹配默认规则得到明确的阻止还是通过。

4、防火墙的默认规则是对应链的所有的规则执行完以后才会执行的(最后执行的规则)。

Linux系统——iptables网络安全服务

防火墙iptables工作流程图

iptables四表五链概念

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

  • 含义

iptables表(tables)和链(chains)

iptables==>4个表(tables)===>5个链=(chains)==>规则(policy)

policy 政策; 规则;方针; 原则; 为人之道; 保险单

4表

filter(作用:主机防火墙) 默认的表。*****

nat(作用:端口或IP映射或共享上网)*****

mangle(配置路由标记 ttl tos mark)

raw(忽略)

表:

filter:INPUT,OUTPUT,FORWARD

NAT :POSTROUTING,PREROUTING,OUTPUT

mangle:INPUT,OUTPUT,FORWARD,POSTROUTING,PREROUTING

raw(忽略)

链:

INPUT:进入主机的数据包。主机防火墙(filter表的INPUT链)

OUTPUT:流出主机的数据包。

FORWARD:流经主机的数据包。

*PREROUTING:进入服务器最先经过的链,NAT端口或IP映射(导向)。(nat表的PREROUTING链)

*POSTROUTING:在流出服务器最后经过的链,NAT共享上网。局域网共享上网(nat表的POSTROUTING链)

Iptables企业应用场景

1、主机防火墙(filter表的INPUT链)。

2、局域网共享上网(nat表的POSTROUTING链)。半个路由器,NAT功能。

3、端口及IP映射(nat表的PREROUTING链),硬防的NAT功能。

4、IP一对一映射。

iptables工作原理见图

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

  • iptables实践操作过程

默认的防火墙规则信息查看方式

/etc/init.d/iptables status

iptables -L -n

iptables -nL

iptables -nL --line-number

--list -L [chain [rulenum]]

List the rules in a chain or all chains

列出链里面的规则信息

--numeric -n numeric output of addresses and ports

以数字的方式显示地址或端口信息

--line-number 显示规则行号信息

初始化操作配置

iptables -F <- 清除所有规则,不会处理默认的规则

iptables -X <- 删除用户自定义的链

iptables -Z <- 链的计数器清零(数据包计数器与数据包字节计数器)

iptables -nL -v <- 显示计数器信息

实践配置一:禁止访问相应服务端口

-t 表名称 --- 指定配置哪个表,指定配置表名称

-A 链名称 --- 附加或追加上相应规则策略,到指定链(链名称必须大写)

-I 链名称 --- 插入相应规则策略,到指定链上,默认将配置的规则插入到第一条

-D 链名称 --- 删除指定的规则

-p 协议名称 --- 指定规则的协议名称 all tcp udp icmp

--dport --- 指定匹配的目标端口信息

--sport --- 指定匹配的源端口信息

-j --- 匹配数据包后的动作 ACCEPT DROP REJECT

-i --- 在INPUT链配置规则中,指定从哪一个网卡接口进入的流量

-o --- 在OUTPUT链配置规则中,指定从哪一个网接口出去的流量

iptables -A INPUT -p tcp --dprot 22 -j DROP

iptables -I INPUT -p tcp --dprot 22 -j ACCEPT --- 恢复连接方法

iptables -I INPUT 2 -p tcp --dprot 22 -j ACCEPT --- 通过插入指定行号信息,指定将规则插入到第几行

iptables -D INPUT -p tcp --dport 22 -j ACCEPT --- 删除指定规则

iptables -D INPUT 2 --- 根据规则行号,删除相应的规则

实践配置二:禁止网段连入(禁止172.16.1.0网段访问172.16.1.8)

iptables -A INPUT -s 172.16.1.0/24 -d 172.16.1.8 -j DROP

-s --- 指定源IP地址或源网段信息

-d --- 指定目标IP地址或目标网段信息

实践配置三:禁止某个172.16.1.0网段不能访问服务器主机的22端口

iptables -A INPUT -s 172.16.1.0/24 -d 172.16.1.8 -p tcp --dport 22 -j DROP

实践配置四:除10.0.0.0网段可以进行连接服务器主机以外,其余网段都禁止

第一种方式:

iptables -A INPUT -s 10.0.0.0/24 -d 172.16.1.8 -j ACCEPT

修改默认规则,将默认规则改为拒绝

第二种方式:

! --- 表示对规则信息进行取反

iptables -A INPUT ! -s 10.0.0.0/24 -d 172.16.1.8 -j DROP --- centos6用法

iptables -A INPUT -s !10.0.0.0/24 -d 172.16.1.8 -j DROP --- centos5用法

说明:只有iptables帮助手册中指定的参数可以用取反符号(iptables --help)

  • dbus-daemon –system

这个是在linux中设置了防火墙关闭的状态导致配置防火墙时起不来所需要执行的命令。

其他

Linux系统——iptables网络安全服务

 

Linux系统——iptables网络安全服务

 

实例拓展:恢复刚才断掉的SSH连接

01: 去机房重启系统或者登陆服务器删除刚才的禁止规则

02: 让机房人员重启服务器或者让机房人员拿用户密码登陆进去

03: 通过服务器的远程管理卡管理(推荐)

04: 先写一个定时任务,每五分钟就停止防火墙

05: 测试环境测试好,写成脚本,批量执行

  • 商用防火墙

华为 深信服 思科 H3C Juniper 天融信 飞塔 网康 绿盟科技 金盾

实战打开iptables环境

[root@web01 ~]# /etc/init.d/iptables start

iptables: Applying firewall rules: [ OK ]

查看启动状态命令:

iptables -nL --line-number

-n #<==以数字的形式显示规则

-L #<==列表链里的所有规则

--line-number #<==打印规则序号

-t 指定表(default: `filter')

命令:

iptables [-t table] -A chain rule-specification

iptables [-t table] -I chain [rulenum] rule-specification

iptables [-t table] -D chain rulenum #<==根据规则号删除。

iptables [-t table] -D chain rule-specification

注释:

-t 指定表d(efault: `filter')

-A #<==把规则添加到指定的链上,默认添加到最后一行。

-I #<==插入规则,默认插入到第一行(封IP)。

-D #<==删除链上的规则

根据规则号删除:

iptables -D INPUT 4 #<==4是规则号。

-F #<==清除一个链或所有链上的规则

-Z #<==链的记数器清零

-X #<==删除用户自定义的链。

实践:

[root@web02 ~]# iptables -Z

[root@web02 ~]# iptables -X

[root@web02 ~]# iptables -F

[root@web02 ~]# iptables -nL

封22端口:

[root@web02 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP

规则注释:

-p #<==指定过滤的协议-p(tcp,udp,icmp,all)

--dport #<==指定目标端口(用户请求的端口)。

-j #<==对规则的具体处理方法(ACCEPT,DROP,REJECT,SNAT/DNAT)

--sport #<==指定源端口。

禁止10.0.0.253访问

iptables -I INPUT -p tcp -s 10.0.0.253 -i eth0 -j DROP

iptables -A INPUT -p tcp ! -s 10.0.0.1 -i eth0 -j DROP

iptables -A INPUT -p tcp ! -s 10.0.0.0/24 -i eth0 -j DROP

-s #<==指定源地址或网段(192.168.1.0/24)。 ! 取反。

-d #<==指定目的地址(nat表prerouting)。

-i #<==进入的网络接口(eth0,eth1)。

-o #<==出去的网络接口(eth0,eth1)。

封IP:

iptables -I INPUT -p tcp -s 10.0.0.1 -j DROP

恢复Mysql数据封除了跳板机以外的IP:

iptables -I INPUT -p tcp ! -s 10.0.0.1 -j DROP

匹配端口范围:

iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j DROP

iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT

iptables -I INPUT -p tcp --dport 18:80 -j DROP #<==最佳

匹配ICMP类型

iptables -A INPUT -p icmp --icmp-type 8

例:iptables -A INPUT -p icmp --icmp-type 8 -j DROP

iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT

iptables -A FORWARD -s 192.168.1.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT

ICMP:

http://www.cnitblog.com/yang55xiaoguang/articles/59581.html

  • 例题:默认允许

1、封掉10.0.0.7

iptables -I INPUT -s 10.0.0.7 -j DROP

2、让10.0.0.7和SSH客户端(10.0.0.1)服务器可以Ping,其它的不能Ping

iptables -I INPUT -p icmp --icmp-type 8 -s 10.0.0.7 -j ACCEPT

iptables -I INPUT 2 -p icmp ! -s 10.0.0.1 --icmp-type 8 -j DROP

3、封掉3306端口

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

匹配网络状态

-m state --state

NEW:已经或将启动新的连接

ESTABLISHED:已建立的连接

RELATED:正在启动的新连接

INVALID:非法或无法识别的

#允许关联的状态包 ftp协议

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

限制指定时间包的允许通过数量及并发数

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

指定时间内的请求速率"n"为速率,后面为时间分别为:秒、分、时

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

--limit-burst [n]

在同一时间内允许通过的请求"n"为数字,不指定默认为5

iptables -I INPUT -s 10.0.0.0/24 -p icmp --icmp-type 8 -m limit --limit 6/min --limit-burst 2 -j ACCEPT

http://blog.csdn.net/gnicky/article/details/51334793

部署一个最安全的企业级防火墙(案例)

两种思想:针对默认规则而言。

逛公园:黑名单

1、默认规则默认是允许的状态。

看电影:白名单

2、默认规则默认是不允许的状态。更安全。

看电影的思想更安全。

实战部署:

[root@web02 ~]# iptables -F

[root@web02 ~]# iptables -X

[root@web02 ~]# iptables -Z

[root@web02 ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT

[root@web02 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

[root@web02 ~]# iptables -P INPUT DROP

[root@web02 ~]# iptables -P FORWARD DROP

[root@web02 ~]# iptables -P OUTPUT ACCEPT

[root@web02 ~]# iptables -nL

Chain INPUT (policy DROP)

target prot opt source destination

ACCEPT all -- 10.0.0.0/24 0.0.0.0/0

ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22

Chain FORWARD (policy DROP)

target prot opt source destination

Chain OUTPUT (policy ACCEPT)

target prot opt source destination

[root@web02 ~]# iptables -A INPUT -i lo -j ACCEPT --- 让自己可以ping自己

[root@web02 ~]# iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT

--- 允许哪些网站服务开启

[root@web02 ~]# iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT

--- 允许架构内部服务进行访问

[root@web02 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

--- 允许web服务与ftp服务器建立连接

命令都在内存中生效。

[root@web02 ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT

--- 允许一些合作企业的外网服务器进行访问

命令集合:

iptables -F

iptables -X

iptables -Z

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT

iptables -P INPUT DROP

iptables -P FORWARD DROP

iptables -P OUTPUT ACCEPT

iptables -nL

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT

iptables -A INPUT -p icmp -s 10.0.0.0/24 -m icmp --icmp-type any -j ACCEPT

iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT

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

扫描测试:使用云主机部署,用笔记本虚拟机扫描

[root@web02 ~]# nmap www.baidu.com

Starting Nmap 5.51 ( http://nmap.org ) at 2017-04-04 12:23 CST

Nmap scan report for www.baidu.com (119.75.217.109)

Host is up (0.020s latency).

Other addresses for www.baidu.com (not scanned): 119.75.218.70

Not shown: 998 filtered ports

PORT STATE SERVICE

80/tcp open http

443/tcp open https

Nmap done: 1 IP address (1 host up) scanned in 13.60 seconds

  • 永久生效:

01. 第一种方式

[root@web02 ~]# /etc/init.d/iptables save

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

[root@web02 ~]# cat /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Tue Apr 4 12:24:43 2017

*filter

:INPUT DROP [0:0]

:FORWARD DROP [0:0]

:OUTPUT ACCEPT [159:10664]

-A INPUT -s 10.0.0.0/24 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

-A INPUT -s 172.16.1.0/24 -j ACCEPT

-A INPUT -i lo -j ACCEPT

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

COMMIT

# Completed on Tue Apr 4 12:24:43 2017

02. 第二种方式

iptables-save >/etc/sysconfig/iptables

生产维护iptables:

1、日常改/etc/sysconfig/iptables配置

-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT

生效:

1)/etc/init.d/iptables reload

2)iptables -I INPUT 3 -p tcp -m tcp --dport 3306 -j ACCEPT

2、临时封Ip(-I)

iptables -I INPUT -s 203.71.78.10 -j DROP

3、常用的规则放在前面生效

[root@web01 ~]# cat /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Mon Apr 17 12:03:54 2017

*filter

:INPUT DROP [0:0]

:FORWARD DROP [0:0]

:OUTPUT ACCEPT [371:25376]

-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT

-A INPUT -s 172.16.1.0/24 -j ACCEPT

-A INPUT -s 10.0.0.0/24 -j ACCEPT

-A INPUT -i lo -j ACCEPT

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

COMMIT

# Completed on Mon Apr 17 12:03:54 2017

写成脚本:

#!/bin/bash

#this is a server firewall

#updated by XXX on 2019.XX.XX

#define variable PATH

IPT=/sbin/iptables

#Remove any existing rules

$IPT -F

$IPT -X

$IPT -Z

#setting default firewall policy

$IPT --policy OUTPUT ACCEPT

$IPT --policy FORWARD DROP

$IPT -P INPUT DROP

#setting for loopback interface

$IPT -A INPUT -i lo -j ACCEPT

#setting access rules

#one,ip access rules,allow all the ips of

$IPT -A INPUT -s 202.81.17.0/24 -p all -j ACCEPT

$IPT -A INPUT -s 202.81.18.0/24 -p all -j ACCEPT

$IPT -A INPUT -s 124.43.62.96/27 -p all -j ACCEPT

$IPT -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT

$IPT -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT

#icmp

$IPT -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT

#others RELATED

$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

chkconfig iptables on

重点:

1、iptables包过滤流程。

2、iptables4表5链作用和关系。

3、iptables工作原理。

4、iptables常用命令参数。

5、部署一个企业级主机防火墙案例。

部署iptables防火墙的最高境界?

不用防火墙,服务器照样安全。

1、尽可能不给服务器配置外网IP。可以通过代理转发或者通过防火墙映射。

2、并发不是特别大情况再外网IP的环境,要开启iptables防火墙。

如果并发量很大,不能开iptables,影响性能,买硬件防火墙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值