插件窝 干货文章 如何使用Linux服务器保护Web接口免受恶意请求?

如何使用Linux服务器保护Web接口免受恶意请求?

Prometheus 配置 ModSecurity api 99    来源:    2025-04-12

保护Linux服务器Web接口免受恶意请求的解决方案

作为IT工程师,我为您提供一套全面的Web接口安全防护方案,适用于Linux服务器环境。

1. Web服务器层防护

Nginx防护配置

# 限制请求速率
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/s;

# 限制连接数
limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
    location /api/ {
        limit_req zone=api_limit burst=200 nodelay;
        limit_conn addr 20;

        # 其他防护配置...
    }
}

Apache防护配置

# 启用mod_evasive防DDoS
<IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 5
    DOSSiteCount 100
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 60
</IfModule>

2. 应用防火墙(WAF)解决方案

安装ModSecurity

# 对于Nginx
sudo apt install libmodsecurity3 modsecurity-crs
sudo cp /usr/share/modsecurity-crs/rules/* /etc/nginx/modsec/

# 配置Nginx启用ModSecurity
load_module modules/ngx_http_modsecurity_module.so;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;

使用Cloudflare或类似CDN服务

  • 启用Web应用防火墙(WAF)
  • 配置速率限制规则
  • 启用DDoS防护

3. API网关防护

使用Kong API网关

# 安装Kong
sudo apt update
sudo apt install -y kong

# 配置速率限制插件
curl -X POST http://localhost:8001/plugins \
    --data "name=rate-limiting" \
    --data "config.second=10" \
    --data "config.hour=10000"

4. 系统层防护

配置iptables/nftables防火墙

# 限制单个IP的连接数
sudo iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP

# 防止SYN洪水攻击
sudo iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT

安装Fail2Ban

sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# 编辑jail.local配置
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log

5. 应用层防护措施

实现API密钥验证

# Flask示例
from flask import Flask, request, abort
import os

app = Flask(__name__)
API_KEYS = os.getenv('API_KEYS', '').split(',')

@app.before_request
def check_api_key():
    if request.endpoint != 'static':
        api_key = request.headers.get('X-API-KEY')
        if api_key not in API_KEYS:
            abort(403)

输入验证和消毒

// Node.js示例 - 使用Joi进行输入验证
const Joi = require('joi');

const schema = Joi.object({
    username: Joi.string().alphanum().min(3).max(30).required(),
    email: Joi.string().email(),
    birth_year: Joi.number().integer().min(1900).max(2023)
});

6. 监控和日志分析

安装ELK Stack进行日志分析

# 安装Elasticsearch, Logstash和Kibana
sudo apt install elasticsearch logstash kibana

# 配置Nginx日志发送到Logstash
input {
    file {
        path => "/var/log/nginx/access.log"
        start_position => "beginning"
    }
}

使用Prometheus和Grafana监控

# 安装Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.*-amd64.tar.gz
tar xvfz prometheus-*.*-amd64.tar.gz
cd prometheus-*.*-amd64
./prometheus --config.file=prometheus.yml

最佳实践建议

  1. 定期更新:保持所有软件包和依赖项更新到最新版本
  2. 最小权限原则:Web服务应以非root用户身份运行
  3. HTTPS强制:使用Let's Encrypt免费SSL证书
  4. API版本控制:便于在不中断服务的情况下修复安全问题
  5. 敏感数据保护:永远不要将API密钥或凭据提交到版本控制
  6. 定期审计:使用工具如Lynis进行系统安全审计

通过实施这些多层次的安全措施,您可以显著提高Web接口的安全性,抵御大多数常见的恶意请求攻击。