ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" K2 Z/ G& z# B$ }
" K1 W- h' R9 i5 x+ {
. X I$ m1 I8 e5 [9 k$ B1 X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
- [) W0 U+ G: s* a8 }$ B/ m) a( l0 a) S$ C5 O% I/ _
9 H& v$ v( y7 x& b/ b什么是ModSecurity w& ]9 D$ |- b) U) h9 N; N
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
4 k( n8 a/ C7 v* D6 X
! C9 }( X- e$ Q
+ N. U- v) F; l* U7 u9 |ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。, L9 P$ e' Y& J4 h( P4 J/ b
" n* S* D& s2 T) c5 V K: x0 X; a" j' }# ~7 ?8 T e9 h! Y* h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
" R) e. U1 a, U! `1 V) I& h
" q, T' A0 [% e$ f) D9 F- d% o$ m8 U2 g0 o5 o
官网: https://www.modsecurity.org/( `1 d" g8 Y, `4 L& o
2 @7 Q" P9 S4 c2 c) F p
2 j# X$ ?& E( ^& \什么是OWASP CRS
: j, w% a: i- [( j% ?" }- r2 FOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! Y J- x3 K) }+ ~' y
3 `6 q$ ]% S- x* [. @9 I; k9 u' V2 y& `1 U( k
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
6 b3 a: c) b, Z2 `
; u) Y) I8 ^6 n& r7 x
% N& o/ d; x( {0 ?! I5 S' E0 nHTTP Protection(HTTP防御)3 x+ x7 R+ o8 Q" w9 i" Z
HTTP协议和本地定义使用的detectsviolations策略。
3 w- e% R. }- I* `2 d$ X
/ U8 Y( S4 a1 m! g
7 _1 F; q! g1 _5 nReal-time Blacklist Lookups(实时黑名单查询)
3 T8 i( c/ b/ Y6 E1 _6 u利用第三方IP名单。
) x% z: p- w) j9 r4 J8 }( H, b X- F1 `+ ~
8 E/ }/ {0 ~2 B
HTTP Denial of Service Protections(HTTP的拒绝服务保护)$ A6 ` \& W# C; H) R
防御HTTP的洪水攻击和HTTP Dos攻击。. U# u4 U% E9 h( T7 G) P
# a! y/ ~: A$ G3 G& h- F N6 a; I3 O5 p U1 }
Common Web Attacks Protection(常见的Web攻击防护)
6 m; j4 L( g, d: ^) a检测常见的Web应用程序的安全攻击。
' _5 W: C0 a7 p, K
8 E2 i9 S+ f+ b& J! [1 ?! S- a, [# p- j1 w! L/ D2 L+ D
Automation Detection(自动化检测)
& ~: s' [4 ?& z检测机器人,爬虫,扫描仪和其他表面恶意活动。# v! i/ b) G+ p
$ D% E: u! `! @
3 w0 L7 p) z0 e1 IIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ @- ^4 g: ^* H+ Z$ ]6 `' {
检测通过Web应用程序上传的恶意文件。+ C% w" R( i4 k; K6 c3 e( I' H, H
9 ^2 e; }! f; ]0 K4 R# W; h
o' U1 M8 }2 Q4 s0 J. \5 XTracking Sensitive Data(跟踪敏感数据)# d5 O$ B4 y. u+ ]! ?
信用卡通道的使用,并阻止泄漏。
/ J5 F+ F3 }7 i6 {2 {9 v) P* A
4 q$ d7 j6 `- f W6 X+ J& _, }. `
" |4 w0 N8 [6 V6 h' z/ MTrojan Protection(木马防护)/ }/ h9 m* o$ O# u* A1 b4 Y
检测访问木马。; r; p( j1 u5 B9 h
: G% B7 v6 Q' {, ^* h: @
9 x+ _/ _% w R+ _Identification of Application Defects(应用程序缺陷的鉴定)
- B4 l6 W' v) |2 v/ K) n0 q p/ c检测应用程序的错误配置警报。
: m4 d* q a% i6 x7 L0 F; S6 [
% Y* Z# [- ]! i, D! T( _
9 I0 m2 l8 z: t1 fError Detection and Hiding(错误检测和隐藏)8 E$ T) f4 Z5 u; t$ T; B' F
检测伪装服务器发送错误消息。
( P" ~' {/ Y5 o9 x1 F# Q6 o# N9 t, }( V. M
/ C ^8 M2 d3 r
安装ModSecurity
; k" j. @2 c& I0 l软件基础环境准备
0 d! J; v; ?& P+ t, n6 W' U下载对应软件包
7 U3 C4 A8 F4 Q" E. {$ cd /root
1 p5 {, U$ _, w7 v$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', E' Y1 N1 U1 ^* k6 b+ O, }% k
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz7 Z ^& v$ t# k4 T6 @# }- p+ G# c
安装Nginx和ModSecurity依赖包! G3 W- b0 X6 P8 `' p/ t: ]8 X
Centos/RHEL3 X" X2 a9 ~/ D6 W
- i# h0 Y" d5 C |1 E' ]" s
; K, M; Y# s( I8 P% J; S/ y* }1 m& [. i8 a$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- M" R+ }; b! C, i0 y. [- IUbuntu/Debian. l# k1 ]8 v8 v
+ A; @. z9 r8 F5 X/ R# q' O
4 U% s3 U. Z+ k1 G" C4 x+ u$ 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& N" @7 Z, f) X- j* f" D$ @
编译安装ModSecurity
) Q/ `' C& l: {* RNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。/ @) h$ b Y+ E
' F& ~/ {0 }* t7 n- J3 f& h
9 P% v; |- \! _6 h; M: y方法一:编译为Nginx静态模块. ^' w9 y, _* ]" ?: i
( Z, r) H5 j+ w' A
* b4 R5 E/ c2 g: `, i/ s6 _* O编译为独立模块(modsecurity-2.9.1)8 y/ }5 P' w9 B
$ tar xzvf modsecurity-2.9.1.tar.gz
S& N( j7 ]: C' U) N$ cd modsecurity-2.9.1/ K1 A3 e) U' D/ l6 R4 `0 ^
$ ./autogen.sh
$ S- v( v t: ]% O0 H- F, w; Z$ ./configure --enable-standalone-module --disable-mlogc
$ J0 G( A7 C# B7 h, M! f$ make- `9 A1 n+ f9 d
编译安装Nginx并添加ModSecurity模块" G) W$ ^2 x" O8 e5 c6 u Z
$ tar xzvf nginx-1.9.2.tar.gz
. q# E+ E2 V' t+ r$ cd nginx-1.9.25 `, K- \% r$ h! t
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
0 H$ ]& m& H2 p3 W( w: B$ make && make install' Z. M; ^; N, @4 Q. U1 Q; S( M ^
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, S4 D( n: z% m3 B# h7 A+ j$ H+ l
0 a+ M0 S- u" X% H7 e4 K$ a& x- V9 M
编译LibModSecurity(modsecurity-3.0)5 ]8 P* v* T0 y; M
$ cd /root. [2 X$ h$ P/ k+ U0 U# z
$ git clone https://github.com/SpiderLabs/ModSecurity
: Y3 g" ?9 y! N$ cd ModSecurity- v0 |/ O4 u+ P+ f) I2 v
$ git checkout -b v3/master origin/v3/master/ o6 ^1 U* _6 f* h, p# c' s. X0 M# q
$ sh build.sh/ O+ Z+ M4 ]+ x/ j5 w4 A j
$ git submodule init
: s/ m' L- k5 ?4 H$ git submodule update4 \: _0 @3 \, q4 T' ?4 x
$ ./configure
3 }9 Y% F" I: V# \, o P$ make6 @$ m% M4 J: s$ q, \ F
$ make install. R5 o: ^- g+ E& l' {* i1 z
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
D* a+ H# P$ K+ P' {
0 y* K6 r/ w9 q- V! M8 l) q; ~% _2 F6 D s8 ~# m
$ ls /usr/local/modsecurity/lib
7 }0 x- J. p+ u9 J H: _7 Q1 N1 F: e* \libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
" T+ _* p A) ~( U( x S编译安装Nginx并添加ModSecurity-Nginx Connector模块6 R7 ]4 Z+ }! j$ A
使用ModSecurity-Nginx模块来连接LibModSecurity
6 d: z: N1 h" @' t, c0 \+ l
( T: o c) L7 U D6 U; j6 Z8 y) v
$ cd /root% l/ s; `% G1 s2 _# }, B
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ M) X' _$ X b& T0 j0 G- Y5 O$ tar xzvf nginx-1.9.2.tar.gz* P, w& I+ G, B% R7 ]
$ cd nginx-1.9.2
5 \* Z' f0 V" a% ^3 _" j$ ./configure --add-module=/root/modsecurity-nginx! U2 N: T% \, L6 P
$ make
( ? ~- x# Y7 P! t" V$ make && make install
7 w% z/ e8 j, w/ ~5 [添加OWASP规则
* N7 p T* p8 [7 e) v* {3 X4 oModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 G0 Q6 G. K* p9 b! _( _& T# A, B w. M
( K8 V6 P' b. x9 \. O下载OWASP规则并生成配置文件% J( x+ Y8 h( L& G. |/ F* `, n1 G5 k
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git3 q0 _7 Z. d* [, y0 j
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 G, M% ~ W1 M0 Q+ E$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 i' q& J+ w0 F- C4 }8 e" {* l, v$ cp crs-setup.conf.example crs-setup.conf
1 m- x4 r% R+ V; n. v! Y配置OWASP规则
7 T. Q$ s, I3 g c: r0 Y编辑crs-setup.conf文件8 k8 v: @+ j( `/ S
7 U3 n. G0 N. J! L( A/ K& l0 ~ S# C: @0 p5 E V
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
% j. H$ e9 D" M! m9 i% E$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
$ _7 x6 K! @7 W- m) o& |$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' J& d% k& W$ p# x$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 d0 W& G3 s' N. K- C P默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) z/ Y" h l ^9 V1 c( ~
; j; M1 Q( B, Z# j1 P* b0 K
U" } D" m' N& M; v: w8 Y启用ModSecurity模块和CRS规则
4 \" H9 C- ^6 M- t) M复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ P1 {$ ?/ r* n' J# t' w0 V
3 e# s9 a) F- j$ F& i" V6 U4 \' j
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。. g1 g1 Y9 q8 A4 c3 t- N
3 _; U7 Z/ D9 l) K4 _& A4 @1 N B' ?1 X: c' k: d' n
$ cd /root/modsecurity-2.9.1/
9 F$ V& ]; A6 G9 j$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
( m' n3 l0 x a6 q$ cp unicode.mapping /usr/local/nginx/conf/
( X1 z' o* b3 J! g, c( e" b将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。5 m+ }1 |/ t2 o5 U, C
) ]& V: R( M0 y" o5 h
" w; S5 | Y! R5 u/ p+ g9 r A( H @$ vim /usr/local/nginx/conf/modsecurity.conf5 l/ K# T5 B. q+ \
SecRuleEngine On
' m6 z& F0 O5 P" F8 ^) \ModSecurity中几个常用配置说明:8 i! M1 r2 V7 C$ b+ N+ W) p! s! v
0 X q% H" b3 _6 ^) F
* p& b9 w) Y) Y/ Y1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。( K$ Q6 ]) e9 H% J: A& w
; R$ ^. y, ]9 M2 ~- D r/ k) Q
+ \; @% ]* r7 q" Q# M
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
$ s' h# a F9 X
0 m3 a- r2 F* A( Y0 n% t
+ c( ]" L4 _! X9 p1 }* ^" O3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- U0 y/ k7 ^5 v: L: J+ F4 Z1 t, w6 O, e8 {7 Z
. ~+ g; R2 [ U/ x7 T5 F& D4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 U4 X* Y j, @5 [' e
3 S( \0 X, F! @6 u3 w" H9 K+ I# j& g( F7 m O- i: I$ N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' b2 S& F2 Z+ W! Q
, r1 i W( u% o3 a
+ Y; D* v( I/ X# g9 k, d3.x版本CRS9 q {9 z6 X7 d7 e# d* I
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 E/ R: V6 F z/ \# 生成例外排除请求的配置文件
/ ~& O8 [# J/ [! V3 |9 f$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf* M8 X) l p8 h7 `0 W2 B
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 Z M# t! D+ |! I$ cp rules/*.data /usr/local/nginx/conf
& q0 F' Q- {4 [7 j. k5 t! j/ H8 X为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。, o4 s! ^% k5 X) B+ b
2 o: e/ Y- a/ Q# c6 D( `
. T0 i- f7 G( c) V5 h) t9 v( N
$ vim /usr/local/nginx/conf/modsec_includes.conf. S5 l; w8 x6 \3 R
2 U# @0 X3 Y! x! S( _+ B$ l5 X
[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
! T- v* p7 d; S$ h7 e- C1 x: _/ I. \7 _8 H3 m) O" `* U v# V( p
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 a2 T/ L( Q! w1 {* U/ @
7 u% P/ e* Q$ e$ f2 @5 j4 j% n+ y) S5 z
" b2 K v: @$ t& v5 x# A) E: [# W配置Nginx支持Modsecurity2 n+ {. P3 x' W3 X) p# X: R
启用Modsecurity
2 k* c1 ^/ x7 U使用静态模块加载的配置方法( g4 {* e5 o* h$ L* C9 f2 m
在需要启用Modsecurity的主机的location下面加入下面两行即可:& A! T+ P H9 e1 H( |9 [1 d
, s. a' D0 b" ^' j8 l7 P
" T6 v& e A4 R7 @ModSecurityEnabled on;
8 a' W! Y# Z( F7 L' H9 |; @+ H& u1 |ModSecurityConfig modsec_includes.conf;
" s5 W; a2 s u* r& h+ `0 g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& K% a7 q7 r- t* a3 q4 D( }$ a3 |8 O9 r! r9 i
- t* l/ G1 H+ v8 h( _$ vim /usr/local/nginx/conf/nginx.conf
* |7 W7 E( C. i- m/ c
! y6 e3 m$ R. D' R% t+ F* x- ^4 P' X3 I% \" [* K* V# _# O
server {
4 N* o* p Y% F, Y listen 80;9 H4 y4 Q5 b4 U4 K {
server_name example.com;
4 w; i( ?/ h# O/ K+ S, _8 W" `/ Z4 Q* V$ T; W
( P: }0 Z1 r. P5 M% G- {3 M
location / {
9 ~& X) O/ K' I( D* @0 k ModSecurityEnabled on;1 h m4 {8 {8 b
ModSecurityConfig modsec_includes.conf;
- ]% y4 A' j2 A+ D( j) q root html;0 |- p7 ~, a R4 q; ?$ B2 J4 V1 e
index index.html index.htm;- ?; Z2 }4 i8 n
}" w! V; h% }! }; Z5 e
}
! a. J- k" i0 G0 l使用动态模块加载的配置方法( S2 Y/ R$ \. q6 ^2 N
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! r |0 s- c- g' N1 f/ O1 u" u& |/ q: z
6 w2 x+ ~* R- M8 n9 o! a& `/ U D, kmodsecurity on;
- I4 Z: W' V% f6 \% X. f7 N4 E: Gmodsecurity_rules_file modsec_includes.conf;1 F) x# C: k2 B7 Z* R0 z' T! {# I
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 {) r4 d3 Y% d# \( g3 B6 j% L1 H8 d9 G3 N# P% w
b2 e, f* d& S/ g6 g
$ vim /usr/local/nginx/conf/nginx.conf
$ s% d9 h/ Z( d1 K7 c: l& M2 E3 N o9 K( E
. B. q/ @% g: @) R& e4 S: P
server {
% a- H/ l. i# _0 e H1 f listen 80;
3 J. d5 H# A8 g' ^: L; M* Q server_name localhost mike.hi-linux.com;
1 S4 I) L. Q8 c; n- k4 U access_log /var/log/nginx/yourdomain.log;3 y* ^: k' t- K, }* j
, O& h+ [0 v9 D n. W; \
) S' v8 u0 w: C+ Z! m1 s location / {
1 R( C/ u$ P! c/ b2 Z# a H) f2 L2 b8 d
8 {4 K! C7 Y8 O8 j0 s$ x$ r4 s( | modsecurity on;* p6 t6 y" A' Y# J+ V" f
modsecurity_rules_file modsec_includes.conf;/ h2 i& X, k/ V; r# Q; Y) L' B
root html;
4 C9 D; ]+ N, v- a4 S2 Y index index.html index.htm;
g i1 u' m" }( j# ?- C: v4 x" @/ v}
- C7 g# h: P3 q/ F4 w}% R' u |# m+ N' t
验证Nginx配置文件7 ?% l! ]' I2 j' [8 o
$ /usr/local/nginx/sbin/nginx -t
g8 u7 S, Z1 D6 i& W* Znginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
' L9 p) g ]/ l7 a p( x) @nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful/ f) W0 {- e" p! R2 K2 t5 s
启动Nginx) a) N$ u! ?0 e/ _+ o+ |
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/ R0 J1 S+ V7 g( s% N5 r; c7 ^* |; \( s/ N( A2 a
测试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能够战胜更多复杂/未知/混淆的攻击模式。
0 E# B3 o& U$ x9 G# S- k% d |