ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 y7 [6 v9 R2 _8 }: s. J# {2 _: s P9 A' i2 u6 J1 G- ^
4 G1 h$ F' ~% `5 d: m1 a7 n: I
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。! G7 B- [; x0 E- Y, s8 k. ^
K9 H; o3 w9 Q
/ s" [0 A) G% v3 Z) g什么是ModSecurity2 f- ?/ q8 L( O8 v6 x0 g7 J# b9 J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
) Y! v1 i# S2 L! W3 q# q: Z& y; o: X2 h! C2 K& g
% R+ }" |3 z! t8 b, E
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) O4 @9 s- ^0 u6 j: ]- \9 \
2 L. l {- I' I! p4 f% m; k6 H4 K* b4 k. v$ j% {' V9 L% b
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
9 N, K/ j/ m' O
6 U0 C, A( j9 u9 ~( W( j" C/ Q. x9 H6 ?, U7 E) g; b9 O, ^* T8 d, Z
官网: https://www.modsecurity.org/
I. J! f( M% [3 W( h
! B6 p7 K) b; `6 U5 D1 {: v8 E3 b& ~/ i4 w: O8 V; |0 B
什么是OWASP CRS
, h8 k9 u h9 E8 D$ [0 A- W6 I- T% R: IOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
( D+ v3 |' H8 X* ?# `3 W5 E# K& U
; ] u3 Y0 h5 D {5 z7 A* R7 D' W d- m+ O+ `* ~3 Q: c
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! T, n) v$ t8 J# N4 U9 ]% w' V$ a! _8 \+ A& \! R
+ A; @+ I+ O+ s- ^# i/ b+ M" VHTTP Protection(HTTP防御)' Z6 ?1 _9 T6 e( l
HTTP协议和本地定义使用的detectsviolations策略。; G1 x' Y. D5 g( @ N
& N1 h$ T/ U; j3 Q. k4 I2 Z/ f3 X, N( D# A3 Q: C# y- F/ j
Real-time Blacklist Lookups(实时黑名单查询). j* J. ~3 d8 i; O! ^6 o1 Q
利用第三方IP名单。: }( E N4 d& Q( c) d9 }
7 h& C% j/ f6 N" G3 N2 v
! `) W0 X* i4 y& THTTP Denial of Service Protections(HTTP的拒绝服务保护)1 {' T1 f' }# e+ u2 i; ]& ^3 g
防御HTTP的洪水攻击和HTTP Dos攻击。
& h: S! U9 Z% z* s+ {1 b/ o! N$ |& X% @
. B0 f2 @8 G. [' g, Y3 j vCommon Web Attacks Protection(常见的Web攻击防护)
% u! E; K, X$ d6 w! s* V1 |, S检测常见的Web应用程序的安全攻击。
; O: _+ X, _' R, b% M, F& V" p; D- S- n+ h
! \- \5 D7 J( V1 UAutomation Detection(自动化检测)0 r. {' N+ V& E- J
检测机器人,爬虫,扫描仪和其他表面恶意活动。
' h ]# n7 K2 N! {
# J0 c- A- F; z x, q" }0 b9 {! S, n) y* r0 i' J2 A2 p$ Z
Integration with AV Scanning for File Uploads(文件上传防病毒扫描): S: u3 r/ i \$ ^5 y3 P
检测通过Web应用程序上传的恶意文件。
~2 d7 @$ B0 s/ F0 d( L e- `" U' P
! N" S/ B+ H/ E/ u* k; S( \2 X" T
Tracking Sensitive Data(跟踪敏感数据)
& ~6 ^5 J f+ P- C% k/ D1 R信用卡通道的使用,并阻止泄漏。
4 R( c7 c; C& x2 n% o$ C0 {) B' B+ E6 Q
# X/ u+ c( b! O& f
Trojan Protection(木马防护)
) n" X6 e8 l7 d1 X: X8 j检测访问木马。; F) C, T! H( ~$ S0 C/ m8 h
$ S6 u/ _ d6 W
% u0 s& A6 v! F) x7 @$ e: b
Identification of Application Defects(应用程序缺陷的鉴定)* k' I: }5 `5 u8 _# z2 N
检测应用程序的错误配置警报。1 M# T; Z! f, Z: e
. ^. X" }5 L4 A; k7 p3 H
& p. z# l' `, e, K# P! jError Detection and Hiding(错误检测和隐藏)
) c: b: N7 U; ?3 r; y检测伪装服务器发送错误消息。2 V6 m' I+ U0 C# ?7 f6 s
/ Q, c M6 _7 u
& X/ r m- n0 h* P$ d' g安装ModSecurity
a& Q8 P, l- ?% C! a& w软件基础环境准备
8 a# \- F4 c" L/ H6 o下载对应软件包
Y' i2 W/ r& Q; d2 |$ cd /root% i# `) I3 `5 {$ J7 H5 J
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* x5 e8 r" A$ @- 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+ d3 J$ Y0 N9 ?/ d. e
安装Nginx和ModSecurity依赖包
1 ?% @2 P$ U* l8 m' F8 P5 BCentos/RHEL: O5 l( B5 y9 e: w* O
; B" y' c: z/ T) c
) J& }0 I8 m5 t7 K. P. f5 }& d$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel) F: m5 |3 |9 E
Ubuntu/Debian
* K6 K+ W, z/ l% F+ o
! [0 q* x9 `5 `( v. K( |7 T! D% s2 y+ G: U/ J& N
$ 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
+ q+ G# |' b8 {编译安装ModSecurity9 K, m' N, I# v) r. I* X3 @
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。, {2 z; S( C& o* w/ i$ }2 i
2 T1 r( H& \! F( {; @, m; `# G* l- @" i6 X J
方法一:编译为Nginx静态模块6 e- {% V) @5 A! s' Y9 T: I4 n
6 ~6 G; G+ M, J
: i8 i4 @. S. c9 q6 F$ P' \编译为独立模块(modsecurity-2.9.1)
( h8 ~$ ]& v% L- j& U: Q. i. k0 [$ tar xzvf modsecurity-2.9.1.tar.gz; J, ^) @6 s, m7 q
$ cd modsecurity-2.9.1/
/ s4 s8 ]1 ^5 w+ E$ ./autogen.sh+ l" j$ J7 H8 }% f5 P
$ ./configure --enable-standalone-module --disable-mlogc
5 J+ D* W8 S5 Z7 ]* u$ make+ y3 R5 `* Z& ^ G( x
编译安装Nginx并添加ModSecurity模块
( D/ C7 i* h% L% t$ tar xzvf nginx-1.9.2.tar.gz
/ [/ K9 _$ W6 I$ cd nginx-1.9.28 P+ C! Y. y9 `& s5 d* L
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# O, i, J/ X4 i$ make && make install4 ^9 C& W2 g0 c2 B
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- Y( R0 }* J4 p! G* D
) Y1 G) I/ |" j: b# f* }% T# Y3 n0 ]4 l
编译LibModSecurity(modsecurity-3.0)
- R$ F5 V6 o3 z4 d. B$ cd /root4 s* v4 p9 g$ d4 Z8 w$ Q: j
$ git clone https://github.com/SpiderLabs/ModSecurity, n. Z# a, M' Z2 E' X
$ cd ModSecurity( A6 k" N0 \4 V% h& \3 {3 ^) U
$ git checkout -b v3/master origin/v3/master8 z8 ?) ? u9 g9 ~0 P% M& C0 s/ I
$ sh build.sh
: X) b6 P" A8 U$ J" H3 @$ y$ git submodule init! l& \, v& ^8 }& l3 P) p4 L
$ git submodule update4 l& ?" ?0 }/ @
$ ./configure8 L& G9 Y& P7 }$ ]" p
$ make
\+ E) z: K! X5 L: E& x$ make install8 V( T7 F+ W2 M# ?. H
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 J7 C7 }% u+ [' U: }! ]' [ R' l$ O# ?
; \: m* I2 Z" R
$ ls /usr/local/modsecurity/lib
3 \5 \! Z& H1 `0 X r+ tlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
( `4 |3 G5 T. M7 b编译安装Nginx并添加ModSecurity-Nginx Connector模块
: n+ H. \$ L [使用ModSecurity-Nginx模块来连接LibModSecurity
$ W$ n: m3 g0 D! ]6 ?" G. K) z3 n* |7 p% y
$ t' E! `5 w$ J3 D- l) T
$ cd /root
* i) E" ?$ q, ^$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx K$ j9 K9 r/ |. A$ |$ c5 q% F
$ tar xzvf nginx-1.9.2.tar.gz
6 P( F4 b$ \( D/ [* u7 X$ cd nginx-1.9.24 a6 D, y7 u! h, `
$ ./configure --add-module=/root/modsecurity-nginx
$ z' S% U! ]$ b0 s& P Q$ make' i' ^# v0 m: z
$ make && make install
: m+ g9 @+ Z) a$ Q添加OWASP规则; U0 t" U7 d' H4 T3 a( ?( n
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
# I8 }5 v* p6 d& f P. P3 p& K# D
- `- h9 a/ f Q8 q+ Q2 X' K$ g& d8 F! c4 S- X0 I0 f a! o8 A- \3 c6 @
下载OWASP规则并生成配置文件$ ~9 W0 O! N& |: }8 m
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) C* k: D( U+ H4 k. K# X. t+ c# T
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
9 w" o2 a! S9 a; r" `8 n$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) v; r+ c/ T7 r( K% r$ l6 E' V4 k$ cp crs-setup.conf.example crs-setup.conf
1 h r9 L, f- g' q$ t' t配置OWASP规则; z& d; F. f# E7 ]' \
编辑crs-setup.conf文件 i9 f: ?, `) R1 i- A! @
, |$ C, J* C/ r- D- k
/ ~! [0 a# J( y3 C7 v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf0 | I7 m9 X) ^
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf! S" y3 n7 j: |' A
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 A0 [! s$ U, O2 l) h+ v$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf4 {7 b/ w) b: l; R3 ?
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。) m1 I3 {" b; s& ^9 g' E
% A3 S! ]! |7 p r
. Q1 }4 K, Q" b* B( I启用ModSecurity模块和CRS规则' L# v& I' L- r& y
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; ]/ d: c; G$ i* t5 v1 A+ r, r
, f$ b6 s* z$ @& @5 h1 v2 D8 X3 v c" ^
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& m9 f+ a/ |9 |2 j! |
3 }% L6 _1 o" b3 m; m7 j
5 g$ G: [6 V$ n& ~2 }' Y$ cd /root/modsecurity-2.9.1/
1 |( P, |$ ?2 b2 u( @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf # c% v3 u& J+ l
$ cp unicode.mapping /usr/local/nginx/conf/) {2 Q3 o. b$ o" L" Z) } W
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# l/ I! O; q- t+ t4 q' K/ D
6 T! G3 n7 Z# X5 a) p4 y
& Z7 F* b, m0 u$ vim /usr/local/nginx/conf/modsecurity.conf
7 x( a" M2 Y7 D2 U" L# iSecRuleEngine On
" K8 s- o: y2 N3 A; C6 u; cModSecurity中几个常用配置说明:
. r& w& u8 C1 M* N2 {9 b* y$ e1 O0 V7 {, M
1 v) w* h; L, U1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 p3 I( P$ o" n( I/ T) z7 ?0 u+ I) v r2 W+ a! P
$ [$ V T+ e- G1 Z6 q
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 c, p( C0 |+ y% s4 H1 H0 P2 b
- Q+ i; w8 F$ I
( \4 C. z* C/ F; `3 {0 ?* o3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。" n& M7 R: |8 _8 c: j5 d& {
+ \# W* o2 g2 n. v, X' `# Y9 p3 P! d* W, d3 J6 A
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
, A) \" U6 e! F: |: }
, g- L8 v+ h5 q' K" v0 X
/ Q0 e4 y' S( U+ T% V6 l在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。) g* o0 ]) f5 I. A3 p
& H% ~0 @- k/ G' }3 Z6 T% D- ?- ]* M( H# \, Q
3.x版本CRS% n1 B- S9 R1 K. {# a6 b
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" j# M/ y O1 j$ o& e5 g! B
# 生成例外排除请求的配置文件
; c, Y* u2 K: U& k2 C+ f' M2 T$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
# e* t" ]8 }- e9 h9 m# q8 t6 Y; j$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 r" I. @4 m/ k/ Q" O: f# J4 g
$ cp rules/*.data /usr/local/nginx/conf# m5 D7 E0 U0 e, g4 e
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( N8 _ C1 @" F
: |* Y( p/ u e) ?% \" l9 S% f
! Z3 j- A& v/ P$ vim /usr/local/nginx/conf/modsec_includes.conf
/ M4 P2 b4 L: C+ a8 h& \/ [/ i: l* E3 ?" n2 D
[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
" w6 h1 o6 a) U0 k* k; \6 o {% Z
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; |) n& A6 T. z( x2 |
6 k6 j0 e9 l6 Z0 u7 [ ?
; v2 q ~$ c: d1 z. y7 I配置Nginx支持Modsecurity
& B6 d/ m2 l I5 Q% K) d% ?启用Modsecurity0 b; I6 M, N0 I: \+ x+ `$ i
使用静态模块加载的配置方法
, O6 o$ j6 ]4 n# ]& j3 \$ q在需要启用Modsecurity的主机的location下面加入下面两行即可:, [' T9 z- d$ w+ O0 C; n, g
+ j$ o5 H8 x' R/ u' L; u
4 A* A s- h9 ~* bModSecurityEnabled on;% N1 X* a8 B+ _1 r
ModSecurityConfig modsec_includes.conf;, ]' R: S) m' q+ \8 }5 T* P6 B8 Z
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 ~: \% u9 |+ h( J' A
X/ { s% E, e" P) |5 V' z8 l# {3 e7 D% Y, J0 L3 [. D. l, ~
$ vim /usr/local/nginx/conf/nginx.conf
9 P9 L3 O3 b* R, ^8 O4 H# A0 C6 g2 Z
# U7 {4 z, ?; t9 K* t' C) \; |; r
server {
- |* O: Z# Y2 G listen 80;4 b( q8 v3 V% b3 i" y
server_name example.com;
7 e: D( ]$ q) w E; [9 T9 V# j
- W3 k0 v1 R8 H7 B- y
6 d3 w" I: U4 D location / {
! K: y8 B4 p; l ModSecurityEnabled on;5 u& [1 ^' v0 [) O# x
ModSecurityConfig modsec_includes.conf;
$ g% N& s+ |- [ root html;# a" U, D) q% g2 w
index index.html index.htm;4 ^& C4 q2 ^1 ]" ]0 H( f9 D
}1 j& f5 J- `2 ~
}
" P& `/ H/ E0 _+ U, D" p# M使用动态模块加载的配置方法1 D/ J* P; ~: n$ F8 v
在需要启用Modsecurity的主机的location下面加入下面两行即可:% t4 m& ]% z- \7 {4 z$ E
/ x/ ^. _! b! q0 U, D7 C! k' g! K R. |! [' }
modsecurity on;
$ c( b9 L1 k$ M. ]. cmodsecurity_rules_file modsec_includes.conf;# N9 G, R& U% N
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 C+ ^# N; Z& x' {6 K, g! r" A/ g2 z# G6 z9 R
% v$ |- O6 t' O( o# f/ `
$ vim /usr/local/nginx/conf/nginx.conf
+ D$ _4 L: z2 |& Q5 ]) j1 _, S$ Q' S
$ S% j1 \2 s9 O& Z, m0 Vserver {
9 l: E* V/ S. j% ^; H listen 80;0 X( J- T% ~* \0 @$ M, M
server_name localhost mike.hi-linux.com;/ p: W( T p' c( T1 y' K9 c, J
access_log /var/log/nginx/yourdomain.log;& P0 n" H. k& @! h9 F
8 b; s/ \& S. {+ p' F1 z3 F, m: }/ I8 I9 k1 o5 X: Z
location / {
* R t3 r s; j: w% W. N- W
% W; [/ S; V. L+ t
4 x+ U9 c# ]/ f" E1 ^: D; _ modsecurity on;
' l9 ^4 N" g! J1 P modsecurity_rules_file modsec_includes.conf;2 Q) y* R0 R; ]: l: d1 P
root html;
9 w1 l4 ?+ i; p" V$ H3 y, C index index.html index.htm;) o3 R4 ~( s9 R# p* C5 x2 t
}
% S/ k: j5 M+ \& y$ K}
: F: R5 E* R" B验证Nginx配置文件
# z$ D, l& E$ Z3 U% K1 c% b9 G k! ~$ /usr/local/nginx/sbin/nginx -t- p9 Z- c! M4 j( x& \$ y8 }
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 v: p, g0 o5 k0 H0 L1 J
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful! g/ v9 d) r$ K# ~0 ^
启动Nginx
P$ p$ e& i3 ] X$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 B+ J, I& { A6 e/ u
8 h1 I& C9 p" m, H" O测试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能够战胜更多复杂/未知/混淆的攻击模式。 " L; o$ z9 G4 b/ V- a
|