ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ A( ^2 [# K$ A. ?4 l/ h( X4 F& N+ w! V% a r" e% Z
& r8 ~" q% J" }
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
5 T) H% p: j; \5 g& w+ a p
4 g6 U! ]! T1 Z# ]4 c }. B _& q l
什么是ModSecurity
( O6 N' }5 @8 c* n- l0 t6 IModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
! v3 @+ o8 [' E" ?" Q, ?; r7 X) ]7 t5 X# J0 A& l7 C
/ m5 p' ] c# J) }) }; E9 @
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
2 m8 A' l) \! h: l0 C6 B; z) W2 G; |& L3 x" ~$ L% A2 x
7 z0 L1 B7 @ b
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。) H& }& F$ T1 k, }# w4 R
: c& k2 Z$ i# I, X8 p3 C0 T1 W5 I$ J$ D1 t, f0 L4 J+ ~
官网: https://www.modsecurity.org/* v. _+ k3 n/ ?' E A3 n) [. {
( b) m" ^( c; h0 G. t; u' b5 j
* s8 U$ M Y- f9 q* y1 x t什么是OWASP CRS9 M* L( U5 t' D
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。" m+ F. p0 y6 W. c
; h5 Y1 t9 v0 m$ P
M5 t1 `, o- F$ Y$ V, X
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 ?/ e$ }% z8 K/ J
1 K5 D! \$ K7 K( J5 K
5 ?0 q$ g" t4 L" G8 m, `) tHTTP Protection(HTTP防御)
5 A: r, P; G' g. L. t: GHTTP协议和本地定义使用的detectsviolations策略。) z# n3 U$ w# f4 x$ A r
$ o# ?0 b7 V' T5 P' [( V4 R) R
# S; T N" o" W) r2 ~& D" C8 }, z( PReal-time Blacklist Lookups(实时黑名单查询). [: r; d1 n% F2 M9 e& o! ~; O
利用第三方IP名单。" _: J2 d7 |! \% L
' Q, [4 P- z& l
" {4 N% V* C* T" CHTTP Denial of Service Protections(HTTP的拒绝服务保护)% _! O6 _) V/ S# H2 H
防御HTTP的洪水攻击和HTTP Dos攻击。+ ~" o( l0 C/ h
0 ]; O6 y( p' t0 r$ \8 z8 {
; O. P( R( r% g# zCommon Web Attacks Protection(常见的Web攻击防护)
, j8 Q2 o) x3 R5 l: V" b4 N4 y检测常见的Web应用程序的安全攻击。$ Y. \7 E1 S4 z- E
4 {) J x! g7 Q; H- X$ v. ]7 Z9 _) I% n; ^, h$ k) X8 \
Automation Detection(自动化检测)
9 c* N5 Q" b; o检测机器人,爬虫,扫描仪和其他表面恶意活动。
7 s3 p2 E& g- O1 N, e/ g) g1 I1 `1 M+ [0 R* }% |! ]
/ S4 e/ e# E# D. dIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 x5 g+ _, g5 b9 k4 x8 Y1 ]+ k检测通过Web应用程序上传的恶意文件。4 l: b/ G0 X( s
. o* R0 F, f9 h- K; F. K1 W
. f3 N( n$ H Q
Tracking Sensitive Data(跟踪敏感数据)
# k4 k* ]) x2 U2 X7 d2 R信用卡通道的使用,并阻止泄漏。
9 [/ _( F7 i+ W, \! m6 v
/ D/ z! h& ]0 S% B6 q5 x& y4 l9 K3 l! y. s+ u( L0 p8 M
Trojan Protection(木马防护) A3 R1 }6 ]( r/ F( s2 w% d
检测访问木马。, ]7 E- {1 ]5 t6 E8 d8 ~
/ z! L' }$ q' j) o1 w! D% v* }9 C6 Z% x' L- m
Identification of Application Defects(应用程序缺陷的鉴定)
/ i6 R/ u- F1 `6 x9 X0 F/ f检测应用程序的错误配置警报。0 P7 w8 s5 Y. x# B, Z, b
1 X) H. E1 d5 ^! l
" c" @& R. a5 L5 L# U$ o( R9 Y
Error Detection and Hiding(错误检测和隐藏)2 I; I4 I8 s$ T
检测伪装服务器发送错误消息。; _) O+ I Q! U+ |, `
5 c% X& {" z) f+ j4 t) Q! [8 Z B( X+ [/ O
安装ModSecurity5 U& Q( A# ? g& C. z7 \
软件基础环境准备' c0 M/ q# p! U, N+ C8 x
下载对应软件包 q# g7 P8 A7 [3 X% Z1 I
$ cd /root3 S% f+ E" d+ o, v8 r7 [) O+ C5 L
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
5 ?, ]& m# n8 m( U, J+ S" j2 Z) W$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 G0 T& j3 ?8 Z2 s6 C+ t! g$ g
安装Nginx和ModSecurity依赖包
2 Q: ?+ M9 E3 D1 k! ZCentos/RHEL
l( x, G8 ^+ I2 ?; d% ^; t
3 O+ U) i( Y: t# ] V4 P# o0 a3 W! m( {
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
; m4 q, H( M/ X8 e( c4 ]Ubuntu/Debian" Q& @8 F4 i6 `0 |$ Y
& `) @ t" R+ R* n% y" ^$ O9 _/ {* H C" C7 u& A# \) b1 x
$ 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++-dev4 f- ?1 U' ?: ?) @9 ?" W, F. Z
编译安装ModSecurity! t. s$ t% E! @3 N: u9 X; j
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
2 U! o, ~; x" U/ Y
: i, D" w, D& Q4 @7 X" g
8 S. }% P! E# T( W1 z0 t方法一:编译为Nginx静态模块0 L2 G4 S' v% c; i3 Z
/ ]" d) h; Q8 H ~( }" j% p8 D
6 I) k$ L, n) W' H- G1 Z编译为独立模块(modsecurity-2.9.1)
) {# F( H+ i$ }( }+ o! \ x/ q$ tar xzvf modsecurity-2.9.1.tar.gz
' x# O3 \' t) q8 o5 v# i+ D- N$ cd modsecurity-2.9.1/
3 j! A+ l( d- p: |: C; r$ ./autogen.sh# C! y$ c" ^6 u& P8 u
$ ./configure --enable-standalone-module --disable-mlogc
$ k) ]8 [ g0 z# @) @! M# p9 E$ make) E1 x E$ D4 j+ O; d5 {
编译安装Nginx并添加ModSecurity模块+ N; X% x0 ?( n6 E& }# x# z
$ tar xzvf nginx-1.9.2.tar.gz
+ ^5 Q" @( c" I/ K/ c+ o$ cd nginx-1.9.2
5 x0 E. O8 F% b0 B- L% c2 M$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
, Z" x' I6 M* v$ make && make install
7 v1 W3 }! g6 g- D$ f. _% {* Z) f方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 X$ i) c- ^+ k
- I1 V6 @* @1 O$ {+ t1 `' H
8 ^5 W$ J/ v7 ]' _5 R编译LibModSecurity(modsecurity-3.0)
7 [+ H+ Q9 U3 N$ A( S$ cd /root
j& Q% ]" k# @ |$ git clone https://github.com/SpiderLabs/ModSecurity: Z* Z/ k! v5 E3 y9 y: O4 X2 s
$ cd ModSecurity
) }. i; p6 b) @) L+ X$ git checkout -b v3/master origin/v3/master
2 h o- H t" ^) F$ P$ sh build.sh
" r6 k, e! L7 I& @ G! k& ~$ git submodule init
) Q" O4 M. A6 W m7 K$ git submodule update
; g ^/ [) q! e8 T- x$ ./configure
4 Q8 I# {& P/ F* J4 _$ make# B& f! O+ @3 A& C
$ make install5 Z3 h2 a3 X: a' [1 E
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
# `# i, X+ f( A" x8 S" _( G! h: Q& h* `! N0 L' C
8 ]4 I7 w/ ]' S( C6 k" q
$ ls /usr/local/modsecurity/lib
( y5 ]4 S6 d3 B* }6 a' }libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0( M/ v. g7 t. T4 p/ @' j. V
编译安装Nginx并添加ModSecurity-Nginx Connector模块+ Z" v1 R" w T1 a
使用ModSecurity-Nginx模块来连接LibModSecurity
7 [/ E& r( S* Y. J
* Y5 D4 W) @7 e; D: a/ H, n2 I' U9 U" @: J
$ cd /root5 c3 Z2 a6 }! M
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& c1 [( z3 o+ h8 S+ ~/ Z% Q
$ tar xzvf nginx-1.9.2.tar.gz {9 n, z" B/ v2 Q2 Q t" E0 C* y! @
$ cd nginx-1.9.2, ?: [/ E r& Y) r, ^6 ]
$ ./configure --add-module=/root/modsecurity-nginx: `/ ^$ z2 m3 I, ]3 j/ s
$ make
i" X% Z, w( M8 @; E7 f/ C6 U$ make && make install
; o7 {1 H- }1 M% U I添加OWASP规则
! e' ~. j @+ E5 |' ]1 f6 `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。; L. }3 V9 `7 z m. Q1 z8 ~: I8 z
7 n% Z4 ~8 ~6 u3 ]/ D! X9 u. ~0 z J1 u% p. I" F3 K
下载OWASP规则并生成配置文件$ m9 @2 X$ J- v) c4 A) i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git4 S2 ?% Z, O: {; K
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
# m- d2 H* o) U1 s' F. p+ O$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" J7 ?7 S+ R5 z& s& }8 u" p$ cp crs-setup.conf.example crs-setup.conf
' I* ]7 \; A5 n" `4 Z4 } l7 m配置OWASP规则
! c3 x1 T# n4 A1 X编辑crs-setup.conf文件
, f: z, R4 {3 p- D- S" D
% D5 J: R c, A& Y! o6 V* y0 h! V1 l" Z
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
& X4 b0 Q: J& \/ i0 m: n, ~$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf8 t! H1 ]* x0 h
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
! ]+ I4 V. Z. {1 {& h# Z; l0 q* e$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 E8 B1 o) q+ P# l% i默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- K, T& t1 o8 z# Z: N4 S4 k
8 h. b H# ]5 y$ L/ D7 f4 \
0 @: M0 n# V0 R% M4 p+ @启用ModSecurity模块和CRS规则
# B% w9 m" _/ E2 h6 U( [! `复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* r3 w( h: g& m7 Q) @7 J2 m: p, J% L
2 K, [% X4 X( b Z8 c
; a7 n" i3 S% \! t+ l) P$ W
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。, g/ @. u$ j+ H1 a, r1 Z
! t; c; n; H# @9 x/ j& l, ^) W9 E
- Y/ K1 L& O9 c& I- `( \# V; m$ cd /root/modsecurity-2.9.1/2 P' V' r% T8 ~& G0 g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
! z8 y* v0 O& x0 ?/ t% u$ cp unicode.mapping /usr/local/nginx/conf/) u m7 s3 \0 o5 G
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。, ^" B% ?3 a: C' Z C! r
2 `0 i% v: r) n; `2 ?5 w7 A r, W- G u0 e+ O: T
$ vim /usr/local/nginx/conf/modsecurity.conf3 F! I; b" z6 Z; _* y7 N
SecRuleEngine On
/ r* I9 _: e3 D4 z; z# j; BModSecurity中几个常用配置说明:# C1 t/ m2 [5 U5 E% {! x# f
# P! S- W% \6 m# p% S% b
9 p4 }0 W5 Y) J% O3 R' Z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, g: d6 P9 \4 a+ N' {. ~& P. ~/ R. O. J% q" ~- p1 n; |
/ M0 I9 z5 J& q) H& N/ R' Q2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。& q/ S' ]6 {9 U0 f
Y3 q: T9 S: z5 k+ S4 E
5 }+ K- C* G9 g, o/ [* `' @( M& N$ _- V3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! X% d8 k* a$ ]) ^& r" H( n/ C( q8 e+ O7 R' Z8 v% k1 m( ^
! |$ ?, e; D2 b6 c4 {- ?% Q D4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
$ s% e/ {* o( c3 w# r
# B$ y0 `' U$ k1 j) k; y2 i; B3 c3 G4 X
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
% ]1 `' I2 g) w0 C0 I* t# Y1 H
x5 O4 t' ^; h$ x& ?$ _7 Z5 ~. m
* T( { R! ^( R/ T7 p3.x版本CRS
* ~$ d" q4 S7 R$ {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 ~) t6 F. [- @) o, K; ]& j6 z# 生成例外排除请求的配置文件- Z/ F( T5 y- t" {1 Q
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
7 d/ X4 c E( l1 S0 m/ a& _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf" _. j4 X, L3 G; e" Z4 k
$ cp rules/*.data /usr/local/nginx/conf2 _$ E9 J i O# \ H% t7 d* B
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* m4 S' ]' q9 r6 ^4 r& u
m: `, z, w+ H* n" o; z7 v4 D- M* U( U0 L/ b5 c, j2 }
$ vim /usr/local/nginx/conf/modsec_includes.conf
& Z* I* w9 v% j+ V7 `9 |6 x" s. z: I- s4 h" u" `$ G$ o% k
[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
" R. j8 }% \1 y; v+ C8 |. L h$ x/ J* U5 H
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
d% b1 Z p5 C
) T p( l0 O, r; T- k2 k; e; \: i5 r. O' H' k- w& m
配置Nginx支持Modsecurity7 D, y5 E4 v ~6 G: D2 E* }, q
启用Modsecurity
$ x5 Q- T% A+ L K7 y使用静态模块加载的配置方法4 c( R- [; U8 D3 v$ \2 V: F; I8 R
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 w4 X2 H, D5 I" A3 F
1 M. ]9 @& M( B7 I% X1 r
1 b% W( A8 s6 Z+ IModSecurityEnabled on;' b2 M- L: l" G1 @! }) ^
ModSecurityConfig modsec_includes.conf;
1 |/ }% O7 u& M+ @3 X, V8 c& A修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! M3 j* [ P* X3 p' T% L* f$ }& T$ o! u9 ]& m r w5 Q
: z7 u7 u8 b+ P3 o$ vim /usr/local/nginx/conf/nginx.conf) d$ q) R9 C5 K- \" [0 l
( e. x4 G$ s' {* [/ j, i0 h" J) N3 A
server {
" r0 g7 ^1 K* F, U& o/ W1 I& B listen 80;0 v1 f8 ~2 P) P& j/ x0 F# \* ^
server_name example.com;6 D5 b( }- m7 @
5 K2 _ T$ `# m) L, Z5 P
) |1 D. W4 d5 y7 A, @2 d$ y: Q1 X location / {
3 m2 e/ u1 ?, G6 o4 j8 \ ? ModSecurityEnabled on;
9 {: t' P8 R7 t8 c ModSecurityConfig modsec_includes.conf;* l: Z& o- a: ]" p# }7 Z* v
root html;
0 j! j! v: e5 }* |# M) v" \3 x index index.html index.htm;
9 C- w- _2 w* L6 j4 p }
- K2 Y0 ]7 D3 E4 h! o* u: y}
0 I& E' C+ G' n- l使用动态模块加载的配置方法
0 q0 Z) q0 O) M, q: k& u" l在需要启用Modsecurity的主机的location下面加入下面两行即可:2 e' u( c. Y$ ?. {4 ]
: v7 L; r5 l6 e; V2 L% a2 b% e, E
, H0 U6 P; Z) x
modsecurity on;
" Y8 Z" b0 P+ s: k1 pmodsecurity_rules_file modsec_includes.conf;
/ Q, D& x' y4 o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. H# Y# e* L" o4 `
. d4 E1 \* F( N; V/ w5 c9 _# K W4 J
$ vim /usr/local/nginx/conf/nginx.conf
' |! B' u4 @" s8 j/ ^! {' L2 h( v2 ?
- Q2 T/ f# h; a3 ?# M1 j3 D
' x# h- ]7 A4 s/ X2 e d/ ^: e( `" userver {9 I/ n# Z0 }0 r3 C- F# d$ c
listen 80;% k1 Z, l$ I1 E% r
server_name localhost mike.hi-linux.com;( ^5 A+ W* v6 ]& i; H; a" r6 O) [
access_log /var/log/nginx/yourdomain.log;
# S$ g) r+ W" Y& m; ? T. N6 H- K% G! o P4 E8 [+ k
, ~- x" ^& w9 V5 D! S3 H4 Z5 [ location / {
& w! H$ ]' a. i0 q
9 Z- q% w! R6 z# t: C( d
& E( U* X( m1 h/ R! k% Y; A modsecurity on;7 r9 l4 f4 t. W$ N, a5 b! V
modsecurity_rules_file modsec_includes.conf;
3 C( `. [; z" H* ]$ T3 k root html;
2 }1 V1 Y8 x3 p, W index index.html index.htm;
. s- f5 h$ S* b* D( i}
; c$ j; A" c8 ^8 P}+ w/ ^" Z7 h& x' \
验证Nginx配置文件- ~7 a3 a, P. S% C; {
$ /usr/local/nginx/sbin/nginx -t2 a( w, n! R, k
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
; P! z& m5 N anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# b- R, e( T9 |6 j6 q% U
启动Nginx
7 g2 w ~% r) A1 o3 W- L, v% d$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
) p. S. u. \3 P; f8 p1 X: V N% O$ i) d; C9 w* W$ S8 ]
测试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能够战胜更多复杂/未知/混淆的攻击模式。
5 q# p/ e7 s8 |! ?: } |