当前位置: 首页 > news >正文

内网服务器安全管控(堡垒机 + 防火墙)实战项目

堡垒机和防火墙综合项目

一、准备环境:

3台centos7.9的虚拟机,2G内存/1核cpu/20Gc磁盘

防火墙服务器 2块网卡/2G内存/1核cpu/20G磁盘 WAN:192.168.128.135 LAN:192.168.6.133 SSH:5566
堡垒机 1块网卡/2G内存/1核cpu/20G磁盘 IP:192.168.6.130Gateway:192.168.6.133 SSH: 8899
Web服务器 1块网卡/2G内存/1核cpu/20G磁盘 IP:192.168.6.131Gateway:192.168.6.133 SSH:6677

注:防火墙两块网卡,一块ens33采用NAT模式,一块ens36采用hostonly模式,其他的两台也采用hostonly模式

二、实验步骤:

1.安装好3台centos7的linux系统,并且配置好ip地址和主机名

1.1 先从防火墙服务器开始配置

1.1.1修改主机名
[root@localhost ~]# hostnamectl set-hostname  firewall[root@localhost ~]# su
1.1.2配置2块网卡的ip地址,第1块WAN口的ip地址保存默认,只是从动态获得修改为静态配置
[root@firewall ~]# cd /etc/sysconfig/network-scripts/[root@firewall network-scripts]# lsifcfg-ens33  ifdown-eth  ifdown-ppp    ifdown-tunnel  ifup-ippp  ifup-post   ifup-TeamPort    network-functions-ipv6ifcfg-ens34  ifdown-ippp  ifdown-routes   ifup      ifup-ipv6  ifup-ppp   ifup-tunnelifcfg-lo   ifdown-ipv6  ifdown-sit    ifup-aliases  ifup-isdn  ifup-routes  ifup-wirelessifdown    ifdown-isdn  ifdown-Team    ifup-bnep    ifup-plip  ifup-sit   init.ipv6-globalifdown-bnep  ifdown-post  ifdown-TeamPort  ifup-eth    ifup-plusb  ifup-Team   network-functions
1.1.3配置好第1块网卡的ip地址、子网掩码、网关和dns服务器地址
#查看两个网卡的UUIDnmcli connection show[root@firewall network-scripts]# vim ifcfg-ens33BOOTPROTO="none"NAME="ens33"UUID="1dd5fb76-8c91-43d8-ae43-f921aab4f836"DEVICE="ens33"ONBOOT="yes"IPADDR=192.168.128.135PREFIX=24GATEWAY=192.168.128.2DNS1=114.114.114.114
1.1.4编辑ens36网卡配置文件,设置好ip地址和子网掩码(LAN口不用配置网关)
#查看正确 UUID
nmcli connection show [root@firewall network-scripts]# uuidgen  (如果没有UUID就生成一个,一般来说应该有)f5f1544b-3d2-4750-b58ce-4548d9368c8[root@firewall network-scripts]# vim ifcfg-ens36BOOTPROTO=noneNAME=ens36UUID=f5f1544b-3d2-4750-b58ce-4548d9368c8DEVICE=ens36ONBOOT=yesIPADDR=192.168.6.133PREFIX=24
1.1.6刷新网络服务(需要禁用NetworkManager服务,不然会和network服务冲突)
[root@firewall ~]# systemctl stop NetworkManager[root@firewall ~]# systemctl disable NetworkManager[root@firewall ~]# systemctl enable network
[root@firewall ~]# systemctl start network

解释注解:

一、核心规则(二选一,不要混用)

1.传统 network 服务 service network restart /etc/sysconfig/network-scripts/ifcfg-xxx 必须禁用 NetworkManager

2.NetworkManager 服务 nmcli 系列命令 同上(或直接 nmcli modify) 必须启用 NetworkManager

