ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。8 w. F+ W% s4 Q; e8 ^
: ^$ O4 e8 \# `( D1 N2 E0 ]" F3 N4 }6 r' } N" O1 c0 |0 l
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
# z, n: ~; e* A: p) ]: q0 ~( L" Y
! v3 P; S: c8 N* s
# Q. c a# L. ]$ Q什么是ModSecurity
* R" g3 B& G6 ?+ |' qModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
# N& X v# `- i0 K6 i
7 {9 P# l# t% j5 Q
5 \4 d8 C) _0 z. l# Y. q& Q' zModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。7 l' }- s& f* O7 N$ G. R; ]
2 z/ t+ f. D4 ~
& N0 C( U- c& ?) S' c* RModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( x ^6 ~) e# v% u
. w3 n9 F5 M+ I$ m6 V) j. u0 m
官网: https://www.modsecurity.org/
' o* b6 i; c. G h, K- S# \, b* N7 G; l: |
7 s! n/ y( ]1 _什么是OWASP CRS: L% ~) n1 u/ b% m |$ M" ^
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
) o( K, U* U) a7 m: T# ~* R, |& ~9 Q
. C6 o5 `6 H c; ]- mModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 S) ^0 h P0 d2 Q# u
2 G2 N3 H3 w1 e. ?# u
% z g/ @4 `0 MHTTP Protection(HTTP防御)8 L0 E1 u# X y8 D
HTTP协议和本地定义使用的detectsviolations策略。
/ D w: b/ s! H* _: y5 M' t' z- F- E" N
4 D4 z5 p" V0 X$ l2 ~Real-time Blacklist Lookups(实时黑名单查询) V( B; {3 |) _% O" [
利用第三方IP名单。
, Q( n8 B, Q. N6 y+ a% O4 U/ Q2 P. }, }; F5 O5 L, b: V& L8 X
/ J' h0 ~/ t0 W: Q8 ^
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
, F: T+ {' _% w/ Z防御HTTP的洪水攻击和HTTP Dos攻击。+ l1 Z, K! y/ |
, `* g' A! O& D7 e
1 ~! U/ j3 a# @; C3 D$ ^
Common Web Attacks Protection(常见的Web攻击防护)
. w# r3 n3 [. R8 u$ T% h检测常见的Web应用程序的安全攻击。
) `* Q# ?$ q4 W2 U* [% |: `4 g$ T
1 @* c+ b' `; r& O8 |& U) a/ E
+ u) U; l) }3 P W, L# E- }% f5 hAutomation Detection(自动化检测); p4 r& F H. N
检测机器人,爬虫,扫描仪和其他表面恶意活动。
( Q1 n$ b% l5 P/ w
, ?" v. Z, L) [3 J" s2 @0 V* z
6 w( w, i$ \) C" E2 QIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
' g2 M9 j% [3 ^$ n7 l) b" `: g8 Y) q检测通过Web应用程序上传的恶意文件。
* E! ^ ^& m6 ^' `/ X3 G7 c$ T
7 J' ]& B! e) a* p6 i1 M* l# S! C" [" ]: F# F4 j& Z/ D
Tracking Sensitive Data(跟踪敏感数据)
" R: R8 v6 V# b! p信用卡通道的使用,并阻止泄漏。" T/ d7 }( y' y# {! I% G9 I- \) x, h( M
2 q3 F# |' ^2 Q M' z, @% q/ w: R
! l' g& [; W( G2 TTrojan Protection(木马防护)
( f6 q: I9 \6 {) {6 d5 J" B检测访问木马。4 L1 r" e0 k; X5 S
+ Q1 Q8 |# r9 d- s6 Z! A) {* j
/ t6 A q! O- @( j1 cIdentification of Application Defects(应用程序缺陷的鉴定)
e8 [' t, ], R4 `$ ~' H2 \% O1 X检测应用程序的错误配置警报。4 i. d8 v8 g1 K! |) n) c) a
3 }9 Y4 ^9 Q2 m4 m* g0 f, U% C9 m6 L) u! |" W/ M
Error Detection and Hiding(错误检测和隐藏)
2 L2 u/ U% j2 q4 n+ B! J检测伪装服务器发送错误消息。3 [- e, c. r1 D! d' J
9 g' G' l6 ]2 O7 f
% W ]5 M' C# R1 l安装ModSecurity: K/ `) |# o% I
软件基础环境准备
6 r# P1 f7 C: _$ S7 l下载对应软件包2 L A K- m8 b* D; x! j" D
$ cd /root
0 A! l- V% u: a& e7 ~$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 [. X- h9 l6 J5 R$ k9 |- B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# w9 i, u' J! ~5 J4 U
安装Nginx和ModSecurity依赖包* t& L" W& g3 }- C
Centos/RHEL- {2 O, h' |; \! N7 @9 M8 V
0 N/ [# a8 L) d! A
! {! d' R' a2 g7 i- |" q# n5 { I$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
+ O3 H& _% z3 @2 }9 @Ubuntu/Debian
2 d+ t* m" w+ u. i* {- e# \+ }
+ y& A/ y' S' `$ K- p$ c+ }" i/ m1 e
$ 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* M& o5 u1 {& h# _
编译安装ModSecurity
4 T! h3 F2 Y: o) S3 qNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! Q+ O) q! j3 e# X
3 B4 O" r+ c3 l' |2 I8 t0 v
' q3 U8 K. B3 c方法一:编译为Nginx静态模块- G3 u# V5 F* y2 ^' H/ j
! ~9 D$ G1 j f/ j7 R% k/ y- r
6 Y6 f5 j8 x% X: z& V) q t: J编译为独立模块(modsecurity-2.9.1)3 h7 U5 Y- C2 H, Z' y
$ tar xzvf modsecurity-2.9.1.tar.gz
( K$ M @# L+ ~# {+ {. E- I$ cd modsecurity-2.9.1/
: @+ e- |8 Z4 w9 M7 t; B4 \$ ./autogen.sh9 l8 x h# X R; g! V
$ ./configure --enable-standalone-module --disable-mlogc
7 ^" @! \7 x0 X! l6 @$ make
& [ r' n7 t, i% d3 j编译安装Nginx并添加ModSecurity模块! }# j6 v6 b3 j+ U$ `; t
$ tar xzvf nginx-1.9.2.tar.gz7 x/ Y+ X& \$ }4 l$ p8 Y
$ cd nginx-1.9.2
5 A9 q8 T6 d( S# o$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
5 N D7 X+ u# `$ make && make install
7 J+ e: W- E: g9 ?5 j方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* ^+ c1 W) G. Y1 x) h
/ c% N( ^) {' h e F& _, x: {
' A5 I( ?) f* e# {- @5 O编译LibModSecurity(modsecurity-3.0)1 O0 q% x* j$ s0 i9 Z' y% Z) }
$ cd /root
2 C; q2 z5 g. j& R% g+ g* G g$ git clone https://github.com/SpiderLabs/ModSecurity3 w/ v2 Z7 K( Z6 T3 G
$ cd ModSecurity
6 u( j- s4 Q- L) C% p" G$ git checkout -b v3/master origin/v3/master
3 `4 Y& c- r1 W; C8 m2 B9 Z! u$ sh build.sh
( h) ?( D) A1 r. V$ git submodule init e5 C+ c" a$ o7 v& l: K$ c- \
$ git submodule update
" x' v1 H' n3 W) c9 S$ ./configure
) V* l3 v8 q% x+ x$ make
7 y& P# I8 I( n( y$ make install2 H, p D* v4 j4 o4 f& ^
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
" S4 y4 d8 B) f7 D: E7 u: `# o4 p8 t' f/ j
1 k) D- M$ z# t. I% A, _4 `$ ls /usr/local/modsecurity/lib
0 x4 p7 _) c$ t5 j4 `6 Nlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
3 {/ e( }$ x4 |, z/ @编译安装Nginx并添加ModSecurity-Nginx Connector模块/ \5 I7 X& s2 q8 W. U- I8 X1 P
使用ModSecurity-Nginx模块来连接LibModSecurity4 v* h5 q* o7 n+ p2 m3 F3 O
6 Y# {, W3 {( {3 s( [( y
$ p( A6 }' g* s3 V$ s+ a% E# h, N% v$ cd /root, m$ g6 V; U, f, H. E8 d
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# \6 |; w0 ]# {9 ~" ^/ E
$ tar xzvf nginx-1.9.2.tar.gz2 C9 [+ o8 P; h! J
$ cd nginx-1.9.2
; G, W. L8 E2 N/ A$ ./configure --add-module=/root/modsecurity-nginx; O6 k c( [6 r4 D9 G! G$ v
$ make0 N Y. ]0 v8 J+ n6 G6 x0 b
$ make && make install% ^+ s. F0 Z2 M- l
添加OWASP规则
8 g$ M& J% n, G6 F+ h8 i' i3 QModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。1 i! ^9 v6 K; s. K, Q$ ?
6 |2 p6 f0 C( V. B; {% S
% U; A$ K4 d) \7 l* {0 o下载OWASP规则并生成配置文件1 S0 _' `- Q1 R4 q2 C! Y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git1 m% E' `4 o- P" d' M) a! O2 ^
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
! N5 H/ Q5 m/ H4 D! u8 S/ r5 J$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ H' J' U6 D+ q$ cp crs-setup.conf.example crs-setup.conf: S& f0 X8 B9 R0 g
配置OWASP规则
2 r4 G; s: v) E7 T7 O( A5 E# U: I编辑crs-setup.conf文件
+ F2 K- }( M+ A' Q& t. S0 k6 c& K/ s0 ]" d5 p, b) L4 x3 @& Z
) s2 f9 a) f. e
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf( y% A; }1 `7 j8 c
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& l! A1 J: b. K
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
8 I1 P, W- L9 v" V' F$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
1 W3 C0 @. o/ c2 q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 h3 O- f: [, ^2 _' K
S3 R) t6 n$ x' Y: R- o8 I5 W; D2 A# B9 M& o- c
启用ModSecurity模块和CRS规则4 q, X Y; C) z! V( u
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. X- ^8 N- ~+ k* {' B$ J1 X7 ?
# G5 x, L* f) K$ }4 ?modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 N9 u1 D+ g4 M# m& g) F0 C* K5 {
2 ?' h. V6 p9 j; a
* U6 P% l6 j( l& I# b/ K$ M
$ cd /root/modsecurity-2.9.1/
8 @1 U' J( W4 i6 b3 K% R0 H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
* _% o* A8 k! G! `$ cp unicode.mapping /usr/local/nginx/conf/6 E2 Z- L# C2 v3 M$ h
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。% g' h- T9 B/ J4 v/ W# n
; P0 V' f6 G8 _3 S3 W% U
4 ^0 M4 W3 J: T. E& l( i- i$ vim /usr/local/nginx/conf/modsecurity.conf
: c: _" Y0 C( ?) _; V) kSecRuleEngine On
# l4 ?' k( r1 V0 e i5 {ModSecurity中几个常用配置说明:
: j0 S# _7 `; ^1 A# n. K1 N( Z* _7 B: ~8 @3 k
, B) d( J% l/ A1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. z0 c- U6 f9 K7 n8 u; L H3 W: D% m2 N- a- A) w. E4 q+ i
5 r0 n% S: }. W! J2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
P" ^0 M- s8 [+ M
& e& a# O4 v; N. F8 A$ i- h {
0 O" z0 m+ r4 h5 L- f3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
t8 T8 a6 I! }4 k, R
$ w' M# g3 q) K" q9 p- m2 z& ~8 Q( m' U/ S6 x5 L
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* @% ?8 E; \7 O
1 M0 I% W! M% D8 l
2 B. T- u/ J) q: n! u1 A在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; r7 |0 ?% ?# z
( |# c. O, W5 U% |# G/ a) |& l+ x$ E8 G' F% t2 O3 B# ]+ ?" U
3.x版本CRS
1 u2 E) j6 v5 A0 Z. X6 Y+ p) t! X, e6 Y$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ U3 O: h; b+ P, x6 V# D( I1 H
# 生成例外排除请求的配置文件
4 X+ K% `: q! e+ E0 T$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 l. Z; X0 B% T8 o0 H" U2 X/ Z
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf J: D2 m" B% z6 o E( N
$ cp rules/*.data /usr/local/nginx/conf$ _# Q) h& c. S$ q" K. ^
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 S( }/ M. d5 @3 }
5 s' b1 }' X! x0 f8 z+ @( l0 K
4 [. f6 A `6 H$ vim /usr/local/nginx/conf/modsec_includes.conf6 W8 {) w( W4 X, \+ X$ X
' @9 I* m" {$ H1 P. ?. _
[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 ; o8 Y1 s1 [ B! R/ p' E
( R' e% v' q# `. M- K; Z注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。/ E. X$ G- m/ _* K! i
2 L/ y, c3 b2 b: r
" C$ p7 ^: N! d
配置Nginx支持Modsecurity( D; Q; t, ? M) p
启用Modsecurity
; b, W, u! C6 F使用静态模块加载的配置方法
& B" ?3 I/ f2 d在需要启用Modsecurity的主机的location下面加入下面两行即可:
' s0 D. u% o2 j. `. p: ^1 i
: m5 t; a# k' s9 l
, B2 h2 H/ C) S z# ZModSecurityEnabled on;3 n9 w, z9 h O: A# R
ModSecurityConfig modsec_includes.conf;* C0 }& t7 U* L G6 _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
5 I# N5 M2 r n+ l7 V- l1 H! U3 ]( @) w& Y, S8 U0 K0 u, N* r
- H4 A/ h; i7 B$ vim /usr/local/nginx/conf/nginx.conf" e+ f: C& B% T' ?
6 E7 b- H/ A4 Z; h3 s2 ?% x& k6 J& g. @+ G
server { r6 b O7 n0 k' N! W) t# _ |1 b6 x
listen 80;7 @& C; j6 G* q. g
server_name example.com;5 U* }* e6 g( E: {( D+ u6 ?7 [, Y
3 t3 i7 C3 C2 u6 j0 z6 E% w: H% T# F( d* v
location / {7 E. [; R1 h9 j% X( c R# u1 J+ c @6 [
ModSecurityEnabled on;
( m6 p; s& K; G5 C ModSecurityConfig modsec_includes.conf;0 A+ A3 |" p. J3 M
root html;- X% E% d1 T" `8 G
index index.html index.htm;
" F. Q+ }7 N6 ~- V8 b9 Z }
9 V, f/ V6 x' x' U% y}. [4 ?: w2 f7 [# G* z! |: t
使用动态模块加载的配置方法 d" {9 P9 {% D8 N! Q z7 N
在需要启用Modsecurity的主机的location下面加入下面两行即可:
; ~: b# x) t8 ?! L8 F# y5 f6 J6 T$ N8 \/ r9 z
! n+ |% X/ c' b4 o8 A
modsecurity on;
- Y2 T' S6 }% z( t, [1 Jmodsecurity_rules_file modsec_includes.conf;% D( `" W$ l+ D. n' ~6 |' ?* u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' @& A- J# F8 _- O7 z: @! |8 D
7 i9 t, D4 ]9 y* z) n3 g
$ v6 K- S" }1 a9 A& _$ vim /usr/local/nginx/conf/nginx.conf
, k+ e3 a! B8 S5 r. T' ?
/ B1 W' n& ? m# C* @: {% ^2 _# e- D, S9 M
server {
& ^" @% F" ] \$ a: d listen 80;. b5 u5 d5 D: M) [, e5 r
server_name localhost mike.hi-linux.com;) y3 _1 d, `7 w# G
access_log /var/log/nginx/yourdomain.log;/ L7 @1 B" f* Z8 x* o8 K. w! K
6 }9 U8 i- q; c, S6 s
' N5 z" v! D& N: m: i4 M location / {
, c3 |3 D9 r4 @0 U# I5 S
% z7 h4 M& }! G$ c5 [) x7 t$ @% n; c2 b3 t2 l$ `, e1 a
modsecurity on;' c) U6 x: m6 a
modsecurity_rules_file modsec_includes.conf;
7 w3 I# ]7 }: o2 T root html;
* V+ D! W' \* c3 }5 Q1 H index index.html index.htm;
+ D& U- C9 q/ ]5 p& i3 [}
" r+ s: \. K; M}/ R% X( N3 h. ~5 T5 o
验证Nginx配置文件6 _1 ?4 u1 O: R7 ]2 c
$ /usr/local/nginx/sbin/nginx -t/ j; g; \( j5 W$ _
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok; t7 S' `# g% u9 r- g
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ N! M: K2 _0 A, H+ p4 ~7 o& S
启动Nginx
) R% o; z- y* y1 M3 ^6 Y L$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
0 A. F, n( d+ }2 C; E0 s& G1 l5 R8 y% d, ?
测试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能够战胜更多复杂/未知/混淆的攻击模式。
3 Z# V7 G' @5 }( d5 F' \ |