ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。8 g6 Y& n# t, p$ @; e
/ X9 A0 U F( |
" @4 `$ x3 r0 Z1 X
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% K+ o# X3 v2 Y* E' a
* }) P/ ~" B: ^8 ^2 f* C5 P% {; T& J& H# `+ z m
什么是ModSecurity. ]4 p& ~% Y& {
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ ~" U" }3 w+ v9 r3 a" X
, y: G, G6 ~! P) q- a* E6 Y: k! F* W0 g% }
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 t1 T* m' u6 \# H5 c0 T7 \
6 m6 B+ J! P$ T; b+ b0 u" b
8 Z9 b+ I+ S; v
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
: ^5 E% A5 s1 T2 B
% N. @8 s) D1 M2 Z* E0 K2 g
/ f+ Z. j. z* i. N) a8 U1 }6 j官网: https://www.modsecurity.org/0 d5 B9 O# e l
$ ~8 U) m+ u u: f* O. x
. s) S/ n6 A, c- T9 P4 a什么是OWASP CRS
$ H: i& [/ U2 k, a4 COWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
{0 k* K! Y: X: q3 c7 e9 `; }. o, d5 E) w
5 T8 Q" y2 K% `! w# U+ IModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
9 z' B& j7 T' T+ a9 j* u, Y8 n9 s% ~* z8 G+ R6 _+ K3 C! p! Q% h
9 j9 Q, H, N0 o- ]; vHTTP Protection(HTTP防御)
/ {! ^- k$ B* g) yHTTP协议和本地定义使用的detectsviolations策略。
+ B8 p) {3 m% F0 h% n& w. S$ b. X! O+ k0 V7 F8 E U
9 A" P* U2 e2 O
Real-time Blacklist Lookups(实时黑名单查询)' V2 H, Y+ a# ~3 d2 L
利用第三方IP名单。7 M1 z* D; p! W
8 d5 L8 X5 |7 M. q! E# f8 c
E% [9 {, |' iHTTP Denial of Service Protections(HTTP的拒绝服务保护)& g4 F* ?- K( E; H: N% H
防御HTTP的洪水攻击和HTTP Dos攻击。
/ c: B3 L) r; o- Z9 @
# I D& g) D% A& _6 ^
7 C4 S8 ^; Q! {3 \& W2 VCommon Web Attacks Protection(常见的Web攻击防护)
' ]2 F6 H" k+ y; {8 A/ ?检测常见的Web应用程序的安全攻击。
5 G8 }/ c9 E* |6 c! T: H9 t: [7 \0 d( N5 z U. ^* a/ O2 |* d
3 o E* [. n& Q) ~9 _
Automation Detection(自动化检测)7 n4 o: A/ p% ?7 ~0 N C, {" F
检测机器人,爬虫,扫描仪和其他表面恶意活动。6 _1 W7 G* k, O6 b7 T
7 r- A! m( Z2 \: I' \6 e5 y9 @% U$ h( [
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)3 a7 s' E$ M% D, P3 ^" t& w5 x
检测通过Web应用程序上传的恶意文件。& ~" ~# i) `& n) E0 N
8 I; r) G1 D* ]/ T" L
8 \) k8 r6 I6 f( P* M# s
Tracking Sensitive Data(跟踪敏感数据)3 _, A6 U* d1 i2 o
信用卡通道的使用,并阻止泄漏。9 o- S9 J1 [! _5 d
. C9 C( G P- l5 D9 b/ u4 L8 i& Z4 W& [/ i" C# x% {1 B5 _
Trojan Protection(木马防护)! b- q Q0 G: c4 [$ c
检测访问木马。
. |/ e. L0 `0 |! C
0 x- [- ?' u# O7 @* U* a* E5 P* d' w: Z3 t1 t. \
Identification of Application Defects(应用程序缺陷的鉴定)
# {2 m8 T" U8 j! i' T检测应用程序的错误配置警报。
- z/ ^ S! n% \- v! @5 Z+ V3 \" g
* ~6 G% @' L0 P. J( v) q4 DError Detection and Hiding(错误检测和隐藏)
4 S3 _" z4 k' [检测伪装服务器发送错误消息。7 ]# Z0 ?1 v1 n' n* b' k0 n! L/ O
1 L* e1 `" r6 X8 N8 w) Z7 O/ E
w* d8 C5 s( i. C+ I安装ModSecurity0 M+ }+ r; K$ \ c2 J
软件基础环境准备
e5 I% m$ _2 K: D& X下载对应软件包+ |! z7 j$ g+ j9 U# h
$ cd /root" s3 l7 H2 s! n9 H. N) p
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. v+ a" p! {" _
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; @; U& W2 u4 J1 B, E
安装Nginx和ModSecurity依赖包7 z9 }/ n/ f! r" w. [- H
Centos/RHEL$ X" v3 E9 F& B
3 H; M& c0 s! d3 `6 R. O7 n% B0 {
$ c, y- K& _3 L1 {2 _! N$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel9 ^# u8 L8 r& c
Ubuntu/Debian
* J3 H7 |' e/ K/ a7 M, ^& V+ o8 D4 I5 m6 f: y
( @( _/ D& E8 ^- ~9 y- T) 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
6 v8 ]) E( h e; i7 q编译安装ModSecurity+ p% P L( A3 N9 J z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
2 U& r; | o1 E. N! f7 {7 N9 a) z6 _6 T4 [: y, K0 o
0 Z* M5 K0 O$ B4 _8 }/ T [# j6 p
方法一:编译为Nginx静态模块9 i6 @6 i d6 p5 @: ?
, M0 ~4 j7 Y. Z% A' x' l- a) _% f
0 W3 B3 |+ [! [: t' \ y) `
编译为独立模块(modsecurity-2.9.1)" B" B2 G; p+ s
$ tar xzvf modsecurity-2.9.1.tar.gz( i b: f3 w. h O$ _# n/ `* r
$ cd modsecurity-2.9.1/
! G- `0 u0 ?+ s3 a0 D$ ./autogen.sh, ^2 S, R+ G7 ~% h& ]+ c! B1 O
$ ./configure --enable-standalone-module --disable-mlogc) S$ Q4 A( w" E$ C. c9 S
$ make0 P5 v% N+ f4 O2 P. z7 A3 @
编译安装Nginx并添加ModSecurity模块. h/ }: M8 ^9 @0 j4 P
$ tar xzvf nginx-1.9.2.tar.gz
) s ~- f4 B4 F& y; b$ l$ cd nginx-1.9.20 `; V8 P+ ?. | N
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/+ Y6 ~% Y+ b h
$ make && make install
! y: a/ f- }# u: J7 X. {4 n方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, T! `& G1 P1 D+ N3 E' p( ?/ ?
& l7 l# ] \9 P! f( g0 m* N; F$ i
2 N5 V. `0 w5 E1 x编译LibModSecurity(modsecurity-3.0)
8 o9 f, P7 h$ B$ ?% r$ cd /root; J0 Q. @6 z! Y
$ git clone https://github.com/SpiderLabs/ModSecurity( i! n {5 G2 d) L$ o% X3 e/ {
$ cd ModSecurity* u& Z" b" Q7 R A
$ git checkout -b v3/master origin/v3/master
. B) ^( X) n& V$ sh build.sh
5 }( e- ]2 R/ I$ git submodule init
?" x# y2 T! G `; Q$ git submodule update5 n, [$ Q0 B; a# ~! r5 _) |
$ ./configure# j& l4 \8 F1 k" d2 j
$ make% R3 X& m1 Q( c+ \4 \+ v4 R
$ make install- ]/ j2 O8 C1 ~
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: t2 T o' a: N5 P7 h
0 h9 C: C" o" {% b9 i* T8 J" D P% w- k
$ ls /usr/local/modsecurity/lib Q& @. I! V X' c& G( C6 W
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
/ x+ Y+ S+ O) F4 p4 j; ?& z: M编译安装Nginx并添加ModSecurity-Nginx Connector模块
! s; [3 l; G5 e使用ModSecurity-Nginx模块来连接LibModSecurity& S5 q+ I/ K r+ K! W- o
4 |/ `, }* v# p$ o" u# |
- p' ]0 r3 c! {$ cd /root4 t! i- D0 T' A* A7 n9 r
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
' l" s* |) \+ Q3 m$ tar xzvf nginx-1.9.2.tar.gz4 q& A0 }7 Z' q7 @. \
$ cd nginx-1.9.26 Q1 @; {$ o# }, r* C
$ ./configure --add-module=/root/modsecurity-nginx
7 E3 X: p8 T* e4 h/ t$ make4 j) d# x X. U s/ k" @
$ make && make install9 n* L0 p/ }5 W' p x5 D
添加OWASP规则
7 v6 Z: d$ a c# z; iModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
+ U) d: p7 F& x) n) D' T5 u/ p' C" L6 Y+ ], T* J1 }* m( K
) R7 j7 R1 A2 j8 A- [+ j2 H6 `
下载OWASP规则并生成配置文件
0 E2 z0 Q. q' x) V. D! o4 s( ^$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
8 V. G0 t& n' Q% d3 \$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
/ H1 f5 D, }4 L/ Z% ^* @+ v7 P/ h1 p$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: `+ v4 D2 E0 H7 {7 h8 Y5 Z$ cp crs-setup.conf.example crs-setup.conf
5 P) d7 ~7 C- Y* G: p配置OWASP规则( M2 j# T I) t2 \, K
编辑crs-setup.conf文件7 p! v0 q% B& E# ^- z0 ?* w
$ V# c2 S! n2 L& z. @8 O! e" y6 A5 |# m6 w# `& o! X
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf, Z0 x+ k: ? L" a
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf; r: P t c4 Q; f3 q0 r3 z
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. D# }& R% q" p# `: _: z* |3 L) c: f$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 G: u! I4 u Z8 h& f. T默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" B! J: ?( U& |$ g+ P/ t* M$ m' y9 m; o" P( ^* z
5 p1 a+ A. m7 [" p启用ModSecurity模块和CRS规则
1 f' U' _7 d/ L5 j1 ~7 X( K4 `0 ]复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( D% {) T& [# @9 ?5 }8 K$ T/ ]0 U8 S! [, i, i) n6 e
$ G) \& v A6 Z: f( l3 w
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 I/ [) T' ]. i' Z
6 M e5 U7 l0 k) w/ z+ x; h. U, O% H$ i0 x3 p
$ cd /root/modsecurity-2.9.1/8 v: H L' W1 Q/ @' ?4 O
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
5 V" G2 _, h( X% P$ cp unicode.mapping /usr/local/nginx/conf/
( R# N* L! ?0 F1 {将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" a. [, D+ Y7 p, r. i6 p9 l( {$ _
" ^+ C+ ^8 N+ O4 _# d) G0 I$ vim /usr/local/nginx/conf/modsecurity.conf
& |4 |7 Y" L8 A6 V3 u" P5 K/ WSecRuleEngine On
5 \( u/ a& z4 F; S! S# H ?ModSecurity中几个常用配置说明:: V+ G; O% ]. {' ?% }7 D
! Y9 l& \; [: _& ~4 {
, v' G) @) h* I {3 M" S3 G7 ~* }1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
5 P. z2 H9 q' [/ t0 w9 R8 h( c3 B6 d; I& i4 X# J
) O, h2 z- D. p a
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。$ F8 L) K8 U! J* R1 d, j! d N
6 m' H# |, N) S8 |, J- N
9 X7 e8 y! u# ]+ k7 J" k3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! p1 Z, ~* r7 u
: o4 r8 m, T- A$ a
! N' s% B3 m2 H' Z' u
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
& A. I0 h( C3 D. R9 u
: m4 A6 \, ?& m/ m' `
, d9 D( ]: d N( N在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 r8 V% O. M, ^7 ]& q6 d
* R7 v) W1 ^3 Y7 c9 `9 M. ~( n6 S2 T, r& s" U
3.x版本CRS
; D. a* i+ s" v$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( n: m6 l" t1 M% s1 c+ w7 E
# 生成例外排除请求的配置文件
`( t4 K9 W5 F2 V2 x9 g) c& f$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
( e$ Y6 R1 P, B! Q8 n0 p$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% I& r. C5 z& |0 ^* ~$ S' Z
$ cp rules/*.data /usr/local/nginx/conf& Z. c5 i2 H7 c( x
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。) l* ]& {2 h9 a! D+ \
% u( w, `/ u/ }! x- w" N# z* @9 v- l" f# I/ p
$ vim /usr/local/nginx/conf/modsec_includes.conf
4 J$ n$ z8 o. S& I
5 x; U8 P1 Z3 ^& j+ S[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 0 E! T9 u4 `+ F# [0 l/ p) ^# I
' N) _' K* g3 m+ Y x注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) a; U* U! t e' k7 T" _4 f' }* r+ Z l/ W9 }1 m5 u" F) Q, k
# i6 n" U2 q. n. {
配置Nginx支持Modsecurity3 K2 b: A. }/ g
启用Modsecurity; C% P" `; k! D* m4 K
使用静态模块加载的配置方法
. z! i' r7 r6 D$ A' b在需要启用Modsecurity的主机的location下面加入下面两行即可:
, i' C$ g* r& Q4 `; m2 n
/ ?4 I+ L4 p; {5 V* J# h/ D( G; s3 {9 Z7 l
ModSecurityEnabled on;
+ k6 K) U/ q+ ^ModSecurityConfig modsec_includes.conf;
5 |# v4 a/ \/ D- n/ K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: S5 x) o" F. L6 g8 c
; r* \8 N( X$ C( c" y1 B
. W' y6 `6 d7 j; J$ vim /usr/local/nginx/conf/nginx.conf* }) M4 Y- M+ b
% w6 _7 z1 B" {6 o: A9 Q" W. G. ` }3 I6 Y- C
server {- E& n+ V E8 j6 `
listen 80;) q6 Z8 M! \9 Q/ L
server_name example.com;1 a+ y3 t! {! B, d; ?/ Z% j
1 W' U, _5 u* ]+ N/ ^) K: S8 @; Q
4 s- S% P8 j% u$ [- P location / {; s4 X# u/ p5 x$ W0 a" W! ?
ModSecurityEnabled on;1 c/ J1 e) P3 h$ m$ Z8 t0 _
ModSecurityConfig modsec_includes.conf;
; ~6 A2 x; l2 F8 f$ ]% T ~ root html;, w6 q7 I R) p' l& Z
index index.html index.htm;
7 t9 S) d" H; }! @' z9 u4 V& \ }
) L& q* S+ W$ }( x}8 ~: _& y1 }( b J
使用动态模块加载的配置方法
& e) X; G% |" M! P) m/ j在需要启用Modsecurity的主机的location下面加入下面两行即可:. O9 J9 r- L7 z5 t
- y3 V9 g% W4 J& A6 O, j) R
2 k9 p1 _3 {7 Y( |9 `5 zmodsecurity on;, \* T" l7 `7 f" @, s1 c
modsecurity_rules_file modsec_includes.conf; i4 c) k9 _5 X* g
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. g& f' r6 D5 f% E% E3 l
3 _: U6 p' k5 S# a0 D( V( p' F5 b; W- C" G: V8 Y3 v
$ vim /usr/local/nginx/conf/nginx.conf5 B& }: o" v3 L/ {
/ x& Q4 \& }* U7 y& E9 ~9 `* c
u: \ n) Z& V5 d- x, z1 Kserver {; b9 }* ?% y7 b `( _$ `
listen 80;
3 p8 V% |% t2 a6 |: S; V server_name localhost mike.hi-linux.com;- s. L0 P& l& }+ v: X) j
access_log /var/log/nginx/yourdomain.log;6 |; z* }: v% N6 z0 L) q$ \: v
9 j( Q, o( G% ^+ \" D) ~0 g7 a
) }$ {& t5 e: w1 v5 F( l2 F4 \ location / {9 I+ D$ L0 T& F1 L. I |( k7 W$ o7 Z$ a7 Y
+ }: \1 ~) o4 Y1 u. f( \$ k
% J4 f' a4 [* G" W* G
modsecurity on;
% _! h7 O. T" { H( O( X$ E modsecurity_rules_file modsec_includes.conf;& M& p% j, Y% v5 q
root html;" |" d, U+ s7 _3 u1 ^
index index.html index.htm;" {1 t( V4 N$ f( B3 q l5 K$ U+ y$ E
}
+ M4 \1 z! U7 [0 L4 f- ?, r% F}
6 y% Q3 V. [8 H/ C$ k* o验证Nginx配置文件0 |2 r7 U. N$ k4 W- V
$ /usr/local/nginx/sbin/nginx -t
( w) x0 _( B0 i7 V* Rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok8 ?7 z. k0 w# O2 m. V& c4 S( b$ _. _
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 M4 `2 j8 y& y" q5 f+ w0 h- W
启动Nginx
8 H# [" t" F7 w$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf! i' t' k/ k/ F8 M' h( U8 D. c9 Z
5 P1 y5 b0 k7 `7 {2 Y) E
测试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能够战胜更多复杂/未知/混淆的攻击模式。
4 u1 W7 R9 j t: J' S |