ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 r Y9 C: Q- I5 s% Z* E0 H
6 y+ Q2 l, n7 J! H. d& O
# |$ e, W+ W) l( L" i" \" g- I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。7 F) V8 |- L- D; ~0 c1 }
[8 ?) ?* Y6 [2 k( S, W6 u6 C0 ~
/ ]6 s& S+ l4 N: Q什么是ModSecurity
3 Q0 D* D! |2 xModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
* m6 L0 t! G5 E2 g& |. }) ^! X3 H- J( y$ K% w; ^& @4 v$ R
! Y( Y- p; W4 B0 D
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% ] V+ h( h% O! R' v8 q7 x
7 h, ~. g9 s4 M4 [( I$ o: U [$ ` v$ Z5 D5 k% h1 j3 t, L
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
9 |7 E2 o5 E' g6 P$ D4 B t* O0 f% g) I( |5 e5 n G7 k: F
$ P3 ] t( K; v, t7 O7 e1 @
官网: https://www.modsecurity.org/
# X7 e; b( z/ f( T0 b$ V* t( N! q0 J ^6 `7 @
, `) Y3 E- f7 S- K0 a7 ^
什么是OWASP CRS9 s+ E) b1 o' q6 N* \7 {
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。$ E7 m C3 l* t! t3 j; }9 V
7 z- ^% c# S) c, G N$ w, S7 T' F( M6 \2 ~; M4 j! w# b
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! O/ P/ D: H6 g6 u- j4 G
- t# D( h6 M3 P0 [
2 j6 U+ X9 p5 H. wHTTP Protection(HTTP防御)
0 Y; d) T1 O+ WHTTP协议和本地定义使用的detectsviolations策略。
0 `$ H1 v9 q% B0 Z4 A) `1 N) }4 L: `1 ^: Z4 @# \6 ^& y+ ?7 @3 u* ^
- ~: ]8 t2 y0 O5 I9 }
Real-time Blacklist Lookups(实时黑名单查询)
2 W1 o+ }+ I6 L# i$ C4 ]利用第三方IP名单。
2 l& k' O( Q; H) F4 V- @" _3 P) c `8 R1 d3 j% V
4 y5 u9 b) H2 S9 CHTTP Denial of Service Protections(HTTP的拒绝服务保护)
& ]5 ]6 h, D b. d d防御HTTP的洪水攻击和HTTP Dos攻击。
& c% \# m2 W) B: A$ `( H' W; g" @9 S* h* @
, Z: @" `* q& ]1 p) R% S: v
Common Web Attacks Protection(常见的Web攻击防护)
/ {+ }6 L5 C9 |' J4 Q检测常见的Web应用程序的安全攻击。
9 V- g% l I5 k7 A' M( N2 g% G1 O+ C8 X) O
6 H* q2 ~! N9 H4 O% r1 ~' x
Automation Detection(自动化检测)
3 i6 C' {2 ?( d* @检测机器人,爬虫,扫描仪和其他表面恶意活动。
7 z# R3 E$ J, v S. ~ X6 M) l$ k- L
7 c) @7 F9 @) A4 c7 L2 Y
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 c Q6 p8 s' m% j! s检测通过Web应用程序上传的恶意文件。
( q* e) \) p+ y- k9 N* z, E/ @' A. c5 w* O, z ~$ R
! c) z! z/ E# B
Tracking Sensitive Data(跟踪敏感数据)% E: C% _ Z: b; ^8 P
信用卡通道的使用,并阻止泄漏。# L* a, I- u/ `1 n8 x# `7 u
9 {! S& K- Q+ |" B& F4 D; ?
: R9 V; X$ T$ Z1 C) Z3 r4 J% ^" c
Trojan Protection(木马防护)% |3 u# Y8 b6 m6 z1 S4 \: W* o
检测访问木马。0 i1 z T& c" e0 b. m z# o
" g& |; j# ?$ Q4 L4 O8 [
* n0 W% e( R4 M: u/ C
Identification of Application Defects(应用程序缺陷的鉴定)
) H6 |$ h5 ~* j! ]检测应用程序的错误配置警报。
3 v+ w/ V$ x" a' W$ b( E8 s( w3 h4 a' b
% l R* K* s# U, w; {
Error Detection and Hiding(错误检测和隐藏)
8 a% \. K# x! _) l% E检测伪装服务器发送错误消息。
6 s5 w9 }2 I. @. M
& i2 H1 i3 P, o# T' A( R' R4 V
& i) M( ^) Y2 _6 _4 O5 V- y, i7 i6 q B+ f H安装ModSecurity5 `4 P' R4 Z4 b# h: L9 C. u
软件基础环境准备; B6 R- |0 w* F, T) S W6 a1 @
下载对应软件包3 l1 n4 @9 Z5 X; _' l ` Q& r: w
$ cd /root
7 @# x3 H" f6 A$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 t6 g: d9 {8 f3 {: V1 \$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz. A% Q$ }( A8 [; y2 d
安装Nginx和ModSecurity依赖包, S- @1 k# ~" C, q
Centos/RHEL! f( U7 x- D0 T- j5 B$ F% j
e7 H/ e" _, Z9 Y( b1 k
" p1 e. l. R: y+ Q. O3 g$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel T4 X3 O; l( \0 l i3 V
Ubuntu/Debian& I7 i6 A0 P i
* ~0 P ?, _( t# v2 N. O M
& o+ d+ |5 H, S+ X6 o$ 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
; }' h; J$ |; n编译安装ModSecurity2 K! k+ W+ O- {3 a7 ]% [* j3 O+ u
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) i) G) ]% k, G1 T9 C+ @
! j8 L# X- G8 F! F1 W3 l
( L9 v; x1 i- I& k方法一:编译为Nginx静态模块
) s9 y5 |5 {! f* S( o; n5 t3 O- s
# y% c o. V- p+ o
( H/ x7 o0 F2 D0 z, }0 _编译为独立模块(modsecurity-2.9.1)8 n3 a! C- S# c% I
$ tar xzvf modsecurity-2.9.1.tar.gz1 O4 t' ?6 H0 [2 i0 F+ j& e
$ cd modsecurity-2.9.1/
( O m& s; ?. F0 e2 V' o% G$ ./autogen.sh1 B. u ~$ d: {4 I& k! {* _# K
$ ./configure --enable-standalone-module --disable-mlogc0 a- J4 p: J, _4 \/ E7 _+ E; P3 T
$ make
$ n3 y+ X3 U5 d7 B, c3 J, G编译安装Nginx并添加ModSecurity模块
, h4 @. C% n- k/ W5 e$ tar xzvf nginx-1.9.2.tar.gz
, V- s/ c4 x) _4 |. i# P$ cd nginx-1.9.2& {6 \2 }1 S8 |. ^/ z0 t& F6 _3 Y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; Z+ w1 Y; ^: e1 [( @$ make && make install
/ j' V- |; Q: _# p6 ?方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, _3 ^3 c2 M( l/ H9 |! t
2 s7 ^3 d2 M9 z! l0 M" |$ g5 d7 T* p$ a6 b Y3 B
编译LibModSecurity(modsecurity-3.0)$ _0 k/ `/ T/ z8 L
$ cd /root
" o4 z, C7 H# S$ git clone https://github.com/SpiderLabs/ModSecurity( D6 g% i" L4 l+ f
$ cd ModSecurity
' Q9 S& \2 d3 s0 y$ git checkout -b v3/master origin/v3/master
4 J0 e6 `) U: B# k# u7 R& P5 H$ sh build.sh
# |( _1 P8 x2 L9 B: P' {$ git submodule init
7 T" T d) X+ H0 D/ I5 F/ Y$ git submodule update; \: d9 o8 z' _
$ ./configure2 H0 z! J/ L0 V5 v
$ make i% ^0 l3 F4 L6 v5 P
$ make install
# ?4 A' d5 W% w0 ^) q/ X& t; ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 I) W; r5 x7 |8 B3 p$ L
6 ^( q* b7 X, u* u l* k" H1 a$ _ \$ Z( W" }" H G1 _2 G! E
$ ls /usr/local/modsecurity/lib
# m! e3 c% d- z3 b$ a4 zlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0 C0 n, E( y: J6 B4 X% N2 r, ?
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 P+ Q' v, m) i: `4 j使用ModSecurity-Nginx模块来连接LibModSecurity
/ Z1 u) G# j" U3 w4 Z& l7 l6 v/ D+ L* j- i9 j( I) \
- ^0 w7 v. @: R6 o2 v" l# C7 {$ cd /root/ l' @3 f# v& c7 p$ J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
7 Z v$ A2 `( P+ _$ p# {$ tar xzvf nginx-1.9.2.tar.gz
, W$ I5 ?) n$ v4 }! A& I/ w$ cd nginx-1.9.24 r! X; d b5 _# B- p! A
$ ./configure --add-module=/root/modsecurity-nginx
, N4 R0 `( z2 H* m# W- g5 X$ make
! G& O0 E8 H- D y. l7 j$ make && make install
( D2 C! o7 q/ n8 T. f+ ~% i添加OWASP规则
, D! E* O! A) x% H/ m% NModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
5 u8 F8 C# c7 E9 F: z0 Y% g5 u0 Q% l
& t" r* _; M' s. n7 G7 P3 w' F3 P# ~# I: n- p* k( R
下载OWASP规则并生成配置文件
. \7 r3 v. \8 V& _( F$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ M$ M. ?6 ^5 q- P# r$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
; U& _; t9 x7 c7 ^! O f. E$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 B9 V: L6 O' y' t' A4 T
$ cp crs-setup.conf.example crs-setup.conf
% Z! s3 p: S5 T* b& K% B) I- Z配置OWASP规则8 s, v/ P' G7 C9 u- }
编辑crs-setup.conf文件2 \7 X; q2 b6 S! J) B/ k6 P3 q) z$ q
3 g" M( w- b! O/ l& `# X1 N) }
9 W1 T, |, N* j" M; A, V& U. w- s
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 G8 s; }1 g- n3 e v
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf4 |7 `2 h$ N: k5 {- M
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf; N5 F( j3 q; T" z; L$ Z, C
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
/ x Z4 Y$ W4 v2 G& D, _9 m默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
! Z5 w/ z i2 C- i, v; U3 n! H; H; Z0 {. v4 T3 \1 t
! T, s! j n* X3 M4 P1 w5 j启用ModSecurity模块和CRS规则
( c- v: X( d" \7 @复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: B- J; i" J0 @% [1 n0 I5 \5 N% G& B) F7 M2 |: _3 N
- X$ M% p0 O9 }3 ]( v* A: i, emodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ L1 i; G' v9 _" [, j6 Y' G$ @5 c. i8 _ d% q/ b+ E" S: O2 L( Y! B; Y
1 T2 }) V# {( N. s
$ cd /root/modsecurity-2.9.1/
+ Z* S6 M" v& J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
0 Q2 R' Z' @! y( b0 u3 Y( @: k$ cp unicode.mapping /usr/local/nginx/conf/
+ a# [, a c0 `$ L6 G* S- w6 Z将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。, S7 J* l& |8 T& t0 Y
& p0 T) J+ y7 a, ]
) a6 c- U; E8 L' O$ vim /usr/local/nginx/conf/modsecurity.conf
% i1 y: a0 T# o6 j: a3 m5 DSecRuleEngine On. r' S+ }5 Z5 X& P+ u" l2 e
ModSecurity中几个常用配置说明:6 C0 O/ W. @( B4 |
& d* n% S! M: @( y- U6 a" i4 O0 C
w/ D* }" [* h0 C3 h& s4 `1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& ?4 D! l" D2 h+ n8 X) w5 ~8 n
9 V3 j) t5 i( U! g8 D' ~4 v; i, e
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 v: ?; H) a$ I. ?$ `- C1 ?9 m5 ]" w- \( v# r( D% Z3 X6 E
5 K |; v9 @9 D8 J3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 ^/ R5 `7 k: h! t
0 O6 ^% H8 o( \8 x
2 u7 i4 W0 ^ N' M4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。3 P) l5 R2 I; f% a0 }5 V) ]
! l S: c; ?/ E9 I% X( b( f4 Y
/ y8 |3 z6 D: K' Y( k, F在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。( Y9 F9 P! @8 V+ U [* q
, h# j# e* ` Y" g1 U
, |1 f7 }" c3 \3 X+ z
3.x版本CRS0 |4 }# }! v5 S% |2 w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 I& O; y9 A8 }! Z4 i9 W1 d# 生成例外排除请求的配置文件
8 G6 u: \: U3 |+ [5 l$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf! e( \5 P8 O/ K) _% X# G
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 s6 r" z3 X# E) Y- o* ]7 R0 T$ cp rules/*.data /usr/local/nginx/conf) G! n) w& i+ a! x9 J7 s8 y; v, R
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
$ N- c+ Y* K+ _. L+ m
# L7 @ b; Z5 Z( e+ `% D9 `" a0 j" X8 q' e: }) w
$ vim /usr/local/nginx/conf/modsec_includes.conf
m5 T7 R/ E" l; j E, U# k0 v6 t7 G4 U+ q* ^
[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 * x6 Y+ g, t6 z
% |: I M! B8 r注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 W: ]7 F8 \ ~7 a/ o/ t7 d
4 O8 h1 D, }, N. o! [- X1 u2 P5 r/ z5 X$ t
配置Nginx支持Modsecurity2 z( o: Q B& G4 C; f
启用Modsecurity* c9 z' u/ W% _& t% h
使用静态模块加载的配置方法
0 R& }8 s7 e4 M$ l' ~( `. w* t X在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 P/ A6 _& Y' p/ ~
9 F8 q! z' Z5 J9 r1 a6 j% r6 Q
- y" `% {- [& N9 W% j+ e! m) i% q8 zModSecurityEnabled on;' s; [5 [( |- [6 o
ModSecurityConfig modsec_includes.conf; _8 ^% X+ n7 p) F5 L7 Z$ Q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( O9 o4 G* e* u: d, ?9 H) `+ o$ x- @+ b7 l# `, ?; d' x
+ O! q& d$ F6 u$ Z7 Y
$ vim /usr/local/nginx/conf/nginx.conf' A, J1 X8 Y5 i/ Y U: i7 k
9 @! G1 D/ r" K' q3 P
$ C$ _& l: G. p: Fserver {' U# e* p$ R( }4 C: P a
listen 80;
. X0 X7 a$ U# b X1 _. O server_name example.com;2 @) Q9 h# m ^/ s/ @" t0 p
4 X2 F/ D" z) w: w' d4 d6 y
i, i) T9 q) ?7 O" W5 L% m
location / {4 \4 M; N2 M4 E: I* i6 m
ModSecurityEnabled on;8 ^" }0 z) ]- v; b5 K
ModSecurityConfig modsec_includes.conf;
+ x" \( C; p T8 r+ H+ D. S! R3 | root html;# I. s: I& Q4 }/ d) v4 h
index index.html index.htm;+ r6 L' m6 D" l
}
) a! }$ Q: |" d& L8 v `% S}* _* O9 c( z/ L) H
使用动态模块加载的配置方法- y! Z/ n1 m+ f
在需要启用Modsecurity的主机的location下面加入下面两行即可:
* o& J/ }9 p! c5 K4 ^. N3 ~: M
4 y, e" w2 i5 M" R
modsecurity on;$ ^+ l0 i# a& Y) ?$ Q
modsecurity_rules_file modsec_includes.conf;
' f6 n3 x" J: v( K2 ?4 L7 Z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 q. p# F# C% \0 l* x
|& H3 K9 Q( R) i5 ~
/ A) N% M3 \) g$ v0 d$ vim /usr/local/nginx/conf/nginx.conf
( D, r7 o0 v9 c, u
) k9 U; C! b0 L; K5 b9 r8 r; u9 @/ l- ^& ~; ^# y) S3 ]$ J% G7 X
server {
# D; y$ b9 v3 b1 k& z, t% c listen 80;3 [7 S/ r R' U4 t. _( [1 k
server_name localhost mike.hi-linux.com;) z G E. z9 q' A+ B
access_log /var/log/nginx/yourdomain.log;
; K- L; c$ X5 M8 @- G% R
2 P- O- w A% |
( ]" _2 y+ a/ a' V. v' } location / {. w& t7 X k6 K$ e; `
- o6 j' H5 `7 q
$ B2 U- n3 C, H: M! n/ O1 D
modsecurity on;
" Y( `/ B) E. S+ N4 J9 S2 t modsecurity_rules_file modsec_includes.conf;
- x, P; H% W3 j2 j0 u' \# P root html;
- u" X L- `1 b/ O# ^2 `) Y index index.html index.htm;7 z n. V0 @2 V' \
}
* v! J2 m, f4 f* l3 ^% C0 P7 ]) t}
' j) a; Z* D, I4 s% _1 f: C& m) L' ~验证Nginx配置文件) s, E6 c/ P, `- Y
$ /usr/local/nginx/sbin/nginx -t
2 }( p# J2 o T. W, u! j' N, X( r" Hnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
5 M( e1 n" ?$ Q! @+ o& B, |nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful3 m) z5 H* c7 z: E g3 x$ B- P
启动Nginx0 m) @9 N0 Q* A1 \8 d( @. Q2 _
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" R) u8 t3 n4 Y, `& j
* I7 Q# J5 `1 q9 b! i* K& a4 ]
测试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能够战胜更多复杂/未知/混淆的攻击模式。 % A6 p8 s4 e" S$ i
|