[root@firewall network-scripts]# service network restartRestarting network (via systemctl):             [  确定  ]#查看ip地址
[root@firewall network-scripts]# ip add
1.1.7关闭firewalld和selinux
[root@firewall network-scripts]# service firewalld stop[root@firewall network-scripts]# systemctl disable firewalld#临时关闭selinux
[root@firewall network-scripts]# setenforce 0#永久关闭selinux[root@firewall network-scripts]# vim /etc/selinux/config \# This file controls the state of SELinux on the system.\# SELINUX= can take one of these three values:\#   enforcing - SELinux security policy is enforced.\#   permissive - SELinux prints warnings instead of enforcing.\#   disabled - No SELinux policy is loaded.SELINUX=disabled\# SELINUXTYPE= can take one of three values:\#   targeted - Targeted processes are protected,\#   minimum - Modification of targeted policy. Only selected processes are protected. \#   mls - Multi Level Security protection.SELINUXTYPE=targeted

1.2 配置堡垒机的ip地址、主机名,关闭selinux和firewalld

[root@localhost ~]# hostnamectl set-hostname jumpserver[root@localhost ~]# su[root@jumpserver ~]# cd /etc/sysconfig/network-scripts/[root@jumpserver network-scripts]# lsifcfg-ens33  ifdown-ippp  ifdown-routes   ifup      ifup-ipv6  ifup-ppp    ifup-tunnelifcfg-lo   ifdown-ipv6  ifdown-sit    ifup-aliases  ifup-isdn  ifup-routes   ifup-wirelessifdown    ifdown-isdn  ifdown-Team    ifup-bnep   ifup-plip  ifup-sit    init.ipv6-globalifdown-bnep  ifdown-post  ifdown-TeamPort  ifup-eth    ifup-plusb  ifup-Team    network-functionsifdown-eth  ifdown-ppp  ifdown-tunnel   ifup-ippp   ifup-post  ifup-TeamPort  network-functions-ipv6
1.2.1配置ip地址
[root@jumpserver network-scripts]# vi ifcfg-ens33 BOOTPROTO=noneNAME=ens33UUID=c330e6ab-e0c5-443b-ba2e-d290b6804bccDEVICE=ens33ONBOOT=yesIPADDR=192.168.6.130PREFIX=24GATEWAY=192.168.6.254DNS1=114.114.114.114
1.2.2刷新网络服务
[root@jumpserver network-scripts]# service network restart
1.2.3关闭firewalld和selinux
[root@jumpserver ~]# service firewalld stop[root@jumpserver ~]# systemctl disable firewalld[root@jumpserver ~]# setenforce 0[root@jumpserver ~]# vi /etc/selinux/config \# This file controls the state of SELinux on the system.\# SELINUX= can take one of these three values:\#   enforcing - SELinux security policy is enforced.\#   permissive - SELinux prints warnings instead of enforcing.\#   disabled - No SELinux policy is loaded.SELINUX=disabled\# SELINUXTYPE= can take one of three values:\#   targeted - Targeted processes are protected,\#   minimum - Modification of targeted policy. Only selected processes are protected.\#   mls - Multi Level Security protection.SELINUXTYPE=targeted注:建议重启jumpserver[root@jumpserver ~]# reboot

1.3 配置web服务器的ip地址、主机名,关闭selinux和firewalld

