ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" n p0 @7 }* e3 n3 v) \6 Q) b
6 U; H8 J9 x( k1 ~/ F* l. ]' i; j) M1 _- V' c# c5 p
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
+ b2 s5 C) k: D" I, v
4 b9 {+ I$ z: c3 X8 y3 ?" l1 @* u8 I, q- X0 T8 P
什么是ModSecurity6 D9 {1 ]! }4 F o
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
# |$ A; u- z9 n5 j, W
g, ^$ \: _! V' u7 D) x6 n9 L
r$ C7 W& L- l; s$ n5 V% HModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
+ S2 l) D3 I6 @! q6 n& m8 {7 I1 n' F. P% U; {% v. k/ y K! p6 ~& k9 [1 L
9 {8 u* U% C8 W2 M; u3 PModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 k1 i4 ^) x T" Z* R$ T' b6 `
+ P% R. n: E8 G, [* ~7 c. e& F1 I* p- i" U- I3 j/ O( E# Y
官网: https://www.modsecurity.org/
; p+ Y1 ^+ n$ D+ c1 L. t) c0 |2 v, k* b9 i1 W/ E( b9 _! |! N
" u+ s8 U, A' ^- `$ }9 K
什么是OWASP CRS9 i, G& w) f; [4 J
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。8 K3 I/ G& F) j* a+ y8 S( A
/ l- z( e, G1 L: z. L f5 o1 j5 Q3 Q, F& z1 Z9 c- A0 s# L
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 q$ G8 X7 k! | X, _7 N; u- }$ c0 Z- N3 Z P3 T( r
! V/ E5 f3 N2 n! pHTTP Protection(HTTP防御)
% V1 A. p, @- o0 y5 q: l6 ~" w2 j. P& |HTTP协议和本地定义使用的detectsviolations策略。: w8 | j/ |) N4 C
% v e* D7 o4 V2 @" N/ _3 \; q( A- \- I, ]' i! B, A" ]
Real-time Blacklist Lookups(实时黑名单查询) Q1 j0 D. a& o& |
利用第三方IP名单。2 k( O' J2 c h' _2 `1 `
# c. _1 _9 W- `0 M a" l+ j
2 t. W1 e5 ]+ d2 h8 q2 LHTTP Denial of Service Protections(HTTP的拒绝服务保护); S# e& b% U+ Z. d0 h5 d
防御HTTP的洪水攻击和HTTP Dos攻击。
) ` Z1 V7 W4 [/ ~% U! A- q/ ~
; N2 f: G; s, ^; [1 B1 B Y- {3 U. v: Z+ C* U. U" x
Common Web Attacks Protection(常见的Web攻击防护)
: K8 H: g* W9 R; u检测常见的Web应用程序的安全攻击。( h+ e$ g% s6 m! S1 L9 y# F
+ ]6 p4 V# X1 C1 e: h, U9 Y0 [, l. H" j1 f* |" l. Y& N3 t! I8 \
Automation Detection(自动化检测)
' ?9 s/ G" L& Q- w# r- S检测机器人,爬虫,扫描仪和其他表面恶意活动。
* _) i1 N% |+ }9 z+ V7 a9 v! s
% ~; L: V8 W6 ]( x0 I! G0 o3 X' |7 h; d% w
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)! Q+ r& x1 [% f/ U, O* ]4 L( t
检测通过Web应用程序上传的恶意文件。
7 _) F% m: M; Z* c0 @3 t. i/ b1 D- L2 _8 _* U2 }
$ a( p# H: G: w- R8 ?Tracking Sensitive Data(跟踪敏感数据)
9 v0 F" `' X1 `; F6 {3 b信用卡通道的使用,并阻止泄漏。 n7 |7 f, L4 e: T
: D, r* H+ U7 g4 j0 m$ ]
& ?# Q* D, M# \Trojan Protection(木马防护)( k7 y: @* Z3 W1 t" t- w5 w
检测访问木马。
% }0 @6 E) i2 M2 m5 U }3 b$ `5 P6 R( l% k
/ T% d2 M$ G, |7 n2 J, t+ w( W
Identification of Application Defects(应用程序缺陷的鉴定)/ N3 P0 w i' C$ S
检测应用程序的错误配置警报。
; e3 b4 v) _- Y* P# J9 ?( P: R- o6 C2 u
8 H8 z( R Y+ N( t% ^+ S& ]Error Detection and Hiding(错误检测和隐藏)8 Y! [# k9 T4 _3 ~0 x; u
检测伪装服务器发送错误消息。, V4 }1 a6 m9 P0 C9 ~; u( c
" \: W; L3 [/ [) g7 d* v% P3 _8 y* D
- `# {: {9 D9 j# A$ Y! D) n
安装ModSecurity7 i1 g3 E( a- S% e2 V) w% k, N
软件基础环境准备
% u# C1 d. e* ]2 ]下载对应软件包
" q4 e& G1 y' J( N$ cd /root
/ G) S3 M' H9 D& s; S+ o$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
8 @: f( e6 x4 K" g5 `6 p$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz- w. p3 g+ }, X# }) [+ N
安装Nginx和ModSecurity依赖包+ B7 ?6 u5 {" J2 d+ F" ]: W
Centos/RHEL* V1 l0 ^; I- c7 [# ~( ?$ o. X
+ ~0 @- |' n$ z% S- ?
( e- U2 x* E F. q: b& U1 F2 q0 b$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, r& s0 Y& B/ d1 N5 Y0 N* X
Ubuntu/Debian
- t$ k. t% _9 ^4 v
0 u: ~' T$ e0 X5 }; f0 B
3 }- g# ^( I$ C1 z3 h0 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
. p6 m9 E) h* X- y0 Q编译安装ModSecurity
3 |. I; {, ~5 e& s2 M) H5 gNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, n( g1 b$ s5 L8 v
9 [1 P: b* b. X5 W1 y' v7 t- {/ ? b) M- c& r7 i
方法一:编译为Nginx静态模块& Z: q, d9 }$ [. h
. P* G3 ^, s2 N' n6 x2 v
' v+ a1 E+ T/ X2 T0 X: _. x编译为独立模块(modsecurity-2.9.1)
! D! ], j# @3 L/ K5 R; Y$ tar xzvf modsecurity-2.9.1.tar.gz8 i0 L3 v3 X0 d* Z. ~4 P
$ cd modsecurity-2.9.1/+ `% ^' L+ F5 e' `
$ ./autogen.sh
$ Y+ ]4 f7 j& e/ k3 x! s0 I$ ./configure --enable-standalone-module --disable-mlogc
3 u& B9 @6 L: l" i6 G$ make
) D+ M* ]2 Z& c; P编译安装Nginx并添加ModSecurity模块
) |6 A+ C9 S# r$ tar xzvf nginx-1.9.2.tar.gz
" \$ `" a5 ]; Z& _+ }6 G1 b, ]$ cd nginx-1.9.2
5 K& r9 N- }6 l& Y/ l6 z2 P$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" C A1 `2 V" p
$ make && make install6 T- m/ C0 z2 Q! {9 n' ?3 S
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 Z* ^, X8 t' A& e0 F
3 `$ ?+ O. U* U2 ^
0 e1 k; Q/ ]0 s. o+ L编译LibModSecurity(modsecurity-3.0)
1 Q8 M3 [+ P9 U" w8 q4 k$ cd /root+ u- U0 Z1 D8 \4 y8 @% ?' s
$ git clone https://github.com/SpiderLabs/ModSecurity
/ q& \) X& v9 Z- E: z$ cd ModSecurity
( `$ }0 w8 R. S2 r C$ git checkout -b v3/master origin/v3/master
; _4 r; d' u% Z3 V$ sh build.sh
# S/ s( m9 Y6 Y4 M1 g& j$ git submodule init
q& {# [3 A% ~$ git submodule update
$ g9 b0 q- i& z) V9 ]$ ./configure
6 r* G3 q' _/ n$ D$ make/ ^. c, p0 H' g2 G8 y" y) V$ c# O
$ make install( L3 z# e- ?3 D& A* V
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! {6 u A5 W+ k9 s
1 Q, g& T( H7 ], p% n6 W% m a: _$ @ t
% [ |( r0 E+ h2 ~$ ls /usr/local/modsecurity/lib
# r* n: G$ z$ P, jlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0. J) e5 v9 a' ~
编译安装Nginx并添加ModSecurity-Nginx Connector模块
$ k3 z8 G/ Q3 @) ~- L使用ModSecurity-Nginx模块来连接LibModSecurity
& z, }8 O% g& d8 y p, l" b" k1 e. z, s( @0 l5 ~; d# r6 q U
" {. Z6 U4 X+ g9 B$ cd /root5 ?" f1 w' o1 {) U* J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
2 p# {! }5 B6 n/ R* Q/ p$ tar xzvf nginx-1.9.2.tar.gz) T" i* @' N4 ~) b. A4 }1 t
$ cd nginx-1.9.2
9 J: t4 H( O3 V$ ./configure --add-module=/root/modsecurity-nginx
; _5 Q7 ^& M/ v3 s- q$ make
1 K/ X6 I. j/ ~! _5 o( l* q$ make && make install7 [: i9 l+ L. K. H3 K9 \# j
添加OWASP规则; T+ W; _# Q: `7 a
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
2 R2 |4 r% N+ y$ ]% q: i1 b: d, g- ], [! l9 H
( n8 B b6 Q6 N下载OWASP规则并生成配置文件
5 Z) S O( A3 t+ y& {$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% ~' N" T5 o- q5 Q7 [( F
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' a2 A! ^ S$ m/ m7 j) [5 Z& L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ T. `# `3 y( h% H% a% }$ cp crs-setup.conf.example crs-setup.conf
8 P5 M% w, O4 Y j$ b+ t$ i8 h配置OWASP规则9 h" I; t6 E- L1 s4 A) H. C
编辑crs-setup.conf文件
- z* p' r6 e/ i4 D$ x" {2 c5 _, H/ Z1 h. P; J1 i4 u
9 L8 C- K" k0 X3 A1 Z/ X# X: g$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 u. A: X; Y; F* i2 t$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
1 W, J. R) w: v4 w- \- u, S: V7 @$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 I" \2 n! Z7 p7 t; a$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 e' Y1 C0 r# q( ~默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. l9 [6 O- I- f1 r9 q
+ I) ]; K% j J, Y$ H( V3 N: T( |" |
启用ModSecurity模块和CRS规则
; o6 e( m& P) d# K% k1 B6 @复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' g" O2 t" [0 N9 m! R, R" y: \) l
8 P4 I# s2 r/ N9 Y. L0 F$ w3 W5 p
1 n6 I3 v; v) K6 O" @& |modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。7 W$ T" {! H6 r* m9 Y; A9 k
! @1 M* T$ a4 y2 k: J
& o9 i' p2 h$ _; `$ cd /root/modsecurity-2.9.1/% w$ R2 j3 ], P1 j! u
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf / G# r9 d1 U& u# G0 m
$ cp unicode.mapping /usr/local/nginx/conf/
) w- W% F; |2 v. D0 y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。( w( i$ i7 R0 y& ^6 @& I' f
# {5 n7 E& j' l+ Y
6 N$ P% s# N" `! l0 ~6 I" X$ vim /usr/local/nginx/conf/modsecurity.conf; y6 L. Q4 k6 l$ U+ ]
SecRuleEngine On
8 Z$ V, E; h& a8 g1 } LModSecurity中几个常用配置说明:
7 p3 E+ ?$ X/ d! A$ K
s) u1 G, p$ @5 _, H; t) \! z2 T$ m: ?3 x% v- F
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
5 q! c; R3 k2 a0 T1 G
# f$ Q' ^' c; w1 B- }, z8 y. h2 Q" c; o& Z
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。6 [. U* J+ s4 v8 t
7 k( z, k6 M i
8 f/ e6 q: M5 X# V3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。/ j/ L. u( f& x2 k
3 [& g, L" j5 n( d$ G
w! [: c) v: i' } a/ ~5 x5 G4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
3 ^2 {2 a# n2 D: N$ K: f; M/ y& H. x) i$ o2 z. E; P1 s
. [# [: u n0 a' L在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 A5 S! a5 E& A% [* ^6 m9 }
8 ]6 k! }7 F; k; U0 o* R2 u' j
9 R/ W) o4 Y* D l9 m" {
3.x版本CRS
7 \3 f# r8 |' `- B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 p6 P# ?' Z8 `/ e
# 生成例外排除请求的配置文件
5 Q9 h' n1 v9 M8 h" k5 ^- q' {8 f$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf* Q5 ]0 d# Y( S7 v: @
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
4 G, f: [& ^" U" _& X. c2 Y- a$ cp rules/*.data /usr/local/nginx/conf, V& E. A# O7 a% _
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* F; r- T+ j* s# v0 P/ n
7 d& x5 S# ^/ l5 |3 i0 E
7 f2 I5 [+ z# i) y
$ vim /usr/local/nginx/conf/modsec_includes.conf/ S8 k( g: a9 w* M9 m6 j/ m* y- b
& i" B7 F! F# o7 b1 ~7 c
[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 N+ e5 z3 |# B# d; v. [
3 ^) X$ {. h* t( ]2 W注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
6 i) e0 c6 b% O2 |1 h: j$ b9 H: p4 c/ r# d: n$ S, j
5 T5 D* s M' C
配置Nginx支持Modsecurity9 X4 o* |9 I3 r' P
启用Modsecurity2 Q% t# w7 _) ]
使用静态模块加载的配置方法
( w& R" [' z9 L# ~在需要启用Modsecurity的主机的location下面加入下面两行即可:
& Y$ E' f5 t/ l: N4 ^1 p+ j$ }5 }
& ~6 m3 R9 \3 a! k- T, A2 s( w' j
ModSecurityEnabled on;
# B2 g/ g& E3 m" BModSecurityConfig modsec_includes.conf;9 W! W1 R! Z' u& T! k: \" g/ v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: o( P' i) u1 ?: {" d' F5 E
9 M& J, s% d' R
3 @% S9 L. O9 T4 b$ vim /usr/local/nginx/conf/nginx.conf! J; ?- K! I2 i, X7 U0 q9 e+ u
" j- u, ~' Q9 t! d8 |/ c- O' V
' O8 F* M3 z- l- dserver {
) C4 n" U+ x H# Z5 |( I9 k( p% C listen 80;8 c4 f7 B; _! f
server_name example.com;
! a) P' U" J Q1 E1 J
- W+ [6 X. `; Z8 g- g
- z5 F7 n7 Q: t location / {2 m) N5 _; f' y |5 o+ i4 O# w
ModSecurityEnabled on;
" l- }# H1 C' ], q, q+ }! b ModSecurityConfig modsec_includes.conf;9 Q7 S, b5 V! j% u' D# H4 B
root html;
2 E P9 T8 s4 Q t v8 I index index.html index.htm;# O& P% }. e8 {- Y) r
}
% a: Q) y0 ]5 ~- p}) O$ r$ N& ^" I! |- t" |
使用动态模块加载的配置方法2 R7 ?" o7 z/ M- h
在需要启用Modsecurity的主机的location下面加入下面两行即可:( b( b1 |1 _% T
! ~: \6 {& n/ G7 I0 ?$ u. f
, S+ i9 k: V/ }1 x, Umodsecurity on;
7 u! g8 L5 Q- k; f( X: rmodsecurity_rules_file modsec_includes.conf;
3 x3 ^9 h1 |9 w) t; g! A0 a修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- j% y6 s: W N# a
: H' r) f) B( t8 J% \' J3 ~
6 B" O- n7 p* V9 t8 V- i5 D2 G @$ vim /usr/local/nginx/conf/nginx.conf
% q$ H- w- I$ j
9 B, i# f: [; P: b3 W3 z
+ }/ o, ]- s& g7 L/ jserver {
/ U* C& T0 _1 N5 i0 \6 { listen 80;
3 h( G9 P. s4 c1 g: d: n6 j server_name localhost mike.hi-linux.com;
6 v* v4 b( d' @& [ access_log /var/log/nginx/yourdomain.log;
+ p! O$ c; Q! q) P! V j! R
" f$ M. x1 H" [9 d
6 `8 `, w7 c! ^& g+ Q: q location / {
+ I1 r: H+ p2 X3 L9 J
' c' t0 Z% K$ X! y( `
" {! ]$ F5 v6 a7 Z& R5 |1 R modsecurity on;
4 Y* Q6 m' U7 r( m modsecurity_rules_file modsec_includes.conf;
/ H7 ~7 F- C- D root html;5 `8 O+ z9 H _8 o3 b# v4 [
index index.html index.htm;
# E# Y/ P' |+ e, k, f5 q}; t G! Q" a/ Y' D
}
, Z3 \$ e/ E4 b, h7 g x验证Nginx配置文件2 d) m, f7 X2 H; g4 a6 E n* N
$ /usr/local/nginx/sbin/nginx -t
" u; \' a$ c; N; \- K5 @1 Inginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) t, e3 x% m) Q0 E e$ a: ]( D
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful. Q, e9 B: C/ b$ X4 I
启动Nginx
) Z$ h9 W3 R" c- [( `. @; i$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
: g1 Z* E( a7 L" \' c
& o2 T/ d/ x5 V- }0 C测试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能够战胜更多复杂/未知/混淆的攻击模式。 % q; T+ Q5 T; g" K
|