ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。) F4 I# s& s* O" O
% C g1 b9 Y( f; {) ~5 w4 ~2 N
* o2 v! Q( g6 h8 [在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。1 w e6 s, }, M9 b1 H m& T
, D& T6 _" ^* k6 o. l
) ~3 `5 F% r6 j5 W8 _, ~什么是ModSecurity Q4 d; W9 f1 }. e9 ^' r
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
5 o+ Z5 t3 x0 k. Q% r
2 ?* `( S8 E' G. {4 t: p ?
! d& i V s) W% O/ c, hModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
* r( @3 `( Y$ n. Q6 a$ g' g! f8 ~
3 T+ B+ Q7 O3 X7 ?2 Q' a& J6 C7 I
- s3 @; I1 c" X* r! M. NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。/ G& k/ c/ H! ?' L6 F7 q& w. c
2 z) y5 [! U: s. E+ K8 q1 s
J E% m W p: q
官网: https://www.modsecurity.org/
# O/ e9 D) B, e! ]) V! N x: a2 |; p7 ?; G) m' p7 Z+ [
: e4 @+ G8 X' s, F- l/ K' ]什么是OWASP CRS/ a l* {- v% r+ k8 u% r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
p* d" V! {# |2 H0 r7 Y+ _ H6 l
9 P3 \; ~1 Y3 l' X- n/ m' J$ T' u7 {7 \9 M! ]
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 ~. l/ y8 f/ N# |2 w
+ i+ o. C8 t/ K3 ~0 d
, |" u% W4 C3 V0 i' |' e3 E$ zHTTP Protection(HTTP防御)* c/ Q' `: t3 R
HTTP协议和本地定义使用的detectsviolations策略。+ }7 O5 M. p, o# m3 ~& [7 g2 B2 p, [
# e( d5 x4 V; p: c9 f% c2 y# ~, z. C9 B, L _
Real-time Blacklist Lookups(实时黑名单查询)0 [: h4 X: _% v' t: s4 U
利用第三方IP名单。
! p! y' |2 {) [: F0 w6 o3 P0 d/ ]4 h# k/ m3 |6 X4 j$ K
- c( u5 D2 A$ H: U, L ZHTTP Denial of Service Protections(HTTP的拒绝服务保护)8 a& m; G X8 V7 k2 a p2 `
防御HTTP的洪水攻击和HTTP Dos攻击。
9 \( K x1 r7 \+ s. O
0 G: `7 O) x& Y# _' \) S0 u* j4 i& Y& p: o2 b L. q: _) n
Common Web Attacks Protection(常见的Web攻击防护)! Z' t5 _ G" y( I" f0 w
检测常见的Web应用程序的安全攻击。& d# K' r! U+ [/ K& v2 H: p
) D3 L. C" S& h. c% b
3 c' g# ~9 Z" K! R" H7 q1 i1 v. K3 OAutomation Detection(自动化检测)
6 V7 h; t, f/ a4 R% v M, N检测机器人,爬虫,扫描仪和其他表面恶意活动。5 } y' Q1 S5 N" d/ y2 F
1 [9 c* e8 _& Q6 _1 C {. e
& m( P9 y& d3 |+ g% [
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% u1 z( b) U$ d3 C检测通过Web应用程序上传的恶意文件。0 G7 U+ ^7 ~% k5 `
g. D. K: f& J! ~6 o
4 d& [! j% d% |" S# z. [$ c
Tracking Sensitive Data(跟踪敏感数据)! [) I, W0 t- }
信用卡通道的使用,并阻止泄漏。
5 \6 T9 b7 l! \3 R: c/ Q& }$ s! j4 z% C: B9 v
. y- x1 }6 J0 q) ZTrojan Protection(木马防护)
9 N7 ]* K6 p/ t X0 h5 W% J检测访问木马。* M! L3 E* e/ z8 t& `
5 K( ?) z( [/ _9 o
. _2 Y) b% |, e" M6 p' O$ l3 D9 C+ Y) uIdentification of Application Defects(应用程序缺陷的鉴定)
" s+ F: T0 A; w3 w/ a( j检测应用程序的错误配置警报。
$ r: Z& q5 z8 e) k2 ~1 `3 n9 p/ a0 _* B% c1 ?
5 t9 c. T: Q, W! m& A# |7 d' a4 E4 `
Error Detection and Hiding(错误检测和隐藏)
4 K& G4 @8 Y1 n检测伪装服务器发送错误消息。! e. B" o: p9 \% v+ A" n, t p' z
: x+ _2 L5 x4 w& O/ r7 d1 t$ w; O$ d8 a* g; [
安装ModSecurity# I; r2 O* b" {* k# t; A
软件基础环境准备
# G) b- i x# b! ]8 q! P下载对应软件包
. Z: W+ ?1 j4 R# c7 Z$ cd /root
0 i8 U& z) S' @' j5 ?$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 l0 f' E, i( U
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
& Y7 D1 Q Z2 [1 Z4 O* J安装Nginx和ModSecurity依赖包
9 c6 i9 Q& {5 v4 z, A: l' l$ oCentos/RHEL+ e, V- N- F! k* J6 {5 }1 e2 \ }
+ o5 i: _8 u* A
+ B* G+ H, Z$ S6 i4 @$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
$ Z) U, v- M1 j* W4 l; O: @+ i) tUbuntu/Debian" B8 o( j* x( t9 v, x# z/ B
/ ~1 w5 J$ [, K' w. Y3 x
9 L+ s, C7 ?- D# E# Z7 M+ 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
* x/ d* E9 g5 l5 r: s编译安装ModSecurity/ {+ b a4 q% i1 T0 `
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. L" d. b/ o8 S6 M
' b) E- V$ [4 a2 q4 Z+ s# ^' n* q, Q6 N f0 s6 h: d
方法一:编译为Nginx静态模块8 P6 T9 T( G8 P
9 m9 f7 A' V" P, g0 z9 ^& k8 K5 j0 c8 i" C3 }
编译为独立模块(modsecurity-2.9.1)
8 j) z, ]- ?" o/ J9 t$ tar xzvf modsecurity-2.9.1.tar.gz& }" K0 n& C! y7 B6 P1 u
$ cd modsecurity-2.9.1/+ E6 R& F. ~( Q1 L9 T& H
$ ./autogen.sh
: j& ^: `% C1 F, V4 I6 a$ ./configure --enable-standalone-module --disable-mlogc# E" C6 j* P! V2 ^3 M
$ make. A8 y4 i( {: e' l+ {3 n7 L
编译安装Nginx并添加ModSecurity模块
$ K3 s. A; w/ w5 K$ tar xzvf nginx-1.9.2.tar.gz! `' o" L4 ?( D& T$ ?( A# Q" Q0 c
$ cd nginx-1.9.2
: \1 N5 q# u. G5 Z: t$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/% m+ M( ^5 ^) e! m
$ make && make install
6 S0 p/ ~# [1 v1 ?+ q2 B" p$ P. S: {方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, s6 `( e& X. K% C4 w" h' n9 U% ]
8 Q) ^! @/ R5 u# p
( l3 Z. L( D$ `& i9 ?4 q编译LibModSecurity(modsecurity-3.0)
% ?$ a: @% ?% D8 W4 ^! N$ cd /root
& Z0 u# ^7 P/ J9 R, p6 q O u$ git clone https://github.com/SpiderLabs/ModSecurity
0 k; j' ?! K8 P" q- e$ cd ModSecurity$ \7 J+ a. @9 b4 @1 U
$ git checkout -b v3/master origin/v3/master( N1 R1 |1 Q' G2 J
$ sh build.sh
* f1 n% s+ b' V5 Z( v D: l$ git submodule init6 b1 V+ i+ `2 ]0 c& c2 Y6 A
$ git submodule update# B7 K5 I' {4 }3 v$ z. b0 }1 A
$ ./configure8 \/ V' K3 N( j M& z+ i6 k
$ make1 J2 J! Z9 r$ d) \. }
$ make install
* D, |$ C3 N' z* `7 w! qLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& I& _ ~/ U/ W. d/ a
1 H- t( U% Z6 S" A7 A' ]- U" [7 x$ ~$ D* }- \/ T
$ ls /usr/local/modsecurity/lib
8 ]4 j9 d! x4 v, X- I# S3 xlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0. x# x" q( [- h0 C; K0 T# H
编译安装Nginx并添加ModSecurity-Nginx Connector模块- l# T% L3 ?5 ^* p' K5 @
使用ModSecurity-Nginx模块来连接LibModSecurity
8 u4 J/ I' a, l* w) ^* G6 i) L2 y$ o N7 J: B" W. L
5 p- }* l7 X+ ^6 y2 F9 g. `$ cd /root
. O0 x; X4 c. u) w( P7 f$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx3 g6 z: W: W' z$ H! T
$ tar xzvf nginx-1.9.2.tar.gz
, l' P# Q t) `$ j. M2 M$ cd nginx-1.9.2
7 L& a) ?5 C" j$ A$ ./configure --add-module=/root/modsecurity-nginx/ W6 J1 P" C m8 |- N' h) h
$ make
( H3 r9 Q6 D% {2 B% [$ make && make install' _5 T9 F; ^5 M. ]3 a" Z
添加OWASP规则2 Q4 ]( c2 |( [# [# k. k0 i, M0 b! ]
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
1 E2 F) H& A. |( x- f/ M
1 W0 q: d; r# R
# b6 ~$ U% }& [$ h6 B下载OWASP规则并生成配置文件
I& T* K, G5 B$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 d: m7 X p- [
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/( ?7 u/ R1 ]/ u" [' }; T
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# W/ z4 S. J, q1 ]: I% f$ cp crs-setup.conf.example crs-setup.conf) O9 C% y C7 M
配置OWASP规则
1 {: I+ `; n+ ^' I/ k编辑crs-setup.conf文件! W' N a$ y1 F% _5 ~; K
# b# C8 L" V# t `
, O. Z$ }6 O) `, Q, j: d' A& Y* X% r( x
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf; A5 a* `' d9 h$ H" r% u) l. p9 ]
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
& L/ X$ R; b3 ?( o$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 @2 ^) q4 b! x; a6 ^) G X
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
2 _" D7 Y7 W$ d/ w# ^5 w, t默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- n7 l* A' p4 I2 s3 e3 @5 I
* M& Y! _8 }# q. }5 J% i
# ]- M5 \$ {: ?9 z4 ^启用ModSecurity模块和CRS规则/ W9 r7 y5 t6 ^1 M" B @
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 e2 c# e5 f( F$ w
/ P) e. B5 j: w% B$ q9 U a# V: r1 q3 D7 a5 W4 `$ J! i
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。: ?; f* V% l; Y1 W0 R
9 b$ u/ h& ^" p# }4 `1 u) A0 B. ]" ~5 ]9 c9 a, ]5 P; x6 s( r
$ cd /root/modsecurity-2.9.1/
0 ^3 W) L. _4 R9 h0 l% s' Q$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 6 M! G9 L& F7 t& y1 p( N A, f4 l
$ cp unicode.mapping /usr/local/nginx/conf/
6 V8 f( }, q" B: d P( I+ D将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
4 M$ Y: o0 D# W# `# f) U `* D' y2 [9 s5 s7 L
" q+ ~8 Y$ h" N
$ vim /usr/local/nginx/conf/modsecurity.conf" ^" O) T; f1 q9 S9 U. L
SecRuleEngine On
8 W8 Y. D5 d9 [8 N- X; ~# g. IModSecurity中几个常用配置说明:9 X4 k/ ^) u$ d. K
: i+ i$ S# u$ X [0 [
' p7 e9 e. N, n U* c4 ^3 P+ O1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 p) ?+ B `1 i+ J$ c! P
# g/ j; ^) c) d$ e, k& d
+ l/ l! p" S& K. h9 q2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
1 `* l6 F, O! B( N8 f6 o$ O: c. q% q3 W# V" J1 b7 h
0 v: o7 g8 r6 s5 m5 {6 i" v3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。. b+ ], j. S# g
4 s) O Q, g: M6 K% W p1 A3 r
9 x" P: ` c: `' X4 F4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! Z2 b- T& U) x4 ^9 X
: E; `1 Q, ?: l1 n6 w; G6 q
1 M. V3 m; `; u7 C
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。3 p( O" Q% o# e3 u) r6 x
$ F4 W. O2 u% v: Q, f
' v! m% n) l8 x5 L" \5 x3.x版本CRS
% P& e, u" O6 G& G+ f# y" a$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# o$ V7 }. n) {) A2 y C# 生成例外排除请求的配置文件/ n X5 T |: F9 ?8 g
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf1 b! R5 I( Y; ^7 Y7 D1 m
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
+ K$ l! R8 p; L) F) @! W; n$ cp rules/*.data /usr/local/nginx/conf; `' b8 p: x( m: b. a
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
8 D y) B4 D1 l! \6 Q( u% A1 x) L! T
# G7 ^; f4 [6 f( R+ B
$ vim /usr/local/nginx/conf/modsec_includes.conf I- r6 e4 D$ f5 l- F" e" l1 S$ c
6 w. t3 ?( P2 t[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 1 _9 X, m+ p1 D8 _) J+ A0 N% ?
# M2 r/ [( F0 x% S注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; P5 ]( V+ [( G3 O0 e
3 ^# _$ G; w, c4 A6 {7 g L# v- g6 Q. O/ X) P, i
配置Nginx支持Modsecurity
9 I* H+ N! d o' F1 ]1 B; K启用Modsecurity
5 q& H$ M6 i# H* G2 V; `使用静态模块加载的配置方法
0 X7 {; V; A$ e" k7 l' n在需要启用Modsecurity的主机的location下面加入下面两行即可:1 N# y3 z* F. i! ~7 D, i
" P1 g3 } O2 g: A. {. y' D+ V
2 ^( U/ x+ E5 r! E9 nModSecurityEnabled on;
# T. T! I! B/ C3 u' {ModSecurityConfig modsec_includes.conf;& s* z. F/ b- ?
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 s- k$ V+ f& I' D" A
2 `! D; O: d( p- r
4 `$ W7 A l1 ^0 w$ vim /usr/local/nginx/conf/nginx.conf
! {0 o' D4 N) Q0 U8 m+ `6 f+ S* b+ e# O, P
, Z$ j; V& G# oserver {3 b0 Y0 E5 r' G/ b$ j3 h" l N# i
listen 80;& D* G. j' c# N
server_name example.com;
4 i2 }7 c& s! x" g4 N' Z. q
% t6 Z) x$ S" b8 N" W7 C/ p& P9 J6 B
6 p$ T7 M% p4 @4 e location / {6 \0 [# z# T- y* c/ H/ Y
ModSecurityEnabled on;
% v' A: |5 A( X H9 m ModSecurityConfig modsec_includes.conf;
! N1 F3 z! j5 N2 M2 {6 W root html;
1 X# B- y R0 |% o( m5 a index index.html index.htm;
% x0 I5 Y* H: k2 V$ T% V" E" K0 _ }
; a" O2 k8 {+ M& i" D}
" M) b' M D6 S% k" t' E使用动态模块加载的配置方法
( }% ?; ^0 @" Z1 {' S+ `' {在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 d5 ~, S! k+ \( G v) H4 j u$ W! d- l1 H( x
+ M) j P! R1 x$ A V3 omodsecurity on;
, O3 l6 Z2 L f2 a! Imodsecurity_rules_file modsec_includes.conf;% v8 z# s3 J5 \# L s; Z& Y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 w1 k7 z4 ]% U& W
4 T% k7 y! p# s& ]2 n# F, I
, E$ r, h5 D: {- y8 b- V4 I$ vim /usr/local/nginx/conf/nginx.conf* g! A, n4 u1 j/ Z4 Q/ x1 F
; g" I. d9 q1 H/ E$ d
+ F# w/ l1 e2 L! ~# x7 yserver {
9 j0 `$ M% x' l) P4 r9 M listen 80;$ R `. g- j2 ], C8 @
server_name localhost mike.hi-linux.com;' n0 `2 m$ H( I0 `* r* z: b0 p
access_log /var/log/nginx/yourdomain.log;6 {0 M" Q7 J% y7 b) ^# Z
0 g2 g$ g7 G Y2 I; {4 j! ?) i6 M' f- Z. e, `4 C: M0 G* _5 w
location / {, _- I$ U o# \5 S
% B, Z+ `' K* J; H" n* m" E9 Z @1 ]- c) s8 g9 ]. ?' N8 H6 @
modsecurity on;
7 {( K" G2 l) l# h1 ?' S0 \ modsecurity_rules_file modsec_includes.conf;
, `- [7 k6 C: @/ d root html;
M" s0 f/ S" g* N6 E# ~ Q index index.html index.htm;
. F3 f$ j, L" i9 t- a6 Z}' ?4 l. U4 u" d' r# F
}" M! v) z& F4 C% j. `
验证Nginx配置文件- e* p# u/ X& N; B( c
$ /usr/local/nginx/sbin/nginx -t
- P, P6 @! \, N+ r8 Pnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok4 y: k: f& ~ ]% g% r, z% M
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2 A7 u: ~0 a( ?! n启动Nginx. u) t; j6 E3 }; O9 [; O
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
; g; T7 p$ E6 X# v3 M
! ?/ L# o, X: O5 _: R! g/ 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能够战胜更多复杂/未知/混淆的攻击模式。
) I6 [$ H* t. R$ o |