ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, d9 `4 E0 B! G5 X h- s2 r, [
: ?5 b/ z1 w& y2 O$ t
3 b/ p% i$ Z8 M) Z) D在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
, J H* x/ c% y: h1 V
% [- l9 o, `6 E
, u6 s3 B$ ?7 a6 ~( I W什么是ModSecurity0 p+ w0 i/ j3 @" e
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
; i8 Q3 e% ?% n' @+ o2 h4 s. T9 r4 C% `& N, q9 R! e/ u h, r0 f% p
" |, M* v' f; @0 j0 t" ~ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) n. B a) m# d7 h
% |: p& X/ a$ X: h, m
. B" o# N; Q* G0 kModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- }+ B6 J6 H4 z2 E/ @: n* u
; G+ w2 |$ i$ X/ G- X# M
5 f, Y/ ` Y- J( ]* z. r
官网: https://www.modsecurity.org/" Y# r' O& B; I$ l, `3 Z, d8 n
j& X3 K$ n1 M1 Y* K* F& g
! s* }9 p9 N0 y" `2 ^7 p2 Z什么是OWASP CRS
* S# D! s2 e. T% iOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: |9 T/ G' j( O, b
% r! \2 g9 S, m
0 T7 k( b4 E l" `ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# @+ a2 k' P+ @& S6 W
/ d# f7 o6 D1 \- y- }% i( H! m* O+ s! U; p0 v" \( @
HTTP Protection(HTTP防御)
3 q4 y; F R# [* U; gHTTP协议和本地定义使用的detectsviolations策略。" g F# X2 j. _+ [3 d- d
T w) q% `8 @1 U+ T5 ?$ s2 O
# [# @6 C! N/ {1 T5 e5 {4 ^/ W$ ^( ~: rReal-time Blacklist Lookups(实时黑名单查询)
9 c$ r( y% x3 S! [6 ?利用第三方IP名单。
7 I- ~& ]/ A+ }9 R) h# l; |+ T* D7 V+ `( x3 ]* }' U! w1 h
0 P( l7 D0 M# A; t5 e3 @HTTP Denial of Service Protections(HTTP的拒绝服务保护)7 I) C" n" O& M/ X) C
防御HTTP的洪水攻击和HTTP Dos攻击。9 m7 \# b& \( H+ k$ K H
3 W, W0 S& a& l9 `, V Z- c
: m. q5 m$ v( h) d# {0 T# c* ]% U9 t
Common Web Attacks Protection(常见的Web攻击防护)
$ P4 B4 K2 |1 n) J) t检测常见的Web应用程序的安全攻击。
2 Z# B, t! e9 V4 @6 R) t$ Q1 w
6 E" O: G% t: X7 U
5 x0 _% X9 O9 M" nAutomation Detection(自动化检测); `! ]) E/ t$ n1 j9 T
检测机器人,爬虫,扫描仪和其他表面恶意活动。' U0 q# C! P" S! Y
Q0 Z& f5 M. I0 [* f! P
9 S' f+ w: `/ q- g- w4 qIntegration with AV Scanning for File Uploads(文件上传防病毒扫描), Q" J$ F' {: g" L+ B: g
检测通过Web应用程序上传的恶意文件。
8 \" m1 A R; G
! q1 ~' t3 N( K3 L+ B) G7 y$ r7 U; p/ L
Tracking Sensitive Data(跟踪敏感数据)
+ e+ Y" a3 i* ^4 H信用卡通道的使用,并阻止泄漏。0 M3 e6 }% S/ i! C& L
2 |0 D2 D" \; e* u7 Z. R4 V+ Y# K+ P0 k+ d0 q! c
Trojan Protection(木马防护)8 t, }/ V$ K" Y* Y; \
检测访问木马。3 ]- Q+ m+ F" m' q% K
1 A `& o! S' h' E4 }2 i
8 w$ p& \5 o& X: f! L+ J4 _# ]
Identification of Application Defects(应用程序缺陷的鉴定)# M. i; z* @! I! I% x- E" ^( Y6 b
检测应用程序的错误配置警报。
9 P9 H. e8 w+ k" p z* l# q# k
$ G! y6 ?# \3 U: h( R1 w
Error Detection and Hiding(错误检测和隐藏)
/ P( W& T1 g* u9 p; a! ?$ F检测伪装服务器发送错误消息。8 \6 w* \6 b9 u3 i
- I5 a! a# N5 ~- x& r7 D
. B' U% V5 i/ F7 ^
安装ModSecurity
$ Z I9 D/ D. j( |# M j软件基础环境准备" `1 S! I" l1 C- ?7 `* y$ c
下载对应软件包# Z" x$ H( |1 i2 H/ _1 G# V) v$ b
$ cd /root7 T( l+ @( @3 I1 h6 M8 @
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'5 A7 X6 |7 ~! S* Z2 s
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
! p& ]! B/ t. W- S( m8 }安装Nginx和ModSecurity依赖包' U) ]" g; J- M% i9 P/ X& @ e
Centos/RHEL6 n6 |6 x6 Q, b
6 c, x, W5 P! j8 o, [" u! g
; L- g/ \4 e5 E5 x
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
6 |- P" o/ I; A/ a @: ?/ t- XUbuntu/Debian4 c# N- x E$ V' O
/ M- y( }: F2 ?" G! M. I
% F5 w" h0 Z4 L8 e, y! d
$ 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
' X1 z8 H( {& r8 ]- [! L编译安装ModSecurity; D& t% V' R, M& w
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
+ {" h( R1 ~2 m* t/ U. u# a0 N( @3 I! R* }# P9 r
; B' U/ P7 Q/ d$ t3 `3 g
方法一:编译为Nginx静态模块 b3 R g" \5 q: a1 y8 D- D% R# I* X
; ~. q( B: X# z E
! p/ \( r+ W' v. x# s" l编译为独立模块(modsecurity-2.9.1)* W O& C" z" Q- H& ^3 D' j
$ tar xzvf modsecurity-2.9.1.tar.gz
/ l+ n& s2 U4 f- J$ cd modsecurity-2.9.1/
; M$ p: p8 ]3 a& Z3 B v1 j$ ./autogen.sh- p( M8 Q* d/ g/ c% ^4 O d3 c
$ ./configure --enable-standalone-module --disable-mlogc O! n/ n, z( B& {6 a" W% G+ l
$ make+ Q: b* w# E7 y+ k- S3 v7 e- a
编译安装Nginx并添加ModSecurity模块
8 h2 M! I1 F) |( G0 x" x+ o$ tar xzvf nginx-1.9.2.tar.gz; U% Q+ P7 I: n" o
$ cd nginx-1.9.28 G: p' s3 w2 C E
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# t) @6 [7 h5 l. e" g- _4 k
$ make && make install
* f) ^. D* E: n2 T' R2 p' w方法二:编译通过ModSecurity-Nginx Connector加载的动态模块1 s) r5 {7 y2 w
# S9 U2 K- }4 a( p* W9 V5 |
1 |- W" g- ]6 a. k6 t2 J- ^编译LibModSecurity(modsecurity-3.0)' f( S7 V; |% H; d
$ cd /root
# x/ _" I% v2 O4 e$ git clone https://github.com/SpiderLabs/ModSecurity
. H3 p7 m6 @2 F- v$ cd ModSecurity7 o/ T, M; z+ @+ W a# @' X0 U
$ git checkout -b v3/master origin/v3/master
. Z4 e, l3 n0 A' @, z7 Q$ sh build.sh$ u( m4 U+ E0 V/ ]) w, u$ O. @; t
$ git submodule init" H/ y+ c$ `6 L+ b# O$ i0 S
$ git submodule update3 D n& _% |! a E( g- q7 K
$ ./configure
+ K/ s; G# v9 ?- S! k7 o5 S: a4 ^4 Y$ make
& S! u% B6 u4 @1 [) J! D$ make install r, f# {5 g9 \
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. d3 P, ~ J/ E- A. o5 R* g
. \$ |# f6 X: _/ T
: {7 T+ F5 K8 q8 v \
$ ls /usr/local/modsecurity/lib, ~7 i Z. E0 E2 d, @
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 [4 a& m6 N% Y" v5 r; c编译安装Nginx并添加ModSecurity-Nginx Connector模块7 K# B h% R& w! Y' N
使用ModSecurity-Nginx模块来连接LibModSecurity
/ B9 q; \: e @6 G% A
/ _- q* I& W f* ~& b
! C1 j( u7 ^$ e3 T6 ]- ]$ cd /root$ t' X! ]0 U& E, n2 s
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: g& j) U( L4 v$ |$ tar xzvf nginx-1.9.2.tar.gz
0 H. \) X/ M: J$ cd nginx-1.9.2: v# z. Z9 F5 k
$ ./configure --add-module=/root/modsecurity-nginx1 o' @0 I8 Z) z' L R/ W
$ make
& z' u1 q5 q. J* Q$ make && make install% k7 M) s1 z0 J4 l5 p) W
添加OWASP规则) X, H7 @3 S% {7 C% m0 P( f( |
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 d# c2 `. n$ k0 p5 s: e8 y- b) d) P$ W
# R! I5 ~- }! g% F) f8 V下载OWASP规则并生成配置文件1 a! H1 N' @( D; h5 l
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 f0 s V: \) k- ~1 L/ L
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/( V! d" D5 D% C; E& B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
$ E1 G$ _6 K6 e3 m! H8 L$ cp crs-setup.conf.example crs-setup.conf, a8 v) u6 Z* O8 U; ?3 ?1 j
配置OWASP规则3 I5 K8 @) H K! c( b- K
编辑crs-setup.conf文件. |1 w) l) I. H
) u7 Y- E3 H/ M1 C" j
1 F/ W2 k0 N. j1 o" |1 Q$ L
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* T9 ~- Z3 B- G7 j$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: H3 q" v) W0 S% V% s
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. {1 i! e5 B4 X2 Q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ v6 }, _ L2 f; C* i默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
4 p) Y, b# z% w7 u- n# a& N3 g4 {' t
- |+ Y$ [5 P% W: `
启用ModSecurity模块和CRS规则
9 O' t9 w& | j. F% V复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( P% ^# i w$ D8 y8 i% g
! F; ~ W, V, Q T7 l
& n# Z+ A" ^) o( \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
8 T0 ]' L+ @% i- Y4 c: |7 t; y- g+ C! K
, i1 F* M% U3 [; r; `( F$ cd /root/modsecurity-2.9.1/
. d$ W* m# X, Z1 |3 C# s$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
& _: i6 P; ]( f$ f$ cp unicode.mapping /usr/local/nginx/conf/
0 m9 A9 `* p9 m7 J( f* a a, Y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
) Y }$ R# A0 d; t- I# q) X! G3 t& K7 `) Y6 n& u! r
( [1 V1 ^' u. N# i, b z8 `$ vim /usr/local/nginx/conf/modsecurity.conf4 `* a( m1 S: |1 }
SecRuleEngine On! P1 r: }8 u* `/ K
ModSecurity中几个常用配置说明:. ~% r* N* J4 z* h9 A7 I+ K
~1 |+ c0 H1 O* Z0 w+ J
0 s \0 C" R) l5 f# _0 |" c& ^1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 S, O' k( }; b _& @) t
H* i/ A" l7 l) w8 L1 l7 I, X3 k8 [. _
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 o- _3 ?; D- q# q3 r) @
8 G8 f* ?( f0 t
1 T" Q" D3 C6 l4 s8 E$ R3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% }( _$ b$ q6 O, {/ v/ s2 [3 d# U o2 A4 _/ A2 l: n9 N' X
/ p6 @& }1 Y8 O2 ?
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# J- K; g7 j( }2 H6 c K( p, w: j% c& S0 ?8 U
2 m, P! T* _' [7 ]* g在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
8 N! o6 _+ C' z! H
0 C) M, A0 L0 a. I& [. T3 L' T$ q% I) I
3.x版本CRS7 r% j, M8 W& @7 T9 h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 r2 f6 k' C7 K3 k0 ^1 D9 i
# 生成例外排除请求的配置文件0 w; E: ?" w" d9 X" j
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. T) P) T4 R+ x$ a1 e7 m8 Y
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
: o2 ~; Z' ]& L6 S) y$ cp rules/*.data /usr/local/nginx/conf# c8 k5 T+ ^. ^! @& b: P
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! {: h6 ~; x: M3 u8 F3 {5 Z A. f) {0 B% y
: r e. }. W* e: r+ F& h# [$ vim /usr/local/nginx/conf/modsec_includes.conf
% _* Z* e4 f3 d8 o( R8 |, a" j! u+ Q: A" x
[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
- t p7 a* `/ a- j; z0 g H% j( ]
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 |: P7 y* M! s4 W) y" R/ g
* B: A# a% Q; W; L5 T7 ~, `$ Q
: V- q# ]( l/ y3 z配置Nginx支持Modsecurity0 L% }4 w8 x# b; Y, O4 l8 b
启用Modsecurity
/ h% x) G& y. M( m" R! h8 {! Z使用静态模块加载的配置方法9 p# {0 D! C3 Y2 Z' k, w
在需要启用Modsecurity的主机的location下面加入下面两行即可:
4 L6 V8 t, x4 i+ a* f; J) X/ ?+ e2 g; x a# ?$ A5 T
8 b& M( T6 R- c# c/ i( A! wModSecurityEnabled on;
. n! t& U" v% C( W. O- g5 G) I5 |1 dModSecurityConfig modsec_includes.conf; ]$ l8 @5 P# n
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ U) l3 Z# d7 [0 r' e
( H8 C5 {3 T8 m7 Q% N& K" b
9 I& `$ S. O: t6 J( e0 d# \% u$ vim /usr/local/nginx/conf/nginx.conf
# x* J& d5 x% x$ K
* w) p, q9 M7 R, J1 e; P h* X6 O* b }" U) H: k) O% n" q' c
server {
2 _2 o. @9 T: i; W: l5 i+ y. p listen 80;
9 a6 m0 {7 M, \ server_name example.com;+ _ `6 N* B; m: c; `/ ]7 C
8 Q, L& y! D- x# H# v8 g- k8 y: l! x! _5 W
location / {
7 ^" o' X# o9 @1 { ModSecurityEnabled on;
! L) N! K2 o$ m0 B r7 e0 ]8 P* I ModSecurityConfig modsec_includes.conf;
- @" T! _0 m2 w6 J1 ?0 B9 L root html;0 D ]8 n, I0 S, y/ ]2 }
index index.html index.htm;, p3 r c6 A* Y7 y& d1 x
}' {$ B: G+ b+ q
}
+ k7 `! Z+ D" b+ c' O1 r. U8 p# s; B使用动态模块加载的配置方法
% @, y% I' P0 _; ~4 k) L/ W在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ O* e e0 o- ?- ?: z: A3 I6 O* N! x1 P& b# H/ r7 Z" r: W
$ R$ E% }+ n. q5 H. `
modsecurity on;2 R+ g/ J1 q- G! q
modsecurity_rules_file modsec_includes.conf;
, A2 ~& _) X9 |/ t9 M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' k6 I* q; E. N9 j
2 q) F& o0 t' l: u$ I0 R0 S" ]; \. g1 ~* }: {
$ vim /usr/local/nginx/conf/nginx.conf% u) `' O' b4 n) j5 n2 W2 {
* Y* g2 b! l" E! A3 m
! G/ p$ l& y: S8 [server {
5 L# c- @. c) f& W* F3 w) g3 E listen 80;6 X: {* y4 E* ]% E9 H1 b* t- R
server_name localhost mike.hi-linux.com;3 V" k1 w8 i' p/ k1 H" ^9 _ h
access_log /var/log/nginx/yourdomain.log;7 D! C! O1 n2 x" \* A0 L
' j! R, e7 }0 R' L2 ~) z( V3 R
: b$ |+ W% ?8 e; G: o: U location / {% a5 Q1 F6 E8 N' |, U7 }
) V; o& L! c" ^, |* @- g% i7 H3 _' f6 v
modsecurity on;
- R. o, y% |9 z4 C0 Z. C3 f+ G modsecurity_rules_file modsec_includes.conf;: O" O4 F" V' r+ _) I1 w7 a* l3 [
root html;
/ M Z0 T" ~+ v* T. b" [ index index.html index.htm;$ l5 K$ S- W; O9 V7 x
}2 ?: H7 |% m6 @2 ^6 ^9 t- g- L# }
}# x ?! y- a8 q) O) Q- `
验证Nginx配置文件
% ~/ g2 i0 ~. z3 R+ ~$ /usr/local/nginx/sbin/nginx -t
5 r# k! i* m# @- \: ~2 |nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok6 O* C9 k* `9 V% M& V
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful M# f4 c. k- r2 i1 W# g
启动Nginx
- ^. f: f7 i0 X9 y w, O) j$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
+ W: p7 s3 P! J2 w( R* E* ?
, r2 V' E k/ `; V测试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能够战胜更多复杂/未知/混淆的攻击模式。 " m. d" [ a$ l
|