ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
- `* ]/ O! A6 f; \1 v# e! K5 z" X1 R1 B# t; ]1 x. r5 O
4 r! [$ a: p) t i" S& v' x
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。, g! U8 M. V/ `3 r
# E4 q) I6 ?: P; Z
$ H/ C5 \. E5 ?什么是ModSecurity' l8 ^0 {1 X* v r1 k
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 C( e& O+ a# J, Y# G: d8 [0 P! `3 ~" R* P
+ w _: z! h m3 |
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
1 g' \1 O/ T2 o1 `
, ^# K" L7 N3 a2 V$ M
0 d* w( ^' s c9 i% i; m* z9 qModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。, }7 n4 Y% ?+ y3 }+ ~1 q
, T4 U! O9 }% h( W
6 D1 l6 A; x3 Q! \5 u5 k8 X官网: https://www.modsecurity.org/
- W# s, W( x) ?
- i) A" S# x [5 u* B7 G/ s; u. y% l" `: G; ]7 s
什么是OWASP CRS5 Q' s1 i1 R2 n. y
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
( M) a6 r6 t2 n$ j! \
3 s3 v2 x. S6 a: j1 a e2 `
1 l. T' i5 X) j: [% SModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 B0 N! |( K$ I% X( t Y: }% I" ~$ k$ l8 g) R( x
$ K3 g, P0 o7 x4 lHTTP Protection(HTTP防御)8 ]0 T0 Y) T. h+ x* W8 |6 D
HTTP协议和本地定义使用的detectsviolations策略。
* m3 Q% o5 n6 _$ x. a2 Y
/ k1 e/ B2 ?/ h0 ~9 r9 c* q% n, R- D; C0 i: N6 j7 l: J6 N- L2 g
Real-time Blacklist Lookups(实时黑名单查询)
4 j, g0 c. B [- v5 V3 @6 A; v利用第三方IP名单。
' H/ e% y9 R( n+ Q/ k8 K" m, k5 P) e; j
! r4 }$ C4 O- J- x4 _: E1 c2 ]HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" _. A. e8 ?. x% y7 d% d' A: V防御HTTP的洪水攻击和HTTP Dos攻击。- d) i8 }# X: `, @2 Y8 S5 v
; v( W6 `: e- A( H, m2 S
' B) x6 I8 g/ T" C
Common Web Attacks Protection(常见的Web攻击防护)( N I( { Y2 _: E- t( b# M* d# \. }
检测常见的Web应用程序的安全攻击。
/ s# O1 f9 F+ {/ q4 G) y: k T3 V0 _, @- v$ \) B3 ~0 H
& y" r( q1 n! b, k# {Automation Detection(自动化检测)
& t% k: Q$ P9 I" u检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ X+ B& @6 S+ t8 O, t% g4 Q' q6 |! c9 i- C
8 t- ^% p+ `/ J- }7 T% BIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)$ }8 U- [: v8 D4 b- C- u9 N
检测通过Web应用程序上传的恶意文件。8 u% R! L$ A0 X7 D& _
, y( ?& H$ q. M0 f! N/ O
5 a( R% P0 }) m, z7 n% k9 G
Tracking Sensitive Data(跟踪敏感数据): {; z1 ]% B: V6 ?! {# ?
信用卡通道的使用,并阻止泄漏。
# H9 x6 T J" u/ m6 G
! h1 W& |. a% Z$ n
( F, a- |% w$ h+ vTrojan Protection(木马防护)2 z- t6 h+ O! B7 y5 k. h
检测访问木马。% E8 X1 u4 Y; n) j
* Q' O6 ]; Y3 o) b! n8 J) y4 A6 `0 a9 l2 s
Identification of Application Defects(应用程序缺陷的鉴定)
% x2 p/ d' R2 v- f8 O1 o, X检测应用程序的错误配置警报。' s, Q! L3 {4 N
0 W4 U( y+ W* ]% J6 w. y
. f# x2 l9 h* B$ p: ]& @6 sError Detection and Hiding(错误检测和隐藏)
/ v" x% Q3 {! G# l检测伪装服务器发送错误消息。 y$ L' A9 W4 Q( k" W+ k- N% T' m
4 D T' b$ K' D+ O+ o" m
" ]% _! i+ a# B安装ModSecurity2 L; h* V% I; i! m* F
软件基础环境准备9 n _, o: Z3 P5 c' J
下载对应软件包
J7 g& ~! B& c6 _4 \$ cd /root7 w9 M$ Q8 o5 X' `$ `) _8 F
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz': v5 m5 |3 b1 o: q# d
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz1 n+ g7 b$ I- \' Y: C
安装Nginx和ModSecurity依赖包
8 k$ P. ~) Y% e9 `Centos/RHEL
3 T( F8 J }/ p- @" }7 }, T6 l: T( x( P% ^$ m5 [& l2 c; @
, k" r4 ~. r0 z9 M7 B9 Q& r
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) U5 S5 z* L* K) lUbuntu/Debian
/ D, s7 r: c+ i) n, B$ o' a( _7 E8 z$ B q. c( p$ S
8 o/ r" B) f( w; r7 _$ q$ 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
0 H, [4 @/ J# K& u. V编译安装ModSecurity
/ c( u$ x/ l- e5 e2 y8 TNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ K% n& l$ J; w( p/ S5 T/ l
2 m: x& i% b Q+ T) m2 M9 F9 _
* R8 ^4 `# H" m6 |7 C9 |方法一:编译为Nginx静态模块
0 N. b& Z# m, f
+ \* t& ^, i/ D
# g7 [9 A# ^ ^4 y( r* [编译为独立模块(modsecurity-2.9.1). L% q! T3 O7 z0 ^& a
$ tar xzvf modsecurity-2.9.1.tar.gz
1 T0 B9 K( q: Q5 n* P2 ]* A( w$ cd modsecurity-2.9.1/
" G* ^! P5 Q' h5 ]$ ./autogen.sh
+ ^4 W9 q; Z, l5 G5 j$ ./configure --enable-standalone-module --disable-mlogc4 ^) }4 B5 `6 Q0 D7 p
$ make
3 `3 U; k3 [" B. P1 ~7 j Q6 ]编译安装Nginx并添加ModSecurity模块* f: ]4 i6 C4 {0 [8 g
$ tar xzvf nginx-1.9.2.tar.gz
4 G; a# z, ~' f# u& I# ~$ cd nginx-1.9.2
q @+ z9 P0 D: Q$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
* m4 R m) |* O: ]) A& N$ make && make install z7 J' F! }, Y+ I; G
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
% a8 z6 d& ^ v$ M, t) \: K7 r2 e4 l' ?" h1 g
$ ~- ]+ x' R7 m0 I编译LibModSecurity(modsecurity-3.0), V$ E. A, {5 Q3 S# L. T
$ cd /root) X) X' w* w# i g$ u; d
$ git clone https://github.com/SpiderLabs/ModSecurity9 s! v1 \. I4 `. [( z7 Q* F
$ cd ModSecurity
, c6 c' O5 { t+ E$ git checkout -b v3/master origin/v3/master% P8 b& P% }* L# e- e
$ sh build.sh8 i$ S* d5 \! Y# N% Z2 R
$ git submodule init
* K% w/ w3 J: c/ G8 o: w; B. r$ git submodule update/ r! g' T8 q8 o- Y' o A9 K
$ ./configure2 P* e( O0 T; k8 V6 x, T
$ make
! l1 T- ?, E0 b* w/ k$ make install3 X; k- s" ]3 O9 a- F2 u
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 C' `5 ~8 c* Q
! a1 C* G( J- } E: K. E
2 C3 Z- T7 E7 H0 A3 R, @: e4 z+ U$ ls /usr/local/modsecurity/lib
) [' d; W7 C% W! p9 qlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0& [, ?* z+ E0 b/ U2 _
编译安装Nginx并添加ModSecurity-Nginx Connector模块
( H. W' q2 l9 o: m5 e% T, Y使用ModSecurity-Nginx模块来连接LibModSecurity
( e- |) g$ Y! x' y- Q
6 P/ C+ N$ O# s m% t8 K/ ?; g) F0 g# e5 ]' A I' p
$ cd /root
( a p5 {/ W. O( J" A+ I+ e$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
) Z: M( `: J b# |/ O! P$ tar xzvf nginx-1.9.2.tar.gz N7 V6 N* q1 f( q; x; C
$ cd nginx-1.9.2
* y( B o# I) K2 Q9 Z$ ./configure --add-module=/root/modsecurity-nginx) j) Q4 k; x$ U3 |( P7 g. s+ T8 c9 j
$ make
1 A% q3 P& u% |# R5 q l0 d" x: B$ make && make install
9 H( C; l1 t1 A+ ?9 \2 r添加OWASP规则
& |2 a6 q, d* v+ k, M$ LModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。: }! c6 d" Y8 \" c* W/ d
) Z/ W% @* m) G4 ]/ @. @0 j3 Q, j& @ |) _. u
下载OWASP规则并生成配置文件 d- _* k2 T7 j/ p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git* R' c7 w! o( k7 a% Q+ X
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
% m- P7 q" E. i7 x2 d( p$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 o: y: W* |; S
$ cp crs-setup.conf.example crs-setup.conf4 |5 k6 [" z$ ~$ p
配置OWASP规则6 t- n( O- l- u' w
编辑crs-setup.conf文件 H* q# K7 g, P/ ~( E+ q5 t$ s* l
2 K' T& a5 K3 k5 Z' l, g" e- p; n- `5 e/ b/ m* N6 M; }
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf( {/ d- g5 k! `- S5 p" h
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
- v( Q+ Y- `* j+ \$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. j3 B% \ ?" L+ [+ C2 `$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( u/ Q9 X# t3 ]; `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* C2 \$ V6 G) v0 x* p6 X6 s0 P) }9 H0 V6 E
/ |1 `5 c; a2 X启用ModSecurity模块和CRS规则
0 i e# G% r" x" K- n! h8 \复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" W5 j+ C! J- V2 n' {$ ^7 @* \1 W- N* F3 U6 B2 o% W. r
5 A. X6 M8 x" q# G9 j- _modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。+ l( B A# ~% ^6 d
/ P! B" r6 x2 Q% P T+ w0 f- z. B, N* z" k! k) q4 ~
$ cd /root/modsecurity-2.9.1/
5 f; `8 {# ^/ C. o, v$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf # D0 }% F9 p& M5 l2 ]) o u
$ cp unicode.mapping /usr/local/nginx/conf/
y0 a- s1 W' B/ [8 F将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 P+ M. B! L, T' d8 [3 R
" J0 W2 p+ ^$ P" ^! [
/ N* N( u3 w+ J: i) \7 v4 ?+ z9 \$ vim /usr/local/nginx/conf/modsecurity.conf
; P: M( k6 H; K+ }* t0 O% f9 X9 [! kSecRuleEngine On
1 \! f3 }* w2 {ModSecurity中几个常用配置说明:
- X' f, L! _% y* i
( F- O- B. s/ p2 i1 v/ k) h9 A/ C2 ~5 K' K( N8 K: P3 X1 m. d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。" v4 A6 ], I" r, [ w
6 {; j$ ~ r/ F: x' K# H0 j9 W/ b7 U
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 f/ f5 K9 C Z4 c5 C5 Q
/ t- P- d# {2 [: w6 P9 u( Z7 L" H7 X+ b1 f- z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
/ U& U9 q' P% C* r9 u2 {7 m
9 I- }% C* X; z9 _# }- n/ K" ~! i: t# y4 c5 o4 k; u
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。( n' D3 T2 l$ M
8 g, Q2 u: S2 ]( f; c; c; L1 I1 {1 z
& o6 @) ?2 o% }在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- o2 r ~ u! V9 d/ R9 L
! {4 p! t7 `5 M3 r% c! E# g) Z x9 g: ^
3.x版本CRS
3 ]/ \8 u3 ]9 Q& I( Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- P5 K( k- p X5 C1 `
# 生成例外排除请求的配置文件
- p9 S. B7 q' P& V+ Y0 J) ?3 X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf9 c# a0 J, } Q a
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
+ g; n" E) u5 ?" P$ I6 M5 S6 O6 ~$ cp rules/*.data /usr/local/nginx/conf
& Y) ~$ K* \$ h( M# o为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
, j1 F2 ^% H! |, s* z" Y% C3 Z0 g6 l# Y/ D& a
& |1 `9 v8 H' m' k2 C7 L2 o+ v5 W
$ vim /usr/local/nginx/conf/modsec_includes.conf1 O& i+ ^" a- u3 A1 H4 [
! C7 d/ [2 ^& Y, F. Z2 p, y3 G7 v, U
[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
* d4 T# n0 Z5 y: X; D* i7 S) B2 S( C
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
7 d) T6 W+ a1 q) \( A' x0 T$ g$ f" ]8 C' v, l) z! I
. ?# P1 t* A' Q+ c, R* c: S1 i$ V
配置Nginx支持Modsecurity' Z Z" A) Y( l8 S& r, n- X6 ]
启用Modsecurity
' F! o. Z R6 ~" j# ?! s+ K# W使用静态模块加载的配置方法3 i$ r4 x: J& q6 i1 d- }' C
在需要启用Modsecurity的主机的location下面加入下面两行即可:6 K. R2 C4 Z9 _% W1 }! k) ~. N
$ P2 c: m7 W$ T v
7 u4 {2 \' _6 v1 v8 P
ModSecurityEnabled on;. ]. q" S% ?) v; x& e
ModSecurityConfig modsec_includes.conf;9 w) P) Y$ t( P8 R: `
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 `- n; N- y; G
- q. X W8 b1 S9 T' d* W
; w( K0 Y' \$ V# Q+ t) t/ J$ vim /usr/local/nginx/conf/nginx.conf
" Z7 \' C* L' I2 ]- V F8 G9 @9 e4 |, w% s; s3 k; q2 v0 |
9 r+ r0 e" s w8 T# @( {: d( s
server {, S* J# `1 H9 n% ^6 c Y6 M
listen 80;! l: n' W% A. t: B3 U
server_name example.com;
E e: E: @7 F5 M! `; N
) X K! x/ U8 ~- q, e0 C3 L7 E0 \7 P3 X& G1 b( O/ p# K+ P! @( N8 V
location / {
* t+ P Y+ S: u/ M k2 ?5 S5 B8 S ModSecurityEnabled on;
/ b2 T0 o) Z! r! K2 z ModSecurityConfig modsec_includes.conf;
# M* _( F; S' v' g) ^ root html;
" S- ~0 x2 a2 Z# ~ index index.html index.htm;
# }9 O" K# q0 T% e9 S! l8 t$ i }! C! e7 a% _# y" y O
}
# u% [% K9 T( C! r使用动态模块加载的配置方法
0 P: o! C, d( U" R( z在需要启用Modsecurity的主机的location下面加入下面两行即可:! Z$ W3 E' R; g2 U$ @8 \
9 p; \) ~1 o2 o4 x/ |
' L; S: _3 O8 x+ O; [1 Emodsecurity on;
9 o% b5 n! K! _; qmodsecurity_rules_file modsec_includes.conf;
& X s3 G3 _" n2 h修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( b4 T- G! C) u& w! h- \0 z
4 ?# F& l _) T' j& A! k4 K0 z) ?
' x- k$ E& q: n5 L) \$ vim /usr/local/nginx/conf/nginx.conf' Q- E- C5 M s4 R7 r% c/ g
( I4 C2 u1 e0 z6 E) r* N* L: S( `. _, o
server {7 H4 y8 q" J( [) c" A6 }: S, Z; x W
listen 80;
. z; M' ~- @$ d0 |4 \& h server_name localhost mike.hi-linux.com;
- _0 @+ ?+ A# ^4 [8 z- d/ l7 k8 X access_log /var/log/nginx/yourdomain.log;0 `) `' R4 B3 B9 G
* P9 ^, l) ~4 D' `" S9 v$ v& Q7 @ D J
location / {4 I5 z( ]) y E7 s$ x
, V1 Y3 G4 v- B- ~. c
2 F7 {7 Z( p3 H' G modsecurity on;
4 D% b" p' Q# K$ w% p modsecurity_rules_file modsec_includes.conf;# q4 [& W7 n+ ]
root html;9 Z8 S) G: h% d+ s, n
index index.html index.htm;
+ R3 \5 w' v& y7 h# F" F Y- G}
0 A: E" a" A7 j1 U4 n}
" h( L V5 U& A- M3 }) W4 e验证Nginx配置文件7 F! u6 x+ I! J8 D
$ /usr/local/nginx/sbin/nginx -t9 o, V- Z. N* b6 e
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- A r# T+ h {% O1 s9 _: ?nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# t" D" M+ A; t! h# B; R
启动Nginx9 ]# X" m5 J _% C2 q m ^
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 X1 I5 E. T* X% a0 y
% e3 ?& G$ M& r9 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能够战胜更多复杂/未知/混淆的攻击模式。
! u- ~5 K; h+ m5 |0 n9 i |