ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。4 t$ n# _! ?# q# M W- T
+ a, Y9 J: S3 r8 H7 k$ ]% b0 Q6 }/ h3 R1 t1 {1 k
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 K6 ?! n+ l/ z& ~ v1 ]
0 Z5 S0 g, R6 K( ]* S; G0 Y- v3 ~$ M3 k L* o
什么是ModSecurity
: Q. B& X0 `, L1 y$ D* T9 JModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 [3 F4 h% t5 s6 ~: f, k, F! _; i. l
+ Y& s" m7 X" M: p
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
/ r+ ]6 k" c. a. x3 @7 U( n' n6 {, ]: t
( P. Y' g0 e2 t# [
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( h) t" u: b3 A- e% j/ g1 M
- n( I( `- R' Y# T- _3 J
: |" i+ D! v8 S5 e4 v官网: https://www.modsecurity.org/- J# m* j6 x8 Y) p+ G, d
N8 N" o$ ]( V; i! _9 q5 n/ @
! K; B2 b1 E8 J+ X9 d7 h: y! [什么是OWASP CRS
( ~/ f+ G* I- V+ gOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
- |- [- O9 e R! g% I
+ F* p8 R! ?8 x8 G7 }6 W4 l( y* c. m. U7 }: ?7 z" v; L
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ d6 X9 A6 S2 g: g9 f1 t6 V/ _) F3 n, R
4 }; d" s3 ^" F( X' }# ^
HTTP Protection(HTTP防御)% j; Z3 L+ B4 L3 f$ O0 N2 B
HTTP协议和本地定义使用的detectsviolations策略。( Q$ a+ x( @/ R, [" H3 i# t* k
7 ]4 A2 x# Z5 p* E7 ?1 w- w9 ?4 u0 j, V
Real-time Blacklist Lookups(实时黑名单查询)
+ V! g' f; s- \9 s( ?; |利用第三方IP名单。
8 Q- E5 C7 y% h9 z+ Q3 W2 P' s' K* I; @8 f* N" p. k
: l, V( r/ m' F1 L1 W/ A5 ~
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 o G; A N: [- B: `$ \+ X防御HTTP的洪水攻击和HTTP Dos攻击。
/ S3 y4 T7 b9 |+ f' f
$ d; h' S2 R* u2 i) W3 m, @/ e2 u) d8 y$ A" M' [+ g
Common Web Attacks Protection(常见的Web攻击防护)# c4 J4 I& S! ~" m4 W4 i% G% [
检测常见的Web应用程序的安全攻击。3 E# Y4 d8 w! q4 [# J: i6 K) W
5 p( D7 l1 B) A2 p+ d
; E. }4 |# _ y# h; _Automation Detection(自动化检测)
( w. ?8 h, B( V* o1 t检测机器人,爬虫,扫描仪和其他表面恶意活动。
) [/ V6 D5 b% B# i8 Z- {3 y) M$ T ?/ H. t7 v* P
4 ^' `! w0 e4 c1 n
Integration with AV Scanning for File Uploads(文件上传防病毒扫描), {$ O9 s% h4 x. |; x
检测通过Web应用程序上传的恶意文件。
* Y0 L; R7 G7 S; D
5 n, S/ u8 U0 D) N- U+ j6 y9 r1 l9 h0 I) \+ c
Tracking Sensitive Data(跟踪敏感数据)6 E6 W+ I! d! w0 _
信用卡通道的使用,并阻止泄漏。
5 l8 w. }0 c' ?3 r" g* T4 h3 V, {
% v' U4 h. D& D0 c: Y4 G* w3 J6 t' |
) t; c% T/ d1 S) M. {Trojan Protection(木马防护)
# N7 m* @! ~. s* ~检测访问木马。
: V& O+ A. l5 M) I7 u& m$ g5 b8 y7 `$ r: n |# D
" _- P- }$ Q/ i2 e; S( j m- a8 X
Identification of Application Defects(应用程序缺陷的鉴定)2 l. O# z/ H$ |3 ]& S# Z
检测应用程序的错误配置警报。- X" J5 J: h: ^. T V- c1 K- r$ {
" r- ^, Z& @. o, r5 E( Z( u6 @: h4 H! X1 e+ O% @. R
Error Detection and Hiding(错误检测和隐藏)# ? y8 G% i# y3 h
检测伪装服务器发送错误消息。- _5 G. K" d! u% J' n# _: o
1 `% K# e- ], x2 p
/ }! w6 G2 f9 D$ S安装ModSecurity
2 d7 \1 Y$ C! t. S& n软件基础环境准备. E8 n7 F% z {1 [: E
下载对应软件包
; [) q1 }: V- r1 U) w$ I4 m( ]2 v$ cd /root
u, L2 Z% Q+ ] Y( {7 h& Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
' C s+ L$ U" T: s$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
8 C2 N3 y9 C( v- g安装Nginx和ModSecurity依赖包4 n9 G% w2 B( h f
Centos/RHEL, E; h4 k: q4 f, U" M. l
[7 {' _, l5 g4 p% w6 L2 e* P" s7 Y/ j# S
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# s T/ p H+ Q' ?Ubuntu/Debian
+ O( G& s+ U- X& E; s
$ M1 A2 j7 @% w7 t' `5 t) L# q* j+ R4 [. V
$ 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 A6 _7 n* h: u0 }2 |
编译安装ModSecurity
- V& D! J3 F' x+ ]$ _/ HNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! q+ g' [" x5 c6 B" L5 ?
- D% H9 g. q7 m
% C4 r+ Q( A. n% G# j' r- ?8 |+ t方法一:编译为Nginx静态模块) M" Q" f! y9 c9 Q5 {
& I- R1 x3 C! O4 Z& L' a3 l9 L$ Y$ D- Y0 v
编译为独立模块(modsecurity-2.9.1)! K0 t: b4 q* R0 b
$ tar xzvf modsecurity-2.9.1.tar.gz
$ A, `1 C- e+ D" |6 n2 k$ cd modsecurity-2.9.1/
9 b" {9 q8 H; J$ ./autogen.sh
2 g* a" f/ U& \ R$ ./configure --enable-standalone-module --disable-mlogc( p$ o' o) u; s7 x( x
$ make
; {: R: A7 a h- N编译安装Nginx并添加ModSecurity模块
B: o* v) b, M! G$ tar xzvf nginx-1.9.2.tar.gz
2 Y6 }$ [* a) X/ F, ^$ cd nginx-1.9.2
+ \! g: H9 y1 d4 q& y2 K- g$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
. r, {7 A( N6 W, n* J( \, ]; i: J" q$ make && make install. c, V6 t8 ]9 L5 a" U( w! t) q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 v1 n- u) Z/ E2 W% E( S. E# X3 _1 Z+ y1 f4 L
5 x# ]9 D; F. S8 N
编译LibModSecurity(modsecurity-3.0)
5 O2 r7 s8 J4 v' \0 y$ cd /root6 R' i; H+ V7 Z& K( T; u, ?
$ git clone https://github.com/SpiderLabs/ModSecurity+ J; E- [( J5 r: S0 `
$ cd ModSecurity
8 j) _- T+ p3 T ]# [$ git checkout -b v3/master origin/v3/master
8 H0 i5 M4 V$ T/ @# t$ sh build.sh: ?' B3 ?% Z! H" L8 e( b3 W
$ git submodule init& X8 F& k# N6 o4 k8 D- F, d
$ git submodule update
}3 L; F# D5 ]: c$ ./configure
. z, Q- a7 s Q# K1 D$ make- D4 g0 M( ?% a7 O* C, _! T/ O
$ make install
6 V8 b- Y$ K- ?4 B& WLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
6 i, W% {/ F: ~7 j" I- D3 w6 E
; @$ R+ }% L' l8 g, Y6 ~( V3 }; a/ i: V# m' K* p) \
$ ls /usr/local/modsecurity/lib, x9 p9 ^% n5 s
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
6 H2 J) s: \; }1 q编译安装Nginx并添加ModSecurity-Nginx Connector模块
' g/ }* g0 b0 q2 F" t使用ModSecurity-Nginx模块来连接LibModSecurity
7 b, }& w8 }+ V0 h
% J6 L# \+ U. C3 U ^; A5 }# P( b+ w$ X% ~( G6 x/ F
$ cd /root8 G9 z, N3 ^+ `; }# f9 E
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ Q$ `: H. ^& Q$ tar xzvf nginx-1.9.2.tar.gz f9 H2 j6 O4 N1 d* |
$ cd nginx-1.9.2& |( l: l( m+ D: F: u( A) w
$ ./configure --add-module=/root/modsecurity-nginx/ ?; Y9 e: R9 \9 R
$ make9 g& l7 {+ I: s! ?4 R' ] G
$ make && make install
B( E# m* |- n2 ]: b1 J! {添加OWASP规则 @& z+ x- |- O9 y, o
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。$ i; s v& q1 L7 r9 c6 ?0 \
+ Y- S' C* v) B1 P8 ?% V. `- b3 l) A) V( y8 r" w
下载OWASP规则并生成配置文件- x) o9 f8 C; O
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, W2 w- S t" c; Z: u6 a
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
3 y1 E# v" h( e6 P5 b# s$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 p; G" ?5 P5 B" N/ X e! z
$ cp crs-setup.conf.example crs-setup.conf" Y; u1 x- o% n& a; P
配置OWASP规则
7 ]) Q9 p5 R; z* O8 `9 u' ^4 {; u+ ~编辑crs-setup.conf文件
' _7 D& K$ H4 X4 w( Y& `* F" @- b) Y
0 ^% |& `+ Z- J' h* D' I
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf7 S& N6 W% a: m, B+ |
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf9 |% |, F, d1 n
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf' F) `: y* F, [& M( d1 X5 L7 Z% @ Y
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: {' x5 ^/ A( S" G; t- W+ C8 @
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
! `, p) K3 p, I% B# {" ?' |: k6 P P1 s
d" n) X0 a5 Z, H: m$ C
启用ModSecurity模块和CRS规则! N1 L* z% [5 I) w% l' k- X, R
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 G/ I6 Z4 D1 `
j# T% f: T8 e1 k
9 T, v5 J _8 M, q# S0 N9 k2 [/ \9 gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 |7 w; p2 d) d3 M
% T5 y# l5 a, {
2 A W1 b4 b9 K% P$ cd /root/modsecurity-2.9.1/2 ^. f; G( a9 x
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
. f; s! x7 ~! i [2 w, ]" A7 R$ cp unicode.mapping /usr/local/nginx/conf/
7 |& b! N1 q) n- P) Q将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 C8 ~9 l0 m2 ^2 l! k8 H
0 m' ^; z9 h7 B9 u: t
- q9 m: z1 q+ D7 p6 u$ vim /usr/local/nginx/conf/modsecurity.conf
, i7 `8 [+ }/ {1 T0 xSecRuleEngine On
, H9 a% \% U' R9 I- EModSecurity中几个常用配置说明:
6 U+ ]! a' C9 }
2 y( O$ Z0 I% \ J: q5 W: W& W. x4 i8 _. C3 `
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
5 A! s/ t) ~2 ~$ [" b$ F! c' S( Z1 `1 k
5 V o4 z8 }/ u- v5 E
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) c3 }' j; J2 p$ ]1 w
1 W' q! ]5 a( h& h. [/ ?/ U; I" [5 I5 f7 L# W: ?
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 V+ w& ~; r/ p% i0 p
2 \( M7 b, X' A' u( D0 R- X2 }
3 m" n8 l l- X, s) P6 w: ?. b, d0 |4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& I4 j7 F7 \# a2 Y+ ]
, }/ f. p$ W. O1 R" Q5 Z+ u! F( h$ x% x
7 A) ^, V2 y# r, [7 F0 m+ d在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& _5 q# v5 x+ k4 M* {7 ~ L- s6 ]3 |
0 j# A7 Z3 y( Z5 y8 w
4 ] p2 e" Z1 d3 u( @3.x版本CRS
d& B z. K& t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 @# i+ M& C3 ~% [# 生成例外排除请求的配置文件8 Z/ S1 N. b. e ~
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ u4 F7 p) h) S3 h$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
' [: j: T1 T5 v/ S S- J$ cp rules/*.data /usr/local/nginx/conf
1 ~* h) ]$ s: d" q2 y# p) t为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。1 A1 y+ w1 c" r+ [* @- w
; f. c% O3 Y/ J- ^' j" A8 q1 Q3 ^5 t0 C( k* V4 K$ ~
$ vim /usr/local/nginx/conf/modsec_includes.conf) I& Z7 U( C' t
6 ~ K; y% y& O) l0 c
[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
- B2 }8 W' g5 O- |0 v2 x0 z. ]+ s3 j$ ^ `
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 o2 F0 W5 T- C, Y4 |+ E
1 [* u9 s7 b7 S8 h' N
) C9 v$ v- K$ `/ [8 Y
配置Nginx支持Modsecurity
7 N) Q2 {6 g. J$ b5 _启用Modsecurity
" B: z' G7 D3 Z P+ y2 v4 }0 N) S6 R使用静态模块加载的配置方法9 o- O8 y* {, s' I. ~( e
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 p# W5 u) V" g% i+ N; |
7 V* c& e5 F+ w1 w1 y) U' c# c/ H! `
ModSecurityEnabled on;+ _% M) m5 D7 n/ z
ModSecurityConfig modsec_includes.conf;: y r4 I l L" \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 n8 |5 O( @8 F( w1 D9 f# n. i. s# P: \
& e- n7 v+ C, b w. o$ vim /usr/local/nginx/conf/nginx.conf
2 y. }, w- E9 ?; t9 V
, k8 ?; l- B( @- j3 N5 ?
' t8 x ]: n* i- n$ B& c1 `! p% Lserver {
q8 v! o0 G: P/ v. V2 |- y listen 80;
n) @# }- Z9 f, u server_name example.com;
3 H+ c6 c7 [ ` I) O q- y$ l' i- O9 y: i$ T5 s% o
8 ^! L. n% ~$ B( r5 ?' { location / {
3 T( r5 ~- f, c ModSecurityEnabled on;
- g! u* o3 g C2 A) u7 ? ModSecurityConfig modsec_includes.conf; X% ^ e0 Y% L
root html;
$ Y" ?$ s. G' W; U index index.html index.htm;
* W2 h7 K' j: `$ T0 I }4 E0 o7 s# @; e5 d
}
$ U. ?# h/ }# e4 D( X7 Y使用动态模块加载的配置方法
' v: L f& C. V$ w p% i7 `在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 C$ [" }1 @- C! L" `
0 @8 m* J5 |( \ ^) D( A9 N% k8 o! j# s# i" D
modsecurity on;
5 C* i( ?0 h. D& ?6 V& ?! Qmodsecurity_rules_file modsec_includes.conf;3 Q1 c% \. t$ @. F4 \! `
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: _3 Z! b. r# Z/ O# x8 h. w
, y3 I! F0 i( C: G/ i. Z
7 |7 \1 A- H' H$ p$ vim /usr/local/nginx/conf/nginx.conf
0 W/ o) X0 r' g' N
4 B4 m+ l& c. [) x+ _9 {+ s
0 M+ q" K. F. a+ b. K3 F( wserver {
6 ^, M" x& i& C: F$ J( e+ Y listen 80;
6 O! k7 u9 K% U; I server_name localhost mike.hi-linux.com;
* h" C+ X. o; t$ H4 k7 q6 P access_log /var/log/nginx/yourdomain.log;
% M7 f! t/ _/ g, [8 I. X2 y
) Y' m& Z! W, q2 ]' D, P$ u) r! d7 ?, t6 h
location / {* p' Y' J4 R5 K: s
9 Y1 g+ b6 o$ A; E' k# h
* \9 @ F, U" g6 O: W: v
modsecurity on;
3 r& b7 a2 R# ?* @5 ?- U2 }$ X8 b& t modsecurity_rules_file modsec_includes.conf;
r% ^ B( N+ W; j root html;
' L9 }8 t. K, g/ }6 K0 I0 Y k index index.html index.htm;
/ m! V+ k% [! s' f, C}
' D: A+ I9 G4 i: l$ }}
7 L# G: S0 A% A8 W验证Nginx配置文件
$ L% [* N. q2 l: C' o* i$ /usr/local/nginx/sbin/nginx -t
" Y7 C6 X# p0 v( `' Jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok* ?" K: A4 v7 W% l3 ?! h
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 M9 L# J* c/ X+ d+ k1 i
启动Nginx* y7 M9 j' n: M
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf, [/ d4 Z) B A4 p4 ?8 e1 n
4 W4 g4 k8 m; U测试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能够战胜更多复杂/未知/混淆的攻击模式。
5 l% X9 h: Q8 S) F& U |