ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。1 g9 m8 N: E( p8 x" ] o! A
7 k* M% H; L3 i2 N0 J' q( {; V$ A( f/ _# K% Q. s+ \
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
1 K: H& u0 g( x# M! V5 Y8 _( T; [' q3 D+ O4 e# z9 e" \
# Z) s" o! k1 ^9 I8 [" f什么是ModSecurity9 q$ ^! U* v+ _
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
( N/ o* Y: A1 K( T$ r2 }1 r8 Y9 J
; G2 y5 j7 }) V, h. b) q: W+ ], b% X( e: K) j9 Z: }& f
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。2 c- b' N8 V. e3 Y# R! Q, I
5 W _/ t8 I `6 d! J2 X! H( V! l& T# S1 z/ r: j+ Q2 e
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 u: a p: R! J9 ^) ^7 Y
0 C0 l& q% i# f+ U; S) u! Y1 ?
2 C5 }# _* w. I2 B: ~# |4 I; b: k+ G( \官网: https://www.modsecurity.org/7 L8 n& p9 c1 | _* @7 d6 P. w6 r
! g" N8 x P% j
3 Y4 W% q% a" r& m什么是OWASP CRS
2 }% A6 l: k, _2 vOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 R7 X9 l/ v- O3 j
! ]! T3 m$ \, I
. J v/ Z# p9 o0 U& T3 }4 @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 r( U% B" g7 ?8 Y' Y% S A- |- @$ p6 h+ c
* k4 Y9 ]1 s: `; _. W3 NHTTP Protection(HTTP防御)
0 W" M2 Z0 n; O0 ~, DHTTP协议和本地定义使用的detectsviolations策略。. {; O9 P/ n) D5 s
4 T. p; D+ a1 J/ N8 C ~4 M9 Z: B: L( \
Real-time Blacklist Lookups(实时黑名单查询)4 j) p# t9 w+ ~7 a; u" K
利用第三方IP名单。
) j0 K2 Q; S: M+ h
4 C, \! _" k, v$ s! \; p( Y' H! Z, }" r9 R7 M
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
7 [+ [. ~8 i6 `防御HTTP的洪水攻击和HTTP Dos攻击。/ ~) A) W G) c$ Y8 y
$ B8 l6 n2 X% f: ^: Q Q$ G- g/ q3 n
7 q5 h/ w9 d, b) ^5 |
Common Web Attacks Protection(常见的Web攻击防护)
+ Q% t9 i: X' Q& L! w0 V3 Z7 W# F. G检测常见的Web应用程序的安全攻击。
% X& b. B$ p1 o$ P1 k& b; C1 h. ]" \9 B
/ ^! c) T* {: T+ r4 N
Automation Detection(自动化检测)
* c) c1 Y) e+ j2 g5 I* A8 C" ~检测机器人,爬虫,扫描仪和其他表面恶意活动。
2 C( ~; P4 ]4 M8 ?8 ~: O% t& F" x8 U8 [% l; J+ z+ `$ k8 p$ C& Q
" G- d8 p( S8 o1 W9 I
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)+ w) ?7 A9 q/ }% E3 M* J% W0 n
检测通过Web应用程序上传的恶意文件。
# i1 c9 _7 W$ g7 F# z% ^4 P1 V+ q
7 a/ K* L" p3 |
Tracking Sensitive Data(跟踪敏感数据)
5 M" E& |# w) Q* I' N( f% [: k) g信用卡通道的使用,并阻止泄漏。+ ~6 ^. m* m1 z' q" n
^ e X v' }, |; g, c7 Y
- R, Z4 X |( S9 x# ~' ?
Trojan Protection(木马防护)1 u$ m9 h) D n+ G5 N" G" v
检测访问木马。
& z6 H8 o. u$ m
0 j+ {* M. v4 N" m7 X8 y- J
3 g% `: p' f6 ?0 L3 H" l4 [ }( B9 G* lIdentification of Application Defects(应用程序缺陷的鉴定)" b r4 v7 E6 m7 H! y& }) t
检测应用程序的错误配置警报。
9 d: k6 _8 w& c& O0 y M4 Q) A7 m/ z+ |3 k! f4 r9 J
5 w1 |1 h1 a: N' D5 qError Detection and Hiding(错误检测和隐藏)
! @9 W" r1 x2 b" s8 _9 \8 _8 J5 o9 i7 x检测伪装服务器发送错误消息。
O" A% @+ D% v7 X1 q3 i# r
- q4 L8 J2 a. ~2 H1 Q
8 C; k- p! V5 z$ \5 F' R; f( r S, n安装ModSecurity
- t# S5 e1 E- s) o* b软件基础环境准备( F- a; p/ g% }! s' d
下载对应软件包! a3 @6 H) f% i0 q, w# l3 T2 ~
$ cd /root
0 `8 |' r/ j3 j4 Q" r3 {$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- Z) f6 I$ A; o/ B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz( `; X4 D7 L) d
安装Nginx和ModSecurity依赖包
" c3 J. S/ ]5 }( P, i" Y2 ~. SCentos/RHEL
: @5 s- k7 `4 |( H d4 O. c3 Z& C8 x; ^
: a7 u! W- ^: w; x! w; o# m$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel) c- V8 B: g5 K! ^7 R" }
Ubuntu/Debian
4 Y- @! o/ Y8 J, u2 E) ?
+ ^# n6 x, K6 U; m( g( G9 L/ D% \- G6 {
$ 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. @$ n( O( Y* _8 P
编译安装ModSecurity- b4 |2 |7 w/ H( G! V. Y
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 z& o& E: m1 r3 \! y1 ?$ I7 X8 h$ u! R& [9 I
2 }( e4 \8 C' G4 G1 U2 D方法一:编译为Nginx静态模块
9 H8 J; U" Y( b! U; a( n
2 Y% P7 S) Z1 [1 I1 U0 ^; P; p% `+ \; g$ ]
编译为独立模块(modsecurity-2.9.1)
0 M+ l* M. e0 q" R6 R( C. H! f$ tar xzvf modsecurity-2.9.1.tar.gz: M: N2 T Q7 F' S, Z
$ cd modsecurity-2.9.1/6 \6 o' i. C b8 R& K/ J
$ ./autogen.sh
p1 l7 y$ z; ]$ ./configure --enable-standalone-module --disable-mlogc
3 w5 J) n+ X) Q/ ?1 o$ make1 _- x4 P; `" \4 H( x! B
编译安装Nginx并添加ModSecurity模块
5 k) i$ p. N7 I' t$ tar xzvf nginx-1.9.2.tar.gz: J& J( m9 o1 J+ G; S% }1 w) Q
$ cd nginx-1.9.2
( u7 E/ g1 P4 _: ]" I# A$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# O( M* ]/ s# `& S9 X# p$ make && make install
- o- Q& }- U4 Z: `: v' s) O方法二:编译通过ModSecurity-Nginx Connector加载的动态模块: @1 h; [' M+ Z5 Y8 Q0 D
' ?0 F, M" w7 {
- C% R9 y' D- P4 W5 N编译LibModSecurity(modsecurity-3.0)
6 w! v% f3 q( B# r$ J$ cd /root p6 z; {, u1 B/ F" p
$ git clone https://github.com/SpiderLabs/ModSecurity% K O0 z. b4 f) K' X
$ cd ModSecurity
* X4 i9 c% {9 k2 P7 w$ git checkout -b v3/master origin/v3/master, _ f! w6 H* x0 H: k' s
$ sh build.sh
5 g* b; } q0 ]) K5 s4 h7 X9 u$ git submodule init4 t' I7 B. E/ p5 c6 ^- M
$ git submodule update$ G* n6 f6 m; `/ C1 ^
$ ./configure
) P7 V ]+ ^ T, k" k0 C5 J$ make, `/ w9 R1 p$ I$ V: R1 j& g. a# b
$ make install$ c% l, `, I" g( b2 k
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。$ B& w3 T4 [# k( H4 r
6 E7 _& T1 |$ y& k9 K3 l. w! F, ^, u) X. U5 U( {7 `- N
$ ls /usr/local/modsecurity/lib' h4 A/ a* R6 M( q* p" P8 e( R) [
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.06 v" a% o" Z. X# p+ l
编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ M8 T; q7 z* M- `使用ModSecurity-Nginx模块来连接LibModSecurity
% Q* c+ {# c# x. G- q0 U# D) p
9 ~* r1 e1 ], w X$ D$ D5 t6 r! O5 `% K
$ cd /root' g" A( z+ P" i; z3 J8 t7 m
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx1 g* c" D7 c/ b x( K; p
$ tar xzvf nginx-1.9.2.tar.gz4 G7 C- \# x3 n
$ cd nginx-1.9.2
8 b$ ` A9 ~# s: w `4 Y- l$ ./configure --add-module=/root/modsecurity-nginx5 f$ X; q/ r- S; ?
$ make, j8 H) n& ~3 \, Y" P% c
$ make && make install
, [8 E, f+ r+ k. g7 S3 P添加OWASP规则" D: h2 O4 w: R* Q* w5 h
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( k) \% L3 T1 S- _ b
3 s# V, D" F8 Z7 d/ g2 D, P* K. U9 k, \. k" ^7 T2 Q# t
下载OWASP规则并生成配置文件" d" {) E+ r& ~7 W. l S/ n; ]
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ u+ f6 e" \; u- x$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/% d; J3 H9 |7 a. L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 u, h- |' t# p; h0 l
$ cp crs-setup.conf.example crs-setup.conf p0 q0 n- Y0 Z1 h3 D3 R& V7 V
配置OWASP规则
9 M. L' U. b) r7 y2 \ _$ Z2 Q& j# L编辑crs-setup.conf文件
3 a! a) O( x+ O, @! U2 U7 ] a' Y& U k1 P/ J* ?
6 c+ c. g( N! r- a! {$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
8 E' N) I7 `1 c. U$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 g& z0 t" ~7 F$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
! S( P* p' [$ c0 i' }' V$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: a/ t( Z1 S: E- y' P
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# j( }) P( |/ b9 O$ s: d. r$ z
1 f$ }" M1 @' z7 W+ N3 F, |( V* {, y' \
启用ModSecurity模块和CRS规则: D0 ^7 D, e9 t2 Y9 P
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 w$ j" W$ ?, J# v
; e: `, v4 e8 @* {* E$ T) K' ^3 R0 d' d- I! l0 b7 l; G
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 J; ~* S: q% r8 j1 F. t n- |
2 J O, U( Z1 x. x$ cd /root/modsecurity-2.9.1/
2 h _' Q$ m' R$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) B3 m* g2 s5 n* k, }9 O/ {$ cp unicode.mapping /usr/local/nginx/conf/
0 o( b1 F0 V" B; [6 g8 z将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。5 d. W5 _9 N; `) A- j3 o2 k
( E: _! X" T) N8 H
- d7 i+ O5 \1 c5 ^. C$ vim /usr/local/nginx/conf/modsecurity.conf
% h8 v; }5 y% c! kSecRuleEngine On) V. C9 a4 l+ Y& E: g
ModSecurity中几个常用配置说明:
9 |; }) ~9 R8 l
. ?& [& O \/ S. z& }7 f( g# |
% Z" t; W1 j& y" _4 p1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。6 T+ \6 R I+ [5 \( B" A/ s) ~
$ E) c6 ?5 g4 T: b
& {* @5 f3 B2 Y3 L2 n* m2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 K8 y2 k @5 H' n/ Q5 b0 ?7 z6 B
& ^7 X" ~4 a- m. g! j+ l. b# ?% U s0 {
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- y; ~" Z: k5 ] c- G: j4 L8 @7 H' h8 Q: r/ ?' \3 l) h
! A* _) N+ R6 B
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; G: j) T4 a }- k% j1 ?
' A! Q( ^ X, p" i; _# d* o% \. C& m5 J; O
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。: m5 n/ |+ I/ o9 v8 {! P9 ^- e4 s
/ D" {. c7 d: o n8 L
, |$ T+ _! G; l. c3.x版本CRS# l { C* s( i1 ^: G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 d6 m1 h( N) ^8 D) ~$ P
# 生成例外排除请求的配置文件0 i8 q6 I1 [3 `
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf |" R! k( j1 w; @: @5 [% C4 j
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
$ K1 B2 ]: }$ M7 b$ cp rules/*.data /usr/local/nginx/conf: e @5 I6 }+ l
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' B4 m* t, j" }( N. s4 D; M# i& E9 Z& ?( c: ~
$ \7 y, Z0 m" n$ r. V' u
$ vim /usr/local/nginx/conf/modsec_includes.conf7 ~. B) X. S" H
7 d$ [( _$ g5 w2 M6 G9 K
[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
+ T5 P, p7 n8 w; Z: m5 W# p+ J; U8 ~3 T: o& X/ o
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, [# [& I# S1 Q' @/ u1 P6 ?9 r F& u
- _, A/ i( L9 N
4 w9 t( {3 {5 `
配置Nginx支持Modsecurity
3 Y6 y0 c/ M* T* i: q启用Modsecurity
$ K, x. o5 z' J/ ~" H% y9 T使用静态模块加载的配置方法( E8 ?! u0 r! f$ q) ?
在需要启用Modsecurity的主机的location下面加入下面两行即可:8 @/ R' j! }% d1 j6 e( u9 H
! [9 V8 F1 d: D9 m+ Z( c
6 j# }: ?( M t. ?" s. qModSecurityEnabled on;
0 V9 U8 P. a8 l. w1 j U3 K8 GModSecurityConfig modsec_includes.conf;
+ ~9 s- t4 b+ z1 X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) A. p4 v( O6 t$ P5 S& k
; B0 \8 A" g# n; ]1 J+ h# v( i; h9 Y# H' X* w
$ vim /usr/local/nginx/conf/nginx.conf3 n; `4 V7 j; V6 q. k+ |
. i+ g9 l+ |2 M& p5 d
( Y/ Y1 n0 U8 s: `: d. O
server {4 a8 g$ |0 b! c, r
listen 80;! ]7 D. x, |" S
server_name example.com;+ d$ K+ E' K2 F
# M) ~# y& h1 e' H9 _1 q% Z4 w
/ S/ y5 r6 i( y& O" j R location / {# W# H: x$ F: J5 J( c
ModSecurityEnabled on;
$ Z ^5 Y1 z' f; b ModSecurityConfig modsec_includes.conf;
; h: Q0 {0 S, U3 } root html;& o" l0 N" z2 G" \
index index.html index.htm;
3 Y" D1 @+ Q" K }
9 ~8 t, c6 L! x7 x* D}
& u+ `( c- p4 s O, W使用动态模块加载的配置方法
' i, R& o& G2 u9 a在需要启用Modsecurity的主机的location下面加入下面两行即可:8 H0 F$ _+ S$ n, }- k0 C
& E6 G% q" W, O1 `- B/ P
) _$ D% }3 A9 P: gmodsecurity on;
0 X, ^. w0 k& D6 E8 k8 Omodsecurity_rules_file modsec_includes.conf;& O, R& I Y d7 T$ |! z r
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% a- n3 A; h3 e. [
5 A: {" F5 A. k: B* e) Q; m4 e; t/ J# c$ Q, U+ n
$ vim /usr/local/nginx/conf/nginx.conf$ y% h+ }* u9 j' ` J
# E- k- t# B( o
6 f# _9 |; H$ {+ C1 ?1 nserver {! j& n$ G3 D/ q/ n& |1 x T
listen 80;8 G0 X! ?3 c- D4 M) p8 n
server_name localhost mike.hi-linux.com;! n7 S9 V9 ^/ @
access_log /var/log/nginx/yourdomain.log;
1 |# ^) Y v# Y
& N+ t$ `9 r' _& a; j2 I, l. F! n3 T5 @) P4 ^& i0 [& Y
location / {
/ V' `% c% T' N1 v; ?& _- g. N+ s& o. q# Z' d8 y
1 K$ E1 V9 y! i6 n4 X' C modsecurity on;6 @8 x9 j% J- g3 {& v
modsecurity_rules_file modsec_includes.conf;
+ \2 K+ e; Z" _0 e& z* j! ] root html;
7 V- c/ y4 N/ Y9 M index index.html index.htm;, Y9 F9 M+ ^; ~* J+ a6 \
}
# ]8 Q; T) O0 n! o& }}" v- H C ^8 G8 ~
验证Nginx配置文件7 q5 L7 S. h; [1 J; K% ?4 A
$ /usr/local/nginx/sbin/nginx -t) t/ R* Q6 {+ |( {
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" I& n- p. s& N- o/ L1 ]- v4 snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) D5 C3 ^' }+ I" A7 r, S1 C% d启动Nginx" ~8 N, K; v& d) I0 V
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf( j# G3 {3 P4 k. D7 H7 g4 S& ^
$ W& P& t- G6 j
测试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能够战胜更多复杂/未知/混淆的攻击模式。 ! H. v0 N: T3 ?- c0 l6 z! X9 P
|