ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。/ O' {# `- ^! y; s. f" L6 X
* n" s7 d/ x0 U+ U& a3 s6 P; ~+ c
& n9 ^) v( k' {! L
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。! i5 }/ ?# O. Q
: l. n$ @2 q% e" x2 \
- U2 H% c2 {9 ]
什么是ModSecurity+ P: g" x- I* u& ^" O$ o6 i8 q
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。- c/ m& r: {- z6 W% X/ T0 r
: [$ I1 ?& B/ B; w9 A$ c/ l$ |$ b. T. {; m4 r
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 k( ^# F a C5 ?+ j6 N
* d) g8 |* t& s4 X/ n x
k3 o, U; R6 x( [ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 A% w! J# \: B- P* g0 a) n0 J# q y8 ^1 B1 @6 @$ J
3 ~0 X5 A3 h! L% A官网: https://www.modsecurity.org/* P& z( i3 D% i' C! S. F8 q
8 P* W) h& M( n0 [4 c) w: ~- i, W! T& L3 n
什么是OWASP CRS& M2 h) r6 u% \; d7 X
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. B. n+ t# y; I
* \2 x' n, W, h$ F' ]7 y/ I. e/ Z8 H# m L+ z
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 u% s# ^- w. u# Q
& u# o A( U2 `6 o- t' c5 o- D0 f6 v: |! ~
HTTP Protection(HTTP防御)5 M$ P. e. z1 w, T) J+ l
HTTP协议和本地定义使用的detectsviolations策略。6 P* U5 @# K( c
2 R5 e$ f7 w5 H# G) i, Z0 E
4 @4 ~) A6 g' \/ V; SReal-time Blacklist Lookups(实时黑名单查询)/ \/ `( x8 `6 m# G% w& \( m
利用第三方IP名单。% I+ A) |' |) e. W& [5 X
0 k8 D- K: W3 a# _/ K* ^ r) y* `# r$ U0 M/ F* n
HTTP Denial of Service Protections(HTTP的拒绝服务保护)5 j% |' {* ?4 M* M; l9 H" b$ B
防御HTTP的洪水攻击和HTTP Dos攻击。' Q! y6 v8 S, e5 o! l
5 O/ b4 C* l% A
1 X$ v3 ~) U1 a3 [) QCommon Web Attacks Protection(常见的Web攻击防护) t4 k' f, J% W: w' i t* p
检测常见的Web应用程序的安全攻击。. y' T$ Z! R' e
4 @8 W. X, J) c3 g, _+ J- k( z$ K" f9 U4 O) t. o4 R: R7 w% a! ~/ {
Automation Detection(自动化检测)
" v. q; l5 l9 h# E4 g# ]1 ?% d( q检测机器人,爬虫,扫描仪和其他表面恶意活动。
S1 P! q& M2 e4 J. L# K; L5 h1 P% k
( t5 i9 W0 c# e+ `+ y1 a
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 H, ?; ^; g9 s检测通过Web应用程序上传的恶意文件。. c4 |' u6 T% j
+ z" r" o4 e) V. b4 s( n
" s. i M( \4 ?0 M% E zTracking Sensitive Data(跟踪敏感数据)
0 m0 a7 g* b$ S0 n( y信用卡通道的使用,并阻止泄漏。
( I% ]0 R2 F& M! t1 {1 r5 R3 q& }& S# K5 \
, k/ \0 \. L4 g# Z( O
Trojan Protection(木马防护)
2 J, z$ G7 x$ p' [8 k, M检测访问木马。
" K' h9 D7 E: N. m5 P9 p
& B1 h' H' X$ p- c5 ^% A
- V6 ]9 C K9 i$ PIdentification of Application Defects(应用程序缺陷的鉴定)8 R. u% l$ ~+ P7 Q5 }
检测应用程序的错误配置警报。
( V7 y) p$ P e& Q$ p+ j. z3 d6 f& T/ N
3 b- z0 o+ `. `& ]! N
Error Detection and Hiding(错误检测和隐藏)
' s8 W! ]! K+ i- _5 f% C" Q1 T检测伪装服务器发送错误消息。9 e, N# O) I/ \0 l
_0 Q5 f! @$ j& c4 E: }7 \. q$ j1 B: b# T
安装ModSecurity
) r, t4 _0 H: s软件基础环境准备/ S+ {: z* U$ n
下载对应软件包
; x6 A$ O8 z% i* i$ cd /root* I; d2 F* ^' n* ]
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 u0 E6 v: [( n$ J9 d/ ?* o$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz% ~: ^: L( b. u* r2 z
安装Nginx和ModSecurity依赖包1 D$ ^* z* u) z% _( u; t" f
Centos/RHEL, w f8 V: H. t
( R7 w% @2 G1 {5 Y; ~/ h/ V: p
# w( s. {. k/ e" E( K F$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel" S, {/ K% K/ T
Ubuntu/Debian
( q0 T% G) _% c* @7 g: p' O; N
5 F: ?/ |, O+ m1 D0 E! _8 O" r1 V4 V% ~
$ 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++-dev1 Y) ^, E5 P1 A+ D1 D
编译安装ModSecurity
7 w1 c5 D9 |$ ?0 m p' pNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ f4 k5 \* B, O+ b4 ]4 _7 N% I6 [" A
# q6 N1 k# C/ ]) |* \方法一:编译为Nginx静态模块! s- M( M( }! H3 {/ q% F! B
4 q( E# d' L0 e9 x& Q: q
% ?$ Y2 ?7 R# u5 c( g
编译为独立模块(modsecurity-2.9.1)
! _+ k! b% U8 g3 ]$ tar xzvf modsecurity-2.9.1.tar.gz
n+ o5 ?8 N4 `: o$ cd modsecurity-2.9.1/
, Z( X2 w m4 s8 |$ ./autogen.sh0 g' l U/ p- Q3 {& r
$ ./configure --enable-standalone-module --disable-mlogc* \4 s* n; l$ i- r% `: A
$ make$ h& w. R5 \/ X7 z
编译安装Nginx并添加ModSecurity模块6 z9 @' a4 x+ i/ w- Q+ O
$ tar xzvf nginx-1.9.2.tar.gz
! M9 |! `# m6 n6 K l$ cd nginx-1.9.2) p) Y( \+ `8 I2 U
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! e1 x4 Q& g' P5 }' r3 t3 z$ make && make install
; _. F7 W3 p! T3 g. g! b方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
5 \$ R# \" R0 g$ w! d
1 G& Z! ]) k1 j1 O5 H% C: Z
- }+ M2 I L1 a) }6 L8 b+ l6 j编译LibModSecurity(modsecurity-3.0)
+ W3 F7 k- W* d! k1 Y# Q. J$ cd /root
+ X( T5 d- x4 g* ~ o0 E$ git clone https://github.com/SpiderLabs/ModSecurity- ]# Z- `0 y. I( `5 Z$ z
$ cd ModSecurity/ K) a, b/ h* a5 _# d, r; r$ d
$ git checkout -b v3/master origin/v3/master
5 ]5 K6 Y2 u, o$ @& k, F$ sh build.sh! W8 L( e- n- U! a6 r4 B# T1 R" M
$ git submodule init
4 X+ @: |* ? H1 W% M8 x$ git submodule update) x! f( @1 i9 h. a4 P6 t
$ ./configure& F6 D' U; S' P2 r
$ make
8 B" t' e1 y, i) V1 Z' ^- G$ make install
/ Y1 |6 E. p# v4 d7 F! V/ `" NLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 m7 _( p; V) B4 O" ~+ e) R+ K7 _5 K6 f0 V
" N, B& i- I8 D9 S s$ ls /usr/local/modsecurity/lib+ T" Y Y0 f& K' Y, J# _
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
$ o5 d% S! @ n编译安装Nginx并添加ModSecurity-Nginx Connector模块
, F, Q% \; b t使用ModSecurity-Nginx模块来连接LibModSecurity/ ] _& z8 V5 ?, ]' l
" o& e% A1 u# h/ f) [9 R0 _1 x x% \# `7 _9 L
$ cd /root
& r0 M' w% N6 x2 x9 L$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
1 |7 c" e; ^6 s5 S- t4 G$ tar xzvf nginx-1.9.2.tar.gz9 q% H7 k* |; O( W& B
$ cd nginx-1.9.2
3 a8 w4 \3 m9 x* p' @$ ./configure --add-module=/root/modsecurity-nginx
5 O1 u/ `1 W1 M% w. v# v$ make
- o* p: `9 g$ _4 d- ~' l0 [& Y9 ]2 t$ make && make install+ P" `4 h2 o) S9 A3 X5 V5 u
添加OWASP规则% e$ {% v+ g2 z% p- G: J' ^
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
" O E( X* y+ @0 [' k/ z) d: p, O( m, Y% L0 ^5 T- `$ @
* f" c! O( }2 d下载OWASP规则并生成配置文件 \ k2 u. J& Y; Q, u
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 F: A! [% C0 b4 c+ ~( Q, j$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
: j: @) e5 G% N+ h8 a# |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 x/ q; N/ z" d- q
$ cp crs-setup.conf.example crs-setup.conf
6 I) _9 a: m9 j4 l" f! g2 x% x配置OWASP规则6 C/ l; j5 K" l$ x% X' @
编辑crs-setup.conf文件
, W- w& ^" O, r" Y% i z% v0 N& w
, z. U$ E5 i3 ?' G% }$ w7 j
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf% c, e# ?6 E8 V/ p4 ^) r
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' b# R; R1 A4 {" ~7 v$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
- k" M+ \" @$ g* h2 z6 e. j) c$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf4 }2 C% e; ^, a# y* q5 @' P2 g
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。1 ^2 d* H7 T8 S- p9 B( Z
, \( c; l: F: ?. `2 s
0 e3 L N- |1 s* d启用ModSecurity模块和CRS规则2 u' Z9 X o) o) `8 @/ D( o
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# `( X" u/ }: B% p$ d5 s `8 i) Y# q0 a0 y- {8 V9 y
( {% r w4 y9 h2 kmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
+ H) X4 M+ r' ^6 M5 |0 w( I$ B8 }8 _, h) t- T
, o- K2 E; p! Q1 y$ cd /root/modsecurity-2.9.1/; Q$ m0 d: G* u$ w$ b( i
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : y# r! M: l! u: A
$ cp unicode.mapping /usr/local/nginx/conf/0 y$ q. L- c9 c
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( o3 z' @, ? ~
/ I; L1 D1 l$ w( d
5 O7 [# Q! c2 F/ `3 G! K u$ vim /usr/local/nginx/conf/modsecurity.conf
; H5 K( x0 Z: `% TSecRuleEngine On
# m6 o4 ]2 z+ SModSecurity中几个常用配置说明:
$ s9 ^( U) G! \: ~6 ]8 Z! n) D8 p, w J2 e: F, P, z* V
1 w+ y8 b/ @* q# E: s: Q) L) T1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; Y9 V5 A3 V9 x' s' J1 q2 L
" o+ }, x8 n; Y* q- E# u1 s; g
9 m$ B0 Y( C4 K N' V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
( f& G' b. R- q# c4 t5 a! `
0 R' Y9 Q; D$ p) \. b; o! n1 ^! w. [
& B( a9 d# o \- }) F3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 k. ~- c+ G- m6 k3 y# O
( E3 U& x. C/ f) @9 a/ b3 O& Z
1 J2 m2 G2 I! [. v( \
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
& D* }: i; N( ^, J4 w, Y, L) r7 ~; t
& r2 I' S U8 A. o9 n; Q4 J: O, l在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ F$ b) @! Y5 w b
4 i2 c2 a* c2 O, x1 w% W6 y
! U6 d+ I# ~0 i; ~. `& V9 I3.x版本CRS' ^- q L" Q: P2 }1 |9 y: B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ |2 y ?$ [8 K! K. d; V- p# 生成例外排除请求的配置文件+ ^: g5 ]8 \) p* R; u# _' O
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
0 P! {2 X5 ]9 f: J& {, l$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 y5 d6 e f: b) ~0 a; w
$ cp rules/*.data /usr/local/nginx/conf
2 j& W( ~( k* h5 g2 e, K3 e为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 V6 Q. r* r7 r6 G4 ?1 f1 i2 O
, A9 M8 \6 H/ L) N$ O) h
O t) ~" p' ~4 U6 p$ vim /usr/local/nginx/conf/modsec_includes.conf
* I. H/ Q6 Z$ t' R
- c2 x- o* t& M5 s7 r[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 1 Z5 o. w r2 c( ]4 ]4 \* V* h: e
\% m# K& C, e/ p" K X" m& N
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
+ b0 Y2 f/ Z5 g1 C* r- D" X( c
! r- U4 }! q w4 {4 J0 I: k7 f( K! K- f3 a, s$ E- f7 i/ J9 P% u! a( k
配置Nginx支持Modsecurity: j9 l3 V' I) T1 D7 Q2 y
启用Modsecurity' P$ [8 _$ D A- A' Q3 [- {
使用静态模块加载的配置方法
2 f# ]6 |* h5 ]! P在需要启用Modsecurity的主机的location下面加入下面两行即可:7 Z# o7 X* b, p4 s' q9 M
. K! r8 A0 {* k. }5 A
9 u" T$ y! z- ]: oModSecurityEnabled on;: l8 s2 t7 b0 O5 C2 o9 U
ModSecurityConfig modsec_includes.conf;
9 Y5 K H7 o7 d8 k% O! Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 x3 n L* t& A
/ `, z8 Z, I$ W4 C/ H& M8 d% y6 \0 ^; t" |
$ vim /usr/local/nginx/conf/nginx.conf
& v0 U( J. y4 K- ?- I. [/ b, F, u' M! G* k
. r, ~ h( ^ P5 c% Mserver {
; h$ k$ u7 f* [ u listen 80;
/ ]6 B1 a. ?7 @: s* _$ w$ \6 f server_name example.com;
) v4 j( @3 b! u, l6 h! t
0 [! C" b' b* v0 S& w0 O( W" d/ r* @$ N' h r4 D6 y
location / {( H, _5 i% ?; t) L5 z1 K7 ~
ModSecurityEnabled on;0 \3 `( S1 E. Q9 K
ModSecurityConfig modsec_includes.conf;
0 u, v y) U9 P# O% N root html;
* y% E2 }7 r m9 |. u4 ]; W9 H index index.html index.htm;
9 X; Z$ n8 X+ n+ c: N: ]( G2 G/ T }
7 D5 C% W' r5 r, p& F}
* }+ M0 m4 ]# ^9 `+ ?0 H使用动态模块加载的配置方法
5 x8 F# R+ E5 t0 h+ `; k) R2 s在需要启用Modsecurity的主机的location下面加入下面两行即可:, v$ k- C) q( K/ f/ z
& Q( e% k; V$ x% C' b* p
6 b* e. J1 k$ M- kmodsecurity on;
% ?9 S. e9 h' o! b# Hmodsecurity_rules_file modsec_includes.conf; k) i y2 w) [! U/ @
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 _4 L/ W; ]1 H U# P0 q) U+ h w5 J* ^
* G! T% ^* Z; b$ vim /usr/local/nginx/conf/nginx.conf, F# {% c% K0 Z( b) U `
( h4 }8 `2 G0 v8 Q6 h7 [' n6 F
6 m: `+ x6 M; O! T; L" R
server {+ l5 z' g2 G# f! ^' ?/ u
listen 80;
" N0 y& G3 j3 q% F server_name localhost mike.hi-linux.com;
: ^3 K8 b- }4 E% l access_log /var/log/nginx/yourdomain.log;1 e2 a- |) U1 r
# A4 q, ^1 R3 d! U! p3 M
! U1 f3 v6 {3 c0 n4 x5 a+ @" n- Q3 Y location / {
$ Y4 _/ ~7 Z( C7 S
" p9 U3 G: m7 s3 _; _( M6 ?& R1 t' ?
modsecurity on; k; a1 z* m, _3 J1 b; [) d
modsecurity_rules_file modsec_includes.conf;$ I: r; J% g& h6 r1 X7 A: ]
root html;3 F" b" g' y/ g- w
index index.html index.htm;
* J/ ~. O+ E* }; l}. Q- \' E$ R v! K# I' @
}
) t0 h) D( P4 A1 T( n: [验证Nginx配置文件; d: x/ c- B, r b& B7 @/ B
$ /usr/local/nginx/sbin/nginx -t1 Q2 d" A2 I+ R4 U/ D. k
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 H! [/ G% X( z. j' E
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ ^3 B( D( d) P5 F7 H
启动Nginx
9 {. g: k# B4 q: I& r$ C$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf! ]* Q" S1 ?& T
. _) R" l. l% {$ r$ v, _ f) Y
测试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能够战胜更多复杂/未知/混淆的攻击模式。
5 J' t- S+ f% Z4 }4 l# a$ Z8 u$ T) v% q |