ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
8 A, l& m4 ^3 L7 G, d" w$ |+ a5 C. Q% p/ u# o
" h: T, a' q' A$ o; a+ l在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。" m! `# O2 X+ ?1 r7 Y# f
8 I! C: H1 S5 y8 f, V. j
0 G2 }# X8 \- i3 m1 d- T& G) ]
什么是ModSecurity
& Z) z/ O+ ^. \* mModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。7 _- @% E# M9 E/ }1 e
: C# {' }# z: P2 d* C
, I$ ]2 T' f7 O
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 W: Y2 i! S; S5 T, D& x/ b, L
8 q( L& i3 i$ A3 A
7 [, D9 ~' U, W. @ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 \( s+ p9 ~+ M# [" a. X
- j- w& [+ W- X) \5 |7 P( \5 s0 i7 [. @4 `% ^, A" X7 u
官网: https://www.modsecurity.org/" L$ V( y3 Z7 i
3 W' [5 q1 K% {
2 }: p7 q* ^) a) l8 z" d1 Z/ ^什么是OWASP CRS; Y- V8 A9 c( R0 Z( I4 e, i
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。9 S/ M* a* @/ q3 w3 y4 E8 E% X
6 \6 y3 H3 i; G, r, I- K
/ @7 i6 @) P2 [( C1 i
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 Y1 V& c7 R9 Z2 {
, A* O/ D+ C i
9 z1 u% F Q3 N/ L2 E% y2 zHTTP Protection(HTTP防御)/ `+ O4 h2 h4 I3 w! d
HTTP协议和本地定义使用的detectsviolations策略。6 I' Q# c5 V" K1 j/ b
. ]9 I0 |0 @8 C4 q
0 x @$ C3 L! W! R0 VReal-time Blacklist Lookups(实时黑名单查询)) L$ J, d5 a: X( R
利用第三方IP名单。! V& q0 r+ R, U$ w, n0 B
# C& g0 K. s1 r8 u
0 V5 o4 V- `* }9 @9 hHTTP Denial of Service Protections(HTTP的拒绝服务保护)3 V0 `+ Z+ g3 b
防御HTTP的洪水攻击和HTTP Dos攻击。
6 k( l6 s# s# [ Z0 ` e# o: p" p P& \6 c: J; K4 _! W9 K
5 j" C" j& l1 z: Y5 }
Common Web Attacks Protection(常见的Web攻击防护)6 p% T6 ~. c# P6 O0 M; y' l( k
检测常见的Web应用程序的安全攻击。, P+ t6 ]$ _% T/ a
0 W' E; X. r9 ^+ b3 q
6 @ {% @. P9 d; lAutomation Detection(自动化检测): J. [9 S/ K R, n+ p
检测机器人,爬虫,扫描仪和其他表面恶意活动。
5 M: ?9 T) @5 g5 b P, x7 Z
9 }3 d0 i6 R) e. U, ?4 C& f# x( K8 j
Integration with AV Scanning for File Uploads(文件上传防病毒扫描) c) {0 L6 d, ?- E# Y v6 A1 E
检测通过Web应用程序上传的恶意文件。
1 a- y8 B1 ?2 Z$ d& M% }8 W
" U1 j, m4 U9 n0 R
" h% F7 J: b9 ?& h! \1 z" vTracking Sensitive Data(跟踪敏感数据)
! a- C* Z9 r, S0 d% U+ t: V信用卡通道的使用,并阻止泄漏。" [+ k/ s& F- s$ h6 T
& i- a4 U& Q) i$ v8 {
4 ~/ }% z' ]; X2 mTrojan Protection(木马防护)
9 \/ q! `! F# L检测访问木马。
Y0 R9 W" Z3 \0 t( B4 t% K7 `9 W! v1 f8 d% c: F+ E
7 x- J& M: D1 b* H+ }' Z' x
Identification of Application Defects(应用程序缺陷的鉴定)- P% d/ P6 r f3 }) I6 W
检测应用程序的错误配置警报。
+ D0 _/ e, b4 X2 Q T5 R3 t3 s" U2 X* R
+ N5 }7 m8 j7 z( f+ f
Error Detection and Hiding(错误检测和隐藏)/ r2 y5 G( W/ O, K/ j
检测伪装服务器发送错误消息。; i0 c- s' L* ]* J
3 J' B% X4 X( T# c
( y0 N# ~" d/ ~' h; y/ k* d; g* }! L
安装ModSecurity
, m X) u( [" k- G4 e软件基础环境准备: p8 d; K. t' `/ F4 n
下载对应软件包
/ k4 f3 f2 O; Y4 o$ G2 F8 x$ cd /root4 G9 }, y+ |* g* g7 a
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'( l+ r! {: P1 _
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
V" w# f6 G5 q% l ^ |% P安装Nginx和ModSecurity依赖包
X: L: J* k2 `4 @9 o6 cCentos/RHEL; q% h5 D7 ~8 @/ A' i
* i& N7 m8 Q1 F* g2 S5 [: t1 x2 j. i0 y/ ?, S9 q' }8 z3 f& i
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 a6 C' [! |8 O
Ubuntu/Debian7 P% X; B2 O! {% `. t7 ^% k
' X8 q+ f0 B5 r6 E# Q* L) r1 l, m5 x4 a7 y
$ 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++-dev4 t* `& I3 n8 E( ^+ @
编译安装ModSecurity
! l- ], P2 C& N$ q+ F, T6 _0 MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。1 |; f1 M1 |5 z4 E2 G: v/ a) t
* E6 a# \+ l5 y; P. U2 I
9 ]4 n9 {' q$ W) f+ F
方法一:编译为Nginx静态模块
4 V0 C* N: S4 X2 g! d
T4 W6 {1 y2 b2 Z5 l
/ g9 V( \" f( }3 C$ i编译为独立模块(modsecurity-2.9.1)
6 g" b" s# b$ P Q2 k$ tar xzvf modsecurity-2.9.1.tar.gz9 ^0 h1 z8 ]# l- Z; `
$ cd modsecurity-2.9.1/
1 a- M$ l( z7 q3 I. Q$ ./autogen.sh; N) w5 W. N% ~) q1 }& k+ s
$ ./configure --enable-standalone-module --disable-mlogc4 y3 Z, |7 H: E- `0 e" l: r
$ make
) _ R8 L5 \' g9 |编译安装Nginx并添加ModSecurity模块
3 q: t7 x( Y) B- }, J$ tar xzvf nginx-1.9.2.tar.gz, [5 n8 ]+ ?: Y7 U% r: E) I5 m
$ cd nginx-1.9.2
/ N0 M( p' V# k# s7 n$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/. Q: |, }1 S6 z
$ make && make install2 N! ?9 E S% i8 U1 v9 |( V" T% ?
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# E, m) N7 v- r- m8 W9 Z
2 J; C/ S5 X7 i9 j4 J i+ K3 x5 v; f
编译LibModSecurity(modsecurity-3.0)7 K* J0 @6 {3 o8 o! j& H
$ cd /root
8 W' F6 N( y% c- x3 _$ git clone https://github.com/SpiderLabs/ModSecurity0 g b7 G$ S' W
$ cd ModSecurity
4 |2 r7 `$ ]. q7 }" g/ X) i3 _$ git checkout -b v3/master origin/v3/master' O. @& ^ q) o: M
$ sh build.sh; `) m/ q$ {) x1 |& |
$ git submodule init* P- m# t8 d) i" z1 a5 T W- I _
$ git submodule update9 j J: q/ f$ T a3 r! k3 ~' W
$ ./configure. D4 g6 P% t$ }! O W3 \# C
$ make& [4 J! c) e. U7 Z \
$ make install, ^& B# t0 K5 o# O* J. |5 p
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。7 P8 X. @# ^8 p) a% _) l+ N
7 I( M4 w! S0 p- V- b4 ?" Q+ Z
: E6 W' q, t: ~2 U( P5 O S: ~
$ ls /usr/local/modsecurity/lib3 ]9 a( o% g# R1 u/ [: T
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
2 R+ {% Z( d+ M7 e! \% n# l5 G编译安装Nginx并添加ModSecurity-Nginx Connector模块+ g9 }% B4 H0 r. A6 ^8 w
使用ModSecurity-Nginx模块来连接LibModSecurity' ^7 k" |- Q" R/ z
& [$ x* o3 M0 b1 O/ M
1 d* o+ B3 b7 a& c! ~. Z- \* i4 {$ cd /root
' e' @2 a1 {$ a, h8 X* f$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx9 x" |, w. w; z7 S6 x( n. l% i6 \
$ tar xzvf nginx-1.9.2.tar.gz8 K% z, D, e% s) Y2 M, o: ]
$ cd nginx-1.9.2
! W3 l! I8 Y) r& R1 N# O5 `+ ?$ ./configure --add-module=/root/modsecurity-nginx5 O& D8 P! N, N; Z4 B+ E
$ make
9 P' |' ]' ?, f6 k$ make && make install
1 ~2 M! n2 G$ J# J% T$ ^添加OWASP规则) }+ O2 D4 }( s6 B+ c
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. E- B( l8 L6 C" Z" f0 D$ C, s4 I/ ~: [
p$ c0 l! {8 w
下载OWASP规则并生成配置文件5 E o: }" h/ m# G1 G; b; \1 R
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
- h U3 r1 Z) }0 b) ]/ g$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
1 H0 H$ a) ?3 k) o7 b( N$ cd /usr/local/nginx/conf/owasp-modsecurity-crs; G5 s) j* h5 H5 M6 C- ~. d
$ cp crs-setup.conf.example crs-setup.conf
8 y) _: G/ t3 \. ]$ `$ m' L+ {配置OWASP规则% e5 d1 H) U2 R9 v) o
编辑crs-setup.conf文件0 n: _$ x/ U9 N% i
6 S; H( }9 [% ~3 O2 v/ Q% S; q* ]/ R" G( l8 R
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! f/ e1 u& U1 P7 r% m
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
$ X' U1 B% {6 L0 r$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
- u9 |, S4 ^* ?% j% P, A# J$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, Z, o6 B- Q- W3 V) k
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
4 R$ D+ y2 |5 l( U" F/ N! C6 k' Q* B2 b: O a M4 B) Q
1 w* g/ L9 j/ {6 t4 s) h
启用ModSecurity模块和CRS规则
, C9 d5 }- H+ W4 e8 j0 J! i复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 l! G3 G. Y$ m7 X
/ `5 ]. M$ `& G) F% X
K+ G9 B) F% O P% {% ~$ Pmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。, s {0 m4 c: x: {& W7 f! z& o
- |2 c( n2 R4 E7 H
) k# d+ B2 B5 T! B
$ cd /root/modsecurity-2.9.1/5 G+ d4 z2 o3 w+ ^# y" N# h
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf " r' E7 o/ V; {: B n8 k# U
$ cp unicode.mapping /usr/local/nginx/conf/
& F9 ]- L' G# z, x: T, v将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ K# R/ c8 v& j1 @, U! Y- r! X+ R, p
' O( r( q7 I4 `1 r$ vim /usr/local/nginx/conf/modsecurity.conf
# \ l! E# C, \+ Z" |; |$ ZSecRuleEngine On% n& L& D0 ^; E" i \
ModSecurity中几个常用配置说明:8 E* L2 P* W3 M+ \' Q' n7 S
; O% h* B, d/ R( G$ F6 `0 M; U0 V3 [% B e% s3 @7 d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。0 A6 h _/ A8 C
& _ N4 \# M, w# b! Z3 D. c" i& O; D. J. t
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 P+ ?* f8 K( g) i) D- Z( h& r/ h g
0 K6 r+ T1 I$ ~+ \, a! N7 t3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。& q8 j2 g6 g9 D* P6 J% Z% W
7 Y3 E# L4 o* a3 ~( A/ K! p4 U$ R* B; X1 U* G( ?* ]0 O
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: U) c1 q2 m+ G$ Z# G
: ~( R! a, m+ N* f0 y
5 \- m; s. y+ B8 C# l1 ^在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 C" G Z, C$ [7 Q7 n" I! \5 k
8 J# ^; H' ^6 d" f% G) J/ U4 g- {$ K: r( T. \& r, c0 `
3.x版本CRS
( p4 ~% [( K. D! [1 n6 ?$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 }0 w/ v1 Y; h; ~
# 生成例外排除请求的配置文件
/ D1 T% V# E# u6 I2 N6 U4 V; ]! [$ A$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf9 F' x# p) b3 M# S. z
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% a3 G8 }: {- J5 o Y* Z. I
$ cp rules/*.data /usr/local/nginx/conf
O7 Z- n! u4 U! J1 i为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
8 H0 [) j) p7 \& C% Z. U/ i0 g1 f. P5 ?9 S( p# Y( y, Y
/ w+ U5 |5 h5 u5 @1 S$ vim /usr/local/nginx/conf/modsec_includes.conf
0 N- P" T$ o: s+ h& N) `) T' ? s1 u# |$ R4 T
[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 3 u" n( ]" E2 `: W6 b
( L5 I9 f" |0 u0 [3 F注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。8 O9 B2 d* [- c [6 b! d
/ [ f* A" Q4 Q, A, F+ ]5 P" A
\& m, ~. |! r配置Nginx支持Modsecurity
/ H( u. C: m9 W m7 I3 m4 c启用Modsecurity, T8 o* c* O8 u1 l* W3 O3 Y
使用静态模块加载的配置方法, n+ c. t1 ]: K& `* V2 d: ~& p
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! l) z8 x3 {. ^& k! `8 S- w# Y5 m/ l) q' b; k
, Z8 N) H3 T) DModSecurityEnabled on;" `+ c9 G, R8 A' A T/ G( O
ModSecurityConfig modsec_includes.conf;
$ M2 z- c( Y9 _; M" j修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' e5 Z6 Y, d0 ]$ f% @5 M& E( e* }0 E5 \; p; P3 v5 @
5 |- t' Z4 o- G5 z: U( L- y9 x& P
$ vim /usr/local/nginx/conf/nginx.conf9 _' R7 T2 s6 H& ]6 A6 K6 A
7 X6 U1 Y- v; V9 i$ i" p, H W; I6 x" b% [3 ~
server {% _+ h% o- Y3 l
listen 80;
M* O8 x+ D) b( x. d5 S server_name example.com;3 U% A/ M' A4 h# e u
* Z2 E0 _3 u( v. k& L" w8 _
, y4 [( r7 O7 l, v( E location / {
1 s( D' F# w( j/ w; E: D ModSecurityEnabled on;
2 \; U' T2 X2 x ModSecurityConfig modsec_includes.conf;* s) x. s1 @ X! K+ d. V: K% y
root html;
9 K! v8 M- g* F$ T' ? index index.html index.htm;& `' G! w1 }& J1 q$ U
}# y; s5 q1 s% Y& M2 W F/ }$ s. \
}
* g9 q, `) A% K6 `9 Y! i( U- W使用动态模块加载的配置方法& b7 H" k9 T: M1 L$ c+ f" y4 R7 ]
在需要启用Modsecurity的主机的location下面加入下面两行即可:7 l/ j) K, N" [/ B
/ V& r% |7 V7 P' k9 v4 m
) G2 m; ?9 b! P3 U/ T/ }1 N' v
modsecurity on;
+ r, ?% U4 M. G5 g# m0 u! W( P/ Fmodsecurity_rules_file modsec_includes.conf;
% q' z7 C. p* n( o) s% m修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
V' l, i6 b K% _1 ]
3 j, E# q/ z! J, G9 C0 \5 R G% B+ U2 E
$ vim /usr/local/nginx/conf/nginx.conf
+ O* L' g* B; h1 G* h/ T( g) N# W8 N1 Q# V& b- H
2 c z; n2 |$ j- N) x u
server {
8 l8 J; f7 o d& q1 B. R listen 80;
$ X. Y& q2 R: G2 Z server_name localhost mike.hi-linux.com;, z: @( z! J# j J
access_log /var/log/nginx/yourdomain.log;# G4 p2 Q! y1 S' A
7 i1 v& I/ ^ s& u! c4 M2 e' D: ~
! g5 x' y3 a: ]+ {) S1 l( ?0 m6 f0 k
location / {4 `% C( g. w3 I# ^
3 M* q, x9 s& x7 x1 A4 W2 n V( k, m" E: W! v
modsecurity on;
4 D7 U3 P+ i3 G' ^# ?! { modsecurity_rules_file modsec_includes.conf;
6 R* q& i$ w) U root html;
9 B F1 W, o8 x+ |1 L. S+ J7 X1 V index index.html index.htm;8 Q/ j& Q+ c! C* v- r
}
( P4 T" Q- x( {% z( c}
: R2 ~% U: G' P; w5 B验证Nginx配置文件& M" [$ s# [6 Y9 v4 n; j/ K
$ /usr/local/nginx/sbin/nginx -t% }0 q; X8 B7 y+ P x1 s6 l: X
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
. I( K- O# C. G( Xnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
" D8 u7 v4 j0 B6 o' K6 S启动Nginx" |* J+ c* f; H8 O$ ~) H0 C
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf# { s. R! m* I2 a: t
$ I7 x% H! [' j* c7 ?测试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能够战胜更多复杂/未知/混淆的攻击模式。
* U% R" ^- ~! S8 ~ |