ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, c6 e6 V7 {) [+ {4 _' f3 N
6 g8 Z4 v5 q6 p3 A( H& e8 X, @ H5 O, i# E' e2 D9 V
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。; l/ K6 \+ b# e7 r- ^1 C
$ ^2 e0 g4 [6 w" w7 r' J4 _9 Y- t$ F9 o% M8 ]( m O
什么是ModSecurity/ U# P9 B: T' Z/ ?
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。7 k( E3 z9 B1 v! [) o) P5 [
% m6 e( }. d% I& _# C/ P0 ~/ ?
9 T8 U+ i6 }* R- W
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
, v) e0 B, a+ G. Y% ]) j! U( `* H
9 d4 [( X. m/ l" ~ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; j# {- F3 V. b0 K6 v+ M
5 D9 N' T% e" m) m, H
$ @" ]1 @2 v1 r- U4 S* n官网: https://www.modsecurity.org/! x- h: b$ m) ]+ e6 J
* I- c! d' A) y9 Q! `
* G8 \2 j/ D0 D/ a( x. s7 E$ i7 i什么是OWASP CRS) x5 l; K/ z5 t* H; x) e. P' w
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
; x0 t. d: }1 f7 z& ]( O
% I$ N; p& a3 n- _+ `7 d, s0 ^0 U2 C# c: F) x( w3 L
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 _) L, Z- t& l- }5 k
' V1 O5 s5 Y1 a! {
1 v, c; n2 w7 b" \' S5 L2 Y, uHTTP Protection(HTTP防御)1 K0 t/ r! j6 Y7 X% c
HTTP协议和本地定义使用的detectsviolations策略。) a. @: N5 A; ?$ D$ S
7 v$ Q( A+ `2 n$ C8 @ ^' w# ?, M) E& j" g' D5 W b: A: E
Real-time Blacklist Lookups(实时黑名单查询)
0 B- [. ~ z3 y7 _5 x/ w* N利用第三方IP名单。5 [$ s$ y% o! Q( ]. J* i, V
3 U1 e7 _/ M3 ]" t- j0 d
! z1 @) w9 P6 c l9 D2 f% o1 ?HTTP Denial of Service Protections(HTTP的拒绝服务保护), u5 w8 ~! L; d- H& X( ^ S
防御HTTP的洪水攻击和HTTP Dos攻击。+ _7 o2 o5 l4 O$ ^
$ [, h9 R' d/ f4 w! {+ C% S- d
Common Web Attacks Protection(常见的Web攻击防护)
. k! F* V/ r! f! I) p8 z2 J- u检测常见的Web应用程序的安全攻击。0 w9 \5 P! B7 R1 y
' r8 P' [; t0 `9 a+ X2 x3 e; O7 Z; L$ q: e
Automation Detection(自动化检测)2 P5 a b6 |; b2 S
检测机器人,爬虫,扫描仪和其他表面恶意活动。6 [+ O1 ~8 C) D# i
, Z+ Y5 C! i- l9 K
4 D4 H7 v# r4 n& B# v- uIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)* Z8 A% F& i' [; U$ \- q) U
检测通过Web应用程序上传的恶意文件。
& G+ P" C: Q$ K$ |
; L& a& e3 K8 [/ e, I% F$ l
. f2 H1 X. h1 y/ R- |5 @Tracking Sensitive Data(跟踪敏感数据)
! S3 Z5 G$ J4 @信用卡通道的使用,并阻止泄漏。
6 G# i8 q, y4 {
! f; s" H( ] n1 d1 E
+ v' q8 j/ ~) R9 F3 L& JTrojan Protection(木马防护)' [# B% ]! J+ A5 Y0 c
检测访问木马。
/ n8 b# P0 `/ g" S+ }! `! u# l! u% l) p
; p* C3 N* x9 {. _6 RIdentification of Application Defects(应用程序缺陷的鉴定)9 d! x# r+ F7 H) r. E# P! x. h
检测应用程序的错误配置警报。
% @$ d$ Z; q# ~3 Q0 o1 Z3 l3 P& G0 G8 }
& ]" [, W2 M$ I" U9 \; rError Detection and Hiding(错误检测和隐藏)& X( D' b& e; B2 L5 t
检测伪装服务器发送错误消息。) V# G) n$ F9 d4 Q% g, l, V
/ w# q$ j* {2 U$ q% Q- M9 e# |) r- M7 k5 u) k( ?
安装ModSecurity
# t. F U$ ]4 {; B5 D/ o: @软件基础环境准备. t6 ~0 g, f/ _3 ^
下载对应软件包 i" i+ a+ J# \' v4 I
$ cd /root
! y8 L" f' |& W) i$ C6 u: m$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 X4 [$ E/ p$ x' x* B4 m; E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* j' p1 E. d, P% v. b
安装Nginx和ModSecurity依赖包) R; G2 {, W+ Q1 o' X
Centos/RHEL+ {- ?! M* d- n5 k6 s- A9 s. S' e
0 E+ ?6 w% W, h4 i3 d
* q* x& I) c2 x: x/ J+ s0 e9 ~7 u$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel2 t, A3 ]- j% j( M' C3 S5 L) W
Ubuntu/Debian
. u: f2 J& }# _, h; e; ?
4 U$ f" I+ @! k0 n) N# u/ q [; G# r* y- P* i: m* w
$ 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
' ~: V- I* C4 p7 C- Z1 e) ]编译安装ModSecurity
" h4 S% V; m9 V+ H2 U/ Q. Y2 ?Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
5 s. s' ^# r3 U' k6 Q1 }2 ?4 e2 C2 q6 N: m
0 c- p7 B& ?" Y' |5 Z: {方法一:编译为Nginx静态模块+ ~0 u7 Q& s& w% B
- i7 A& c. ]+ k+ i
x. T: e. G) S2 H+ Y9 K" @编译为独立模块(modsecurity-2.9.1)
3 ^3 \3 Y9 \" A" t7 y* `& J$ tar xzvf modsecurity-2.9.1.tar.gz8 \6 n! Q% Q+ c% X
$ cd modsecurity-2.9.1/
9 U4 a+ U4 O0 a& L% J6 U$ ./autogen.sh# G9 R; A# q8 n) s$ u
$ ./configure --enable-standalone-module --disable-mlogc1 q: v' ~% @+ u, g4 W3 J3 S
$ make1 _8 |6 o3 v' w9 z' I
编译安装Nginx并添加ModSecurity模块. A' w7 T( d* a' N6 p
$ tar xzvf nginx-1.9.2.tar.gz
2 m+ Q! p+ w* W! O8 t$ cd nginx-1.9.2
& s% T7 q0 X! i/ W( Z. Y4 ~$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/, q! a- o9 ]) d* G6 _1 g6 T% U
$ make && make install) [# F! {( R, L2 Y2 R
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
5 ~" b% ~( D7 M6 h" o3 ~ p; l& y2 f3 S2 D- k" }
/ s z$ C8 |( B+ c" {& v编译LibModSecurity(modsecurity-3.0)8 w( w6 f" f5 }! R+ X% C+ P* |
$ cd /root
' o0 ]3 D% u/ j9 i: @ X8 C; ~$ git clone https://github.com/SpiderLabs/ModSecurity/ ~) ^" {% B+ h5 Y6 o" |
$ cd ModSecurity8 j, { L( X0 i7 U' ~0 \- a0 b
$ git checkout -b v3/master origin/v3/master
2 |2 r& ^: b, [ G! }) l2 x$ sh build.sh
0 o7 l2 x; G8 I0 N. A9 `$ git submodule init
( z ~* `% Z8 ?8 y+ U' T$ git submodule update, _/ t9 E) K7 m) N. T
$ ./configure2 N! x6 b& P' V# t
$ make
1 F; [6 W; J) }+ G$ make install
# H0 j6 D+ Y' J. U- c7 PLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
# q) ]8 e4 `( S3 P+ Z- \* A& T& k4 e2 e% \
3 d; [& O5 }( P2 u3 F4 I2 J
$ ls /usr/local/modsecurity/lib
" U. B0 b! _* \libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
# H; Z+ l; H! X7 W$ v" ~# E6 X编译安装Nginx并添加ModSecurity-Nginx Connector模块
4 k5 f; s" ]8 T T" o使用ModSecurity-Nginx模块来连接LibModSecurity* z [ T5 x& @; K
- u6 ~+ n' n: ^+ M
# m2 i" F* T0 O' Y# s# ?. S' m$ cd /root
. r2 J- `& C; W0 @$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx8 [. H0 h, W7 f' y
$ tar xzvf nginx-1.9.2.tar.gz' h) g& e" [+ E3 Z3 e! q
$ cd nginx-1.9.2+ c9 C# J; a3 W( q! P
$ ./configure --add-module=/root/modsecurity-nginx( Z' W7 D$ H* r) h) M" D
$ make; @ q8 M5 M& R
$ make && make install
4 ]5 h* {* x7 Y7 V添加OWASP规则
/ g1 Z. _/ Y# }8 x( B; uModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. n' b6 c2 X& \
+ y- U+ z( o, v1 p1 o: W" G$ ]2 j4 [0 G- L9 H9 ]# b1 ?
下载OWASP规则并生成配置文件$ \- I" B# s& g: j/ _3 g* N# D/ {
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git0 b1 G4 E. i" [% `" S
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' c" u1 A) s' k6 Z/ p# O" v5 X8 v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# W9 h( J6 n" v+ s S+ J$ cp crs-setup.conf.example crs-setup.conf8 ^4 {# ~: [: J, q2 h9 ~
配置OWASP规则
9 b% _ v5 X* J7 q编辑crs-setup.conf文件
% N8 K j5 v- \4 `# {* D9 _
9 A% c. s; e9 `3 W5 C" X" }) Y1 Z1 d! v$ [. ?( h5 Y- G
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 v2 E# ]4 \4 ?5 r4 j# O$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% S3 T- n& t9 S; s
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! T3 _9 T! r h6 g2 \4 D
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
' n1 R' p2 k7 E; @默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ z8 [% P% n0 w6 i
& S' j1 q. y+ p* E L( N
* b, i1 M! q) R& D启用ModSecurity模块和CRS规则
( s t ~& }; j+ E1 ^9 F# q) Y/ n复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ U9 c# K2 t9 f) d; D
( o9 a# [ p# Z7 j7 [
& A0 ^4 K3 S: \
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. v$ v+ ^2 v7 Z8 Z
9 H7 d" k7 `. ]6 E0 }2 K7 B
d- B4 Z1 C' g% `& I$ u3 E$ cd /root/modsecurity-2.9.1/
* P+ B" V4 d6 X" @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
1 ?4 P+ v$ n. e* V8 P$ z$ cp unicode.mapping /usr/local/nginx/conf/
+ M, ]2 _0 T5 h4 A ^4 M/ X, \将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 r/ }% U, {7 l
# H; i: L/ c6 S" y% N# a$ F; Q( e% D
$ vim /usr/local/nginx/conf/modsecurity.conf
5 ^; C. @0 q( a' [) ]SecRuleEngine On
: Q: n8 N' a$ Z* u1 wModSecurity中几个常用配置说明:2 b+ d4 [2 j4 D) |. n# o
$ X) H3 c- _6 A
7 J& _/ a" v+ `( a1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* v* {$ _/ l9 Z1 E
8 P/ S& D: S) I+ F3 _
) z9 i2 j2 f' ~) @
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 u; v( R/ g0 _& s8 y3 z. u
; E. F* w# R! U( n% h2 i. F
: A) l# f1 s9 [# ?1 P" x% Y7 i" D3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. `( K4 D' g( Z8 u8 g( Y: H4 k! H, w, h" Y; c' i6 Y
$ O) ^2 {. S4 }6 S+ d3 X5 T
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。/ a' d0 Z1 A# K
" F; }. L0 Y* c
1 x! s: O2 }( w; U) g5 X在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。: I, s/ a& J7 G- O8 ~) H" {
2 E* f# Y" A0 z5 e% b) ^
, t! A( ?# G( b# J3.x版本CRS% k8 M* b1 Z9 Z) c# {4 q7 _# p" h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: P2 U" @. k0 r* g' O# 生成例外排除请求的配置文件# l. M$ M! G& p1 _6 B7 Y
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf/ C1 U4 @7 n7 n4 J
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 R5 d2 V. A/ ^) s2 d+ C4 c% L$ cp rules/*.data /usr/local/nginx/conf
( t, i" ]8 i7 V1 @- r为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
( r0 j5 d" ]* d1 ]/ R2 L2 L
/ q* c' W+ j% j7 t% @9 {
7 t$ T) V2 C: J3 }0 j$ vim /usr/local/nginx/conf/modsec_includes.conf
, y3 v0 {" H* T+ Q: I+ M9 }* T9 |5 U5 j1 l/ u5 G2 g4 i9 t' O9 Y" _; 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
- @" f/ T! O3 f p9 ?8 G2 R1 P& D. T2 ?* z4 O
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。9 O& V R; i/ W! P, R I8 X' j
2 H6 A% m7 k6 f: _, l% o: S- l
* S# {8 o5 {! M, |* j$ l' z: Y
配置Nginx支持Modsecurity
$ Y% r. `( s! ~1 F4 h3 p3 @6 W" F9 B启用Modsecurity
+ W. _! |8 [8 a3 `+ ]使用静态模块加载的配置方法
% K- P; n" }8 }3 e# K* U/ w在需要启用Modsecurity的主机的location下面加入下面两行即可:
# ^' h+ H( }1 J5 m0 V
0 p7 h8 B* Q% I' o- Y5 A. f2 E# _$ |3 ]% ` H+ P! u! }: ]8 q
ModSecurityEnabled on;
" N9 ?) k+ d8 [: |# \+ C* c9 {ModSecurityConfig modsec_includes.conf;( W2 b7 [( Z7 t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; X7 C ]. @5 o0 P6 @
( O4 _: P* }: S+ J; S( c3 v" j }% J, d C1 V6 n
$ vim /usr/local/nginx/conf/nginx.conf
5 h3 x! d- t; ?! j0 J+ w) s7 c5 _# S
) B4 u- n9 [2 t5 L. |server {
3 L% H6 C4 I8 N, i3 ?" l listen 80;2 H4 X/ W( M, c! a k
server_name example.com;7 h; |3 @, o! V# L' [0 Q
5 u" N& E& b+ K; _' w
( v+ x( y* r8 n2 k& h4 r; ~/ R8 ^( ~3 i T! u
location / {
5 b% c) u) z( {; o ModSecurityEnabled on; a) `6 w( {5 N6 E3 g- g
ModSecurityConfig modsec_includes.conf;
5 L5 a9 h8 ~" E" y+ H3 \ root html;. G% O9 c3 i& A7 k# i( U& v# T
index index.html index.htm;! W/ m- X- [7 B( i
}
5 ~, r5 X4 }& e/ a2 y& a}: y0 Q. ]2 R& f7 @. }
使用动态模块加载的配置方法
/ h! M4 o! p2 W4 v( Z在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 \; E8 Y9 [7 F6 l
# p1 K7 E+ A; s0 M/ X% \+ O- n+ Q
modsecurity on;2 l4 b0 h7 }* G4 ~
modsecurity_rules_file modsec_includes.conf;$ A0 S5 X9 `7 L! i, Y% N1 P; X6 }
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 a4 U4 [" h+ a6 N1 Z
; ]5 m& a+ \ u9 _ O7 u1 t
9 d3 H: `3 W1 d( Z6 w$ vim /usr/local/nginx/conf/nginx.conf! D# `" [/ e: W3 s9 g3 f' ?6 p
( B' d) X6 `. y: V1 O# m' o7 N6 ], K% Q1 O" B$ F
server {" U2 y8 Z+ A& l# z
listen 80;
! a' G2 | t/ `9 X U server_name localhost mike.hi-linux.com;6 P' `3 x. V& Z* C+ E* `
access_log /var/log/nginx/yourdomain.log;/ N( P* A) P1 H
) W8 p: O6 Q* m) j6 m& g |
* A, {! h$ r1 R: {! B4 g
location / {4 w' |2 L, z% |/ S7 R
( `5 c0 ^+ a) N2 N: B8 Q8 a. `& u8 l; | w% ~+ s
modsecurity on;
6 F5 T7 A1 w+ n5 S+ @ modsecurity_rules_file modsec_includes.conf;, D: W4 c9 w6 k' \. S3 X
root html;' |* |- D* o0 v) S* B6 ?
index index.html index.htm;; @ e/ K- k: e
}- D: _8 a |! d' Q
}, O: b6 C; q8 h8 i" v+ f# ^
验证Nginx配置文件
3 N1 H6 K. b% Q! \$ /usr/local/nginx/sbin/nginx -t% N8 d0 w/ s2 @! P4 m9 l1 l
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
K: B0 t. b! j& |3 T8 l# qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 p- J5 r/ k j& o+ R% [. E- X. i' |启动Nginx
6 J" E! y& c/ x2 I2 n- f9 H$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8 @0 u1 a3 `3 X
2 y( t( L# j* n7 ?- [2 X/ g0 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能够战胜更多复杂/未知/混淆的攻击模式。 ) ?% [, ~; J0 ^6 V7 r v9 l K2 g
|