ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
; }' B6 U3 r+ x
% |2 T) E( w8 I- V$ F* O( n9 ^; w9 x/ a- G& S6 I
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
& k# p. q) K; o9 h6 `
4 \/ p, O. R7 C3 @
7 i0 U) W8 K2 E0 W0 ]- Q什么是ModSecurity
d, P3 m- r* FModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
+ ` {. ^ [4 [
/ Z. h! X# `: P n
. u4 o2 c7 x& R8 UModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。9 K- j' s. M7 ^- ]: ~# N5 y
( J n! @5 X; M: V( M
4 s( i& o6 H6 M$ L. @4 I& H4 cModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。1 k+ p& F( O' x. S! s2 |( j
. I5 ^) A3 d* W5 X
% J- w) u* _ L官网: https://www.modsecurity.org/
7 }2 C, u/ a% |7 }
. }4 P/ H: K8 }) G) M) R5 {; j# M
' ?: Z+ B7 `$ F; E什么是OWASP CRS- H% U; [2 n4 ]. ~% h" Q
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
- `% g4 I0 |3 E$ P% j% l/ b9 T8 C
% J6 c$ g0 ~& b6 U
+ L. L' z3 [ D+ aModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* }% O, H- w3 j3 v% U7 i `: ?) ?$ M+ I$ s( ?
9 z9 P$ _4 p7 W3 FHTTP Protection(HTTP防御), _. U$ E% S' u @) W. O
HTTP协议和本地定义使用的detectsviolations策略。/ o6 N: @! C% k1 K. O' t1 N9 V3 ^
: O8 ~7 m6 Q+ N1 p2 c" T
& W7 G" P) p0 g, l$ w. X9 X/ ~Real-time Blacklist Lookups(实时黑名单查询)
# S# |' D, P* c/ P利用第三方IP名单。
7 e1 N8 j% K. T* O) ]( X/ m3 F' p. @4 e3 h* Y
% Y* W# l4 u6 Y9 `HTTP Denial of Service Protections(HTTP的拒绝服务保护)
) J8 ^ m! U' ?7 i防御HTTP的洪水攻击和HTTP Dos攻击。
* O+ o; ~1 K2 D, i/ O; F4 q, T+ k
+ t; z# Q- a& _0 `8 I
Common Web Attacks Protection(常见的Web攻击防护)* m! e! A0 r1 f. l3 u0 T$ {0 o
检测常见的Web应用程序的安全攻击。
8 a" ~' a' F6 L, X2 c* H4 W1 {4 g7 s0 x, q4 z
+ {% ]7 h) o/ d5 f! c* Q- i6 N2 t
Automation Detection(自动化检测)5 l* b( S2 t& |+ k! P
检测机器人,爬虫,扫描仪和其他表面恶意活动。3 k& J$ G) L; P; l: |. o* C! P
% L$ h) m" L% s% f% w4 i
6 |2 [' J% g: i5 d$ SIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
( V4 O$ G" p( c9 n* |检测通过Web应用程序上传的恶意文件。- C& r P1 z0 N( s8 ]
8 e' P4 n. h$ p( h+ i, s0 J4 E: M" Q! J* s3 c- B
Tracking Sensitive Data(跟踪敏感数据)8 G& i( ~) b* V ~$ W( S
信用卡通道的使用,并阻止泄漏。- R6 T: s" B) P" t- Y
: q0 Y1 g: ]/ z* q
5 t- T8 r7 E/ zTrojan Protection(木马防护)) l/ Y; ^5 V N6 n6 V
检测访问木马。
: f8 [ ~) P* Q7 ]2 S4 f) p
6 h+ D" r4 x) R1 ~3 }2 o% B2 x& N! R% E% d
Identification of Application Defects(应用程序缺陷的鉴定)9 Q- [% [4 d( P
检测应用程序的错误配置警报。
- U" b% X2 y4 R3 Q# L
, y; _' L- e) R9 p* S1 K1 O, M# s. a; T5 H; x+ R4 X* ]
Error Detection and Hiding(错误检测和隐藏)' \! [& z* d1 l( y8 b
检测伪装服务器发送错误消息。- G0 @5 K, {1 ]
) h( Y" f, A/ @; I
6 g2 X( Z1 k$ w5 P2 k9 m安装ModSecurity o) ^6 n3 Q6 L- c' H+ H& {
软件基础环境准备! Q0 j& m x' P) k) ] `
下载对应软件包2 F/ G+ Z' z3 z' C- b: \ \
$ cd /root
& [) H; D/ ^0 k+ B$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; S+ A5 x% m# ]$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz) n6 \- P u, t# C
安装Nginx和ModSecurity依赖包
$ Y$ P7 d! O. y" ?Centos/RHEL
( f( h3 Y- k6 t; i0 T; ^% q2 s
) ^+ O U0 v: l# `( ?# d- F
/ l( `# R) Y9 T. k( z$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
S0 l! F! u9 ?$ OUbuntu/Debian
) J {9 F8 M+ O# G1 H! N0 z
' Y3 w( R6 S; M! N c, H
$ u2 ]: \1 @2 n- M6 t# G" h* u& [$ 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
' Y4 ^1 m# m/ a! U6 L5 {编译安装ModSecurity
9 J+ [* z1 W* q% [$ C2 y1 @! F; xNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. O' t3 Y3 c$ c, i1 g0 [
2 O- [( r* U% }: v0 c; i7 e8 ^7 k! j" r S8 z7 V5 W
方法一:编译为Nginx静态模块
+ }1 ^/ r% I2 U8 `- l; s" e# k- m3 S0 v! S
" R: S4 N# _8 T5 @1 X编译为独立模块(modsecurity-2.9.1)/ F% H w" l! `
$ tar xzvf modsecurity-2.9.1.tar.gz
- p6 U, O. W/ A1 c, S Y0 c6 f$ cd modsecurity-2.9.1/
% g" B1 p8 Y& l# z% A! y$ ./autogen.sh
6 h9 d- n9 K0 ^2 `& R" P$ ./configure --enable-standalone-module --disable-mlogc
# M+ Z% T1 M* ]7 g2 @1 N$ make+ O9 J7 `0 v# z. @
编译安装Nginx并添加ModSecurity模块
r' @+ E i0 S. e5 K( @. `7 k$ tar xzvf nginx-1.9.2.tar.gz8 y' J- l9 \4 M9 n: Z
$ cd nginx-1.9.2
. \. p: |' D# h- H& A$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" e$ j5 g1 q5 O% ~: F1 t$ make && make install
$ R' @1 Q F% b" X1 E9 P! S方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, }* O& [4 f& v/ v
0 K& L3 \% x' a( A5 j0 `, b+ ~! E: T: n" M* b9 ?) h
编译LibModSecurity(modsecurity-3.0)
4 e# a* g& l8 a5 T$ cd /root( d& s) K# Q+ w" d
$ git clone https://github.com/SpiderLabs/ModSecurity. i e& h/ n* H0 H& o" [
$ cd ModSecurity
3 ^1 j& z l% H! A7 u6 @$ git checkout -b v3/master origin/v3/master! f8 K8 ?0 T# E( G( \/ ?
$ sh build.sh
6 Q/ |' x& S3 p9 H4 i e9 I$ git submodule init
( X( Q2 O! ]0 y1 q0 K; m0 n$ git submodule update$ L1 q' @- ~# u# j( ^
$ ./configure7 p, T, r* `) [! S7 C) O9 E1 T
$ make
6 d4 O/ ]6 @) z' a9 b! L" T" Q$ make install
3 x! K5 |% g2 q/ A7 K) P3 BLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& E- L4 t( M% J8 z, X
% I. n* l- I7 r. @% O1 ?
( {& O* @! T& c" j$ ls /usr/local/modsecurity/lib% w9 Z+ b: \1 W' ?* H
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0+ _) L, _ ^) P% }
编译安装Nginx并添加ModSecurity-Nginx Connector模块
- q, e! J" J/ C使用ModSecurity-Nginx模块来连接LibModSecurity4 L+ t3 s+ g" }0 p* |$ J' @3 k
k9 [/ a3 P$ z' k. k- ~& @
' l; J! i% w9 @$ N8 F0 l
$ cd /root; L* Y( m# c2 p. Z; [% N3 x
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% w6 |* K2 k8 ] W
$ tar xzvf nginx-1.9.2.tar.gz7 n6 s& ~' N: |2 `: Z
$ cd nginx-1.9.2. [3 W! o+ W- ~3 X
$ ./configure --add-module=/root/modsecurity-nginx
0 I3 ~) T- \, I, T& ?2 b! E, [$ make
# n d; u* J% x4 H' X$ make && make install( J9 f% F0 |& n+ f3 [, v
添加OWASP规则
% g; g, m- M. a2 i- K, q. oModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 [7 p6 f4 q( Y9 w a
4 o( [) `/ ?9 g( y* q# Q# S0 e# n& [' }" q$ O
下载OWASP规则并生成配置文件" I% k. t' U! w Y* B5 W) d
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; ]) c) A& m" O+ d0 y A$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
" q( n3 [9 q( M1 O+ C! F2 F7 z- Z3 M$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ P- c) L8 X, A) b. y) X
$ cp crs-setup.conf.example crs-setup.conf
! n" c* M; L/ k0 F$ @配置OWASP规则
5 E+ r$ \- A4 R3 J6 H. ~. A编辑crs-setup.conf文件
8 L% x x' F4 S+ a* M. m4 ^
# ~* x, L7 z( o$ W: N i! D, V
5 [% ]- h4 [6 e$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! n" |7 P$ G/ l' ~
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf# {) ~7 y* C3 f+ b$ b
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf1 v9 O9 P3 T) u/ C! r2 z8 w
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf9 ?: ]0 T: }: Z! _6 ]: P0 r9 G
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。 u9 v* C& s* ~+ |( w( P
! ]& j8 o& f" [! q7 j* C
% O9 @7 N! c0 T7 w. M启用ModSecurity模块和CRS规则
" V4 M1 K* [. P8 U$ w$ m- h1 j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ b; z# U% y9 K0 J: b, y
' O, @6 p/ L% h5 Q$ g& A! b6 e$ k" U& z5 T+ ]. ?
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! ~% M2 O( f4 [1 L5 w( G6 Z
0 ]$ K8 `, ^5 i- }
4 y3 h" d) f% O4 m+ g, ]" ^% s
$ cd /root/modsecurity-2.9.1/# x& ~4 K3 R5 @* S) {8 n8 t% c0 B
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf + U# `+ s1 S P# w8 F7 ?
$ cp unicode.mapping /usr/local/nginx/conf/
/ D0 A9 _" G; M4 B" ]* W# E将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 h& ?0 H- U4 x0 b! a3 U
7 }# a+ v' K% x. Q4 \
# r6 \) m* @' C8 `( {5 _$ vim /usr/local/nginx/conf/modsecurity.conf9 A- W8 `* u4 Q( t7 M4 e, d
SecRuleEngine On4 M! }1 n7 ]9 x. z
ModSecurity中几个常用配置说明:
2 H; S/ ?7 K* p2 Y0 `/ v, s5 ^& N' H( ]8 l
( q0 b+ j1 m: f, b
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
* T/ N% q! U" j! F8 E
9 D, N; e5 I2 P4 w! m6 E
7 k: k& p V n8 }$ c: {& I/ s2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 a: v3 r& B. S
# n3 V+ d, M7 D, E
6 H: A K6 ]- z' B8 \7 G, o, F3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 }, I: l4 x: V) W: n, a
{. X* T3 O/ h& a" j6 B" c4 x5 j& k
* V' G" Y4 t' X) G/ {( R3 r4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* w+ V+ c! t9 Q0 D
/ {- R: V6 @9 S: o
2 s7 O& o; ^! [; U8 w在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。. y- k8 H# Z9 f
' O) }& E0 t- ]% t, |, B
6 Y$ Z% P6 J! ?/ a$ }3.x版本CRS
6 w a# A q: F$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 c+ ~4 S$ D' j7 s# 生成例外排除请求的配置文件- _1 I) @5 q# g/ U
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf* E6 j8 `) c% d3 d9 T, R
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* y) ]3 N1 y3 G- m( v; A0 d
$ cp rules/*.data /usr/local/nginx/conf; G4 f, y! @3 ~: J) A
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。- ?! V# C$ j/ S5 T* y$ N% b; W0 {
' A( R8 f4 g+ i+ E4 W, @$ u* D9 {7 T" U& K6 }
$ vim /usr/local/nginx/conf/modsec_includes.conf. q0 c5 i+ f- e( i4 V& h. d
8 ?/ \/ O3 T( x2 E
[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 9 x, X- {' ?! I4 D, w2 ~* m0 }, [
0 R- ?9 S9 K+ B+ T
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
& d3 s2 q8 h3 x2 t
$ U- o4 y: t3 v. ~2 c$ H5 l, @+ K" `: C5 V, A+ @4 |
配置Nginx支持Modsecurity/ x0 Y& c" J2 r* P$ _( N: B
启用Modsecurity* p ?/ {4 ?( [6 k
使用静态模块加载的配置方法
, F& U2 c6 Q U, ^. ?" O; y( A在需要启用Modsecurity的主机的location下面加入下面两行即可:% k, F7 Q( n! f
& B. L5 C" ]7 G7 W* ~8 H
% {) J8 B* t- c, R. R6 X
ModSecurityEnabled on;
. f- t0 H9 s5 H6 k! _& w2 A1 I: }2 hModSecurityConfig modsec_includes.conf;. k2 Z# j. A7 S1 T) F
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ _$ K' `1 I1 [6 n x# n7 d
" y+ D. M& }' r/ P- P7 Q5 l
% M1 o: Y" I: a9 v+ O T/ u$ vim /usr/local/nginx/conf/nginx.conf8 n! F$ q2 O' L H, \' x2 `5 J, @
% d y& c: W& O$ }! S5 a# ]! {" g# ?
server {) ~3 J$ @) H0 c6 |# [ o
listen 80;- P- G! N, A9 `
server_name example.com;4 J- k S l* m! G# V5 [
* v" V/ i7 M7 @' V, \3 w, V) ^' X; j' h8 J1 ^& g( T9 {# o) ~
location / {( _$ p7 t7 [7 T* O. u" q
ModSecurityEnabled on;" Z- m( ~% W. T) D2 }
ModSecurityConfig modsec_includes.conf;
) E. @4 |/ s: J9 a& K root html;0 J) h. \+ d B7 W' y. v
index index.html index.htm;
7 \8 \- M* X d7 T }5 {) T% e/ }0 ^6 O8 J
}" g) i3 j7 |. B. q% @4 ]$ L
使用动态模块加载的配置方法
$ t9 y' v" q% z: g在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 f( l E& Y9 B! S* o, X4 Z2 y
. D8 N G2 _, W
0 V# B3 @# g. ?# |) M- x" smodsecurity on;" y4 H T+ _$ `2 d4 S
modsecurity_rules_file modsec_includes.conf;3 @/ E6 S& t u$ y2 A! q. c( }
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' K! I3 I6 O* g7 U0 O
. ]- }. Z* v7 g; @8 c7 v: k; q2 p5 @5 D8 B' b, Q
$ vim /usr/local/nginx/conf/nginx.conf
4 L: f2 \. u; P- K" A" ^1 ~5 d0 ~( G9 o
4 J- R7 b! |& U% Yserver {7 _* y/ d" m8 B% k% a
listen 80;4 F. g8 y6 K% _* w1 F
server_name localhost mike.hi-linux.com;. N. f" G" X1 }' }( H
access_log /var/log/nginx/yourdomain.log;/ H2 m8 f$ d1 ]4 h3 t8 {% X
, `& X4 m1 p6 p* ?8 q4 N0 U, [; C0 m$ W) y" u
location / {9 O/ G- B. K! ~" \+ }5 P
- z3 `$ N) C& a7 W
2 k5 E4 ]& q5 W2 { modsecurity on;# e; L% X3 q7 e& P
modsecurity_rules_file modsec_includes.conf;
4 B. _$ k1 g. X e root html;
y, c- D2 M K! k* J7 T4 }" ^2 r index index.html index.htm;2 M# \7 o; X4 R/ r1 p/ P9 E& k
}( E% n2 {. U) h7 H" l- r5 v
}
. e0 y7 C- ^, i3 [' W* P验证Nginx配置文件. }5 ~/ ]( p4 b5 U0 r5 V8 k
$ /usr/local/nginx/sbin/nginx -t# R# E" _( j+ _$ l) E& M' m# |, p
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ x; f; V2 M0 f2 Z2 v. [, Unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful5 m* ~0 s( C* C
启动Nginx
) z, O; E c+ O7 ?) Y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 z+ ^! W' D9 w$ P4 m2 |( q: Q! q
( K& v! D( P& \ m2 R2 Q. q测试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能够战胜更多复杂/未知/混淆的攻击模式。 7 L! Z5 U- }+ l+ E
|