ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。2 z. f. T) y, h; L, b4 F: r
; W( ^. `1 R- j$ i* H" e5 n
5 c% L8 r1 k2 ?- o0 j+ P) K在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& x' E2 C4 \0 Y9 _- b$ t- V
& [1 U3 @& G0 R& Z2 X
& [* l6 i- W1 Z5 g什么是ModSecurity2 k' k0 V. L9 D8 D$ E
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ u: V7 a( j: [/ }2 \# M' X
. q3 L- K8 T) a/ o5 Z. s. e2 _$ e
! M, ~9 O3 O: c# `( LModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。7 |0 E/ I/ g& f1 D+ }
$ @! M4 L- o, E; }& O3 g% X) X
$ \# t* c0 a( kModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
5 b* ?- L- u% n- k- L$ W T# c" a
7 y1 i9 E" H: w3 O$ V
官网: https://www.modsecurity.org/3 r9 x& r3 U0 V) h0 ]
8 p- s2 c2 H3 c$ F" |* I Z( I
' b: v: C1 @7 L3 A& v, b* N) q什么是OWASP CRS
?5 E+ h8 A( F3 t2 JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 \# f& n8 |3 ~0 B
: C4 G& D0 r9 S$ m( l/ W
) F& G/ f) ?9 O. k- ]ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
: q) g( L( P, y! ?# ^3 d0 o) b4 k: T# K) P0 F6 M- n& R
; ?6 p% F; ]5 N6 X/ f
HTTP Protection(HTTP防御)$ T! n7 {1 i+ r o i. Q( U
HTTP协议和本地定义使用的detectsviolations策略。# d2 w0 t# i8 C' L( s; B" A0 @2 [
" K7 f' @7 O, a8 K9 C
$ S* ]- u6 B# ^7 q, S
Real-time Blacklist Lookups(实时黑名单查询)
9 J+ \, F: B' ?- Q1 l, ]1 X4 J; x利用第三方IP名单。3 K+ A P2 n5 g
( d, t5 Q H2 _* Z7 A; b
& h( W- c& I! i9 z( D8 Z
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 ^8 T8 j0 b) v* [8 u- Z0 [4 c+ A防御HTTP的洪水攻击和HTTP Dos攻击。
- M( g& p* G# d0 s) X; |% \( m& j- E5 k, j& P
( ]! z* h5 @5 b4 Y# U* X
Common Web Attacks Protection(常见的Web攻击防护)7 j5 a3 E# N$ P9 q- d
检测常见的Web应用程序的安全攻击。
7 m7 V. B4 B7 p8 w) G; z, m/ Y! U3 R3 q
) e) D2 F" _1 n# t0 P/ TAutomation Detection(自动化检测)
# I) b1 S' f' c+ Z% }) Z3 k( N/ D检测机器人,爬虫,扫描仪和其他表面恶意活动。
* g- C# v! z( R9 D L) H5 `. q: F
8 | j. _, H9 {, ?( X3 }
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
! L% u, ` B- ~6 m+ b& @检测通过Web应用程序上传的恶意文件。5 A) T* ^' y( u* u" G1 F! Q
" \) l0 P% N7 K2 w! l0 n: V% K& l
+ Y3 Z& w3 _ x' V0 ]Tracking Sensitive Data(跟踪敏感数据)9 o3 J. `7 C* \' V4 ]+ \& [/ c
信用卡通道的使用,并阻止泄漏。
. r D5 Y4 ^7 I, y' z/ H( _- H; s4 w& a8 J" x, [) q
; k- g D. H$ k1 _" N- _Trojan Protection(木马防护)% d2 W2 u; I* q4 W$ @7 o
检测访问木马。 M* A7 ]3 q4 ~- |1 C/ C- S
2 ~& L4 Z, M% H. ?9 t1 L) X: x# d, j7 q# t3 q, k3 V+ A
Identification of Application Defects(应用程序缺陷的鉴定)
3 |2 Z" A- M0 V, S1 Y* }检测应用程序的错误配置警报。5 |' J" m7 `, U6 {
; x& E1 A G+ W5 s; x4 r8 ^9 M& w
Error Detection and Hiding(错误检测和隐藏)* \: s9 s% B0 N, F* G" m
检测伪装服务器发送错误消息。
# }/ X. z) P; o0 s
4 i! A& M S/ A' |, V& ?! t% _; o6 N% _" W6 B& p$ u9 w5 A, M
安装ModSecurity
! @' m' T& K+ H! H! ^! E软件基础环境准备( H/ @0 x! D: g' u1 E
下载对应软件包
! L7 g1 T; p( l: X' Z, c( E$ cd /root
}" ~3 l+ g( j" [2 q# a+ |$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, H: y T- W+ L3 B2 {$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz1 u+ g* U% @/ \; i* {& e; M) a
安装Nginx和ModSecurity依赖包
; X8 K2 O% M; U ICentos/RHEL' t7 t6 i9 S# a3 S. i0 y, }% K4 @
! b$ j+ W* }7 W5 W& z4 w: d6 `
7 N3 N: [& O/ @0 t$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) r r H6 A- l' DUbuntu/Debian
# [; K! ^; Q! v7 J- U8 ?: @' o* M% _4 Z: ?
1 ~% g2 l9 ]2 n" d' n0 j9 {( m$ 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 c& M2 T4 p( P, _; L) g: u编译安装ModSecurity
r" E' Y5 l% j" r( KNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! U4 m1 f; V) i4 I$ a! ?
2 D+ S y* ~9 v
& |3 n8 ^& t( @方法一:编译为Nginx静态模块
% q" } x; u+ {0 |
7 {: P2 z' W) t# y# K+ B
8 [9 z0 S) J! d' a1 X+ K编译为独立模块(modsecurity-2.9.1)# d9 n6 V/ o# s
$ tar xzvf modsecurity-2.9.1.tar.gz" k( }: T6 p* r4 z2 N
$ cd modsecurity-2.9.1/
. @. c8 r1 `2 H( ~" }$ ./autogen.sh
; w9 u Z* Q' U% ?$ ./configure --enable-standalone-module --disable-mlogc, W/ z+ N+ [9 }$ g9 S$ |
$ make! X. r4 T& z+ f8 K( g. ^$ t. N% `
编译安装Nginx并添加ModSecurity模块
R( j8 c" M1 b$ tar xzvf nginx-1.9.2.tar.gz+ s- R; P7 G, S- O5 U& l8 ~
$ cd nginx-1.9.2
2 x: b; d% M% [6 ?9 j0 c0 O2 @$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/( j" {; E* q9 ]
$ make && make install
8 q4 T# [3 x' n3 x+ }方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
$ D) K+ T) m% t$ E9 d0 ?: I6 q
" I9 R0 ~) l/ q* A \
. C0 N L8 g% P! ]) l编译LibModSecurity(modsecurity-3.0)
0 B6 ? u3 p; p0 X% u$ cd /root
) h3 s# x- g+ j2 F4 x1 ?; }$ git clone https://github.com/SpiderLabs/ModSecurity
0 t: b0 `% k# E4 T6 s: M$ cd ModSecurity* R! e# @8 V+ J; n6 K1 ?8 @- i
$ git checkout -b v3/master origin/v3/master/ J7 S# C+ k. J' Y0 @" @
$ sh build.sh P. Z9 i$ `) A! @6 c) D
$ git submodule init
- _+ U2 P& ^ \) [$ git submodule update
8 N- L; N9 T* F$ ./configure& U4 G( S, X0 n; _) P
$ make/ H* P1 h9 M# o2 i6 `
$ make install
2 ^. q$ T6 Y+ U! uLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。) i8 m7 L" [) V4 w
6 u0 [) }3 y! ~7 O, W5 r, |1 r) N' s
: Y% r# ^6 \& t% Z0 N% D5 {$ ls /usr/local/modsecurity/lib3 O& ^! H3 H7 X0 e: g( m2 A. m: A
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
' @. c. A8 I, U4 W编译安装Nginx并添加ModSecurity-Nginx Connector模块+ E8 G# {3 w% q
使用ModSecurity-Nginx模块来连接LibModSecurity7 [) }# W N7 k/ o: t& w1 e
$ H7 \9 j0 ~! ^2 |$ y" @/ ?9 q2 E' }5 i& R$ n ]( d( h* A
$ cd /root
9 g0 R6 G5 _4 ?' }. r3 ^$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
. h" f/ k+ S$ i7 [& E$ tar xzvf nginx-1.9.2.tar.gz
) S2 @9 [: P' N8 U/ {$ cd nginx-1.9.27 f Y& k a D, j2 s
$ ./configure --add-module=/root/modsecurity-nginx: H/ R3 y0 q' A# [% K, c0 T
$ make/ f: M3 _, d9 `4 R: J4 ]
$ make && make install. ?$ {$ z- a: O* x+ n& z* W A
添加OWASP规则% E5 M# S1 \: B- m+ C; y7 g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。 M5 r1 o( U; f8 e
( G' L' v: ?3 Z+ I
3 w/ K; A! b1 K9 d N: O下载OWASP规则并生成配置文件+ p; m9 ]% n1 x, o r
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 K* p7 O# z4 m. @3 x$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
9 C! {; r3 v7 Y. Z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% h+ Y, R! o. L$ cp crs-setup.conf.example crs-setup.conf
/ U5 q1 Z, E: L# u配置OWASP规则3 G! e- p+ u2 j! Y5 n5 d+ f# ~7 g
编辑crs-setup.conf文件
. @& w4 l# |; d$ k! q0 H& @% _# A( ~9 ]( l
4 U/ E! }' B3 ]1 I, o+ G: q7 O
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
$ a v3 ?. P; D6 o- b4 S: i h7 L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf) Q6 z- L1 g0 O
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf' B- O' {, N$ {% w$ n
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* l1 x9 \/ j, V6 K
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 m' @% X1 k4 X- {: n
8 l' K8 C% n/ r2 K4 _/ j
7 ^- N! N. t. A& V' ?8 s8 s启用ModSecurity模块和CRS规则
5 W: Y i0 H" m复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; i6 X* N- G/ H% B
! g: V3 B: {3 p ~7 I& I" n
1 l* g9 x" h0 E0 ?! D0 n
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& V7 o, C5 m; v' q5 e
O3 ^4 _& _ b' X' E, }, r$ b( ~2 Y
$ cd /root/modsecurity-2.9.1/
+ a' N+ m7 H6 l2 |7 g" u9 E; C% l8 \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 0 L: f6 w, S/ U6 p( J3 O: K
$ cp unicode.mapping /usr/local/nginx/conf/
: O% T, C, ^; W4 _将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
: R" k* R3 R2 Y4 u% I8 G# @6 u; ]( _9 l* A( p1 l* Y [1 [! r1 I
8 v5 g0 X5 a# J2 D/ u, {9 s$ vim /usr/local/nginx/conf/modsecurity.conf
" i, f: h7 G/ D* G# d6 h' u) p0 h1 SSecRuleEngine On- w w% [" V" g( p O6 b8 K) N
ModSecurity中几个常用配置说明:
; ]+ V+ T5 ?/ R: V7 D& z0 k/ \: C- K6 C' y9 S Z! t; h9 j5 J
0 ~7 p6 x3 t( f9 g: @5 v; H
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* I+ |2 d% `- D* o- @
6 `8 R8 G$ ]; I+ T* n3 D% S
7 d0 F A R/ o, k4 ?
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
- k( `1 R, x; z# u0 i4 y/ g- B) O
& s4 C4 n: q/ E+ n$ o
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ R* m* p; J9 D7 ~ i& x9 J3 g
1 b. @& O0 `2 j8 M4 L! V
^7 i# `6 N2 c
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: L( z& E- c8 V' d! K
. d2 T% N' M4 J. v4 A ~
, g# r0 x* ~9 Y4 v: x" B: p
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。3 V9 j7 B1 n2 F, f. w' H+ Z( \0 v+ A
4 L+ _4 p0 W9 o
& W! |4 q6 I6 e' }3.x版本CRS7 A* b7 L7 b, k8 N
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 z- Y4 w5 n5 ?0 i/ `- N5 n3 s# 生成例外排除请求的配置文件. b: X( Q3 Y2 F& i1 b. V
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
2 ?; T; }" m2 m0 M$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) O/ P: R# L1 m4 C- Q$ cp rules/*.data /usr/local/nginx/conf: P4 [4 R0 x4 g4 D/ Z3 q) x; ~. E
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。6 B" D, ?/ V" i6 O G' f
0 r# i. E" X5 h7 w# d
) k5 ^1 N \6 S; N5 ]$ vim /usr/local/nginx/conf/modsec_includes.conf
+ n2 C9 B3 e y5 @! @
" ^8 j3 E% G; u2 h- S9 P* r[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
4 m2 v" z( S' D" G: j! ]' B4 j
1 x+ U, g' p4 ~- l0 v注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。' _3 `, ~ y. [: _/ p
) d/ [0 Z7 c7 _ q2 F5 r- M; e$ [! b8 {% C' a$ n) V4 V. o W
配置Nginx支持Modsecurity
/ B3 }" V- \# a0 W. n, d启用Modsecurity0 \! p- V: s! s5 P" X; h
使用静态模块加载的配置方法
0 l2 `+ z3 l8 ~8 }在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 h+ C/ a, B7 ^' y D0 A4 _) E& j
5 ~& ?4 u5 _% M* ?# d4 O& d. _5 G3 V" ~* ]: M
ModSecurityEnabled on;7 j S0 X) h4 ]8 j5 m
ModSecurityConfig modsec_includes.conf;
0 j* u0 V6 z P修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ ?8 x1 K% X8 c, S5 i- S
- j7 G! \! _: U' X& f! |3 C2 f# m: a6 O) l
$ vim /usr/local/nginx/conf/nginx.conf
1 a" x' ^9 L/ g
( {# k! B0 y, c1 {: e4 ^$ e2 }$ i0 B4 X
server {
2 K4 u( D' q+ b M+ s listen 80;
0 _; @+ [: R# u$ W0 O server_name example.com;
3 I9 }( P9 g/ l( T& X+ Y4 C
# o& {0 R* L' ^# _6 p. Z! A3 _
m; F3 K5 M9 W" Y location / {- x, F o9 M. _* u$ `8 m7 E y$ e
ModSecurityEnabled on;
- @6 Q: U5 q" {2 ^' j ModSecurityConfig modsec_includes.conf;
3 E3 S( g4 t8 n" Z: K$ d9 S root html;, |8 A& t0 y+ e
index index.html index.htm;
! a3 g% s( r, _3 [8 q* J }; B3 m* E4 t; H S F% ]* O/ k
}1 `' C/ z; q7 t7 ~, q1 j* @2 C
使用动态模块加载的配置方法' G. @8 I: \& n; k6 e4 P
在需要启用Modsecurity的主机的location下面加入下面两行即可:
) o% L% e6 _3 j2 v: @* H% Y l5 v3 G7 r6 O7 W, Y6 D$ l
! F* e0 p6 F# b- Z$ u/ Kmodsecurity on;9 V( M$ z: x* Z" N
modsecurity_rules_file modsec_includes.conf;
0 s. j% j/ K; v5 z2 g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# R: p- F' m1 x) ?" O. s$ s$ k+ }$ b" @/ i: S! p# a6 P+ J$ A
+ ^& P3 c0 T' g3 C8 n" |. ]$ vim /usr/local/nginx/conf/nginx.conf4 C' \8 {/ x9 P
5 ?7 A3 g! K7 L4 U) p3 Z
" e: U$ C! z- y' Y5 hserver {
/ P8 _* ^2 t Q' L1 N. v4 O listen 80;
/ N! Q7 i# B. ?' V$ m$ \: O* Z server_name localhost mike.hi-linux.com;
8 R7 M+ k1 n% X7 I3 _$ X: ? access_log /var/log/nginx/yourdomain.log;
1 \5 {9 [& i" E
( L& E9 C& M- _2 P8 E
: Q' h1 }* f9 P% q: Z# u$ i location / {
6 X7 k' W7 P# J* A9 T) D: A6 h9 X
5 Z0 O( i9 M1 f% @; D3 ]
, P! h* w9 h7 s# N, l6 g3 c modsecurity on;
" B3 _- B+ c7 [/ Z6 `. G+ _2 b modsecurity_rules_file modsec_includes.conf; N& \3 S- @) j
root html;* _9 ?( |1 b o7 }6 ]: x
index index.html index.htm;
# x8 [* f: q2 A# D}
: U s! a; l( }}3 g2 w9 F! W$ n: |$ Z/ }$ @& o# Y
验证Nginx配置文件6 s6 K1 V2 |8 }9 `
$ /usr/local/nginx/sbin/nginx -t3 n/ A" e1 g0 s
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2 y- h9 N( l% p+ r" x/ p6 A0 L. {
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful+ W% j. Y# U2 |. D5 B ? y
启动Nginx
6 l" |3 Z. Z0 O) c$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf( y. n, t" s# y+ [# D5 }. ^
( d4 v7 o5 O' V( X3 j测试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能够战胜更多复杂/未知/混淆的攻击模式。 - x8 L" z. U5 j# ]
|