火墙

firewalld 内核级加强型火墙

在做实验之前把虚拟机都reset,保证实验环境的纯净

 

getenforce ##查看selinux开启Enforcing警告模式或者改为Disable 关闭模式

在虚拟机中配置两个网卡,一个为172.25.254.155,另一个为172.25.55.1

配置yum源仓库

 

firewall-config  & ##图形管理,后边的&符表示不让图形战用自己的终端

图形中一些参数的意义

permanent ##永久更改   runtime 暂时性的修改

Options ---> reload firewall ##刷新&保存

             Change default zone 更改区域,其中这些区域包括

               trust相当于把防火墙关闭,对所有连接都开放。

                block 相当于拒绝全部连接。

                drop 任何数据包都会被丢弃,而且不做任何回应

                dmz非军事区

                public公共区

 ports ##添加端口选项

Services 添加服务的选项,添加完需要重新加载火墙

在测试端的虚拟机安装http服务

Yum install httpd 便于后边的实验

用图形修改端口

 

测试

http://172.25.254.155 ##访问失败

http://172.25.254.155:8080/ ##访问成功

 

 

firewall-cmd --list-all ##查看

firewall-cmd --state ##状态

firewall-cmd --get-zones ##查看系统火墙域有哪些

firewall-cmd --get-default-zone ##查看默认域是哪一个

firewall-cmd --get-active-zones ##当前正在生效的设定

 

 

firewall-cmd --set-default-zone=dmz ##dmz设为默认域

firewall-cmd --zone=public --list-all ##列出指定域的策略

firewall-cmd --get-services ##查看所有可以开启的服务

firewall-cmd --list-all-zones ##列出系统中可以直接使用的域

 

 

firewall-cmd --permanent --add-source=172.25.254.55 --zone=trusted

##允许55访问

firewall-cmd --reload

firewall-cmd --permanent --remove-source=172.25.254.55 --zone=trusted

##移除策略

firewall-cmd --reload

测试

systemctl start httpd

http://172.25.254.155/

 

 

firewall-cmd --list-all  ##查看

firewall-cmd --list-all --zone=trusted 列出在trusted域中的策略

firewall-cmd --remove-interface=eth1 --zone=dmz 把eth1在dmz中移除

firewall-cmd --add-interface=eth1 --zone=trusted   把eth1添加到trusted域中

firewall-cmd --list-all --zone=trusted 列出trusted区域的策略

 

firewall-cmd --add-service=http --zone=trusted

firewall-cmd --list-all --zone=trusted  

firewall-cmd --get-services ##查看所有可以开启的服务

firewall-cmd --add-service=http ##临时打开http

测试访问172.25.55.1时可以访问到

 

 

systemctl restart firewalld.service ##重启后消失

firewall-cmd --list-all

firewall-cmd --permanent --add-service=http ##永久打开

firewall-cmd --reload

 

 

vim /etc/httpd/conf/httpd.conf ##更改端口

42 Listen 8080

systemctl restart httpd

firewall-cmd --permanent --add-port=8080/tcp ##打开端口

firewall-cmd --reload

测试172.25.254.155:8080

 

 

firewall-cmd --add-service=ftp

firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.55 -p tcp --dport 21 -j REJECT

##172.25.254.55能连接21端口

firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 ! -s 172.25.254.55 -p tcp --dport 21 -j REJECT

##移除

firewall-cmd --add-masquerade --zone=public

firewall-cmd --list-all

#masquerade: yes

 

firewall-cmd--add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.8

firewall-cmd --list-all

#forward-ports: port=22:proto=tcp:toport=22:toaddr=172.25.254.8

firewall-cmd--remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.8

 

原地址转换

systemctl restart network

 

firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.25.254.155 masquerade'

firewall-cmd --list-all

#rich rules:

# rule family="ipv4" source address="172.25.254.155" masquerade

 

辅机:

systemctl stop NetworkManager ##停止网络记录

systemctl restart network

nm-c ##配置ip172.25.55.2网关172.25.55.1

