Skip to content

Neo4j 防火墙配置

Neo4j端口配置

核心端口

Neo4j使用的核心端口包括:

端口协议用途访问控制
7474HTTP浏览器访问和HTTP API限制内部网络访问
7473HTTPS加密的浏览器访问和HTTP API可对外暴露
7687BoltBolt协议访问限制应用服务器访问
5000TCP集群发现(Causal Clustering)限制集群内部访问
6000TCP集群通信(Raft协议)限制集群内部访问

端口配置示例

在neo4j.conf中配置Neo4j端口:

txt
# neo4j.conf 端口配置
dbms.connectors.default_listen_address=0.0.0.0

dbms.connector.http.enabled=true
dbms.connector.http.listen_address=:7474
dbms.connector.http.advertised_address=:7474

dbms.connector.https.enabled=true
dbms.connector.https.listen_address=:7473
dbms.connector.https.advertised_address=:7473

dbms.connector.bolt.enabled=true
dbms.connector.bolt.listen_address=:7687
dbms.connector.bolt.advertised_address=:7687

dbms.cluster.discovery.endpoints=:5000
dbms.cluster.raft.listen_address=:6000
dbms.cluster.raft.advertised_address=:6000

Linux防火墙配置

iptables配置

查看当前规则

bash
# 查看当前iptables规则
iptables -L -n -v

# 查看nat表规则
iptables -t nat -L -n -v

基本防火墙规则

bash
# 清空现有规则
iptables -F
iptables -X
iptables -Z

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Neo4j端口规则

bash
# 允许内部网络访问HTTP端口
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 7474 -j ACCEPT

# 允许外部访问HTTPS端口
iptables -A INPUT -p tcp --dport 7473 -j ACCEPT

# 允许应用服务器访问Bolt端口
iptables -A INPUT -s 10.0.0.0/24 -p tcp --dport 7687 -j ACCEPT

# 允许集群内部访问发现端口
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 5000 -j ACCEPT

# 允许集群内部访问Raft端口
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 6000 -j ACCEPT

# 允许SSH访问
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

保存规则

bash
# 保存规则到文件
iptables-save > /etc/iptables/rules.v4

# 配置开机自动加载规则
echo "iptables-restore < /etc/iptables/rules.v4" >> /etc/rc.local
chmod +x /etc/rc.local

firewalld配置

查看当前状态

bash
# 查看firewalld状态
systemctl status firewalld

# 查看当前区域
firewall-cmd --get-active-zones

# 查看当前规则
firewall-cmd --list-all

基本配置

bash
# 启动firewalld
systemctl start firewalld

# 设置开机启动
systemctl enable firewalld

# 切换到public区域
firewall-cmd --set-default-zone=public

添加Neo4j端口规则

bash
# 允许内部网络访问HTTP端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="7474" accept'

# 允许外部访问HTTPS端口
firewall-cmd --permanent --add-port=7473/tcp

# 允许应用服务器访问Bolt端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="7687" accept'

# 允许集群内部访问发现端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="5000" accept'

# 允许集群内部访问Raft端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="6000" accept'

# 允许SSH访问
firewall-cmd --permanent --add-service=ssh

# 重新加载规则
firewall-cmd --reload

查看配置结果

bash
# 查看已添加的规则
firewall-cmd --list-rich-rules
firewall-cmd --list-ports
firewall-cmd --list-services

Windows防火墙配置

图形界面配置

  1. 打开Windows Defender防火墙
  2. 点击高级设置
  3. 选择入站规则
  4. 点击新建规则
  5. 选择端口,点击下一步
  6. 选择TCP,输入端口号,点击下一步
  7. 选择允许连接,点击下一步
  8. 选择适用的网络类型,点击下一步
  9. 输入规则名称,点击完成

PowerShell配置

查看当前规则

powershell
# 查看入站规则
Get-NetFirewallRule -Direction Inbound | Where-Object {$_.Enabled -eq $true}

# 查看特定端口规则
Get-NetFirewallPortFilter | Where-Object {$_.LocalPort -eq 7687}

添加Neo4j端口规则

powershell
# 允许内部网络访问HTTP端口
New-NetFirewallRule -DisplayName "Neo4j HTTP" -Direction Inbound -Protocol TCP -LocalPort 7474 -RemoteAddress 192.168.1.0/24 -Action Allow -Enabled True

# 允许外部访问HTTPS端口
New-NetFirewallRule -DisplayName "Neo4j HTTPS" -Direction Inbound -Protocol TCP -LocalPort 7473 -Action Allow -Enabled True

# 允许应用服务器访问Bolt端口
New-NetFirewallRule -DisplayName "Neo4j Bolt" -Direction Inbound -Protocol TCP -LocalPort 7687 -RemoteAddress 10.0.0.0/24 -Action Allow -Enabled True

