Linux知识之防火墙系列

补充Linux的防火墙的开启和关闭(不同的系统有所不同)

/etc/init.d/iptables start/stop/restart
service iptables start/stop/restart

Ubuntu和kali一般用的是ufw

apt-get install ufw
ufw disable  //关闭防火墙
ufw enable  //开机启动防火墙
ufw status
 防火墙介绍:
分类1:
主机防火墙:一般针对单个主机进行防护
网络防火墙:一般部署在网络的入口
分类2:
硬件防火墙:难被破解 但是安全性高
软件防火墙:性能不如硬件防火墙 而且会被破解
iptables:确切说是客户端的代理   用户空间
netfilter:真正的防火墙的安全框架   内核空间
1.nat
2.数据修改
3.防火墙功能   数据过滤等等功能
接下来介绍 iptables

安装iptables:

yum -y install iptables
systemctl start iptables
yum -y install iptables-services     //安装iptables服务
service iptables save    //保存防火墙规则

规则保存的位置:

/etc/sysconfig/iptables

五链:

prerouting 数据包刚进入网络层   数据包进入本机 转发之前的规则
input 路由判断好了,进入用户空间  入栈数据包
output 用户空间发出,让路由判断从哪走   出栈数据包
postrouting 数据包通过网络口发出去    在进行路由判断后进行的规则
forward 不进入用户空间,直接转发   转发数据包  数据的转发 比如arp欺骗

四表:

filter表:负责过滤(iptables_filter)   input forward output
nat表:网络地址转换(iptable_nat)  除了forward以外都可以
mangle表:解析报文,修改报文,封装报文(iptable_mangle)  全部都可以
raw表:关闭nat表的连接追踪(iptable_raw)  prerouting output

表优先级关系:raw>mangle>nat>filter

处理动作:

ACCEPT
DROP
REJECT
SNAT    //源地址转换 用于解决内网用户共用公网一个地址上网的情况
MASQUERADE    //动态改变ip  SNAT的改变版
DNAT     //目标地址的转换
REDIRECT   //本机端口映射
LOG  //记录日志 日志位置/var/log/messages

顺序很重要,其是从上往下操作的  如果同一个ip但是不同处理,在上面是拒绝 下面是接收 最后就没办法接收 因为数据包已经被上面的规则拒绝掉了

iptables的参数的介绍合集:

dport  destination port  目标端口
-p  protocol 协议tcp udp udplite icmp esp ah sctp icmpv6 mh
-s  source 来源  可以是目标的ip或者网段
-d  destination  表示目标地址
-A  add  在规则链的末尾添加该新的规则
-I   在规则链的头部添加该新的规则
-D  delete  将某一行的规则删除掉
-m  表示拓展模块的功能
--dport  目标的端口
--sport  来源的端口

-t  表名
-L  链名
-i  指定使用的网卡

一些iptables常用命令的实战合集 帮助理解

iptables -F   //清空防火墙现有的规则
iptables -t filter -F INPUT   //清空filter中所有input链的内容
iptables -D INPUT 1   //将第一行的规则删除掉
iptables -D INPUT [行]   //删除第几行的规则
iptables -D INPUT 10   //删除第10个  INPUT是因为整个链是Chain INPUT
iptables -L   //查看所有链的内容 相当于查看iptables的内容
iptables -t 表名 -L 链名
iptables -t filter -L    //查看filter表的内容  里面默认已经有很多默认规则了
iptables -t raw -L
iptables -t mangle -L
iptables -t nat -L
iptables --line-numbers -nvL INPUT   //查看INPUT链的内容 并且显示数字标号
iptables --line-numbers -t 表名 -nvL   //查看某个表中的内容
iptables -vL INPUT   //查看INPUT链
iptables --line -nvL INPUT   //查看请求包的接收和拒绝情况
iptables --line-numbers -vL INPUT
iptables -I INPUT -s [ip或网段] -j [动作]

常见用法的举例:

