ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
1 z: B; Z V$ G- b- \
/ ? V" ~1 h3 T6 A7 @3 v7 g. u1 v% A4 E
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ h0 \' m$ E/ I4 [1 ]3 U
2 R% U8 {% d9 m) H1 x& Y: v3 _7 P3 d: @
& A `& M9 C# h, s L8 n: Y* T什么是ModSecurity+ @( D9 r# t& O, v
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。& W# |- f- C: c* j
9 e: _+ W+ t% z2 O9 e. B% S# F5 J* f0 ^" J
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 b# \& r' n. ?! i: h0 q% ^4 T+ E b, n5 Z0 G; [" r& r# u
8 i. v. O4 R( F' NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。' [3 G8 d! H% w- T$ \ H% F7 E
" D3 o9 |7 k% m8 i5 ~& i
! d$ M* ]1 q% X( N" _" c& ?0 N官网: https://www.modsecurity.org/
. R7 I: F: F4 |
% {$ {5 [# C+ \' l6 f7 O) J1 i5 [: ]" v8 _
什么是OWASP CRS; _6 k/ t% r8 n& Y
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。2 r: S) r9 |$ `& q. d7 A
2 [5 v" y& w' w1 m/ O
" j3 y1 K' r8 p0 U, k
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 y0 T$ [( Y6 u2 E" _! A8 r
5 H1 L. w* F: Y8 I+ g! X2 B* X. x- M4 }* a; ~: O# Z
HTTP Protection(HTTP防御)
* g8 J$ O8 \( x! h, wHTTP协议和本地定义使用的detectsviolations策略。
, m- k" I8 P T8 I8 {/ X% B; G' f7 ?$ T1 z; c# X; |5 K
3 n4 k! v1 r% C+ S% f' I
Real-time Blacklist Lookups(实时黑名单查询)- _: ^8 G- U- P) u# Y& a5 y8 J
利用第三方IP名单。
/ {3 H! @+ ]8 f/ C8 E7 R0 I, q6 ?% o6 s& Q7 ^( o% i# L
+ Y% i- m9 h% P G5 ]- H9 fHTTP Denial of Service Protections(HTTP的拒绝服务保护)
8 \' T5 ?3 {$ O9 I防御HTTP的洪水攻击和HTTP Dos攻击。/ g. l* k* v4 }2 H/ L: ~8 l! v
' q- I* K# g7 l/ g$ y* b# R( r: {6 s `! @- I' P, H6 y& q( B
Common Web Attacks Protection(常见的Web攻击防护)
/ d' i, Z; _, ~0 O: R检测常见的Web应用程序的安全攻击。
6 ]6 [. A9 b4 s9 U8 c3 C7 W+ p. l$ n h7 l+ Y% ~+ b; ?. @ H
' ^3 [7 g$ z. v" p
Automation Detection(自动化检测)0 o: l; N+ y. @9 f( `
检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 J9 z t( b& `/ b
% R% l0 m) i; V/ X- P9 `" q0 R; d6 n4 Q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)8 W1 R' K. } s" K7 n
检测通过Web应用程序上传的恶意文件。 f0 L7 X+ w; L! T3 e) v9 x, y; d- S
& N3 {3 U+ h: F
- N7 o6 I- I) }$ q
Tracking Sensitive Data(跟踪敏感数据)
( i! _. x% V. ^4 }. b信用卡通道的使用,并阻止泄漏。- o! d% ~$ l9 g) l
& L4 I: ]$ F `! ~' f# ]0 g/ z* ^& C2 E6 t
Trojan Protection(木马防护)( f( H0 m g. _4 V- K" ? V
检测访问木马。
; j; m7 c0 i0 V: `3 C) M/ \5 v6 O2 c% v( `" U3 I% B
! @, Q# w% K) e* o0 R" X
Identification of Application Defects(应用程序缺陷的鉴定)
0 x$ v- Q% G& ?+ U1 C检测应用程序的错误配置警报。
( p, P2 U! |9 ]7 ~1 F1 y3 G( O3 U5 p+ g5 `! h; K! m h# O
& a8 P( I4 P' X6 ]* hError Detection and Hiding(错误检测和隐藏)
9 ~/ z" B; _7 S0 X1 _" W检测伪装服务器发送错误消息。
7 ^5 p5 M. S$ F; [' }1 K$ G1 m* O% |1 w- V, r* {" W
; B! j) m: ~6 |, P
安装ModSecurity' G/ R7 e9 e) L) [1 T
软件基础环境准备
) @* r9 z# f( q- t0 O' c下载对应软件包) N2 G8 m, `: n1 N
$ cd /root' |7 q2 T2 q* D1 b7 q
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
5 V8 i% I% v( g4 V4 A7 ]- Q) D# n$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz- @. ^3 j6 s' Y+ _$ v# Y9 c
安装Nginx和ModSecurity依赖包
$ @7 e; {5 ` m" n) ^Centos/RHEL
2 i0 ]) ?1 o6 M" g5 c8 O. b
: Y$ K7 J7 c- i$ N/ g& m- q3 S) m+ q4 s& y9 u w
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 ~3 d! d/ J6 n
Ubuntu/Debian) Z1 d& O& A1 Y$ G* ]
5 [0 P4 Z1 u7 m. N& v
; {+ Y+ n& E7 ?' O$ 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- Y4 ^6 n! a7 h- m$ k
编译安装ModSecurity
/ J& l* ]& q) ?. X4 Z3 v# \Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# X) w6 _2 y& h3 Y Z, ]0 E& f2 o& v0 \* I a4 J
" U3 N' E7 g2 \" L
方法一:编译为Nginx静态模块5 `9 b/ r) x8 e" x, J" N$ y
/ Y3 j+ {. J" T4 P' e3 C4 a1 f' y9 M2 W& p) l! T
编译为独立模块(modsecurity-2.9.1)
, p( f! c1 D, c7 n( `, a$ tar xzvf modsecurity-2.9.1.tar.gz
* Y0 C* T8 v" H' E, ~/ _ z$ cd modsecurity-2.9.1/* e& S2 z9 W n% c) t* S/ Y1 X
$ ./autogen.sh2 }9 K/ c% Y& L
$ ./configure --enable-standalone-module --disable-mlogc
# z" k1 ^" Q1 D- M$ make
" ?' F5 M8 h+ U9 ^8 x8 g编译安装Nginx并添加ModSecurity模块
! h( p* D7 I2 S1 A! {1 F- Z$ tar xzvf nginx-1.9.2.tar.gz( z0 M$ d. P& ~& V9 p0 H
$ cd nginx-1.9.2/ ~- G. x) V+ ~( {( U
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/) t! R! m7 D) o4 F8 t+ O
$ make && make install
) J9 W8 ^% L& }+ m7 }8 R+ j; h方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
: C( X4 x1 X4 ?# @5 |' J! q* j" p1 [- |6 w8 B( \% d
. @& Q3 u$ X+ k2 D编译LibModSecurity(modsecurity-3.0)
1 F* k5 X% s" O; ?7 z$ P2 [' n$ cd /root
( Y, n& F( Q6 w$ git clone https://github.com/SpiderLabs/ModSecurity
6 t5 e, ?+ I9 N, O+ l7 J$ cd ModSecurity# e: C+ K6 o& S% B! O. @3 u
$ git checkout -b v3/master origin/v3/master
. q2 T- ^" i a- ^ z9 ^' @$ sh build.sh1 U, C9 i f* [' t, L
$ git submodule init
, }' G! c9 J0 w/ w$ \$ git submodule update
* y/ G& ~1 ]* B$ ~- U$ b1 \$ ./configure
, ^% z5 u5 K; l0 ~4 B$ make
! Y! `5 a6 O) N. x$ make install
% a5 o. J4 k5 ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。7 A2 N) ~" F, M5 i
8 V- r% ^2 i9 J5 v$ ]
' z4 n& u9 N3 }& X; @* E
$ ls /usr/local/modsecurity/lib
+ v7 P2 A5 V# ^$ E6 M3 _2 ilibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0( E! V2 h: A( X2 v3 f3 S1 t3 D
编译安装Nginx并添加ModSecurity-Nginx Connector模块
5 K8 x0 C# M1 U2 o4 q使用ModSecurity-Nginx模块来连接LibModSecurity* Z; P) n1 J1 i& R
' @1 h$ w4 o4 s/ c+ m0 F
1 T m* k: b8 |6 e1 v" Y7 q
$ cd /root
/ `: [, \' Y4 R, {8 q) Z9 [9 Y8 |: z$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx5 I& @1 T1 w w9 M2 ^
$ tar xzvf nginx-1.9.2.tar.gz
; `+ ~* V& V: P0 x" E$ cd nginx-1.9.24 z/ P" v) T q9 x) Y! y* D
$ ./configure --add-module=/root/modsecurity-nginx* n' |2 W+ V8 R& Z4 u' q
$ make
S7 D8 U0 w8 s4 o' S: b& ]$ make && make install E: G t- S# N' [' @" {1 z
添加OWASP规则
+ S6 D* d, a( q' {ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* X; ?' e0 q* P; J) B; I' t
& U9 u: O* q$ {
" a5 c; M& l" x7 ?下载OWASP规则并生成配置文件
# {/ M/ ]# J' h" o$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
, c& k! I9 z$ a. F( U2 s8 k$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
" s8 K6 q' m' _" V6 S' c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ G, E3 Z+ u q8 a$ cp crs-setup.conf.example crs-setup.conf
- T3 q# A% p- Q5 c! Q) N) |- q配置OWASP规则
. G# Q d' O, ~编辑crs-setup.conf文件
7 H4 o- y- h# |% w% G. e. W: a8 x1 p z& U
" n1 L. N5 ^% l# i$ _% l9 L1 T4 z$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 j# g2 J0 a; B5 w9 S7 T$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf6 o% p" u4 V- Q1 T
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 e# }; ]! u: ^: [% n
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, |& n* P" b3 }0 o J# K默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
7 q& ?7 l8 {& Q
; Y* p2 ^4 v8 u' R! a6 I* ~1 {; `, {
8 i" I; O# l; E+ ^5 p% ]% S启用ModSecurity模块和CRS规则
$ C; a8 u8 y1 b" v复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! F8 f1 ]$ h( C
J% l* h* o9 ]# u4 S; S4 z8 [- f v# X7 U6 }, a
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 N$ I4 I9 l/ ?$ Y: o
+ H, T2 h5 u- L% T5 p
g3 }8 x2 f4 Q! x) l$ cd /root/modsecurity-2.9.1/
5 C5 Z* L5 `' j% ]/ {8 Z$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
$ D+ ]5 M' N7 B; M: j' U* V0 p! }$ cp unicode.mapping /usr/local/nginx/conf/
7 V& k, a+ g' h9 L& ]" R: m1 w; W将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* \! e! a% Z1 W/ I- _5 i5 O- n* \- |0 ]3 U ~& F! P/ N% H
/ H! G0 {2 Y# q& l. |7 y
$ vim /usr/local/nginx/conf/modsecurity.conf- m6 O* n# k/ u& z0 C' d
SecRuleEngine On( o2 Y1 W6 o! T$ p% X# Y1 `! _
ModSecurity中几个常用配置说明:% _0 Q/ d4 h3 e7 C6 C
* y: ?4 o0 k% ?
' z: l9 H5 R2 s' z. k0 q, k9 S1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。9 q' S6 Q4 h% |1 c/ I( p
3 m- g, U' U* v$ j" a' N S
0 k; m9 c0 Q3 Q# T4 S3 `
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。1 `1 z) y) k1 y$ Y8 K* n
8 k: T8 g+ U v) h# x
. F+ A$ ]! A& u! v/ W N2 v3 a3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。6 U% G9 `3 q0 s1 h5 t6 y0 _, z
4 c, o5 S/ B$ l, T! q' ^
, Q1 E. [# p0 W. L4 p9 Q
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 J+ C$ A( H4 x6 q+ v6 ^4 I9 Z
3 z Q# h$ {5 F6 k
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。8 z4 z% @) G4 f; A4 |; z7 |! E
2 [' v, }3 K2 N: f6 B. f
& e8 G. @$ {" I' i) z3.x版本CRS
) F- _2 W' w, `& i$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ o' v5 W* A! @" {; z q
# 生成例外排除请求的配置文件3 p) @4 o, Y" p) u, ~. o7 c- B: I
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf* k9 f9 H0 J8 C7 x" {# i
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf# x& y- ^4 w2 x& ]( @, {7 `
$ cp rules/*.data /usr/local/nginx/conf R! S! R- b2 s/ O/ z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' [& |8 x0 E8 A; \% [ p6 v, `" [
6 Q' o$ }$ z6 e u6 Q# l0 z. W
3 P3 _- Y4 o% _( A0 M$ vim /usr/local/nginx/conf/modsec_includes.conf7 D2 P) x7 ], ]# v3 W
5 q# _2 H/ L9 m7 R o, I' g& g" \+ s
[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 ) r% M1 e3 G) ?0 B
$ u9 J% C' d/ j. j+ v: a, N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. U- \1 _ @4 c6 ]# S; ?; m. z# C1 o/ a) Y2 m7 a5 ]
* O0 Q9 ]/ v4 s4 A配置Nginx支持Modsecurity
; Y: S4 f+ [% [: ?启用Modsecurity
) o+ F5 Y1 ~* j6 ^. s. _4 e9 m使用静态模块加载的配置方法 r& A; T8 M! i+ Q
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 H( }% c8 c$ C @' b
( G1 t W3 ^6 D9 O: ^+ f) x/ w* ?* r
ModSecurityEnabled on;
1 I0 K6 I# {* B6 {. o4 KModSecurityConfig modsec_includes.conf;
) s& T" Y# W3 g9 e! I修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 p7 P" G) Y8 ` E+ Z5 \& S
4 }2 ^& `7 L& n, Y, z4 s. {' Q0 c% I& @- t1 d, A# E" [
$ vim /usr/local/nginx/conf/nginx.conf
( E) {3 P2 V5 C' m! L6 O6 _
9 v, Z z5 b* F5 p
4 {4 M2 V& k, Cserver {7 Z3 D1 D9 T @. W$ h9 K2 F3 d
listen 80;. S! H) f1 h) O: R& z2 D% H
server_name example.com;) E+ @3 u0 \8 p0 c' E
2 T& f/ t) z/ M* I: t
) b; @, A& P/ [/ M$ [4 d4 @7 G+ B1 q/ k
location / {
* q: D1 q- V! ^3 N$ E# h ModSecurityEnabled on; C" [+ ~7 }: C6 O/ Z1 e( l2 _! j
ModSecurityConfig modsec_includes.conf;/ y c g, I/ |- Q4 D. v @# `
root html;
# f/ c4 Q+ @9 w7 m( j- @3 |. Q index index.html index.htm;
7 l' U" X7 o) b$ v b5 V; w }
; C" U" z3 \3 F- u y8 H6 Q7 O: w+ k}3 s& G, l' i$ T; r9 g$ {+ e, W
使用动态模块加载的配置方法
t7 u# a2 d& o$ N' W, R在需要启用Modsecurity的主机的location下面加入下面两行即可:
" ~" @& ~9 C. L* _: O4 C
2 I: b. h* y5 V9 I; P
Z* e4 F; q2 P3 A4 ymodsecurity on;4 c) C( V& U/ J) @
modsecurity_rules_file modsec_includes.conf;
1 V+ @! e3 b- C) w. q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% e/ U! Y7 [. L$ S+ N
+ y2 D4 U% o; p# d( I5 I) p, v2 g
7 {/ y( w4 t5 j {$ vim /usr/local/nginx/conf/nginx.conf
4 F! B! \+ P% R4 {7 E' i% v/ L6 O+ N' x
. U" U! S* T/ a! l, \
server {
% h0 G. Y: C- U( ^4 T. a. K7 N listen 80;6 C8 Z5 p+ u/ X% s: G9 G2 B
server_name localhost mike.hi-linux.com;* k9 l% s0 q" H$ P
access_log /var/log/nginx/yourdomain.log;; s' x- e+ w6 {: p
! M4 c/ {8 n8 _# @1 _# W% Z$ N
& A9 l1 n( O+ W' P location / {5 L( `- h7 t5 A" f" Z+ I+ h- H
' a t7 G! ^0 e2 L. M7 u3 |6 K
- f7 A: ]% ~ \5 }$ T- T( k modsecurity on;, H; y. U" V" \# |. z# d- P( ~
modsecurity_rules_file modsec_includes.conf;7 P1 Y+ t: ]1 R/ x# {- k4 o1 `
root html;; S' a: v" ^' r# I( X8 p
index index.html index.htm;
; ?; Z; O; A4 ]' T1 X* W}
6 \, e' i9 Q, n4 M}
; O9 e, o. I' ~1 y- \5 ?) p3 \验证Nginx配置文件" b* C4 i0 ~! I# d5 |; k5 w
$ /usr/local/nginx/sbin/nginx -t
) n& u1 `; V( r8 V9 M# G% Q. rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# I7 C0 M h9 v3 r8 cnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# ?# x7 U z& o2 n& z: _
启动Nginx
D( B8 E0 q2 `- M: E' ^( {$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& d; K' A6 N% {& y7 { t6 E* x
# e) }/ p9 F, Y" V
测试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能够战胜更多复杂/未知/混淆的攻击模式。 # i+ W- R% A+ ~, f) y' ]
|