ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" w+ U2 ^6 v2 _4 y) x, W2 N; W
i; N( }7 `0 w8 P2 ?4 s( L: F9 u5 ^ V& Y
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。 U9 [+ w# Z5 M2 ~* ^; i. Q" J: \
, `0 Q* L: u- k g
. [4 X! ?6 ^% m9 _* x: F3 k什么是ModSecurity2 x+ s/ ^ p0 Q' [. I" X! v
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。% v0 I% w9 F6 l. k; C9 e* U; J
6 R0 {" r: ?0 w* B/ R6 L
7 C$ }0 x5 B2 s; g' s8 B
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 k$ b. \/ N0 c4 B; y+ z
' V0 w+ Z8 ?4 d o5 G$ e5 t5 n
% q/ ]' E, ?( d+ P. Y: k1 f
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# j) t7 Z3 c$ o! R* A) _4 X. t: V# O7 ~- i
5 l8 L6 O8 b$ q5 U. e* P7 N官网: https://www.modsecurity.org/% U: j! d( g; I
9 j6 k" T0 T2 S5 j
5 Y$ T) g% z6 K0 V( S
什么是OWASP CRS$ _9 A4 s+ R2 D1 o! o* z& r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
* ^3 [( N4 u1 W; q4 b, q( s& I7 x
. l' O B+ J5 M0 \# T. _4 o4 p2 n( f' z, p
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: L: H+ ~0 W* h8 K- L) l" w" w
3 M/ p6 _$ O+ \! v+ O
7 d& `3 m {2 [* bHTTP Protection(HTTP防御)
5 n3 j& z) \5 J; OHTTP协议和本地定义使用的detectsviolations策略。
% X( D9 Y: M5 y+ M+ _$ ~5 _
( G: {! h1 d- F0 y* J
% [; u0 u3 r, T7 i4 [. B7 UReal-time Blacklist Lookups(实时黑名单查询)0 O# M/ P& h% i; u- c/ E7 m
利用第三方IP名单。9 F- d& }: @' C6 l
/ a) [ `; U% ^4 ^( ^
9 X5 Z( l) _7 X8 ]. j2 ]( b. KHTTP Denial of Service Protections(HTTP的拒绝服务保护)1 T1 g7 j% V3 e
防御HTTP的洪水攻击和HTTP Dos攻击。
7 M7 `8 t: i. T8 ~1 ^
& P* ~; k X* R* s- S
2 u9 r0 ], p8 Y, |8 {Common Web Attacks Protection(常见的Web攻击防护)4 u5 R1 U1 V9 i: S @6 L5 c0 y3 ?
检测常见的Web应用程序的安全攻击。
4 V3 W) @$ W% I3 x T& K" z2 l( D. ?8 d5 F: }; R t& ^2 L, N I/ o
' B: U5 m8 I( z D0 F) c( p; C
Automation Detection(自动化检测): P4 }+ L0 i' t; a
检测机器人,爬虫,扫描仪和其他表面恶意活动。
: _1 s- l5 F6 s! }& G
6 f% M' a2 l$ G% w" t4 A; E0 D6 c: @0 X0 m% `* ~
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)! k/ T2 D/ c( h5 _7 }+ M
检测通过Web应用程序上传的恶意文件。
# F N" B+ M. U; M' C& J
7 U+ I: D- {& y1 c, U* \8 B: [3 W1 t8 h5 d* Q$ E
Tracking Sensitive Data(跟踪敏感数据)
k5 e% O$ \, a Y) U3 v$ ]$ p信用卡通道的使用,并阻止泄漏。
$ s+ e% m+ v1 s$ n" [5 K7 r. x
$ E4 w8 p4 r. ^$ |; @7 J, n y1 y
Trojan Protection(木马防护)
N) H6 ?8 D: r8 N; _2 q检测访问木马。
v. @+ K' a/ j8 e, j2 p: Z4 {/ N& q. ?8 ^! O, B; Y5 F3 B! I
[0 S3 M# w: [: [
Identification of Application Defects(应用程序缺陷的鉴定)
2 \8 C/ ~8 G$ I' o检测应用程序的错误配置警报。
. q/ L: r: F+ q9 ]4 W3 h8 c1 |; w* v9 E1 y* V) k
. g6 A, N) Y7 W& u J$ G( b# JError Detection and Hiding(错误检测和隐藏)
1 y* q% r/ w" e9 e/ Y检测伪装服务器发送错误消息。# Q: x7 c8 E g; _' z+ }
+ O. N* t4 F5 N d
# T# R- R, @- U安装ModSecurity% \) g7 A! r# ]5 o0 d+ _
软件基础环境准备7 m; O) H( E6 Y* l+ |, G& X
下载对应软件包
( x& E/ L9 o/ k H8 M$ cd /root
& Q2 `; k* q% {$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'# p% w# E( U: A. c9 i0 O
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz6 Y' l9 e% j: @4 g! b
安装Nginx和ModSecurity依赖包( C. t* M/ @/ F" ^) e
Centos/RHEL( Y& F. z; L2 q, U0 F
R. g. w- Q! }
1 y" F4 x7 \* `0 E( a3 }8 q$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 l {9 g; ~7 S7 p4 I; r
Ubuntu/Debian5 ]1 v* L5 K7 F0 k/ D" j1 y7 h' g
- c7 q' {' \6 Y4 X
! V8 s& `" X' x8 z) 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++-dev. t+ A5 x0 [3 {. z8 [2 y5 U; P' o
编译安装ModSecurity* d( ]/ u+ N6 Z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ [5 A3 C _6 B) L& q+ o2 q$ ^4 e! {. _4 F* s! x
k" |7 B+ P9 s6 @5 p方法一:编译为Nginx静态模块
7 z+ E# P/ n1 o: `) D( i0 Q( O& Y5 a4 p
$ V3 t1 }) B; i) a0 u编译为独立模块(modsecurity-2.9.1)
j% F& j7 i7 {) Q$ tar xzvf modsecurity-2.9.1.tar.gz* F0 y' O4 |/ L$ P/ m9 m3 t
$ cd modsecurity-2.9.1/" a3 e+ m l& N$ z7 o A) s# |2 F
$ ./autogen.sh! ]) u2 y5 i+ b- l( ?! E
$ ./configure --enable-standalone-module --disable-mlogc. u, w; H, U0 r8 ]3 a: }+ ]. X
$ make
" L7 Q! P& A2 @8 J2 k: `/ C编译安装Nginx并添加ModSecurity模块
7 D2 C' b, d5 ^9 f2 c$ tar xzvf nginx-1.9.2.tar.gz! Z6 R6 p; K5 Z3 R2 S
$ cd nginx-1.9.2
+ P& J( F4 }/ ?+ T/ V9 C+ @) O$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
) h v4 M) x( r1 P( K0 t$ make && make install
2 O( ?: j& S F( X4 _方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; z6 H @4 o# T* v1 J/ g% f2 ~) f/ f' R2 `* C( S/ u
# o& O$ P2 n" Z" j( Z编译LibModSecurity(modsecurity-3.0)) C* ?: l5 j$ c* v" O
$ cd /root0 L$ ?( j7 |. p) d
$ git clone https://github.com/SpiderLabs/ModSecurity- w) v; O: e% N3 {
$ cd ModSecurity2 U6 i: Q! ?$ ~7 X$ \" c6 S9 j
$ git checkout -b v3/master origin/v3/master( K5 a( }/ I) t2 n7 A9 R, t/ }! C
$ sh build.sh. A6 u6 s/ p+ f: v+ m7 V/ ]/ H
$ git submodule init
0 q, {* E5 ?( Q. F$ git submodule update
! K3 h4 ~6 [% Y& T0 [$ ./configure
6 k% Z3 W; Y3 P, E, J/ X$ make, w+ m$ J* t7 M5 f5 f$ n
$ make install0 X; C8 h) ~( W1 i5 ~ @4 A
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
9 ?8 F, V% a" U' H( J0 @7 A. w; q/ Q; U9 U: }6 P( F
4 X6 X+ G, C4 _. E8 h& \
$ ls /usr/local/modsecurity/lib
& O/ S. d; [1 d' W, C) Ylibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
/ m' [1 n n T) O$ b6 o$ H编译安装Nginx并添加ModSecurity-Nginx Connector模块 B+ ?4 w3 ?5 n: U$ ]9 V7 h4 s
使用ModSecurity-Nginx模块来连接LibModSecurity
) w, C" g) X; T' y& B/ S
! K9 f7 J) ?; s/ k, l0 ]* ]2 {0 M& k5 z5 M
$ cd /root
8 t2 O4 X) |7 ^1 w d, Y: l0 R$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 i5 o' p& a @% b" n. I$ tar xzvf nginx-1.9.2.tar.gz
' M7 ?# n) r+ U5 H, O$ cd nginx-1.9.29 M/ O6 i9 I0 _! ?3 P2 x0 k
$ ./configure --add-module=/root/modsecurity-nginx
' B5 U- P2 H1 P% @2 L$ make
5 d$ a" O" y: [* Q1 e: q1 z2 L) |$ make && make install
, M& V+ y |5 _添加OWASP规则4 G4 _3 G: f$ A
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
- A/ {' S$ ], J# w. S" D% z8 b) T# y! W2 T8 v2 \. V
* T5 M) L9 z* y
下载OWASP规则并生成配置文件* |# s" }! |) H5 x& a
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ G- K+ @; W8 t$ t" o* C$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
0 z& G0 _! C7 l; m% p' @$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 S* M: ~6 s6 j. T$ p
$ cp crs-setup.conf.example crs-setup.conf, x( O& J8 F& `: r% X
配置OWASP规则$ |: Q3 ]' @, v+ w
编辑crs-setup.conf文件
! K/ L6 U9 h& v- g g
! [. \4 D) C% N9 ?- V# v
) J. t: }" I* G4 |" a$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
5 @% B5 Z- \: f$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 |; T5 R' }- U: g0 G$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
]5 L' i% T: h# `$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* ~1 M- s( r. m! e
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。: E& i! c$ B! g3 v( A+ G$ T
+ {8 @* J& S5 U# o
v0 c9 ~1 n1 K; d( l1 w; ^
启用ModSecurity模块和CRS规则$ P4 N. k) ~, z* v7 o6 u
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! g {9 e) L9 ~
2 ~" g3 q" C& x% m4 [" H( b. f8 q' P
( Y8 j0 d& z! ?# N c# h ~! zmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 ~7 X p/ w* d8 {+ H6 a
* p7 o3 K& ?! x$ d3 M' b; r4 C/ R9 f7 Z* I
$ cd /root/modsecurity-2.9.1/
# t3 p2 ^$ |6 o, t+ t$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf * n, x1 q( @2 m; b
$ cp unicode.mapping /usr/local/nginx/conf/
3 I. k0 o! G+ R* W0 |: o# M将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。5 q: |; m5 E+ K$ v
& b( j: C; ^3 m* e& r/ y# \: E+ E, F/ y% R# y9 s
$ vim /usr/local/nginx/conf/modsecurity.conf1 y- O/ l7 V5 P
SecRuleEngine On+ \. z! ]* i, e8 _* X _2 W
ModSecurity中几个常用配置说明:, V& r0 E# i5 {) n7 T
5 |, `3 {. v$ C, s, m
( ?$ r8 Q& s4 d- K7 E0 y
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。6 {! L1 o, b+ {2 G4 _$ N2 N
9 e- R y& @% g, c; X7 g9 _4 u K' S
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。/ W# d9 u5 z5 N4 {8 b, ^
8 G3 F0 ]' }$ h# ^8 y* u2 l8 c$ f3 g
: v8 x2 p$ `6 D1 {0 k/ {) Z! K3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 k& u4 |( @2 T6 U
0 F E9 W* b* @
( J8 i" @4 t- |4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( L% x6 |' c1 o7 } {# a5 I2 W, i6 F* X& M" [4 _& C
/ v' I6 x+ o' Z& j% w5 h
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
$ G6 J. z( k3 h/ y; Y, A3 V5 d4 c# ?9 C
( C; b: R6 G/ c$ b3.x版本CRS
; _! v+ p* |6 b) s) H1 E" f2 [$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 V" T; b) ]! z" b' [( g) _# 生成例外排除请求的配置文件, Q1 S# f4 }! x* M; S9 }' ?
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ w3 ]4 ]8 }5 a) h$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
; B: R" K9 F/ R" F! }$ cp rules/*.data /usr/local/nginx/conf
. r/ C0 ^4 c4 p% B9 q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。, F* D, F( n9 A3 R H0 v/ J% Z3 [1 C
, R$ B, y' m' b, q0 K3 I) |$ U5 H2 z+ H' G5 C5 h: ]# a% f
$ vim /usr/local/nginx/conf/modsec_includes.conf* A8 l! {& @% b1 W" Z( R1 k
" r' q# h9 u6 C[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
$ ~% p/ W9 b- v' N6 g$ [7 ~7 b) c g. f c7 g
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. S8 ^( s# T! M, @7 }0 g6 t. e, P9 v& A9 l6 r! u
3 \, j [3 I/ \! d) J' E7 @配置Nginx支持Modsecurity
5 q+ B+ u. _ R2 M! ^, e1 Z- `/ i启用Modsecurity" i5 a0 M. _+ u
使用静态模块加载的配置方法& l, T% |2 o' Q! U+ N. G* g
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ {2 ?* k+ V# x" X3 X/ e7 v# N5 n% d6 m/ C' W5 ]! W2 f7 i9 a8 ~: k- e
/ I2 e5 V7 ^$ D7 j2 C- n5 k3 ZModSecurityEnabled on;
{! e6 A3 V! S9 l# S& wModSecurityConfig modsec_includes.conf;
" _# z S0 @: a/ ~修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
5 ^) X2 V) J0 G9 c. N5 {' c; @/ G" o f6 ?# {
7 t- G) z( x* F* C' Y6 l9 _
$ vim /usr/local/nginx/conf/nginx.conf
" {& \6 p- |6 z+ {: x P9 }* ^3 C" J, \6 x6 c& t) I* x
6 e% b5 g3 [4 F# v) e# wserver {
; M/ c6 P5 W" Z0 V( F( j listen 80;- s2 }$ C* h; l. H* h
server_name example.com;& n0 f7 U/ g7 `1 S; W, Y7 E
; F& _" H$ o1 C
- |0 Y6 y) b3 j, D
location / {
o. r1 X0 N4 z: e/ q. x" e8 u ModSecurityEnabled on;) C5 O m* [7 P; n J
ModSecurityConfig modsec_includes.conf;
! F+ |* h3 i, P# V9 y root html;& ~6 N$ f) { d: @
index index.html index.htm;
' k) q d; M" X; ~" O }& g, G3 s+ n. l0 c
}
% U- C; {* H; g! _" B3 J8 B使用动态模块加载的配置方法
+ I8 t: e' S; o在需要启用Modsecurity的主机的location下面加入下面两行即可:/ i" t# ]3 {. A3 y0 w
8 s$ i0 k" X! n- k, C; E0 D
; `- w8 Q: |" `; x( p& a# [0 ], w! \
modsecurity on;. _) r7 k$ U- M0 C; F
modsecurity_rules_file modsec_includes.conf;
: m) n6 r. [$ {7 H5 R3 M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 O) M( T+ q3 r6 G$ e
" B# W) {4 [, Z9 M V% x/ t
$ {; j" Z6 \2 w1 T! ?$ vim /usr/local/nginx/conf/nginx.conf
( ?' x8 r9 _1 f4 X! Y! Z# V b: h7 z) D* U3 p
, y& y- x5 ^! m- G/ }7 n x; f
server {
* t$ K9 X% ], l5 F: a( x) F" Z5 W listen 80;' q8 x% p; U' {3 M( V
server_name localhost mike.hi-linux.com;
5 ^6 v! q# X$ y/ g0 x: ~9 q access_log /var/log/nginx/yourdomain.log;
\8 W! N( y ?) v+ G* O- Z; w6 B5 {# r
# n* r. N! Y1 z. ^, `2 h7 K
location / {
e) v, Y7 G) S- ? J( m8 p" f" |+ c+ V/ [
4 C, D9 _- l0 Z4 E modsecurity on;& y( y) _% O& `5 B
modsecurity_rules_file modsec_includes.conf;
9 T$ U& h4 N" h& n! i* }+ n& l* v% f$ J root html;
6 ~3 r- s! e% [1 ]1 q index index.html index.htm;. e9 d- i" J h* |8 ~8 R
}9 [, r% V, i) J
}/ ?" g6 z6 q( }" w( E! v' ^
验证Nginx配置文件
7 }, J, u% \6 ~8 ]$ /usr/local/nginx/sbin/nginx -t
5 i4 V7 l+ _' r0 S& d7 D" ~nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
4 v& K9 s3 ?5 A3 n4 o/ Wnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful1 z" ?" e l8 D( q# r
启动Nginx. A) j- k/ R0 {4 J* L7 y
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf- Z0 N2 _- f# z
" x# d9 W2 j, F; _: _测试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能够战胜更多复杂/未知/混淆的攻击模式。
; d6 j9 j( U6 y |