systemctl restart network

systemctl start NetworkManager

firewall-cmd --add-masquerade --zone=public

ping 172.25.254.55#网络通畅

 

 

yum search iptables

yum install iptables-services.x86_64 -y

systemctl stop firewalld.service ##停止火墙

Systemctl disable firewalld ##开机不启动火墙

systemctl mask firewalld.service ##冻结火墙

Systemctl start iptables

Systemctl enable iptables

 

iptables -nL ##查看所有策略

iptables -F ##清空所以策略

iptables -nL ##再次查看

systemctl restart iptables.service ##重启

systemctl status iptables.service ##查看当前状态

 

 

iptables -nL ##查看策略发现是临时改动

iptables -F ##再次清空

service iptables save ##保存

systemctl restart iptables.service ##再次重启

iptables -nL ##再次查看

 

iptables -t filter -nL ##查看filter

iptables -t nat -nL ##查看nat

iptables -t mangle -nL ##查看mangle

 

 

iptables -A INPUT  -p tcp --dport 22 -j ACCEPT ##添加22端口

A添加到最后一行

iptables -A INPUT -i lo  -j ACCEPT ##添加lo回环接口

iptables -nL ##查看

iptables  -A INPUT -j REJECT ##拒绝其他请求

iptables -nL

iptables -I INPUT 3 -p tcp --dport 80 -j ACCEPT ##插入

iptables -nL

 

 

iptables -R INPUT 3 -p tcp --dport 8080 -j ACCEPT ##修改

iptables -nL

iptables -D INPUT 4 ##删除

iptables -nL

yum install vsftpd -y

systemctl start vsftpd

 

 

iptables -P INPUT  DROP ##默认修改为丢掉

iptables -nL

Chain INPUT (policy DROP)

lftp 172.25.254.108 ##请求不会回应

lftp 172.25.254.108:~> ls              

`ls' at 0 [Connecting...]

iptables -P INPUT  ACCEPT ##默认修改为允许

Chain INPUT (policy ACCEPT)

lftp 172.25.254.108 ##请求会回应

lftp 172.25.254.108:~> ls              

drwxr-xr-x    2 0        0               6 Aug 03  2015 pub

 

 

iptables -A INPUT  -j REJECT ##添加拒绝,请求会回应

iptables -D INPUT 4

iptables -nL

iptables -N redhat ##添加链

iptables -nL

 

 

 

iptables -E redhat linux ##修改链

iptables -nL

iptables -X linux ##删除链

iptables -nL

 

火墙策略优化

iptables -F

service iptables save

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

iptables -A INPUT -m state --state NEW -i lo -j ACCEPT

iptables -A INPUT -m state --state NEW -p tcp --dport 8080 -j ACCEPT

iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -j REJECT

iptables -nL

 

所有策略再次使用时会直接使用第一条提升速度

 

源地址的伪装和转换

 

你访问别人的时候你从双卡那进去再从另一个口出去,出去之前伪装地址,在系统决定了数据的路由之后

172.25.55.2访问172.25.254.这个网段的其他ip时我们需要通过双卡路由从172.25.55.1进去从172.25.254.155出去

所以172.25.44.2的网关必须为172.25.44.1

 

iptables -t nat -nL

ifconfig

iptables -t nat -A POSTROUTING  -o eth0       -j SNAT --to-source 172.25.254.144

                 添加路由后策略   eth0接口进去  改变转发数据包的源地址

sysctl -a | grep ip_forward

vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

sysctl -p

iptables -nL -t nat

测试

172.25.55.2主机可以ping通 172.25.254.55

 

 

你访问我的时候我就让你去访问别人,在路由之前更改你的地址伪装

当任何其他ip访问172.25.254.155时我都指定它去访问172.25.55.2

 

iptables -t nat  -A PREROUTING -i eth0 -d 172.25.254.155 -j DNAT --to-dest 172.25.55.2

                                                         改变转发数据包的目的地

iptables -nL -t nat

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值