ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 M" x# I- e q$ A$ }
$ @7 j6 V5 T2 D& v) }, |1 \! d9 C- o9 Y6 L, t* [- E" j
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。; w9 q0 r2 \: B+ \- B/ q
* u* ?2 s! E* }! l
$ o7 N* p+ V: ^5 Q1 y" f什么是ModSecurity
; o" ?( n* p- ]' f1 e- {/ ~$ J: \3 fModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; E& a% {+ s& g; v7 H% ?
# Z4 f& `! E6 a1 K+ X- `, O
# d! ^* e# F- z$ _8 [ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
( U3 J* g$ ^ P- P2 m$ p7 x" ^ R' T. E- T$ k$ l8 e; V
2 [7 m/ Z6 n* RModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- w; Y7 f- u! |+ W+ n3 A
( J# Y+ ^# M! G: g z0 B6 E& Z
/ q8 N+ k( T$ N8 X官网: https://www.modsecurity.org/
. u1 T0 a1 _! @6 D0 x* J+ y
# s( S6 F/ N' |0 e& X1 e$ M H
7 G6 o3 T* J, {2 C什么是OWASP CRS
) ?5 x( T0 ^8 |& V0 M" y# jOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
2 N8 e5 z% C& |' F( M0 ]7 k, d7 W( t+ p7 K# k
1 U( z- G! E* i, l* O3 ]ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 _0 o; x" \( W, T4 }" H
3 w) C( x5 u+ _5 s
3 f0 t& Z9 I: j# @4 nHTTP Protection(HTTP防御)$ j. l \) o* |3 }
HTTP协议和本地定义使用的detectsviolations策略。: {" v: b* X1 F$ W
5 D# z$ E; n6 M' ]
5 ]; \2 x$ B& f o+ L! W0 J
Real-time Blacklist Lookups(实时黑名单查询)+ Y2 U8 c' _( ?! y
利用第三方IP名单。
& V7 F: r+ L) Y8 R/ d' P7 H' {5 Q
0 {% N. ~0 t! l* s) `3 c
4 X" f# K% f4 k1 I' g+ e: NHTTP Denial of Service Protections(HTTP的拒绝服务保护)
/ G1 X& ?7 _5 T1 r; F% H. t( p. y防御HTTP的洪水攻击和HTTP Dos攻击。
* N2 F! x1 N m* X$ ]2 U- c1 {# b+ p2 a" H
& m( g7 s: L$ L" |; Z j2 `6 \, @
Common Web Attacks Protection(常见的Web攻击防护)
/ ^" ^' g: F6 i5 L: v( K检测常见的Web应用程序的安全攻击。
L+ @% ^7 U" G ~) b8 k w- L$ ?: z; P
8 E, n8 z: F$ t, d+ sAutomation Detection(自动化检测)
% y$ [( f p+ n b8 f6 I检测机器人,爬虫,扫描仪和其他表面恶意活动。
+ o8 C, b4 S9 w" w, A
' ~. Z4 }& k) G! _' Z$ u
1 |: Y0 T$ X6 b' j. f5 d9 \- aIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
' }* P" \" H1 b& ]1 e检测通过Web应用程序上传的恶意文件。0 h5 ^5 h* p& W6 Z9 R+ c
0 ^, p8 t- x/ p6 ]* O0 L
. D4 i2 g8 v4 W0 h
Tracking Sensitive Data(跟踪敏感数据)5 Z- J9 H* `# K; n# m/ k& U
信用卡通道的使用,并阻止泄漏。
9 W% J' D! `2 A. Q
; ] z( ~! j0 F1 J. K' G/ @5 V8 K4 A; Q6 s8 B' W
Trojan Protection(木马防护)
* ?+ d5 x% i) j: d6 ~. @检测访问木马。, L: W# `3 l6 m
* A6 N, q. d* Y0 ?( Y: a" S' t0 H/ T8 V4 Y% \
Identification of Application Defects(应用程序缺陷的鉴定)
2 T e( Y @% T: g( Z1 q( p4 ]9 H" ~检测应用程序的错误配置警报。
+ k. R K' m7 I) N9 u
: u$ l! w/ w( Z5 c- b
' C+ k I8 Q: X+ M- d8 d6 PError Detection and Hiding(错误检测和隐藏); L! c7 t7 ?7 w8 \7 h' v4 d
检测伪装服务器发送错误消息。3 F6 o4 J! j3 F/ E* r% s
/ U6 U, X5 t8 ^1 p$ I1 j: k' Q! e0 D, \/ Z1 Q; ^/ s8 d
安装ModSecurity0 N( I: H2 {( @0 G3 o, n) V% B
软件基础环境准备! N( L4 E2 w( F5 G j! J- c+ U0 w" l
下载对应软件包# o. @! X# ^( q! {/ @
$ cd /root
, h- I" s* [, F/ m, w$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
. ~: c! d4 ?8 E; L$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
$ E1 n; G. J/ \- c2 p$ P5 i安装Nginx和ModSecurity依赖包
3 P* l+ G* {1 ^. J8 ~# zCentos/RHEL: o7 P; K+ A, K; P
) G" I& t6 L6 y" ` ]+ _: p$ q) ^: Z1 M
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel7 B: H! L) F0 E7 J9 e0 G8 U9 v
Ubuntu/Debian1 V: \( F L. O3 Z+ Y3 l0 f, P% D
! b; c) ^9 a' J; c( ?5 [, X! C/ ~1 w0 k# N; X, p
$ 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( \% C' T( D. z
编译安装ModSecurity) B+ ~/ Z# U: l+ G3 F
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
) ]+ a$ z/ {1 A+ p
g( I1 y9 F; H( t; P+ f3 @7 }8 n, U
方法一:编译为Nginx静态模块
2 z2 y2 b& L( E% k2 d8 c6 n: I7 t7 T9 p. E. ]( d4 ]% v
# I' }9 t$ |. e- i g3 Q' b编译为独立模块(modsecurity-2.9.1)" T! _0 Z2 Y( Y# T
$ tar xzvf modsecurity-2.9.1.tar.gz- @" f4 Y1 t9 c/ s" P
$ cd modsecurity-2.9.1/
: T) }3 c: G0 p d2 G, e6 D$ ./autogen.sh
5 y* `% q7 z1 J# y$ j+ w$ ./configure --enable-standalone-module --disable-mlogc
) n4 E" o% E6 ~' h" Z( U$ make: E' K) T; ?/ L; Q* n) t
编译安装Nginx并添加ModSecurity模块
$ M3 B) l$ C8 ^% b5 B, S$ tar xzvf nginx-1.9.2.tar.gz
( F. O. \# J( J$ X$ cd nginx-1.9.21 |* x" z' @! O& e
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! o" ?) H% o' y1 F/ P! `/ Q; a9 z$ make && make install7 b; z/ m! g r
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, p' {: G/ {$ ~' [9 t
& Q' k' I6 t4 @) Z: H
) ]& M" N' G+ J5 T1 }编译LibModSecurity(modsecurity-3.0)
+ j: Y! Q L( @) N% _7 y$ cd /root0 _/ U* y$ q" _6 M* P ?
$ git clone https://github.com/SpiderLabs/ModSecurity
6 |! u9 c! c! \# t' q% _$ cd ModSecurity. q& |* M- X7 M5 A
$ git checkout -b v3/master origin/v3/master
# R; w- }9 F# s( r2 D4 `" y$ sh build.sh# I. m' [6 o1 O, \* l( {( S. t% }
$ git submodule init9 }- O: o& @ D, r' e5 j
$ git submodule update! _( r/ c o6 X
$ ./configure2 @) Z1 O. K$ S
$ make* \8 l U, G T0 z4 i& g( F% b# z
$ make install$ c9 x7 r* A! m# T5 R/ B
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。7 j5 L% N) e$ s8 W- d
8 c( [ e: z* ?" \* v7 x t
( F. J3 A5 e/ X" c' Z1 L7 x2 R$ ls /usr/local/modsecurity/lib# t4 C" f* w& W# e$ g
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.06 E$ u7 c7 _6 D% e# R* U6 O$ P- ?
编译安装Nginx并添加ModSecurity-Nginx Connector模块! ~. c* \& {' L* `
使用ModSecurity-Nginx模块来连接LibModSecurity' I: {2 r) D, }) K: Q
$ d- O o; O1 y' g! ?4 s+ I$ V% O
% e* d$ b; o S; x( ?9 a, \* C6 C
$ cd /root* Y1 U4 U! k3 A. u! F! p" ^
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx) J e( t0 b2 h$ u5 J5 G, m
$ tar xzvf nginx-1.9.2.tar.gz
0 k" V' ]& Q4 S% {0 h/ z$ cd nginx-1.9.2; z: a$ q( D5 _" a9 M- U
$ ./configure --add-module=/root/modsecurity-nginx! @6 b# M) g% V J) }& S0 V
$ make
$ T( O9 r! d0 H7 x9 ^5 q$ make && make install
& T4 E k* j. |6 ?添加OWASP规则
, [7 H U3 X" w2 WModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 p1 C2 b$ S' e9 z' {) I2 N( L' ^2 `/ j
0 V8 V: f. G* a. m1 [7 u0 n( t; [# l6 f' F' B, z: J2 @% g
下载OWASP规则并生成配置文件# U. x, P# Z. b; |* y4 e
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. ~5 X4 b( @# N2 ]3 R- ?0 h$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
- O* t5 L, u" }0 T/ ^2 u5 o$ cd /usr/local/nginx/conf/owasp-modsecurity-crs& ~ z$ ^7 ~6 _# n7 n
$ cp crs-setup.conf.example crs-setup.conf
! Y: U# s* v$ ~% N2 }& o8 j配置OWASP规则
/ O( G) P8 b' ?+ o! B; Z编辑crs-setup.conf文件0 I& t8 v- c2 K+ \& z& d' F, ^
) A. l. {1 n( f2 e
% D+ r* P8 O2 t! v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
$ P: B) x! K" H& j$ I$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& o! H$ _7 |. ^3 Q9 W; b, Q4 o
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- i! t+ F/ I4 G1 l, A) s
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf$ L, g1 E$ H* G7 e' u2 n& \ I+ t' R
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。' F% M) o5 C- B3 ^7 Y6 t, X! i; _, w8 w
/ _7 R5 F5 e# T2 l- J/ h/ @9 }
* q, g# o8 L8 g% z5 U0 R' F启用ModSecurity模块和CRS规则
* m: d/ x& [7 ?) U复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( ]; n: Q/ B2 H. w/ p/ k
) Q; u, M; t6 q# j9 a
# z1 N; w* Y1 Xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
[) K5 M- l; ~/ C8 S" V5 B7 p1 B) E9 O
' Z$ B1 b8 y( _6 A- X6 J, _' Q9 C
$ cd /root/modsecurity-2.9.1/' S: d. j. g( i3 f( V' D. `5 K e
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
' L5 F |5 B0 |' o9 O ?3 n$ cp unicode.mapping /usr/local/nginx/conf/( s+ h. ^# Y; i
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
$ I) \2 x9 C7 e4 i' h
) s0 T q3 v/ x( [
/ ~1 j* M& i3 B$ u$ vim /usr/local/nginx/conf/modsecurity.conf
( F$ p2 h2 A; L2 p) cSecRuleEngine On
$ D8 \1 P( F" ]7 o; d! L% C2 i; zModSecurity中几个常用配置说明:
$ v: _$ J0 c# _% G2 R: X0 J" U1 p. ~7 q1 V2 f7 K! A! o
9 H5 E0 }; ?7 d$ n" ]: G
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
) x$ B, O, K' B3 v- h( W! J) O- { r: Q3 [& Q
. I0 k! \: P$ ~
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。4 {; a. h: G9 k3 }2 c- S' s
6 o# ~7 Y. n M7 |
# U- N. y6 ~0 L) D; J3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: ~/ k& A R' g* {8 T. T. o. F- ]& ?0 e3 _, w5 i7 U
: Y0 q" M+ f6 i: \6 z, V4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# k& E9 K! Y1 u
8 L: r6 Y& r, [$ p
g5 R% q3 u' Z) ^在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; V( g( L* H u, q
- ]0 f$ @- J' {& t6 E+ V6 Y# ]- J, e. {
3.x版本CRS2 U) u$ ~6 [" f* M; k. G2 {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% x- \% ~3 d! W n, j. i% H( J a. ~" r# 生成例外排除请求的配置文件2 v4 ~+ u& p8 f
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf9 d' s' F' W* ?) E& s: {8 e$ W
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
1 V3 S6 Q# A1 U: R( ~% Z0 F$ cp rules/*.data /usr/local/nginx/conf7 f) d7 @4 d. R* D! o$ J
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。3 m; l& \! k2 S% b% `0 \/ p: S# S
' e+ q7 K! U3 i- J
/ {9 X. r( A, h) H/ ]
$ vim /usr/local/nginx/conf/modsec_includes.conf
% S7 M/ f; v [! f# ~/ B7 S6 g6 o# E, R9 d. h3 x. F
[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 - j' `8 w: ?5 W7 v- c3 o& T
# K, M' J+ w9 V4 R s注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ S/ X/ Q9 Y& P6 b
4 a( A, g# L+ U2 f5 Q' c Y
, h6 L5 ]0 c: j. ^ @2 u) X0 W7 D配置Nginx支持Modsecurity) _3 ] ?" W& z* |) d0 E- e' z
启用Modsecurity
0 b; v1 ?/ k# O& i使用静态模块加载的配置方法! C) |7 K% N$ m
在需要启用Modsecurity的主机的location下面加入下面两行即可:* Q4 C3 S: h% r9 l3 U! L
8 s0 e- ]" r- C6 w( o
' T# l/ S) T; yModSecurityEnabled on;( U* X5 ~! R# E
ModSecurityConfig modsec_includes.conf;
# r& r9 L5 \0 N4 o$ m/ A5 b8 V, Q8 x. Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% T# G4 B4 m8 P- }7 D q! n- a
) K' ]) z: Z( f( y
8 @- W3 s$ ]9 ?8 E$ vim /usr/local/nginx/conf/nginx.conf
6 z: \, K: Q& K
: A. Z0 X3 m+ H& c
1 a0 f7 q* t- {' l2 x( {7 ~" nserver {+ R- ?4 R/ ~! {! ?$ `
listen 80;
6 m1 E- S6 x8 o server_name example.com;
8 o& O/ t4 C5 a; A. j0 s
, U2 Z; r* `! F+ d9 U" t3 U r; G. B6 t# q: m
location / {, e& x; |( v5 A
ModSecurityEnabled on;
1 t4 }- c2 h1 M$ J3 z% I ModSecurityConfig modsec_includes.conf;# b/ P1 c/ m" T- u' }/ }
root html;
) |& [: S5 y* `9 B/ r5 a index index.html index.htm;
& U' Y, k4 D) G5 B }5 i' J. }2 K5 S4 ~4 ~6 i: n
}
) K# p7 c" b& s' {/ ?" X4 X5 Q使用动态模块加载的配置方法7 m$ d8 E# k! E; B( I* I8 r
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 d: e1 ?( r; d0 j
0 z( C/ V' s M" G* i; ?( Q7 a, n$ M) A) u
modsecurity on;
( `. v U/ F7 @8 }/ D$ t/ tmodsecurity_rules_file modsec_includes.conf; G- M2 a, S A- `1 T. k
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 z, i. q8 F% P3 d( l/ D
8 w1 z* h# \# Y3 a2 c4 W3 H- I. z1 H3 j F) f
$ vim /usr/local/nginx/conf/nginx.conf% ]! R% t2 w6 F
' A% b( O1 j# O2 X: }/ i
, ?/ C) _; A0 a& c5 F) P i/ l% b: wserver {1 r& z; N6 Y' y5 g P6 @7 a
listen 80;
! d, Q2 z' z; x* p/ c server_name localhost mike.hi-linux.com;$ j) @6 I# r* F# |# N5 P+ h+ @$ ^9 D5 i
access_log /var/log/nginx/yourdomain.log;) {9 I- a4 r0 n5 Q& I. @- ^
9 z* l0 @8 R9 |- B; }7 w6 X9 ]( d' p
location / {
6 F7 G* n* K4 {" B1 b) H
8 ?. ^, E% {0 g/ w1 C
& [0 @& T: v9 \5 d1 d J. S4 ? modsecurity on;; d" ]% x" P5 W: ?5 y
modsecurity_rules_file modsec_includes.conf;
8 M0 D- r- i- G5 n3 {9 q root html;2 J' w) w1 W* R0 f* b i' h/ d
index index.html index.htm;
9 n6 r% u" R3 H$ ]& [}, c, ~$ N* k! y; z) B
}4 R5 e9 |8 J' o3 \2 T
验证Nginx配置文件
. Q$ k7 ?1 Q% w9 x$ n$ /usr/local/nginx/sbin/nginx -t
# w, [# x) E1 n/ anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
! h a3 k- m! R: w. F" L$ Lnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 n) T# f- J: e: Q( J+ L& U; D2 s
启动Nginx% X' E, C' `9 T2 f3 A
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
" Q% Y( ]# u% y
3 \( o V3 I# ~3 u+ N) H" O/ R! T测试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能够战胜更多复杂/未知/混淆的攻击模式。
% Y4 Z# E4 k' B7 g/ {! K2 X9 R |