ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
^4 J; f$ G; O' |9 q( f9 \* c% w
3 K2 @( Z! w, I' v! t5 m( {
/ d3 o) l v6 ~0 E' J9 T1 a在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。' H8 X% d* w8 b0 b& ^/ S0 |6 w
" r9 ^1 x$ w6 f, \/ a! A, w
% H9 a8 U( b0 f7 M什么是ModSecurity1 X" }+ b! v% R/ p
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ x1 o. |; C& t1 a q
. A1 i( S3 V) b
) C7 o' e/ G# r) e& A
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
2 X/ a0 d9 s9 H* C2 B5 z& Q4 E: P
4 `! Y; F# \* ?/ g# B) ]0 e1 m9 T, k+ ]6 D. p
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 J! y0 |2 M7 G% r8 ~/ }
4 x1 p" \$ n. M% |5 h# N+ k' T- w) V
官网: https://www.modsecurity.org/6 A( {- c6 B: v2 r" |( D
9 k4 @/ ?& m# T: a# l: b- ]' K. `( b4 O; I7 w
什么是OWASP CRS' `2 |1 R; {3 ]7 F
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 A ]0 j7 `% F$ w; I4 k( X6 j( M6 O) q3 K& P9 d5 o
3 [" X. U$ p4 y* D$ b
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; k$ R0 c0 b0 H( c- I8 E: {
m" p' H6 ^' H* y( r/ z- R" ~
" I: h3 `& M- ]2 s* I, hHTTP Protection(HTTP防御)& d8 t! u( e, ]* Y. i7 N( x
HTTP协议和本地定义使用的detectsviolations策略。. G- ~) a) g8 q. S0 Z( R: r
2 M* d% V" r7 l& w8 _* K4 {
7 w8 Q; B1 V! v8 {/ D: BReal-time Blacklist Lookups(实时黑名单查询)
, l% G2 \1 s/ m) ^* ?! |利用第三方IP名单。
2 a: s1 ^8 |2 Y# q( L/ S0 `) d9 l( P, C. M3 ~/ P. S
7 f: W! X3 V0 k
HTTP Denial of Service Protections(HTTP的拒绝服务保护)- ~6 Z+ X" h" D+ [& w& _0 w6 e$ n
防御HTTP的洪水攻击和HTTP Dos攻击。8 A" R4 S5 e$ [' ^: i9 U0 s
6 ?- r3 V2 G: _. S% G, {
+ x7 c6 i' O$ a5 N9 ?( z
Common Web Attacks Protection(常见的Web攻击防护)
% \* m5 w2 c+ B0 n3 s+ g检测常见的Web应用程序的安全攻击。
9 c& \& K2 ?9 S0 o
2 s" ~3 |' u( Z3 C% h' @% V
& u4 V w) f1 D! u4 C% RAutomation Detection(自动化检测)
; T( r# {0 t$ Q) _8 ?6 Q" s检测机器人,爬虫,扫描仪和其他表面恶意活动。
& ]# @# ? C' X& d5 K# ]! Q: R7 Y/ M) u; h5 ^- E) R( M
5 s. Y7 X& P& X3 Y/ yIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
) t! O" r6 d) k: o检测通过Web应用程序上传的恶意文件。2 t% X% r$ n5 @( h) E
8 L1 f( T- x$ |, _$ [2 F. Y, t, R: h9 Y- V
Tracking Sensitive Data(跟踪敏感数据)5 w3 g& b, M7 T8 R5 t- D( Z
信用卡通道的使用,并阻止泄漏。
4 y8 O- z2 q& i v6 ?2 y1 V
2 R( p! ]+ ]8 d' [8 f @- |) J" S4 Z8 z( T, w* c9 S( b/ S, Q
Trojan Protection(木马防护)# o8 D, \( C' I, R/ ^
检测访问木马。4 S8 B( Y0 S7 [) R+ s9 @
G% X# o3 N4 n$ i! c6 U7 Z
* G2 K3 {5 \; ]: @* SIdentification of Application Defects(应用程序缺陷的鉴定)6 P' e! P! A$ n3 U ~/ ^
检测应用程序的错误配置警报。
7 Z5 B* j, ?* `0 S: _2 d. V2 u6 b$ M9 J2 Z3 m8 z
" O8 s3 A7 W/ \6 H. E
Error Detection and Hiding(错误检测和隐藏)6 X$ n. U2 z# \
检测伪装服务器发送错误消息。
) j9 V) C8 X" ^8 l. r& B
; o$ ^# g6 t9 R4 [8 e( S
0 A1 A0 D1 S( `! C& }安装ModSecurity1 C6 b. V2 [) {; M3 `& C. U
软件基础环境准备
4 p9 H- }2 @* m( u7 b% m下载对应软件包
# j. T+ Y# c8 a$ X$ cd /root# Q% w4 v+ s, c* t9 k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
. y. ~4 j4 N+ d0 u' J6 H" V+ a$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ P1 l1 [8 N. l+ G; r安装Nginx和ModSecurity依赖包
& z& b: Y, [+ R" t: X) u% [( `Centos/RHEL
" n& b4 {& u# f' w1 k, D
# S' i( D4 _. O- ~7 m
$ D y8 B; Z! _% x: I5 @$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel1 ]+ g# _' @ ]6 s+ Y2 s/ d6 d
Ubuntu/Debian! ]9 j( S0 {! g; c, X
3 m% C7 h9 f: {2 f; e: b" e# |
4 m/ a* m, K. U' Y$ a% ?$ 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
! r! J8 q, y9 R7 X# N7 G* E7 I编译安装ModSecurity2 M6 [4 c% i( r I/ |* X0 o+ k
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! l$ D5 c6 U" ~ \0 S! [ B5 W0 b( r) |0 Y
' Z' j3 v& B9 |
方法一:编译为Nginx静态模块6 S) y- r6 r/ g' ?' Q4 s. e
% F7 `4 W1 p" P1 F* \+ J4 G; v2 h7 n) m- ?; L3 s8 I2 z4 a2 K
编译为独立模块(modsecurity-2.9.1) y# \% U& E6 ~& A
$ tar xzvf modsecurity-2.9.1.tar.gz6 _* y' z, o# D6 T9 R, x2 e
$ cd modsecurity-2.9.1/$ }5 S) Q6 V% B! j2 n
$ ./autogen.sh
2 M+ p) B1 F. q/ [, D& _$ ./configure --enable-standalone-module --disable-mlogc9 H. y& u& q9 F9 o2 ?+ E
$ make6 N. d+ m( |6 Q0 Y4 h( w& U
编译安装Nginx并添加ModSecurity模块
1 ]. N2 v& c7 l$ tar xzvf nginx-1.9.2.tar.gz% A9 ^2 [* I, o: y- i- a( C( H- O
$ cd nginx-1.9.2
5 b+ Y7 o S2 e$ k6 n. \! f$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! t$ F, n) d( h$ make && make install
8 K- T% u3 i- ~, Q. D2 Y; x方法二:编译通过ModSecurity-Nginx Connector加载的动态模块+ q0 r6 l$ D" z: R5 r/ P7 K
' p& y7 T) p& ? }) [5 o# [& K; {! H
3 ~4 N. c' a: d2 y3 y编译LibModSecurity(modsecurity-3.0)# ~# N5 S q: {) L& g; {1 `7 x
$ cd /root
5 ]& U2 x( Z8 t: Y- j1 }8 O$ git clone https://github.com/SpiderLabs/ModSecurity
t+ s/ X% i7 {5 z$ cd ModSecurity/ C+ T$ ^1 l) e+ [/ s$ u$ `
$ git checkout -b v3/master origin/v3/master
/ B+ t# m1 \2 Z; O+ O6 U$ l$ sh build.sh$ V! D" `; j6 |* W9 i9 n) Q
$ git submodule init3 E7 _6 S2 V3 N8 z2 u6 w) D" R. W
$ git submodule update
6 s% C0 q; ?) I# g2 ]# b8 G: C$ ./configure
, }1 c o! P8 Z3 K, x" @$ make
8 d) C8 y6 y3 V( L$ make install; k4 a9 H2 _) Y: }- P
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
' |* D0 e& Z- A% G8 b5 l: d3 I6 t4 W/ @2 \% `
: R9 E0 O4 ^, g* }6 x
$ ls /usr/local/modsecurity/lib
2 }& w q5 @3 y/ ~. ^$ G! F4 ulibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0' ^2 h: E2 W. k/ |7 e' E0 C
编译安装Nginx并添加ModSecurity-Nginx Connector模块
' h$ J! d4 \1 H; @( P8 L使用ModSecurity-Nginx模块来连接LibModSecurity
1 s( ^* _, C! V6 E% P; c+ r* V3 I) e0 ~* k( J. x. h' M
& `: b1 O- E: v5 Z$ cd /root
N* O& Y0 M$ z9 r. M. p$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 q/ w( b: f; l% ?$ S! G: t$ tar xzvf nginx-1.9.2.tar.gz
: n5 x* P- {6 W) _) Q4 z$ cd nginx-1.9.2
& W1 |/ l A' Q6 V$ ./configure --add-module=/root/modsecurity-nginx+ e) |; i, y* {& ?- z/ U
$ make
& f7 U* Z1 T5 {9 ]0 ^7 }& N$ make && make install
4 G4 I+ H) e5 m添加OWASP规则
8 K- K+ m: d# N# d9 iModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。& ?8 h, y' y) f7 S t; l
! ~* a/ Q( P! `7 L$ d2 I
4 A2 k7 x- c+ A2 a. B* D7 {下载OWASP规则并生成配置文件9 f: @& h* p' O" I j
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
1 e9 z2 ~% q2 k3 [* [$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
) {7 O( Y8 n, O" x, `2 {, o0 x$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' O$ S3 |% S" F# y8 c
$ cp crs-setup.conf.example crs-setup.conf
z# a4 k, k; e4 y配置OWASP规则
! d* B& Y) R( [: W5 a编辑crs-setup.conf文件
) x7 b7 `3 }4 @$ f/ }. b' N
4 m7 O' {# d3 F# r9 Q6 V; d: B" }
8 f, J h9 [! p8 m2 U4 H& C# t6 S$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. ~0 _' J7 K Q8 } p
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
+ z/ ^. j5 i+ Y8 r$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! W7 i: u1 X" g1 X/ X0 Q5 O, k
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
& e q, q$ @3 e6 l默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" s7 \' O, C4 h' c, U: G! f
! r9 x5 w+ A/ J0 S" q
7 m! x' }5 q1 n% g4 J( g; o
启用ModSecurity模块和CRS规则
; C/ ~! I8 m1 Q复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- Y7 H5 _1 F& g/ S
6 ~) p& r2 O: l' n
" ]0 A4 y5 U( ]: \
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
# f; N" E0 A4 Q7 {
# j" {/ S2 D l
! E. Z9 D; ]0 b7 Q- P# o$ cd /root/modsecurity-2.9.1/ P; t, x r6 _$ u/ i
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
: s3 Z$ e& K( N$ cp unicode.mapping /usr/local/nginx/conf/
" p1 G8 J: l/ b将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 m1 d" |! ?1 D3 e# C
" C( `: I9 J+ {
) G3 O( `' |9 W- @6 h0 X3 i$ vim /usr/local/nginx/conf/modsecurity.conf
j, K4 f& c$ b3 e3 FSecRuleEngine On
$ a% C" o/ i* F6 V! p: p. g' H5 m, kModSecurity中几个常用配置说明:* S- y6 t. G; O/ @ o! q$ y9 a, {6 ?
: v' V1 V. k" h2 y/ k& T w
$ M/ d, Z, V/ ^9 Q1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; \, f& g9 |2 l+ Y1 V0 K4 t4 o4 {6 b- c) D6 P% B
' i( @1 T0 Z s4 Y9 M2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, f5 ?7 |0 q c+ ?* q
" m2 H3 H' X; e8 w( R# d2 U6 k2 h+ [, k& X9 |/ \: v
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
& ]' S& M% r2 J6 [6 D( [* I. y/ @9 Y8 n2 S; l! ]
1 v8 H7 e& h6 A
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: h R5 |) G" J
m7 a9 e$ B5 _ t4 W: v* q) n% z; o# b
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。; Z7 l9 W: \$ V
5 N/ \1 w1 w2 K) `9 ]; s6 e1 ]
6 ? ~4 ?2 w4 I8 Z$ \; R
3.x版本CRS/ V& L3 k( T$ j- Z" g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' l" G5 Z4 L' c( A# 生成例外排除请求的配置文件! |# y3 G5 [9 h: O
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ ~; ?; [4 J+ D6 ~7 Z/ r% Q! f
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' M; D0 k- c; T% ~* F
$ cp rules/*.data /usr/local/nginx/conf( l! n: t! b( A+ u1 o1 A! `. M
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。 K* N9 W) H8 k* J. q) ^) r& z
8 K! ~" f) `5 s# t) D- T
$ F! w3 r4 v) x/ D, E$ vim /usr/local/nginx/conf/modsec_includes.conf/ ?6 ~# ]7 J5 i, z5 H2 v
6 t3 e/ V. W- d8 w, `9 R% m$ l
[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 L% M; d* H, ]* Y" c- |3 M' g; _4 Z4 [* a; `4 f& a2 i- Z* R) Q, P
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. |- y/ }- P& T* i/ n) D. z* }6 i1 O
* t$ y) ~9 c5 i! w3 Y" P; }
! @. a- t* B3 @3 V7 [# C配置Nginx支持Modsecurity
% Z3 _/ C) a3 F( M$ p启用Modsecurity
3 g' U2 Q; f9 c- }7 t$ }3 R使用静态模块加载的配置方法
7 H% |. U( e+ V4 K7 J2 l4 B在需要启用Modsecurity的主机的location下面加入下面两行即可:
! O) |9 f( m7 s8 J2 F
% R- t+ n7 Z; p) Y( k
% p0 ^% v: f1 a9 \8 y" H, CModSecurityEnabled on;
7 O* N7 w' f. r QModSecurityConfig modsec_includes.conf;& L) L+ s, _1 T% u8 T! e
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ C$ D- [6 M; d* `* V
8 |& b$ ]1 w- H. l9 [
1 p" r" p* K. `6 I( y( o9 B$ vim /usr/local/nginx/conf/nginx.conf3 b8 R; S A$ u& \
; h" V# S# s8 v4 p- A( S
% i) W8 ^! J( fserver {! p. H# z/ W' ~! |, s. v
listen 80;
. D$ S2 s* ]! [ server_name example.com;
. G1 d2 v' i+ B# n' g6 x8 s* F P1 i( Y% l' j E% o# _% g& o
0 u# ^- y0 l( Q
location / {
4 G& t: o6 r: n" R. `, _* f ModSecurityEnabled on;
% F ?1 H- ^2 K9 ^* w0 ? ModSecurityConfig modsec_includes.conf;
7 l, ?4 ?5 O0 l& q1 q; L root html;
$ U& @- F4 n$ g2 U- a1 E# s index index.html index.htm;
, T! e( ~, R" }, r" r4 V m }
& _9 ?% U6 n: m% ?1 l}! ~: I, i6 o9 g t8 V% j
使用动态模块加载的配置方法
6 Q; T+ ?3 j# k# b: s" y在需要启用Modsecurity的主机的location下面加入下面两行即可:' ~. U# W9 u- v' J
1 C+ c1 ~0 f" i3 l0 M6 u( |
& q$ B- g0 Y- m% {$ B; r
modsecurity on;
; y) f- r7 c9 b# o1 u' P; imodsecurity_rules_file modsec_includes.conf;; A- O+ S# h5 z/ L; r: v6 f
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; o- J: n2 C; X6 W% @& q
7 V$ N: z+ ~2 ^% h
g% b/ K$ i! v0 o4 }8 E2 \$ vim /usr/local/nginx/conf/nginx.conf& w" M: O/ ?5 g& U$ B$ U
6 K4 t4 o5 t2 _4 {
# V, f$ d' ]' A2 r0 d7 D
server {
* A: u4 e9 m3 Z' h3 n6 `+ E listen 80;
6 r; z6 h0 E+ h$ ? server_name localhost mike.hi-linux.com;
, \: g2 b+ r0 L) U" A( w) {9 x access_log /var/log/nginx/yourdomain.log;0 A: u( ]2 ^+ L4 L" X! S
! S% ?" Z' L; d2 j
0 o! b0 p! t: r) n/ u. }/ c location / {
2 g! \& t9 `3 j/ N. j/ i4 T7 [5 p1 e7 m( x/ P
" n/ e2 y' m. k* \ modsecurity on;, z/ }3 t" D/ B+ V/ C7 ~$ r& X
modsecurity_rules_file modsec_includes.conf;
) k$ T1 b# H5 a3 B' o$ G$ [ root html;% k4 Q% E7 P. E
index index.html index.htm;% |+ \6 w) A' f/ B
}
- u; g$ X y, M# m5 [7 G}
0 d5 T, f! t6 e) E6 y9 a+ _验证Nginx配置文件+ G9 a# B$ P ~0 d# r3 p; x
$ /usr/local/nginx/sbin/nginx -t
6 D5 S. [! J1 p* \4 g [nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
( {! R( n: U Z0 D: m' Z6 l+ a5 mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' J8 O [: e C8 y1 W) o
启动Nginx8 ^* S1 m1 t* o8 R- j2 N
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf$ \; o' w9 O' s+ \- p
2 d" ?; F9 v9 Y _, @ H
测试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能够战胜更多复杂/未知/混淆的攻击模式。 6 D( z6 a$ V2 ]6 X' X9 A+ F, {
|