[root@localhost ~]# hostnamectl set-hostname web-server[root@localhost ~]# su[root@web-server ~]# cd /etc/sysconfig/network-scripts/[root@web-server network-scripts]# lsifcfg-ens33  ifdown-ippp  ifdown-routes   ifup      ifup-ipv6  ifup-ppp    ifup-tunnelifcfg-lo   ifdown-ipv6  ifdown-sit    ifup-aliases  ifup-isdn  ifup-routes   ifup-wirelessifdown    ifdown-isdn  ifdown-Team    ifup-bnep   ifup-plip  ifup-sit    init.ipv6-globalifdown-bnep  ifdown-post  ifdown-TeamPort  ifup-eth    ifup-plusb  ifup-Team    network-functionsifdown-eth  ifdown-ppp  ifdown-tunnel   ifup-ippp   ifup-post  ifup-TeamPort  network-functions-ipv6[root@web-server network-scripts]# vi ifcfg-ens33 BOOTPROTO=noneNAME=ens33UUID=4a0e0c57-4474-42c9-9d69-80c2d7737b84DEVICE=ens33ONBOOT=yesIPADDR=192.168.6.131PREFIX=24GATEWAY=192.168.6.254DNS1=114.114.114.114
1.3.1刷新网络服务
[root@web-server network-scripts]# service network restart
1.3.2关闭selinux和firewalld
[root@web-server ~]# service firewalld stop[root@web-server ~]# systemctl disable firewalld[root@web-server ~]# setenforce 0[root@web-server ~]# vi /etc/selinux/config \# This file controls the state of SELinux on the system.\# SELINUX= can take one of these three values:\#   enforcing - SELinux security policy is enforced.\#   permissive - SELinux prints warnings instead of enforcing.\#   disabled - No SELinux policy is loaded.SELINUX=disabled\# SELINUXTYPE= can take one of three values:\#   targeted - Targeted processes are protected,\#   minimum - Modification of targeted policy. Only selected processes are protected.\#   mls - Multi Level Security protection.SELINUXTYPE=targeted[root@web-server ~]# reboot

2.在防火墙服务器上配置SNAT和DNAT功能,加固ssh服务

2.1永久开启路由功能

[root@firewall nat]# vim /etc/sysctl.conf  
#添加下面的配置永久开启路由功能
net.ipv4.ip_forward = 1[root@firewall nat]# sysctl -p  让linux内核加载上面的配置net.ipv4.ip_forward = 1

2.2创建存放脚本的一个目录

[root@firewall ~]# mkdir /nat
[root@firewall ~]# cd /nat/
[root@firewall nat]# ls

2.3编写实现snat和dnat的脚本snat_dnat.sh

[root@firewall nat]# vim snat_dnat.sh#!/bin/bash#临时开启路由功能,因为前面已经永久开启了,这是给大家提醒,需要开启路由功能
echo  1  >/proc/sys/net/ipv4/ip_forward#开启SNAT功能
iptables -F 
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables  -t nat  -A POSTROUTING  -s 192.168.6.0/24 -o ens33  -j MASQUERADE#dnat 发布堡垒机
iptables  -t nat  -A PREROUTING  -d 192.168.128.135 -p tcp --dport 9988  -i ens33  -j DNAT --to-destination 192.168.6.130:8899
#dnat 发布web服务的80和443端口
iptables  -t nat  -A PREROUTING  -d 192.168.128.135 -p tcp --dport 80  -i ens33  -j DNAT --to-destination 192.168.6.131:80
iptables  -t nat  -A PREROUTING  -d 192.168.128.135 -p tcp --dport 443  -i ens33  -j DNAT --to-destination 192.168.6.131:443

注:*如果你想加固防火墙(推荐的生产环境配置)*

可以添加更严格的FORWARD规则:

\#!/bin/bash\# 开启路由功能echo 1 > /proc/sys/net/ipv4/ip_forward\# 清空所有规则iptables -Fiptables -Xiptables -t nat -Fiptables -t nat -X\# SNATiptables -t nat -A POSTROUTING -s 192.168.6.0/24 -o ens33 -j MASQUERADE\# DNATiptables -t nat -A PREROUTING -d 192.168.128.135 -p tcp --dport 9988 -i ens33 -j DNAT --to-destination 192.168.6.130:8899iptables -t nat -A PREROUTING -d 192.168.128.135 -p tcp --dport 80 -i ens33 -j DNAT --to-destination 192.168.6.131:80iptables -t nat -A PREROUTING -d 192.168.128.135 -p tcp --dport 443 -i ens33 -j DNAT --to-destination 192.168.6.131:443\# 设置FORWARD默认策略为DROP(更安全)iptables -P FORWARD DROP\# 放行必要的转发流量\# 放行内网访问外网iptables -A FORWARD -s 192.168.6.0/24 -o ens33 -j ACCEPT\# 放行外网访问内网DNAT的服务iptables -A FORWARD -i ens33 -o ens36 -d 192.168.6.130 -p tcp --dport 8899 -j ACCEPTiptables -A FORWARD -i ens33 -o ens36 -d 192.168.6.131 -p tcp --dport 80 -j ACCEPTiptables -A FORWARD -i ens33 -o ens36 -d 192.168.6.131 -p tcp --dport 443 -j ACCEPT\# 放行已建立的连接回包iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT\# 保存规则service iptables save
2.3.1查看规则
[root@firewall nat]# iptables -L -t nat -n