# 允许集群内部访问发现端口
New-NetFirewallRule -DisplayName "Neo4j Cluster Discovery" -Direction Inbound -Protocol TCP -LocalPort 5000 -RemoteAddress 192.168.1.0/24 -Action Allow -Enabled True

# 允许集群内部访问Raft端口
New-NetFirewallRule -DisplayName "Neo4j Raft" -Direction Inbound -Protocol TCP -LocalPort 6000 -RemoteAddress 192.168.1.0/24 -Action Allow -Enabled True

删除规则

powershell
# 删除规则
Remove-NetFirewallRule -DisplayName "Neo4j HTTP"

集群防火墙配置

Causal Clustering防火墙规则

核心节点间通信

bash
# 允许核心节点间的所有通信
for node in 192.168.1.101 192.168.1.102 192.168.1.103; do
  iptables -A INPUT -s $node -p tcp --dport 5000 -j ACCEPT
  iptables -A INPUT -s $node -p tcp --dport 6000 -j ACCEPT
  iptables -A INPUT -s $node -p tcp --dport 7687 -j ACCEPT
done

只读副本与核心节点通信

bash
# 允许只读副本访问核心节点
for replica in 192.168.1.104 192.168.1.105; do
  # 在核心节点上执行
  iptables -A INPUT -s $replica -p tcp --dport 7687 -j ACCEPT
  iptables -A INPUT -s $replica -p tcp --dport 5000 -j ACCEPT

done

跨数据中心集群配置

对于跨数据中心部署的集群,需要配置防火墙允许数据中心间的通信:

bash
# 允许数据中心间的集群通信
iptables -A INPUT -s 10.1.0.0/24 -p tcp --dport 5000 -j ACCEPT
iptables -A INPUT -s 10.1.0.0/24 -p tcp --dport 6000 -j ACCEPT
iptables -A INPUT -s 10.1.0.0/24 -p tcp --dport 7687 -j ACCEPT

云环境防火墙配置

AWS Security Groups

创建安全组

  1. 登录AWS控制台,进入EC2服务
  2. 点击安全组,然后点击创建安全组
  3. 输入安全组名称和描述
  4. 选择VPC
  5. 添加入站规则:
    • HTTP (7474):只允许内部网络访问
    • HTTPS (7473):允许所有IP访问
    • Custom TCP (7687):只允许应用服务器安全组访问
    • Custom TCP (5000-6000):只允许集群安全组访问
  6. 添加出站规则:允许所有流量
  7. 点击创建安全组

关联安全组

将创建的安全组关联到Neo4j实例:

  1. 进入EC2实例列表
  2. 选择Neo4j实例,点击操作 > 安全 > 更改安全组
  3. 选择创建的安全组,点击保存

Azure Network Security Groups

创建NSG

  1. 登录Azure门户,进入网络安全组
  2. 点击创建
  3. 输入名称和资源组
  4. 选择位置
  5. 点击创建

添加安全规则

  1. 选择创建的NSG
  2. 点击入站安全规则
  3. 点击添加
  4. 添加以下规则:
    • 名称:Neo4j-HTTP,端口:7474,源:内部网络
    • 名称:Neo4j-HTTPS,端口:7473,源:任意
    • 名称:Neo4j-Bolt,端口:7687,源:应用服务器子网
    • 名称:Neo4j-Cluster,端口:5000-6000,源:集群子网
  5. 点击添加

关联NSG

将NSG关联到Neo4j子网或网络接口。

GCP Firewall Rules

创建防火墙规则

  1. 登录GCP控制台,进入VPC网络 > 防火墙
  2. 点击创建防火墙规则
  3. 输入名称和描述
  4. 选择网络
  5. 选择方向:入站
  6. 选择行动:允许
  7. 选择目标:指定的目标标签
  8. 添加目标标签:neo4j-server
  9. 添加源IP范围
  10. 添加协议和端口
  11. 点击创建

应用标签

将标签应用到Neo4j实例:

  1. 进入Compute Engine > VM实例
  2. 选择Neo4j实例,点击编辑
  3. 网络标签中添加neo4j-server
  4. 点击保存

防火墙日志与监控

Linux防火墙日志

iptables日志

bash
# 启用iptables日志
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "[IPT] DROP: " --log-level 7

# 查看日志
cat /var/log/kern.log | grep "[IPT]"

firewalld日志

bash
# 启用firewalld日志
firewall-cmd --set-log-denied=all

# 查看日志
journalctl -k | grep -i reject

监控防火墙活动

使用工具监控防火墙活动:

  • tcpdump:实时捕获网络流量

    bash
    tcpdump -i eth0 port 7687
  • wireshark:图形化网络分析工具

  • fail2ban:防止暴力破解

    bash
    # 安装fail2ban
    apt-get install fail2ban
    
    # 配置fail2ban监控Neo4j端口
    cat > /etc/fail2ban/jail.d/neo4j.conf << EOF
    [neo4j]
    enabled = true
    port = 7474,7473,7687
    filter = neo4j
    logpath = /var/log/auth.log
    maxretry = 5
    bantime = 3600
    EOF
    
    # 重启fail2ban
    systemctl restart fail2ban

