ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" ^, c1 o6 F# k$ a0 E
" A6 j5 m3 T" F& |
" R/ H) T' H7 z5 X) D7 f- |' \& K) J在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ ^. i& M2 e$ C7 r2 M
* Z" F# w" x) P0 r i! G+ m. J$ f. ^% `3 s# c6 t) I
什么是ModSecurity3 O5 q3 t1 r- v4 v: k' C0 S
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。1 K9 C: E) g+ z* a6 M8 c$ t+ F" k2 x& i+ ]
" q/ M/ E- ]- D& a3 j
' Z/ c' {- o( v h. ?& }
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 z' I; |* A' V5 i, v8 v; U
$ `: x- @+ K6 _( p* ], F1 K2 @. p* c# h3 p& {; {
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。 i8 P% E( @% [ F' V8 J
2 [ ]$ _( t+ v% Q4 b
* u/ L5 ^. K) |2 G! u! v
官网: https://www.modsecurity.org/0 y2 B+ G8 ?9 N% o+ [- h) ^/ {0 r3 v
* i$ T1 Q# e" B" u
$ w/ C9 z) D' R( t什么是OWASP CRS5 Q1 C w! q% }8 c# T1 i. N
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
* Z0 ]6 [: Z0 S$ |+ C3 L8 D% Y
1 u4 v6 z- x7 f( U8 W7 n$ V# s' [2 h7 e& n3 V2 l9 l! Q
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。7 W/ e8 ^* `9 M# P! s
, h- Z& I5 p1 C" |" ~6 |3 w2 x0 d7 W
# L9 r9 d6 a8 j2 @: Y" e( Z
HTTP Protection(HTTP防御)
) x& z$ k2 {# E9 { OHTTP协议和本地定义使用的detectsviolations策略。
* G4 W( T- d8 k* @1 v7 `, N
' R! W; ~) N- G2 R. {$ ~) u2 N9 d a6 i; s2 w
Real-time Blacklist Lookups(实时黑名单查询)
% c* R# I9 P j# F% S利用第三方IP名单。' F4 i5 i+ j- [( w- j
, L/ ~: C6 c1 I/ ~8 a$ g& K' E' \
0 d7 _# c9 Z/ L W+ zHTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 i" n' I) c; Q% l, f0 @防御HTTP的洪水攻击和HTTP Dos攻击。! U7 B: Z! R' [. q( `- R
/ V- y$ t5 h$ V
0 q& Q }- d0 {) PCommon Web Attacks Protection(常见的Web攻击防护)
+ }) j/ E! t) T; ?检测常见的Web应用程序的安全攻击。
4 P3 {) o2 b: K: r% n8 p5 F7 l0 J4 l
9 c0 G- H& r! s# Y& X% gAutomation Detection(自动化检测). h) t4 U- K1 M. j9 S
检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 Z7 j9 L' }' ~ [" H
: R1 R- V% A. a
& L3 o; K9 `5 B q7 C) E8 v9 T; bIntegration with AV Scanning for File Uploads(文件上传防病毒扫描), x4 |) f9 R. Y; W: V/ O4 i
检测通过Web应用程序上传的恶意文件。
( D- A" s- `5 I
1 @4 H" n( j0 C) K7 ^ c3 S% V4 p' q& L$ ?/ m- q, M; O) d
Tracking Sensitive Data(跟踪敏感数据)
9 s0 s6 x( R# G) H信用卡通道的使用,并阻止泄漏。
' @- V% D \$ L3 | H- @; r" v, v8 W' u
. ?( D4 t; X# j! d9 i5 ^' lTrojan Protection(木马防护)4 a9 I6 S6 _4 q8 `7 T
检测访问木马。
" j1 M% ~9 Y n' _! H8 t- Y. T1 b0 M4 i# n5 x# J4 c; H* m/ `
. O; @ Q2 c- e! H1 T8 e2 f7 s M/ [
Identification of Application Defects(应用程序缺陷的鉴定)
m( z- [6 x: S6 W. Z" W6 W# w) W检测应用程序的错误配置警报。
+ x; }4 o9 e. X0 ?( z
+ n& h6 ]4 ^, p& L* O
0 H( K- d' t, J% }Error Detection and Hiding(错误检测和隐藏)
. m! u3 S+ q6 u8 b# r6 s检测伪装服务器发送错误消息。
3 s6 \" O6 W5 W4 n9 t! N; g7 m- X. n0 I: X; v& |. j
( A5 m! ]- a# \& K5 D+ W$ q. j
安装ModSecurity
, a. ~' g) x* s8 z8 [3 U8 r软件基础环境准备5 a- H6 B" m, D& _- r- A( h. S
下载对应软件包
1 `0 K# A8 G9 i$ cd /root
/ n7 z. n% i5 F2 n$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ O: f: }" M3 \8 G5 j+ t6 x( T
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz2 b. m i1 n( u: O6 Y# c K; y5 c0 O0 w
安装Nginx和ModSecurity依赖包
% q( l* |+ {$ H, _Centos/RHEL. T" J* o& f8 F& P6 ?
' E J4 R+ |, ~3 d* r9 K
( _9 ]8 d% v2 W8 p3 l$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
, T5 @, U8 i2 F7 X# _/ H; f PUbuntu/Debian) b7 ?+ m2 x) P- Y5 }4 \
1 Y& d9 d" D$ o3 Z+ K
" u4 u. C8 q+ A' A( m* J% Z- s! M! y$ 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
( O" p+ G& [, @* V编译安装ModSecurity8 }( f2 @' f0 y' ?
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ x; `( Q* r# L/ E1 x6 y* A0 l. J$ I& C/ \
$ _( O1 N! ?* M! B. t) u
方法一:编译为Nginx静态模块% k! w# q. N, m$ K
+ N" A' }/ U7 y0 y0 a. k( u4 ^; v
' i1 h+ s6 q' _3 v q6 e编译为独立模块(modsecurity-2.9.1)1 z+ A A. @/ `( \- z* S' s1 [
$ tar xzvf modsecurity-2.9.1.tar.gz
|/ L( L2 t! h1 ?: f5 l$ cd modsecurity-2.9.1/
* w6 Y9 D5 C2 w' p3 d% J; c$ ./autogen.sh9 E- b1 x' J- o5 |- {1 f7 U+ _4 V; B
$ ./configure --enable-standalone-module --disable-mlogc5 D F' d" t; d' K: u& u X0 a$ a1 S, @
$ make, J K/ A; Q: n" h) O
编译安装Nginx并添加ModSecurity模块' s5 x3 ?/ {9 Z# F. ], y
$ tar xzvf nginx-1.9.2.tar.gz
( Z2 \6 W5 L& q9 o$ cd nginx-1.9.2
9 w+ Z6 Y/ @0 j6 \" o' \5 ?- K$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
) j7 ], U& |1 e7 ]* n6 S$ make && make install1 x$ I: r9 g6 y" h# ?4 Y- _
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, v& O; ^' ?0 n {
0 `. \! P+ ~; v% T H/ q. `$ }1 J. @2 j
1 I, l: X" x+ z1 \4 H4 Y编译LibModSecurity(modsecurity-3.0)
3 b B) Y* m! T! L$ cd /root0 i1 b: z/ @! k( w7 |6 [
$ git clone https://github.com/SpiderLabs/ModSecurity- E) E Q- U( Y% P: B) y; e
$ cd ModSecurity( D5 H! e8 O9 G# s
$ git checkout -b v3/master origin/v3/master0 ?5 G, N5 r. k" g O8 j5 v
$ sh build.sh" r+ m2 e% A. O$ c/ H
$ git submodule init
# |' W5 l6 g$ @# K$ git submodule update
# _ E f, g% p0 y# l$ ./configure
% h+ Z6 R, ^. h) j$ B1 y3 D, }* B+ s, T$ make
5 q' o& \9 w+ F Q$ make install. e, O0 }6 ~' J7 |2 U$ g8 r
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。5 v* F( y; D- v: {$ x6 g
4 K f# C, M+ S$ b* p6 M& E8 w4 X! S1 D- i8 R6 U
$ ls /usr/local/modsecurity/lib
: ^! Q E' S. a( Q9 Ylibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
3 P3 P' K6 j% M% P编译安装Nginx并添加ModSecurity-Nginx Connector模块8 q5 a+ h5 \# [9 T& f& Z6 l
使用ModSecurity-Nginx模块来连接LibModSecurity
: |6 n! ^7 n: Z) x% P7 c. @) [ S# V
. F% \' U" @. s4 ?4 E$ cd /root
) ~5 }& |% G$ z+ Y% Z7 _- {9 [$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
5 g, p1 M6 @( w5 m% l- v$ tar xzvf nginx-1.9.2.tar.gz
9 E# Q6 _# M; {8 s# a5 I3 c$ cd nginx-1.9.2) {& B( o& c5 n; @+ U) s. m& i
$ ./configure --add-module=/root/modsecurity-nginx( H) X5 }3 S. I& B& g* v; K
$ make) T% U& E/ j6 E& f' V( y! \
$ make && make install
3 f( s) H% H3 o5 M添加OWASP规则
: p2 s! Z3 B$ P$ T. X- CModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。( l; p* j1 A$ }2 \. q
) e; o1 ^# k0 X. `
# x& o2 P, _: q1 k
下载OWASP规则并生成配置文件$ R0 N+ y- q7 f `5 d' g& x# [
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
- [. S9 F4 i4 l; o$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/7 I" y7 v/ _, s3 w# r. ~# J
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 \, B# K" _$ y4 |: M6 r$ cp crs-setup.conf.example crs-setup.conf
/ T3 g/ J' I) m配置OWASP规则. R4 ]( T, R$ A- b! U$ K1 ~8 T; u, }
编辑crs-setup.conf文件4 q8 }, f2 Z7 J( z9 I/ c
0 d/ X: z: r0 v+ W1 k
" E Q/ ^4 _9 _4 R0 n$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 l: Z$ O( Y: u! d$ d$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' |, E0 ?- y7 n# r# g5 I& Z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
9 G3 l- {5 `7 |9 ~$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf- ]! C" l# P' \& s$ l: M
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。$ X5 j4 y4 K! c
: T) ]+ G& A, B+ ]/ b* |7 l% o2 H& ` k0 w
启用ModSecurity模块和CRS规则2 z2 q$ s' B+ x! ~
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% \) u2 W- J# A- D) u
/ [) E3 ^+ Y4 b$ @2 Q. C1 q" c
% I3 e! C! R8 [7 z* f" o
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
6 I. N6 R( [8 |' ~$ e; k1 ~3 J$ N2 T) z* m! a6 J
. l1 p; `" D2 e- w
$ cd /root/modsecurity-2.9.1/
- S- z. U4 G' l6 v" e4 J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 0 `& E5 G0 n' g3 F9 z2 _
$ cp unicode.mapping /usr/local/nginx/conf/% u9 _* F' V8 R; P# D
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 u) P& q. \# ~: l9 U' B# R
" M' ]0 @0 C5 I! F) R$ K
+ u% m9 @0 x% |2 ~" I& c" s$ vim /usr/local/nginx/conf/modsecurity.conf, v, B, ?0 U: j+ K" T& W W$ l: ]
SecRuleEngine On$ r) i1 _& u3 ~! }7 z8 ~
ModSecurity中几个常用配置说明:6 `- H9 W8 j& F" N3 D8 F/ w
& q. p# R: a" m5 x9 M
) Q J$ y6 w$ A3 U8 _7 H
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 w' b4 M0 Q. c" A* j+ }6 W5 I1 V' a$ o+ r
) k$ ~) D1 }% V" c& t0 c8 T5 a4 C
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
/ R! B( R: `5 U/ t( R
' u$ \2 H( I" C3 K" G. s6 g6 w+ g/ E/ A; ~
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
# V, f+ O0 w, ~! F! M1 D, R
$ R7 O: c0 f9 q5 D# }. g+ h
( R, r1 w6 S; v G4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
8 a% D) K& g7 K! M- K. f8 h* ^3 |' F" b9 m4 F& O% G
5 z! S' m4 C1 ^- k$ R8 w在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' M* n% |- O1 V1 c/ X
3 |, W) d/ y* {" y# }; n, K e5 B j' F6 q1 v6 Z( v1 k: n5 A
3.x版本CRS- t4 U% S/ b6 J- ^, j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. @8 R N; o1 Q |# 生成例外排除请求的配置文件: N2 _) g6 x6 |4 p
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf* {2 E5 D7 g k7 E! E6 I
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
/ `. q E1 p, M7 P9 d$ cp rules/*.data /usr/local/nginx/conf
[% G' q7 v5 }1 y为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
0 u; `/ ^3 y$ O7 L: {, d2 Q, y$ [9 f! a& ^
; |/ }% E2 X' f1 p$ vim /usr/local/nginx/conf/modsec_includes.conf7 R8 V9 Q! I3 m1 X
& v" u: ^& s8 C- 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
" p+ Z1 Z; k( A! V* K3 c+ m
$ x( }# l& |0 O注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' L& t- R8 y# l, v
- W b" j# h1 v- L, A1 x# h
- l- l9 C, `" h; C) G配置Nginx支持Modsecurity0 i7 F& P5 K" R# M& i
启用Modsecurity
) t- O" V6 W! T, `使用静态模块加载的配置方法4 z4 H$ b. M8 I3 S
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 n) V6 K, `7 ~' f4 V( q
( \+ b3 n ~ ^
9 P7 I! W# ?# _5 S6 N* |6 `3 qModSecurityEnabled on;+ [- C) t8 `9 l) V
ModSecurityConfig modsec_includes.conf;
! u9 F/ n2 l' R8 C修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
4 i. W9 @6 f+ g6 @9 m
( E) E7 y( p7 X# M7 H, s5 C) x" L2 Q- j
$ vim /usr/local/nginx/conf/nginx.conf9 L' _( w# W E5 F9 N
) [: @& r: [1 ?: Q0 L4 Y
- T. w) a8 d: q; y' Qserver {- I, }& A! D# l) R( L( G" H X6 R5 z% J
listen 80;+ X* p& Z* d# L, |' F* Z, u0 \2 z
server_name example.com;" W9 q: @( ]8 \9 F' K( k" W
; U% c; }! L2 R
( D0 i2 S. f; ]( U location / {
* N2 l- `9 k# g8 y# {% z/ w+ l ModSecurityEnabled on;
3 ?! H" P `1 y7 O) n ModSecurityConfig modsec_includes.conf;1 i! Y0 q$ m1 X& z
root html;
+ F& v) P7 Y2 `! ~$ e/ c$ \6 k; ` index index.html index.htm;' p1 R' l* ], s0 l2 A* n
}1 o( [, M6 a' Z* @ p+ T0 \
}$ F) Z9 P1 D5 W
使用动态模块加载的配置方法
' z8 D" [9 k& R) ?& ~6 K% S# {在需要启用Modsecurity的主机的location下面加入下面两行即可:& M, v7 j. \$ j
5 w/ I8 i+ t2 p* e1 F6 ?: o$ p$ O! t
9 K/ t8 h* h3 T; { u3 tmodsecurity on;
2 W1 p: I; u4 j# ^0 Jmodsecurity_rules_file modsec_includes.conf;6 g6 @" a# P" c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! E; u; D4 z9 E1 S* ~; u9 i3 E
9 b& h$ K" ~1 M4 }4 @, E% _2 t1 M4 _' w$ Y1 w" L, P( T
$ vim /usr/local/nginx/conf/nginx.conf
2 D. ^5 A# S7 @: B
- g9 D9 _( S) P* h' x2 w5 |
, N, |* y) ^0 q/ C- [server {$ g0 ]# }) L$ m6 B/ v# w
listen 80;8 f# L6 x0 [% i6 N' i! b4 w
server_name localhost mike.hi-linux.com;0 v# J$ c# ]) N9 |& H7 W
access_log /var/log/nginx/yourdomain.log;! p$ v7 N# u9 y5 v
X2 }' `# l5 M% R, d" L+ v
6 o1 d, ], G: @- g" i& m* Q5 i location / {
$ f+ o+ D4 X- ]/ @- F- {4 V l0 |8 @) M' A
: s' T1 U, P8 e1 }- w1 S% c modsecurity on;
8 q6 R- t3 H9 f8 j modsecurity_rules_file modsec_includes.conf;2 G( j1 F; h, u1 E' [8 o5 v
root html;
! n. Y# R1 w3 b& p" s index index.html index.htm;4 B0 C: N" E V( Z
}
4 m' V3 G7 M3 ]% v" d( E, R; A}
% B2 ?, T! m* y验证Nginx配置文件
7 f, w6 b* ~! |. I/ ]7 n# f$ /usr/local/nginx/sbin/nginx -t. k! L- R0 b& ?: b. |9 W J
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok* C! k9 _/ V' c$ j2 B$ N) E
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful. Y2 v3 [# V/ M$ X; n
启动Nginx8 s( `0 j! ?: {% m/ ^$ j5 H* b
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
0 Q8 q+ G4 s1 m: {* w% t9 @5 M( X( E9 N; g5 y- X
测试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能够战胜更多复杂/未知/混淆的攻击模式。 ' g6 e# O$ y0 A1 B# c. l6 W
|