ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。5 c6 Q$ m* B0 M4 W( u. g
6 M- m6 D* B6 N$ h1 ]: @
- q" I e% g1 H2 w; o. N, X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 Z/ X+ [7 b2 r1 z* t0 s5 H
j, t3 ~9 B: l1 h/ ~
/ ^$ J: B: R4 ?) p" ?什么是ModSecurity
1 c8 Q6 A6 w6 F, x) U' FModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 D9 P& H0 A. [; E4 b
' B" k/ [4 V: e; A0 J+ B( N/ l$ ` f! F/ X/ M, q
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# V: x) v! X7 e. x4 c
0 }- L- J3 a, ?/ H! m4 Y2 V4 j+ |1 \! V% u8 w: i0 I7 d1 k
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; B1 T, i$ g+ Y- ~: d& Q ?# v
8 q. w N; M, E% c
( `% ?: q+ v* Z6 n/ W官网: https://www.modsecurity.org/
! o) }, i. V( V* ]" v, R* P& t1 A# W2 h, d6 _
+ M$ u" y% I5 H0 B% d9 b' E( }什么是OWASP CRS* c% o n6 m/ ?
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
; y% z* z# x3 u% {3 I7 [4 x
4 `& [* p" x0 B1 k; b8 c* j _' z+ b6 z; D
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 _; J8 |6 j- w/ o/ d7 v
& @0 J7 E1 t3 ?8 E; Y7 z! j" E! h1 E( _! w
HTTP Protection(HTTP防御)
3 x9 O( T( p. c0 @% J0 O6 D$ IHTTP协议和本地定义使用的detectsviolations策略。) W, P" B* F9 |' D5 d
6 E: j. ], R. \8 P. `* |2 j5 T
1 l. r- i. \/ ^- j' L6 P0 ~Real-time Blacklist Lookups(实时黑名单查询)
5 \0 M& ^' v0 d利用第三方IP名单。
8 F( D5 p7 S! c. s/ |2 h( D
+ |1 u8 J2 D* t- Z, T
* z5 e/ M! Q" T5 ~! ]) yHTTP Denial of Service Protections(HTTP的拒绝服务保护); x# F# G( V: X0 C
防御HTTP的洪水攻击和HTTP Dos攻击。1 O! P. V, H' X. W! B
: k1 `5 {0 U4 O! ]4 ~9 n# l |4 C* l6 L7 P8 }8 F6 k- h* N# u
Common Web Attacks Protection(常见的Web攻击防护)" F" X1 p- Y- n* A/ Q/ _
检测常见的Web应用程序的安全攻击。
0 W6 p2 Y1 J4 F* X: _: I
4 W" H4 b" C, t" `! r% L
) A; R# G* ? I( e3 Y" S4 U2 oAutomation Detection(自动化检测); e+ y' N* j7 Q! F7 @# X. H+ i
检测机器人,爬虫,扫描仪和其他表面恶意活动。 J4 \+ T* N; @- D
5 ?$ W3 P6 Q$ I
3 k/ b b- n! jIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 T2 F! y7 U- _9 [# p- W; Y检测通过Web应用程序上传的恶意文件。
# G7 M. V4 M+ r# F4 v8 f
' s6 w, _( x4 A& X4 ^, h$ ^$ `- ]7 a6 j& o9 b' l
Tracking Sensitive Data(跟踪敏感数据)
- Y2 D+ o: y( J! d, \2 g信用卡通道的使用,并阻止泄漏。& H: r3 P' v' `9 B6 l9 Z
" g0 Y. P d, P3 {3 ]- C
: E' Z, [7 u% a# ZTrojan Protection(木马防护)( y2 x: a/ U& _! s: S! x# _
检测访问木马。
' g* q: |; c1 w% E+ {' s3 D+ c! T7 X0 S6 h- O; F6 p. o* O2 J, \
5 x2 ]/ f. U( r* TIdentification of Application Defects(应用程序缺陷的鉴定), ^$ j& t& h6 i. f) a( }
检测应用程序的错误配置警报。2 B9 a8 Q# P0 v; t
/ |/ b7 v( Y0 u/ r6 R; N- E. m$ b& J) P5 O
Error Detection and Hiding(错误检测和隐藏)$ }% z$ U- K) l% I: x4 T- e% J
检测伪装服务器发送错误消息。 d: I! b4 @ Z- b u" e; F8 s
: c! D" o$ ~* \3 o
( |0 e1 a4 C3 v2 }安装ModSecurity
" o$ O5 R4 K9 j3 Y) |% `+ U软件基础环境准备
! w/ ?5 Y6 A Q下载对应软件包! Q& `" { L8 l
$ cd /root# h1 k1 F- R8 v d7 T
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz') M1 a9 m) U: J" W/ u
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
1 V: I) {+ x1 x% a3 B# r安装Nginx和ModSecurity依赖包
. k7 A9 J# g5 L/ UCentos/RHEL
$ S1 }( Q! [% E8 v, ^- ~- Z0 H0 r D0 {
: T( c2 j! o0 A ^' {2 S. l: G y
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 T; w9 R/ T% n3 G' o5 a7 g; E
Ubuntu/Debian6 e8 I ^8 q2 E
. ?$ i! f9 }$ @! {6 A* m2 C6 R2 j7 ~+ r8 j
; w- k2 H# t- V: ^$ e; O$ u/ S$ 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
2 B2 u, M' z2 t% C+ j编译安装ModSecurity
' f5 E/ m8 d5 h+ }$ kNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 _4 n: V8 g6 r3 g. \# L+ M7 ~1 n" y3 K8 [" L
+ Z( R4 ^* Q8 Y" q. _) q- V方法一:编译为Nginx静态模块
3 m# [8 h$ r& b; q
( U7 ^& b' h$ J
* s9 `" z+ a. F$ l编译为独立模块(modsecurity-2.9.1)
# M) {( i4 B5 p; B5 q; ]# ~6 P; U3 c$ tar xzvf modsecurity-2.9.1.tar.gz
- v2 k. }8 k9 F% w& \9 O& }$ v$ cd modsecurity-2.9.1/
& T3 R" ?' Z- K2 x5 I& m p9 i$ ./autogen.sh
& M* L! X8 i* t# W: Y9 z$ ./configure --enable-standalone-module --disable-mlogc
2 \, P4 \/ b7 q% k: d$ make4 s% Y0 e$ {9 h7 w
编译安装Nginx并添加ModSecurity模块3 y* M0 P# H0 D. U+ m9 N" O/ u
$ tar xzvf nginx-1.9.2.tar.gz; R# B. `8 h. g# c
$ cd nginx-1.9.2* T O8 @; t) V4 v) w' m
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" ^; b$ f% d: T- c: v
$ make && make install
- }/ ]! l! [- q' @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" U3 @& |6 X; t, J4 }2 ]& D/ g1 Y
. z& c0 I8 x: i9 _
" |+ W' r: s. E& v- |* V1 g9 K2 q编译LibModSecurity(modsecurity-3.0)
2 ~: f( b; I! W1 _$ i$ cd /root" l4 U; D" a2 s! F; {1 ?
$ git clone https://github.com/SpiderLabs/ModSecurity6 W0 a) i4 O, s9 \
$ cd ModSecurity
8 u8 b! f8 j5 C& _/ ?* f$ git checkout -b v3/master origin/v3/master
. H2 N* e% h) F: U8 t9 G7 p& E$ sh build.sh
: ^2 h: i/ }8 ?6 h$ d$ git submodule init
8 v% n0 g$ F& K( V2 ]% n$ git submodule update
: S; f. z x6 A/ O* Q; I$ ./configure: |9 I& `8 c0 C! L7 u6 g3 a9 y
$ make
4 O0 a& I$ c- R" L# T" C1 M. |$ make install
& z7 [4 h* H) q: t BLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
" q! q- @! S- @ x3 f
1 e/ \5 O) c: ]. g! b8 Z$ c) Y0 s( {3 M. i
$ ls /usr/local/modsecurity/lib
. V5 Y5 }: t' {, `, Ulibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.04 y2 c: _. M3 J' R
编译安装Nginx并添加ModSecurity-Nginx Connector模块! x. E" U+ o: @7 y- W
使用ModSecurity-Nginx模块来连接LibModSecurity. `! H+ C# o: d1 A
! N' Z& Y9 C9 D# |0 k
: u7 n; e" M( M& B( @$ cd /root. ?1 p' k" L( r% K
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" S6 {' [7 a8 l
$ tar xzvf nginx-1.9.2.tar.gz8 {- I. s6 x* B e; b( G" R
$ cd nginx-1.9.2+ f o# {5 l9 ^4 h$ I/ S8 j
$ ./configure --add-module=/root/modsecurity-nginx
) T8 s P: x- e- {* P4 Z1 d5 X% b$ make: T" P# { Q" J$ y: l
$ make && make install+ Z& m) @1 j9 n# l0 q
添加OWASP规则3 R* d1 t5 J7 V& R& _! b
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
* ]( g7 ?7 z/ Y0 e Q
0 G4 u, O( ?, W* o
! U8 b; a4 e. z3 z1 w" Y+ S下载OWASP规则并生成配置文件
, D$ D% P) H0 l: l6 ^$ f3 P$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 z' t5 ^- k# n; Z$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
, x0 i$ ~+ l9 K! ^# X' s. [$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- L) Q- n6 F8 B7 ^7 `1 n1 K; K$ cp crs-setup.conf.example crs-setup.conf7 k* f. g+ B) g8 J# v. n
配置OWASP规则2 t( n. ^4 S1 d! r, _
编辑crs-setup.conf文件" s" S, ?' a7 l( L
! r1 d3 u F7 r; c" r; |
4 [* V4 n+ G: ~7 v0 J
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf) ^3 j: E' j% R0 o1 m$ b8 g5 E
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf( N- [# j) p7 T, I2 p
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
) ] f( K( k6 X/ H7 Y$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ E: D3 T, a1 |! V% E5 E2 J& F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ k( P. l4 n$ F V& r+ d4 A: n
! l% d4 W4 G+ l! j
3 @; e- Q5 n; \4 z- j2 }! }- [$ j; T
启用ModSecurity模块和CRS规则
: l4 g& h) l9 r$ z复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% X1 j7 L' q r# n& V
1 Q6 |, {) Z3 H7 i7 t" e9 m
}0 i; U/ m5 W: S" Q1 ^3 U0 Amodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
8 C5 a( j# F8 E: E2 i0 O
, p! Q8 P4 @/ d: [/ t6 h4 q
6 m0 q2 c( t; [$ Y$ cd /root/modsecurity-2.9.1/
- L, U; Z. C- [. o' M$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
7 ]& g. O1 w5 {' u) w5 E$ cp unicode.mapping /usr/local/nginx/conf/4 V3 D% ~, a3 h
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( s* G+ q1 H& Y- ]' V4 A+ y: \( h7 ]- u( S2 g
- k2 p+ o) R+ S# M2 x0 i$ vim /usr/local/nginx/conf/modsecurity.conf# J2 U( P& ~) f) B/ y7 A
SecRuleEngine On
9 I8 n6 i& Y+ p6 w3 u- RModSecurity中几个常用配置说明:. w: j- [1 x3 H) G, |$ h
( Y4 m r( N( W, O7 w+ Q
0 u4 L$ A! J+ w+ i% z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
\1 Z. {+ A* L) t8 p& U/ @; w6 W: Q! d* I; b
3 d/ P5 m) S6 ~% X
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 Q. O/ F: v' ^" K/ _$ R7 {; K: i% L y- [7 a( y' ?
6 s- I7 O: s& @6 c3 X2 k# p3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ ]/ K8 {& r1 l1 w9 s2 W) z
/ r! B$ h% l% O) | Y9 u1 ~& t+ `% [
8 l* W; f2 T! _# W a" M4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。. m @" j1 o, M; O, u
5 e5 N8 \( b8 x
4 n5 {5 |. |! b4 x- }& ?在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
8 g* y2 \$ o( [* Z: I9 l q9 ^* B4 |8 e0 V \0 g
/ E+ t% m5 L! B9 w4 L- |4 N3.x版本CRS
7 A/ _9 _# M4 ^* W: z+ z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% g- [! A: U! I
# 生成例外排除请求的配置文件
% f; @( v8 v3 u' H/ y$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ ~" a4 }/ \; n& y0 E3 c
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf) y: b& f! v7 B+ O% C: d0 K
$ cp rules/*.data /usr/local/nginx/conf' g$ L; o+ W j D, F5 I
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' r& D2 x8 G2 r* S; Q, W" z& `# N, F% k9 E3 s/ X
7 h5 s: Q, y$ B5 D
$ vim /usr/local/nginx/conf/modsec_includes.conf
- P$ N- l8 w: {: z2 F8 t) s5 S2 B3 @ U8 T& o
[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
9 u/ O$ E7 V$ u7 i/ e* u) l3 V3 X8 Q' V6 ^* ?
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。- U2 x' A5 \7 c [- U) t/ `
0 N0 \3 z4 K+ o( X& L
, p2 j) T' o0 q4 G配置Nginx支持Modsecurity
. N% O3 o% B3 L5 `6 `启用Modsecurity8 |* P- `* ~- Q0 P5 O( ^; `1 z
使用静态模块加载的配置方法1 `8 I4 N" j6 v; P
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: Y b; Q- e1 @* w
8 S+ u. c% V6 F! v/ V/ _/ H6 M# |3 r' j0 n. m% A1 G `
ModSecurityEnabled on;
& K: a' k, \' \7 k! I& oModSecurityConfig modsec_includes.conf;
/ r1 F. \& G' K, E O+ b, G1 p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 [ ~+ r$ e, Z/ @; x, W# x6 B( L
; h# { a" d8 B4 F) Q+ {+ V* ]4 j$ P3 K3 ]4 c
$ vim /usr/local/nginx/conf/nginx.conf
; u& B, L% [6 p* n. _1 M+ @
& r, T0 Z0 V/ c3 t6 C- V0 C0 X$ l, Z% ]4 P! q6 S% R1 u
server {) W0 D# e/ _" X' N; G7 |" W
listen 80;' e8 g/ R9 L* n, c7 U8 ~3 u' t! }1 f
server_name example.com;
9 I% `$ B# A- i) b( m! q9 P0 O; A6 ^
3 b5 m Y, q/ e; S
location / {( D: |" _& P, Q* z0 j) v
ModSecurityEnabled on;% k% S3 h, I) }* o$ o) B
ModSecurityConfig modsec_includes.conf;( {) ~3 C- `1 V% e: a+ Z: b
root html;6 U1 E) F' r5 c
index index.html index.htm;
* v- X6 }: k0 l }5 W5 g: o& U4 k$ L
}
" A1 q I) `- g; g8 g0 _+ r4 l使用动态模块加载的配置方法
$ p# u! K# C' S& {% X; i在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 D; L J) p$ X; P7 I/ |1 G
* g! G. v9 O( ^
2 d2 i2 D/ V0 {! wmodsecurity on;: m/ P, `5 q; z
modsecurity_rules_file modsec_includes.conf;
3 f( O: K3 r5 [- Z0 L" g+ J" j修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) m Q3 n! j; |2 T' m) R' R: s
' x; x# A$ p2 p8 b& ~3 W6 f" b; i( s- Z" g5 {1 E2 O3 a
$ vim /usr/local/nginx/conf/nginx.conf
0 ~2 U2 p: [% W, _" y [ x! X9 L5 q
# [; M8 }; `# t% B. s( v# b# G
server {
" U% B* p0 z5 f/ O1 s1 H listen 80;) G4 Y; T/ u) ~2 E
server_name localhost mike.hi-linux.com;
% t! F1 w, O9 _( s access_log /var/log/nginx/yourdomain.log;5 V0 p; T1 |' \% r
$ x# s0 o5 v8 [" H- [7 R, b% ]" `# K" `' A8 i
location / {
, Y E" m" ?, Z' `: B" M0 k
& k6 }( S( I% |
9 p" Q4 K6 Y" Z; Y2 V" k: Z modsecurity on;
% a* a' T& N5 j modsecurity_rules_file modsec_includes.conf;; i- p% y, y4 i# s' e8 f- K
root html;- X; q& \$ A+ c D3 R+ ]. J
index index.html index.htm;
: f( r2 y, a/ W+ p y) \. C, O3 f}
3 y( E7 V1 ^. T, e! @- S" ]}1 E) m& J8 o: L
验证Nginx配置文件: P) A) [* Q2 q; T' R! s8 u
$ /usr/local/nginx/sbin/nginx -t; f% ^* F. H9 s; Q- `& Z% j
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) S, L2 G& X2 I6 w8 ^, y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful/ ? \7 e1 ^4 g* G" K! \
启动Nginx
) k9 O0 Y3 ~/ h, I L$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; m' r( Y+ s' S& _2 }
, |! n1 l3 P2 k! k7 x1 n
测试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能够战胜更多复杂/未知/混淆的攻击模式。
3 h( g/ H) j( Z5 C/ I e |