ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。9 K9 Q7 t1 y' g- E( P3 h& G$ H
/ \0 G, k! x7 g G
7 n: n" h: T: B8 U7 J/ @
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。4 G3 E# @9 \; Q- q2 `3 R/ Z
+ Q' J3 X+ _4 X$ I4 r% U' J
/ G& C6 Z- u" J% {8 [
什么是ModSecurity
: F6 r1 k! a$ P* k+ J- u4 N: j. sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。9 J% {# T# D8 v; P4 }& `! Y9 ?
" Q6 r+ v6 s( @
- j4 @/ c Z$ OModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; l7 x! d! a7 T+ b1 J+ E! a5 y
9 Q8 Y7 I# E5 v' l
. v2 a7 E8 v2 ?( N/ _, AModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
2 W8 p: A2 A1 }2 z7 m* V" {9 H2 B7 p- a5 {( u! n
' C, c+ n* x4 B C
官网: https://www.modsecurity.org/
8 `0 o7 |5 M; J( H) G8 F
& K& I5 M7 L p' O& i4 \' ~& x5 N0 c' {: K2 j4 J. c. X
什么是OWASP CRS
* W4 i' [3 q) K) U! l* tOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
$ n' X+ p* {* D6 V- U$ ^; i
! ]- h6 P8 K$ p q1 m, w& }$ e5 y6 \. b
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。+ E& {: X( b- `* c0 j* t# @
/ N' ^, L: n" {* p3 V
1 g V3 m# ]6 Z* T1 b! cHTTP Protection(HTTP防御)# ^1 [- Z t' l/ {/ m
HTTP协议和本地定义使用的detectsviolations策略。
( ?% |3 u. }' e" w: |$ T; ]: I; D/ d( j+ |7 k
! W3 C3 m7 t3 d y/ ]# wReal-time Blacklist Lookups(实时黑名单查询)
6 s1 Y$ t9 ~; y* h# _& a利用第三方IP名单。
* t' l% J' u- A$ i% x/ N: i" h4 S- W" z: N' C) w- U) f8 I
w, }; G! s0 i% a- uHTTP Denial of Service Protections(HTTP的拒绝服务保护)
% n5 s; T! f/ _. E" x防御HTTP的洪水攻击和HTTP Dos攻击。$ k d% \2 C0 d9 Q _
9 b/ o1 ^4 n& m4 m3 E/ z) F; d
: M A9 E9 l3 }; x' V' n' [Common Web Attacks Protection(常见的Web攻击防护)
# F8 B- Y h7 Q' B0 T; s0 x检测常见的Web应用程序的安全攻击。# f2 U# `- v+ b# n) q/ D+ I
! ^9 e8 S5 s6 V+ w; R' d. t" p3 y; M1 J6 S, ?2 [
Automation Detection(自动化检测)9 w1 ?, a- e* [3 E. _( z. N
检测机器人,爬虫,扫描仪和其他表面恶意活动。
8 H- N/ g) S0 e1 {5 u) {, W: M+ t6 o$ \
+ {+ X6 g1 m9 @7 e' e5 k% X- WIntegration with AV Scanning for File Uploads(文件上传防病毒扫描). H4 X' ]" J# s+ {4 i+ `
检测通过Web应用程序上传的恶意文件。
0 f, k& f0 L- [! }9 k
+ q; m' _8 A+ F4 r7 C }1 m! Q# d. Z3 U% [$ z$ s' g0 l. G- O
Tracking Sensitive Data(跟踪敏感数据)5 g2 ]' Z; [; j& M5 N+ k4 [
信用卡通道的使用,并阻止泄漏。# W, `" h0 c( \0 z P
) W( J' q+ }" @% i
7 F8 t' R8 ^/ ~. E' V9 r vTrojan Protection(木马防护)
+ I( _2 ^4 f. ~检测访问木马。
& x! |' _/ F9 B0 z* d6 C/ d( m0 V6 O+ i* K, ^4 ^0 D4 o, y% w7 n
" g( |% T/ p4 s8 O
Identification of Application Defects(应用程序缺陷的鉴定)
4 k! A4 E+ Q# ^" q \+ n$ p4 }检测应用程序的错误配置警报。
) B) Z- Z' G' k! E4 W2 l/ O
* h0 g4 X; b( [# A
5 A% Q7 X6 @! d. i$ uError Detection and Hiding(错误检测和隐藏)9 Y" ]6 w& o( W: G# K3 f# J
检测伪装服务器发送错误消息。% z2 b6 K/ ]+ C, ?; }" j
( j- J6 g, k" u( t( x
# e+ K- Q% {) B$ [/ I( m安装ModSecurity
" M' i& s) ^" w; h& y _软件基础环境准备8 D/ g/ Q1 k# g' C* P( b: G
下载对应软件包7 r! \( B- a$ N, i+ m, @2 Q$ I3 q
$ cd /root
" S7 m& z, ]% d5 M! e& V0 y$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'3 @3 j/ ^/ \% r, 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
' |" d+ E3 x) D7 f5 `, H安装Nginx和ModSecurity依赖包9 M1 ~% d8 P. t
Centos/RHEL Z& } N2 V% {1 Z1 q) p
- M8 A1 R; ]) A- X) {0 i1 e
" @, n( ?- i1 {8 l
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 o( |0 c4 Z( _! ?# v# }
Ubuntu/Debian
5 |, Z6 |* P$ c, }: |: V+ K z
$ k; b* j8 ^) L7 I# q. f+ H- F d
$ 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
% e4 H5 Z( g. ~; K' \6 I0 t' N编译安装ModSecurity
. C7 f3 f% v9 }3 g. ? GNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
; p" E" G5 ?1 Y
/ F5 Z8 H- d1 ^* v& F: h4 x" m1 D4 G+ }
方法一:编译为Nginx静态模块* s7 h6 l* C* t! O3 B
0 j5 V7 Y* _: q* n8 [, @
3 L- Q# |0 ] W( Y
编译为独立模块(modsecurity-2.9.1)
, ]3 p6 M+ _" g9 t( P1 X$ tar xzvf modsecurity-2.9.1.tar.gz
7 F9 t$ J6 Y) D5 \$ cd modsecurity-2.9.1/
P p6 b4 t5 r: ~0 X. M u$ ./autogen.sh$ x* e' A4 u- b8 q
$ ./configure --enable-standalone-module --disable-mlogc
6 B# ?* e1 T, J% G+ F8 Y- p; o9 D$ make
" r/ ^5 e( f, c6 v% {" _% F. z编译安装Nginx并添加ModSecurity模块# I" J8 [' {# ]) w* S( s j
$ tar xzvf nginx-1.9.2.tar.gz9 u, d% l- i+ g' ^
$ cd nginx-1.9.2
9 k) |$ }4 Y f6 ^6 [2 r/ J# I$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
, E" C9 O- }9 C# e$ make && make install
; }7 O+ B8 Y. c. _! }+ J( Y方法二:编译通过ModSecurity-Nginx Connector加载的动态模块" `' z( a- m/ C3 L
- t$ l: ?, w* M
+ D& h1 _8 @9 H) ?2 y7 A编译LibModSecurity(modsecurity-3.0)3 m' B/ a7 h- A$ r( M, G1 r
$ cd /root
& B c# |0 N w U I# J$ git clone https://github.com/SpiderLabs/ModSecurity
1 g' V6 D: q2 {: H- m+ T( H M, E, B$ cd ModSecurity
! A8 W( v$ U. u8 R! ^9 U$ git checkout -b v3/master origin/v3/master) x6 H0 a& r ~0 n/ L& \7 M
$ sh build.sh2 G& r% ]" a- p& e$ B; |
$ git submodule init) r- j% ~: _1 G# Z! C& ~
$ git submodule update' i( S& f1 z9 m' s7 h7 V
$ ./configure5 w* |. m$ ~6 x/ L$ e O- _
$ make. ~& i2 c+ n: d4 P) R+ ]$ Q
$ make install
, X x8 w* E3 Q! U/ A+ f1 W9 r, j6 ^0 fLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 a/ a5 }$ V% T) ~* n! P4 m# V/ a9 e6 M* y
# }# p8 { M, D# q) _/ \* s$ y$ ls /usr/local/modsecurity/lib' f+ S3 F3 t* |9 H
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0" Z; B# G8 X/ K
编译安装Nginx并添加ModSecurity-Nginx Connector模块
; {1 i/ t7 w2 F4 b a6 F% P7 W使用ModSecurity-Nginx模块来连接LibModSecurity, b( c/ y$ @$ D
( Q* A: J' J- e
8 ?2 U2 E* b" [& p
$ cd /root; p1 B$ G$ m# A- n/ q- Q! G
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
@& a( X: Z d; K! u7 S$ tar xzvf nginx-1.9.2.tar.gz# ?* Q& X0 T. u u) x; H
$ cd nginx-1.9.2/ U, J8 A" ~' H6 _
$ ./configure --add-module=/root/modsecurity-nginx
. t! t% V8 e9 t$ ~3 g) m$ make! B# u! i3 c6 F7 ]: F
$ make && make install
+ z' i. O/ w5 K; _1 i添加OWASP规则
# T% N0 X. B* \! ?1 h6 w: z& X0 ZModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 ?( j0 n! F, D2 @/ e) Z: n3 [( B$ g$ m9 s) R
2 g4 M- C4 r4 q, H; L
下载OWASP规则并生成配置文件+ s( C7 Z$ _% l9 n1 F0 e6 g: \: U) H
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
% u5 e* n% X! t# z' b$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
! U! f# R* O" T+ a- N S$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 ~' ^8 Z, v, k9 Z7 Z$ cp crs-setup.conf.example crs-setup.conf; d2 g2 W" e" N; A
配置OWASP规则
) b. A$ E$ E2 g X! @: I编辑crs-setup.conf文件
1 n; ?8 D H$ g# m" }3 `8 V0 X# a! T) r* G; [8 R
) B3 N# h6 `( s5 d" b- F" ^9 i' f) ^
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
) k% ~1 l) g, N4 j2 W( ^* d$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
* J* H/ r) ~0 j% I0 Y$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ j! N- D5 c1 N( j$ o
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ b5 W7 W1 r7 I) N( f默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。5 C! A/ c2 T8 y9 f, Q7 s. z. F1 Z! o
! T& R$ Z2 W' }6 ~ ^+ ?' ~" O
" M; E7 s1 u* H3 \4 b* s
启用ModSecurity模块和CRS规则
3 c8 d2 a7 g$ o9 a! t) {" ?7 q复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 Y- t$ J: Q7 n2 L8 T/ }# j/ q3 d
+ x2 o; x9 x) }+ x4 c V3 ~
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! I! q$ \5 x/ M7 F! P7 r: b
% j) A. O% P W; \/ B4 g4 A" F% z
4 C, {0 h% O1 n* A$ cd /root/modsecurity-2.9.1/
2 l6 N$ J, G* |" w4 n+ j% `7 M$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
0 V4 H L3 f( X$ cp unicode.mapping /usr/local/nginx/conf/
* Q* n; b# w& n将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
. G% K$ P* b8 z+ J1 D8 ]8 S
p2 m0 `3 J% X5 v2 q/ F. N9 }7 f
( F9 ~; j. E' c M, m$ vim /usr/local/nginx/conf/modsecurity.conf
+ V* c8 L9 `3 P; E# ^SecRuleEngine On5 X6 X1 C* O- L& u! D( w: `
ModSecurity中几个常用配置说明:
6 x: G$ `" F: S( \, \
: | P6 R/ {' q2 u( L6 v. k( ^0 G6 `( Z6 W1 @3 U8 u8 T
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
\* g! i8 A! p0 X0 ?4 ]6 X1 c# a6 O3 y
/ f9 |7 h$ E/ e
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 @3 X; ?, r2 ^7 y% J3 ^. h& {- I9 Y3 i- b3 b5 ^, t
. R4 P- \( J# B% W6 N" d ]* Q
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 N. B( J- ^% |5 C- K% U
3 a: P7 M$ P& C$ V) O+ o
0 [, Y1 ]7 T3 p1 [+ r! a4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。# `/ i5 V( h: o; S. O
$ g4 \1 a5 \4 B; R4 t
, e: ~! z5 B; p ^在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 q) R# ?) U; g2 {! A
" N, q) }/ p" ~! A( R! O! I3 ~1 U: b9 a G
3.x版本CRS3 R/ e r: t7 C ?& z# e* }: p+ V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 g# D5 c4 [: I
# 生成例外排除请求的配置文件
# w! \; u* ?5 E( ]$ ]1 m$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
2 F8 `3 X+ L% P; T$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 ~- C' {. c& ~
$ cp rules/*.data /usr/local/nginx/conf, v/ J$ J: Y0 g0 `, y
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 s; {# P- Q2 `( ?3 k' N: X9 x
$ m# Y0 b. Z. q" n3 q7 h! d4 R& _8 a8 D/ y& W# {
$ vim /usr/local/nginx/conf/modsec_includes.conf3 d& B2 n/ C) {; s6 S ?3 i
: }( O! Q6 q: E6 A1 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 4 u! i% V5 w' T
" b: `0 w" O0 t. c/ G
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 n$ d; k3 n7 G+ N6 ]) S! h
3 I( O* h0 n6 g, G6 L6 ?
- x) V) R/ T p配置Nginx支持Modsecurity8 U" O9 r8 ]0 m- w$ O, o J( @' r
启用Modsecurity
+ q) H( O3 W3 l7 w. D使用静态模块加载的配置方法
0 @5 N, n8 C/ ]: h2 d在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 d+ V+ [0 t6 q9 e5 D: | \
% @6 E& |: Q; @
& @& t! E. G4 M2 m7 a) X0 z( R, oModSecurityEnabled on;% t2 n: l8 E4 x: ?# t2 |& M* F
ModSecurityConfig modsec_includes.conf;
; l9 Q/ W. G) H# T1 v5 X0 I) A修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ r9 c8 X3 A3 x; O- j4 {) e1 W
0 P* C9 X: O+ s7 e1 [, G
7 c# r# ^2 \+ S, S6 r# @$ vim /usr/local/nginx/conf/nginx.conf
+ T, j3 R3 y2 a7 N! ^: n# C# V; M# R( }/ P$ p
+ v3 Z3 ]1 h4 `; Y* O+ l+ ~
server {
- U! _! r2 z. [/ d$ j4 b& D listen 80;
+ z7 K4 Z- {" ] server_name example.com;
! X8 E. v7 O8 q9 {' r2 N4 k" S8 h- Y& _
0 N: T- D* J1 H" ~/ K5 s* `
location / {
$ k7 e$ P" V. | ModSecurityEnabled on;" _7 U Q" K/ u( U$ L1 _6 r
ModSecurityConfig modsec_includes.conf;, z. N: f8 K. w3 u2 R
root html;, T1 X- _5 C/ e g% z+ [; y1 T) \
index index.html index.htm;' H5 I6 @$ D; d( F) R
}
( o. o' y; {. w}8 b$ y _) k/ g9 y5 X; u
使用动态模块加载的配置方法
7 I& W- D+ F9 d+ E. J' r, E在需要启用Modsecurity的主机的location下面加入下面两行即可:
, {& S) M( P3 M( ^/ A/ ^# H) k' e3 c- f( }( Y
! _( D, a; k. `; S
modsecurity on;
1 W5 ?7 m( q0 w( X# U2 K4 hmodsecurity_rules_file modsec_includes.conf;
8 B0 g T7 C: o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ S: Z! @8 p! T. j% F9 T! l$ X& W
2 `2 V, ~* u* Y1 \! X7 z/ U, s2 L4 b: a/ ~3 y1 d
$ vim /usr/local/nginx/conf/nginx.conf
* @$ Z+ b$ u8 r( {) e* o2 Y3 B6 V% E
8 A; J) o% ~# S- I5 |server {( g& f) Q Z: {: c& r+ {
listen 80;
0 F# @6 C3 L+ z& x server_name localhost mike.hi-linux.com;
- k: q+ R g* Z5 I6 P access_log /var/log/nginx/yourdomain.log;
3 R9 C' C0 {! B( h
1 t0 M7 x" f0 W: j8 S0 D9 y* l' _1 X$ x U
location / {! q) J( y- a2 b4 n0 r y
0 X* Y6 g6 Z; X- M3 t/ M+ P
- K, U' Z( j2 @9 q- g( a modsecurity on;) [) Z1 q+ f/ g1 D
modsecurity_rules_file modsec_includes.conf; [4 Q5 I$ m8 t8 {9 Z, `
root html;
* z0 y- k/ Q$ X. g, u7 e' R) } index index.html index.htm;
! d8 Q. _4 b" s% O}; C; d0 k1 S6 ]& ^
}
]- j* A1 M$ \4 O9 B. y验证Nginx配置文件
$ W( h1 R) r" W9 w$ /usr/local/nginx/sbin/nginx -t& h% [, ?0 M7 e j: p4 ?" _
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
8 m5 v: i: s" B9 |2 P8 ?) anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& J) |/ |) X# `- S4 V8 B- L* B3 b
启动Nginx9 H1 [5 f& D% L
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 N$ T9 w$ q" |" w8 `
3 S; u# h4 `4 P/ B测试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能够战胜更多复杂/未知/混淆的攻击模式。
# f8 n; n# E9 q# v |