网络 || iptables

防火墙是什么?

防火墙(firewall):对进出系统的数据进行过滤,符合要求的数据,可以放行也可以不放行---》对进出的数据进行限制
    对数据包进行分析:
        帧,ip包,tcp/udp,协议(icmp,arp,http等)
        mac地址,ip地址,端口号,协议来进行限制

Linux系统的防火墙功能是由内核实现的。

2.0 版内核中,包过滤机制是ipfw,管理工具是ipfwadm

2.2 版内核中,包过滤机制是ipchain,管理工具是ipchains

2.4 版及以后的内核中,包过滤机制是netfilter,管理工具是iptables

netfilter 位于Linux内核中的包过滤防火墙功能体系称为Linux防火墙的“内核态”,是内核的一个模块

iptables 位于/sbin/iptables,是用来管理防火墙的命令工具 为防火墙体系提供过滤规则/策略,决定如何过滤或处理到达防火墙主机的数据包 称为Linux防火墙的“用户态”。

习惯上,上述2种称呼都可以代表Linux防火墙。

iptables是一个软件,给用户传递参数的 --》用户端的程序:实现给内核传递参数

比较老的防火墙工具:Iptables --》netfilter
新的防火墙工具:firewalld -->netfilter

firewalld 底层仍然使用的是iptables规则,加入了很多的概念(zone,DMZ)类似windows里的防火墙。
内核(kernel):是操作系统内部最核心的软件--》内核在内存里运行
内核的作用:对cpu、内存、磁盘、网络等硬件进行管理

# 查看内核版本
[root@manger ~]# uname -r
3.10.0-957.el7.x86_64

进程分2种类型:
    1.内核态:  内核态的进程可以访问用户空间,管理用户态的进程
    2.用户态:  用户态的进程不能随意访问内核态空间

防火墙的位置

路由器/防火墙

    重要的服务器集群前面也放一个防火墙
    服务器上也可以启用防火墙

防火墙的层数越多越好吗?

不是

    1.成本高
    2.会导致数据延迟(网络的速度会慢)

关闭防火墙

[root@manger ~]# systemctl stop firewalld

包过滤防火墙工作在TCP/IP的网络层

规则链

规则的作用在于对数据包进行过滤或处理,根据处理时机的不同,各种规则被组织在不同的“链”中。

规则链是防火墙规则/策略的集合

默认的5种规则链

INPUT:处理入站数据包

OUTPUT:处理出站数据包

FORWARD:处理转发数据包

POSTROUTING链:在进行路由选择后处理数据包

PREROUTING链:在进行路由选择前处理数据包

规则表 

具有某一类相似用途的防火墙规则,按照不同处理时机区分到不同的规则链以后,被归置到不同的“表”中

规则表是规则链的集合

默认的4个规则表

raw表:确定是否对该数据包进行状态跟踪

mangle表:为数据包设置标记

nat表:修改数据包中的源、目标IP地址或端口

filter表:确定是否放行该数据包(过滤)

