ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。: H) O) P+ B3 ?
) O3 d y0 y5 W1 d/ ~6 u; ]9 x
3 \! @( \5 D5 a3 |在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。8 N7 q' H4 X0 Q
3 ]6 p0 {7 k, g; h! R+ z
. Z; z z! s$ O1 a
什么是ModSecurity6 L) D2 \3 M! W7 N
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 k8 Y7 W! _8 _) `$ E+ A ^
) E! M& e/ l% }6 r
" ], i5 b8 Q( o6 T; b3 vModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
9 J1 k* G2 S4 A5 }& R2 n/ x! S% {: \' P1 |
. C4 Z2 z4 |/ m% @2 ]4 zModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% ~: Y! ~4 n6 z. w+ ]
; a: n4 q* d" i2 I% A
% a; V& _, u& e' H3 p# J; F官网: https://www.modsecurity.org/
0 j+ R6 r9 M% X6 k$ E7 O5 S1 z% ]6 o( z4 t' \* ]4 V/ H
' @8 o. _: s+ j8 x# D8 \) s什么是OWASP CRS2 D5 v7 y: A i7 c- o8 s; L
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
) a! w/ Z0 w( t
5 v$ \9 e) h1 u, H# d* x# J8 ]
; R: J* B- J- I6 C5 x% j- q$ j+ AModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
% P/ j8 d5 g* M# H' v5 O& k1 G: K6 P6 {% X& z
1 c; c& t3 I5 C9 W ~6 A0 M
HTTP Protection(HTTP防御)( E+ x; o& ^1 }* O& v
HTTP协议和本地定义使用的detectsviolations策略。
+ I+ r8 {' a& ^' Z' `2 ^+ J! t$ ^8 w* W- W5 o+ T( x) |
" e k4 b# r/ S0 X6 y; x+ R. j
Real-time Blacklist Lookups(实时黑名单查询)
( y1 ]+ D2 K$ V9 A* [& ?, |利用第三方IP名单。' W/ V$ ^4 ~; ?+ b) j A
' @. B! u" g: k8 L
3 \( g! n6 P1 C+ T
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 A3 ` i! ?" O0 |防御HTTP的洪水攻击和HTTP Dos攻击。4 v4 E3 ^7 |" h. H; v4 r7 I
7 H. E' {5 d4 m2 Y; [
! b( e& P3 g# G$ _Common Web Attacks Protection(常见的Web攻击防护)( w: Y. a8 M1 v5 g7 e
检测常见的Web应用程序的安全攻击。
6 d, p3 s2 ^: `5 i) T$ Y: N) G5 Q$ |* {$ H4 B
8 C: j4 T, p4 F3 `7 @Automation Detection(自动化检测)
7 ?; b( u' L6 Q3 `. R: u检测机器人,爬虫,扫描仪和其他表面恶意活动。/ h/ f, [0 H) B1 {( u4 F" ]& A) h
6 T' q P; X9 J6 b- h9 V+ N3 q9 Q5 o/ a
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
+ a4 V& h- i+ N+ D4 ^" V- Z6 ^检测通过Web应用程序上传的恶意文件。7 M" D L6 V3 r
# K, K6 P" H( _
* O2 Y5 Y* v' R# O9 z: D# C4 fTracking Sensitive Data(跟踪敏感数据)
, H0 {! ^" h9 l+ W' Z信用卡通道的使用,并阻止泄漏。& p$ @: T& [4 o3 c/ S
% b' A' i- G' O
: A6 i5 S/ o0 h* V( e
Trojan Protection(木马防护)6 c- x* x6 o$ `- u
检测访问木马。! z4 E& v9 ?' p+ g
+ J1 \" J. [1 B4 ]& `5 }
7 E; C J' } X1 k) a) k- g
Identification of Application Defects(应用程序缺陷的鉴定)
6 C! }" z c$ x5 I- X/ ?3 g检测应用程序的错误配置警报。
9 G+ {1 _- ~, e% m, I
2 @, j: ~2 e: a; p- m, w ~% o4 }
Error Detection and Hiding(错误检测和隐藏)% F) d0 f' ]4 n
检测伪装服务器发送错误消息。- a2 K$ v6 H% f* p4 B8 G! g
J3 D2 s k! V8 T* _ U. r: F+ z8 P' F! ?$ Y( m7 B
安装ModSecurity
& s) [9 i" j$ g% ~软件基础环境准备
" Y0 U6 \& m+ S) }& q下载对应软件包
/ U0 l1 g9 }& b3 D$ cd /root
- K3 v& _7 Z( m$ D! ~- p7 t$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
0 ]& p. l. s Q$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz& q0 x6 A J7 N. u/ g
安装Nginx和ModSecurity依赖包
. B5 p, }7 F+ t/ Z: G/ V+ R9 DCentos/RHEL
0 z* v! Y! a& v) Z) A9 _: N% _
, S0 s0 i9 n/ c& B8 Y" Y; F3 P; x u2 f; `- l9 D
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel- i0 ?0 G* S- c: x D4 n, Q4 q3 N
Ubuntu/Debian
- w3 N n' o1 t1 R8 _9 m. ]
e3 a$ ^/ a+ z+ \- y3 T9 N) h; u/ _" t W' O
$ 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! U2 t% h; C* o0 b+ O) {, `
编译安装ModSecurity4 O# F3 \6 s1 J, \6 J
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 F7 O) @& Q( t6 T# N
: b' D- |/ O7 [- r, M! X2 R* T4 K% O3 D+ y {
方法一:编译为Nginx静态模块
9 w* A O4 S1 ]' d) E& f; c: Q; j! o% A$ \' p3 V
2 D+ x3 y8 c# p1 r编译为独立模块(modsecurity-2.9.1)
, h: Y$ L2 ]% I: z. A$ tar xzvf modsecurity-2.9.1.tar.gz U$ W) E8 ]; f. @7 t5 Z& c* Q1 q
$ cd modsecurity-2.9.1/- q/ A# s- p" y/ q: _8 A6 n+ s |
$ ./autogen.sh
1 A- w- R6 w) a5 E+ O$ z$ ./configure --enable-standalone-module --disable-mlogc. d& h3 a% o# Q2 @/ I' z9 {+ E
$ make) w \; Z m( ?. L( l- g; M$ C {6 B
编译安装Nginx并添加ModSecurity模块! A& N# Z) m) P0 G
$ tar xzvf nginx-1.9.2.tar.gz$ x q# ]8 O2 [8 }$ Q
$ cd nginx-1.9.23 y- M3 r0 |' B1 v: k) C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 b7 Y( K# `7 k, ?( |
$ make && make install5 w/ ~2 C# H- F+ n
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 n, ?: |" Y' M' j0 t- E9 I1 Q
- O# V! i- B* L# h {: e
编译LibModSecurity(modsecurity-3.0)0 s z: m% Y$ \7 `) V e% l3 ~
$ cd /root
$ e% ~# I: B2 e( U" ~% s, o$ git clone https://github.com/SpiderLabs/ModSecurity# G7 d8 |5 W- A: V
$ cd ModSecurity
9 g V3 f" @; M- k6 k8 D$ git checkout -b v3/master origin/v3/master$ s/ Z; @& N# I8 O
$ sh build.sh
) B! Z5 l: t! M' c$ A7 r/ ^$ git submodule init$ O% n! K9 P2 i$ p& u% m
$ git submodule update
' n7 V' P9 O. g# F8 h0 B$ ./configure
+ t- S, d6 ]0 r8 K3 n G9 c$ make
3 J# e& [) g5 E# q# n$ make install
0 F/ a% j! H* t C& @# _0 wLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
, n q( _: A, I, j. n4 F) s9 Z' S7 H& B, E" W. S& u
. p) w; n) J9 a' N5 i$ ls /usr/local/modsecurity/lib" {' \; U( m1 |4 |& l
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
" V. ]9 @, Z! y' s8 f编译安装Nginx并添加ModSecurity-Nginx Connector模块
, D4 b7 J# h n- G" G使用ModSecurity-Nginx模块来连接LibModSecurity# L) X8 E& Q6 @) q3 n, e2 [
& n8 D0 p7 c; z' L2 l0 S0 z9 ?7 P; {- C, |5 P# f0 \8 [" h
$ cd /root' N7 W/ j; ^3 C. S6 ] I# g! n
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, I0 e1 a8 P4 N4 ~1 g. {9 j/ r$ tar xzvf nginx-1.9.2.tar.gz$ ?! v4 v8 r8 G7 ?$ v% K
$ cd nginx-1.9.2
" _+ g7 {$ ?9 E! {- z$ ./configure --add-module=/root/modsecurity-nginx
8 X% u; r; l% _4 h" [, ]+ |$ make
7 J- B/ f$ m! P3 c% {* \$ make && make install1 t9 m% A1 H/ s# ?; H
添加OWASP规则
% N% m6 F" q- H. R* bModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 t, Y% w' e( t
$ N! V$ q) f) T$ O' }* G% j; b7 O( | q
下载OWASP规则并生成配置文件
; s! J+ Z0 i [# m$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, d2 `9 L' @( m q* W; M* ?
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
# L$ T: _" o- v$ o$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) g# R# B! H) I+ ~* n
$ cp crs-setup.conf.example crs-setup.conf
/ k. p, N; R' I8 ]/ q配置OWASP规则# b1 E& @6 Y# R8 [% W& H& K9 ]. k
编辑crs-setup.conf文件3 u2 R8 P4 @; v( W# B1 s5 M8 v4 T. _
. ]; ^# d- |- k. Z' ]
; |. u7 i8 z7 c4 ]5 f6 h+ b$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf) q- J# L+ L' b5 e7 E. D
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
8 b& ~7 ?" ^! y1 b. s$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: o, o2 K: T0 ~ A5 p9 ~$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf; `0 @, W! b* F' T- `" h2 L
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。- q+ O' V x0 |; p& @
' ]4 S5 j i. b/ V
7 X# z# w5 q- w3 P- ~启用ModSecurity模块和CRS规则5 ~& z: W: n' ]
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& b9 @0 P* {* G: Y$ m& G
2 D: f9 ]. l; ^6 d( d( h
7 G b" i+ f* ]modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 b; X" M4 i8 V
) _: V! F( W2 J/ Q) T
9 B7 V8 h3 V5 v$ cd /root/modsecurity-2.9.1/6 s1 F$ [) J, Q! v) h
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
4 i. J: ?$ J, D& T0 L% T8 ~5 s$ cp unicode.mapping /usr/local/nginx/conf/5 I2 i0 ?3 Q6 q8 D L/ w1 X
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; l6 U W+ d7 d& y. X- N
, j6 V5 e, s9 H; B5 O; L/ l) A. L7 f/ [% b. e+ Y3 a7 ^
$ vim /usr/local/nginx/conf/modsecurity.conf5 \$ m' c9 u1 F: b4 |; J [9 n1 k! I
SecRuleEngine On
}, a& U: h u+ kModSecurity中几个常用配置说明:1 \/ I* Y5 n9 M" F
" D, T3 P' V( F; w# J7 C) H9 ?! ^
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 A/ S( \8 \7 K
* P4 d$ ~" L# \% S# t( r7 @
1 I9 n& C5 Z% B/ S2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
6 Q, e3 a' [8 C; c- p1 |1 U! s% {2 x' D4 F4 x& \
/ s- y9 g% {: l- U& M! C# Q3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。3 ^- F# G9 M, D$ J. `
4 D; a6 |" w! K" R" l6 i/ v7 _' a
/ @% |6 R" ]7 E# Q
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。1 o& \7 T4 z3 d" P& X; c4 @
( \2 n8 T7 h& M+ m r( p* ?) l- E# z; d0 [/ I3 v
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。; `7 `7 E9 ]" y
! J5 }6 L% J" N! @: y
, D9 V! |' L5 l7 e7 ^; p O
3.x版本CRS
* v! m: X+ i0 d% B( h0 o- c/ O$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 B3 r8 T X6 _
# 生成例外排除请求的配置文件
' }' x/ e; N* v# T4 D+ [$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. }( s/ n$ c# u- \, F$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf! n* h9 {9 l8 b, ]5 z& X
$ cp rules/*.data /usr/local/nginx/conf8 U% I+ ]) N: l0 }; K2 x
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。" C: @) O4 R3 m0 ^" C1 W
1 n- w5 g: ~( i4 N C; _3 f
K* {8 u; R# A; V3 f$ vim /usr/local/nginx/conf/modsec_includes.conf
) E: s5 m- f$ a6 o' K% }6 b- H8 n3 n# ]; i# X$ 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 5 A( q2 t! a3 n0 a" r5 \
' [. |; Y5 a- x9 q/ J) `注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 M& U# W+ ^! d0 X
$ o% Y" Z4 k+ X) n
$ M: d7 l4 b6 v. {; R. m0 K* D配置Nginx支持Modsecurity
?. C/ T/ ~$ e* G4 z6 r启用Modsecurity0 b7 l( x4 B7 ?- q' h+ `
使用静态模块加载的配置方法
5 \ Y; w& x2 T在需要启用Modsecurity的主机的location下面加入下面两行即可:
* _# A/ }9 C7 t5 _: C+ X# y! D( ~2 ~7 M5 b+ f
1 \& }) {, c# `: l4 _ModSecurityEnabled on;
& u: W1 o9 g1 r" g' |$ z4 qModSecurityConfig modsec_includes.conf;
; q b5 k5 B- Q( K. i* b修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
5 ~# f* N7 G: k" y/ Y9 N4 ^ [" B+ G
d$ w) I( E# @$ L- `/ C l$ vim /usr/local/nginx/conf/nginx.conf
4 [& z3 P+ |! _' l
0 ?# V4 u! z# {( {) ]* T
1 G- Y" E' Z+ j3 A* @# e, {& bserver {
2 O0 a6 D: |8 c0 ^8 R! x: _: X listen 80;
, k3 \- O! Y. P! ?& w% x2 { server_name example.com;7 P1 X- S$ f; M8 b
5 q; l1 b2 ^) @% h" }# z% e) G5 s n
location / {: g% C! m' a+ J' R/ b2 `
ModSecurityEnabled on;; \3 m4 T+ A( R6 w: e5 d
ModSecurityConfig modsec_includes.conf;# v- T5 c) d! y( p# a0 O2 ~7 T
root html;
- K+ e4 H/ T+ r) l% _& U index index.html index.htm;; r$ I. W( I. T/ s% r: \. X% M9 J+ h
}
% M* P) p" v4 C7 ^( v c+ u, j}) `2 D# o8 T) B4 G
使用动态模块加载的配置方法8 z. W# p r8 H1 a$ r* Y# }3 T
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 g4 p7 s; R& ]" K6 b
, @" Q3 P- W6 g. q2 Y, a0 }5 p# ~& a! H
- I1 Z' k! t7 T( C+ g1 hmodsecurity on;
. a5 j9 M* c" p* n$ Bmodsecurity_rules_file modsec_includes.conf;" t- g6 }9 R2 S3 E- i: R+ d* e
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。 m+ B6 v2 s0 q0 } w
) |0 E* M R; m: }) q. W/ }* o* m/ q) e( S# Y2 `
$ vim /usr/local/nginx/conf/nginx.conf
) z& |, i6 ]3 B% Y5 |, H$ R7 h+ ?$ e# ]2 T% u
; p8 n& u ^+ j
server {- y" F; D2 I; U9 R
listen 80;. U& {, Z2 f: _! @2 R8 }
server_name localhost mike.hi-linux.com;
& _' J5 J* A* L' v2 | \ access_log /var/log/nginx/yourdomain.log;
# ]6 h# B4 f6 {; i
# U, ?# Y& d+ a% m# c: a
& o* E3 M' n4 K, c& W8 n, k4 s location / {
! m) p0 m5 C( |0 \6 V1 y% h( @$ f9 g
9 [. H' c M& U4 o. w9 `
& Y1 H$ |- Y: ^: {# y5 {$ [ modsecurity on;
/ ?. `( J8 j" c5 x2 K, O modsecurity_rules_file modsec_includes.conf;% F% e0 V9 R( {' w$ u8 q0 W! @$ `
root html;
g% Y7 w+ _. ^" E: q8 g index index.html index.htm;% v* A7 r% j0 X* _! t
}! v6 Y; ]' B0 s, @) O" F
}4 U$ @1 A1 U; J5 u* e' Z* W8 F- M, x1 [
验证Nginx配置文件! M' g: Y# f( p3 j2 t
$ /usr/local/nginx/sbin/nginx -t! j3 \; J. Y# A& z1 T; H% z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" b6 b9 b- G) c; a0 S2 A. A1 G; {1 `9 d
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 {% k7 Z8 m1 E5 Z
启动Nginx& [7 U* Z& k! ` h$ w' ^* ~) r
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- o* ~0 u8 p+ G+ s; N6 C
0 J& d' A: T3 C测试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能够战胜更多复杂/未知/混淆的攻击模式。 $ r/ X, ?, [! _: `" U
|