ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
& U* l+ i5 L1 D
' u5 Z0 d- O8 U( ?, I1 Z
. B& |6 p- c) D9 `在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( [! [, k( J6 i0 W; U& l* s1 a3 |+ g( M: |
3 m; S6 a) X% l! I3 p什么是ModSecurity. ^. j Z; i) R- C; y( t4 O
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 d% X. h7 b \& \0 _8 [% |! F! E+ {" _. v" `, a8 Q
0 c/ F0 [8 H2 M
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 Y& S" W0 I. _4 z
; l- f, i. q; ~9 l" O' {; P. z
* a% [3 m2 q# N {
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ N( k/ H! H" x. I; M( i: T' O: P6 t, ] j
$ r$ V! u0 | H7 v3 ]% Y官网: https://www.modsecurity.org/
) X. D2 F$ r: a' K5 Y8 W
2 A0 l6 G0 l1 S6 o Z" O0 p" u6 |1 k9 @4 X Q' d/ `
什么是OWASP CRS* X# Y+ I, K5 w |# x
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。) q; B5 Y$ N; r( b) [6 `
1 e7 K. f, ~/ C% d' P% i1 S8 Z$ n& Y& N! {) N. O& |
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
# y l" Z- s. f& ^& ~, Q# j. a( q& J$ F, O& [" U( @6 C# E, W: }
2 m! U; s2 m7 G8 D# c% ~. H) A0 w
HTTP Protection(HTTP防御) [* H; [) a& f8 R1 {2 L
HTTP协议和本地定义使用的detectsviolations策略。
2 s0 J+ W6 T. b! N) ~, g+ f. _. Z! {4 Z6 L. s: t, U
; B" v: A# V$ _
Real-time Blacklist Lookups(实时黑名单查询)
) D4 k: c2 c1 U0 y5 P4 q3 q利用第三方IP名单。
5 ] M) f* y" Y: L; y* E! A# O" c- n* g
# V( C2 A, }9 J. M7 a5 I$ y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
* x* h& u/ Q' q3 A# i2 c防御HTTP的洪水攻击和HTTP Dos攻击。
( W$ I$ }" c1 g- m0 T& b% s5 J: i$ e* ?4 @2 {
9 r7 F/ p. A, F) v- ^! xCommon Web Attacks Protection(常见的Web攻击防护)3 d6 d" c: X. c8 W
检测常见的Web应用程序的安全攻击。
. ]6 [9 a' c/ q) i% N6 j1 G$ _: I. r7 r0 O- ?5 k _
$ q# g$ t x4 v# ]2 TAutomation Detection(自动化检测)
" g1 F# d: P/ q' q( Y检测机器人,爬虫,扫描仪和其他表面恶意活动。: i1 [2 Z( h& _- p' U+ }
5 o1 h! E9 ?/ l' a; T
' D4 ?! J! K* |) U
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)1 S: ]2 K$ K0 I) V' ` K$ R$ F
检测通过Web应用程序上传的恶意文件。
1 s# i0 }$ \) q9 f2 x! f
/ v) h+ K; S# `6 q2 {" N! d
0 m7 y" V* d' m& \7 ^4 C- R! wTracking Sensitive Data(跟踪敏感数据)
) M2 x5 T0 V* R- U! A信用卡通道的使用,并阻止泄漏。
0 J' p; v$ p4 |2 I4 c3 e$ {) H' K: Y, [; w4 D% [
% N, M, q: P. x( V! q. ~0 ^( }$ a
Trojan Protection(木马防护)
3 ]6 Z/ K$ q6 n7 h: r8 t, L) I" n% h检测访问木马。
8 ?$ i8 Z8 V4 ]6 S+ w
+ R5 |' O# C% k- h4 S7 I9 d5 M# E& U$ C0 Z* R2 k9 S
Identification of Application Defects(应用程序缺陷的鉴定); X5 N6 n# x; h
检测应用程序的错误配置警报。4 y* v/ L+ B/ ?( g0 M M- d% @. z
# g+ n4 ]$ X& W& i
) Y" G! s9 X- o5 M$ G7 p/ g" B/ X/ QError Detection and Hiding(错误检测和隐藏)
# R, L! z- T+ M3 p检测伪装服务器发送错误消息。( O0 g8 z8 P! P# N- N. V+ J* |
! o! b$ E/ h* Q: a/ U* n& A% D; k6 }# H, ^& f( G/ Y4 } y G" J* _
安装ModSecurity
5 @" K3 s, {) T' C3 H) x- D软件基础环境准备! I4 F4 j! U/ B2 \- b
下载对应软件包
, c3 ~5 X, c. l2 [3 f' v6 p# N6 ?$ cd /root; u2 F+ j% P8 n0 ]2 h- w. j9 v: ?( i7 H
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 m' z/ _, d6 T% I3 w' h
$ 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 y' y+ @* I. ], S1 I安装Nginx和ModSecurity依赖包6 r X/ |2 C" g1 @! }5 X
Centos/RHEL4 Q( z5 j4 C! q3 e% Z9 V
) i7 o6 ]: u+ u6 l( R
% h' c# H0 H7 W, R8 H7 x! B4 I" ^; c
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 `6 U( C$ D t% w- @Ubuntu/Debian( j2 X6 G8 O8 [+ P" V& \
9 Z9 J; \6 a3 C6 p# M
1 c4 ]* Z& {8 b+ }, G$ 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 Q: v3 G( y. e1 N* T4 L
编译安装ModSecurity
3 _- g+ K9 A% T hNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
& @+ B" T2 M u _
" U2 R4 G" W6 h$ f% W- P7 R. w9 t
6 a2 `" Z8 U4 F+ v3 ]: B8 S方法一:编译为Nginx静态模块8 }8 L$ o- B2 _) A
. o: _' ]8 x+ U3 t$ |) m( V" F1 q) ~& P* ?1 X
编译为独立模块(modsecurity-2.9.1)
3 q/ H. t+ R1 _* N" u( F; j0 e$ tar xzvf modsecurity-2.9.1.tar.gz0 z; ~ S8 s& m8 y& q/ U8 k
$ cd modsecurity-2.9.1/
# Z: T. L ]2 v7 e- ]1 I$ ./autogen.sh% q1 [. N; n) E( Y
$ ./configure --enable-standalone-module --disable-mlogc( E$ |- Y$ m+ o
$ make& r0 B2 s9 P: S2 C+ I
编译安装Nginx并添加ModSecurity模块( Q6 _& D1 R# s% r! v) E9 D2 z/ l# G- {; m
$ tar xzvf nginx-1.9.2.tar.gz/ b$ N" K% p. H# V8 Y" R9 E6 F
$ cd nginx-1.9.2! y; ?, g4 D5 _! ~! {. O. s7 I
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* Y. j) m/ K2 p
$ make && make install
& k/ s8 Q: w! N, I6 P8 S方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
* I; G) }1 o$ q- C7 B' J4 j* p5 w5 G4 R' ]' `' E" x( w' Q) i3 i8 V) z
, M0 n" |& j$ T/ H编译LibModSecurity(modsecurity-3.0)
; J& _, p* ?: r1 l" I$ cd /root- s2 B: y! f- E. d7 u
$ git clone https://github.com/SpiderLabs/ModSecurity+ C% P; H5 t1 X+ U2 n J
$ cd ModSecurity3 x, I) r. v" d0 C# E
$ git checkout -b v3/master origin/v3/master9 h8 n* s6 v! E+ k5 m6 L, s2 [* r/ ~
$ sh build.sh, V1 J1 M1 l' e4 n q# B) F$ R8 l/ J
$ git submodule init, ^; ^7 ~# ]% ^$ c4 Z! B% T& O
$ git submodule update
9 m$ Z2 B8 q& b" H+ j, Y$ ./configure
6 Q, o9 {" b% Z: e$ make) W& M" f A6 m- O; H8 e# m
$ make install
$ n# a, @+ Q. v) kLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! W' q% _6 t) X$ @% d1 F' H4 X2 W5 R" @/ l( M \
, |& J7 Q0 {: I2 U7 M* e5 F" V$ m7 D$ ls /usr/local/modsecurity/lib
* T# D) r: e, i1 H: hlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
9 f6 L0 `( Q2 g' R1 x- S: i编译安装Nginx并添加ModSecurity-Nginx Connector模块& q, h9 c: _$ k3 B. u9 L' x
使用ModSecurity-Nginx模块来连接LibModSecurity, Y* P; ^8 V" b
* t. V9 m/ @( l3 F* Q8 D$ P4 M+ o, n: e1 S2 D+ T$ i& s V
$ cd /root
) t& `4 R* D) E; n# ~! T2 K0 H$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
1 Z7 t3 D; {. A! r$ y o F+ U$ tar xzvf nginx-1.9.2.tar.gz
& s2 w8 D9 N( y$ cd nginx-1.9.2
( w& g* }6 g# Q2 L/ o$ ./configure --add-module=/root/modsecurity-nginx
1 u# U4 A2 J; {- M8 v$ O$ make
6 L6 l* U7 u- y% H4 A2 H6 g# W3 i$ make && make install0 r/ ~/ C+ ~ f( x1 m) M' s
添加OWASP规则
% D% Q k: {9 r. M* o! UModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 b2 E- K4 }+ Q0 |6 j
7 P; Q" O) O9 D- X0 B( T+ \0 e* c$ y
6 ?# {/ n% L1 k+ j; m% B下载OWASP规则并生成配置文件
7 ^4 k$ ] R1 j$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: v& x1 ]6 w3 w+ L5 [( x$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' a4 s9 z# n2 \" X
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs& I u' ?, |; R# E4 S# i. j, X4 ~
$ cp crs-setup.conf.example crs-setup.conf
' Q, T# p5 b2 q6 P ~0 Z' b6 j配置OWASP规则3 m2 y, x0 c5 G
编辑crs-setup.conf文件
9 G, l- |% n- i; B3 Z/ e& ^2 _" {- E9 n& P5 K
$ @. h, _4 t8 x6 v# u$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! `7 D0 j# |1 B% D
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf3 l7 U$ G3 u7 ^1 a
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf8 ]4 u; M3 `8 g/ D4 L: G& U, j3 N
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf% z3 s+ T; Y! E7 f* x/ G( [/ l1 j
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" k# C {8 }0 ~ ?
4 t A @" x3 g- e1 J$ ?
' @; _2 A% c9 F q+ Y启用ModSecurity模块和CRS规则
/ `5 C- `2 G8 G( Q" K2 t9 U复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) S) m8 g0 {& r* ?7 y+ X, v) X
/ V5 G" L" j- f" q, r4 x+ Z
! u3 V, V3 ^" I% q4 `1 M- Vmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
+ O! H7 }2 W1 I+ |9 O/ `: }/ {
! F! g+ x* U* ^; }
4 G* q% G: Q" a) z/ z( _1 S$ cd /root/modsecurity-2.9.1/
, |( y6 ?$ [& x! B$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf / ~ ^% \" s' Z6 L6 `
$ cp unicode.mapping /usr/local/nginx/conf/
: j+ U! X" G6 g# d, n8 H8 k将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ S( k3 J" P) Z0 z$ z# [! x- \7 |- }) b; O3 |% N( x
2 ]& F! y& u7 G$ vim /usr/local/nginx/conf/modsecurity.conf. Y9 y* |3 n$ D$ O4 H& ^0 ^# A
SecRuleEngine On6 Y3 Z8 R# b4 z: b- y
ModSecurity中几个常用配置说明:1 }! }8 f- }9 p2 i
1 b- {3 l G+ b1 H- D" F: d0 A: z& k4 i0 t! W8 F' t9 x; C
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
6 x6 h! i1 i* V2 S) _& C
7 V- P) l" A* {# M1 X* y3 d# n5 [7 `$ t2 K* l! B& w) e, J5 F
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。% A. N: Q) a8 m. F
4 y1 a2 K0 J- U8 Y3 i
: s- A/ y* q& Q2 w* m1 \4 b, p
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
7 f% S$ h: [$ [( X! [1 {2 z: }) `9 `' x# A4 J+ N' J/ x& m/ Q
) l) [6 O1 L$ F- `3 M
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' O) J0 T5 U+ \% x
) T# O& q$ u4 H1 X! `7 k
6 U: I: D+ q* [( H4 C
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
o! W; j. ?: X* F& l$ Y1 y
/ H0 F* \ [8 p, ]% r+ o2 Y) h8 D# n" U6 _' }+ b. t
3.x版本CRS2 p* _5 L' k! h- {( w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs1 C2 h- t, R- Q2 \1 b( ]3 v; O
# 生成例外排除请求的配置文件! b5 e. u2 P, G/ W0 c
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf3 h0 l7 K$ e6 }8 ]3 K
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
2 c0 @5 w6 ]+ S" `8 c5 Z$ cp rules/*.data /usr/local/nginx/conf
4 H9 C4 L! q6 z: U6 e4 f! n为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。8 v6 | U& h2 ^. S" z9 M/ x% |
& S8 w8 d& X1 W0 f
- [) a+ o1 t7 N9 [9 Q( s6 n u
$ vim /usr/local/nginx/conf/modsec_includes.conf* d( m4 A3 G6 b/ m( X, n* r6 d' R
) B# S* m$ L3 z' M[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 5 w+ n0 @$ K: v8 u; `8 |9 g% x6 P- M
+ k% a M$ C" }. Q: D注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。8 A2 L3 O$ O8 C8 l$ c4 i4 A
Z, L# c$ C F. z
5 d$ j( _5 q& ^" |* j0 f: W1 t配置Nginx支持Modsecurity8 \% Y1 B. F2 h" W3 y
启用Modsecurity
" ^ r7 D2 c$ v- c使用静态模块加载的配置方法9 H! j: `/ Y- x* b4 z7 Z& J
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 }) H; a+ n) U4 T5 f8 D
9 r2 u; _6 ]; k" p! g x4 E' d
ModSecurityEnabled on;
! ?: ]* R3 k1 t$ ?) oModSecurityConfig modsec_includes.conf;
$ v. I! m/ w- y2 H修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ H8 v% W! h! q) h! x( Q6 V7 ^% h2 v$ _
* ?: H' I+ J$ W5 ?' s% H
$ vim /usr/local/nginx/conf/nginx.conf& H' a5 E5 y \* x4 n% C
% y9 g$ E& `5 a$ \# d* ~
+ ~( c/ O7 W3 u, U% W+ O# k. o9 zserver {1 j1 A" }1 v& M
listen 80;0 a1 W8 x& o" Q
server_name example.com;$ C% e8 a" k" }4 N
) c! ]' l+ v3 n+ `/ i" }% X% H
$ ^; ?/ g% Q! @; T7 _6 h. m8 L location / {: o# b2 x( P/ L' ?5 K) Q
ModSecurityEnabled on;
% `5 C, J3 l! f5 ~$ f8 W( x! l- Z ModSecurityConfig modsec_includes.conf;
' ?1 a# P$ |6 W- V w root html;
, m Q1 \. q; b6 y2 A3 c+ _5 Q index index.html index.htm;2 c5 d3 M9 [/ V3 f5 [
}
, ?, ], Z$ Z2 c}
" n2 x/ v% D6 K: P& D; ~& d: O使用动态模块加载的配置方法1 J3 O/ ?( t) T# R- d; l; K! e0 u
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: w% Y6 z6 Y [; o8 E+ y9 _4 T: M+ d. w6 t+ R
, t1 b! O1 a( h _# @3 omodsecurity on;
% f; V% X8 J- x' m o; s. Hmodsecurity_rules_file modsec_includes.conf;
% k- }+ L" T1 \8 |. x7 X4 v修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% N9 g+ d/ N/ R8 m7 s7 b' r1 X t% `/ I9 Q% c
6 i5 T& a8 {: n7 @1 \$ vim /usr/local/nginx/conf/nginx.conf$ F' T# ~1 a1 ~3 R) f% V1 @) Z
- k6 N2 P4 y1 m, x8 Q# K4 R6 O4 D9 J& o6 O3 g5 G, l n# Q3 G
server {7 l: W: u6 K1 Q! t. l: ^* k
listen 80;
6 `9 a& h' B( b9 w: D server_name localhost mike.hi-linux.com;$ P8 U. G# U: {" d1 A6 S
access_log /var/log/nginx/yourdomain.log;
$ c8 Z9 S. l/ k$ B6 C ?3 t0 D$ c* s! q I$ c1 V
) X7 s# ^4 a8 N( a1 E6 J' Q
location / {$ L/ c% |) j, C0 c1 A/ }9 \# L/ I
; \" W( H0 ~2 s5 _& I
5 o6 } Y* v" m% U/ J
modsecurity on;
$ D% A- K Z2 ]) t/ U0 v( f0 D modsecurity_rules_file modsec_includes.conf;9 l6 q' F" {3 B3 S5 L
root html;7 F+ j- M2 e4 h# B6 D8 Q) x: D
index index.html index.htm;
# I: E/ F7 i, l& `/ |}
) \* D3 i7 J* Y+ n# R}9 A- a+ \1 @' M' z$ O& e, e
验证Nginx配置文件- L8 s& _9 e+ t4 B- g& }# D
$ /usr/local/nginx/sbin/nginx -t
( n2 I$ t" h& x" `7 tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& f8 R# x7 `" H( _; {nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ j8 c& g8 }9 h W8 @; R8 f
启动Nginx4 M# j m. \. C6 C8 b* V# f
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
; Q% G& S' ?4 N0 C% _: H v
$ Z4 r2 z' C j4 @测试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能够战胜更多复杂/未知/混淆的攻击模式。 7 _ S6 w& c* h( `
|