ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。% e8 U. Y- \; A @4 X5 @: y4 Y
: ^. i7 ~4 H- i* u% d- H0 U& O1 V& M7 I2 l
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ N# l/ J! m) f7 i3 W2 w
2 o( X# g$ M6 a+ }! y8 K# x( @
% t! N0 R% ~. C' O/ {( H% A5 X什么是ModSecurity
9 j: ?; S- }- R! ]6 O* ZModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 [! q" y6 V& z8 U5 @1 I3 j7 Y8 n+ `& w3 B6 J5 W) m/ s
& q A( t7 L! z$ f6 y4 XModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# T/ B9 X" B8 E5 M7 R
$ m0 [# E. M) Q6 t, I& ^5 S
5 N. G, @; K+ G8 r% q7 EModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
- |! d, Y+ @3 r Q( ^/ P
" E8 d$ X3 L1 E$ b1 P% y7 T7 c: l5 s& G! P( y
官网: https://www.modsecurity.org/
( H/ h0 b9 d' g" v% e) Y$ ^ a- ?: u1 L0 V- q* k, J1 A0 `
) g% d8 y+ c7 q0 m/ O- G: }- W8 y4 m
什么是OWASP CRS( h1 }# Z7 l$ ~
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。" e* {/ F6 @( A/ l: J; c
! @* `- o6 H1 D/ |" w$ ]# Q( \& {! L. z) c8 u9 [" Y c& J$ T, y
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 T& B1 ?& X ]9 H
6 ]. b P& t( C$ A0 ?
5 x. v n, x" k2 o. s$ t jHTTP Protection(HTTP防御)6 D |& \6 U+ N3 t" E
HTTP协议和本地定义使用的detectsviolations策略。
+ @1 J- v! ^# m- J' f
: s0 P- B* l7 U% w6 ~! {
3 h% @2 V( f; y" L# c0 jReal-time Blacklist Lookups(实时黑名单查询)7 `( D P4 z% [0 z R( `6 c
利用第三方IP名单。& [* @1 i M4 R
8 L8 x1 U8 e1 u# N- s) A4 V- \# A
HTTP Denial of Service Protections(HTTP的拒绝服务保护)6 C) g) j( r8 f n7 d4 R) g* E; G; o
防御HTTP的洪水攻击和HTTP Dos攻击。
6 a' d9 |& W0 ]0 O8 L2 M, Y* D- d
0 S; K: n7 a/ b7 E* ?7 t9 W, @* K* N4 m
Common Web Attacks Protection(常见的Web攻击防护)( A Y/ p( T) d; A& R8 G! d
检测常见的Web应用程序的安全攻击。7 s, ~8 p, W# j1 i* i L
L; x3 C+ B/ ~+ K
: I \5 R" ]2 V, b5 f8 j! @. w* @Automation Detection(自动化检测)# I1 D) c9 w# A9 g
检测机器人,爬虫,扫描仪和其他表面恶意活动。! `( b4 C; P9 ` Z6 F }
. D, W+ c# b+ Y, h4 y6 G( g7 t
( P2 y9 S: }/ b+ S# t- w7 t. CIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
6 S z; G" S! @检测通过Web应用程序上传的恶意文件。. o( }) H: E, d7 |9 p4 O
: M: r7 x" l, O: s: D0 x' e7 f, m% ~6 T& B! G
Tracking Sensitive Data(跟踪敏感数据)
/ O( _: X. a" G4 M2 v信用卡通道的使用,并阻止泄漏。1 M8 \; z" z4 H7 V! g; n+ ^% O
: i; b N+ e+ ~3 V
: f* Q y+ x: u7 K0 T
Trojan Protection(木马防护)# B8 `2 l# m. \4 b
检测访问木马。
2 i5 }( l9 I# `1 ?* x" B; B/ `& i Z! k0 v" c4 b/ [
4 K, C! W4 c# h8 y$ M3 c
Identification of Application Defects(应用程序缺陷的鉴定)
2 ~! Q( x4 k j检测应用程序的错误配置警报。
. E' `. t# ~! \. @- b* q: X* ^ Q
- e3 b+ S6 \1 B6 W# g+ M# S# g* i) t' w4 d6 t
Error Detection and Hiding(错误检测和隐藏), o* K) v2 X7 E" n
检测伪装服务器发送错误消息。
; ~ _0 G. p* ]1 k# { d
$ {6 E$ o# s# ?: b1 ~! t3 p
/ E4 p }) I$ J) j5 M安装ModSecurity( k- {5 s6 h, k2 T- n% x k
软件基础环境准备
: v; H2 \4 | I) g8 I下载对应软件包, O- z& `7 v6 b" {9 ]( p
$ cd /root
' E4 s) |( A9 L3 z9 Y$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
0 `: N8 l, { u, S% ^3 U$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 o' [ B1 V/ d2 W6 U1 X9 o安装Nginx和ModSecurity依赖包1 k2 }0 Z& V! y% L& T
Centos/RHEL$ z5 ~7 d% r/ q. S
# t5 f+ A. Q0 {2 `. U V8 H, O/ w( R' ?
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# e' T A+ x! H0 g0 `" @$ zUbuntu/Debian
9 K5 {$ X( y% a3 R& W6 B m/ O T. V4 d7 B# T6 I8 A* `
! N. B2 p6 k/ ^! c$ 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
5 n8 a, W: D* F- }& A/ {' ~编译安装ModSecurity. \3 F. I$ _! j; n" H
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
$ p( R- Z$ [7 G* s* M* R1 H3 g& J3 Z% f' T
3 d. J m2 t1 f+ _0 ]方法一:编译为Nginx静态模块 S- `& y2 ~& Y6 O) i
( }3 C- {" j6 w! t2 T) A1 i- _2 Z0 F8 `
编译为独立模块(modsecurity-2.9.1)
9 s( d' V# Y v$ tar xzvf modsecurity-2.9.1.tar.gz g! N8 W6 T- ]3 z, g
$ cd modsecurity-2.9.1/
$ Z( U9 o) R$ m* W P# i3 s$ ./autogen.sh4 m# i. x" N9 o0 y' l
$ ./configure --enable-standalone-module --disable-mlogc
/ G. d3 {" ?; n9 v$ _3 W- r- K; R: X! @$ make: K- ~; ~! {4 S. k
编译安装Nginx并添加ModSecurity模块 ~. D) z* ? p
$ tar xzvf nginx-1.9.2.tar.gz W3 ~0 N6 s6 S4 g& j
$ cd nginx-1.9.2
2 Q! ~$ J/ J' ]" J) z: L6 b$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* @! N* O4 Q# ~
$ make && make install9 F- _ t, ~8 P- `8 F0 d0 A
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 g$ {' ^% P) d
+ e9 X9 g& c# E9 p5 k0 A: d8 y1 K- D. E9 C$ x8 `. ^# q
编译LibModSecurity(modsecurity-3.0)
9 _0 ]+ O Q B, j$ D$ cd /root
3 P% M+ n9 |7 `: P" l$ git clone https://github.com/SpiderLabs/ModSecurity) i7 I T' v. L
$ cd ModSecurity( \/ o4 ]% e+ R6 h4 q
$ git checkout -b v3/master origin/v3/master
" G) z4 j1 ~/ C+ K8 H$ sh build.sh
7 h9 C9 N' v% V# O0 p R$ git submodule init
L8 z& ]1 l4 V# C$ git submodule update
: J- P; r: V, S1 S$ ./configure6 N8 K& m6 R! o d5 }: M# e
$ make( C9 m1 R/ ?! s) V
$ make install
. \7 _9 R4 Y- [$ O+ O2 H# ^7 I! m; ILibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( @$ q7 H: |8 ^& g; o
# i- R" @0 f. w$ t$ j+ [- R- s$ c
$ ls /usr/local/modsecurity/lib; P% \0 s3 D4 I7 E
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
& b3 P2 g' z. g; `# y) l编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 s' j: ?1 u( T" B2 R' ?; p. l5 e使用ModSecurity-Nginx模块来连接LibModSecurity
8 j& k4 B3 J! Q! g$ a
2 u2 J0 G# z. R$ w6 C- z3 t$ q0 g( @7 y; h; Z( @- i- ^# c
$ cd /root
3 y9 c1 E0 o+ @$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ Y2 e0 M' l# ~; d# o$ tar xzvf nginx-1.9.2.tar.gz
, T" |; t) q) K& z* n$ c* V$ cd nginx-1.9.20 U4 C( A( E2 E( A; X$ ~
$ ./configure --add-module=/root/modsecurity-nginx# b# G4 s; x# [# U: j% k4 Y
$ make
3 E7 M! u6 s# ~ q2 L- E$ make && make install
' ]" Q: Z& y- s: I% l% ?添加OWASP规则4 T& s, p+ |5 H( @' w, Z1 h4 K% v
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。- b1 B0 S! K) a8 G& g' f, U
' i _% i6 o: }3 H- p3 d* i+ r# c# Z [* b
下载OWASP规则并生成配置文件% P$ _: i! \( V$ g U% j
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git7 f. _( J% z" i: U
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
% _% c. ~* b9 |% D: ]6 R$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 ]. @4 O1 I* J' z& N# k
$ cp crs-setup.conf.example crs-setup.conf
+ i% K& }/ z/ _, x0 z配置OWASP规则& }) o) }( j# \/ L
编辑crs-setup.conf文件' x* `0 B8 E v Y% l
2 H! ~3 C+ w9 g- s
& A8 o! T7 @ Q8 |$ w# G/ N7 j7 {3 l$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! w4 _2 U1 V: M6 E* ~8 S; t: y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf7 ?, {8 V7 r: s [/ |! Z( ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
! X" }1 z: U) |6 @$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
; }- j) e+ B4 J% p6 o- B* O默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 A& \4 E9 V# [8 T$ E
7 M# [( N1 p/ A- u
0 Q6 K2 S! f- T) i7 f0 H
启用ModSecurity模块和CRS规则
- q# Z) |5 B0 s# V复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ I: Y" F: `4 _, x% x7 R3 }( s1 H" Y1 g
' Z6 [9 M" N2 c$ Rmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ y/ K M. ?! ~- C
( C( ]; o; i6 p& O, T
+ a2 ]: C% \. g1 R- [$ cd /root/modsecurity-2.9.1/
6 A: N7 @% ~3 b( C9 p$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 9 I8 O9 K4 G, z% H# w
$ cp unicode.mapping /usr/local/nginx/conf/ ?" W. p5 j T
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
0 G) O0 N$ V/ l/ ]* k" F \+ X0 K4 t: Q
8 {8 [" i. B: q/ M0 B; \$ i
$ vim /usr/local/nginx/conf/modsecurity.conf
1 ?' S/ v ]: B7 r* y4 I2 t3 [: ISecRuleEngine On8 c6 {- D% _! Q3 o* C
ModSecurity中几个常用配置说明:3 Y7 r" |0 G0 ^5 R9 J5 b, s
; w( u' P2 [( M# \' K
6 a% }: ~2 W9 H, Q! @; u( ?. O: G( x1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
9 U3 T- c, U+ ?4 M' O5 B. A" Y* B
4 d4 }# D. H9 L1 ^$ V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。- T) |# g4 h3 x3 E3 L$ M- ^: D
. }4 W- b% D! G0 \! P& i3 N( {* k! {+ t
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 l6 ^$ {/ A6 ^0 o9 G
5 D9 a; h7 N( W. k5 e
* b9 t" s' n7 ]# k4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
8 k, u6 E# L+ Y. t; {: a
7 r( R9 e1 a9 ]" S1 ]
( a) t! r/ U0 x$ O+ d- h在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。* U' A2 V3 |% ^2 Z6 ^5 _. J
[3 p% J, B8 Z h/ g( G% ^
9 Z9 Y, X& q9 N% `* u3.x版本CRS2 K0 B8 N2 j/ W8 L1 m' q$ h) L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. O* M- X* X9 J {" l# 生成例外排除请求的配置文件
; P8 k$ R/ o2 O4 e2 Z3 ~, B8 [$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf: b U" [. I) c: t
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
- u2 k9 X8 Q: M. q! E7 I7 ?3 w$ cp rules/*.data /usr/local/nginx/conf
& f! M1 Y: y3 `- S4 b为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; F% t# P% i- u- J+ P; @0 E; Q4 t1 e" W4 v8 k
( |9 ?4 @0 C6 T5 `6 l$ vim /usr/local/nginx/conf/modsec_includes.conf
0 R) z; r8 D& A3 a5 E$ i% d( f9 C ]+ `2 m4 @* t
[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 ' h* h$ u/ z6 c# F4 m
5 n" e0 j0 D. f; u2 H0 \% F+ W7 }
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
2 x% J! H0 A4 i
1 y0 V E0 n1 r+ l& J0 u
- c" w$ B2 n$ F2 t# Q& K配置Nginx支持Modsecurity- z5 I3 O& Y; u( H
启用Modsecurity
4 p5 v5 G; |' K+ \0 F* z使用静态模块加载的配置方法
" G5 i( p$ U; e( o# q! W在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 s0 _7 i+ x' g% C2 M- d8 ]9 p; A4 m5 |, c3 Q( ~/ _) l+ ~- R
' o2 O8 m' ^$ ]) m; H( eModSecurityEnabled on;. m) \" n0 A; ~$ L% N
ModSecurityConfig modsec_includes.conf;
3 v; C0 D9 b1 q/ H5 V9 F6 N( b% i修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ q- E% P# P( t% c3 c
* w/ C) k1 S* S. v
! l" m1 B! ^* Q# s! P9 a' j) h$ vim /usr/local/nginx/conf/nginx.conf. P% R* a' N) l: X
# q/ o# o+ n* E1 \; `9 Y6 h. o, { M; x7 F( ~ K% K
server {
+ b8 [) u5 s$ O8 j1 Y) V listen 80;
3 O3 \2 m x K* F1 d# b server_name example.com;
/ H" U$ D3 N* A) W! Y9 c( R- x1 Q0 g) E+ L8 _+ x
) E2 h, G; t0 U) v2 b7 S8 N location / {
. S' j% k3 k/ t( R. T; X4 U# @ ModSecurityEnabled on;
% h( [ ]7 ^7 m8 O9 K ModSecurityConfig modsec_includes.conf;0 R. D8 M) z: ~9 K |) M
root html;3 Q6 K/ l* ? C: {) L8 g3 B5 E: U
index index.html index.htm;
1 }8 Y5 ?; O: W$ Q }7 L$ D) l: r1 t: |2 J0 q: ?
}: i2 D# t1 j! N5 t* d
使用动态模块加载的配置方法. p U3 v. J- A
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 s; G" Y/ g/ C9 F+ @- E: E
2 f5 y/ F# U% I- r* y/ k2 Q7 ]/ k3 ^* \( y8 h. A" P% f6 V2 P8 p
modsecurity on;
# U8 K) _. H @modsecurity_rules_file modsec_includes.conf;" G C, X- b/ D2 ]2 q4 k
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 \5 Z- h, x8 ?* w' z) B3 I; o0 W Y) R" D0 L' F) v+ P
' c1 L/ B* [6 K! J; ^' P$ vim /usr/local/nginx/conf/nginx.conf) p9 T9 W. [; C7 [
) B6 c; g% N: D2 H
4 V, {( N3 C9 W% iserver {
( G1 Z$ w% J+ O3 f listen 80;
# q/ t. A( p( B$ I+ _ server_name localhost mike.hi-linux.com;' n& N$ w2 b2 N- q
access_log /var/log/nginx/yourdomain.log;4 G0 J6 w$ l; V/ z/ ]! t, q
0 F/ G8 F0 [( i- n; @, C0 r
1 `8 o" b. b) F7 \% _1 a location / {
/ G$ a* x/ f+ B3 \* H9 z
5 R A5 p4 c# ~
9 S- u" k" I, Z) p. p. o modsecurity on;7 d- H. ]) Q- x4 K# Z' ?" K* @
modsecurity_rules_file modsec_includes.conf;" w5 @6 _8 D0 ?6 d$ r
root html;9 ?3 l3 ` M- [9 \* {0 x& |5 E5 a
index index.html index.htm;: z- t- H- ~* O3 Y8 N) X
}1 A, I! e! S' B# z4 V) ~0 T
}
+ U. S7 X) T" {1 z% O+ [% J) }验证Nginx配置文件
/ X/ s* a* b2 k: [$ W- {$ /usr/local/nginx/sbin/nginx -t/ ~! r/ G2 J. {. W6 Q" ^9 I
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2 }. h5 X& Y2 n) x% C) }nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful- H, G2 `% t% _0 @8 Y
启动Nginx
" E& y" e0 z" C* K/ s7 x8 `, X$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 G! [6 y( `# ?: j; R
5 `. K c9 i6 G8 C1 M1 E测试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能够战胜更多复杂/未知/混淆的攻击模式。 + N- ~# W7 m6 I/ k" T
|