ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 X6 x7 O" Y' M9 n6 \% n6 H2 v- f
2 O8 @4 H6 k* Y9 @( M d
3 U/ |1 H# D6 ~( g
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& V- Y! g( G5 A1 _. }/ C
7 O: K2 w R+ l$ u: h
; ~, V0 z$ h" f* K$ y! _0 n: p什么是ModSecurity9 u# b! Y+ a+ y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。* S1 a5 L" P$ Y; e1 D# u/ ]; q
8 D2 N2 [4 j7 o G. {/ ?6 B
: h: a! Q6 ^7 z4 K% R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
$ s+ y9 O; y: g9 \+ E
2 a9 R& y, J4 }' N* I" W9 r0 S9 q' H e+ G3 d2 S6 f
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 D) E8 j, F9 U4 N
* i/ w# j: l! w* t7 h' e; ^, J; [) ]: \' t
官网: https://www.modsecurity.org/3 t N/ Z0 \6 y% J$ N2 }
3 q# r, t( |0 \& k O/ q4 P8 Q0 t* _$ I+ f) o
什么是OWASP CRS+ F9 {/ a7 I- q p y# w0 k
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% Q4 z; h( O/ [" \. _1 p2 F# s) e3 i' w' A6 T
/ E# h0 o9 y) ^- u1 c5 E
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 H5 x, D ^% U6 _5 {
4 }2 ]6 j/ F$ y/ ?: l: D5 n8 @! M' |5 x6 k- g
HTTP Protection(HTTP防御)% A& b/ S9 C# x' ~9 o
HTTP协议和本地定义使用的detectsviolations策略。$ Z0 \& |$ }7 M1 v2 W, k
2 V5 i2 [4 ~ i# B5 ], u$ z/ o
" [: T) P8 C7 t3 T: q) Z
Real-time Blacklist Lookups(实时黑名单查询): e j/ H/ u; o7 ?" ~' i) N
利用第三方IP名单。0 X1 e6 l" n% u9 o& I; u* N
. x ?/ x0 F+ s0 X( T* V
- x# `& n$ ]. LHTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 D5 }7 U" g; ~( E+ e; ~防御HTTP的洪水攻击和HTTP Dos攻击。7 N3 E. E @- Z6 @- v& s! N, @
M. _. ?0 G: e8 c% Q6 K2 \9 S% N% F( K7 m( n& D, A. M1 b
Common Web Attacks Protection(常见的Web攻击防护)5 R% }# H7 _+ s, X
检测常见的Web应用程序的安全攻击。
2 x* y" o( d: U3 h7 R; G' u$ I/ j, g8 ~2 @7 V
. x3 U ~6 R3 U. @5 T
Automation Detection(自动化检测)
( S4 Y+ I5 Z* v/ G检测机器人,爬虫,扫描仪和其他表面恶意活动。( T: [; o7 R8 G9 |3 a7 a2 m# B
7 e" _2 Z( p2 B( c3 p% w
) [% E7 G( S- @Integration with AV Scanning for File Uploads(文件上传防病毒扫描). M; U( f/ I3 p
检测通过Web应用程序上传的恶意文件。
2 D% U a9 O- ~5 w1 x$ s" u& v
( K. K6 j! S' Y& ~' S! e6 T$ V& ^; h% ?* o1 f' U0 i
Tracking Sensitive Data(跟踪敏感数据)
7 a3 k \; h4 }9 L- z7 E, |8 r# f! M信用卡通道的使用,并阻止泄漏。
0 t0 d5 x/ g' [$ g3 q K
" ?# M8 q3 Q# p' l, _/ a
# H- Y/ r, O2 f4 h4 ATrojan Protection(木马防护)
]( Q$ u* o. C- ^8 B- Q检测访问木马。3 w8 g/ ~, n8 y* o2 b1 A7 q
0 v; D. |! x5 B4 F
. R M4 ]: s2 h# v* s
Identification of Application Defects(应用程序缺陷的鉴定)& z' ]: G. T) j; f
检测应用程序的错误配置警报。
; K# \% H" _/ m& H6 t$ ~
8 O @" m$ z- y$ J( k6 _5 D& h1 m5 k
Error Detection and Hiding(错误检测和隐藏)3 k* i3 @" V/ K0 S+ J! C3 S: x
检测伪装服务器发送错误消息。
9 X, J8 h! q8 x: J- I, G! c0 _" L$ g' }0 p1 n! B* o
& r/ J) s" ~; J
安装ModSecurity5 W' b4 J% k$ G: C, _9 U' f
软件基础环境准备' g- U+ D/ v- ]# }" Y( O
下载对应软件包5 y1 X0 L1 u; ?3 Y4 G: `9 x" z$ x
$ cd /root) J7 [# z* H- x! V2 V
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
5 O0 Y) j# T. @6 o) M0 B- x" ~$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz7 D& j4 A2 Y2 P n% g
安装Nginx和ModSecurity依赖包
' H, _; i# U( g& P, I/ QCentos/RHEL
0 E, _8 D) E1 a
& u( P1 @- ^+ e- _! X0 o- {& k% ?* n0 r! P) O3 H* l# |; Z$ h& B
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 B* B2 _6 x% q! U( U. S$ |Ubuntu/Debian
], ^; N$ I: D( F/ c3 R1 y. H
7 v' X9 ]9 B4 a- D8 t6 q+ p5 {) K9 ]* V5 u
$ 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 W7 @& w! O& P) ^0 {$ s+ y
编译安装ModSecurity
$ f* G# C+ B& K# Y2 K7 ?, T4 LNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! k2 i$ K6 j. E0 F5 k; z- }8 T( }9 V* M# B6 N1 y( t0 d$ s1 ^
+ l% p# \9 ~: v; w5 e" T
方法一:编译为Nginx静态模块
: c* a. Z; M5 \/ b) v: K7 [4 ^" X- E6 @1 y
* @: i. V2 Z' B
编译为独立模块(modsecurity-2.9.1)+ G! `3 O. q' p* Y+ K5 T
$ tar xzvf modsecurity-2.9.1.tar.gz
. }$ B6 {& ?* T9 M2 c$ cd modsecurity-2.9.1/# _# ~9 O+ r' w& V, O- T
$ ./autogen.sh
/ H! k# T! ?) q2 i( z$ ./configure --enable-standalone-module --disable-mlogc
5 ?& H. i e3 W- P4 ]1 w$ make6 [) c4 C, f% E! y0 I3 `9 H
编译安装Nginx并添加ModSecurity模块) u; `7 x3 S- j6 u+ {9 Y$ N6 y
$ tar xzvf nginx-1.9.2.tar.gz
/ a" I0 Z( Q0 T" b! n; b$ cd nginx-1.9.2 z% f0 s# Q: p+ _( ~ |/ [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/8 N* l. z: b L3 |3 U
$ make && make install% G" L+ k! \) S% S" O
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, R( K9 c. m7 }" s5 b4 A9 ]2 w
- V6 t% ]# c+ E6 p- v, K0 T
1 T& z9 f5 R3 _0 B& [+ U编译LibModSecurity(modsecurity-3.0)/ d) A! G2 ]* \ ]
$ cd /root/ r$ L* _) c5 b5 {( A
$ git clone https://github.com/SpiderLabs/ModSecurity$ I8 [% y! ?0 g/ e& D& u$ C
$ cd ModSecurity
( L& J5 |9 o: I% V3 S$ git checkout -b v3/master origin/v3/master: h7 p/ F Z6 g* ~2 ~
$ sh build.sh. k& v( e+ D* u. @$ F
$ git submodule init
8 e; s' @# q0 j$ git submodule update. A$ Z; K) X$ \1 R9 K
$ ./configure8 D) c4 N! k( T0 T# J8 k
$ make F1 s1 d! w. l5 r, p
$ make install% X' g( d9 F V2 `/ y/ H
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。4 K2 Y! x* |, E, b; E6 K; s
; M/ v6 D/ E$ \, R
$ @4 d$ i/ E) s' U+ k: I
$ ls /usr/local/modsecurity/lib
/ |6 v' h; E* K. v$ Y0 |4 ulibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
; u. E3 {: j v4 I7 B编译安装Nginx并添加ModSecurity-Nginx Connector模块
2 l' b: ?; P P% t使用ModSecurity-Nginx模块来连接LibModSecurity! C* U3 d" R+ ^9 e5 ?8 i
1 x* c1 D$ p4 _) y* A: i% C
" P- a$ _" x, k- K$ cd /root
- A5 g+ E, q; r. H1 h W$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx9 \2 N; S' O* [8 v9 m
$ tar xzvf nginx-1.9.2.tar.gz
6 F/ e3 j) X9 \' J/ b$ cd nginx-1.9.2$ a/ t# b" @# @# R
$ ./configure --add-module=/root/modsecurity-nginx
# m+ ?8 x9 x+ I \ @. V$ make
z# f5 d5 l, a$ `$ make && make install. v$ @3 p3 ~* H! a9 W9 [$ s
添加OWASP规则" F, B, Z- ~ m% N- H! T* ?" K F
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
; @, m( W) b$ O3 v+ H+ t4 j8 Y; |4 V$ s( Q
" K8 w; r: z/ |4 P5 X
下载OWASP规则并生成配置文件5 h$ ]* Z5 w; j! U) p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
b& }; N- N3 U6 }' b/ P$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 ?, } v. E3 ]$ J& t( k+ l$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% z! w! Y9 d9 T! E" ?7 @$ cp crs-setup.conf.example crs-setup.conf
$ p6 }8 K0 O! s/ d- n& r配置OWASP规则
, ^0 p, M; |9 I( h7 |, L$ d# K编辑crs-setup.conf文件
! b) c* a8 p! P( x
" \8 N" ?/ q% a+ u( W. b, D8 s- W$ N8 C6 I7 z$ z
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" A$ v; d6 B9 V0 L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf$ I$ i( y: K, N2 V! a5 Y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
& t9 J0 L( B+ D$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, f- t; y) {, {( ?; y默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" V0 B# b) \4 |* e( `! ]0 ?/ u" ?) y/ @
5 F5 x, x& f/ `$ n0 X% v: _, R( G9 H: K& U
启用ModSecurity模块和CRS规则
: y: J5 e) \+ b* Q' {2 r/ k/ ]复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" U1 E m K7 u! K( S. m, V& a' }# K6 x
2 L1 v4 E' _6 v7 m- N% j
; c _. i0 e$ s8 s) c$ e- [$ }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& y$ ~0 r6 f- Z; @6 a. q [9 D1 b9 m3 ^1 x5 g# c; G- O/ m
1 O$ q/ l1 q5 b% K
$ cd /root/modsecurity-2.9.1/9 C, \; [4 Y) B& W; S
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ' ]3 y+ [) l: I
$ cp unicode.mapping /usr/local/nginx/conf/7 s! G: T# O" H3 q+ f5 |/ A
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( b' \. D+ ]/ s3 w% F$ z; U
; J5 R6 @ P! _/ J9 W2 @2 ?3 Z
- W' a- C* l8 f9 i) `$ vim /usr/local/nginx/conf/modsecurity.conf$ s- A, Y+ j7 h* W9 h' ^
SecRuleEngine On# `6 r; W* E* E
ModSecurity中几个常用配置说明:
+ o) Y; y R' p p: _
$ J' `$ O7 `1 f4 F, e& a$ Y5 `5 z& d9 s5 @
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! [# @1 h2 D5 e$ j% c
( Y$ n% R( b: [$ {( J" f3 H* o. m* G9 j2 V
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。$ p- v: K0 z' V+ j: n1 \% a" X
$ w! L! s& D) i. A+ g4 q8 ^9 A9 a: E/ ?6 ^0 o; L
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 y$ ]9 s2 L$ m/ H6 ^* Y
9 j& U! O( X& A9 P
3 G/ ]) H& X; C% h7 Q" R; }: P8 y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。7 S' Q! M9 f8 u2 L
) i# k; g8 B+ }" v6 i' T+ y- q g2 `' i; O" k8 ?
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。6 f' B) V( a$ T x- u" G, @
0 E. k+ O8 J4 s
1 H6 e: C, T) s+ s6 w1 U$ c* X3.x版本CRS
# _2 J9 U; H" y5 _* v7 M( f( T$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' E1 E: b4 ?9 D" Y9 W# 生成例外排除请求的配置文件2 E+ ^3 u" r7 e/ A5 b2 o
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf6 ^: P0 K6 p# f3 f, ^: p# r
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf( z% W0 O0 k' \6 }" o( G
$ cp rules/*.data /usr/local/nginx/conf
9 Z. t1 f1 C9 n& E9 T- A为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; y/ ~8 I% @& a: S* I9 B( D
- g. P+ Z( M/ k5 S, y( @# \7 }- w2 b% O0 q! Y& R3 V- A9 H" J+ d" o; f0 M
$ vim /usr/local/nginx/conf/modsec_includes.conf N& [! B2 R" Y+ P' D
2 s. {& l+ `3 K" \0 f. Y B3 H[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 6 Q9 q4 f0 E$ I6 [
( U- v) C) w( l注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。 r$ n+ Y) p+ ?1 F0 H5 R
6 m, B: w# G% I
5 T0 {/ T. V0 `. c' m
配置Nginx支持Modsecurity
: G8 H7 g0 ~" ]4 i* k5 P9 T+ M& M启用Modsecurity
8 Q" u+ L; H1 g& b使用静态模块加载的配置方法) p9 l Z6 v" v; k2 W ?! n" t4 @! V
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 X6 G1 w0 _8 h. g9 d* |
) i& ~: Z9 C6 i% W3 ^4 g8 l' C# r6 m& K$ h9 ~) f! E% m. s+ F
ModSecurityEnabled on;
0 i1 W& [3 p H& X2 {ModSecurityConfig modsec_includes.conf;: A( X- k X" s* |- p" n
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
5 K% O" d X# m `8 I4 P" o0 e2 v* s3 ]
- @. C( ?! b5 k0 r/ }; z7 g$ vim /usr/local/nginx/conf/nginx.conf
, t1 u; w6 r+ X( Q# M) i) x* Y- y) }& t) C! c1 ~2 A
$ u6 y5 M$ R& j# U$ K7 xserver {
# }4 m# I7 E" \: q/ K+ H listen 80;
2 P) \$ O5 i; l s9 M9 s server_name example.com;
& Z/ N0 ^: p* }9 N1 `
/ ]: \% O; P1 |/ o, E6 R. \6 a0 ^& ^2 S# e: S6 h7 v* v4 H/ g+ z
location / {9 [# i+ F. p e; |1 A7 P% ]
ModSecurityEnabled on;2 R) N5 ^, L4 P" M$ }( ^! L
ModSecurityConfig modsec_includes.conf;
6 ]/ |+ Q- R S& V. j5 I( | root html;
" O- C9 D7 D5 U0 C7 w1 M9 z index index.html index.htm;7 e( c6 C2 c6 o2 K7 j" j2 g
}9 C3 B# l. M% h! L
}
0 D6 l; Q0 R. ?+ N# n使用动态模块加载的配置方法
7 U1 H& {1 C$ N! j0 L8 [在需要启用Modsecurity的主机的location下面加入下面两行即可:
4 Z5 k h# i4 ?0 O6 q$ L* u
7 w2 k" i) q; P* O6 v& D
, S! s" e0 x9 p$ A. r1 gmodsecurity on;
X0 f3 n! x- j* gmodsecurity_rules_file modsec_includes.conf;5 I- O# P) [ L+ q# D$ H
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; V/ t' T8 o1 T0 y$ m! H+ m- m1 O# S( o3 [ K) g
3 a9 @8 R6 t M6 h1 o$ H$ vim /usr/local/nginx/conf/nginx.conf0 z) y) ~3 x& Y0 }, |
# l3 X2 [5 T1 L# W8 ~
4 T4 q. \, e+ s7 s! d
server {( u+ r7 P6 D6 V( z ~! `
listen 80;
( E4 [/ ^) \+ o# t- n; S server_name localhost mike.hi-linux.com;1 p, J# u4 ^+ X
access_log /var/log/nginx/yourdomain.log;" G& N G; @$ m% ~$ {5 I
6 f3 d% `% L/ Z! Z- U+ U
( k6 N; Q/ ?* F v+ r location / {) p, J& t! L9 I( X5 r& d6 Z
/ D0 O; `+ u& V, N4 E6 Q
# J& y$ p$ p% Q- g& Q- z1 ~
modsecurity on;
5 {) _) s: b8 v- Q modsecurity_rules_file modsec_includes.conf;
0 @7 Z1 X+ B. i1 m) ] root html;
& G" ]. x9 l- V2 u& G) \6 F index index.html index.htm;- J8 @; V. c5 y6 C
}
# Y! R: d9 W* j" R}4 `/ s* ~; @" j$ X8 f
验证Nginx配置文件# w' b2 Y. g: ]! V: k) b
$ /usr/local/nginx/sbin/nginx -t
8 x+ L6 S% |( O8 t3 H/ U; @# _0 Wnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: r% _' ]% M. o$ Q Hnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful+ U6 @ b+ z8 z
启动Nginx: A) _( ]4 `, D
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
! k2 Q" Y) Q2 I/ A: q% O
& V# C4 n$ \1 Q! K8 _测试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能够战胜更多复杂/未知/混淆的攻击模式。 / R7 b9 `, U! v9 @
|