ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
- K$ l& H Z% G# G1 V
% Z: z6 n& K4 H9 l8 n
" y2 y8 i) q- x7 }在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) z: a+ u" D2 s" h
) f9 ]) t. c' p$ E3 D% ~% l
& w3 o& j0 ~ y# n
什么是ModSecurity8 P$ X5 e1 E# w$ D# G% L7 }& Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
* M E |: l; A
4 i! v# X5 n7 e* z$ H, O$ K
# `, q, J; G# hModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
! x5 y9 Q0 Z2 i/ n% {+ D
* p7 [; ^- Z! V3 O% a' B& ~' P5 c6 o8 d8 \- T6 s" x
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
' F+ g3 ~- |6 p$ c! w+ Q/ O
& d# c2 s2 }, G" p7 |9 Y9 r$ M; t9 R
) u' J9 ]* U" H1 E4 t+ h官网: https://www.modsecurity.org/5 p/ J" ]0 g h5 d2 k
. D% h2 u$ C# N, @2 X) {2 m
, Q& v% Q$ s) q) e6 J8 y. S什么是OWASP CRS
0 l) B: i( w( E AOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
7 I/ t4 |4 W) e/ R s3 w6 M& z! f4 H$ o. B& P
3 X# a U: R4 g& Q ^ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 v. ^" x0 i9 O8 h/ D5 X" Z1 s8 g. n+ o, p" q
, x; P& f- f. Q6 u, m" ~
HTTP Protection(HTTP防御)6 s$ x+ T, W7 Z/ B3 P
HTTP协议和本地定义使用的detectsviolations策略。! q# l( j) N2 C; t6 _8 B
`. o* F# ^' M; |7 U- V3 N7 V; S4 `+ O! | y
Real-time Blacklist Lookups(实时黑名单查询). u! A, n# S0 L* I) `
利用第三方IP名单。6 Y% V3 j- _* }8 G
6 s+ A3 K/ y3 o, C# ?, n- y; i% U; X3 T5 ?$ r1 E; }
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
% P8 F! b/ v2 S9 E防御HTTP的洪水攻击和HTTP Dos攻击。' m6 q- Z- U+ G. G- ?
" a" ] f3 h% B- T0 ^$ O4 v2 d
6 I }6 d! R0 p: c# m$ D, u+ V
Common Web Attacks Protection(常见的Web攻击防护)# [1 Q+ f% b" a! q: M0 a
检测常见的Web应用程序的安全攻击。" i, ]% h1 b. ]
, H: |- Z# r, R- e% m! {5 }7 ?, L- X9 M: [) o
Automation Detection(自动化检测). W8 A) F# y+ k0 ^
检测机器人,爬虫,扫描仪和其他表面恶意活动。6 q0 b3 h9 [% F* |6 ]) g6 x
) M2 a3 M/ O$ x6 K4 S6 u
2 a* T! b. i, G: n! q" zIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 s0 d$ m, n. m3 s/ J9 m9 O检测通过Web应用程序上传的恶意文件。
# L7 V" E9 _* R9 y* h8 S" I
7 j; x$ B- C8 h! K( _7 A7 U8 V. G- H
2 p! o, U$ D* p, tTracking Sensitive Data(跟踪敏感数据)
9 N2 \, n+ L( J2 P! H# y; ]8 M信用卡通道的使用,并阻止泄漏。' ]7 I$ E' T* D" T- b
h% N, V. |: U
7 M" k- T ]. gTrojan Protection(木马防护)
: }" B, B: H- d/ Z3 K检测访问木马。5 B- y: c) [% r
. U/ x h5 H/ _7 a% i
) [0 C9 q/ s5 W
Identification of Application Defects(应用程序缺陷的鉴定)2 Z4 m* k$ `2 y6 Q, @. @* {) q6 f9 d
检测应用程序的错误配置警报。0 x: u! p3 b8 I
4 z, |: s! Z8 U! q
5 M$ h1 z. E( x; z6 y# NError Detection and Hiding(错误检测和隐藏) B% s# B$ a2 |6 O* e
检测伪装服务器发送错误消息。
8 Y( E# h# k+ a6 V( W. A5 z6 T6 z
& Q {0 o; f# N, L8 N( P
' T1 W5 f' ?1 L$ F g, Q安装ModSecurity
8 U* _# ^# U r+ ?软件基础环境准备
! K* Q( x# s6 {: H- A: M下载对应软件包
% G% y) l1 H. k0 t& {$ cd /root4 d1 o8 b1 V s& n8 p9 D0 U
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
' W, O% E/ x- Z% X% ^) o$ P( T$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
( F8 C4 A- Y- H2 F安装Nginx和ModSecurity依赖包
0 ~3 ]6 a0 F6 j" JCentos/RHEL" F- K) t5 G+ m1 c7 P1 L. B; m
* p( ]$ k5 A8 w
( q" S! J* |7 B% }' ~" y4 h$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 I& N+ F- p! h+ l2 F: H8 n( N
Ubuntu/Debian
/ M6 a( b) Z. [- ?. g) A9 v! k$ Q+ t; `9 @2 Z
0 I S7 M0 v4 J# `" J, B) F
$ 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
" W {% Z, O( B编译安装ModSecurity4 G3 i) {" ?( V0 Z* N2 l! v+ A" l1 d
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
4 G' s, L: _( @6 w8 T% n' p$ o7 D: K" g& @1 x8 Q- Q- P
* r' _) l0 d$ D8 s G方法一:编译为Nginx静态模块
7 n0 h: u' W( i
7 V& A1 X. g, M6 p% L$ u7 i' o- g( x
1 h0 b5 B2 V$ E0 Y- T) V* o编译为独立模块(modsecurity-2.9.1). F, p- C4 {9 |! m6 t) H) l
$ tar xzvf modsecurity-2.9.1.tar.gz
J4 Q& W- }2 ]1 H1 e$ cd modsecurity-2.9.1/
1 O/ W2 H- c; m4 \. ^$ U$ @$ ./autogen.sh' z4 J; r0 s" H+ ]
$ ./configure --enable-standalone-module --disable-mlogc
0 H7 m. a5 L2 P, p F0 M. i& I$ make) ]( O3 |3 s( D. y& d9 s. S
编译安装Nginx并添加ModSecurity模块
4 v. \3 V' [! u7 T) V9 V$ tar xzvf nginx-1.9.2.tar.gz
8 l* q2 i3 @: Y7 ?* X$ cd nginx-1.9.2
8 s5 T3 i! {3 g/ D5 n& K* }8 U% }$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ X1 W4 p1 \+ i4 B/ j, L7 f$ make && make install3 I2 H: F2 R# q& M0 v
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; x6 @* O; I- d7 d# n; `, C( z- p4 X4 K& e0 I0 J
4 Y- K0 i; D8 q |5 G编译LibModSecurity(modsecurity-3.0)
1 K" a+ E* i6 n, a6 v5 I$ cd /root
1 b: t! g. D0 ?1 H: n$ git clone https://github.com/SpiderLabs/ModSecurity
+ M2 I5 o& V+ {* |+ m& q. A0 ^$ cd ModSecurity( w S4 v4 \+ _2 b7 G) ~- l2 U
$ git checkout -b v3/master origin/v3/master! W# F' J3 T1 z, B$ j
$ sh build.sh2 |- {9 A# a. ?9 f
$ git submodule init
/ D- G( r: [. A, B- _# Q$ git submodule update
9 E3 E) a9 y6 u' v1 T7 q6 k+ j5 \8 t$ ./configure
9 d2 p; c/ n* _, p o$ make. a; f( v' M* j
$ make install- E# T* e$ R) F$ j7 @ K* B9 |
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
$ c; w" j: p4 [% {- h$ e( S' O. {: ]' N% c. ]) g1 O) ]
' a1 [4 g0 a; A) E
$ ls /usr/local/modsecurity/lib) n$ P. ^9 [2 x% G( _1 O
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
2 U6 O/ ^8 U7 K3 L J编译安装Nginx并添加ModSecurity-Nginx Connector模块: ~2 w: M5 y) }% G
使用ModSecurity-Nginx模块来连接LibModSecurity, u m: c/ r2 t8 i0 _/ {3 D" C$ W
0 ]0 X; n/ A; A5 |
* c8 s0 y. Y$ r& O$ B
$ cd /root3 }3 X2 w# D4 ~' l
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, U/ h0 f I# x9 a7 z4 t F$ tar xzvf nginx-1.9.2.tar.gz
1 U ?: g; L" _4 f7 m" h' X$ cd nginx-1.9.2
! y8 t4 q1 y$ T$ ./configure --add-module=/root/modsecurity-nginx6 A( z! R0 m1 B" e/ g" A
$ make! M! V8 R5 F( u- x# G) I( |5 d
$ make && make install1 n3 l& e0 T9 p# N1 G$ X
添加OWASP规则
; h) L9 l- f9 C0 i: ?' { aModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。! Y% r. x' a' V( K; a$ q7 |
/ h7 w% ]: q6 k
2 D4 b0 j7 o9 W* c, F+ h下载OWASP规则并生成配置文件
! M# c7 I& }* g h" t B2 \$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ d" r) l' D/ G7 Z7 u. i" G" e$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/" f7 d/ Y8 b* E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 P7 M2 @7 x6 L4 `& Z4 g) D
$ cp crs-setup.conf.example crs-setup.conf ~: G0 b. X/ ?6 I3 A# s
配置OWASP规则
; J0 S/ u7 F2 \编辑crs-setup.conf文件. Y) `$ k: l, |4 I! R' n
: F" ~8 E' M. w- X0 i6 B9 @1 V
# F: \- _0 n4 {+ l$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
, y( J% {: s0 L% {3 g$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
) o$ j4 G: c: O' R& y% t$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 P4 A, b) |0 p2 P$ T% X& ]1 O$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
' n2 G' F2 G9 u' ~$ g7 i, _默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" `0 s) i5 b+ l8 Q% H( m1 {4 Z* g2 H* h2 x B. v+ o r
( P5 P: l" i/ z7 H
启用ModSecurity模块和CRS规则. p6 G" U2 F0 N% L6 \) a/ _
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* `' ~ p: K; \8 x
( W/ z' Z* y5 J4 R3 S" x, K5 ]" K$ L+ a0 L5 ^9 P
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' T0 O& J/ J5 q" D; F
" d: `. x7 o* H1 M/ u, q2 L( A/ y8 Q
# Y3 P) S4 k9 R5 ?( Q6 C$ cd /root/modsecurity-2.9.1/
% \( u& j; S8 E7 E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
/ [3 N1 ]$ G1 \( t" Z" G0 K$ cp unicode.mapping /usr/local/nginx/conf/
! e+ Z) @5 |( v$ }0 H$ c( T" `' W: v将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 A* x; B7 O# M& I3 V f
0 K; y" }+ ^: S) k* M) z) L& v; Q- H
# h9 X! @4 v8 ~& v) G4 e. N7 e- e, f! p$ vim /usr/local/nginx/conf/modsecurity.conf; O6 J+ a$ A& ]3 A/ [- L
SecRuleEngine On/ b- \ Y5 G& e" Q+ y/ Q
ModSecurity中几个常用配置说明:+ D$ S% m0 n" d! F2 d
a' z2 ^* B3 T
$ i7 U7 Q3 v$ \* P6 [! O
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。+ ^* [- s# K" p U
: ~1 S6 s4 _3 q6 P, ?9 L: T( c# c* c- @# A( r( V
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。; P; S# `! J$ [
; @1 O% ~$ P/ p4 U9 I9 l B1 H& K
8 i6 u- @% S2 K5 o3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
, o9 u0 r% o1 X3 A" A& S
7 w! H# |8 A4 m3 r3 p: a
! o2 H/ f' T& Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
. V) X; f/ f& z9 H% f u& z- |, X& [4 p* `0 D2 @
5 Y J; x+ y- z: B! X- C在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
! G' ^3 V# ^) e$ a" X
. I+ B& G* Y( A3 v6 V4 m* N) `) F$ }3 e2 B0 O& ]
3.x版本CRS# w6 j5 D+ ?/ W+ ?, l
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: e4 x1 L! a4 D7 ^; G# d9 U# 生成例外排除请求的配置文件
' h, s6 t; i0 C% `. g$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf4 X8 t9 m- T% Y+ ~
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# I: X/ t O2 { } w( }% Z' q$ cp rules/*.data /usr/local/nginx/conf* ~3 C2 E& x% M, S4 }8 j& [8 e
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
7 C& v5 c/ y+ C; ~2 M Z: k9 F3 z S$ G
' P' M8 `: N0 y. G+ s9 w! m$ vim /usr/local/nginx/conf/modsec_includes.conf2 e0 e) v. u# f+ B, R
9 P9 a8 @2 y3 j5 I, d% \- y
[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
) f+ ]% C( w4 y2 V5 O* u: }5 ^% E8 k( J" o' w
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
! v6 _0 f% e* u! T" l
, ]) V2 n- p( l" q2 t+ ~6 G$ R, G( P' i! e+ g
配置Nginx支持Modsecurity
! L: X" l6 ]7 `( C& F启用Modsecurity% n8 g, Y1 C& r* z
使用静态模块加载的配置方法
- J% S5 q+ K5 r9 q) w( H' Y- s" U在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 N6 z c/ U: U8 \, `1 A% ^' J9 \. }) e. R, r
8 ?9 u- l) j0 H% W& Y m
ModSecurityEnabled on;
& W0 D( Q6 T1 Z, \ModSecurityConfig modsec_includes.conf;
5 J* C) J/ F" ^( p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ E' E5 ]8 c+ |# Y
* A/ b4 m$ B/ S8 H, }# C. D6 a* u1 o% l* x
$ vim /usr/local/nginx/conf/nginx.conf
+ x; k7 _7 e7 b. e/ N
& ~3 O0 H9 Z( `
2 S4 h9 ^& h3 ^0 @server {
+ R; s8 K: P; |6 i# ?5 u listen 80;5 _' f( V8 L' ~4 v( d) h
server_name example.com;2 o5 W* M0 o; d0 ?$ w+ U
" j% L8 N; J7 M" M6 G' a! @
6 S4 \# S1 N8 d' ~! N8 U
location / {2 M) ~+ c3 {0 u& c. Z1 T3 _
ModSecurityEnabled on;
1 q) t( f# O: }' s ModSecurityConfig modsec_includes.conf;
# D/ ^6 k3 [- a1 T3 r( x2 b; w% j" W root html;
1 A) z* p4 A4 g' ^$ G: C* c% R: w1 H index index.html index.htm;, @% U+ ]$ h. Q$ q: r7 N
}
9 _. g O8 ]! k, @& D3 J! B}; y% a; Y/ h' v: \! Y
使用动态模块加载的配置方法; ~* @/ J* t% H, c0 ?, H4 }) d
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 {" F; c8 _6 y; U! W$ h4 g% O4 w7 L
3 C. M) _/ E0 y; h4 ?1 |5 ~
6 B5 D& d2 a0 X! {; Nmodsecurity on;
' I& G+ j: b5 T+ ymodsecurity_rules_file modsec_includes.conf;7 f$ a* |9 q4 z0 U: N
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( h) q) o" r9 I5 L( G$ ]7 S/ n9 C& f3 o: @; N7 H
; D& P0 t5 ~/ Q7 g' y7 s4 h5 E$ vim /usr/local/nginx/conf/nginx.conf
+ I. @! A7 y0 b$ z# ^4 ]
$ H4 g4 J y8 F7 q9 O3 m
% V$ X) X* X0 a2 i( Userver {9 r. C5 e; d7 o3 C: {. U
listen 80;$ k1 z! D4 Z% q* _, x5 D
server_name localhost mike.hi-linux.com;
( ]; l/ l8 n- I$ x access_log /var/log/nginx/yourdomain.log;
- b2 P( d* r, ?8 ~' Y
; F L" X; Y. E# L
! {3 \! B5 u* T9 f |# @. S; j location / {
1 S: g# ^ W! c, h
6 N& `0 n- |4 h! K6 l) o
! Q- D g& g* O6 j0 N9 f* w. { modsecurity on;) w, f3 f. h; t. D
modsecurity_rules_file modsec_includes.conf;
1 v' J5 X# K) L4 ^ root html;
( F7 {: I0 x0 z index index.html index.htm;6 r* r+ L! i9 U% D
}
+ s, \! u: R4 {}4 K/ I+ w1 d- `0 i
验证Nginx配置文件
2 V9 i9 F+ v ~0 n e4 x2 k3 I: w8 M! w$ /usr/local/nginx/sbin/nginx -t2 ?7 L9 \: W, d
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. i3 Z6 l) b( c) s+ j
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 I/ L) X" L/ _8 a启动Nginx& Y' F5 ~3 n9 x! e4 G2 _! v4 Z
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 z1 h; Z& [3 T
; z+ p& `/ [! O测试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能够战胜更多复杂/未知/混淆的攻击模式。
; n$ `8 G0 D' |. ~+ ~& J |