iptables -t filter -I INPUT -s 192.168.1.61 -j DROP   //拒绝来自192.168.1.61的input流量  -I表示从最上面开始添加
iptables -t filter -A INPUT -s 192.168.1.133 -j DROP      //-A表示在列表最后添加
iptables -A OUTPUT -j ACCEPT   //允许本机所有的向外的访问  也可以加参数进行细化
iptables -t filter -I INPUT --sport 22 -j DROP   //PS:如果使用的是vnc连接vps  可以尝试把22端口的ssh禁用掉  避免公网上对ssh的猜测和爆破   也可以要用ssh的时候通过vnc把这条规则打开 不用再关闭掉即可
iptables -A INPUT -p tcp -m tcp -s [目标ip/网段] -j DROP   //一般用于屏蔽ip  屏蔽整个段的方法 192.168.201.1/24    凡是192开头的都屏蔽掉  192.0.0.0/8  
iptables -L -n -v   //可视化的 更加详细的看iptables的信息  可以动态实时的看到接收到的数据包和操作
iptables -L -n -v --line-numbers   //并且显示行数  比如你屏蔽了22号端口  那么别人发送了数据过来被本机拒绝了以后可以在可视化界面看到数据包的大小变化
iptables -A INPUT -d 192.168.1/2 -j ACCEPT    //允许向目标发送数据包
iptables -A INPUT -s 192.168.1.2 -j ACCEPT    //允许所有来自于这个目标的数据包    
iptables -A INPUT -p tcp --deport 80 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT   //允许被ping  DROP就是不允许 可以防止扫描器扫描
iptables -A INPUT -s 172.20.10.4 -p tcp --dport 22 -j ACCEPT 
iptables -A INPUT -s 172.20.10.4 -p tcp --dport 22 -j DROP
iptables -A INPUT -p all -s [ip/网段] -j ACCEPT
iptables -P INPUT DROP    //默认情况下 不允许所有数据进入
iptables -I INPUT -s 192.168.1.100 -j DROP
iptables -I INPUT -s 192.168.1.100,192.168.1.101 -j DROP
iptables -I INPUT -s 192.168.1.100/24 -j DROP

常见用法举例2:

iptables -I INPUT ! -s [ip或网段] -j [动作]     //取反操作  可以用于防止DDOS攻击 表示除了这个输入的s目标以外执行相应的动作
iptables -I INPUT -s 1.1.1.1 -d 1.1.1.2 -j DROP   //拒绝源是1.1.1.1目标是1.1.1.2的INPUT方向的数据包   可以防止别人把本机当作路由器进行转发
iptables -I INPUT -s 1.1.1.1 -p tcp -j DROP    //拒绝所有来自1.1.1.1的tcp请求
iptables -I INPUT -s 192.168.1.132 -p icmp -j DROP   //拒绝来自192.168.1.132的ping请求
iptables -I INPUT -s 1.1.1.1 -i ens33 -p icmp -j DROP   //拒绝来自网卡ens33的input方向的来自1.1.1.1的icmp请求

拓展模块的使用举例:

iptables -I INPUT -s 1.1.1.1 -p tcp -m multiport --dports 22,3389,80 -j DROP    //同时拒绝多个目的地端口
iptables -t filter -I INPUT -m iprange --src-range 192.168.1.130-192.168.1.133 -j DROP
iptables -t filter -I OUTPUT -m iprange --dst-range 192.168.1.130-192.168.1.133 -j DROP
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "XXOO" -j REJECT //string是匹配字符串,algo模式匹配算法(bm,kmp)  比如匹配黄色的字段
//时间拓展模块 datastart datastop
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 19:00:00 -j DROP
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 6,7 -j DROP
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --monthdays 27,28 -j DROP

connlimit拓展模块   限制连接模块

// --connlimit-above 单独限制每个ip同时连接到server的连接数量 抵御cc攻击和DDOS攻击
iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 2 -j REJECT

//--connlimit-mask 不能单独使用,限制某类ip网段内一定数量ip连接数
iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 20  --connlimit-mask 24 -j REJECT   //某个网段(24位子网掩码)允许tcp80端口20个连接数

limit拓展模块  限速