注:防火墙服务器上没有相关的进程去监听9988和80、443端口,防火墙服务器只是检查,然后进行DNAT转发

2.4加固防火墙服务器的ssh服务,修改端口号5566,禁用root用户登录,新建一个专门用户登录防火墙ssh服务的用户fwuser,并且设置密码Sctl@123456

[root@firewall nat]# useradd  fwuser
[root@firewall nat]# echo Sctl@123456|passwd fwuser --stdin
[root@firewall nat]# vim /etc/ssh/sshd_config Port 5566PermitRootLogin no     #禁用root用户登录[root@firewall nat]# getenforce Permissive
2.4.1刷新ssh服务,如果失败建议检查selinux是否禁用
1[root@firewall nat]# service sshd restart
[root@firewall nat]# ps aux|grep sshd  #检查进程
[root@firewall nat]# ss -anplut|grep sshd   #检查端口
2.4.2后面再连接防火墙服务器,请使用用户fwuser,密码是Sctl@123456 连接的端口是5566
2.4.3设置snat_dnat.sh开机启动
[root@firewall nat]# vim /etc/rc.local 
bash  /nat/snat_dnat.sh[root@firewall nat]# pwd
/nat[root@firewall nat]# ls
snat_dnat.sh
2.4.4授予/etc/rc.d/rc.local 文件可执行权限
[root@firewall nat]# chmod  +x  /etc/rc.d/rc.local[root@firewall nat]# reboot

3.加固堡垒机上的ssh服务,启用防火墙功能和web服务器建立免密通道

3.1 加固堡垒机上的ssh服务

[root@jumpserver ~]# cd /etc/ssh/[root@jumpserver ssh]# vi sshd_config Port 8899PermitRootLogin no
3.1.1新建一个普通的用户scjump用于ssh远程登录,密码为Sctl@123456
[root@jumpserver ssh]# useradd scjump[root@jumpserver ssh]# echo "Sctl@123456"|passwd scjump --stdin
3.1.2刷新ssh服务
[root@jumpserver ssh]# service sshd restart

3.2 启用防火墙功能,只开放8899端口

[root@jumpserver ssh]# mkdir  /iptables[root@jumpserver ssh]# cd  /iptables/[root@jumpserver iptables]# vi rule.sh
#!/bin/bash
#开放8899端口
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
#考虑防火墙dnat映射转发到堡垒机的8899端口的数据能进来
iptables -A INPUT -p tcp --dport 8899 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp -j ACCEPT
#考虑的是堡垒机访问web服务器的数据可以回来
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 6677 -j ACCEPT
#设置INPUT链默认规则为DROP
iptables -P INPUT DROP
3.2.1设置防火墙规则开机启动,修改/etc/rc.local文件
[root@jumpserver iptables]# vi /etc/rc.local 
bash /iptables/rule.sh
[root@jumpserver iptables]# pwd
/iptables
[root@jumpserver iptables]# ls
rule.sh
3.2.2授予/etc/rc.d/rc.local 文件可执行权限
[root@jumpserver iptables]# chmod  +x  /etc/rc.d/rc.local
3.2.3重启服务器,验证以上配置是否开机生效
[root@jumpserver iptables]# reboot
3.2.4启动后登录
[scjump@jumpserver ~]$ su - root
[root@jumpserver iptables]# iptables -L -n 
Chain INPUT (policy DROP)
target   prot opt source        destination     
ACCEPT   tcp  --  0.0.0.0/0       0.0.0.0/0       tcp dpt:8899
ACCEPT   icmp --  0.0.0.0/0       0.0.0.0/0      
ACCEPT   tcp  --  0.0.0.0/0       0.0.0.0/0       tcp spt:22
ACCEPT   tcp  --  0.0.0.0/0       0.0.0.0/0       tcp spt:6677
Chain FORWARD (policy ACCEPT)
target   prot opt source        destination     
Chain OUTPUT (policy ACCEPT)
target   prot opt source        destination     以上效果证明配置全部生效

