ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
/ |4 j. F( \% U0 y: S# i ?( e) g U7 A9 C& r4 [
% I3 d! k1 b, r; r$ I& @在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。# C F2 _/ ]' Z& Y
! E5 v; p: I6 w+ R
& ?/ o s7 x! |# T什么是ModSecurity/ b- E$ E& V4 S- [! Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- ?0 u8 W$ a/ [9 ~3 G* L7 R/ K" b. u( F8 q' A
! p1 A% L: j) k2 D9 wModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。2 n+ o! R" k; g5 W' g
* z& M; c5 o8 J' L( E( ^6 M! i9 l1 A5 s% y' m( d) U
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。3 n( K; F8 I4 y( x! ?. h Q
" W4 I+ E- t1 q& q: ]+ a# {
1 p* a- b0 t c9 ~8 E9 h M/ _官网: https://www.modsecurity.org/; Y: ^* w5 b* q* h' d/ f3 @
- L' [* G5 c. p
8 s( O: M' b8 o6 P/ ?! D8 k" Z
什么是OWASP CRS
4 X* a+ v2 H) {* G3 W2 zOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
5 p( e. z9 F8 W' Z- r ]
/ p0 S, o# x7 X& L( C5 y$ q+ |! o1 e. M
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。( `+ }( V, B5 K+ b6 n
' y' ]6 e4 ]$ [7 |
- M9 a6 F' z( F. f# O% `2 tHTTP Protection(HTTP防御): J: r) i6 G* q3 j5 _' Y
HTTP协议和本地定义使用的detectsviolations策略。. p3 t. }4 x9 R$ h* P3 T4 q
# ^7 t& I& E% C3 G* v) H. Z
: w4 G* R) p( ~: f- p
Real-time Blacklist Lookups(实时黑名单查询)
4 a& q3 k h f e利用第三方IP名单。
0 x0 t0 S3 {: h5 B/ e1 C5 V+ [: h2 l1 V
$ \0 L: y" r3 g6 S) ~9 e& D' YHTTP Denial of Service Protections(HTTP的拒绝服务保护)1 G! F e" J+ d% v7 M9 |) j
防御HTTP的洪水攻击和HTTP Dos攻击。
" [9 W1 t1 s: J P& D4 i) P
- ?( w3 F5 Y! d, o1 I
( Q: O2 U' u1 KCommon Web Attacks Protection(常见的Web攻击防护)8 V8 |% b3 B, U4 y6 L
检测常见的Web应用程序的安全攻击。
6 R. e I5 a" k" a/ H- m
) M9 X# W* t) U. V2 O8 z
% w. J1 l/ J8 {9 Y' c# AAutomation Detection(自动化检测)5 d1 A5 O, T0 r+ A E% f2 K
检测机器人,爬虫,扫描仪和其他表面恶意活动。( M) a" v5 O, w; K$ r
5 r- o, ]# P2 O( n" j+ {! r+ S4 X8 j# q/ S/ L4 B
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% M1 @. ?9 B' s检测通过Web应用程序上传的恶意文件。$ x) I" z! F- M6 Y. E
* z1 c( m% ]) B2 D. y, S8 \9 D8 G% l+ h8 R; H9 V$ J
Tracking Sensitive Data(跟踪敏感数据)" w6 p: {8 Y( G8 E0 x M& T
信用卡通道的使用,并阻止泄漏。
' d9 a5 P0 R/ `8 ]/ T- T6 X& V: A G/ f, ~' V+ W* y
; F P G6 V8 I5 W
Trojan Protection(木马防护)- L. G$ M6 u; j7 t1 g
检测访问木马。3 Z- y5 g+ \ w0 u4 d
2 S5 i& `% y4 _4 n* A- Z s8 x( g" E
( i/ u8 }+ E( s6 nIdentification of Application Defects(应用程序缺陷的鉴定)1 |! m6 p3 }4 }2 g3 ~9 V3 i* H
检测应用程序的错误配置警报。
& W# |' A2 N1 K* e+ [8 e! G
5 w2 I3 G2 V1 X& T2 `, \3 t+ a% R9 ]. D \4 {2 T
Error Detection and Hiding(错误检测和隐藏)/ _7 u8 W1 Z0 [5 x4 c$ p( O" [+ C
检测伪装服务器发送错误消息。
! v/ p+ g: b% G! G
# G/ ^; m) V: S0 w; U3 [
# M8 v7 m1 h6 a, c- ]安装ModSecurity$ A U% y \0 B( x' f/ G
软件基础环境准备
0 s- K% z! B% @( I! Y. w下载对应软件包' R5 \5 Q4 z5 c( O: x# \4 d6 |
$ cd /root; V8 T! `/ [' j6 [
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. F5 r! p3 _2 Z [$ ~7 f
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: ]" D0 o$ z* O9 z5 A% l
安装Nginx和ModSecurity依赖包
5 w0 X4 B( P& Y4 mCentos/RHEL3 ~# m+ t; Q4 o7 r; f
+ D0 A7 |1 g. ]& Q' n G) d+ U3 N7 J$ H3 r3 t4 h, G
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& V |0 o& a) z) ~
Ubuntu/Debian
* N+ ^1 C% s* |0 z( U6 W. f5 C0 }! o* c' M
- V7 R ^9 n2 z8 H# G& U; L
$ 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
4 C- l* m& j, \ `7 |: @9 g+ `编译安装ModSecurity
$ Q' e2 h0 m' xNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# O! h; A) Z5 \7 G4 T
8 }, F. M, w8 I7 }8 y$ h
4 k2 P% ? G5 `4 j8 z方法一:编译为Nginx静态模块
+ j- |9 c% Y/ a& ^! p% A' l- s5 l& e8 M0 E3 V3 C) i* I4 ]
0 W5 r- A/ t: @' z" i# _1 Q
编译为独立模块(modsecurity-2.9.1)
& P e! W) d8 n, }6 w8 I$ tar xzvf modsecurity-2.9.1.tar.gz
+ |5 _* |) ~' V W$ cd modsecurity-2.9.1/& I% t0 E" v4 z) j5 C6 u' `
$ ./autogen.sh
- S. F- M9 d! W; B6 G- \- M$ ./configure --enable-standalone-module --disable-mlogc
3 T6 Q+ S: Y* ^9 t! O2 K$ make& g, `3 R- J9 L, o/ t* k
编译安装Nginx并添加ModSecurity模块
: J8 H- X8 {# F' ?2 f$ g) o9 @3 w$ tar xzvf nginx-1.9.2.tar.gz
2 i _3 X4 I2 [. S# Q5 I) V$ cd nginx-1.9.2( Y0 |+ T% y4 b/ y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* S9 a: W! K4 F8 Y, I+ z
$ make && make install
9 v3 u# v6 g: m7 u/ C方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; u' B5 P3 | H) x% n5 Y$ ^7 O7 a0 ~% R6 G
# t0 h" K6 U1 J9 T( z编译LibModSecurity(modsecurity-3.0)
* \" @1 H+ g3 V$ O+ G- R$ cd /root
! p; R$ a4 C9 ^$ git clone https://github.com/SpiderLabs/ModSecurity
# M, u: Z5 c5 r7 w7 u; O$ cd ModSecurity. [. B, X) P0 P/ A
$ git checkout -b v3/master origin/v3/master+ k# Y% n& `2 k# s' K7 l
$ sh build.sh$ h0 l0 ]$ Y3 d* P7 a5 y
$ git submodule init# F- E3 `7 ~9 K; b& v' |
$ git submodule update! f+ O$ L( G& H V ~. Q. [$ y& r
$ ./configure
4 k' y% Z4 c% W7 G7 {' i$ b5 |$ make+ p: c. D' E) i7 x/ q1 J
$ make install2 ~1 l1 l& z# h( g1 ~6 n% b
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。$ u. t$ C1 @! i9 [- v. F6 _
% ]2 y. ]# g0 n w& Q" z, q, R1 |+ ^9 l3 z" ?4 U8 X
$ ls /usr/local/modsecurity/lib5 D+ M) r: @9 M8 `/ E
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
- i2 |* Z4 @( O3 h8 [# [编译安装Nginx并添加ModSecurity-Nginx Connector模块) F5 j& q. d7 F& G; r3 N2 B _6 }
使用ModSecurity-Nginx模块来连接LibModSecurity
* V/ E7 d" K# r4 U4 m4 s1 n
1 n7 P; f* O1 a X C5 G
L$ d w+ o }: d! O$ cd /root
' ]" o" l4 f$ ~; W0 E$ L$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ S& a( F) F6 x. S* Y$ tar xzvf nginx-1.9.2.tar.gz1 [. X1 K' t t1 \
$ cd nginx-1.9.2
2 {$ J3 l" j/ Z4 l- N9 L" t$ ./configure --add-module=/root/modsecurity-nginx* h. u% h$ q# F+ g- s1 N
$ make
& O; L% L2 b' y0 u% G$ make && make install
1 N) p7 N5 ~( I$ f) Y添加OWASP规则8 u A9 \$ }3 ?* j3 H* `5 f
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( Z! ?+ O: _: g; h/ U5 l' M" q9 g' h8 U! M4 i
1 q5 E- ~5 n3 q) ?. V. D, R下载OWASP规则并生成配置文件
4 f R1 j" S0 T: S$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ Z( U- x7 ~3 a0 V. b5 [7 H0 i# @$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf// P6 k! J% x- d' N/ c: ~' F* h0 r5 B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! Z! R9 j/ [* ]5 S! o0 a8 s. `$ cp crs-setup.conf.example crs-setup.conf
5 o v" c5 w4 j( H( z) b配置OWASP规则4 U+ @; n/ ^# C! F; e. P P
编辑crs-setup.conf文件* G/ r7 j% G F! }( p0 f; _- H
3 }6 R9 _+ `4 t& n9 H# J; W) C
. M7 i: C8 R+ j+ n7 u& `
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 h$ \* W1 _5 g$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, q5 H1 W' k% Y. ?$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
+ E" _; W* L9 n$ E$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
! c; m) w3 I# u, x! |" @- X默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" ?$ _8 L1 M/ Q. q- S; S7 B
4 [& |: @& n4 ^6 u* M/ h
# W! C; _0 j& H启用ModSecurity模块和CRS规则
, d( X- c6 \6 D/ I ^复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 ]4 k" K1 N! a5 S s" z. }; K
+ C1 G4 o* s6 X- u0 C6 }' P& y6 I6 y& X$ x
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 q& G- e. H; r: v
) B9 J' @) o( e% }+ X7 _: } ~' n/ z. w; k, x Y
$ cd /root/modsecurity-2.9.1/
; ^1 _; z9 k1 T& O( B$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ) {8 y* ?2 Q0 Z# X, U' E+ `4 m
$ cp unicode.mapping /usr/local/nginx/conf/
) T( s. l5 j9 z, S( i0 l将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
2 t# O R9 A" X, R4 d1 p. a. M
4 |9 T2 z& k8 w0 ?5 M: c: \& t! C+ z5 F8 h
$ vim /usr/local/nginx/conf/modsecurity.conf
( @0 s) O# r, ^' lSecRuleEngine On: t% [% x$ l2 Q$ k, |# a& v* j) z
ModSecurity中几个常用配置说明:$ i; w, V e! e5 Q* w
6 L' [& J- D$ m5 Q- T9 ]5 L
, `' a1 ]$ `% |. E; Z( D# ]+ w1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。 @0 H1 k! b7 P- N- C9 w
. H; u$ v9 W: y# X* d$ p/ |
% w: Z# d, T# ^% \' ~: {; j! l2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, X& Q& c4 ~' i! o$ K
& q6 U* I2 P' y7 O+ L) K: w) ^; f. L/ ]$ a4 ~
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
7 ]- j5 H/ e( I& G) m& H- J/ b4 L, ]& X- S! w
' b8 d T' w; @$ [2 m4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。/ @2 r" Q: s9 R
4 V2 k3 \+ R9 g$ v* {& N: p- Y# u0 i; U. q; y% \! f. Y8 n) f, F) c) e* r
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。3 ~( b5 ]: H) t# T
2 N4 W, O5 @ z" l: Q, @$ E& `2 a& E+ f/ r. K I% f+ ^( t, _: \
3.x版本CRS; `, g" c/ G6 r8 o* a
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 K8 i9 x8 Y8 F7 j( Y) m# 生成例外排除请求的配置文件
6 ~$ k1 W# v! j5 e1 |5 Y$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf7 q3 W g0 i" K2 t$ W
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
8 @; w$ q7 M- v4 m: J. X. |% V$ cp rules/*.data /usr/local/nginx/conf
9 O8 k+ R' @- a4 D为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( L; D) T/ M+ X9 Q
- _$ {+ G6 v5 k C0 U
/ t' u3 j0 B3 E( }$ vim /usr/local/nginx/conf/modsec_includes.conf
. }; z/ x# n( Z
4 ?2 Z O) M. X- d" l! }$ `% a- Y[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
# L C; \' \9 [& I+ z- J& ^! G4 n0 K' R* @% A" O# y
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。+ r( x: L# G$ F
' u0 Y- x; z! ]# y- I" A+ I
. f8 G7 E9 q' `6 v- R配置Nginx支持Modsecurity# T- b9 U) F- t9 S0 o9 n) C
启用Modsecurity
2 D1 Y' D# g* E8 C1 u使用静态模块加载的配置方法
: \) Y5 [7 D5 U6 s. n8 N在需要启用Modsecurity的主机的location下面加入下面两行即可:$ G. f, f, p9 K0 d% c4 I, b3 w
- \. i) O3 v, ~
! z/ U) H& _7 v' ?; FModSecurityEnabled on;
6 g. N' R+ T1 ?* Y& u5 d* e# f3 H6 DModSecurityConfig modsec_includes.conf;- G2 \" p" s9 T9 _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' U$ v, l! n; x+ d+ S
8 T$ B- ^- t. P# X) V7 W Y) o$ n1 N( Z4 T# K
$ vim /usr/local/nginx/conf/nginx.conf
2 T' G( _# Y9 g) ^& Z( J; N! P: a* \5 B. q8 _* Z3 a
1 k/ ~+ M3 c M' i5 i u' s$ mserver {! `7 j1 m( b: ^( T
listen 80;: L$ X3 J- Z. Y8 Y! {
server_name example.com;7 X8 D8 W5 A6 @, h$ P
& E; b( i3 x5 ~+ b0 b0 Y9 E' Q& O: G, L: i* W2 R
location / {
* l9 e; z. A. O ModSecurityEnabled on;' x9 B |- I% j8 c
ModSecurityConfig modsec_includes.conf;
2 c C! S, W' X% D0 y2 @. I: M7 ?% y root html;8 U7 w$ {4 Y7 [3 V$ _
index index.html index.htm;
6 {6 {9 `4 U' j) q }' d! j/ x" ?! S/ S) L
}6 \9 d: X; l( ^) B# b
使用动态模块加载的配置方法
9 Y. z3 t, D$ y0 h5 f! E在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ V9 w4 m U, v; [- I, {' O# N6 V% T+ U$ w1 F
& r. E; U6 W3 }/ W" r( p# H
modsecurity on;- y8 L( V# v. T0 y7 H2 U
modsecurity_rules_file modsec_includes.conf;$ y4 o, z& K9 T p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' W* J8 K6 C# L: J" L
$ R/ T2 a$ P3 ` X7 `! Y: g5 ]
& T3 b/ G& I7 ?% X* a3 S
$ vim /usr/local/nginx/conf/nginx.conf
: a( @; j/ M: }
! Y4 _! |) u3 |. L( q* {& ~6 O/ _4 z4 b
server {3 v- d7 Q1 ?: t1 f' r
listen 80;9 C& n% R1 E5 f: }$ d! r# {: ]4 j" |
server_name localhost mike.hi-linux.com;/ [# a0 I3 |7 `
access_log /var/log/nginx/yourdomain.log; `" [9 v" S9 u2 ~' B
. o4 R4 m+ X& K, J# b+ y8 q4 j* \# w' I! A) n6 [0 }
location / {
+ i" P9 D1 {0 o$ g# Q
( ~! N6 z# x) H0 H2 d* X3 s$ L# }$ J! L4 K) }
modsecurity on;
/ m' g- \8 s) V' `9 `) X modsecurity_rules_file modsec_includes.conf;
; Y! _% A; l1 J6 p+ e! @ root html;
1 Q2 f' O+ G0 F index index.html index.htm;. g% `3 r4 G0 |1 q9 b: H9 D- }8 ^2 y
}
3 g: G$ r% M% S" D}
2 r) t4 B& y1 X6 @5 ~. {验证Nginx配置文件# f# c$ V v: e2 t! A
$ /usr/local/nginx/sbin/nginx -t
+ o1 _- X1 S% Jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 @( _2 M, a. ^; Z1 J% G
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
" o) X( d% E0 h* @2 Y启动Nginx4 ?8 _. p! ^4 ?
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf3 G& c2 F' h/ z
% i' \( l4 P0 h+ R4 V5 n测试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能够战胜更多复杂/未知/混淆的攻击模式。 + @% r3 g% u6 \1 O/ c& @" U
|