Centos7安装modsecurity验证防止SQL注入

本文介绍了一种使用mod_security模块及OWASP规则集来检测并防御SQL注入攻击的方法。通过配置特定规则,不仅可以记录攻击尝试,还能有效阻止恶意请求。


比如:在用户名输入框中输入:'or 1=1#,密码随便输入,这时候的合成后的SQL查询语句为:

select * from users where username='' or 1=1#' and password=md5('')

就可以直接实现登录了。这就是SQL注入。


但是安装后发现,并没有阻止SQL注入进攻,也没有在日志中记录任何日志。查阅书籍,发现要寻找旧版本的SQL规则脚本。modsecurity_crs_41_sql_injection_attacks.conf。然后安装到指定位置,重启Apache就可以了。


重新发起SQL注入进攻,发现依旧没有阻止进攻。不过在日志中发现完整记录了这次进攻。


结论是:升级了规则库 发现可以在日志中完整记录发现的SQL注入进攻 并不能阻止SQL注入进攻。阻止SQL注入进攻 还是要在开发代码中予以防范。但是可以结合目前日志监控脚本,一旦发现有SQL注入的进攻 可以立即发出报警 并保存证据。


当然,我也可以考虑修改规则,一旦发现有SQL注入进攻,则立即让该页面跳转到一个假的提醒页面。


modsecurity_crs_41_sql_injection_attacks.conf

加入如下规则,就可以实现阻止SQL注入进攻了。直接跳转到403页面。从而弥补程序没有写防止SQL注入缺陷的问题。


SecDefaultAction log,auditlog,deny,status:403,phase:2



查阅日志:


