ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
# w' R. M. C: {/ C9 w/ r9 u7 G- y5 K( K: b) B: y4 M4 W# K8 l5 x
, m u0 u2 t. k# |; G; [在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。' m- r8 L9 f9 t, W; E% f
! M5 m8 e2 a; I! Y3 y* s: u
3 e' i/ Q/ s% g6 D什么是ModSecurity
. _6 u0 O$ H: Q" |- IModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ B' ?5 ?1 S% E- _$ S/ \2 n
- {. W4 j6 `# d5 h2 N! ]7 a A# H( d' @; S, c; i' O1 ?9 a
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。/ g' ~3 z5 l) p% c: y
; D4 G, N' A |$ P" G9 ]) A# J3 _$ A5 Q& l+ Z Y( F/ r
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 M6 t# K$ Q' n
. ^, Z8 Z! W' c. c) Y; ]7 s' P; P- j8 q0 j; v7 }7 L; ^, g2 f v
官网: https://www.modsecurity.org/
0 ?5 d9 k0 p& @" G
# n" t# @" |4 m$ n( H3 [
* R5 P) e! l- }6 v. y% y什么是OWASP CRS* {! e3 D/ O( Y' l5 O8 |0 T; g
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。9 V3 r* E: _# [) t9 A
% f/ V6 {' s, Y, V% }8 R* y
$ O: w9 m5 v3 N6 |/ T
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; l$ j- A( s' h/ M% d
4 ^4 X: \" E' Q, \6 m4 X0 @
) U9 S: u, e. t i) fHTTP Protection(HTTP防御), I/ r% H) P8 r! i k! {. A
HTTP协议和本地定义使用的detectsviolations策略。
7 @$ j% w( ~% ~, s# }- c/ @) z
1 ?3 F3 K$ U! }) Z, _( k; A" H/ s- D$ ]& F( v
Real-time Blacklist Lookups(实时黑名单查询)' a0 G: W6 y# y- t* _/ m1 h
利用第三方IP名单。
. u/ l: O% X0 T. G1 r. V# q+ _ V) P" n5 ]2 x5 J* v
# c5 m3 i7 b$ ^5 _8 R5 X
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
: S2 M7 M3 p+ R" K$ T防御HTTP的洪水攻击和HTTP Dos攻击。
' z* t' b% b: t( h9 i \1 J/ p* w' e; }( w" }
" L+ \+ J; f' Q- [Common Web Attacks Protection(常见的Web攻击防护)
# W* c- V1 M3 U( d# p, }; u( q检测常见的Web应用程序的安全攻击。
6 u% e. S# K+ G) o1 n, m- _. d9 J
( N9 J$ D! ]! f1 S; R% g
* t" t# b9 k2 h# {. }Automation Detection(自动化检测)
6 Q1 z# n1 P9 R8 H0 f. ^% u检测机器人,爬虫,扫描仪和其他表面恶意活动。
4 T$ P( b, ^/ x: j' U. f1 p7 |6 [6 _# J
! F: v, j4 s, \ [( ~( u& Z" }
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 `3 ]4 b( d- K检测通过Web应用程序上传的恶意文件。; t: x2 K m# I P
& m# ] Y8 y$ H. B
+ e% |8 m( C) V0 K5 y4 C% GTracking Sensitive Data(跟踪敏感数据)8 P( y1 |: [# L, o V P
信用卡通道的使用,并阻止泄漏。; `* k5 v2 n& Q7 ~& _4 ^
. R( j& C$ p# J1 v1 ^- U' {
1 M( S+ _! d8 R8 oTrojan Protection(木马防护)* [% I' C" u8 i \3 e
检测访问木马。# }" X( [3 o6 ^/ P! x. f( c
9 ]; n7 S& S6 d( P! W( o8 k# Y: e- H
6 x2 T6 S8 p) F8 n& UIdentification of Application Defects(应用程序缺陷的鉴定)
4 V) h5 T( Y6 U; J( P* p0 O3 @, x检测应用程序的错误配置警报。, I3 {1 X4 t+ S% v; ?! U
; J! x$ |5 z; `- [2 Z. n
0 s2 v, v: t1 e" [$ h7 u2 AError Detection and Hiding(错误检测和隐藏)& e c% Z3 `6 F: [9 H5 H9 V0 X5 Y
检测伪装服务器发送错误消息。
6 P5 H. J4 I6 ~3 A
$ S @* k: u; A# e0 \2 n( c+ Q S2 i7 e( A
安装ModSecurity
" u4 o. E9 s5 ]3 [; r; m软件基础环境准备) L0 a0 ]& L) d' n% p
下载对应软件包( z) |9 A& M' I: R$ J
$ cd /root* i+ q! o; j; S: C, c$ q
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz') `' K7 E* w/ S" o6 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/ t, V. V- p! D9 {: Q/ n
安装Nginx和ModSecurity依赖包3 z" H, F- n: T" F
Centos/RHEL
+ l3 M5 q# f, l% j. L9 a9 l9 i5 M% {$ z7 }+ Y) D
2 O4 p% ^7 O1 J+ O$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% o8 Y5 K1 Z; ]- hUbuntu/Debian+ Q' r- L1 `" I a
! c4 m* q$ `( n
0 r# _/ x( E) o; i1 e! t x
$ 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
+ |, g( I+ }7 _% z4 u编译安装ModSecurity: g9 C" z" q# q- `# W
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 M# X9 J1 ~2 U
% V. r7 e" m4 m! ]
* M% i) U* C* Y* x% `$ `8 b3 y% {
方法一:编译为Nginx静态模块
2 |- g2 L4 |: e P
( ^, r7 K8 P% J2 |$ X# N+ T9 Y5 _9 M' N) l3 I2 o
编译为独立模块(modsecurity-2.9.1)
, y# @9 i' A5 Y1 f, d) C2 |% ^) T9 l$ tar xzvf modsecurity-2.9.1.tar.gz
7 z2 B" t `' V9 X( u$ P$ cd modsecurity-2.9.1/
3 ?: E, P/ Q9 @5 q! N$ ./autogen.sh
8 { d& U) U# J7 s9 Y8 Q/ g$ ./configure --enable-standalone-module --disable-mlogc; B& a) k8 P9 F( M
$ make+ L9 D8 F! L% ^0 Y% P
编译安装Nginx并添加ModSecurity模块
1 Y0 o% k' B' v& F! i1 z! l. n$ tar xzvf nginx-1.9.2.tar.gz
0 m# v# h* d, _# N$ cd nginx-1.9.2, n3 r- j ?9 Y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ |0 t- G1 H$ A: _
$ make && make install
! |2 j" A" b2 B方法二:编译通过ModSecurity-Nginx Connector加载的动态模块1 V, k9 k$ ?# O: ]
# e5 Z8 Z \4 b. j0 W* B! R$ K7 P6 k( d: i$ P
编译LibModSecurity(modsecurity-3.0)6 W. }& z4 w$ Z3 C1 u7 D1 f
$ cd /root- y# K# i( Z" o; [6 S1 z) g
$ git clone https://github.com/SpiderLabs/ModSecurity9 I7 b# t: s8 X( n
$ cd ModSecurity2 U$ X2 s6 }" L/ O' S2 |+ k: u
$ git checkout -b v3/master origin/v3/master
- Y6 t) n; W/ g5 M* G8 ^$ sh build.sh% ~$ Z% a5 m+ e1 Q: c! f
$ git submodule init6 o ?4 x' G$ }' c
$ git submodule update
, H5 A1 B6 K. |( k6 Z5 w$ ./configure
$ O2 H% @2 C) ^8 C7 B- R$ make3 e+ `6 _5 i6 k
$ make install
+ N/ x# [9 ~2 Q |7 pLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: P& M0 ]8 A" Y: M( P. K9 x/ l6 I
+ p4 H6 r: f8 U1 V, o; P, ~; n
& w9 c6 ]0 c, h: |" [$ ls /usr/local/modsecurity/lib
, M' c r6 \% q; @8 ?. f) ylibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0; }% S) k) z: h! l# x9 R1 {, a
编译安装Nginx并添加ModSecurity-Nginx Connector模块5 z/ S6 {' E: c$ N0 H5 `
使用ModSecurity-Nginx模块来连接LibModSecurity7 U+ A( i* r# y n' c1 y; w
, Q$ |, i$ e( \* @: Q0 Z/ T( [8 D. V
8 J: X0 i, x& Y+ r4 g% l$ cd /root
, ]. ^# x5 V s+ h, K6 y$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ h9 ^& X3 p6 j$ tar xzvf nginx-1.9.2.tar.gz) e, W" F" {- ]+ ~6 M5 x1 @$ l$ k
$ cd nginx-1.9.2
% p( @+ I4 X% c; }. r, S4 z( D$ ./configure --add-module=/root/modsecurity-nginx
! k' J7 u+ d; r4 R$ make, U7 g1 l5 E& M$ p1 T+ A
$ make && make install) {9 m- i2 c! T: x/ n9 w& D( s
添加OWASP规则6 J& b0 J7 p+ b4 k9 N
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 i+ E& m# I2 i4 p; l# k j2 p8 T; h6 z, a: s1 M Z, \2 J
8 r& p5 d6 `0 R% Z" F' p+ c; h. X
下载OWASP规则并生成配置文件+ c4 s3 Y9 ~( i, d. P
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
' x+ Y- o) C! F0 m; c. s# j* `$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/+ f7 ~6 `8 _: p; Z5 v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ @; A. [ `8 ^6 K6 C
$ cp crs-setup.conf.example crs-setup.conf( B# Y0 k$ y- M6 M
配置OWASP规则 R1 o* G) K6 ?, j/ g" t: n9 {5 _6 _
编辑crs-setup.conf文件# D+ }! x2 a- x( o) n
# m+ }9 O# W9 X7 X+ m. ?1 N3 P' a) J& V/ J
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 m& D+ p. C d" ~9 I- \! G$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
5 K8 a& R( v- V8 K4 k( ~( {$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf3 Y& n* G9 s/ b1 B) u
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
0 C1 y4 u3 c- T默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
' a* Q0 M3 e" t0 E+ V; v* U7 \2 o- n) [( N8 [* j; Z$ e9 K* Q$ Z
, U+ S$ v! t& ~3 ?1 \0 d
启用ModSecurity模块和CRS规则5 | B8 H- }, N* ]. o+ t& ^2 F: I& s
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" w, e4 ]# l7 h% J5 H% q
; L2 M' z' W+ k8 s& V) d, _
9 i# |$ F/ {- s6 \; z$ e2 S* B& c/ p
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
' z! t# e% E: g0 D. i: G' g' U" I
, \7 J& I: |- X' q- @7 D; m" O7 J$ cd /root/modsecurity-2.9.1/4 j0 N( X3 N& F# H! Z; I7 E* w8 w
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 5 O v, j4 W9 L) l" j# E+ d f
$ cp unicode.mapping /usr/local/nginx/conf/2 }# C; d* }7 @$ B3 j* Z" _$ k
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" J6 U( _. ~9 S2 x2 i. j3 i2 f5 F7 Q& w: i
K& l* I- Q( n0 `/ B1 S7 |1 J% m$ t$ vim /usr/local/nginx/conf/modsecurity.conf* X5 [. ]( n) g" ^ x
SecRuleEngine On$ z8 } {; ` i
ModSecurity中几个常用配置说明:$ [ j: N; a, {# Z: H+ k
! v; V: k1 |/ ^% R
6 X6 E1 J1 T3 M0 x* t- Q3 q3 [1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。# [: k0 u" `$ ~" O4 |" m
+ Q' E! g; E( u: M% S' [5 _, l4 ?* u; Z& c8 O1 }: E
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 h# ^9 n s1 r- d! E3 J* i9 [
2 _. v) g% k3 a1 D
/ n( @; W( p' b0 d$ h1 P3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 j: H5 X. C( T$ f" m- K
. x8 k. E, e; r/ J
: C- h" T( x1 E5 M& q$ N' A5 b% D4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 o# q7 B/ a7 a5 Q6 L
6 G9 K0 T- N# o! ~# \* N6 @7 }8 k; P- f, O: b* b+ E
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 i) s z" R, }* _4 j& ?0 f$ n) L% K
# e8 B0 F. T; [4 g5 e5 j6 `
* p* _6 U( w1 q9 s3.x版本CRS
. N& q6 M4 g, z [$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 n3 t, z8 A" s7 h. p: \- J$ T; z
# 生成例外排除请求的配置文件% i, \) o& ^7 [2 _. w! n# Q
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf- Y7 w# M8 y! T3 Z7 U# ]
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
0 m6 G" q, s# L' v9 C7 G$ cp rules/*.data /usr/local/nginx/conf) W! i0 Q' Y0 A# s- j J# c, V
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
( T5 Z& E0 M% t G1 I& O$ K a- ^( R Z; Y0 {
0 }7 e# A2 ~+ Y8 O% T1 ?! x1 P# k
$ vim /usr/local/nginx/conf/modsec_includes.conf; R0 f! u" I0 b8 _9 R
* j0 `: I4 h' V: 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 : R: F" g4 ]; \
; W. `0 u: e: v7 M- P
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 C ~# W0 b7 \! O9 i% I# D! y
4 J. K; q5 d6 C. U x, Y" s
6 u5 C( n/ s7 a% M& ?4 i
配置Nginx支持Modsecurity
. n; U7 A7 ?1 ~启用Modsecurity7 X5 h1 [* S8 K" d- x; \( ^
使用静态模块加载的配置方法3 P9 w3 h7 t& R5 Q' n
在需要启用Modsecurity的主机的location下面加入下面两行即可:" P' `2 G7 S/ T) V
- t e$ p! T M( W
u) h! b e9 vModSecurityEnabled on;$ h( L3 L& n% `9 A
ModSecurityConfig modsec_includes.conf;+ `" J; @5 n! D0 u% B! D
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; B8 @ u! u5 P& g4 z
1 k9 W1 g, z- n" ^& s* }
. W( [( P. q7 z h1 }* x! e3 g
$ vim /usr/local/nginx/conf/nginx.conf
& ?& t( g) L+ K
/ \# _! }/ }9 V0 k7 a) A
) h( x, J$ c' v) G. kserver {
, Y. ^( ]6 h& X listen 80; J0 V9 p$ C+ e
server_name example.com;
0 |$ N. ?' m4 _7 s: c
/ E5 v: {! a, ^* z# H$ q* U0 ?9 @. {9 y% K6 Q k* n
location / {% b8 x/ }0 Q- C0 E& ^
ModSecurityEnabled on;" }0 A% C; W8 k- D# I# j$ F; x# R9 r
ModSecurityConfig modsec_includes.conf;5 y8 V, S5 R/ A* Z
root html;8 I; t A" R6 }9 o: i8 }
index index.html index.htm;
& D5 v& m' P: t+ }. C# W } T h/ R& d$ _ Q- e
}
7 V5 h8 ?( P$ Q% g7 b- ~使用动态模块加载的配置方法
; H2 N/ u! X3 Y( ]# D; T' D) s在需要启用Modsecurity的主机的location下面加入下面两行即可:
, f; z4 @1 p* P$ K# M; m" k7 n1 d2 c7 V7 f' `
}( `2 I4 u% i: v2 Q. z1 T2 h; [7 E
modsecurity on;
- V9 u8 `0 O+ I! rmodsecurity_rules_file modsec_includes.conf;
' `' {# m5 B+ _- L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& O3 o7 Y/ Z' G* Z1 N
1 T& S: U* `* W }5 B
1 [# g2 ^5 ~; L# I) q& l+ q& z$ vim /usr/local/nginx/conf/nginx.conf
W. w$ `" ~! b$ V; _+ ?
7 P" S6 Z1 C/ B* D H: @4 T( b, x% Y$ r
server {5 S! x2 F6 X- S# T
listen 80;
I- u, M$ i1 [( z3 ?8 J server_name localhost mike.hi-linux.com;. L; ]* s# B& `" s$ a- [
access_log /var/log/nginx/yourdomain.log;
" D# ~, B' e; m2 N4 j: Y5 {
6 w1 {0 T- g9 P6 B. B: n" j, l& `) R9 l+ L0 Z9 d2 S& Z# S% L
location / {: g# W$ e# x$ ^6 V4 @- _
T5 C( t# M F; J' c1 G
3 v0 |: v8 w" ? i! u4 [4 W
modsecurity on;0 h/ m9 h# k6 z# @2 S
modsecurity_rules_file modsec_includes.conf;( x; P$ G% J7 l" T9 t) B
root html;
7 j4 s8 y% J2 x3 j3 i5 E1 X index index.html index.htm;
. L/ ~% i3 U4 [4 g1 ~6 s- |}
5 j. S& f1 w4 _( G& C s}
C) r6 {" `( q. M! S9 q验证Nginx配置文件; a! v9 {7 J% j+ D$ E4 f* w9 K W/ X
$ /usr/local/nginx/sbin/nginx -t0 K6 M: }# I8 _! b1 Q3 S3 X, h% ~# y
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ O8 @2 V: u2 Y3 t+ D, R5 j
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4 w( T- T1 {) M* X: a启动Nginx: }2 k& y# z& {4 N2 n9 }
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf- k% Z j2 `+ D" a3 m# M2 {
7 X* c) T: i6 R( `4 w- G: A* Q) 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能够战胜更多复杂/未知/混淆的攻击模式。
$ a, N1 A5 i3 L8 w% h1 z |