安全最佳实践

1. 最小化开放端口

只开放必要的端口,关闭不使用的服务:

txt
# neo4j.conf 禁用不必要的服务
dbms.connector.http.enabled=false
dbms.connector.https.enabled=true
dbms.connector.bolt.enabled=true

2. 使用SSL/TLS加密

启用SSL/TLS加密所有通信:

txt
# neo4j.conf 启用SSL/TLS
dbms.connector.bolt.tls_level=REQUIRED
dbms.connector.https.enabled=true
dbms.ssl.policy.bolt.enabled=true
dbms.ssl.policy.https.enabled=true

3. 限制访问IP

只允许可信IP访问Neo4j:

bash
# iptables只允许特定IP访问
iptables -A INPUT -s 10.0.0.10 -p tcp --dport 7687 -j ACCEPT
iptables -A INPUT -s 10.0.0.11 -p tcp --dport 7687 -j ACCEPT
iptables -A INPUT -p tcp --dport 7687 -j DROP

4. 网络分段

将Neo4j放在独立的网络段,与其他服务隔离。

5. 定期审查防火墙规则

每月审查一次防火墙规则,移除不再需要的规则:

bash
# 列出所有规则
iptables -L -n -v

# 移除过期规则
iptables -D INPUT <rule-number>

6. 使用VPN访问

对于远程管理,使用VPN而非直接暴露管理端口:

bash
# 只允许VPN IP访问管理端口
iptables -A INPUT -s 172.16.0.0/16 -p tcp --dport 7474 -j ACCEPT
iptables -A INPUT -p tcp --dport 7474 -j DROP

7. 实现入侵检测

部署入侵检测系统(IDS/IPS),监控异常网络活动:

  • Snort:开源IDS/IPS
  • Suricata:高性能网络安全监控
  • OSSEC:主机入侵检测

常见问题(FAQ)

Q1: 如何测试防火墙规则是否生效?

A1: 测试防火墙规则的方法:

  1. 使用telnet测试端口连通性:
    bash
    telnet <neo4j-server> 7687
  2. 使用nmap扫描开放端口:
    bash
    nmap -p 7474,7473,7687,5000,6000 <neo4j-server>
  3. 从不同IP尝试访问,验证访问控制

Q2: 防火墙配置错误导致无法访问怎么办?

A2: 解决方法:

  1. 如果是云环境,使用控制台的串行控制台或VNC访问
  2. 如果是物理服务器,使用本地控制台访问
  3. 暂时关闭防火墙,修复规则后重新启用
  4. 恢复之前的防火墙配置

Q3: 如何配置防火墙允许所有集群节点通信?

A3: 配置方法:

  1. 将所有集群节点放在同一子网
  2. 创建集群安全组或防火墙规则,允许该子网内的所有通信
  3. 或者明确允许每个节点的IP访问所有集群端口

Q4: 启用SSL后还需要防火墙吗?

A4: 是的,SSL加密通信,但防火墙可以:

  1. 限制谁可以发起连接
  2. 防止DDoS攻击
  3. 记录访问尝试
  4. 隔离网络分段 SSL和防火墙是互补的安全措施。

Q5: 如何处理大量并发连接?

A5: 处理大量并发连接的方法:

  1. 调整防火墙连接跟踪设置
  2. 增加防火墙性能(使用硬件防火墙)
  3. 优化Neo4j连接池配置
  4. 实现连接负载均衡

Q6: 如何监控防火墙性能?

A6: 监控防火墙性能的指标:

  1. 连接数:当前活跃连接数
  2. 吞吐量:每秒处理的数据包数
  3. CPU使用率:防火墙设备的CPU使用率
  4. 内存使用率:防火墙设备的内存使用率
  5. 规则匹配时间:处理规则的时间

Q7: 如何备份防火墙配置?

A7: 备份防火墙配置的方法:

  • iptablesiptables-save > iptables-backup.txt
  • firewalldfirewall-cmd --list-all-zones > firewalld-backup.txt
  • Windows:使用PowerShell导出:Export-NetFirewallRule -FileName firewalL-backup.xml
  • 云环境:使用云提供商的API或控制台导出配置

Q8: 如何实现自动化防火墙管理?

A8: 自动化防火墙管理的工具:

  • Ansible:配置管理工具,可自动化防火墙规则
  • Puppet:基础设施即代码,可管理防火墙配置
  • Chef:自动化配置管理
  • Terraform:基础设施即代码,可管理云防火墙
  • FirewallD Ansible Module:专门用于管理firewalld