# 网络安全基础知识：如何设置防火墙

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/10030.html | 发布：2022-09-15 | 更新：2022-09-15 | 分类：IDC云库

为什么防火墙如此重要？开放 Internet 上的安全性每天都变得越来越重要。随着互联网和互联网素养的增长，现在全世界都可以感受到专用/虚拟服务器的好处。

为什么防火墙如此重要？开放 Internet 上的安全性每天都变得越来越重要。随着互联网和互联网素养的增长，现在全世界都可以感受到专用/虚拟服务器的好处。

 

![如果您有路由器，您的企业是否需要防火墙？](https://www.mfisp.com/wp-content/uploads/2022/08/20220809105948RNbuT6.png)

 

对于许多人来说，个人数据和 Web 服务的可访问性已成为日常生活中不可或缺的一部分。具有可访问性的好处意味着该服务是面向公众的，这使得该服务容易受到不良和看似随机的连接的影响。

 

通常使用机器人和欺骗性 IP 地址进行，在开放的 Internet 上体验登录尝试、端口扫描和其他侵入性活动的情况并不少见。有一些基本的安全和防火墙实践可以帮助防止这些活动变成更令人担忧的问题。

 

## 如果没有防火墙，您的开放端口如下所示：

 

首先，为了帮助掌握这些连接背后的动机，我们使用了一台新安装的服务器来记录 2 天内的传入连接。在没有防火墙阻止与服务器的连接的情况下，可以分析日志数据以查明集中区域。

 

### 技术信息

 

操作系统：CentOS 7 + cPanel

 

（禁用 cPHulk）

 

– 使用 iptables 记录连接，并记录到以下目录 –

 

`/etc/rsyslog.d/my_iptables.conf`

 

`:msg,contains,"[netfilter] " /var/log/iptables.log`

 

以下 iptables 规则用于将 NEW(state) 入站数据包记录到 eth0

 

`iptables -A INPUT -i eth0 -m state --state NEW -j LOG --log-prefix='[netfilter] '`

 

### 示例日志条目

 

`Jun 15 08:02:27 gigenet kernel: [netfilter] IN=eth0 OUT= MAC=d6:f4:8e:aa:a7:94:00:25:90:0a:ad:1c:08:00 SRC=<remote IP> DST=<server IP> LEN=40 TOS=0x00 PREC=0x00 TTL=244 ID=24288 PROTO=TCP SPT=54102 DPT=1433 WINDOW=1024 RES=0x00 SYN URGP=0`

 

（IP 地址已被删除）

 

SRC – 源 IP 地址

 

DST – 目标 IP 地址

 

SPT——源端口

 

DPT – 目标端口

 

PROTO——互联网协议

 

### 创建了一个脚本来分析和格式化日志数据

 

`[root@gigenet ~]# ./analyze-iptableslog.sh`

 

日志文件：iptables-1.log

 

记录日期

 

`# awk 'NR==1{print "Start Date: " $1, $2, $3;}; END{print "End Date: " $1, $2, $3;}' iptables-1.log`

 

开始日期：6月13日08:02:21

 

结束日期：6月15日08:02:27

 

### 记录的新连接总数

 

`# wc -l iptables-1.log`  
 `16299 iptables-1.log`

 

### 每个协议的连接数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/PROTO=/) print $i}' iptables-1.log | sort | uniq -c | sort -rn`  
 `15900 PROTO=TCP`  
 `366 PROTO=UDP`  
 `33 PROTO=ICMP`

 

### 唯一 SRC IP 地址数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/SRC=/) print $i}' iptables-1.log | sort -n | uniq | wc -l`  
 `2886 IP Addresses`

 

### 具有 DPT 的实体数（Total-ICMP）

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-1.log | wc -l`  
 `16266 DPT Connections`

 

### 唯一 DPT 命中数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-1.log | sort -n | uniq | wc -l`  
 `1531 Unique DPT`

 

### 每个 DPT 的连接数，前 15 名

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-1.log | sort -n | uniq -c | sort -rn | head -n 15`  
 `9595 DPT=22`  
 `1309 DPT=80`  
 `885 DPT=445`  
 `742 DPT=23`  
 `188 DPT=8000`  
 `157 DPT=1433`  
 `153 DPT=5060`  
 `111 DPT=8080`  
 `90 DPT=8545`  
 `90 DPT=3389`  
 `83 DPT=81`  
 `80 DPT=3306`  
 `73 DPT=443`  
 `67 DPT=2323`  
 `44 DPT=8888`

 

## **如何使用防火墙缓解端口**

 

数据显示了主要的联系目的地港口。正如预期的那样，具有最大连接量的端口对于 Linux 和 Windows Web 服务很常见。

 

端口 22 – Secure Shell(SSH)  
 端口 23 – telnet  
 端口 80 – Http  
 端口 445 – SMB（Windows 网络文件共享）  
 端口 1433 – MSSQL  
 端口 3306 – MYSQL  
 端口 3389 – RDP

 

根据正在运行的服务，这些端口可能需要对远程服务可用。注意的端口是 SSH 端口 22、telnet 端口 23 和 RDP 端口 3389。

 

理想情况下，系统防火墙应将这些连接仅限于特定 IP 地址。此外，机器人通常被编程为以默认端口为目标。因此，更改默认 SSH 和 RDP 端口将有助于防止入侵。

 
 
1. 更改 SSH 端口（Linux、Freebsd） 
1. SSH 配置文件：  
 `/etc/ssh/sshd_config` 

 

修改不常用端口（0-65535）的行

 
 
- 端口 22 

 

重启 SSHD：

 
 
- CentOS：服务 sshd 重启 
- Debian：服务 ssh 重启 
- FreeBSD：/etc/rc.d/sshd 重启 

 

更改 RDP 端口（Windows）

 
 
- Windows RDP 永远不应向公众开放。如有必要，应更改 RPD 端口以最小化匿名连接。 

 

打开注册表编辑器

 
 
- 找到以下注册表子项：  
 `HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\PortNumber` 

 

将 Decimal 值修改为未使用的端口，单击 OK。重启。

 

## **基本防火墙设置**

 

有许多防火墙服务可以作为主要的安全模式。提供了一些帮助入门的基本规则命令。

 

### **1.添加iptables规则**

 

iptables 是最常见、最熟悉的 Linux 防火墙。CentOS <=6 的默认防火墙，iptables 经常被用作 Linux 的基线防火墙。

 

基本规则

 
 
- 允许已建立的连接：iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT 
- 允许输入策略：iptables -P INPUT ACCEPT 
- 允许IP：iptables -A INPUT -s 120.0.0.1/32 -j ACCEPT 
- 允许 IP/端口：iptables -A INPUT -s 120.0.0.1/32 -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT 
- 允许 lo(localhost) 接口：iptables -A INPUT -i lo -j ACCEPT 
- 允许 Ping：iptables -A INPUT -p icmp -j ACCEPT 
- 允许端口：iptables -A INPUT -p tcp –dport 22 -j ACCEPT 
- 插入允许 IP（位置 5）：iptables -I INPUT 5 -s 120.0.0.1/32 -j ACCEPT 
- 插入允许 IP/多端口：iptables -I INPUT 5 -s 127.0.0.1/32 -p tcp -m state –state NEW -m multiport –dport port#1,port#2 -j ACCEPT 

 

（或者，将“ACCEPT”替换为“DROP”以拒绝）  
 使用 -D 选项删除现有规则：

 
 
- 删除允许IP：iptables -D INPUT -s 120.0.0.1/32 -j ACCEPT 

 

拒绝其余的，拒绝（阻止）之前规则中未定义的所有连接。

 
 
- iptables -A -j REJECT –reject-with icmp-host-prohibited 

 

冲洗规则

 
 
- iptables -F 

 

## **2. 基本的 firewalld 命令**

 

Firewalld 在 CentOS 7 中具有突出的特点。Firewalld 本质上提供了更多人类可读的命令来提交 iptables 规则。

 

可操作，打印当前状态信息

 
 
- 状态：firewall-cmd –state 
- 启动/停止：systemctl start/stop firewalld.service 
- 开机启动：systemctl enable firewalld 

 

区域信息，pinrt 区域参数

 
 
- 默认区域：firewall-cmd –get-default-zone 
- 默认区域信息：firewall-cmd –list-all 
- 列出区域：firewall-cmd –get-zones 
- 区域信息：firewall-cmd –zone=public –list-all 

 

修改区域

 
 
- 创建新区域：firewall-cmd –permanent –new-zone=new_zone – 更改默认区域：firewall-cmd –set-default-zone=public 
- 更改接口：firewall-cmd –zone=public –change-interface=eth0 

 

修改规则，区域的子网

 
 
- 允许服务：firewall-cmd –zone=public –add-service=http 
- 允许端口：firewall-cmd –zone=public –add-port=22/tcp 
- 列出服务：firewall-cmd –get-services 
- 列出允许的服务：firewall-cmd –zone=public –list-services 
- 列出允许的端口：firewall-cmd –list-ports 
- 允许 IP/Port/Proto 使用富规则、显式规则 
- firewall-cmd –permanent –zone=public –add-rich-rule='rule family=”ipv4″ source address=”127.0.0.1/32″ port protocol=”tcp” port=”22″ accept' 

 

（使用 –permanent 选项为重新启动创建持久规则）

 

### **3.基本的ufw规则**

 

作为 ufw(UncomplicatedFirewall) 引入，在 Ubuntu 8.04+ 中受支持，它作为 Ubuntu 系统的默认防火墙提供。

 

操作

 
 
- 启用/禁用：ufw 启用/禁用 
- 打印规则：ufw status verbose 

 

允许规则

 
 
- 允许端口：ufw allow 22 
- 允许 IP：ufw 允许来自 127.0.0.1 
- Allow IP/Port/TCP: ufw allow from 127.0.0.1 to any port 22 proto tcp 
- （或者，用“允许”代替“拒绝”来代替拒绝规则） 

 

删除现有规则

 
 
- ufw 从 127.0.0.1 删除允许 

 

### **4. Windows 防火墙（Windows Server 2008 a newer）**

 

**控制面板 >> Windows 防火墙 >> 高级设置 >> 入站/出站 >> 新规则**

 

### 奖励：cPanel 工具 – cpHulk(?)

 

作为测试用例，WHM 的 cPHulk Bruteforce Protection 使用默认设置启用。在记录的 24 小时内，iptables 记录的新连接明显减少。

 

`[root@gigenet ~]# ./analyze-iptableslog.sh`

 

日志文件：iptables-cphulk.log

 

记录日期

 

`# awk 'NR==1{print "Start Date: " $1, $2, $3;}; END{print "End Date: " $1, $2, $3;}' iptables-cphulk.log`

 

开始日期：6月19日04:31:43

 

结束日期：6月20日04:53:53

 

### 记录的新连接总数

 

`# wc -l iptables-cphulk.log`  
 `3223 iptables-cphulk.log`

 

### 每个协议的连接数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/PROTO=/) print $i}' iptables-cphulk.log | sort | uniq -c | sort -rn`  
 `2974 PROTO=TCP`  
 `213 PROTO=UDP`  
 `36 PROTO=ICMP`

 

### 唯一 SRC IP 地址数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/SRC=/) print $i}' iptables-cphulk.log | sort -n | uniq | wc -l`  
 `1432 IP Addresses`

 

### 具有 DPT 的实体数（Total-ICMP）

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-cphulk.log | wc -l`  
 `3187 DPT Connections`

 

### 唯一 DPT 命中数

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-cphulk.log | sort -n | uniq | wc -l`  
 `943 Unique DP`

 

### 每个 DPT 的连接数，前 15 名

 

`# awk '{for (i=1;i<=NF;i++) if( ~/DPT=/) print $i}' iptables-cphulk.log | sort -n | uniq -c | sort -rn | head -n 15`  
 `415 DPT=445`  
 `270 DPT=23`  
 `257 DPT=22`  
 `233 DPT=80`  
 `97 DPT=5060`  
 `72 DPT=1433`  
 `59 DPT=8545`  
 `53 DPT=8000`  
 `50 DPT=81`  
 `49 DPT=8080`  
 `46 DPT=443`  
 `41 DPT=3389`  
 `34 DPT=25`  
 `33 DPT=3306`  
 `27 DPT=2323`

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
