ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. @% ?0 B: n; U" a# O8 H( ]: M
/ ~2 u( a7 ?& H* P3 n
+ F; |* V- a4 T' U5 ?
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
3 u% K1 @0 T" L) `
, `& i8 G" T, N7 e
" U0 c2 k% l' _; ]4 i什么是ModSecurity* X( {5 v( ]1 i2 R3 E" B9 n
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, |) C3 [! f$ [. a Z% j) B2 K, r- ?1 H4 \0 P
, ]! b1 h2 c* {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! e& R9 {, x* J$ w5 h
6 y1 A: ?6 I4 S h* ]& e
( F; K/ w0 n" H/ e1 w6 M
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
( N8 O, v: D2 D# @, s; O. L! b+ \
7 J3 H- p, \9 e; P3 j- F( f* J% p7 l) E. R
官网: https://www.modsecurity.org/2 Y$ k+ {( M7 [5 g' Y& {& I8 w- Q
2 T5 o& B7 G3 J' y/ g9 n2 N1 W
; p! n% x& ^" X. ^, F6 B什么是OWASP CRS
; n0 B0 o7 R) B/ m$ TOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
9 O3 g# |. b% f" C7 ~; {: J# F" ~
5 A/ ^& T9 W! O: R+ g" ^
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 V6 ]; B% k) a7 r3 i8 B2 F- g N) V8 j
1 s1 s/ A7 ^1 A
HTTP Protection(HTTP防御)- m$ Z2 {" _( {6 R1 V" {* Y+ o
HTTP协议和本地定义使用的detectsviolations策略。5 T! t H% x$ q9 s
6 y+ S q2 `. @: ~* S
+ O, a' o9 @5 X/ hReal-time Blacklist Lookups(实时黑名单查询)
$ o; M6 I. s) u+ P/ H利用第三方IP名单。% N& _- g0 H3 E2 N1 S& y' Q
! |/ W2 t, P% J# e
5 N, U! a/ X% B2 d! Z5 {7 \1 @: n& AHTTP Denial of Service Protections(HTTP的拒绝服务保护)2 u6 q4 |# X3 u; l3 i7 X, ]
防御HTTP的洪水攻击和HTTP Dos攻击。
* a$ W! Q$ F4 e* }6 F
5 B% q8 F5 k5 p$ S& K
8 |( b6 T: i, Y1 H9 y# _Common Web Attacks Protection(常见的Web攻击防护)
- x H# E3 ?* l2 b检测常见的Web应用程序的安全攻击。$ c7 x- X8 F' f7 t6 f8 ~
3 X' f2 _8 f% l) T7 }
9 r3 N6 i" v# ]6 @+ QAutomation Detection(自动化检测)$ W/ D! m2 N* A7 i0 d
检测机器人,爬虫,扫描仪和其他表面恶意活动。
S" t$ {8 E# }2 `5 p+ J' P, ~. o" B1 u
; j# c; T* m% ?7 `$ aIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)/ c. \5 V$ J- q
检测通过Web应用程序上传的恶意文件。* y" K% u& T( W: j$ R4 r& ?9 W9 A
. X+ s/ G5 \; O$ O9 ^6 T) M/ v' r% h5 z$ Z% e O% n
Tracking Sensitive Data(跟踪敏感数据)) S! X2 R/ X* n; s) I9 r, v$ g
信用卡通道的使用,并阻止泄漏。
4 M$ t) m( x$ u" U/ Q) m' p' h# a1 a0 }! h" L) t5 @# {) L+ {
* ^( I4 @) e4 A. L' n" E
Trojan Protection(木马防护)
$ E8 ?7 X0 f' @) y' O( {检测访问木马。1 x: K0 g$ Z4 m) N1 `
9 G) d4 Q4 l- Z6 U
4 K* \' r* [6 @. }8 rIdentification of Application Defects(应用程序缺陷的鉴定)
" C* \* }3 K, s1 L: I检测应用程序的错误配置警报。9 V A% Z3 g% J# z
4 q& C* y- ?( n+ ]6 v
. M2 H$ y( U1 ?Error Detection and Hiding(错误检测和隐藏)
1 Y4 [6 [1 e: _. i$ f4 o) w/ ^检测伪装服务器发送错误消息。! g- K, v4 J! \$ |: J' z
( j. y5 U& ?& z/ t0 t$ C7 \1 o2 o9 d6 u Z+ f1 I' o
安装ModSecurity
: `& P4 E* b( R$ f6 n, G& k- Z' U. f软件基础环境准备
9 ]3 _1 B, G) [8 E下载对应软件包
: k9 P4 X ?, @5 y5 p$ cd /root
% |" A6 |* z# O/ l. h& ~( z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' t6 c' ]8 V& u" e( w+ P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
5 H3 x2 S; l3 f- r安装Nginx和ModSecurity依赖包
8 Z" B9 a! C: eCentos/RHEL
; L3 C4 V; J* j( _( j3 g8 P8 S |2 R/ j) B
1 o w2 r7 Q1 Z# P5 \- D6 t$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel* `8 r o8 B, T; K
Ubuntu/Debian
0 Y: O+ f3 e% b& S7 o/ v5 v0 q; H9 y6 ?( `) t* k
9 P% T/ ?$ R+ ~4 S" p5 s- 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" Y9 I% x3 E3 P# R A$ g
编译安装ModSecurity
8 m5 {5 _& A( f7 R" aNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。5 ^$ o; A& b: U: b9 M, F
5 v& d: n T4 t0 L2 d
~7 y" f, J* \- |7 i, t8 m
方法一:编译为Nginx静态模块
9 X. C; M% C a9 [6 p$ X1 e6 v8 Z) w) G
) g. s' `5 \" p6 ]5 E* ]1 X% f, m编译为独立模块(modsecurity-2.9.1)2 T. A0 J' K% S5 w k
$ tar xzvf modsecurity-2.9.1.tar.gz
3 Q' d% f- ~+ J% s* d9 F$ t$ cd modsecurity-2.9.1/" k: i( S' z7 C& p3 V$ n0 \ h
$ ./autogen.sh
7 w& F4 V* v$ ?( ~% A* t" \; @9 A+ m$ ./configure --enable-standalone-module --disable-mlogc
! o j" F) G. [/ f$ make
8 M" [& E/ H* ?8 j6 o3 K编译安装Nginx并添加ModSecurity模块, g( M0 g7 l5 X! d* T, P
$ tar xzvf nginx-1.9.2.tar.gz x6 w1 I2 \5 F1 q7 ?
$ cd nginx-1.9.24 W% Y" Z/ G; Z
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/ V1 W4 ^3 l) Z
$ make && make install( r2 E3 L, }2 d7 N0 d8 k# p
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; S+ I3 @% F3 Q; ^
5 ?4 a- B3 _# z5 J( w+ V
2 [$ ^: O X- s编译LibModSecurity(modsecurity-3.0)" u! E7 S/ a! D. [( y# e
$ cd /root/ }5 {$ l4 [/ |7 x, o( d
$ git clone https://github.com/SpiderLabs/ModSecurity
1 A/ G: x, j6 \3 h# h$ cd ModSecurity
3 L6 f. `# g# C$ git checkout -b v3/master origin/v3/master
: B! n1 [# \* f" n$ w4 X: I$ sh build.sh
' \; Q! e0 L8 X' T' i$ git submodule init
7 V& h F# o$ {$ git submodule update" n [( s% `" h* A) h- I( @
$ ./configure6 j( W) x( j' l! P- P0 x3 U6 w
$ make, s' g( p$ @4 g" i6 ]0 g
$ make install/ {7 f+ b: M. R% f3 p, u. p
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 }8 ]' l+ [1 _: k# i9 C* }1 O4 I
3 A, z% O1 n* b w0 [) Q
2 J8 Y; H2 k* x$ ls /usr/local/modsecurity/lib
8 Z, F$ a$ j$ p0 h0 ?4 u* Ilibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0" p7 }$ z- }" n( H1 d: P( H/ k
编译安装Nginx并添加ModSecurity-Nginx Connector模块
! l; O( e: N! K8 w* x使用ModSecurity-Nginx模块来连接LibModSecurity
5 y$ ]5 v/ N! s. ~
6 ]. ]4 n* g; ^
- b5 a" p. T8 f# N L$ cd /root
: p% r# _; x7 z/ y$ j6 s$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: P8 f6 g: j, Z) ]6 ]( _( ^5 A
$ tar xzvf nginx-1.9.2.tar.gz$ P6 G. {1 K0 D Y/ n
$ cd nginx-1.9.24 { n* \- B/ A2 _
$ ./configure --add-module=/root/modsecurity-nginx
7 E" `4 s0 T* q5 N: T- E. c$ make3 o L9 J6 n+ I+ i4 A
$ make && make install; h5 L" w. F, x
添加OWASP规则 ~/ t- y. d1 H% a
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
' i' p1 V( O* M$ k1 ^- S/ G7 W; r+ N9 m# T1 j
, @* @/ i, ~8 O
下载OWASP规则并生成配置文件
6 D2 Y& {; x" H$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git! p7 T0 {. v2 b+ I: Y7 D- C
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/* N5 @- @( ^6 H. d
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 }+ y; c5 H* u3 j5 {) _6 z9 s6 a3 D
$ cp crs-setup.conf.example crs-setup.conf
/ k [! B* D& v- k配置OWASP规则
: `* o7 Q( i5 o! Q" X编辑crs-setup.conf文件8 l" U5 t# h1 ]9 j& w+ q% S
1 M" E! G" P" W) X: P
! E% _1 x. \6 z* G* N$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 \' n" o6 J0 d# N1 h$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: {7 s/ f+ a6 g0 O1 n( I1 }
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ J1 z3 X3 ~4 ]9 ^
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. S) q' Z' d# R6 b默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。5 L+ y. w! @' t, `) ]! i( ^! p% K
- ~7 a0 Y _$ j3 c; z
+ e% `! p/ L0 |" T2 Q; |
启用ModSecurity模块和CRS规则+ M* F) ^# J. K# u1 ]: l/ w
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 x& l8 e6 M' z5 W. i6 q I6 C- n' G# l7 b8 m: k" M
3 ^7 V, e8 O3 J3 O
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 r7 m) B: p+ b3 n: i% w0 Q) W% C, Z
O6 G; O5 M1 s; x/ b0 |
$ cd /root/modsecurity-2.9.1/" g7 {( E' a# G
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ( S0 u( o2 R* H. p! k- n
$ cp unicode.mapping /usr/local/nginx/conf/% {! a6 P$ M2 ^9 G- M4 l& ?
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。) X( G. {$ t( i( Q) v6 B
! V, W( \3 L: ]1 w- |# P. D
1 Q+ p, j+ w7 U+ v+ T. D* A( _$ vim /usr/local/nginx/conf/modsecurity.conf) e. }) B. t8 z( y3 M
SecRuleEngine On
. ^. H/ k, G8 ^, j% {ModSecurity中几个常用配置说明:: _% q8 M% e) Z( q# q, H$ ]+ ~
$ ~+ U& U4 U$ A( V9 p% p
+ m3 [. L& ]4 x9 K
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* W% }6 K: d5 {% O* ]8 w
6 H9 V' }8 ~' f& ?
. f7 C# `1 |3 y4 o3 r# C- Z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 M" S! f+ K$ v H8 `+ I6 g9 c# y& |) W/ S
; Q& W1 M4 }& Y) g3 e/ G3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
^7 i/ V" f3 K3 e6 K) F9 }
$ c# b; z6 J& Q/ R
" Q( d$ N& }# v& w+ q {$ L9 U2 F- _4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' }6 D8 d) _) a# k2 m
/ I+ N8 N7 R" n- u! w B: f" @9 r1 }# L' A
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。* [0 z1 u4 i/ J; q1 {# Z
/ T: b$ P3 g2 Q( p
# P! e! F5 _9 c4 B: R3.x版本CRS
$ ]+ @" B: {8 N7 Q$ u$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; S" j, ?7 i; J! |- v7 _; s# 生成例外排除请求的配置文件
6 F& F" R: n0 A; A% [$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
# V6 n1 W& O3 `1 o7 k) j$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. V+ ~0 T4 c5 F; j
$ cp rules/*.data /usr/local/nginx/conf9 W+ C* ^4 b- R' N$ Q) y1 L* `' Z# j
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 |9 |. F+ q5 j5 ~; `) v
" R# ?' _, i: a/ H; ]: m0 j% A* C( |' u* _
$ vim /usr/local/nginx/conf/modsec_includes.conf7 n, h( V+ ~' Y
% |/ T9 H" _7 \7 [) ^2 W1 g: 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
% b4 s N9 e6 N7 r. W. r
, W2 d9 [ M9 d" n注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
3 t/ A& j, m, l4 i, w* t* V# k, S; \& _! y- I- l4 S. \- j9 i
9 E8 _8 ?0 @* J2 R+ P
配置Nginx支持Modsecurity
% u5 H4 v, h* U3 f8 p9 }% S' z启用Modsecurity
% |& z+ w5 l% |. e使用静态模块加载的配置方法
& f& P4 s9 v4 t$ h. F! Q& a1 Y在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 k( w H) L! D! F% L% w1 m: A* b0 _7 X1 |/ w3 D5 y- T- u
4 T$ H1 @1 [! c8 D$ F7 |# t2 u9 `ModSecurityEnabled on;: X$ F @/ L. S0 |, g+ c2 W
ModSecurityConfig modsec_includes.conf;) j0 ?' q% Z( b' x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# [% \; k' V5 Q; ]' ^1 O: a$ ]: Q& E1 J$ E0 Q4 ~5 g9 d: e
5 e* v7 z2 u( N0 V1 B8 U$ ~
$ vim /usr/local/nginx/conf/nginx.conf
& Y5 ~0 e* |( t8 j4 \9 v$ V4 A$ Y4 t& ~% u1 S8 p+ {
2 I# W! h# |9 H+ e, J
server {! }2 U* l. u P$ P/ d+ L! ]
listen 80;
5 W2 l# r) n9 |. t3 f server_name example.com;4 t( f0 \9 I% H
9 v2 U- W8 o4 k' W$ A; m- a
' W4 o; u+ ~' b, P9 }# u
location / {$ Z I" X2 b2 y5 I- j) n
ModSecurityEnabled on;
! g% O* f* w' Q7 X7 J+ I2 ?. _ ModSecurityConfig modsec_includes.conf;+ h, B- D% G5 L8 J1 p1 J
root html;
- h, _$ l- o- c: h5 u* u index index.html index.htm;
; _8 Y: `" y6 y" V }& z% N% m; P5 T& v5 U3 E' X+ g0 S! H
}
8 d2 }9 j" y# H- q x5 y8 I使用动态模块加载的配置方法/ @7 k9 x$ m* T& p- j( I; v {/ o
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. c: V) w$ q/ V2 t; m2 n- t: E ~- d* i) S% M4 A# y! @
5 b g. E) f# Y2 J9 I6 ~
modsecurity on;
. g ]3 M0 J3 R" u% P0 Vmodsecurity_rules_file modsec_includes.conf;% k+ Y5 l% ?; V6 ]4 o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 P; Q* D9 E1 H' l9 |4 ~6 ~6 S
' ]* {6 B# z; @( Q3 m( U
; G; w; O9 H9 z1 G4 ?: |0 K
$ vim /usr/local/nginx/conf/nginx.conf
I8 Y: t1 U+ v9 T- a3 E9 M! x3 b# h: Z2 h, W9 f% D
+ \. S, {; s# fserver {
2 R0 [- K0 ~- W4 o4 g listen 80;
& K V9 h3 q" f5 l+ |$ s2 m0 ? server_name localhost mike.hi-linux.com;
8 T0 F0 C1 L k5 i access_log /var/log/nginx/yourdomain.log;( ^: U1 A2 A( {
! @6 @$ A R9 b' A$ }: S, k2 V* P a1 l7 e: x; r7 M
location / {8 O; K0 v- k0 `* `7 a
- d; {; P; A/ p, u$ \& D$ y
+ ^5 ~ h2 @2 _% c2 U( p( C
modsecurity on;+ V7 E1 i9 W0 Q
modsecurity_rules_file modsec_includes.conf;9 N% O$ h- U1 U$ J
root html;
7 _: [$ V5 }7 Q' r index index.html index.htm;0 `: K* `4 [# B! ]* A$ R
}
0 }; d+ u# N, Q' j}
5 |2 ^! W& c' z8 i* E: ?4 h验证Nginx配置文件1 w0 C3 ~7 K# S/ n' q$ X. F c
$ /usr/local/nginx/sbin/nginx -t
! Y8 L v! ?0 Y) Nnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 Y- @% B1 P# A! R, b- Tnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful9 l) T8 P; _$ @; ~9 ], T
启动Nginx+ u& g/ Z' j! z( M4 F& F$ f
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
! U- t5 d5 |* f( @ E5 ~+ e4 E: W- I9 s0 e' k
测试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能够战胜更多复杂/未知/混淆的攻击模式。
4 y' d: u3 r0 n |