每一个人在令牌桶中拿一个令牌才可以访问网络  
--limit-burst 令牌桶  后面数字表示桶中令牌的数量
--limit 令牌桶中生成新令牌频率second minute hour day

iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT  
 //每十分钟生产一个令牌 拿到令牌的就可以放行
iptables -t filter -A INPUT -p icmp -j REJECT   //其他没有令牌的一律拒绝访问

udp拓展模块   限制udp端口

iptables -t filter -I INPUT -p udp -m udp --dport 136 -j ACCEPT
iptables -t filter -I INPUT -p udp -m udp --dport 137:150 -j ACCEPT

icmp(Internet control message port) 即ping命令  拓展包

iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j REJECT

自定义链

iptables -N IWEB   //新的链名叫IWEB
iptables --line-numbers -vL IWEB   //查看该链的内容
iptables --line-numbers -nvL IWEB  //查看该链的内容

iptables -nvL   //看到新链
iptables -I IWEB -s 192.168.1.32 -j REJECT    //添加iptables
iptabels -E IWEB WEB   //修改名字 edit编辑

iptables -I INPUT -p tcp -j IWEB
把IWEB添加到INPUT中(引用) 因为是引用 所以修改IWEB中的内容会同步修改引用?

iptables -F WEB   //首先需要清空规则
iptables -X WEB   //删除链  (如果这条链其中还存在规则 或者已经被引用了 都没有办法删除)

2.网络防火墙

iptables -nvL FORWARD   //查看forward链
iptables -A FORWARD -j REJECT    //添加默认拒绝
iptables -I FORWARD -s 1.1.1.0/24 -p tcp -m tcp --dport 80 -j ACCEPT   //添加转发规则

SNAT:将内网的ip地址转为外网的(里面怎么出去  将内网ip地址转为外网ip地址)

举例:内网所有ip在10.1.1.0/24网段   外网ip是172.8.7.1(类似内网穿透)

iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -j SNAT --to-source 172.8.7.1   //内网网段改为公网ip

DNAT(外面怎么进来   外网想要访问内网某个服务器的端口)

适用于公网某个端口的访问,将请求转至内网某台服务器的端口上 也就是内网的服务器暴露到公网端口上

iptables -t nat -I PREROUTING -d 172.8.7.1 -p tcp --dport 80 -j DNAT --to-destination 10.1.1.2:80    //有人访问172.8.7.1:80就转发到内网的10.1.1.2:80

配置完DNAT必须配置SNAT  别人把东西传进来 也要设计SNAT才能把东西发出去

MASQUERADE 

(动态的SNAT 一家家中的IP地址是会随着路由器的开关改变的)

iptables -t nat -I POSTROUTING -s 10.1.1.0/24 -o ens33 -j MASQUERADE

把内网的流量转发到某个网卡的公网ip上,相当于动态的SNAT

REDIRECT

本地进行端口映射 (本机的某某端口映射到本机的某某端口上)

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080   //当别的电脑访问我80端口的时候,会重定向到8080端口

REDIRECT只能定义到PREROUTING或者OUTPUT链中

firewalld和iptables区别:(都可以去借助中文文档自行学习相关命令)

(1)centos7以上用的都是firewalld  centos7以下是iptables

(2)默认使用的是firewalld来管理的 但是底层调用的是iptables的命令

(3)ubuntu使用的是ufw  ufw是轻量级的iptables

(4)firewalld默认服务是拒绝的  iptables默认服务是允许的的

(5)iptables必须知道表 链 firewalld不需要知道 更简单

man firewalld   //查看帮助
firewall-cmd --get-default-zone   //查看当前使用的区域
firewall-cmd --get-zone-of-interface=ens33   //查看ens33网卡所在的区域
firewall-cmd --list-all   //列出所有条目
firewall-cmd --add-service=http --permanent   //添加http放行永久生效
firewall-cmd --add-port=80/tcp   //放行80端口tcp流量

selinux

安全子系统,安全加强版linux,不需要学习

/etc/selinux/config

SELINUX=enforing 默认开启
改为disabled即可

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Simon_Smith

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

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

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

打赏作者

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

抵扣说明:

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

余额充值