[root@manger ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

chain          链
policy         策略/规则
ACCEPT   接收
DROP       丢弃
REJECT    委婉的拒绝--》不接收数据

iptables的规则表、链结构

规则表间的优先顺序依次为:raw、mangle、nat、filter

规则链间的匹配顺序

入站数据:PREROUTING、INPUT

出站数据:OUTPUT、POSTROUTING

转发数据:PREROUTING、FORWARD、POSTROUTING

规则链内的匹配顺序

按顺序依次进行检查,找到相匹配的规则即停止(LOG策略会有例外)

若在该链内找不到相匹配的规则,则按该链的默认策略处理

数据过滤图

 iptables命令的语法格式

        iptables  [-t 表名]  管理选项  [链名]  [条件匹配]  [-j 目标动作或跳转]

注意事项:

不指定表名时,默认表示filter表

不指定链名时,默认表示该表内所有链

除非设置规则链的缺省策略,否则需要指定匹配条件

设置规则内容

清除规则

-D:删除指定位置或内容的规则
-F:清空规则链内的所有规则

自定义规则链

-N:创建一条新的规则链
-X:删除自定义的规则链

其他

-h:查看iptables命令的使用帮助

案例1

[root@manger ~]# iptables -D INPUT 2

[root@manger ~]# iptables -F

[root@manger ~]# iptables -t nat -F

[root@manger ~]# iptables -t raw -N tcp_packets

[root@manger ~]# iptables -t raw -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain tcp_packets (0 references)
target     prot opt source               destination 

案例2

# 编写脚本
[root@manger iptables]# cat clear_rule.sh 
#!/bin/bash

iptables="/usr/sbin/iptables"
$iptables -P INPUT ACCEPT
$iptables -F -t filter
$iptables -F -t nat

[root@manger iptables]# bash -x clear_rule.sh 
+ iptables=/usr/sbin/iptables
+ /usr/sbin/iptables -P INPUT ACCEPT
+ /usr/sbin/iptables -F -t filter
+ /usr/sbin/iptables -F -t nat

# 创建计划任务,每隔1分钟执行
[root@manger iptables]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

[root@manger iptables]# crontab -l
*/1 * * * *  bash /iptables/clear_rule.sh
[root@manger iptables]# ls
clear_rule.sh

[root@manger iptables]# iptables -A INPUT -p icmp -j DROP
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    

[root@manger iptables]# iptables -A INPUT -p icmp -j DROP
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       
  
[root@manger iptables]# ping www.baidu.com
PING www.baidu.com (14.119.104.189) 56(84) bytes of data.
^C
--- www.baidu.com ping statistics ---
16 packets transmitted, 0 received, 100% packet loss, time 15140ms

[root@manger iptables]# curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

#超过一分钟后在ping
[root@manger iptables]# ping www.baidu.com
PING www.baidu.com (14.119.104.189) 56(84) bytes of data.
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=1 ttl=54 time=24.4 ms
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=2 ttl=54 time=34.1 ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2006ms
rtt min/avg/max/mdev = 24.441/29.275/34.109/4.834 ms

#查看规则
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     

案例3

# 添加自定义链
[root@manger iptables]# iptables -t filter -N sc
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain sc (0 references)
target     prot opt source               destination    

# 在自定义的规则链里添加规则     
[root@manger iptables]# iptables -t filter -A sc -p tcp --dport 8090 -j DROP
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain sc (0 references)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere             tcp dpt:8090

# 在自定义的规则链里添加规则
[root@manger iptables]# iptables -t filter -A INPUT -p tcp -j sc
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
sc         tcp  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain sc (1 references)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere             tcp dpt:8090

# 删除自定义链
[root@manger iptables]# iptables -D INPUT 1
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain sc (0 references)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere             tcp dpt:8090

[root@manger iptables]# iptables -D sc 1
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain sc (0 references)
target     prot opt source               destination     

[root@manger iptables]# iptables -X sc
[root@manger iptables]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination      

# 默认的链是不能删除的   
[root@manger iptables]# iptables -X  INPUT
iptables: Invalid argument. Run `dmesg' for more information.

对tcp的操作

[root@localhost ~]# iptables -I INPUT -i ens33 -p tcp --tcp-flags SYN,RST,ACK SYN --dport 3306 -j REJECT

[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306 flags:0x16/0x02 reject-with icmp-port-unreachable
zhangshaowei  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain zhangshaowei (1 references)
target     prot opt source               destination         
DROP       tcp  --  192.168.2.113        0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  192.168.2.110        0.0.0.0/0            tcp dpt:80

对icmp的操作

[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j REJECT

[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8 reject-with icmp-port-unreachable
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306 flags:0x16/0x02 reject-with icmp-port-unreachable
zhangshaowei  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain zhangshaowei (1 references)
target     prot opt source               destination         
DROP       tcp  --  192.168.2.113        0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  192.168.2.110        0.0.0.0/0            tcp dpt:80

[root@localhost ~]# ping 192.168.102.129    # 被限制访问
PING 192.168.102.129 (192.168.102.129) 56(84) bytes of data.
From 192.168.102.129 icmp_seq=1 Destination Port Unreachable
From 192.168.102.129 icmp_seq=2 Destination Port Unreachable
From 192.168.102.129 icmp_seq=3 Destination Port Unreachable
From 192.168.102.129 icmp_seq=4 Destination Port Unreachable
^C
--- 192.168.102.129 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 94ms

[root@localhost ~]# ping 192.168.102.124    # 可能ip地址没有被使用
PING 192.168.102.124 (192.168.102.124) 56(84) bytes of data.
From 192.168.102.129 icmp_seq=1 Destination Host Unreachable
From 192.168.102.129 icmp_seq=2 Destination Host Unreachable
From 192.168.102.129 icmp_seq=3 Destination Host Unreachable
^C
--- 192.168.102.124 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 105ms
pipe 4
[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@localhost ~]# ping 192.168.102.129
PING 192.168.102.129 (192.168.102.129) 56(84) bytes of data.
^C
--- 192.168.102.129 ping statistics ---
45 packets transmitted, 0 received, 100% packet loss, time 115ms

限制多个ip地址访问

[root@localhost ~]# iptables -A INPUT -p tcp -m iprange --src-range 192.168.1.20-192.168.1.99 -j DROP

[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
REJECT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8 reject-with icmp-port-unreachable
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306 flags:0x16/0x02 reject-with icmp-port-unreachable
zhangshaowei  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            source IP range 192.168.1.20-192.168.1.99

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain zhangshaowei (1 references)
target     prot opt source               destination         
DROP       tcp  --  192.168.2.113        0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  192.168.2.110        0.0.0.0/0            tcp dpt:80

导出规则
iptables-save
结合重定向输出“>”符号保存规则信息
导入规则
iptables-restore
结合重定向输入“<”符号恢复规则信息

[root@router nat]#iptables-save >/etc/sysconfig/iptables

[root@router nat]# iptables-restore </etc/sysconfig/iptables

LOG会记录到日志里

1.记录到哪里去了?

            /var/log/messages

2.谁会帮我们记录日志

service  rsyslogd    帮助我们记录日志
kernel netfilter 会把日志告诉rsyslogd 让它记录日志--》kernel 将日志记录功能外包给了rsyslog

Linux系统里的专门记录日志的服务:rsyslog

# 打开日志功能
[root@manger ~]# vim /etc/rsyslog.conf 
  *.info;mail.none;authpriv.none;cron.none             /test/sc.log

# 重启服务
[root@manger ~]# service rsyslog restart
Redirecting to /bin/systemctl restart rsyslog.service

[root@manger /]# mkdir test
[root@manger /]# cd test/
[root@manger test]# touch sc.log

# 测试
[root@manger ~]# iptables -A INPUT -p icmp -j LOG --log-prefix "teacher feng" --log-level 4
[root@manger ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
LOG        icmp --  anywhere             anywhere             LOG level warning prefix "teacher feng"

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     

# 查看日志
[root@manger test]# tail -f /test/sc.log
Aug  3 16:13:05 manger kernel: teacher fengIN=ens33 OUT= MAC=00:0c:29:90:7f:ca:12:63:ac:ce:30:6c:08:00 SRC=192.168.2.4 DST=192.168.2.11 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=2674 PROTO=ICMP TYPE=8 CODE=0 ID=1 SEQ=1117 
Aug  3 16:13:06 manger kernel: teacher fengIN=ens33 OUT= MAC=00:0c:29:90:7f:ca:12:63:ac:ce:30:6c:08:00 SRC=192.168.2.4 DST=192.168.2.11 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=2675 PROTO=ICMP TYPE=8 CODE=0 ID=1 SEQ=1118 
Aug  3 16:13:07 manger kernel: teacher fengIN=ens33 OUT= MAC=00:0c:29:90:7f:ca:12:63:ac:ce:30:6c:08:00 SRC=192.168.2.4 DST=192.168.2.11 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=2676 PROTO=ICMP TYPE=8 CODE=0 ID=1 SEQ=1119 
Aug  3 16:13:08 manger kernel: teacher fengIN=ens33 OUT= MAC=00:0c:29:90:7f:ca:12:63:ac:ce:30:6c:08:00 SRC=192.168.2.4 DST=192.168.2.11 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=2677 PROTO=ICMP TYPE=8 CODE=0 ID=1 SEQ=1120 

cron.info 记录cron类型的日志的info以上级别的信息,包括info。

日志的设备类型

    1.kernel  kern.Info     内核产生的info以上级别的日志 kern.* 表示所有的kern类型的日志级别
    2.authpriv  --》ssh远程登录,需要输入用户名和密码
    3.cron     --》计划任务
    4.mail
    5.local1~local7 都是自定义的类型
    6.news

日志的级别

    none
    7 debug
    6 info
    5 notice
    4 warning
    3 error
    2 critical
    1 alert
    0 emergency
级别值越小则优先级越高

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iptables是一个用于Linux系统的防火墙工具,它可以对网络流量进行过滤和控制。通过iptables,你可以定义规则来允许或拒绝特定类型的网络流量。 iptables使用规则链(rule chains)来管理流量过滤,包括输入链(INPUT)、输出链(OUTPUT)和转发链(FORWARD)。每个链中都包含一系列规则,这些规则定义了如何处理进入或离开系统的数据包。 在iptables中,每个规则由三个主要部分组成:匹配条件(match conditions)、动作(actions)和目标(targets)。匹配条件指定要应用规则的数据包的特征,例如源IP地址、目标端口等。动作定义了当匹配条件满足时要执行的操作,例如允许通过或丢弃数据包。目标指定规则所属的链。 以下是iptables的一些常见命令和功能: 1. 添加规则:使用`iptables -A`命令添加一条新的规则,例如`iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT`表示允许来自192.168.1.0/24子网的数据包通过输入链。 2. 删除规则:使用`iptables -D`命令删除指定的规则,例如`iptables -D INPUT 2`表示删除输入链中的第2条规则。 3. 显示规则:使用`iptables -L`命令查看当前所有规则,例如`iptables -L INPUT`表示显示输入链的规则列表。 4. 默认策略:使用`iptables -P`命令设置默认策略,当没有匹配任何规则时将采取的动作。例如`iptables -P INPUT DROP`表示设置输入链的默认动作为丢弃(DROP)。 5. 网络地址转换(NAT):除了过滤功能外,iptables还可以用于进行网络地址转换。通过使用`-t nat`选项,可以创建NAT规则,实现端口转发、SNAT和DNAT等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值