ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% {2 R2 u# v/ [5 S9 i+ k, F' ~9 O$ K& T$ q8 E" ]
' k7 B% E6 O0 `/ w3 J" z4 p
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。. ?6 P# _7 L& G* q. ~
( `- t$ G7 P9 G# a6 P: C- o0 n) I
# A8 ~6 d8 J& [$ G什么是ModSecurity) p+ G: m: @( z. `& R& }* d; x
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" c& @' K/ D6 @+ ?8 ]; o5 q7 P: O$ b) @9 G7 t _
( K2 ]& Q: h' k' OModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。: J4 j2 Y% W `
& q- L0 V& v! a* q/ [$ d
* D, `+ i# P0 f WModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 E' i2 | D( X
/ ^3 x3 |: X; ]- X' I9 o/ c4 V: s+ |" p
1 _" e5 A- d3 ?( P! Q2 ^
官网: https://www.modsecurity.org/, C# X0 R" z+ h6 i) q @6 f
* }1 m/ U; q5 _) v, Z( B" n
, b. c: X% U/ }1 F$ H7 h什么是OWASP CRS3 l1 P7 Z$ [( K: S
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。! _# }3 v7 ?: x# Z
0 y8 s- I' U" v% ]2 b7 B
" y3 J* q- G& a. HModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。" o, s& M4 U7 [% v4 V
" Y# _8 h3 Y# x Z; j0 C5 e. V2 y/ o
HTTP Protection(HTTP防御)' w/ Z0 Z( w! |* T8 S6 A
HTTP协议和本地定义使用的detectsviolations策略。5 G c4 x. L' }
- Y9 u* c5 L G& \
, R4 s, [" x: j: K/ L6 K1 W
Real-time Blacklist Lookups(实时黑名单查询)+ q1 C0 l& R7 f) h h2 L
利用第三方IP名单。
8 x1 B. ~, I! m% @6 L+ ?. D$ I: S, |6 m4 h* V b; f
8 H( k" ]" {5 i$ R$ o- V0 M. r9 O
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
( p Z/ N( z! O# X* F( B防御HTTP的洪水攻击和HTTP Dos攻击。
* K2 j T* K/ u, r7 M d- D7 O+ ~* h; {- T1 p8 Q( U
' ?+ a" T3 a; s! S/ D0 x% E3 q
Common Web Attacks Protection(常见的Web攻击防护)- a- V- V+ E! }
检测常见的Web应用程序的安全攻击。
* M( u) d) P( f
# r4 y4 c! k) k3 f6 v! `; {" A9 |4 T! g& z" ]
Automation Detection(自动化检测)
, F: b0 x V) [0 ~. R' ~3 n3 n检测机器人,爬虫,扫描仪和其他表面恶意活动。9 {7 k i; g/ i$ b
6 t) A" C8 z) Y) A
) c% O( G- W2 c; i8 y- o+ X& o6 ^Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 N6 ~, P% [3 V
检测通过Web应用程序上传的恶意文件。
4 @9 o& l4 L. O* W7 {# k+ m* J; ]( [+ V) U& W
8 p, m1 e8 Q$ UTracking Sensitive Data(跟踪敏感数据)- K, z V9 e* \+ j
信用卡通道的使用,并阻止泄漏。+ h+ ~$ M# q3 [" C( _
8 [% f$ X: g/ ?% |9 Z. j7 r1 F* L! n( n2 q6 o! ?2 C
Trojan Protection(木马防护)
' ^, T0 ~2 `+ [8 }9 _ A检测访问木马。
* p |' J3 J9 r, A* O0 |6 R Y7 H5 u. }- s
. v5 o8 @* G& K P- SIdentification of Application Defects(应用程序缺陷的鉴定)
5 Z* C/ O; ^! Y2 {( {检测应用程序的错误配置警报。
' A8 _" ~$ w6 w; o: q$ A8 t/ r" d0 y: ^2 f$ Z2 @5 t6 o* f: R
# r% K# {7 {" k! q% d) {
Error Detection and Hiding(错误检测和隐藏)
) O$ f6 T% A+ p/ a% o! H( D检测伪装服务器发送错误消息。
0 {/ y% A0 X( \! j( [1 s6 W1 ]5 U T7 z6 v% p$ G0 A
[* v% W. {( w3 b; E9 f' j
安装ModSecurity
6 q# [$ p; ], c7 Q$ ?软件基础环境准备, P) I& L6 t9 A: }( `2 y
下载对应软件包6 Z, m2 Q, Y' N/ C6 H+ j
$ cd /root
6 ?( t% h% O, _$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
5 r4 K" E) G, ~+ G5 B$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
: k5 c! [& R3 R5 z) e, d! b安装Nginx和ModSecurity依赖包: K0 ^1 P7 P+ t# h
Centos/RHEL
2 d$ q. n {! [4 C# E8 e @" M! ~8 w# K/ a% y! L1 d8 P; ~) Z
* o) W9 w( T" J" _2 L$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 k' b, @, [! Y( x' d' F$ Y. A, X; g7 l
Ubuntu/Debian0 S7 U8 d- o" g2 D
, a4 L6 ?# N) p, r$ {' V9 d' x8 K, Z5 n) x6 N( X# M% I
$ apt-get install libreadline-dev libncurses5-dev libssl-dev perl make build-essential git libpcre3 libpcre3-dev libtool autoconf apache2-dev libxml2 libxml2-dev libcurl4-openssl-dev g++ flex bison curl doxygen libyajl-dev libgeoip-dev dh-autoreconf libpcre++-dev; [" @9 }! G- t; T; c; L
编译安装ModSecurity8 E+ b, Y& ^& z8 g/ z& Y; `
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
5 j% q: q) a# [1 z9 u
4 {$ I+ C* C6 L; ? P6 c- ~0 v2 [. C) t, S5 H9 A: v+ t
方法一:编译为Nginx静态模块' l$ P) Q5 t+ ]6 u/ ?' p$ K; R
6 G/ @0 [4 l! L' w* o
Q8 b* V8 Q, w3 K3 B编译为独立模块(modsecurity-2.9.1)
& {- h& z. B8 \/ h" P9 |$ tar xzvf modsecurity-2.9.1.tar.gz, h0 q# n0 [) i: `9 k
$ cd modsecurity-2.9.1/
$ a( t, ]0 M# A$ ./autogen.sh+ k0 C2 E, W& L+ w( S3 ?
$ ./configure --enable-standalone-module --disable-mlogc
, W$ h# S+ ?- ~4 R) H- f& t$ make+ \$ Q9 d: \$ h9 L+ b; s3 c
编译安装Nginx并添加ModSecurity模块% a& k# Z# @: }$ C3 M% {
$ tar xzvf nginx-1.9.2.tar.gz0 Q$ Q) R4 y9 B7 w, R4 d5 q/ l% Z$ n
$ cd nginx-1.9.2
' r& I/ F: S( V& P$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ m* r6 l) M4 S6 I
$ make && make install( _: X/ H$ ]8 Q: M2 d& l8 r" X
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 M: g+ @, ^0 E1 g: L6 E
+ V- B5 i1 S- { l( o% B
8 o/ `. X% t8 X6 P5 g3 P, F* o编译LibModSecurity(modsecurity-3.0)2 P9 {* U: t- \% |
$ cd /root8 U- O: x" ~* S
$ git clone https://github.com/SpiderLabs/ModSecurity! t. W5 u4 k3 u) H$ O: B8 ?
$ cd ModSecurity
) f3 ?5 f5 ^) n7 K$ I, z- U$ git checkout -b v3/master origin/v3/master
8 d! E# `( X) D% c$ sh build.sh
* @7 D1 V2 A" M& C' i# G$ git submodule init
. {7 g$ a+ K9 ~4 D. [- c" @$ git submodule update; Q* D: J, n! ]! F! h4 W/ J
$ ./configure
2 k0 q: z4 \: l$ make; s# N w5 v6 s, r& }
$ make install3 x3 `7 M% v1 y- M5 i
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。, J7 m( Y( q, f0 V) X9 U) I
$ \9 M* ]5 L4 |, S/ m9 B2 }( l
6 a( z m2 `, o$ ls /usr/local/modsecurity/lib- R. ~" }0 m, P# z# W* d
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 A0 n& e* c# j& O: S7 u# \编译安装Nginx并添加ModSecurity-Nginx Connector模块: E2 M j2 }0 i2 J% U
使用ModSecurity-Nginx模块来连接LibModSecurity
6 Q& u$ E5 x; F9 ~5 u- u. A% W3 U# O0 G P
0 D3 r- [# z. Z9 ~1 i8 n% M
$ cd /root
; K9 c4 Q. V9 A1 \$ `! o4 ^) b5 F$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
0 T% y* J: n. I+ R/ T$ tar xzvf nginx-1.9.2.tar.gz
4 ?, u3 |8 ~5 Q, N& f$ cd nginx-1.9.28 G& R+ z; W+ m, y; J9 C
$ ./configure --add-module=/root/modsecurity-nginx2 x2 _1 m P6 R7 j* ]/ p: \9 o6 h
$ make! M' F! l* n R: a! [$ h
$ make && make install9 j6 ^! r: y$ m; d R/ V) i. ?" L
添加OWASP规则
4 H/ X- x" O# T- kModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ l0 Y9 C O+ n) u6 x" f; Q
7 e- a9 V4 Q+ V4 _3 [+ L7 B8 x% F: W8 T) d8 t- i
下载OWASP规则并生成配置文件
5 ]; E& a0 r+ w+ c$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git1 U1 s; x0 P/ b( y, V
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/3 o6 n0 n1 w+ h. k+ j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 G) e9 q' p* Y; r- O6 A$ R- W: E7 R$ cp crs-setup.conf.example crs-setup.conf- t! b( D' A8 u3 g+ A
配置OWASP规则7 S) Z9 t; }% W
编辑crs-setup.conf文件
' x' J$ i1 J9 I3 G% j" t: |# U$ j- P. ^7 j( ~! S. i
0 ?/ f+ j2 k6 l4 T: m
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. Q" J3 p( w5 \4 k* j3 q! d+ f
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; d! R* r& b# ]; h5 ]" ~- O: @$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf/ |+ a* I" D8 y+ k' _0 t' C" W
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. f8 `9 S. Y! Z" x默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. X9 C; P% x+ D# m; A
3 i7 [, l# @. v/ ^
: d' f( f) }1 E+ }3 d启用ModSecurity模块和CRS规则+ p2 J/ I2 ?4 [
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' D, h- l1 j, O1 z; F
# X" D# U. h$ c3 J, q/ ~: G
+ _8 w2 z3 o: M; `modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ V. i! w, d" U) \; z* W+ m8 M
! k: D+ W' ] y' z- _9 f9 n
) e3 L3 s# ]- R* W9 A' m
$ cd /root/modsecurity-2.9.1/3 U \0 X, f5 f) S; N7 p5 P% f
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf , x- M' W- s) Y( r6 l) V2 X3 G* y
$ cp unicode.mapping /usr/local/nginx/conf/! q6 J7 ~8 o- Y: i9 @
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。, Z1 a9 h* i. F( u8 ]$ U, A, M
) Q6 g" A: {" g
7 o" R# R% r; s$ b8 n7 } B" p$ vim /usr/local/nginx/conf/modsecurity.conf$ E" i! r$ j, G/ {) v/ V+ x
SecRuleEngine On
/ K5 d6 i0 r; p( {8 |7 Y2 V& ]ModSecurity中几个常用配置说明:3 t$ M, Q) b6 G0 K& p$ F
5 p% i3 G8 b' \
# _' E. f: o& P1 j; e2 T w1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
) w. ?0 U& P5 X" o
$ [1 F! _5 t) t' C* V6 x+ J% o9 Q" h5 r& D, K6 t
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. N' r$ l6 P$ a& T" j6 f8 `
0 L5 t3 M; T/ F: a$ p9 D4 z$ I6 c5 {+ g/ f" ?" n6 g
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' a0 ^0 n3 p7 a8 m% t
2 D3 s) i: z5 Z! f0 b$ i; _/ G9 G' Z. J
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
' G0 J. O% L$ X( _# V" ?; o- z( p' p4 J" k
- o0 E; }+ K7 `, c: B4 O
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。: e) Y- l" H. _) n. {3 ~
. ]( v9 I) G2 C8 n& ~" u* }3 y$ T& I M3 O/ {
3.x版本CRS5 [/ }, }7 v2 {. w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. f3 w9 r8 y B% z& R. }# 生成例外排除请求的配置文件
5 Z3 B `9 `8 L8 R1 J$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. f& a# m; X; m$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf$ C, x4 Z' y9 D+ o9 n. m& y. s
$ cp rules/*.data /usr/local/nginx/conf& ^4 v" l* o t0 l. R' t' l; Y: ?
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
3 Q: j2 N1 ~- x& m3 ?. v8 m& h. {1 b
3 L% Y3 ~/ N- _. m; s& e6 d3 N9 ^0 R+ B' Y0 U
$ vim /usr/local/nginx/conf/modsec_includes.conf( V# ^6 O+ M- D; a- g- w+ E6 y
a( n7 ]2 V( |" i; j" F0 K! j[Bash shell] 纯文本查看 复制代码 include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
' P/ \5 E) s" A& R4 |+ s9 @: q' W, ?' Z( z! L0 r' Q1 }
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ D& V1 g3 d" p5 [+ { k: [5 O
1 F% V3 J3 o& Y2 S3 I U! V. s' L2 t9 a7 P, ^9 R
配置Nginx支持Modsecurity/ S# Z3 g* o$ f: L6 j
启用Modsecurity& i* d0 w! p- i$ D t( d- t9 |4 j
使用静态模块加载的配置方法 v4 r [8 p0 h# I% {% {+ D: O
在需要启用Modsecurity的主机的location下面加入下面两行即可:
" e4 w8 T8 O& z7 Q9 Z+ |3 q9 l4 s* s! g0 M' S s; E
0 O* J" c6 M! ^: Y2 kModSecurityEnabled on;* O: }5 t4 b5 F$ E; b8 |
ModSecurityConfig modsec_includes.conf;4 R5 d8 q, y8 C- ?: P+ W4 t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) f' E$ P v& ~! v5 Z+ M
& N" s) e) p% s0 _* M7 d3 X0 R
. v" w8 a5 `5 Y( o$ vim /usr/local/nginx/conf/nginx.conf
3 D* `/ i3 r- ~$ x6 D0 L5 ^! _- P/ p' q4 h
- Q/ E0 b; _# K" P3 M% H/ f- Z8 n; O
server {
& }- H. e5 o7 m3 X listen 80;" Y8 b5 n5 s4 \2 S* M1 V" d# H
server_name example.com;- h* W# j- N& p( q( {/ h
. u3 `/ m' M8 s1 I4 j
1 ]& Z5 Z) t7 z( |6 k
location / {
7 l$ U$ q. V! Z ^" n W% ?0 K" w ModSecurityEnabled on;: t% c& U8 p0 }
ModSecurityConfig modsec_includes.conf;$ {/ w8 i* H4 a9 ^7 ^
root html;1 h, o& d$ ]$ E3 Q
index index.html index.htm;$ ^; o" p: \) K2 s! {) `8 A
}
7 Z/ P4 V ~- f# a4 z1 A: G}# S" d- n+ t+ p4 r ?0 F! W. T
使用动态模块加载的配置方法
. a# A* B, t) [/ w& a; s9 o在需要启用Modsecurity的主机的location下面加入下面两行即可:6 D. u/ y! Y% `# m/ B6 u2 B+ Y
q. E7 K! |4 h. g
8 [1 ~, I2 C) z( Lmodsecurity on;* I5 @7 d1 w( ~+ @; ^- }7 s( l3 a6 D
modsecurity_rules_file modsec_includes.conf;- A% T4 a1 @; T0 [( v4 Z- I* t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 J, `' A5 S4 O2 w3 Y
6 r0 d/ Y' A$ x. y" c! n+ Z6 d
3 i+ i* B7 W" s0 m$ vim /usr/local/nginx/conf/nginx.conf
: \* k' L6 u& M$ _; \# K, l# r% @6 Y# N" o
/ G0 D. u' {% @ l# ^7 Zserver {* ~, n" h6 m) K% O O) o
listen 80;% e* Z3 Q! u; Y& ^( r0 g
server_name localhost mike.hi-linux.com;/ P4 V7 u5 L. H' w' y& {- _$ t) }
access_log /var/log/nginx/yourdomain.log; N7 B( T: M8 m: x r+ I8 ?
4 @2 u* e$ P6 S
& s$ K b: i+ V% U
location / {; M4 z, a' g) B" d
( ~+ ~' t+ Y# D( k' U( ?0 e6 _
4 M: C5 o/ J' l X8 R" k modsecurity on;
) |# |1 f& F+ w+ r0 {4 J x modsecurity_rules_file modsec_includes.conf;8 A N) o6 t& L0 P/ t1 c
root html;3 i) D1 q5 q9 Y3 w- D/ |
index index.html index.htm;
: `; I3 s' X4 ]}
7 S9 ]8 Y% r# k5 S8 @& ^/ c}8 T8 p$ @" S' D
验证Nginx配置文件
$ ^( h3 N6 V4 `2 t( p/ m! W$ /usr/local/nginx/sbin/nginx -t" J; m' Y) J2 q6 N& E/ G" j
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok6 D8 c- k* o0 ~! G7 @
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- r I5 N0 D. c) B7 p r. X4 L& o% B启动Nginx
: R/ L5 C9 J" j5 y/ d: ^" ?" }* c$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 d2 o# M, p5 m1 b8 ?5 i
3 E, K5 x5 x9 A9 D/ g0 h测试Modsecurity ModSecurity现在已经成功配置了OWASP的规则。现在我们将测试对一些最常见的Web应用攻击。来测试ModSecurity是否挡住了攻击。这里我们启用了XSS和SQL注入的过滤规则,下面的例子中不正常的请求会直接返回403。 在浏览器中访问默认首页,会看到Nginx默认的欢迎页: [/url] 这时我们在网址后面自己加上正常参数,例如: 。同样会看到Nginx默认的欢迎页: [url=http://img.colabug.com/2017/06/842f48f203c6c2cd30144f29b57af97a.png] 接下来,我们在前面正常参数的基础上再加上 ,整个请求变成: [/url] 就会看到Nginx返回403 Forbidden的信息了,说明Modsecurity成功拦截了此请求。再来看一个的例子,同样会被Modsecurity拦截。 [url=http://img.colabug.com/2017/06/246ce28e95310a32f791893d4f5c55ca.png] 查看Modsecurity日志 [url=http://img.colabug.com/2017/06/ae44dcb58b8a4a0ea761317e398b3101.png][/url] 所有命中规则的外部攻击均会存在modsec_audit.log,用户可以对这个文件中记录进行审计。Log文件位置在modsecurity.conf中SecAuditLog选项配置,Linux默认在 /var/log/modsec_audit.log 。 $ cat /usr/local/nginx/conf/modsecurity.confSecAuditLog /var/log/modsec_audit.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
2 I" @3 c( p) R0 ?1 ~ |