ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" p- H1 V# j5 N
$ Z$ g0 }& d; o% ?0 D# F* S2 l- j0 f4 ?: [; E
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。1 A% n$ ^, n8 c5 Q- I
7 N- V; J) J* |
) [! e! x4 y* l
什么是ModSecurity; d; k% z7 O+ f* g4 ]7 t4 |: h$ R
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
4 I& q/ ]) `* z/ f$ [5 `
+ n* I8 V7 B' y( v) I* H
0 T; ^1 [) N2 \( B, uModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! k9 _6 J- ^9 p8 R: w
: z5 ?4 c! K7 U; f
5 F% v7 r6 ~5 L W% { |ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 w$ Q F3 K' k
/ C) e. o S8 X* {! `4 @6 F! \
6 X9 n8 |/ i, e6 v* @3 A+ W) ?
官网: https://www.modsecurity.org/
" \) ~: v4 v% W- C5 A! H# a, P( x7 K/ {7 b, V2 f3 |- L
- l' o! f% m7 s, y Q! Q3 P* a
什么是OWASP CRS
% Z- ?0 E8 m' K2 JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 x# K& Z# ?' p* A- y! z" K8 t( T
2 P% N0 {8 ^8 K( d& S& o% B6 e5 U7 q1 e" y6 R0 |6 x- W) s/ w0 t! h8 V
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
% w# G1 r% K, M& p" _7 o& G+ g2 M! j; ^) q- J3 f* u1 p
4 i4 O* s% H% ^9 lHTTP Protection(HTTP防御)
& N' z! j, x- [$ L2 O! \HTTP协议和本地定义使用的detectsviolations策略。1 ~" w$ C7 w" h; Q0 M
* P) P9 X! x& o+ ^
. s. M7 [; b* r7 Y2 q* iReal-time Blacklist Lookups(实时黑名单查询)
, [# c" w; L3 a. c' n9 B% d利用第三方IP名单。* F4 A0 R7 m' r* V; d* o3 o
: G; G N+ d4 @' b# X4 R
) w# o2 |* @; W0 V' b0 I# qHTTP Denial of Service Protections(HTTP的拒绝服务保护)* e9 m2 @8 Y2 C( s1 E- B
防御HTTP的洪水攻击和HTTP Dos攻击。: H# y# o' P) U3 P# D0 ~. o
* U. i7 t% s* b7 T; Q
+ t8 O, A* S; }* ICommon Web Attacks Protection(常见的Web攻击防护)
9 q7 |0 e3 A8 G6 w! @检测常见的Web应用程序的安全攻击。7 K7 O( M" S+ W1 |+ E
' j5 A5 H' @. U7 U8 `9 q) ]# g' Q
2 G# B: R2 n d/ O' _+ }) |Automation Detection(自动化检测)& d' _' T$ K( F2 @0 s
检测机器人,爬虫,扫描仪和其他表面恶意活动。! C# Q6 d9 i3 O8 ~
: R4 O) F% v( d1 W
' d( G* Y7 A. ~3 Q, f3 ~- \Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
2 t( ~+ `- u" I. s检测通过Web应用程序上传的恶意文件。) `* Z8 e2 a2 f# P
. h A' u/ W9 t; w
1 D( r7 H- ] Y: fTracking Sensitive Data(跟踪敏感数据)2 B0 D; P5 Y" w' e1 m3 \' K9 f* D
信用卡通道的使用,并阻止泄漏。( q# R3 c5 A) e5 k, `
0 L. C* y2 b k' \" m
. Y+ E4 B$ ^( STrojan Protection(木马防护)8 C. _4 ]8 E5 P% d# ^3 H
检测访问木马。
) I, m* T- W% v) K. P v0 d. W( m/ R# ~' [# x. h
- m8 E( w4 l2 b; a8 {* EIdentification of Application Defects(应用程序缺陷的鉴定)
# Y7 w5 H3 e- X+ ~# r4 ~4 ]检测应用程序的错误配置警报。
" a9 |2 s3 ~. ]4 u$ I! h( Z/ C1 L$ V" M( i7 e6 I ?2 o
: p+ ?3 b3 Q9 J: wError Detection and Hiding(错误检测和隐藏)0 m$ x1 F# t) D6 _9 y
检测伪装服务器发送错误消息。
- _, w- j$ s# H }) B4 v9 G
/ y* C! ?4 G; [4 l" j I
/ N2 ^0 ]8 g; Z5 Y% E' O! g2 v安装ModSecurity7 p$ `. ?5 q! Z* O8 B6 e' W& n% w9 W
软件基础环境准备
. n. w' `# J t# ^下载对应软件包
* o! n* T; h. j+ A- D! D$ cd /root; W7 q2 n* s7 W, I1 W' O3 \
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& n, Y# ^, h! W$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz0 A4 q9 A( ^3 E3 I& t5 ]6 M7 {
安装Nginx和ModSecurity依赖包
$ ?/ ~! C. K ], g! s7 W; V6 jCentos/RHEL+ l Q* E, w6 T# y" q: u$ D
! n7 c! e' a8 j( ]! d G2 @
( q% y9 G4 o9 T8 K6 _& E) ^$ |$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel; J3 a! D. Z. y* b2 b! r
Ubuntu/Debian0 r9 c Q# g7 t% H6 @
. s9 K `9 g& R1 h( A7 p; o8 R x! }9 ~& R0 o. f A- N, J9 P
$ 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
. q4 i6 K3 |- }6 q编译安装ModSecurity
! h. y, B! y' x$ kNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
' U$ @6 _' i9 w% g+ W0 T) r2 [1 ^! y, X% \& {9 d' _3 S
: |2 F c6 J, v* \方法一:编译为Nginx静态模块
( k3 p( P9 q7 }
, r3 u) \6 p. N0 {" y) o' [0 G
/ f: d( C0 I9 P2 _$ l编译为独立模块(modsecurity-2.9.1)
8 W0 F% ?9 e, W$ tar xzvf modsecurity-2.9.1.tar.gz
4 ^- K& Z3 M$ p f. _# T7 h2 T* a$ cd modsecurity-2.9.1/0 x, ]% H4 c) ~1 S( a4 z; o
$ ./autogen.sh7 `3 [3 K$ |2 G1 n+ T
$ ./configure --enable-standalone-module --disable-mlogc' h/ V- p% {! ~+ f5 n
$ make1 L |3 j7 d: o2 M/ M) i4 v& W
编译安装Nginx并添加ModSecurity模块! K4 Y! A' F: n8 M' H, D
$ tar xzvf nginx-1.9.2.tar.gz' X5 U k5 }1 b$ ?. J1 `) N! x1 A
$ cd nginx-1.9.2; b/ H( u2 |* |' C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# B1 D1 S4 V$ ^+ ^1 s" ]; p5 K R* |$ make && make install4 f* g3 u. G' _( f" I
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
* N z N' T! o4 t8 s7 P& D3 v; a& e
3 p I+ @+ P0 y7 R8 c编译LibModSecurity(modsecurity-3.0)' I, l3 x" H; C4 K) H* T
$ cd /root
, b g2 K* J$ _3 ?( X8 h7 q$ git clone https://github.com/SpiderLabs/ModSecurity; b9 Q: |1 N; c$ o
$ cd ModSecurity
3 n- _! c' k+ g) ~# r$ git checkout -b v3/master origin/v3/master
. i8 D6 A$ y G$ C, |/ h/ g$ sh build.sh
' d; \+ w0 S8 d# a* ]$ git submodule init
9 B% ]( }0 {: f- n9 {$ git submodule update5 t* }& z" ^" a( g( F& O8 H
$ ./configure* d4 Y" s" x4 K3 p+ c8 w9 p
$ make0 ~# m- K$ B! j7 Q+ N3 i+ R
$ make install5 F( V% p1 N% N1 G4 K6 S
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。* I2 ?4 t1 |4 i/ O( v5 A% o% a' g3 u
8 |% z; }4 L$ K; ^. y
% j, Z. S: h! s$ ls /usr/local/modsecurity/lib/ s! l! G; i/ [+ H- U8 p: h1 F4 U
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0! M, `# d6 Y( }0 }* X' C7 k& Q6 X, g
编译安装Nginx并添加ModSecurity-Nginx Connector模块
& G! x( M+ E% [$ ?% \1 o使用ModSecurity-Nginx模块来连接LibModSecurity7 j( V' G/ l. r9 s
9 I- }" d" A- p8 }5 Z1 Y. I7 j+ Y# M3 X# b1 F
$ cd /root; o" W* I3 J4 G, E E2 l7 }
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ A T$ P! I! G) x$ l
$ tar xzvf nginx-1.9.2.tar.gz
/ a2 R1 F( H6 U$ cd nginx-1.9.2( s4 e" l! b2 B/ i9 I
$ ./configure --add-module=/root/modsecurity-nginx
( M2 R9 c1 n9 U6 x6 d: H' B$ make* s$ ], |/ H8 Y" L# Y: ?; F; M v3 `& L
$ make && make install
" T/ E5 o3 R9 d; R* r' [# _* i$ @! M添加OWASP规则
8 L$ b' f) W" \ `& D+ aModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
/ u6 m1 |$ F5 H! m
2 }$ G% L8 @9 Y& R+ y% b" F1 [/ e. I7 X7 j' i1 Q6 a
下载OWASP规则并生成配置文件' `2 h. F% P0 V4 a+ U( ~4 z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git. t$ c% Z6 a+ [) D; p; s
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/6 m: \ V$ }+ B" ]- |# b) }$ o
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 m$ H2 o5 G, w
$ cp crs-setup.conf.example crs-setup.conf5 p, z; K6 q( w. @' b1 Q
配置OWASP规则/ r& a1 k& `- |
编辑crs-setup.conf文件
) h/ x, `9 T3 l5 ~" h2 {, G* `3 q4 X3 t; |4 y; d* C
% {3 H$ S/ d$ ~$ E; E$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf C& p3 b$ _4 Q9 O0 w
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. z2 f6 W! Q+ O$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. A6 m- n6 }9 Y$ |( \* c$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
; b+ f6 U+ A: {默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。) b) Q% c% d# h& ?9 C% n
4 q8 b: |! z/ o) x! T+ d
) W6 B# X: v: m$ u! H9 _3 h- |1 W3 C" Q% _启用ModSecurity模块和CRS规则7 T. O+ a2 w6 v5 F$ g# l; b
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; F i; W4 E ^) }7 r. W- C
( F8 l* M" G$ J. N
+ f; J4 v1 i$ x" i. K! n8 Hmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
, u# N+ f; c2 Z& l& Q
& X% r x( m: d4 c2 L1 L" V9 a, X9 p( m' Z4 T2 I2 D4 B0 X
$ cd /root/modsecurity-2.9.1/
2 u3 I; G2 W [5 `9 C$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf % ?& |! R! p' j$ o5 _
$ cp unicode.mapping /usr/local/nginx/conf/9 F7 t- q& D8 F- t; O! S; J
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 [! b4 G# ]$ @% Y% X- m8 H
& A9 S5 s( O3 a. M
4 u; I3 Q) Y& }+ u, ]( V4 m, d F$ vim /usr/local/nginx/conf/modsecurity.conf* X- V9 i( ~: _8 _: K$ w# V# M
SecRuleEngine On! l9 @4 T) z" A: O! \
ModSecurity中几个常用配置说明:
- K- ? z8 {' V2 E3 [6 k8 p+ E) ~' W3 A& Q
& Z9 R$ g+ _; }4 y
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。0 H# I& {0 g% @" G) G
% B' P, L' Q5 _* Q* c, C, _
; K. z2 ~ p* P7 l0 q6 `- }2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
% J8 H! `3 v5 `' ?9 P5 A' C) a
) k: F' L2 w1 \8 i8 r4 a2 o) J' [/ c, C. U# G8 S8 B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。) ~- c4 U: k! n9 d( E
, p2 s! `5 k2 m$ z* ^
+ r& y3 c) g9 x! G& Z5 U
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。) H; B; F6 o* n5 y! `& v
6 y* q6 v/ K; k6 x" _! s
1 S; S v2 Y8 m3 I- }在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
" S! ~* ]7 @5 @1 j6 Q6 O% z) m. R# G7 d, x- ^ ?0 Q
" ^* X% t# L" G3.x版本CRS7 L z, |* J' _ o! f) F" {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; i; ~/ M( F! O. T7 j s% p# 生成例外排除请求的配置文件
. @# Q. u9 D- c% g' ]$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ ]4 L l% C) C: d) h$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' D2 Y8 t1 ]9 q0 u$ U
$ cp rules/*.data /usr/local/nginx/conf
4 \, s8 p1 P( C H为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
+ G9 j0 a- k8 ]' X7 S# x; W% B* E% ^/ z' V
* ?2 \6 I9 A# r, w7 Z+ S1 M; e+ _
$ vim /usr/local/nginx/conf/modsec_includes.conf5 z- D% Y$ v4 H. C1 T: }
! N O: z' P, j- `% A& p7 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 t# h- g ?2 ?( Z
6 K7 b, J9 R- A% X/ p6 g3 I0 g$ F6 j注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 t- U; \! }/ @+ l- c: p6 A
& Y1 q$ u# w2 |/ C. X+ ?1 [ T
; O4 E# i* N( [$ u配置Nginx支持Modsecurity
$ w, T x9 K8 r; \启用Modsecurity( Z! ^1 C) O; C( r
使用静态模块加载的配置方法6 C/ ]) } ]' [
在需要启用Modsecurity的主机的location下面加入下面两行即可:
; y" a3 L* k# o l1 x: l% N/ l4 X# U/ r! o/ o: u- j2 Q
# E0 y$ C M5 Q( I4 \ModSecurityEnabled on;8 e. `, u4 j/ u+ V3 R5 r0 c
ModSecurityConfig modsec_includes.conf;1 z- V; ]2 s' H" r% [
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 M! {$ b3 u: C. V
3 G5 I g* Q9 Y$ v. t% n5 F3 v
$ vim /usr/local/nginx/conf/nginx.conf
) D9 V* `& |8 X% U5 x: l. `# v, ^9 G, _ t! s
" g/ R! ]- M* {4 i$ |
server {
2 u0 P! f* |" ~0 o9 g3 b listen 80;( F$ @" J& ^7 L* G
server_name example.com;
1 B( R# c0 g8 B( K, g" l3 S& M+ x1 ?" U9 G# g$ F/ T F
/ c' c! e( a: t$ n0 X! p z location / {
6 Y& Z! j" I- M2 [- m ModSecurityEnabled on;. ^! ], ?$ z" I5 \
ModSecurityConfig modsec_includes.conf;4 o- g: r. u3 p! J
root html;& I3 P! h$ j/ D x# p
index index.html index.htm;
$ w9 E. G6 e( R% ~! N D1 f }1 g' V0 X9 f8 i& O6 Y, p2 ^6 p
}
+ X6 { n6 V; k" u" f4 X. ^) k使用动态模块加载的配置方法; J D! K% y, C" v# @" q2 O
在需要启用Modsecurity的主机的location下面加入下面两行即可:/ O, W, Q* |3 L ?, U) p
" M/ x. D! s4 ^6 C! I5 f# p
' d7 A9 F1 e2 m: C; Wmodsecurity on;
& w5 n% I: S; e3 E: }8 f" Dmodsecurity_rules_file modsec_includes.conf;4 i3 S; s$ g6 _) l: m" C
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( T* {3 a3 ^% U2 ?8 @4 U$ w
1 M. S3 b8 P3 q$ P4 `; a+ e) {. k# l; z- B- |$ M
$ vim /usr/local/nginx/conf/nginx.conf
$ ?1 k" Z( {& Q7 m, W t8 x1 C% E. {, t' r/ k* q. _
- P* B# N; x8 Dserver {
4 |8 I' [0 ?* R- L0 s listen 80;
6 r2 e8 G. p9 j' R server_name localhost mike.hi-linux.com;$ W4 v7 Q( q1 S& ~, F* r
access_log /var/log/nginx/yourdomain.log;
! d# @2 Z8 M' j8 d. w z7 a% M0 ~) N: j8 G, ~; h
9 d6 R9 s8 ?7 d9 l! }! c
location / {% K0 v; |4 G$ h, Y
2 u2 ~ F* K% _9 q6 g( Z
8 }# P3 r' _0 k: W: t0 W6 J2 q3 g
modsecurity on;; D6 E/ ]* n9 W: N& o9 L5 @) W
modsecurity_rules_file modsec_includes.conf;7 Q, Z+ e! R8 s/ \: L2 j
root html;8 c3 I3 ^7 m' t- _2 r: s
index index.html index.htm;
- A8 y0 {3 d8 A+ v}
' i) u& {$ ?9 @: w) P4 m* T}
0 R e4 [# t. W4 n* |- z验证Nginx配置文件2 N. x' Q/ u" ]
$ /usr/local/nginx/sbin/nginx -t& i, j; ~% \2 H3 n" {' p/ Z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- y1 c" }0 T! N+ V4 a" i1 x
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful- h5 a; J, P1 i
启动Nginx. P, g: F5 ^/ U
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf% g. n8 o2 y2 q2 P
( U# w5 F7 a; K3 m3 ?
测试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能够战胜更多复杂/未知/混淆的攻击模式。
4 K2 v9 y2 f7 j2 n( E$ L8 r |