ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
3 ~, E) ^' Z/ E% M5 Q3 J1 v
9 }: e2 C6 z; H0 |/ ~9 T
4 @" `! T, O0 g9 L( g: H" J) M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; _4 l5 K( d6 o; i2 T% P9 Z' {6 z- b1 w5 l& @; s
8 s" | g" w) P. s( g; S什么是ModSecurity
; V/ e% w& a, B9 l, G ]' XModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。* m# A; h0 {; P& B' j
3 y+ _) I( M+ K/ L" |; p
% {( w" p* @# ^, `6 w1 O
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
/ ^& B( i% {4 R7 P* ]* r9 _4 |9 l; K+ t
. r w- U$ M, W6 M2 |ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
D! G2 X& c3 ^1 n: d4 w( k) D7 W
* s/ Q6 j1 J' w1 G+ X
6 d. C* I8 a" t6 j( r" t官网: https://www.modsecurity.org/5 {! K% H. G) V1 C- C9 H9 V
! r" U' q& H. G# D/ w* o- O
! J h) z- N h9 m
什么是OWASP CRS
) `, h2 d! @; b. \! jOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* v5 P; i+ u9 n M
1 d- w7 T7 h o& L* o/ N! h$ v3 A; U) f8 B8 b
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 r) V. F+ `5 F9 z8 Q
+ ?$ P! Z P' Z8 ^+ z* B
; S6 Z9 X4 H4 s& q0 n+ d' J {HTTP Protection(HTTP防御)
8 l G/ u( I7 a- K9 p' nHTTP协议和本地定义使用的detectsviolations策略。
. x2 C. Z1 k5 w
# \# n( a0 \! u' g3 i! _" |
1 A- r2 P. R5 c# J R' vReal-time Blacklist Lookups(实时黑名单查询)
5 h( O3 [- P) ]" I2 u利用第三方IP名单。' g$ a: B* S6 B- G
" n+ g3 R: b# ?# ~* a
: N. L/ ?8 t! s/ G C2 tHTTP Denial of Service Protections(HTTP的拒绝服务保护)
( j+ _* y% g9 y& Q7 E: ?防御HTTP的洪水攻击和HTTP Dos攻击。* v0 |# L) B# `( I. h
7 F' b2 b4 G- l3 I, ~" m0 K; F1 e" d) v/ g1 L# |
Common Web Attacks Protection(常见的Web攻击防护)
! q+ r- O2 Y" k5 Q检测常见的Web应用程序的安全攻击。
! E9 ~* @9 u" ]" D3 D/ t
" Z8 A0 ~" w- R. ^/ Y) l g8 M; H8 i m5 X
Automation Detection(自动化检测)
6 |- E) t9 H6 C& c; f* k检测机器人,爬虫,扫描仪和其他表面恶意活动。/ d6 @9 `, p/ i! v# F
2 Y" g) Q4 z2 P' @6 f5 t
- c$ J2 ], W) a
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
0 l, z. ]$ g% R检测通过Web应用程序上传的恶意文件。
& N% M& Y; L7 C" j* ]
4 ^! e! }, }# O" ` [$ m% X1 J |/ Z3 f# g' ]- ^9 Q
Tracking Sensitive Data(跟踪敏感数据)
/ Q% b, x/ B* m, p4 ]信用卡通道的使用,并阻止泄漏。
M( ?8 T! z( }. }5 F1 I
0 w$ e$ C# F2 x4 T( L4 L! D3 i1 \6 p4 A( A) i0 C+ |, _( M/ a
Trojan Protection(木马防护)0 N2 Q" y& P6 J1 J# K
检测访问木马。3 }7 L" Z' l- |% b/ G1 _/ Z1 g
+ ?/ T' Y* A5 R8 {$ j
$ _" j) x! t( O7 bIdentification of Application Defects(应用程序缺陷的鉴定)8 P* g1 M* N- g, B- E8 P
检测应用程序的错误配置警报。9 ]% y" G6 C: }1 b% f+ \
" v( b( D4 S- v
: M/ D3 @! L: s8 z+ PError Detection and Hiding(错误检测和隐藏)) y% T; L7 l; G
检测伪装服务器发送错误消息。
, O1 G9 Z4 a9 e6 [+ \/ ] ?, K/ M
8 _8 u) t8 ^" U1 Y. Z
安装ModSecurity
0 a2 G( ?! V E4 t; J软件基础环境准备8 K" Z7 I8 v; k# k: z
下载对应软件包$ w! s2 P! ]) V( J
$ cd /root/ z" Q5 ~; x, v7 q: H: A4 n
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'& `: L" a1 U4 q" k2 R
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz |7 d9 x5 P% r$ q- w" p+ b1 L
安装Nginx和ModSecurity依赖包
; M5 Z6 u* u V0 P4 ]- bCentos/RHEL; }9 _ r8 l% y1 r% X" U* A
0 @4 `' X. D% ^4 L+ H) n3 S$ `! y9 W# O7 S7 g% e, _) N: ^
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& |8 w W) W8 u. w& R# {
Ubuntu/Debian
4 O( p/ H& ]6 m Q( R% F- R( P" |, @4 j H4 x* [
3 k5 l9 A$ ?& W
$ 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
/ I; S2 t$ _6 E6 ^* z编译安装ModSecurity$ Y' Q$ i: k4 i
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
2 R0 s) I- h. m$ V b/ f
6 H( H, j- R$ U2 i! w
7 `0 a7 `9 Z/ R3 X2 [. Q1 Z方法一:编译为Nginx静态模块& A7 W6 _( I @- s2 h2 e
/ n3 V8 I0 h6 @& E5 c* s1 f- i
编译为独立模块(modsecurity-2.9.1)
6 C# V' s7 ^: T* h! M$ tar xzvf modsecurity-2.9.1.tar.gz- l, o& ]2 o- Y5 J7 F2 h7 p; z. u! q
$ cd modsecurity-2.9.1/
5 @6 A9 V6 p+ {' K- v+ z& u$ ./autogen.sh
S" j6 {- A& n3 D3 a) O/ c$ ./configure --enable-standalone-module --disable-mlogc
5 }. J! } ]# S$ make, ~5 \: ]" d9 w. H; G+ K% y
编译安装Nginx并添加ModSecurity模块
) ~" G' b, w6 p% u6 R) m( T$ tar xzvf nginx-1.9.2.tar.gz1 u8 L1 Z' D/ Y# p5 l' c, w
$ cd nginx-1.9.2, ]3 a4 ^$ Y2 c
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
1 I7 M- h! |8 E/ ~) P) B) n$ make && make install
: L- C+ i1 r' @$ G# [# U, y9 B方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- H7 k. a$ }) E/ Q
" P- z: D2 z$ J1 e, u0 D1 Y
; a s& v! o& n! b1 F, ~5 y, X编译LibModSecurity(modsecurity-3.0)
. T; d$ N1 ~' p6 H1 Z$ cd /root5 _' w1 d" {* O! C
$ git clone https://github.com/SpiderLabs/ModSecurity
e4 L+ C5 ~9 Q$ cd ModSecurity& P6 q- X3 r `4 Z% |
$ git checkout -b v3/master origin/v3/master
2 R+ e( H, ^& e& O7 z4 \$ sh build.sh# B) w+ }7 x$ y/ \& a7 |
$ git submodule init
) w6 v' T% r: P z7 ?$ git submodule update/ ?' R7 [; u( O" D/ n
$ ./configure
4 I) c! f$ ?& b1 n8 c$ make# _# O. @- G+ q3 G
$ make install
" k! n, u8 P7 sLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 E% r2 O( R8 o# B# t! n
9 d5 D! Z, i0 ^( R9 h, J' y9 O/ X( Y% x
$ ls /usr/local/modsecurity/lib; N4 Q7 |* o. n% i7 B+ {% Q
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0) Q1 k, z# p2 v$ _5 Z
编译安装Nginx并添加ModSecurity-Nginx Connector模块" h/ r. w. c7 S! K, k1 i! F+ s# ^
使用ModSecurity-Nginx模块来连接LibModSecurity
! l) e8 [) a: B/ n: ^. U+ Y- l+ |8 {( R( H; j7 \
* C! Z9 q* X6 o- n
$ cd /root7 i% X! Z4 J/ c/ {
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, ^1 ^ g8 z5 P0 A& U; T6 \2 E$ tar xzvf nginx-1.9.2.tar.gz
5 Q( v7 F1 X5 y, \4 r$ cd nginx-1.9.2
9 h. r T- `& i* A" P4 u$ ./configure --add-module=/root/modsecurity-nginx! }2 K3 h! g4 V# M2 a, P+ x1 G
$ make: d6 y Y7 _; {: K; M
$ make && make install
4 ?! v3 b/ d1 w. U# p添加OWASP规则3 z: q0 b$ G! P4 {) ^: l" A# f1 d
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
- H2 N; h9 J6 w7 k- t
+ v9 n+ \7 D( l) X6 {# x8 L g' h; O9 m
下载OWASP规则并生成配置文件
0 ^- t3 E( T- h, a1 h% |% |$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. x0 X9 Y {4 J, C: K$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/9 n, c- r- ^: A. s
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* B0 q; |$ h- }5 s3 j$ cp crs-setup.conf.example crs-setup.conf" Y ]1 n; Z* w4 z. H
配置OWASP规则
" a3 R& X5 h( \% c8 K编辑crs-setup.conf文件/ ? ]& S' U: [6 ]
7 _7 A3 ^: x( T( N3 Y
3 H" r R5 E5 d6 u( O# K' I$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf1 O1 W. m# y& q
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& {6 E0 i1 s+ i8 F+ h) r
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! w8 i. P0 } i
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf8 \; \; Q5 h7 E! e" M# r
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。9 R+ B L2 `' x
0 t% D% c* e- I* T1 K4 ]
2 I$ `' k- U/ ?& | h' C启用ModSecurity模块和CRS规则
& A$ i" |# ?# N* B; ^2 ~复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* @1 t3 x/ c* y' e$ Z) d5 }
( D, ], t8 Y! \2 E* U
+ k G5 R7 w4 v# Hmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。4 C4 `5 W, g: U, F1 j( d# {
# o: H" b- [& j$ w) Z7 j/ a/ | F# G. h$ v* @# ~
$ cd /root/modsecurity-2.9.1/6 v7 o7 l" ^% y i7 Q7 _, G* C
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf " ~& R- I4 B# {2 z% Y4 k
$ cp unicode.mapping /usr/local/nginx/conf/
$ _( a) {5 ~, }0 Y: y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# U5 a8 }: `* m
% h! g4 I: N4 Y* X' B) f5 A" x! k" Q' O- m v; \1 y1 j. n1 l5 R, r
$ vim /usr/local/nginx/conf/modsecurity.conf/ |4 t9 l0 B% n, x4 h; e
SecRuleEngine On
2 T5 `' O# l% i1 W: {ModSecurity中几个常用配置说明:# u ~. o: `6 H) F7 R4 m0 a* m
! Z0 M9 S' ^4 K+ H: ]
6 n' @) W+ U4 A$ D) v+ X" s
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ w& U7 X3 J! L9 P
9 B, }$ N. u$ h& x1 L3 t3 l7 q( s& O
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
6 g& d9 y9 E& J4 A1 K6 S, _& b4 H# W1 H, a. A% T
+ F, }: C4 ^* ?8 I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。& M# c) h4 R8 t, C2 C5 q p: G
- S9 y* `1 @# _7 C x3 u
& D0 T! z5 ], j& ?7 j4 ]4 |4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
: l0 G' \$ r2 ~! E7 `+ B+ t- E1 U
- q* P: d u5 x& l) i4 }# y1 f6 r9 U+ k2 T6 I0 ^+ }
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! b0 C5 r& i7 q3 T) n
( ]7 l& M; T) |: u3 G: h
9 @: D( e1 { ]
3.x版本CRS8 v3 R' D2 L' Y: `/ G' O8 W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, Z' Z: V0 Y; q" j0 ?. H# 生成例外排除请求的配置文件
: M9 n9 e2 w* A6 v4 B* g. \$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- Z" b9 S f/ @$ L( G$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf) _& ^1 o5 r# r* M8 T4 [
$ cp rules/*.data /usr/local/nginx/conf
& H* a- `5 _6 p: x) ] u7 z1 i为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
. o( D! H- C; ^" n$ d7 W* A' Y
0 {( [/ j) ~' [- ^6 k" j& _6 U; P. U
3 d4 w3 W! |- w7 O$ vim /usr/local/nginx/conf/modsec_includes.conf( A3 N, S) |! e3 P, U9 J
- n# |& I+ @0 E6 R
[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 ( F+ B) q& r6 S9 q0 ]" _# q4 @
! a3 b$ J. d; h" O+ Y( t0 J3 H
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。( D: Y) S! y9 W9 x: z3 W8 S
5 U5 C9 _$ C- p5 j. f
6 N) Q! O- K" e5 _7 a* S
配置Nginx支持Modsecurity ?" `5 l8 o0 Z0 r& S4 y0 y+ l% M
启用Modsecurity7 _& L, W* ?9 ?( o, g+ Z3 X
使用静态模块加载的配置方法- U' B0 b: p: a( Y% c/ I9 {
在需要启用Modsecurity的主机的location下面加入下面两行即可:- p, W" i/ J9 `+ j! c
$ a7 I7 o! g/ c+ g, o5 n4 k4 A2 l. p6 L8 H. D* \) \
ModSecurityEnabled on;5 T/ P1 ~, C# L; U0 [' D
ModSecurityConfig modsec_includes.conf;
+ K. s" b+ p+ \5 i( L* w" ]3 d: `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- P4 n1 G/ j# ^1 u5 @- h
5 D1 r1 E# |, ]
9 \0 z k1 G7 r: f h$ vim /usr/local/nginx/conf/nginx.conf, A6 d& S! |( _8 e# b7 y' X
; W+ l, u& [4 c' D: m5 c% c4 |6 j$ e% j+ ]
server {
; P9 C- \: d: T ?" l$ z1 s" j listen 80;6 ~) o( a* Z5 O" q5 B0 V
server_name example.com;
6 f/ g0 r9 i6 ^: O. b
3 ]4 G( A" O# }7 Z6 S) X. A' z% i7 h$ \! G
location / {8 n; p2 ~, a5 F5 o, }6 G
ModSecurityEnabled on;; ]4 s# Y( h' V8 w! X: J3 ]
ModSecurityConfig modsec_includes.conf;! `1 A$ ^' A3 D/ s; H, s& o! q
root html;
- t; g K, W! {1 d+ |* a: V+ R index index.html index.htm;
* _# c, j' o F" R: Z }
6 \9 V- N( b& [$ A}
9 b* M7 h( p `3 m# Y N; E: V! s使用动态模块加载的配置方法% J( T' e, p9 j9 K0 P
在需要启用Modsecurity的主机的location下面加入下面两行即可: \5 ~" y$ b* i
% m( O+ K) u4 Q7 n. S
3 l! e2 v' ^4 @# hmodsecurity on;
- d% T/ b( l1 V5 ]modsecurity_rules_file modsec_includes.conf;& w# A% A. b& H. Z7 F) ?
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& F" M$ X% N; R! [4 ^, e! K
( U' v( v: e- L& R2 V* o0 s
7 W. a# u- k9 c: |& h( S+ p R$ vim /usr/local/nginx/conf/nginx.conf `* k! k; d, j/ H
# p2 @3 m' w6 P" l3 V% q
3 s. n: [$ S8 Z
server {
$ K! R% k6 x+ C8 [9 U listen 80;
" x' s% j- H9 ?9 p server_name localhost mike.hi-linux.com;. Y' L( b! l4 \) ~
access_log /var/log/nginx/yourdomain.log;
" C5 n- X; O* ]: \$ m: K
/ X. Z* w; y* U
1 D9 I, T- u1 ^7 }) j2 B( g: h location / {0 V8 R) N, R3 s5 p0 _2 J7 b% R3 H
( G$ F% j- F) A/ d& j
% h2 s) t! |, v) s modsecurity on;$ M: D$ Q" c& }& a
modsecurity_rules_file modsec_includes.conf;
1 g' p9 g6 W* N+ A root html;
" K* ]2 F- y" }1 R- ~ index index.html index.htm;. j. m1 w8 o# T) W' Z+ n
}: I( V' o4 ?; ~$ N/ F$ r2 O
}
6 C$ k! o$ i# s验证Nginx配置文件
* T- C2 X$ Y$ P) |3 Y& Y; s8 b$ /usr/local/nginx/sbin/nginx -t( h* g: X2 l$ H' Y) } f' V5 X
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
, V5 ]6 }' R" unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3 N' Y- R) D" E2 W( f! c启动Nginx
+ Z1 Y, Q2 B2 O$ B9 t9 p H$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 a7 |& t! `2 {. _* _: t0 Q8 b( k# S6 C( s$ w- D7 i: x5 \8 L
测试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能够战胜更多复杂/未知/混淆的攻击模式。 % m0 H( t& [$ x" x2 k/ G! W
|