--c4ea7e6f-H--
Message: Warning. Pattern match "^[\\d.:]+$" at REQUEST_HEADERS:Host. [file "/etc/httpd/modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf"] [line "810"] [id "920350"] [rev "2"] [msg "Host header is a numeric IP address"] [data "172.26.18.112"] [severity "EMERGENCY"] [ver "OWASP_CRS/3.0.0"] [maturity "9"] [accuracy "9"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-protocol"] [tag "OWASP_CRS/PROTOCOL_VIOLATION/IP_HOST"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A7"] [tag "PCI/6.5.10"]
Message: Warning. Pattern match "(^[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98;]+|[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98;]+$)" at ARGS:username. [file "/etc/httpd/modsecurity-crs/rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "64"] [id "981318"] [rev "2"] [msg "SQL Injection Attack: Common Injection Testing Detected"] [data "Matched Data: ' found within ARGS:username: 'or 1=1#"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.9"] [maturity "9"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"] [tag "WASCTC/WASC-19"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"]
Message: Warning. Pattern match "(?i:([\\s'\"`\xc2\xb4\xe2\x80\x99\xe2\x80\x98\\(\\)]*?)\\b([\\d\\w]++)([\\s'\"`\xc2\xb4\xe2\x80\x99\xe2\x80\x98\\(\\)]*?)(?:(?:=|<=>|r?like|sounds\\s+like|regexp)([\\s'\"`\xc2\xb4\xe2\x80\x99\xe2\x80\x98\\(\\)]*?)\\2\\b|(?:!=|<=|>=|<>|<|>|\\^|is\\s+not ..." at ARGS:username. [file "/etc/httpd/modsecurity-crs/rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "77"] [id "950901"] [rev "2"] [msg "SQL Injection Attack: SQL Tautology Detected."] [data "Matched Data:  1=1 found within ARGS:username: 'or 1=1#"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.9"] [maturity "9"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"] [tag "WASCTC/WASC-19"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"]
Message: Warning. Pattern match "(?i:\\bor\\b ?(?:\\d{1,10}|[\\'\"][^=]{1,10}[\\'\"]) ?[=<>]+|(?i:'\\s+x?or\\s+.{1,20}[+\\-!<>=])|\\b(?i:x?or)\\b\\s+(\\d{1,10}|'[^=]{1,10}')|\\b(?i:x?or)\\b\\s+(\\d{1,10}|'[^=]{1,10}')\\s*?[=<>])" at ARGS:username. [file "/etc/httpd/modsecurity-crs/rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "133"] [id "959071"] [rev "2"] [msg "SQL Injection Attack"] [data "Matched Data: or 1= found within ARGS:username: 'or 1=1#"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.9"] [maturity "9"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"] [tag "WASCTC/WASC-19"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"]
Message: Warning. Pattern match "(?i:(?:[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98]\\s*?(x?or|div|like|between|and)\\s*?[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98]?\\d)|(?:\\\\x(?:23|27|3d))|(?:^.?[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98]$)|(?:(?:^[\"'`\xc2\xb4\xe2\x80\x99\xe2\x80\x98\\\\]*?(?:[\\ ..." at ARGS:username. [file "/etc/httpd/modsecurity-crs/rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "237"] [id "981242"] [msg "Detects classic SQL injection probings 1/2"] [data "Matched Data: 'or 1 found within ARGS:username: 'or 1=1#"] [severity "CRITICAL"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"]
Apache-Handler: application/x-httpd-php
Stopwatch: 1506493991045636 5049 (- - -)
Stopwatch2: 1506493991045636 5049; combined=3257, p1=488, p2=2453, p3=85, p4=158, p5=72, sr=74, sw=1, l=0, gc=0
Response-Body-Transformed: Dechunked
Producer: ModSecurity for Apache/2.7.3 (http://www.modsecurity.org/); OWASP_CRS/3.0.2.
Server: Apache/2.4.6 (CentOS) PHP/5.4.16
Engine-Mode: "ENABLED"


--c4ea7e6f-Z--


参考URL:

http://www.cnblogs.com/sdya/p/4568548.html

https://github.com/SpiderLabs/owasp-modsecurity-crs/tree/v2.2/master/base_rules

### 在 CentOS 8 上安装和配置 ModSecurity #### 1. 安装依赖项 在开始安装 ModSecurity 之前,需要确保系统上已安装必要的依赖项。以下是所需的依赖项列表以及安装命令: ```bash yum -y install gcc make httpd-devel pcre-devel libxml2-devel curl-devel git unzip ``` 上述命令将安装编译 ModSecurity 所需的工具和库[^4]。 #### 2. 安装 libModSecurity libModSecurityModSecurity 的核心库,必须先进行安装。以下是安装步骤: - 克隆 libModSecurity 的源代码仓库: ```bash git clone https://github.com/SpiderLabs/ModSecurity.git cd ModSecurity ``` - 配置并编译 libModSecurity: ```bash git submodule init git submodule update ./build.sh ./configure make make install ``` 完成以上步骤后,libModSecurity 将被成功安装到系统中[^5]。 #### 3. 安装 modsecurity-nginx 连接器 modsecurity-nginx 是 Nginx 和 libModSecurity 之间的连接器。以下是安装步骤: - 下载 modsecurity-nginx 源代码: ```bash cd /usr/local/ wget https://codeload.github.com/SpiderLabs/ModSecurity-nginx/zip/refs/heads/master unzip master mv ModSecurity-nginx-master modsecurity-nginx ``` #### 4. 编译和安装 Nginx Nginx 需要与 modsecurity-nginx 一起重新编译以支持 ModSecurity 功能。以下是具体步骤: - 下载 Nginx 源代码: ```bash cd /usr/local/ wget http://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1 ``` - 配置 Nginx 并启用 modsecurity-nginx 模块: ```bash ./configure --add-dynamic-module=../modsecurity-nginx make modules ``` - 将生成的模块复制到 Nginx 模块目录: ```bash cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/ ``` #### 5. 配置 Nginx 使用 ModSecurity 编辑 Nginx 配置文件 `/etc/nginx/nginx.conf`,添加以下内容以加载 ModSecurity 模块: ```nginx load_module modules/ngx_http_modsecurity_module.so; http { modsecurity on; modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf; } ``` #### 6. 下载并配置 ModSecurity 规则集 下载 OWASP Core Rule Set (CRS) 并进行配置: - 下载 CRS 规则集: ```bash cd /etc/nginx/modsecurity/ wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.zip unzip v3.3.2.zip mv coreruleset-3.3.2 crs ``` - 创建 ModSecurity 配置文件 `modsecurity.conf`: ```bash touch /etc/nginx/modsecurity/modsecurity.conf ``` - 添加以下内容到 `modsecurity.conf` 文件中: ```conf Include crs/crs-setup.conf Include crs/rules/*.conf ``` #### 7. 启动和测试 Nginx 完成所有配置后,启动或重启 Nginx 服务以应用更改: ```bash systemctl restart nginx ``` 验证 ModSecurity 是否正常工作,可以使用以下命令检查日志文件: ```bash tail -f /var/log/nginx/error.log ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值