# 防火墙准入与拦截技术文档
1. 引言
防火墙是网络安全体系中的核心组件,负责监控和控制网络流量。本技术文档详细介绍了通过防火墙实现程序联网控制和端口管理的技术方案,包含Windows和Linux平台的具体实现方法。
2. 程序联网控制 2.1 Windows平台实现
方法1:使用Windows Defender防火墙(图形界面)
1. 打开"高级安全Windows Defender防火墙"
2. 选择"出站规则" > "新建规则"
3. 选择"程序" > 浏览目标应用程序路径
4. 选择"阻止连接"
5. 应用所有网络类型(域/专用/公共)
6. 命名规则(如"Block_WeChat_Outbound")
方法2:使用PowerShell命令
```powershell
# 阻止特定程序出站
New-NetFirewallRule -DisplayName "Block_Chrome" -Direction Outbound `
-Program "C:\Program Files\Google\Chrome\Application\chrome.exe" -Action Block
# 允许特定程序入站
New-NetFirewallRule -DisplayName "Allow_Teams" -Direction Inbound `
-Program "C:\Program Files\Microsoft Teams\current\Teams.exe" -Action Allow
# 查看所有规则
Get-NetFirewallRule | Format-Table DisplayName,Enabled,Action
```
2.2 Linux平台实现(使用iptables)
```bash
# 阻止特定程序访问网络
sudo iptables -A OUTPUT -m owner --uid-owner skypeuser -j DROP
# 允许特定程序访问特定端口
sudo iptables -A OUTPUT -p tcp --dport 443 -m owner --uid-owner chromeuser -j ACCEPT
# 永久保存规则(Ubuntu/Debian)
sudo netfilter-persistent save
```
---
3. 端口管理
3.1 关闭端口
Windows平台:
```powershell
# 关闭TCP 445端口(SMB)
New-NetFirewallRule -DisplayName "Block_TCP_445" -Direction Inbound `
-Protocol TCP -LocalPort 445 -Action Block
# 关闭UDP 137-138端口(NetBIOS)
New-NetFirewallRule -DisplayName "Block_NetBIOS" -Direction Inbound `
-Protocol UDP -LocalPort 137-138 -Action Block
```
Linux平台:
```bash
# 关闭TCP 23端口(Telnet)
sudo iptables -A INPUT -p tcp --dport 23 -j DROP
# 关闭UDP 1900端口(UPnP)
sudo iptables -A INPUT -p udp --dport 1900 -j DROP
# 使用nftables(新版本推荐)
sudo nft add rule inet filter input tcp dport 23 drop
```
3.2 开放端口
Windows平台:
```powershell
# 开放TCP 80端口(HTTP)
New-NetFirewallRule -DisplayName "Allow_HTTP" -Direction Inbound `
-Protocol TCP -LocalPort 80 -Action Allow
# 开放UDP 53端口(DNS)
New-NetFirewallRule -DisplayName "Allow_DNS" -Direction Outbound `
-Protocol UDP -RemotePort 53 -Action Allow
```
Linux平台:
```bash
# 开放TCP 443端口(HTTPS)
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 开放SSH端口(仅限特定IP)
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
4. 高级防火墙策略
4.1 应用程序白名单模式(Windows)
```powershell
# 1. 启用默认阻止策略
Set-NetFirewallProfile -DefaultOutboundAction Block
# 2. 添加允许的程序
New-NetFirewallRule -DisplayName "Allow_Edge" -Direction Outbound `
-Program "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" -Action Allow
New-NetFirewallRule -DisplayName "Allow_Updates" -Direction Outbound `
-Program "C:\Windows\System32\svchost.exe" -Service wuauserv -Action Allow
4.2 基于时间的访问控制(Linux)
```bash
# 仅允许工作时间(9:00-18:00)访问HTTP
sudo iptables -A INPUT -p tcp --dport 80 -m time --timestart 09:00 --timestop 18:00 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j DROP
# 周末禁止游戏程序访问网络
sudo iptables -A OUTPUT -m owner --uid-owner steamuser -m time --weekdays Sat,Sun -j DROP
4.3 端口转发与重定向(Linux)
```bash
# 将外部8080端口转发到内部服务器的80端口
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80
sudo iptables -A FORWARD -p tcp -d 192.168.1.100 --dport 80 -j ACCEPT
5. 防火墙管理最佳实践
1. **最小权限原则**:只开放必要的端口和服务
2. **分层防御**:结合网络边界防火墙和主机防火墙
3. **日志监控**:
```powershell
# Windows查看防火墙日志
Get-WinEvent -FilterHashtable @{LogName='Security'; ID=5152,5157} | Format-List
```
```bash
# Linux查看iptables日志
sudo tail -f /var/log/kern.log | grep iptables
```
4. **定期审计**:每月检查防火墙规则有效性
5. **变更管理**:所有规则变更需通过审批流程
6. **默认拒绝策略**:
```bash
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT DROP
6. 故障排除
常见问题排查步骤:
1. 检查规则顺序:`sudo iptables -L -n --line-numbers`
2. 验证规则生效:`sudo iptables -S`
3. 测试端口连通性:
```powershell
Test-NetConnection -ComputerName 127.0.0.1 -Port 80
```
```bash
nc -zv localhost 22
```
4. 检查进程绑定:
```bash
sudo netstat -tulnp
```
5. 查看拒绝日志:
```bash
sudo dmesg | grep DROP
7. 附录
Windows常用命令:
```powershell
# 禁用规则
Disable-NetFirewallRule -DisplayName "Block_Chrome"
# 删除规则
Remove-NetFirewallRule -DisplayName "Block_Chrome"
# 导出配置
Export-NetFirewallRule -PolicyStore ActiveStore -FilePath "firewall_rules.txt"
Linux常用命令:
```bash
# 保存iptables规则
sudo iptables-save > /etc/iptables/rules.v4
# 恢复规则
sudo iptables-restore < /etc/iptables/rules.v4
# 查看连接跟踪
sudo conntrack -L
端口对照表:
| 端口 | 协议 | 服务 | 风险等级 |
|------|------|------------|----------|
| 22 | TCP | SSH | 中 |
| 23 | TCP | Telnet | 高 |
| 445 | TCP | SMB | 严重 |
| 3389 | TCP | RDP | 高 |
| 5900 | TCP | VNC | 高 |