ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 P, ~' F; g! [" o( {; g S5 e
, g6 O- s* K% k1 B/ K3 J3 `
& r. t \% @. R1 q) ?在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
. t/ S3 V8 V! W2 Z, H9 J0 r# \& b6 o5 O8 ]! y9 g& G
7 H( m2 S" ?% V+ B6 V# e# g什么是ModSecurity
8 @; Y9 w1 H5 k7 sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: \( y$ @+ P& w4 K% p- K
+ {4 L, v* l" y' g0 v: ?% c- h8 z6 h
! Q. U. t4 o* v9 A2 O8 g8 g. aModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。1 M8 u, S: ?" H( @/ g" Y: Q: d
9 K4 i0 a. ]4 r6 S. X5 p
* d& {, h; |# |5 f q9 oModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。, C9 E, t3 ]! `- B( `. |+ f8 ~
. T3 T9 x, H; l4 e6 d! V1 b
$ c. x& ^) O7 [) }9 j2 `官网: https://www.modsecurity.org/
3 z. a" k) g+ Y5 X3 t4 H5 n
, H3 Z7 w D$ P" n9 {0 i( r* ?9 O
什么是OWASP CRS- f+ A/ ?# ]* N' {0 L7 W' Q' i) B4 ]
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
2 \7 D, V d' S' v
" K: i# L- \9 Y$ x/ Q; @
( E& J- n6 F! V% |1 J& s2 o+ |* IModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 Y2 J9 Q# K! [: T1 s: S- J7 H! k, F, i% [ g$ @
' U# Q* o% T& w+ \HTTP Protection(HTTP防御)
9 C0 G. `, }. @% c( vHTTP协议和本地定义使用的detectsviolations策略。7 q/ e4 _" \1 F5 E1 A$ P. P3 M& w
4 U: H3 g9 R1 {/ a7 z- M) \( V# G2 i" @; Z; T7 G. y5 x
Real-time Blacklist Lookups(实时黑名单查询)( e0 v4 X; |" z, p) {* a
利用第三方IP名单。7 p# i0 R5 T& a
7 e# y* d+ J0 F% L: z2 Q
: o" L* k; U6 w
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
& y8 T' a7 J* z4 b3 N0 W防御HTTP的洪水攻击和HTTP Dos攻击。
* ?2 ]6 |& x9 }. U7 f6 i: I8 X6 w+ A6 ]
% I/ r0 k4 w" D
Common Web Attacks Protection(常见的Web攻击防护)5 W+ X/ Z6 E) v% z, O! R! C
检测常见的Web应用程序的安全攻击。
% o' z0 R! q% v! o
' W& Q% U$ w& @# F% ^
3 D2 Y: X( ^* ]' @8 M% OAutomation Detection(自动化检测)$ m2 E( y* Z9 f
检测机器人,爬虫,扫描仪和其他表面恶意活动。" d& `# e, ]4 G2 O' x$ T
4 c+ V5 A h- M! \! ~. ^ S, |) V6 E2 ~5 N1 J' T
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)% b; H' \& v @% Y
检测通过Web应用程序上传的恶意文件。7 w/ y6 h7 M9 A% b" E4 g" o
) E- F5 S v0 _* ^6 s& n5 {) g/ |; |
" u9 ~$ a6 R7 I; M: S W6 sTracking Sensitive Data(跟踪敏感数据)# K) g4 h9 P4 h/ ^* I( K
信用卡通道的使用,并阻止泄漏。+ o2 Z h0 U7 e3 ^2 N- {& }
+ S' y/ a0 |" f/ D& H
4 r" h. n& Q5 R- t. Y
Trojan Protection(木马防护)2 p) H" P' x3 ~* D+ \' T# C
检测访问木马。. M! G3 g* H/ a$ p
0 X [3 a5 p% D( b3 j0 K
& `4 z1 y! ~* _
Identification of Application Defects(应用程序缺陷的鉴定), l4 v2 a8 z4 f9 X) d4 }
检测应用程序的错误配置警报。
$ H" Q6 g) |# ~5 Q/ ^
- T2 ^0 b& Q) T& r' c+ m- \% G* g: _
Error Detection and Hiding(错误检测和隐藏)9 g! f: T: P2 Q7 y; }2 G
检测伪装服务器发送错误消息。, g0 N* n% ~% b
/ e& P# S& _( T* \. X4 S& A9 @" |- P: N. Z" x3 I7 y/ J$ f+ b+ ]
安装ModSecurity
. \1 u/ H# \$ P) k+ {# f1 R' {0 M; C软件基础环境准备
. S( |+ k6 n2 \- Q8 C下载对应软件包, ^# i% s9 R* J3 K" D" x+ G! Y4 |
$ cd /root
; w- ^ h$ r" Q; l; \$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, M1 w6 w. X5 Q4 [$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz+ d- K0 p5 a9 q7 f. _& y
安装Nginx和ModSecurity依赖包
+ _+ b0 k- }0 \Centos/RHEL
! X& l) t: T/ e( V3 i& W" t; d2 G+ x( z! O" x5 M
4 X- L: n0 m# P, v3 T# J* a8 O
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
1 I2 j. z! S$ ^& |( }4 M. V4 u9 lUbuntu/Debian
/ H8 Z3 Y3 j( @4 T
) M1 ?0 A/ h- ^' J: \9 S& `) J( L; C' U k) \% r" x
$ 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; T+ s$ w ^9 x3 f& `( d
编译安装ModSecurity
3 Z$ C* A i' @* r( E0 x5 L: RNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。( S1 r) i7 p3 s0 m) i: d) ^
" o* X3 _0 ?. R* }( h+ D0 Z7 z
, |' H/ p5 C) j" }8 X# _% @1 ~6 e方法一:编译为Nginx静态模块* g, `2 t! [# q- [
]: t( d; B9 O9 G2 h; D+ S
1 C1 {0 `/ T) O# j2 m/ t编译为独立模块(modsecurity-2.9.1)
5 q0 Q& G# J- D2 k. e5 l3 o2 r$ tar xzvf modsecurity-2.9.1.tar.gz& [' d% q8 a! e h( m
$ cd modsecurity-2.9.1/
; N. \- J9 Q! a$ ./autogen.sh
5 _' H4 x _6 i; D$ ./configure --enable-standalone-module --disable-mlogc
' L/ L8 e1 M, m$ make
! g7 |% J8 W. ~1 x# t- p) o$ b编译安装Nginx并添加ModSecurity模块) S, g8 E+ R# Z: f, N( t% ^1 i
$ tar xzvf nginx-1.9.2.tar.gz
" y7 o2 Q. c0 t; k0 R$ cd nginx-1.9.2
" a) e. L: O) k. N- H- E# M$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- I- O& ?& \6 F8 r: m' w" u
$ make && make install
% Z" d( b, w$ u方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
' e' u N+ O% H$ v* @2 e6 a6 ~( y; k& w5 r" n
* b* q: g$ O5 h4 G
编译LibModSecurity(modsecurity-3.0)+ ]# |3 }% r" x% u# p/ g( t9 a
$ cd /root
# N0 d" s: c" j; o4 `$ git clone https://github.com/SpiderLabs/ModSecurity
m5 H7 q, R F) P5 F$ cd ModSecurity
. k; h9 {, o+ Z9 F* r. A1 {% k3 w. U$ L$ git checkout -b v3/master origin/v3/master5 E# F+ V ^5 G" A6 _; y
$ sh build.sh1 Y8 \/ F& Q/ V) Z2 ^/ {8 a+ S
$ git submodule init
, M" U, h8 i- w! B. M5 F$ git submodule update
! }5 K; W9 g0 c& u; G) O, a; e$ ./configure0 d i% q U9 J: f% @& S
$ make- T+ f" ?6 G/ E5 X# U3 K6 R, E
$ make install
$ ~5 Q. h4 Q7 rLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ n% j9 L% c6 P/ y" l% S" `
: X# g1 F% x3 y( G1 `3 ~: e
6 a, j/ s) D, p9 I# q) n8 x$ ls /usr/local/modsecurity/lib4 @* Y5 @: q" U
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
: k2 u6 k7 \- m; v7 J3 h P编译安装Nginx并添加ModSecurity-Nginx Connector模块- |' ^- d- m/ x _( |
使用ModSecurity-Nginx模块来连接LibModSecurity
& U! N/ f& |7 ^" k6 f% F" w) v8 [3 m5 _/ F$ I# k' l
8 Q# D9 I# I4 m9 e. }, m# H
$ cd /root$ U1 m( R( Z( a% ?+ c6 p
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx- ~; {+ i$ ~7 C: `
$ tar xzvf nginx-1.9.2.tar.gz
3 ]0 q! y- |: u# Y+ M, y$ cd nginx-1.9.28 K! t% }+ Y! _4 I5 [
$ ./configure --add-module=/root/modsecurity-nginx
7 f8 M& @* M8 s8 w, L$ make
' q5 I/ A5 m) J1 M$ make && make install
& b, P1 B2 [, J添加OWASP规则
+ W3 g8 m9 B5 n! n7 }' A9 h5 EModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 o$ u. J, T6 m$ M) @/ O* y
' C3 R ~2 k# r! O, n( p* B+ D( s8 D3 J+ C' e: F" k, N
下载OWASP规则并生成配置文件
, }% X/ M# i! c; o$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
, j0 P; q7 M+ e- k0 H$ L2 |) `$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
1 z0 ]4 a' p# e; p4 n$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 W' e0 k8 B: s' g* a
$ cp crs-setup.conf.example crs-setup.conf
3 Q8 p! e* ]+ g1 f6 f* I+ K, J4 C配置OWASP规则
/ O w1 v1 P! P4 |: ~编辑crs-setup.conf文件
1 o t* @8 o/ _; b* y1 W; t; P) n2 }3 q6 O
% |2 E W0 D# D
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf$ Z% F7 Z5 K, X' l
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& i3 ~- t* |8 g2 f3 R5 @6 X: z
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
6 P- ?2 n T2 r1 k" E d% h1 O" ?$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
5 M$ v p9 i" d默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。1 s3 j; [- m% A H4 @; k$ B @. {
" V2 e9 i2 l% {% Q& D
# u) t" o% B$ F- {1 t7 Z( C! r启用ModSecurity模块和CRS规则
/ C, M$ f5 ~$ F复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" j; o* w( {3 r+ G' H( \$ |* B) v8 O# [! U
' s* \, [, m1 Mmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
( s( w# c3 ]: U2 p, N o
$ `9 c; T1 @5 P: F! j
! u0 ^/ Q9 p7 w$ cd /root/modsecurity-2.9.1/; e6 r5 v* B, `" v3 N1 w6 R! y
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 8 v2 l; A2 R1 T2 E9 P/ R- G) S. V
$ cp unicode.mapping /usr/local/nginx/conf/
1 c' i; F3 B) _ u将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 X8 x3 l* |8 n6 I- W. U3 E; h& {3 K, o6 m" U- c* e4 H$ E
2 N y. F$ Q1 f+ o. b3 G, m6 E$ vim /usr/local/nginx/conf/modsecurity.conf
* A4 v4 ]7 g6 v- i$ uSecRuleEngine On- G' J E% g( |* Q3 s+ F
ModSecurity中几个常用配置说明:
- K/ C) J$ q: i6 V; q$ Z
' N; Y6 X% |. p. m4 d! i' ~6 J j# k) f& A( O
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。) s, ~6 s4 A l& D4 J; }# d
7 c' ]4 a0 X8 R0 ?. ~( W) Q6 n% l2 C4 v Q, f, E
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, Z4 f4 i0 `, r: B" R- `+ q/ \
3 D3 Z$ A7 n. I3 _6 [2 o) j$ |3 d: H4 l+ _! F. j0 {, p" B% l
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。3 Y- x5 A/ Y. p$ P8 Q: l' M* R
$ n' r5 e# Y- F" e2 S/ l6 T- C- Z
9 T u) m' J' U8 W
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。/ ]/ ~/ k3 |* Z6 `" O$ J6 P
( L: g7 r: T2 b' Y3 u
( P6 z; x+ B) E+ \4 j$ S- Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- {+ n1 O) S9 _# `: _* @& Z
" J) l1 M& ]! a
' O; @4 E/ o' C* x3.x版本CRS W4 |* W5 O. n; g4 c7 e& q' ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ m+ U$ I5 Z6 s- p/ j
# 生成例外排除请求的配置文件
# `% k+ L; O9 |5 v$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf# b$ @1 N" Y( l5 N# \
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 n1 y" ], x6 s$ U
$ cp rules/*.data /usr/local/nginx/conf$ J9 S I( }6 t2 |
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* n1 S7 J, i* x2 b' a' ]$ }3 M
q6 E8 l/ q* v" _7 ?/ H
9 H; C* g) F( {8 V( _$ vim /usr/local/nginx/conf/modsec_includes.conf z) f' K) R% M4 W5 q% p
, v; m/ H* b7 t1 h) m9 C; \2 \[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
* \9 H( ]/ W4 B
* U) U; t5 A8 c8 \% @; K3 o- w/ r注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
3 c0 g1 ?7 U% m# b8 r. Q3 P1 u6 j t0 k! T( @' X
; W9 W" Z2 V; O' \0 s) g
配置Nginx支持Modsecurity
# k6 u; Z! D0 r2 T' j& p启用Modsecurity
' W$ H' R3 Y6 F# B6 n. \使用静态模块加载的配置方法
6 y G0 s0 u; X4 Y( w+ W- W, {' j* c在需要启用Modsecurity的主机的location下面加入下面两行即可:
& B6 n/ y, z7 A x0 a& X# Z4 p
+ g& r. {5 `4 A, y$ i$ W+ N% W$ F# q- {: W6 B8 N1 _) }
ModSecurityEnabled on;
8 O, H' [; ^: J* Q3 wModSecurityConfig modsec_includes.conf;
4 f) Z: `6 p4 v3 M7 V修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, w9 |7 B( t/ t
3 ]( L n/ s2 G" T5 \8 P4 S
# C8 X! j" H* t# X
$ vim /usr/local/nginx/conf/nginx.conf
) f0 n, h+ k6 g+ l6 Y& G1 [% ~% g
) n* L6 N% I3 j: ] H8 S1 T
* |6 \( R% n2 K! |/ @server {
: z9 b4 A( n7 {9 d) c' E( R listen 80;; s C6 ?8 p7 F7 z7 r
server_name example.com;& Y7 m; e( m" C0 @
- g) e" d% _# `8 D! X; j- t
. R6 `! a" S* j. y9 Q$ y% k1 _ location / {% b i7 {( ]0 k G
ModSecurityEnabled on;9 ?9 h: g" B; b: S% _+ d, e" Q
ModSecurityConfig modsec_includes.conf;
! r8 r/ w0 `3 g3 X root html;+ n( r( m g- z
index index.html index.htm;( ^* |+ G/ ~1 E; w* i; `: g
} A l; k6 Z$ t7 u) z2 r! Z3 Q
}1 P. B6 [5 y# b& u. H i) p
使用动态模块加载的配置方法) X% b8 w; a# x! R8 z) l) @8 ~, ?# H
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 g9 M6 ^/ K/ W4 P1 i, A6 o: f
4 R0 P' M7 j$ Q$ \+ I U: r( k# e' l( z
modsecurity on;+ @0 A5 u( K/ U6 \) h
modsecurity_rules_file modsec_includes.conf;
# H0 Z1 T' x% P3 e0 x! E# M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 i3 J3 |" L! F1 L1 y
0 ^9 O2 f$ J# R+ e0 K" ^) x# v0 E' g2 Y. ^/ n2 j9 } ^
$ vim /usr/local/nginx/conf/nginx.conf
/ N p9 ^# d. y( w
7 ~: K- w6 o% w" r% K6 U! P
; [5 ~% e4 v6 F! v- R# s8 d. a9 zserver {
+ H, f3 o) p0 v i listen 80;
) b- q2 J1 T. S9 a9 ` server_name localhost mike.hi-linux.com;
. _# X( g$ P% y% w6 `0 y- c. j8 K access_log /var/log/nginx/yourdomain.log;# [5 s# R& S4 h$ B9 x
! h' @9 k1 F1 m2 l( \: O/ `$ k( S, I0 x/ a: V
location / {5 x3 v! }/ K* \' f+ Y- s. F2 g
, a/ c: R0 G$ C" e% ~* K
+ o) [$ r0 ~! [8 V
modsecurity on;
3 B0 }; C7 @ O- i modsecurity_rules_file modsec_includes.conf;
9 X' L) M, G: u9 f root html;2 j5 W( {( E( w( [& `, V
index index.html index.htm;
) `. O+ j; s" l}
' ^. t' L. A- ?# I" m- B: d}! H. Q3 o }! F: V; f1 F
验证Nginx配置文件3 b; y. p. }1 u9 y1 W6 o( b5 p* |, R
$ /usr/local/nginx/sbin/nginx -t! ]+ d0 Y8 G. n, ?, `5 d( ^. A
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ \- Y2 f# ?( l5 z* S
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& Q# {) z w, I7 o% U& c6 E7 t2 C启动Nginx" y5 J, ^7 d g0 T$ J' F( }
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 l% |( Y, E% O9 I3 J1 c% {; r8 S
: x1 F5 |! ~+ b6 z6 u6 u8 u: T0 M& B测试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能够战胜更多复杂/未知/混淆的攻击模式。
& C% [* m% |7 V& v; J |