ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
( G) ]* f# Y; c. F3 I! r
$ Q F7 M$ ~& l+ t. R' ~' N, k9 D0 C. T2 {
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。 k, c. r$ B# y9 v" Y1 h% T
7 G0 Z4 u$ d6 d9 m
8 A/ k9 V5 }3 B& D/ [& U
什么是ModSecurity
/ q3 z- J: }- nModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, r5 i4 G" ?, ]7 X* q" j( h* I# [7 A7 d- T# u/ \- K c
& P; r. I1 g2 l1 L2 fModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
7 `) d, [, w! F, J& g& w
! G! u1 ?0 F& M8 f
6 v: |0 t! K5 V0 c4 m8 hModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。9 l' A$ c7 l; D
4 u) w/ H7 U# v' {, H* u
# ^, x+ X; F/ ?! F) g官网: https://www.modsecurity.org/
, B, Y9 ]) f6 M. W: k- K7 y( S0 B
, ~: W) F! }: f) a( J0 x6 S
什么是OWASP CRS
! v! ]# {2 Q5 F: p: vOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。& Z! A- ~ F5 u0 c' H3 F8 F
9 b" E/ q2 G5 k; a+ {2 ]- F, k* U5 U$ s- ^$ Y( m3 G
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
4 b* q, X6 t0 e0 T" I8 \* G9 F; q( X6 L8 ]
4 f: Y4 P: N$ A1 \ `. I7 z/ mHTTP Protection(HTTP防御)8 U% M. j) w2 e/ j( e2 j. ]
HTTP协议和本地定义使用的detectsviolations策略。
- N5 J6 U' }8 z# I( F2 j! m. m
1 [% Y1 R; ?% e" Q/ \7 U$ M0 ~1 y# J9 \8 p* z
Real-time Blacklist Lookups(实时黑名单查询)( R- n2 X. }5 I! E$ w
利用第三方IP名单。
& q* O. f/ l, @! h3 n0 F! @# q- E; X* o( v
6 S) ~0 W9 w" n( ^HTTP Denial of Service Protections(HTTP的拒绝服务保护)9 e3 N/ e3 X6 o+ N7 o. a
防御HTTP的洪水攻击和HTTP Dos攻击。0 L- D) r- H0 e0 _: `8 E( N3 l
! f1 I) C/ P8 u, {2 s! O
" N/ D4 N& ]" rCommon Web Attacks Protection(常见的Web攻击防护)
1 ]5 w2 N2 j. z9 S$ l检测常见的Web应用程序的安全攻击。
/ e1 v; X- C* V3 H d8 C
) i$ `; k. [% w, j+ L' z1 } u
) W H- K/ \7 A9 R ]Automation Detection(自动化检测)( Q9 d4 A7 x9 z* |& p
检测机器人,爬虫,扫描仪和其他表面恶意活动。' Z$ i' }7 ]/ E6 T: h5 ]; I. T
- w- c, e- ^; W5 h, g% \* w
$ A3 s5 |! N6 g: YIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
& C+ Y! d/ z, |% G+ w; \检测通过Web应用程序上传的恶意文件。
) z9 U8 i+ @* t9 c) R; e
* A( G0 d; a6 \0 r7 L$ n( l' n; N. U& x2 F
Tracking Sensitive Data(跟踪敏感数据)
# c3 u; w3 [6 o' p( p/ M. V" ^信用卡通道的使用,并阻止泄漏。
1 t& U: t# P6 W' q$ y8 `: v, }
" z' x& B2 d3 R. n/ C5 p1 a
Trojan Protection(木马防护)7 z1 Y( T7 @3 {1 `# A p/ \" P
检测访问木马。
+ @3 Z" c( b. ?; T8 x5 A; j6 t# C% _" ]. Z
7 |% T3 a! ~# d+ \
Identification of Application Defects(应用程序缺陷的鉴定)3 Q3 O; i9 C1 W! K9 N/ s! f
检测应用程序的错误配置警报。2 [6 P' D( D' n! l- l- Z
0 k m# {8 K; Y2 X! ]- X0 i* g, T# a
Error Detection and Hiding(错误检测和隐藏) y& v; J o+ X" ?7 [
检测伪装服务器发送错误消息。$ U+ q! r1 @5 _2 ~9 L! b
1 w5 {' ?" S" W5 W
' U& C7 b) d' z* C4 R/ F3 y9 m安装ModSecurity& U- H, _3 u% I' p3 b' \
软件基础环境准备- O5 \* h9 q' D& W2 |, I2 f3 V( h
下载对应软件包
* L5 _0 U8 q# Q: E$ cd /root' I2 m7 e# j% R8 b( ^9 q8 D8 G
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. r- x6 w# _- u G6 F
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz- K$ ]. w! W1 K/ r
安装Nginx和ModSecurity依赖包; f: v P, [" W) c$ g
Centos/RHEL; I0 S- \" S1 `& f( c8 O! i" L
' H" d9 h0 \$ J# L, ]7 `. F7 I* K( W6 }/ a' F. x
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 Q N7 _" U; h/ f! sUbuntu/Debian4 ^0 v" f" M s
+ t- x/ h* b; ?) v) u) m& a Q2 q4 `0 Y' T2 \$ N8 |: \( C7 o1 `6 a1 b$ d
$ 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
+ X0 q% ~# J0 S* M, U编译安装ModSecurity5 g* ]/ n1 z, G* ]+ o7 \
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
: G* e$ Z5 l9 F5 T3 e( g. P4 @; y$ N# v3 x" w; L
( b$ I- ^ |/ k( i8 e方法一:编译为Nginx静态模块: O K, ` J; H; q
F$ N9 ^" P1 m8 H0 E5 K
5 L/ G' {4 Z$ m/ y编译为独立模块(modsecurity-2.9.1)& b6 `# @/ E+ m2 o+ r* n6 \
$ tar xzvf modsecurity-2.9.1.tar.gz, Q% O; I% L k/ Y
$ cd modsecurity-2.9.1/
3 }- ~2 m/ B5 J+ S4 L( ]( `$ ./autogen.sh& d1 v3 {3 a" A0 S
$ ./configure --enable-standalone-module --disable-mlogc
( b, y9 x( `& K! c9 [+ l0 h% M' b$ make. D. U, ~" w0 W1 v7 `( R* u j
编译安装Nginx并添加ModSecurity模块 X$ v$ Y( Z7 {6 d( }
$ tar xzvf nginx-1.9.2.tar.gz' L1 O& E& H' n/ s. M+ `
$ cd nginx-1.9.2' d( q$ y- B1 }9 ?2 }
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: O' i8 C9 v u! O! B3 C$ make && make install# I' ?+ F% Q) T" C1 ~" Z7 D7 O
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
% q3 }2 v* ?! [# h' k! X
) o0 o. B4 c h' E. F5 H( D1 Q' y
编译LibModSecurity(modsecurity-3.0)* @+ Z* H8 @' r7 ?* ?' K
$ cd /root
1 W; s# {# U3 z" F; ]. K$ git clone https://github.com/SpiderLabs/ModSecurity
" s* r7 a- u0 [& C: ]; u/ Y$ cd ModSecurity
f) `5 a. H5 h3 k( J% R H$ git checkout -b v3/master origin/v3/master E$ a: }% A. V; H5 F" ]7 c
$ sh build.sh
) |& x' }( t b1 Q$ git submodule init
d0 n/ h; z) K0 x" m$ git submodule update) q7 ~# p" w* q# z! X
$ ./configure
& a8 Q. g/ V1 h1 \- x3 [$ make! B: ?% w2 v/ b8 \. w% S8 x
$ make install
% o9 c: b/ F" O; }LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
6 w. }; _# A6 c* E
1 Y8 L- n' P- x6 p8 l+ g1 L( P; K& w, M! Z( U" o' B% _, t4 l4 I
$ ls /usr/local/modsecurity/lib
$ t# r) M; A% d6 L7 rlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0$ W5 b3 }! @& ~7 C( j+ ^. T6 Y
编译安装Nginx并添加ModSecurity-Nginx Connector模块
j+ X0 |; y9 N# C使用ModSecurity-Nginx模块来连接LibModSecurity% i9 R4 [+ I, G& I+ y k
- }5 M1 Z; M. m: J) y4 |& b2 S4 n% j1 H+ W# Q
$ cd /root( x1 a+ G1 t i1 w' L6 A
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
/ g% c# R7 i2 c, R3 w4 C$ tar xzvf nginx-1.9.2.tar.gz' [9 Z5 v) Y1 }" H! d+ e
$ cd nginx-1.9.2
" @, r, M( c3 T6 @. ]$ H/ n$ ./configure --add-module=/root/modsecurity-nginx0 z3 c4 z) y: {7 `
$ make
% Z# H! j* g- ]) R2 g' T$ make && make install% Z4 j+ Z. n8 |# [# T
添加OWASP规则- x" q `7 h0 @- t
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 Y) c. u6 |4 j2 @) i
$ }: I: y8 ]& e. V7 W
8 x; M. W3 c% C3 ?: z! f' x下载OWASP规则并生成配置文件
' z2 K: p- B; m; F$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
2 p3 f; ^3 d0 t5 k. }$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
- |7 m' h9 d O# x1 S1 i* [5 c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, L6 |2 l5 U; ^. k' ?7 [
$ cp crs-setup.conf.example crs-setup.conf+ h2 |% q2 X2 D7 G. c$ Q
配置OWASP规则
" {2 R( c; f, k z* ^9 Q编辑crs-setup.conf文件( m* v" s6 g4 y; ^" z4 ^
$ r, u3 x J/ r3 x
! X5 ~* w' _0 |1 A! p6 X. ?$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
' r' `+ S1 n; d9 ]4 Z$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% {4 ^0 b8 U; U3 I- x @$ L% I4 N: G
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
( `# U% r i% G! n- f R: [8 E2 Q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# c& \0 n7 o# ^) t4 `1 |默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。9 m$ T$ f$ @1 G1 y
4 v- R9 U* E7 i5 I& O- S" Q
5 U3 J5 A! G+ _/ S w$ }启用ModSecurity模块和CRS规则
. b' d ]- d% V7 c' H" X+ q; \5 t复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ i+ m$ Z6 t: J& i- ?2 W. x& v
* }) _( ^1 y" i; k; j2 E
0 \. h9 {8 z4 p
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 x2 V7 E- T1 J9 y8 B
. S1 e3 h8 D* ]( D5 Y$ L* ^
1 i. L+ _8 ^$ d/ `( E0 Y/ ?
$ cd /root/modsecurity-2.9.1/0 M; Y9 h2 Q0 |; [( t4 _' a
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf % c$ L9 `' P/ l" {$ x7 t
$ cp unicode.mapping /usr/local/nginx/conf/0 j' P& i( t' A2 y" g! P
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; l& r' S4 b/ K& o+ @
3 a0 |( I3 ?# P6 k2 N0 s, ~6 d
$ vim /usr/local/nginx/conf/modsecurity.conf) G. p$ x( _5 h* x" y& o6 R
SecRuleEngine On
@8 v* T! d) p& x) MModSecurity中几个常用配置说明:% ]* i9 z: A: K3 @
2 }% j8 {( }/ k5 ~- z X
& q9 @5 }- n+ m* U8 o- v% Q
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
7 f7 V+ P9 \; l' u/ q! T5 s
! n$ [& p, S( U& w* h
9 u# H$ H- }* T6 x8 i5 O1 q3 b2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 w! h% s' M1 q, ^$ } k3 r
, i4 v. T! P3 N' V' U7 E8 z: W+ C, ]
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
0 |1 Z U/ Q) ]( f
* }" w% t( E9 I' ~. j* ~+ a: Z: ~& a0 P" O4 L
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。8 J5 c' p0 U3 c! v2 f* q
, K% Y6 f6 n4 S& q, m0 ^! _* d% {! v" U0 \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
3 J5 G, \# Y( H8 k% Z/ Q4 h' b+ o. t* ]+ m* _* L
+ c6 I* @0 l) N$ P$ g3 P" @
3.x版本CRS& W# k; h4 ~6 x3 r5 T3 ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 x) f- O# d: N" @8 Q% t. Y
# 生成例外排除请求的配置文件4 c( q+ j8 T! ^4 g! L$ h, c
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf; ~, e+ B" q2 w! P) D L; h
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf$ J5 C4 T# [! \5 b4 n
$ cp rules/*.data /usr/local/nginx/conf/ X4 M' ` |. `' Z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。8 V) Y. m# S& |1 h* f
1 C7 J" u5 J8 s: r* _! m* f5 U
$ n1 I1 d' }; r* H# y& U' ^
$ vim /usr/local/nginx/conf/modsec_includes.conf
/ \- b1 w) `" r9 G% N, d }" k e% x2 t- }" d4 o6 a
[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 $ U! _) Z0 a$ ]% g: n
( {! ?* N2 B9 d0 a
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, t U8 c8 a1 z2 t$ m
" @5 H+ G0 s, d( F, B
) g5 U1 M3 v- F. Q9 j, Z1 K
配置Nginx支持Modsecurity! X. m5 r# k3 a) }1 k1 s0 Z
启用Modsecurity/ R) k' Y0 R4 M, |( u
使用静态模块加载的配置方法
9 E9 G1 l! P6 {在需要启用Modsecurity的主机的location下面加入下面两行即可:
# H- n4 H# ]& Z) @9 R, Z$ i2 Y7 v) |" [8 R8 B Q2 C6 h
; v+ @) j3 W9 _/ `* ?
ModSecurityEnabled on;5 \) o* [- M2 \+ Q. f a
ModSecurityConfig modsec_includes.conf;. `9 B+ \# R8 P) x% Y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ T9 A5 z- j/ U9 |, R1 A* r1 z4 p
# X0 T' y3 X0 u3 c5 G' d" `- j: ]& n, z, N* R
$ vim /usr/local/nginx/conf/nginx.conf
7 E& y x: h/ @2 a# k( G
! e7 j/ y) U: O* D( {: V! J5 p6 I2 P; l! |6 n) @8 D! ~
server {, F& C% I" O4 c0 Y/ h% g
listen 80;0 S2 [$ \0 w4 `" c5 ^
server_name example.com;4 t- N/ S c/ T+ `; H
7 j @0 V2 y5 q4 z, W# L# S: C3 z# C* R
location / {# \' k/ F" I. ?1 i6 T6 q
ModSecurityEnabled on;
4 G! P+ g- L% [/ F" z& [) B! P# e ModSecurityConfig modsec_includes.conf;
( n2 E( i d) b0 } root html;
, A, K. V t$ V index index.html index.htm;: S* u2 L5 ?% {( |6 E: V, L/ n
}4 R: V# e: v1 p) e$ C. j
}
& s9 k \) a3 g使用动态模块加载的配置方法
9 S- I' |* o- n& d( o在需要启用Modsecurity的主机的location下面加入下面两行即可:
& O, w9 F% P0 M& ]( ^$ t
$ a5 |3 j [+ f8 W7 ~) k: p( E9 N" l+ u8 L4 {% G$ z
modsecurity on;9 t8 |3 g" M a
modsecurity_rules_file modsec_includes.conf;! N; {% x& K$ b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ K# B* d" J5 G8 w& {# X' M
9 c* K+ j6 ^2 j9 a' c2 r' b
+ B. t% {; J, z/ D$ vim /usr/local/nginx/conf/nginx.conf I# O/ |! @5 R4 @* x3 J: {
' [4 E: U' W; v3 Z# @- l8 ~2 X9 X
# h& _ Z, Y$ xserver {
2 u. v( a2 X+ z1 l( g: f# x listen 80;: U8 p+ z5 B2 Z1 s2 J
server_name localhost mike.hi-linux.com; j* y2 m- Y% ^/ h' c% w" e
access_log /var/log/nginx/yourdomain.log;
7 p6 z- A7 h: P- w0 F
; H a2 @; n1 G$ G9 F# H- v- Q! Q. G: Q0 s4 a& K5 R0 v) k! ]; M
location / {! r3 @( h3 e: W) X: ~; u) c
8 T( Q. S6 K3 I
6 W9 \5 u! }, z4 \7 d; Y modsecurity on;# R% }/ d) B Y6 y
modsecurity_rules_file modsec_includes.conf;
2 P1 Z9 e7 R" K root html;
! L8 S2 I' y* z# j: U index index.html index.htm;0 Y% `9 R5 {- M
}3 B$ `3 j1 F4 [- G/ [2 c2 R
}7 z. ?$ Z7 o6 n* M3 A& }4 R
验证Nginx配置文件
0 m+ b- |6 s5 p8 } c1 s$ /usr/local/nginx/sbin/nginx -t
! K. b Q) f2 d# K" ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
, d0 c7 A$ R [% q3 E6 c% Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 j8 T8 B( L; {, D8 x
启动Nginx
: U& Z( O$ N" N7 S7 d# T$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# k4 |, y/ [; }$ x, S# s
5 F! C% @% H0 ?3 P测试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能够战胜更多复杂/未知/混淆的攻击模式。
# h0 F# [6 O: I7 z0 z9 h( I |