3.3 和web服务器建立免密通道

3.3.1生成密钥对(直接回车就好)
[root@jumpserver ~]# ssh-keygen Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:FtmZcpjAamqqdRZv3GJdsaTr06z8o1yG1XXq0GINByE root@jumpserverThe key's randomart image is:+---[RSA 2048]----+|   ..  E o.  ||    .. = + .  ||   .  * B . o .||   o   * + * o ||  o.  S + = +  ||  o  + + = . +  || o. o * =oo  .  ||.. o o =.o+    ||o    *=..   |+----[SHA256]-----+[root@jumpserver ~]# cd /root/.ssh/[root@jumpserver .ssh]# lsid_rsa  id_rsa.pub
3.3.2上传公钥到web服务器
[root@jumpserver .ssh]# ssh-copy-id  -i  id_rsa.pub  root@192.168.6.131
3.3.3测试堡垒机和web服务器之间的免密通道是否创建成功

[root@jumpserver iptables]# ssh 'root@192.168.6.131'

Last login: Tue Dec 19 14:38:50 2023 from 192.168.6.1

4.在web服务器上安装web服务,配置tcp wrappers,ssh服务的加固

4.1 安装web服务

[root@web-server ~]# yum  install  httpd -y
4.1.1启动httpd服务
[root@web-server ~]# service  httpd  restart
[root@web-server ~]# ps aux|grep httpd
4.1.2查看端口
[root@web-server ~]# ss -anplut|grep httpd
4.1.3在windows机器上进行测试,访问web服务
192.168.6.131

4.2 配置tcp wrappers

[root@web-server ~]# vim  /etc/hosts.allow 
sshd:192.168.6.130,192.168.6.1    #允许堡垒机和本机连接过来
[root@web-server ~]# vim  /etc/hosts.deny 
sshd:ALL
4.2.1在防火墙服务器上测试是否可以ssh到web服务器
[root@firewall nat]# ssh  root@192.168.6.131
ssh_exchange_identification: read: Connection reset by peer连接失败,说明tcp wrappers设置成功

4.3 ssh服务的加固,修改端口号为6677,禁止密码登录

[root@web-server ~]# vim /etc/ssh/sshd_config Port 6677PasswordAuthentication no
4.3.1刷新服务
[root@web-server ~]# service sshd restart

5.验证DNAT功能和堡垒机的功能

5.1 验证DNAT功能,发布的web服务器和堡垒机

#需要在windows的机器上验证
#访问防火墙服务器的WAN口的ip 192.168.128.135的80端口,看能否访问内网的web服务器

image-20260311172304249

以上效果说明DNAT功能发布web服务器成功

5.2 验证堡垒机的功能,能否通过堡垒机ssh登录web服务器

#在防火墙服务器里面DNAT配置开放的是9988端口
#用一台测试机器在shell里面输入防火墙的IP地址,运行ssh远程登录的用户scjump和密码Sctl@123456

image-20260311173005331

image-20260311173054458

看到以上的效果,说明你的DNAT里的堡垒机发布成功

5.2.1切换到root用户,进行ssh连接到web服务器
[scjump@jumpserver ~]$ su - root#登录web服务器,指定端口号为6677
[root@jumpserver ~]# ssh -p 6677  root@192.168.6.131

6.编写一个可以ssh登录其他机器的脚本jump.sh,方便记忆有哪些机器需要登录,以及它们开放的ssh的端口号

6.1编写脚本

