ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 |3 `% N1 L& v0 T' T$ B
$ c: R/ M' a/ b+ X7 e+ m$ X* a" ~
4 p1 T9 D* B; D, i* d7 ]0 ^在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) a6 G P7 ^. i. B7 N
5 n H: V% {' ]- \8 ^% t6 R& ]
什么是ModSecurity1 `1 Q8 t' s0 H
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, w& G1 j9 O- X$ r' @. ^9 a/ `
5 |/ s& k5 m, l% Q% @% P; [* u! ]4 R: H5 r9 t" L8 l4 ?# a4 |
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 x" J5 z" ~1 ~
# a8 M* i' O( k4 U
# \) L4 g; b' I" e; o- L! }+ sModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
0 _* \. ^) g& ~7 s6 V
1 b" l$ v" ~( G4 p
( A/ l" f# ^" O5 {0 ]! Y官网: https://www.modsecurity.org/
3 ]1 ~2 z+ a& |# ?) ^- \) n2 M; W8 e" r0 P5 h+ [
8 P. s* r( N) P3 l, s什么是OWASP CRS
$ A s5 H; O% [8 d0 p3 B: eOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
2 F6 X/ [" v2 j) P7 A8 e1 I. U$ c5 A5 L0 M
" ~( ]' d4 b$ j* B2 l2 O9 j; }& zModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: v0 q5 ~ w+ }: n5 n
2 L; q. k6 k6 b% Q2 K) n0 i+ G; ^) N1 E
HTTP Protection(HTTP防御)5 G1 n9 V/ y% B9 G; x! N
HTTP协议和本地定义使用的detectsviolations策略。
. b, h j9 x9 z% T0 c, ^5 ?; c8 T
( }3 N4 r( r% X; o/ q" P) g1 t3 U5 U# q! V7 l
Real-time Blacklist Lookups(实时黑名单查询)4 C! }: {$ T4 S* t" B
利用第三方IP名单。
& d' ]* s5 o$ Q& b4 w) V1 w' V) C- z7 [
$ J# U" [; J& iHTTP Denial of Service Protections(HTTP的拒绝服务保护), V( ~6 e9 J: ?# }6 v7 e- W' v! R
防御HTTP的洪水攻击和HTTP Dos攻击。$ U0 x. T" @6 T/ j
" w; g8 W Q, u. L3 j l
' G' N3 V6 g E1 ]
Common Web Attacks Protection(常见的Web攻击防护)
! M( D/ F6 Z5 m# g2 I检测常见的Web应用程序的安全攻击。 C U _+ @2 c9 E
- M( k0 E# d; F4 L, n8 K/ r
& g* E& a* K1 H+ j
Automation Detection(自动化检测)1 I2 A3 z; ]; E, |- Q& j) J
检测机器人,爬虫,扫描仪和其他表面恶意活动。
@( }) _, q" L8 b1 f1 O* C5 q3 k2 i4 _* ?7 `( P; ?6 W) b
: Q% w& R; w$ z$ R# N
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)' g7 c' O; _/ e# W
检测通过Web应用程序上传的恶意文件。
& X' n1 q% b v& L: X$ r: M4 A
( ^% p% @6 e+ W5 L. p. b6 i5 A8 v* H/ n5 S! Q3 F( x
Tracking Sensitive Data(跟踪敏感数据)
. O% _9 Z- m/ C/ M0 M0 ~信用卡通道的使用,并阻止泄漏。, |9 T# G/ p& f2 p
* x2 s3 O B }$ o E
: J" U! R5 o. l
Trojan Protection(木马防护)( x( q) k- \1 a, H! i
检测访问木马。
* D$ \- [. \/ N8 r e/ r8 M) E, h& @& T9 ~# z% m! n1 v+ M
9 H- e/ i C7 g0 e* C; M) S
Identification of Application Defects(应用程序缺陷的鉴定); ]% p) I/ h1 \6 N8 H
检测应用程序的错误配置警报。
4 Z9 }% E6 S& z* l. v5 P
4 e$ }& q: ?% [
2 |( z, o0 z) {# AError Detection and Hiding(错误检测和隐藏)0 u4 N1 a8 n3 a; }0 i
检测伪装服务器发送错误消息。, I- G& L6 s; \& C' q
/ R7 o2 h" ~/ T V: {- n' @+ D& `
安装ModSecurity4 t. p+ u' i N: N4 q5 r
软件基础环境准备, c* c3 ?7 F! n7 y% m
下载对应软件包
% I( S* r D* ]1 l$ cd /root
3 f" U1 `! [6 v9 m1 G$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'7 F0 B1 w8 R! q% I
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
& q$ W. n4 ^- D/ k. `. G+ q5 L$ I安装Nginx和ModSecurity依赖包6 }1 [9 c& B2 p) S5 q& D( G
Centos/RHEL
, W6 y1 ^ }0 F; s0 V
4 }/ J+ l/ X( p
) }4 T x) n2 j9 g: ^) h- x1 J$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel: A0 y/ P9 _3 x$ S) u. c- y `1 }
Ubuntu/Debian
. z: e V1 Q1 i+ k' F2 V# X5 U" s
4 S* S C) E5 y7 n
+ M5 t+ d/ _5 A: w4 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, Z' R( Z9 c1 e5 z
编译安装ModSecurity
/ a0 n# I: q6 ?# M) o7 Y( gNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
5 n- u/ L; {) V9 w
+ ]7 u M! C) r/ f2 t4 ~: l1 q" k
* O) k9 [; b& ]5 i* |8 S) ^7 z方法一:编译为Nginx静态模块/ q: k7 L# n! l
" a7 w8 G7 i: [: q1 s( W! D
- r O8 u/ S! o% G4 c( Q
编译为独立模块(modsecurity-2.9.1)
9 q. Q% x, l- W5 f$ tar xzvf modsecurity-2.9.1.tar.gz8 M0 n P. l, x1 d+ s
$ cd modsecurity-2.9.1/
7 H9 N) M, A! V; ]% N$ ./autogen.sh
+ a' A8 y! l5 R9 n9 g7 N6 P: O$ g$ ./configure --enable-standalone-module --disable-mlogc" ]& b. z" F7 W8 T8 }* {7 t4 e/ X, p
$ make
9 w/ n, Y1 n+ y2 g( s. }9 `- g编译安装Nginx并添加ModSecurity模块
: F" D# T. G2 p% H1 b0 K$ tar xzvf nginx-1.9.2.tar.gz5 k; T/ H6 B9 @" l9 r. k8 y S& T( D# H( d
$ cd nginx-1.9.2# q( ~ q- o6 |
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ |& ^0 K( v$ M* U0 V8 c* ^# Y$ make && make install. i7 Q( f) ?0 G3 F; a; ~. P3 b8 }: f
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; d; p& \* p7 M! |0 ^* N
8 p# ]& r; B$ H% `
- p$ R8 |! L$ B3 `5 d2 Z: U
编译LibModSecurity(modsecurity-3.0) P2 w! O7 W- m6 ?/ y! p
$ cd /root
2 \' L1 M6 ]. R9 H) d$ git clone https://github.com/SpiderLabs/ModSecurity5 O+ a0 D+ R! v4 Y( I" l6 T
$ cd ModSecurity1 G% A8 v" a- j" H y0 m8 y
$ git checkout -b v3/master origin/v3/master, u3 h# T9 L8 I; h5 B# x( K
$ sh build.sh
* H: T7 p2 j+ ~$ git submodule init
( p. l5 f' L! O% ]2 p% c$ git submodule update
, I) ?: R$ i. V! h7 V( S/ V$ ./configure
/ d0 a' z: F8 W" I. m& _1 f! c$ make
7 ^$ s/ z! j# ~$ make install
. }1 b# |* ^9 [, s/ ~# O9 V" mLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 c, {! u" Y6 O3 W z$ [3 B5 P0 M: ^" d5 V/ c
+ y6 K# L4 o' ]4 J* }
$ ls /usr/local/modsecurity/lib
. r4 V$ N2 ~' X$ Alibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0' H* I7 w/ ~2 l7 w' t2 V
编译安装Nginx并添加ModSecurity-Nginx Connector模块
0 i* L) l! Q$ y7 P使用ModSecurity-Nginx模块来连接LibModSecurity
2 t0 |. E, u# \ [8 D1 D+ a$ G1 ]- e( E- E7 R7 b
; c' \/ K$ Q$ t
$ cd /root0 \4 u y4 B- B+ g9 M% f1 m3 v
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% h( v0 X7 }1 ^
$ tar xzvf nginx-1.9.2.tar.gz
8 G" k, c0 x* a! q- W8 s$ cd nginx-1.9.20 ], }. o) H: E6 D7 o, H( f
$ ./configure --add-module=/root/modsecurity-nginx2 B3 {+ y; P1 I. D" Q
$ make
s } G" t+ G0 }9 p+ `$ make && make install3 N K9 L& S& G. A" z+ u4 E8 M
添加OWASP规则. c9 E* B. `3 l; Q0 X( G
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
$ M% u' M- ^5 C+ t
* l( s: t# u8 ~& @' C8 s m3 S2 [: I1 T k7 v. t
下载OWASP规则并生成配置文件
! X, d; ?6 J# |2 V: z5 p* | t$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 p1 h: h% |7 w& M- \2 w
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
* n( c$ K7 S% e9 s* \% T B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 E5 r7 i( k* s v1 _2 B$ cp crs-setup.conf.example crs-setup.conf, A6 {+ X5 L ]1 ^ f& c2 ^# S
配置OWASP规则
1 {# R8 B$ a n6 o1 x: s编辑crs-setup.conf文件% J& U# }( M8 L0 f
/ h0 _7 ^$ j: r5 P0 _
% Z: c3 y( x5 [' R$ y# `7 V
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: _. q. s5 t" H5 f
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf3 ~' T' e W% l4 m
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* d2 a' m v( {* W
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf7 u. E+ J9 Y* U. t
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. F/ ^, a! X, o! j: K+ f( Z }
! r# \1 i+ T# X3 F1 {5 u
; ?1 y+ P6 f8 R& `. C( e# z H1 p启用ModSecurity模块和CRS规则
9 X' t, C% z: ?8 D复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' ~5 a+ E' W Y N, X' R, i
5 o) U- _( A/ P' J* I# j) ~3 B1 n R( s3 P& B
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。+ C7 O( Q# ~4 T6 E3 w
3 h6 v1 h- T( o J' `$ P0 W
' ~* c$ c9 v) S1 G! o, d/ v$ cd /root/modsecurity-2.9.1/* i! ^1 g D+ `5 P6 F3 K7 F
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
: Q9 b4 w9 _- D3 D$ cp unicode.mapping /usr/local/nginx/conf/
+ [) ^( m( Z9 f) `将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( @# ]% [) Z( x$ @
0 L: a. ]. ]% A5 U
+ |0 B& u3 t! z# k$ vim /usr/local/nginx/conf/modsecurity.conf
# D2 O k4 r1 ySecRuleEngine On
8 c; x4 j2 E5 i* sModSecurity中几个常用配置说明:1 b* D9 Z+ l, U- T0 G( L T+ b
9 g- F. F6 k( z
# S3 s- L% \( X- b6 ^4 w/ F1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
- k9 a* j C+ J! a6 L5 L1 w+ W5 q4 b) C/ U6 b# k0 u
; d4 |0 u& d; z) }0 i
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" y! v$ `6 ^) [8 H2 l( W* ?* T
9 ^ e9 g9 G1 [) L# f( X' l) l0 \7 y4 ?; ^- n
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。/ D, T8 _6 t1 i$ E6 P
( b/ Q# t, W8 ?% z' C+ T! F% N
$ J) i- h- P( {7 k' k4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; i, o2 f0 H" v5 N
; `9 p$ w% l2 \4 d' Z; n" w( n6 X' [, L/ W6 L+ P
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 M6 y1 { v7 ?$ s7 y+ m% I" J
8 K3 x+ Y3 d' z$ ^3.x版本CRS
( m+ _" p+ X5 \+ V1 I, U. y& D2 ]( e$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 J( E, M# M" U2 R2 g8 ?, H# 生成例外排除请求的配置文件8 v6 X9 |$ s7 v2 ]- |5 c8 N9 e. u
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
0 _! g$ U. z( d( |$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
' g9 E8 B& P5 m* T% [$ cp rules/*.data /usr/local/nginx/conf
- g8 s( V8 D' m( q) _为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' V P( j& S& S1 n- A D7 K, m8 p- U( R
- B7 Q% ~& x+ I
$ vim /usr/local/nginx/conf/modsec_includes.conf
# Q; O2 H+ Y: l$ {: J8 Y6 r+ u4 b3 V0 ^0 g* W4 }3 a+ n
[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 2 Z: R7 X5 O3 |" B0 x4 j7 x7 n
- q3 Y% F; C1 D% k
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' m2 r" _" Y$ k$ A; u3 S( T3 h2 E( t, q2 r/ ], y4 j( p
% N0 L n" d# n, {9 @$ o, k
配置Nginx支持Modsecurity" V9 j( ~. r3 E) F9 H/ M4 r# q: W
启用Modsecurity" x" h, }5 j, ^; J4 e t4 [
使用静态模块加载的配置方法
& ^( {7 s2 l/ C在需要启用Modsecurity的主机的location下面加入下面两行即可:
" J; b! C+ e- f# g6 W# X6 n/ n
1 L* h, B- X8 I: P! P$ U }! x/ ^/ u7 m3 w: T' K
ModSecurityEnabled on;& ~5 c4 B( D8 s" g7 `
ModSecurityConfig modsec_includes.conf;
6 N2 J6 w8 z0 Y: K) P. L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. s6 P' q- I' O7 `/ r0 K$ J2 c2 `3 x! x- m; g
7 s q% Z+ x( i* ?6 E+ p$ vim /usr/local/nginx/conf/nginx.conf
% a: X: `2 c; n, H7 b3 q' d9 d0 t, A. @0 Z0 A3 Q5 E
6 U+ a3 S0 j# m$ S8 i: Y0 @
server {
. k3 Q9 l/ R7 T' E# \ listen 80;8 C. x- N, ~+ U- \* e8 W5 s& K
server_name example.com;% e+ n+ i K. s) I
6 H9 p Z2 C5 I$ P+ |( D
5 ?( M& t$ W" ]. _( P location / {) ?) \/ Q5 |$ X }7 w2 R1 U
ModSecurityEnabled on;* h& ^# d. ?+ O: E6 s( r
ModSecurityConfig modsec_includes.conf;
( P' _, Z/ O+ o- } }0 [" ~1 t root html;5 i0 b* u5 q) A% Q6 r, t: m; t% g
index index.html index.htm;
; {; q- F0 p' d2 `; e }
5 L2 H% C% [8 G% R" D}& R& B) O5 K' n4 n2 x
使用动态模块加载的配置方法5 U6 A/ B7 }$ n" x! h3 q9 u' |0 a3 k
在需要启用Modsecurity的主机的location下面加入下面两行即可:: j+ Z9 w5 k, A8 t6 K6 d. c8 d8 a% [
: S0 d+ {# ?3 Z9 D0 `: K
9 F0 N( Y( W4 O! t, A! Z, ~
modsecurity on;6 z& c# s! g' a$ _- s! y
modsecurity_rules_file modsec_includes.conf;2 b( h2 q; L% y$ u/ k# {7 v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 q0 i8 m- j8 W( o- B, k! z( o1 `4 j3 `; J
( |! U Y+ j$ B2 {3 x$ Q' W& W$ vim /usr/local/nginx/conf/nginx.conf
4 ^7 k! I" Q4 |+ n' v; r9 \. v6 ^: ^
6 W& p4 Z6 O' S. s# s4 u7 B) J
server {* w6 T/ k9 T" q
listen 80;
6 G, O: N9 v% o( B R" c. r server_name localhost mike.hi-linux.com;
2 f3 @' H8 S3 O! b9 @% j3 q; N$ S access_log /var/log/nginx/yourdomain.log;" ~. j/ D5 p: b6 k" [+ m3 ?
8 ^# k; F5 e8 H: U7 y
9 _: l% E+ ~1 t; [/ T3 U. Q7 L location / {$ _, Z9 i# e: i& ~1 e
* E6 l1 h$ m# v# L" V
! n* S X! @5 K modsecurity on;
# t ]6 r3 H8 x0 t/ x modsecurity_rules_file modsec_includes.conf; k7 b7 r4 H0 ^" m
root html;
5 m: f" B4 i# ]* H index index.html index.htm;
9 [4 {# p) D) O2 [! D}
B1 {& x; Q1 a# d! b! @+ @}
; p) z9 D' c: U* T' }. E验证Nginx配置文件5 w% |3 s2 |7 P5 o5 ?
$ /usr/local/nginx/sbin/nginx -t
7 V) k$ v+ }0 t; U" ^2 mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- d& n' J7 ~" J/ [4 C
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* Z P i0 ?7 `! g# p2 y! s6 {
启动Nginx [( k* |% y1 u) V4 ]
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
% p9 a( m1 z% E# E1 x. U! H7 O
; \: r1 b- ]$ Q, p1 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能够战胜更多复杂/未知/混淆的攻击模式。
! J5 \; }. p% e: O+ U. m |