ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
0 @2 }, G/ o- G- F& |) k0 ?9 M$ S8 o( Y& F, l0 I4 G' p
5 h* ? _6 Q* t) R0 r
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( C$ `; h5 U) U. {7 n9 m
, m8 o4 C0 k5 k2 g6 I
; f U8 h; x! a什么是ModSecurity' K7 T' r5 Y. s1 \9 |4 `
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。, K# Q4 E' o( a) o
9 H2 b! T6 H# r4 C. g' M Y4 F: X+ x/ V8 \$ X! L6 W& `! p
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。1 s2 B) B# I1 n; ~+ A; P, _* Q
& @9 e. n5 y2 u& E$ g" f$ L
1 `& y) p7 P. {! j" K0 qModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 S0 ^0 A* C: L' ?
i( k) l/ z* o o) }6 m. e, q$ D3 l R+ G- x
官网: https://www.modsecurity.org/% o& c% S& t1 }! ^# S# |9 l, Q
3 H- L. e1 G* E+ z; Q+ K4 I; k. e. y6 i6 Q5 r
什么是OWASP CRS
$ N( O1 u, {( ?; U& GOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
d* K- p' V4 z
& Y6 ~2 K2 g( ?, G5 Z* R, k& s I0 `5 a1 d
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。" h' N. L5 X. d5 f3 V* H
1 S- W# N# w P0 v
, G0 _3 e/ S0 _# ]8 z, ]4 qHTTP Protection(HTTP防御)
; S4 Z& j9 g: g4 SHTTP协议和本地定义使用的detectsviolations策略。. H! Y7 S7 q* K6 W
- @: C" Y$ U& [- m9 W- b; g% M% U) i$ s/ D( L
Real-time Blacklist Lookups(实时黑名单查询)
\ C( {/ P( Q) @# w {利用第三方IP名单。& e' s7 Y4 G& {" Q- ~' R
. z5 e2 N7 }8 q2 I) W) m0 Y {* h1 _$ j7 y, k6 y8 }6 |4 n4 g
HTTP Denial of Service Protections(HTTP的拒绝服务保护)! C `! _5 y! f2 u, @; M
防御HTTP的洪水攻击和HTTP Dos攻击。
9 j9 q# s$ d0 @: b' O. l
' L+ {* p; L; l
" ]: @5 t% z; E; BCommon Web Attacks Protection(常见的Web攻击防护)0 L: M* M0 y; F* A3 p
检测常见的Web应用程序的安全攻击。
# V u) f% \: `7 @. u7 J& U; y3 c( w1 q$ w' f
' C. j+ U+ |' T1 y0 OAutomation Detection(自动化检测)( j: x# Y8 K5 D7 [4 A4 t% v
检测机器人,爬虫,扫描仪和其他表面恶意活动。* _8 g1 x$ C2 L- q* b& N4 Q
- i. b( r0 d" f
$ D8 v0 Y( @, n$ y" m( |; O" C% h/ kIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)7 v4 H& }5 [3 G( ]
检测通过Web应用程序上传的恶意文件。
* [2 H& T+ ~) c% w1 W: d h9 D/ [ G& C+ j G# C9 m% X
4 m) P' d' W2 |4 MTracking Sensitive Data(跟踪敏感数据)! y. B. u3 O! [1 y$ y2 T
信用卡通道的使用,并阻止泄漏。) u8 Y. b5 g4 Y7 }3 \8 p {/ c% Q. I7 L
; ?. r* o( h- F4 s% X) N. U: s
S. _! Z* {, h$ r! B) xTrojan Protection(木马防护)+ z _5 c. E# A4 \1 Z9 M
检测访问木马。
* E7 r% d$ B0 {2 y d$ t
4 M! a0 e% ^ M" s1 ~5 m6 Z8 X9 M- u/ d" g' X8 Y! w
Identification of Application Defects(应用程序缺陷的鉴定)
7 o: q* D3 m% j5 q- T D& w0 {8 h检测应用程序的错误配置警报。
& i/ L; k) g# A9 g
' P+ Z0 ~: a% m5 H {: X8 E7 Y
: ]( c0 W7 u2 @0 T. Z$ A& p" |Error Detection and Hiding(错误检测和隐藏), Q/ W* g, [9 \" j: o1 g( ? R3 \
检测伪装服务器发送错误消息。) [7 W9 I1 {. \- z5 M3 _
% Q3 D8 b+ L' I, |: Y, R$ [* `, A" Z) G5 n0 h
安装ModSecurity5 p0 k5 M4 C4 V- m# {
软件基础环境准备; l' e/ @- z! d* z+ L
下载对应软件包
5 p5 }, v* s8 c$ E8 Q/ w9 V) H! t$ |' I' g$ cd /root) q6 Z2 l) l5 X5 |7 ~) u! k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% x1 X; @5 \, W
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 ?" K& l; q+ A0 }9 A) B安装Nginx和ModSecurity依赖包
2 b4 X! | O7 I' [Centos/RHEL
8 x& X. Q2 d# @% H( S- Z r3 U+ }0 \
, V- o7 n* @( T" K$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% e4 F- C( n5 o1 t, Q7 }) {" tUbuntu/Debian9 B; \: M, h& S/ f. _. c
" P. \' N T6 q* @+ y. Q" \" O L |
M# g8 K7 M9 P8 t$ 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
! F2 j- h4 I" p, }5 \9 c0 R- `编译安装ModSecurity
8 X7 s# Y `) W2 E5 INginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
' V) o* \' W) X* G$ d4 }& j5 D) ?8 a
& u- ?9 c( A% E% [1 f8 Y) u1 e$ |/ s+ T4 r6 I: `! ]4 b
方法一:编译为Nginx静态模块
7 p+ W( ]# b+ n
; M- P2 V- k& V/ i( D% Y. v4 [% g# U9 C0 d0 `5 ^# b' @1 ^& x/ n
编译为独立模块(modsecurity-2.9.1)
+ A, G7 ?3 y; p( \$ tar xzvf modsecurity-2.9.1.tar.gz
6 P- x) j3 D) w; I5 s- I, p$ cd modsecurity-2.9.1/* j; [" i0 V, H5 |( P) z7 S
$ ./autogen.sh
0 w' l( \+ C6 l: y Z4 m4 V$ ./configure --enable-standalone-module --disable-mlogc
8 c( E+ B& z& w: f7 W! ^- W$ make1 C5 u3 b1 h7 Y& h9 _6 \# ~
编译安装Nginx并添加ModSecurity模块
0 U) I6 ^) h8 i3 O7 a$ tar xzvf nginx-1.9.2.tar.gz1 |; c0 {& X/ Z$ ~! j0 k
$ cd nginx-1.9.25 S' \ { g) ]$ _; v: F7 c
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# ]/ Z- U. E" J8 w
$ make && make install
3 d6 n; M9 |: M9 |. z3 S方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
# Q1 N$ z2 `/ R3 @
$ ]# _; H: o3 r$ F: N, U8 N9 d2 _% S* `- T$ ^1 {
编译LibModSecurity(modsecurity-3.0)
2 _7 a' U# k0 o6 u- n9 N8 J$ cd /root3 R6 B( a+ G( t' m6 `1 I4 q9 R
$ git clone https://github.com/SpiderLabs/ModSecurity
* A1 ?3 M4 T; e: d( g5 r6 Z6 a$ cd ModSecurity" v# ?, E0 h, ~/ |* G( a
$ git checkout -b v3/master origin/v3/master
) O. |6 q- r) w$ s$ sh build.sh
; o3 g/ k) p$ \; t) Q O$ git submodule init3 `& K0 ?- |; h$ k
$ git submodule update' F: W, l' d0 C( T9 Y- N
$ ./configure
1 a. m( A+ g, d+ y' l$ make( i) x% R! @" v- O2 }2 X. h' j
$ make install: E5 u( S6 ^+ r( \. ^; q# l
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 g; M( X# {; s+ ?$ Z" u
1 L: Q1 `4 q J3 B
" ]% b9 E' w0 ]4 e+ R$ J0 f3 h$ ls /usr/local/modsecurity/lib
8 s! O% v7 [) V3 Rlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
2 M, T) T/ ~8 v6 m9 d编译安装Nginx并添加ModSecurity-Nginx Connector模块
X5 c. s5 F5 Q }使用ModSecurity-Nginx模块来连接LibModSecurity5 c! D; r$ n) _% y2 {
. _8 }* x R' [! y; P
6 O q4 `8 w+ p% H. S. a$ cd /root
; P8 L, j3 j& @! a; [; Y$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
+ ~2 N% l5 a) O0 i0 e# e$ tar xzvf nginx-1.9.2.tar.gz
# T( a# d2 i5 l; X9 _3 B, L8 b9 K' e$ cd nginx-1.9.2
9 K, u; A3 ?& @3 d7 P, }0 c$ ./configure --add-module=/root/modsecurity-nginx" s8 U2 I% v( E' ?% t) E, f: u
$ make
$ i5 o$ l6 e( v& p9 f% }$ make && make install& D' e+ C( E. ~; S
添加OWASP规则$ }, u& V' D, E8 f# a; h" z$ i# |! J, m
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。/ D: Y; a- z4 A
0 p q# k% K s+ W4 B
- s3 r+ E' o5 R! V- L$ ^
下载OWASP规则并生成配置文件% [( l, n* M* \9 N3 r
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git R5 I3 s2 D1 i" U$ Q5 u
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
: R2 h# q3 ^; }2 @) E- Y6 C$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 i% j% V }7 K6 R |5 Y$ cp crs-setup.conf.example crs-setup.conf
3 K( u) z4 j2 p: M: x' J2 m" c配置OWASP规则
9 g1 P& Z& T5 W4 ]$ m2 `, q( F- z编辑crs-setup.conf文件
! U, W( D+ T3 G0 k' s2 A, x/ [# ?. }0 x, w! D& @! h
8 n- E5 x' I% ]. e& @$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: S. a8 A0 Q' r; |
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
* M; [) r+ |3 w/ [; j$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
7 J4 \9 o* \. i$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* X2 J6 `4 u" ~8 A# n& T' n
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
1 e$ |2 d; D0 c8 r9 X; X
5 `9 Z3 k4 y. i/ N! j1 D' A8 L! E
' F' N+ v ~/ Z" }4 G" m5 ]启用ModSecurity模块和CRS规则
9 E% [' q3 Y0 u复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ U# _: ]) c$ {1 d5 `7 u7 F% a
" _; p0 f) v- h, V5 f' L# |
- @# O" U( j) \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
6 J, G4 i) o7 `0 I/ t! Q3 ]5 E! g q" Z. A
9 U4 ]; c( R) S: u- w/ R$ cd /root/modsecurity-2.9.1/
+ o7 D z- F( A7 ~2 Z8 i, P$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ' k, y9 F# F+ m8 Q) _. H" g$ O! I
$ cp unicode.mapping /usr/local/nginx/conf/+ K& a q4 M% U, a J4 l' x/ g" K
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。& A3 U. w+ |! _9 M; C
' \( o5 O% d8 ?
# Q8 v9 Z: K6 }7 F# a$ vim /usr/local/nginx/conf/modsecurity.conf" R5 f9 h, W( H0 T- r- G3 s
SecRuleEngine On
" C7 L* C0 F6 B, l7 n2 ^: V, cModSecurity中几个常用配置说明:
* `! ^# V, O% y h& T% r
6 F V4 g( z8 w4 P
' \: K ?- l1 T$ u6 P9 S4 a1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* I1 {9 f9 C6 g8 F. O
6 k( t) j: E% [& s2 k0 H0 k8 U( i8 q1 p9 k0 @
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。' U' D, L* \5 Y
. t; x. H; o2 F/ j
6 l, J: J: G* L3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ B0 m" v& ?* \& ~* E7 @' m# g. U
. \5 C' |/ X6 `7 y
1 G6 z7 y, ~1 Y1 c8 [. v/ x( m4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 ^, p) a. J4 U& F9 r( }
: E5 i' ~6 \* |# h+ P- y7 a' K
% K# h+ ~5 d$ k! A5 @) E% ?" ^5 _
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
1 t6 Y" I c7 E. K* ?% Y) ?
8 Z! N) H# y/ w) w4 S
8 b: n# n2 ^3 X+ u, C- f& m3.x版本CRS8 t- |- b2 S) m1 Z5 r& i
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 B* m4 i! ?* t! y! U' R; N
# 生成例外排除请求的配置文件# U- x: f3 U/ ] @6 N
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
! o. |% m4 ]+ x- F4 T$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 j) {: y# ?8 j0 d; r
$ cp rules/*.data /usr/local/nginx/conf6 t: o2 c6 O! R, t" l4 L
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
4 B# Q i" J" Z. T
; [0 f: Z9 N0 ~+ Q2 }# {
- N W2 B5 u' }$ vim /usr/local/nginx/conf/modsec_includes.conf/ D) a8 z% X2 I. g C3 b
0 W/ S5 R0 q, n, F- r' L) d
[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 3 `% M) u* l+ Z; S4 K4 g
& N3 U9 T. e6 D% `7 i注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 g0 j. ?/ D4 }1 O- p6 q/ B
3 ` N) V9 c+ P, N) u# V
5 p6 F c6 m# y& `. o; w9 y
配置Nginx支持Modsecurity
$ j- d% J' Y9 v% C! A& k1 e4 h启用Modsecurity
( d' G$ I2 W' G: C使用静态模块加载的配置方法
; n0 u( K6 S0 k- B; I+ B在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 E' c' {: ^* l% o2 p0 ?( K! q6 d7 Q. p
* c! V- W9 y; {) Y
& O, j6 u, H9 kModSecurityEnabled on;
2 e* \6 A. [( o3 {/ xModSecurityConfig modsec_includes.conf;
: s, T3 \( m( G, Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- L6 Z0 r0 D: y1 C
8 O- B- v5 x* O3 @. g* z2 _
_' n* ]* }' `4 L( j- m- ?. |3 ^
$ vim /usr/local/nginx/conf/nginx.conf
; K* T6 ~5 Y8 I3 G) Q' X
- ^+ _7 F/ t n1 Q7 c9 G w. B
* }& R0 ?" j1 \/ R8 hserver {
! L& C- a3 C- d8 p" Y9 a listen 80;
; Q$ v6 C; ]8 }" I4 l. i server_name example.com;
2 u s% i$ r6 x7 U# x" Y( [% B( W1 b5 N, V3 D: r" ?4 D
% K" m. x, K) y. x) }
location / {7 G: p& |6 c) l. C% [& [3 Y {
ModSecurityEnabled on;) C& w1 Q! M9 H1 w2 i' n
ModSecurityConfig modsec_includes.conf;' h ^. E( e; d9 r
root html;3 @; G" `) m* E, r: l8 L
index index.html index.htm;' C' `: |9 v; |3 g! F1 B: o3 {7 m
}
5 @) F, i& y' l) c}
) _8 A3 @ S5 {- T: J E' @# k; {" @使用动态模块加载的配置方法8 ~: c4 a* h' _9 h" T
在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 Z' Z3 Q1 m% a# B# ^' e( v) @- A/ v6 t, ]* O; L5 g
- ^; V! v+ G! imodsecurity on;1 F8 y/ g3 M6 B8 r( A
modsecurity_rules_file modsec_includes.conf;6 N2 q5 K5 g& K: s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
c. X% s, p% u
6 `8 J! F0 g% H) `% h1 t
" ?- `& v4 G, o4 |' v$ vim /usr/local/nginx/conf/nginx.conf
$ h8 _* w* f: v9 ~0 Y1 W) o4 H8 S/ [' W1 |6 ?- v2 f8 T0 Y
- L: |1 _% a3 u" W1 Fserver {
9 a- h; w5 `- c2 Y- k- q0 Z P+ I listen 80;
5 s* X5 k' l2 d+ [1 I9 K server_name localhost mike.hi-linux.com;, c6 j5 X4 V; B) m% O" q# P' [
access_log /var/log/nginx/yourdomain.log;: ^. h+ a6 l: l& L" _- F$ x0 \& E9 ]
4 [0 @3 n7 z, C9 ] z& S0 |
# d% S! U. W1 i
location / {
+ t, w+ T% P4 H2 t- P" e+ \4 ^$ Y, B* f
4 B* ^' l" F. L( T& R6 ` modsecurity on;) C. y; c$ J p7 p; a
modsecurity_rules_file modsec_includes.conf;
9 E, N8 |" E2 Z root html;
' d- V" ^/ s6 U$ s5 I' \9 ^- c! a index index.html index.htm;
+ g, P5 t" O% E% s( l( Y5 g* i% `}0 P/ n4 q/ A; }* S
}
3 `% ^% I+ H+ b5 x5 m" G% X验证Nginx配置文件
4 {7 |; E( Q: u a; @& P, N! v' g$ /usr/local/nginx/sbin/nginx -t
& Z3 r0 l' [7 A$ f8 u. C& f0 Ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 E4 c; h4 _& X: ~: |/ b1 Fnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# m9 O m( I: r启动Nginx
4 _+ g% L b9 n) Y$ F5 M$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. C# r; T( A$ ?% T Q9 C8 `+ M, R
; w9 b" V" ~& e% V M+ `$ g/ b/ M
测试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能够战胜更多复杂/未知/混淆的攻击模式。
8 i$ B! {% o, L* F3 O |