ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. F0 R; q4 I" x1 P
) ~4 |7 k* S3 n1 l4 P+ D5 K; k
: N. V3 A0 V6 q5 F8 r& O在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。% |* ^* Y( z3 V7 W3 z; X5 O0 O
" E& n# t# U) L% D K% V6 o; {; i1 t* x
什么是ModSecurity
' }+ o; t5 e U) V( sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
# n# A0 v1 k, x
0 S1 j' A7 V, @" ]7 Y5 C7 O, ]+ m4 K+ ~8 Q3 v0 ]/ @) Q1 y1 Y
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。* B# {7 A8 M6 i
, G) V2 x: M4 l
5 y& j1 R( j2 v" S/ x1 q4 q5 C
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。9 \0 E3 ]5 K. e2 ^2 L4 o
' h4 n$ S$ k1 f7 M0 b6 N: n5 w! e ^; J8 V3 Y) f8 k$ l
官网: https://www.modsecurity.org/
7 W' t m" k6 M9 h- x- y
/ z: K/ W2 E/ l8 h- y( C
" |2 C: [( ? d0 f0 I什么是OWASP CRS
9 r8 G. z6 E3 X7 [/ p5 a; COWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。5 J3 y i/ {/ {6 c5 [
3 r' m5 H* P4 G t) l3 B: u! D* u0 f _4 s! { h
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
; Z! f; d! I7 K4 f& J
& R2 L( J7 `3 v* p+ s9 t$ g7 T, ]" b' e6 S
HTTP Protection(HTTP防御)# w! z$ L! I4 b& a1 v4 m
HTTP协议和本地定义使用的detectsviolations策略。
5 J7 b$ r- M* z. w$ v+ Z" c- U5 y% R* V
6 f+ C" @* R/ @& |! i# D0 YReal-time Blacklist Lookups(实时黑名单查询)
- ^4 M1 p' q6 J O2 J利用第三方IP名单。
/ y$ r) [- y) Y7 g; z2 v6 m8 U) h$ z' a) Z2 R
/ S$ O2 N) v" l6 Z* o- B, xHTTP Denial of Service Protections(HTTP的拒绝服务保护)
. h3 d( }/ ^9 r防御HTTP的洪水攻击和HTTP Dos攻击。) J g/ P- Q* L5 V8 h6 b3 v
. e" p" d6 c5 }
" D) U& N: R* O
Common Web Attacks Protection(常见的Web攻击防护)* t5 ~& I; C, _) e4 T6 {, P
检测常见的Web应用程序的安全攻击。$ N! ^5 p7 B6 j, T1 j/ F/ M4 U+ b
$ m% _2 [# a: `1 j* F$ O+ I9 W
" {. ^* d( m' Y: E5 q9 d" ~! m( L- cAutomation Detection(自动化检测)
( q# ]! S9 h$ j' N8 G+ ^5 ?9 X检测机器人,爬虫,扫描仪和其他表面恶意活动。
% g6 D- L b q( F2 @; I& ~- r7 U& a: e
1 |( s8 a0 Y7 Q0 t2 D/ S9 WIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
) |# n# L6 J' c) h8 I0 B检测通过Web应用程序上传的恶意文件。
L+ @3 X7 T N2 _
9 R* i9 X, l4 [9 ~8 B7 c6 W' A% V; d/ V& P4 y6 L1 u0 e- m
Tracking Sensitive Data(跟踪敏感数据)
! o, C4 K- C- H) C信用卡通道的使用,并阻止泄漏。
: q5 @- L9 Q) K( \; G6 G. \; C# O# ], i* D g3 |
7 D& I0 O! M% S) l, D$ Q' zTrojan Protection(木马防护)& F, j+ u3 i, k
检测访问木马。
9 n9 I& v5 c6 {; c1 k' H. D
# v3 o" z! d L1 v9 `0 i M8 |3 t e, S# U7 j" _
Identification of Application Defects(应用程序缺陷的鉴定)/ X# ?* v. T7 s
检测应用程序的错误配置警报。8 W8 R6 M* S) p, C* _# P
2 f E9 W& `! _* f( ?# _2 v6 Z* }, B$ g% @1 c' e: B2 D4 ?
Error Detection and Hiding(错误检测和隐藏)
7 b0 }7 O% ^6 h, \- {检测伪装服务器发送错误消息。
, E9 a$ j- o0 l5 x
/ ~# j5 k) F n8 { f1 j# \
0 C' m. | Q" M4 r* R) W i安装ModSecurity
& Q) ~* U: Z+ I) m软件基础环境准备/ ` ^9 Q1 P+ t& _9 \; O J
下载对应软件包
& s' n8 U4 w3 I' v; @6 X9 I$ cd /root' D+ u. q- ?6 I
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
1 Z" i- A. d" c& Q# O% R- L% O$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
' b% V$ i# m8 q& q Z9 [! T3 \* X安装Nginx和ModSecurity依赖包
1 E' g0 ~: t4 T7 N, S9 L9 L9 Y' XCentos/RHEL
\1 x1 l" ^$ S f' x# v( X- ^0 d. @1 u8 i
" z5 I5 o$ b0 N+ k$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
/ h$ S3 V4 w: e4 {& I9 Z! G# BUbuntu/Debian7 `$ k* F" C4 W
5 T1 d& ~% Q, F/ n" f$ v
& o# f* U1 _ O X& a( V5 R/ D* d$ 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++-dev6 ?/ F& w4 g9 y) [7 d- s" G
编译安装ModSecurity
4 G! D6 S2 h/ K0 M2 o0 hNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 M J' h/ ~2 ?, a d
0 c$ Z# a0 }: N. m# X/ ]; w% u, H) a7 t" L
方法一:编译为Nginx静态模块+ |: H, x: w- f( Z q
5 `1 R. l4 e, f( L
4 V+ M0 B, X; Q编译为独立模块(modsecurity-2.9.1)
" V( x9 x2 h( Z% _8 S1 o7 d$ tar xzvf modsecurity-2.9.1.tar.gz
3 B3 |7 e" l) ]% L; y$ cd modsecurity-2.9.1/
' m6 X5 K+ u9 T" G1 o$ ./autogen.sh
' L, a: y! a8 F$ ./configure --enable-standalone-module --disable-mlogc
, T1 g0 p) I" o& F$ make
1 o! d/ D& X# r2 Q C% G9 i4 w编译安装Nginx并添加ModSecurity模块
& a" m: Q6 r" U# x/ K$ tar xzvf nginx-1.9.2.tar.gz
& `' N& |2 i, M, j% \# U( b, r$ cd nginx-1.9.2, Z# d! j* l) u
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! g, g- [4 O/ }/ G) m$ make && make install% w" d$ P5 G+ G2 I( J
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
# x6 K9 [# w* b4 v3 ]) }& l4 w% C% D( ]9 h: t. i
2 F1 {- \4 x+ S* ]+ H# x
编译LibModSecurity(modsecurity-3.0)- F7 s" O4 k; ~' E3 F0 |" x* G3 N4 i
$ cd /root
3 N5 Z& |, ?/ f3 b$ git clone https://github.com/SpiderLabs/ModSecurity7 i* E( u* p) l. X8 C
$ cd ModSecurity/ z5 l4 E8 X& I3 a4 w' O
$ git checkout -b v3/master origin/v3/master
8 G0 v* a2 m3 H' _$ sh build.sh- S2 r& ~& B5 S l
$ git submodule init
3 L3 C+ q- Z! g. S4 @6 h2 i$ git submodule update
$ \2 X) Y+ Q& e; i3 w$ ./configure
$ X& b# |! D) M4 \. r$ make1 @+ |9 l. @, ~; C' S
$ make install# |& I- S# y& z. k5 F- T
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。# V A- _. K, l
' O! o% {2 u- C+ \; `! P* ~, L; Q) k2 [6 e
$ ls /usr/local/modsecurity/lib! M# Y# c3 V- B% \0 o j( `8 o
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.06 z: A% p9 l( p- C, F1 e/ }. n
编译安装Nginx并添加ModSecurity-Nginx Connector模块
" D5 {3 d b$ N( ~% |使用ModSecurity-Nginx模块来连接LibModSecurity
( U' P& W2 X8 ~9 Z5 h
0 R; o* W/ L, t8 [" \; }/ m* e& `/ R2 c* \
$ cd /root
+ W6 _6 X8 Y, k; O" O3 I( B% A$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* F* ]* H8 C" ~$ tar xzvf nginx-1.9.2.tar.gz$ a$ I* f/ E0 ^% K
$ cd nginx-1.9.2$ \8 m: L" M) i% O, M* f
$ ./configure --add-module=/root/modsecurity-nginx, R( u1 T3 X6 k* x. U
$ make* l9 ^# q5 D- R! B' P* ?
$ make && make install# A! \3 l6 e9 p$ {! C
添加OWASP规则
' i& w, y1 E& D3 @0 Z$ h- C# `4 I% \ FModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。6 k, T9 V6 { _- }& j' \
7 Z4 u7 q [ |8 p( D2 |8 M {4 G
2 R/ |7 G6 b1 k下载OWASP规则并生成配置文件0 U. J/ v: i: ?* X0 Y! i) o% f
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# N! d3 N. F; n8 K9 E( N$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/9 R2 x! N6 m; h( A
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 a% a J8 b3 C4 N+ s
$ cp crs-setup.conf.example crs-setup.conf& d) n/ C I) f. _, P# D
配置OWASP规则- J6 J( l0 C( x
编辑crs-setup.conf文件
7 T" \, ]* L4 R- D3 \: o
! f) J/ k5 O2 b/ O
8 u8 y% K$ |. Z. J3 J" e: `$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf7 L+ z4 G' A& W9 T( [4 q, m1 r ]# M
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf g; o' j+ P- |- u" J+ }+ [* ?6 n
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 f4 b3 o9 _( b7 _( C$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# M9 V9 b8 b& m) t0 o x7 I3 ^默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* j2 Z4 U8 `6 A2 E% O0 [% [7 ^
: T4 H6 n" e# V1 H& g9 J# l1 T$ }, K9 h& b6 o( X5 Q1 P
启用ModSecurity模块和CRS规则
+ a& L7 B3 a* Z复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 n5 `! Q; w9 L
% Y, X% g& t! o/ r [5 Y" \! h: e, f+ C1 E) P! i. K/ Y1 s p* D( A
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
3 {9 ?! k5 F* F. }
# B. O# ?9 K, ]+ p1 r9 w) Z$ T5 I- z6 p S8 g
$ cd /root/modsecurity-2.9.1/, `9 d; c1 ?. X6 T* i1 U- V5 ?
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf * R1 l/ L) B A# J z8 q0 r5 S+ L5 m( \
$ cp unicode.mapping /usr/local/nginx/conf/
; T, Z0 q9 r2 m8 y8 |将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 f) ?. w. v4 y7 C) f/ l
) @7 q: b- s6 {) U/ v: @4 S6 Z/ [' D8 S) T
$ vim /usr/local/nginx/conf/modsecurity.conf$ ?8 M Y/ q+ ?
SecRuleEngine On
$ e, e( v7 H& A/ \* Z5 t* pModSecurity中几个常用配置说明:* `7 _. l( h" K
$ w \& E& U/ C7 z( O( E3 O9 u2 C9 z6 [# o
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
$ \$ P3 \$ K; ~8 @7 k# g8 u" v/ j8 ]" D M1 w3 w* b$ U x5 X
" P0 F }9 y. d4 ?9 @. Q
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。6 H% }6 ~. H! K( F$ p
( o/ A, [+ s+ r
$ a$ @$ x. h$ B. c3 _
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
" m( V7 }4 }+ z8 {7 X$ U* ^5 y: u1 d4 R9 t* U; b0 U8 t
1 y$ J; J- |- v5 R
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。, X1 @) I+ y: Z8 i; r$ K0 r
' k1 ?: H* ?8 |3 e0 {
, g- t; A5 e- R
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 Z+ K2 G/ y2 h; T; A/ T; e. v7 x3 t& j7 ^# @
4 J: E. P* L* x. m3 A3.x版本CRS; m( A$ O( A& E* }! X( n
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& ]9 c3 t: N. D5 b3 l; b" \# 生成例外排除请求的配置文件
( Y3 H+ Z: ]7 R& |$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf, e" L9 U1 [0 @" |
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
}7 D3 P9 T% K2 H+ o) ]$ cp rules/*.data /usr/local/nginx/conf
% B' L. X+ S0 D- K* e* u0 T! }为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. T- _/ t- M% A
_2 `/ e% G, G
+ l% z' O7 a# y* v
$ vim /usr/local/nginx/conf/modsec_includes.conf, x) r& u6 P- Y5 J0 z- N0 b$ l
: [# `/ ~- v& \7 r% V @
[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
: a4 p. ? D! i4 @2 {, m, u, \# @* X! n2 [
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。+ [& M9 L3 W7 g$ n& {6 ~3 C
( {8 x' H# ?* y }. A. d
; t- p' H& o( k% E& v: G配置Nginx支持Modsecurity
' d$ O! F* M: h4 E8 r t+ _启用Modsecurity( x: H6 `; S2 v& L7 s3 P) @ L7 v
使用静态模块加载的配置方法9 F( I- N! V- [* x% R
在需要启用Modsecurity的主机的location下面加入下面两行即可: s& m. `3 Y G r
( s% h6 q5 k. ^/ R$ b1 r/ B0 f1 T9 c/ h) w" `! ^; B& C
ModSecurityEnabled on;. j; {! x( n/ F) d3 m! c" W- v8 r
ModSecurityConfig modsec_includes.conf;
/ m! O/ Z3 l) H) i: I; ]4 g: X8 c. I修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) P8 G, G1 o& A* s
$ }5 v& `( a9 A( o- \; u( D
h3 f3 K. r; k9 J2 H9 ~$ vim /usr/local/nginx/conf/nginx.conf
. ^* s$ V% \/ F' s
7 x; _* e$ X7 N/ h- T4 l# S9 b# {6 s5 e3 \ X
server {) Y% L; T# \+ k
listen 80;
5 {5 F2 O; k5 f8 n! F3 i. w server_name example.com;- U, O: s5 H% A! ]! B
, k6 n0 m6 Z4 R3 X
; J1 p: S" p- K' J
location / {9 T& t+ ?7 ~& P4 R* F
ModSecurityEnabled on;2 i: ^$ d( b! L' F, A0 m$ q5 Q
ModSecurityConfig modsec_includes.conf;
7 I8 F4 Y! L& J4 O root html;
4 Y! e; \# B% ^/ z) D' E5 u index index.html index.htm;+ g7 A3 H- x; Y8 M
}7 q* I) X9 `; {. ~' N
}
% ^; m0 l& r: _使用动态模块加载的配置方法" s$ S2 \& d' t1 u2 f
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# \3 ?/ _' w- o9 |. F( D& X* r( a- z; C8 [. Q/ n5 F
5 \7 A* {6 {3 `; o. Z# t4 Xmodsecurity on;9 f1 q8 `) ~0 X8 ^+ @6 m: j
modsecurity_rules_file modsec_includes.conf;
1 }% o( R( `2 M/ G0 `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: s' s) ]5 k" t$ e0 T! s1 h: Y+ V/ ~" C# ~/ @% U; d! P
7 ^6 Z. @; o7 {$ u6 c$ vim /usr/local/nginx/conf/nginx.conf5 {7 A s& S7 W+ r1 [
" c8 V! A: R3 G/ A5 H% k, m
]% x+ ~. O+ Q
server {4 z* l, R, ~! u9 g3 v+ G
listen 80;) Q) j; \4 A) H$ a& e$ c$ F
server_name localhost mike.hi-linux.com;5 ] j& B7 S) N# o' D4 k- k! i
access_log /var/log/nginx/yourdomain.log;
# W8 k2 _0 t1 ?* U7 d! o; c3 D/ D, g, Y; i6 _$ u
# ?' \0 l7 j1 d0 o `4 f5 _ location / {
4 O) O! l* o6 M
" ?& I6 l' I7 q) {$ y/ J7 ? }: [6 }8 m0 @7 G' V. T+ d1 R9 y
modsecurity on;
+ g" _: o9 @* F modsecurity_rules_file modsec_includes.conf;$ E$ B5 j/ e2 _: ^7 [
root html;. v7 q0 ~# U: d, ]0 h
index index.html index.htm;
# Q; {+ @8 \ {: T3 P' a: q}
2 ]0 N2 S6 Y* M! B5 ~4 U7 h: k}& `# E5 a( J7 Q0 f
验证Nginx配置文件
/ Q% H, V8 x; p: _6 Q7 p$ /usr/local/nginx/sbin/nginx -t
- f. P- i; {( j- w: |1 _. Gnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
; `# \* E4 ^5 Z enginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ v: x1 a3 C2 k7 S6 {) N) a" i启动Nginx
* y4 W6 E6 i1 E) H# P+ ~* J. N! f/ _$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
' Z# t3 ]7 z; Z" M6 K7 P7 M) w+ b
测试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能够战胜更多复杂/未知/混淆的攻击模式。 9 k2 F5 P: Z3 J) @# t
|