[root@jumpserver iptables]# vim  jump.sh
\#!/bin/bash
echo "1. 连接web服务器"
echo "2. 连接redis服务器"
echo "3. 连接mysql服务器"
echo "4. 退出"
read  -p "请输入你的选项,输入1~4之间的数字:"  num
case  $num in
1)ssh -p 6677  root@192.168.5.80;;
2)ssh  root@192.168.5.36;;
3)ssh  root@192.168.5.37;;
*)exit;;
esac

6.2使用脚本

[root@jumpserver iptables]# bash jump.sh 
1. 连接web服务器
2. 连接redis服务器
3. 连接mysql服务器
4. 退出
请输入你的选项,输入1~4之间的数字:1
Last login: Tue Dec 19 16:45:00 2023 from 192.168.5.99
[root@web-server ~]# 注:看到以上效果说明成功
[root@web-server ~]# exit #登出
http://www.jsqmd.com/news/463304/

相关文章:

  • 2026主治医师考试用书排行榜|在职医生备考必看 - 品牌测评鉴赏家
  • 2026工业纯水设备品牌推荐:工业纯水设备/工业软化水设备/洗衣房软化水设备/电镀用纯水设备/选择指南 - 优质品牌商家
  • 暖通设计师必看:从GB 50016到GB 51251,建筑防火防烟规范实战应用指南
  • 3/10nvdia:AI“五层架构”-产业底层逻辑
  • selenium-----(判断的方法)窗口的切换
  • 别再用3389端口了!Nginx反向代理+RDP端口隐藏的5层安全加固方案
  • 本科留学机构:透明服务+优质文书,申请更安心 - 博客湾
  • SpringBoot实战:5分钟搞定JWT接口认证(含AppId/Key/Secret完整流程)
  • 2026主治医师备考交流:如何选择适合自己的辅导课程 - 品牌测评鉴赏家
  • Wireshark抓包实战:5分钟搞懂NAT协议如何隐藏你的内网IP
  • 2026年3月数据分析报告生成工具有哪些?从技术架构到行业落地全面盘点五款主流产品 - 科技焦点
  • UWB-IMU融合定位在无人机导航中的应用:从原理到落地实践
  • 从原理到实践:GNSS差分码偏差(DCB)对定位精度的影响及修正方法
  • 广药集团白云山:以全产业链优势,打造医药健康产业高质量发展标杆 - 包罗万闻
  • 2026 年广东广州打金培训五大公司排名及解析 - 十大品牌榜
  • 主治医师考试资料大揭秘!在职党高效通关就靠这套组合 - 品牌测评鉴赏家
  • 从法律条文到实战案例:一文读懂网络安全基础核心概念
  • 医学考研课程怎么选?淘友实测分享,高性价比款参考 - 品牌测评鉴赏家
  • SCSA认证通关秘籍:60道选择题高频考点解析(附实验考试避坑指南)
  • ios学习路线 -- Swift基础(1)
  • 2026播种机橡塑配件推荐榜:土豆链带拖轮/塑料件配件/塑料植保机械配件/尼龙挂板/异形尼龙件/排种器/排种盒/选择指南 - 优质品牌商家
  • 避坑指南:0.96寸OLED屏幕滚动效果不生效?可能是驱动芯片搞的鬼!
  • Unreal Engine 4核心概念解析:PlayerController——玩家意志与游戏世界的连接桥梁
  • 树莓派软路由Openwrt进阶:Syncthing与ZeroTier内网穿透实战
  • QFileDialog的使用
  • 2026年气泡膜与气泡袋厂家哪家好:专业制造商西安盛邦深度介绍,涵盖珍珠棉袋子、珠光膜信封袋 - 深度智识库
  • Unreal Engine 4核心概念解析:AIController——AI自主决策引擎与行为树实战
  • Docker如何改变应用构建与部署?从Node.js和Java的Dockerfile看统一制品的革命
  • OpenClaw For Windows本地电脑的快速在线安装部署
  • Freerdp实战指南:解锁开源远程桌面的高效连接与安全配置