ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
+ Q3 w( R0 e I B# v
* `: T; s; ` I) q
8 s# S* J7 V( B/ x$ @在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 N. m7 q6 X0 Z3 W5 i0 r! d
3 R+ R3 D5 n" |7 k& O4 o6 M( A$ P2 T- M, {, I: b3 E- Z7 T
什么是ModSecurity
; ~/ A) R4 e6 y1 K" O) U# {8 [% tModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
: W/ c2 E; t& D, z& E0 [( _1 n) K" k, z4 V
5 X9 v8 ]! ^+ y1 ]% }; mModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。9 K1 L/ B8 z2 C$ j+ d# v% c' h
Y5 b3 F o% E6 h# ~6 a/ |
# Z- r9 k; ~! ^! J/ k! }( R) YModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。9 w, g% {5 ]! N9 F0 z
/ _2 g: \/ t3 ^) b: }
" t9 y4 s) g' c' O! ?
官网: https://www.modsecurity.org/7 G( Y3 H; j& o& o
7 y5 u- ^4 C# q. x8 o# ?, m( c& o4 E [, ?
什么是OWASP CRS* c4 Q4 t& `, h. F4 a
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。7 B/ ^, N6 P$ G+ b$ i+ e
- \1 x! _! c7 T
9 }" N4 j5 M$ C7 y) i4 j8 jModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: }8 X; E# s: K0 H3 v8 h- A/ J
& X; s' N6 ~! O7 S+ n. u: S: q0 @6 M$ v
HTTP Protection(HTTP防御): ~9 R2 A, }% U& R: k( [: w
HTTP协议和本地定义使用的detectsviolations策略。3 Z. o& S% t- l, Q" m. N3 \
* @# v6 _3 N( t+ J/ R% C
H) M. e& I* o3 C) i
Real-time Blacklist Lookups(实时黑名单查询)
0 ?( I3 @5 u/ @6 C' W. }利用第三方IP名单。
2 f9 E' H3 s3 |' N4 ]
$ G4 T# c9 o' r5 |& o6 ^" \3 P$ t k
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 l" D4 k! N; `* | V( G3 A防御HTTP的洪水攻击和HTTP Dos攻击。. u; Y; c( V8 Y! x; ?
1 u5 L$ i# P# f
- f7 |: m1 r: H& Q8 }4 ?6 D
Common Web Attacks Protection(常见的Web攻击防护)
9 K* |! r- n6 Z4 x, O) K检测常见的Web应用程序的安全攻击。
1 R% C& \8 k O* J0 q/ w) Y, s; W9 Y; u3 i
. x! k& G* s$ h0 S7 B- eAutomation Detection(自动化检测)
6 N' Y+ Z& S- E d v% D检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 ^& B7 b& F$ ?2 e: c2 ]1 S: P1 U# k- G3 P+ ^" @! q7 C6 R3 U
# y# z; n4 i' `( C9 B( ^Integration with AV Scanning for File Uploads(文件上传防病毒扫描): N( I5 o8 ^; D% a
检测通过Web应用程序上传的恶意文件。
: M+ ^, F7 S p( i
0 S0 r, x9 @+ r. t9 B0 Y2 u/ i6 Y$ p& J$ t$ e. q; u
Tracking Sensitive Data(跟踪敏感数据)' O7 N' b9 r# k. G2 D
信用卡通道的使用,并阻止泄漏。4 [+ q3 I6 }# u3 N: [$ G
: l* `9 t$ A1 f4 }" o
t; I$ C' E/ wTrojan Protection(木马防护)
# B4 A% o9 D; w. n# J4 z/ m1 S检测访问木马。" P+ ^" B( t, R
% X8 n/ v6 \: K
0 D2 O; Y8 g- d% w8 y! @Identification of Application Defects(应用程序缺陷的鉴定)4 h: _4 \6 f. s% F) ]/ _
检测应用程序的错误配置警报。 W2 ?7 @( \0 A
3 w9 u$ s9 x. L" i2 A1 e9 N2 K+ y* X0 t) u9 D; c7 @9 F
Error Detection and Hiding(错误检测和隐藏)" J" C' d- u' z/ P/ z
检测伪装服务器发送错误消息。
9 Z$ D6 k& m" r. |0 W `, I X0 \# I: g9 Z3 T7 U4 m9 Z
" w/ }$ [ c( H1 \6 R, A
安装ModSecurity
9 p4 v8 ?4 C* c& H0 \. s软件基础环境准备
3 q1 B% s0 f0 M2 _! H8 r下载对应软件包1 C; d: d, |4 {" _
$ cd /root
$ I$ U% K3 G, L1 k- V$ Q$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- i( [, C" X5 B4 ]$ t) h
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
% T4 K& O, O: k3 Z* y安装Nginx和ModSecurity依赖包
+ A8 v+ U3 y/ p2 HCentos/RHEL
: h% p# s% G4 ] M Z( p# \
6 `) m. J T& Z% I) W& N
4 D b+ s+ w4 ~1 l: L; E) n$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
s, [5 ^& o" G. `7 {Ubuntu/Debian
8 B9 l, d- w+ q+ q' c! D& x. b7 }' A+ m1 S6 k
( j9 ? c7 r L$ 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
( A8 a) }. n9 k& P+ S编译安装ModSecurity: f ~ j, g0 y0 O( ]
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# o! r) g5 G5 [7 j, g: h5 L; V+ }; b; D7 c4 t, m9 b
. v! E- \* d2 P8 D1 I% ^; n
方法一:编译为Nginx静态模块& I: _& `$ ?5 j9 W/ B
% H5 L- q. z( w7 T7 E# P( B' f; N5 }4 ^
编译为独立模块(modsecurity-2.9.1)
: q6 }4 z& K3 C9 k$ tar xzvf modsecurity-2.9.1.tar.gz
3 B( ?7 t8 u7 @2 p; h$ cd modsecurity-2.9.1/0 ]6 N( N5 ?+ X: N( h
$ ./autogen.sh
) d- {3 o% Y; b" G) V# `+ U% J+ g3 N$ ./configure --enable-standalone-module --disable-mlogc! d9 Y0 ]7 ~2 f b3 f2 R
$ make
7 c. m' H) Z& l# B* q6 d8 r编译安装Nginx并添加ModSecurity模块
2 ]/ Q! }$ S w! T$ tar xzvf nginx-1.9.2.tar.gz& ], J4 Y. T8 v
$ cd nginx-1.9.27 i9 v/ `" s* \# y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: o( @8 Z X1 \$ make && make install
* E( _+ Z3 n6 Q+ C方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. _9 f( o2 X( o( Q5 y. }% H# o* `: B/ s2 d- l" z8 M( B
$ }2 v) _7 W6 T& P9 V" m编译LibModSecurity(modsecurity-3.0)& T( j+ ]$ @- ?; v! c
$ cd /root
: Z3 u5 i5 U2 p9 ?( j0 c1 _$ git clone https://github.com/SpiderLabs/ModSecurity
. O6 `: O; N: `) }: y! \: A+ W$ cd ModSecurity
/ R2 k8 r8 x2 r' U* w0 X2 ~ J$ git checkout -b v3/master origin/v3/master) y7 Q1 e4 F7 ?
$ sh build.sh4 z0 ?3 u1 ?) F
$ git submodule init
, @5 n8 s4 L5 D4 I4 l# o; P' x$ git submodule update& G* h; [/ ~9 Q% }5 j; h# x
$ ./configure* Z5 D- j0 u* d: l
$ make
/ ^! x# A) ^7 c8 ~$ make install
& S3 i. h5 V& e; s) qLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。% g0 U% M. s, x# u
* {& m* M9 T3 a, b5 ~& k: }( X
, }0 a7 y9 S5 l: {/ Q$ ls /usr/local/modsecurity/lib
8 g/ M9 h/ u. a+ Rlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0# m( P2 S z, \4 {6 G( v
编译安装Nginx并添加ModSecurity-Nginx Connector模块1 Z" s% M- x5 J3 z S( z
使用ModSecurity-Nginx模块来连接LibModSecurity5 Q0 X) ?% \" b
8 y8 L/ q- U- |* h
6 U9 N: Z& W# t" b T$ p
$ cd /root3 f( K$ l2 A' g k8 U
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& ~4 v2 @6 I, g6 I! E" g
$ tar xzvf nginx-1.9.2.tar.gz% w9 j% U5 Z! u5 r# @6 A
$ cd nginx-1.9.2
' V9 F3 i- D6 |; z$ ./configure --add-module=/root/modsecurity-nginx; B4 X) O2 ~( S) H% x: a$ P
$ make: |; B) z. w% \$ ]8 U8 h
$ make && make install" n: T, {, W9 J5 [, \5 q
添加OWASP规则
; U8 }. z# d- M2 Q ?$ k! m: jModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 F- d" V5 F5 W" ]: P9 t B8 q1 C) _/ V8 T$ L
5 I! `" n& V0 O4 ]1 j
下载OWASP规则并生成配置文件
, D9 A& I8 v0 Q1 ~. p, m0 ?$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, O4 @! j, d& F& V( s4 |6 Z
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
+ U: i3 x% V& E/ {# `5 r6 O& ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 B# ~3 F K3 d
$ cp crs-setup.conf.example crs-setup.conf8 P) O+ a Z( z% |; J5 W
配置OWASP规则4 D \4 L/ D6 D k/ ^
编辑crs-setup.conf文件+ P; T( ?' V4 t5 y
7 x: w2 K+ n J
2 | ^ d( ]1 s: ^- Z" I$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 f3 \- Y. t2 o& D9 o7 ]$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
0 j) s& Q7 p- |$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' S; S7 G1 V) c: G" e- s$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
5 I$ t7 ]! o% a+ A2 w Y$ D默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
. W* S: i: A; |- w1 p. n% L
' ^# o" f7 V) i0 R! l6 }& ]2 p3 e- ]
' Y. q; ?% r: Y8 J启用ModSecurity模块和CRS规则
: C. b. u* \/ l- b复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 U. {2 O- x7 T/ K1 {3 B
7 U/ @' J% L3 @* _3 j
' ^! x0 _5 ]& M; y7 k; _modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& n5 E6 C" ~4 B* J3 T( g; M5 W
: }5 X8 P9 u" o7 H; p. N! m) l/ x/ ]8 j7 q; R0 ?$ }
$ cd /root/modsecurity-2.9.1/* l# e* k- B; a* k2 E7 k" k9 C
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) M( }$ U/ y T3 c- F0 i$ cp unicode.mapping /usr/local/nginx/conf/1 }9 ^9 m' P, l+ F: ?
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; v2 @$ n- s3 a3 z3 Z
, ^& Z& W( ? v2 @
% D2 _- h% T, |+ Q5 f$ vim /usr/local/nginx/conf/modsecurity.conf
+ `7 G: i7 P4 qSecRuleEngine On
5 A4 t, Q5 v. J3 c7 l' @ModSecurity中几个常用配置说明:
, N' n: |5 z- O" Y& A; ^' j Y6 D, N
; r3 A* D$ ^( q' L( U Y1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! p9 |, P8 Z4 Y- M3 Q
& i1 D) n5 Y, C' m/ X; e# U
+ f2 C N3 f1 K8 }# O2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) p1 M/ d% _ s9 H! R R% L- K! ~' J0 s0 g
7 p2 }0 o4 j6 [6 I1 o) H3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
8 U6 x$ ~, H, Z5 O" X2 E6 `) M5 V% s7 z3 o
4 `/ p+ l- {" f' T# r# i2 _3 f
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。. I2 C7 ?3 j; r! X6 J8 {+ F: ~
8 Z3 w7 t4 }/ p: V+ h4 d0 p
& {1 T; E* m+ w0 B0 d' u& Q7 r7 B在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ W% R$ p4 ?: c& w) S" B$ A( \! G
+ M5 n0 c3 n1 a, q* r* z3 S4 t1 _ v
( x2 z+ f9 r# W J3.x版本CRS5 G) C2 `! {2 i7 B8 y% S0 V3 l
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 @3 s) d/ U. s( r3 b9 r; {) o1 K4 S
# 生成例外排除请求的配置文件3 w) E9 v2 E+ r9 n, |8 b
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: M( k* g; q) |* ?: L. e$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) S- c r4 a% d+ t7 [* m: x4 \! k4 l& f$ cp rules/*.data /usr/local/nginx/conf. B3 A4 r' A5 a0 j! p0 E* J' N
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 m3 \# q+ Z3 i, @- `; |5 ?
4 {* c$ l8 b& G, `
: U6 p6 q- A7 b# n o/ e' W$ vim /usr/local/nginx/conf/modsec_includes.conf
2 A8 a6 z$ P' E/ l+ B# [6 l, Y* ^* L
[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 - o- N) B5 u3 O) n" u
- d: K- l9 q/ T/ h' c( s1 i注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) X$ v4 ]# l/ c3 `' m/ }+ R* R) X2 b; P# X8 {/ R+ h
& W- g9 L2 c: s4 X
配置Nginx支持Modsecurity
2 s1 ]0 ^# h$ M启用Modsecurity
% @# L1 P2 v8 W& ]; W; R; {使用静态模块加载的配置方法& h" u3 [ Q; o: O+ l/ x
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. {4 H4 q) `: Y9 c& T
v- C8 O4 t7 _. b( R o9 F
# t; r( f# I7 G9 ]3 M, q# qModSecurityEnabled on;9 Q. v; a- r( G2 s3 @* c
ModSecurityConfig modsec_includes.conf;" W. U% }4 m+ P" T H) v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ D- K, ?* o6 B2 ^0 F5 A R% R5 k. m! i( A) Y: z( K4 R
0 z9 I) F# J# t3 S
$ vim /usr/local/nginx/conf/nginx.conf
, L. Y+ Z4 y E$ c6 j! ^2 P N8 P4 `2 o, Z
, ?% x( c# U/ L# w
server {. N* E2 K2 g7 c
listen 80;
" g- H# J# |" _; G# h; B' i: L server_name example.com;
, b7 l7 x( U1 V e0 N
& V8 O( Q4 S4 E* _2 k( D) H/ O, o* k' s2 d6 a
location / {
3 b! c2 n2 R( {+ ?, i ModSecurityEnabled on;
% V- T, g' ?/ e" t v2 i. R- s$ e ModSecurityConfig modsec_includes.conf;; }4 ]% J. X. g S* x9 _
root html;
0 I. O6 t/ c+ R! W/ W3 T index index.html index.htm;
3 W8 i, ~5 o7 j# Q7 K }
. @$ ~+ ?2 W8 Y- O}5 R6 S2 l+ ^% h: W+ b& j
使用动态模块加载的配置方法
5 l, ?1 z8 p$ ^4 d1 ?3 n/ O1 X0 b" A在需要启用Modsecurity的主机的location下面加入下面两行即可: `; l/ j6 W$ e" [) ~: q3 {
1 M8 N' q1 p1 _! h8 x+ l
# c+ I S5 j1 [; Z
modsecurity on;( l2 S7 b1 m2 v0 o D
modsecurity_rules_file modsec_includes.conf;
8 G8 o- z6 f8 @( R" u+ |" R# _& [修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: d: t9 g# Y0 x" r w
2 w ]! m# {6 C- w1 x6 P. W' j
9 ^/ b! X0 J$ b- g# S
$ vim /usr/local/nginx/conf/nginx.conf: \" i' {, s( I$ g! m; ]6 H
# p. @; C! @6 q, g
: Y# ?) l+ e" `* j
server {2 u% P8 o3 {; @, @6 E, l
listen 80;4 ]0 `$ A3 T! ~3 A3 v
server_name localhost mike.hi-linux.com;
+ M2 t0 B% v2 J" @8 J4 m0 ? access_log /var/log/nginx/yourdomain.log;' s" x& r1 b$ U; K% u
# b& \6 ^# ^/ R" {, ]- l: U& h: ^1 N3 ?) J2 \9 {! p$ I
location / { ^0 `$ x2 f! _, L9 `2 ]+ C% m- \
5 N; a& H* T: q9 r6 K7 U; d+ G5 |+ }9 P
modsecurity on;3 S, T1 ?* a5 t( c6 H* E
modsecurity_rules_file modsec_includes.conf;
- B( ~5 z1 D* C, }2 | root html;/ k. m# J7 O0 c( H
index index.html index.htm;
5 |+ N2 n' G! y, f}
7 u8 q3 O% D6 [) e+ [}, r7 y5 `4 F; G7 k, Y3 T
验证Nginx配置文件
( D- o$ _" ^, G6 z, s: U+ H$ /usr/local/nginx/sbin/nginx -t0 n, z& s- ?7 [
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok& s8 v$ j/ S7 X. y' F
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; F! v+ P+ s& o7 w1 h
启动Nginx
, j1 l1 E% y, `% L& Z$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; B. n' a1 H5 }: z0 d5 R
: ^3 o- |9 e0 B: z9 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能够战胜更多复杂/未知/混淆的攻击模式。 2 E% w; N0 Z6 p* i$ x
|