ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: A, i1 W( H$ n; r% [8 N a
3 ?+ c0 T/ k0 u2 H0 P* G$ G V/ H* }+ z- o& r, j+ N7 g8 S
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
# w' A0 \ W% t) i5 k$ Y+ ^ J5 N- o7 V
1 i6 v7 H( |" x0 V4 m2 i% z1 q6 |4 i3 F; W, r& |
什么是ModSecurity* Y, m2 r5 v7 s2 ^# q
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
. D, g* `7 _% O* l; ^4 s& a2 V2 j& D; q I, j+ C$ b
1 o O, X M J- e1 d6 |ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 X3 x. Z' ?: h( {) D" \
; ~' H# c2 N# b; r. R) F
3 ?3 E+ D1 [; O5 g( [( [7 v' SModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
7 Y0 G5 x# j# d
0 y7 w! X7 a& ^# J
. R7 m, x5 P+ G/ y) ?, s( Y官网: https://www.modsecurity.org/( a+ _1 Y7 B2 G" T$ \
; ^" i a5 B- h( N. Q5 y& v' ?% z0 K1 |* g2 k
什么是OWASP CRS
1 N" n, e+ b' C$ @% UOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
+ }: d( k) z& h: o5 R6 x/ r3 I5 h3 K Q6 W+ U- e
8 Z1 ?; ~* H. l/ a! k/ p' XModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
" G* n! A: v+ V' r, n4 v- f* x7 Q
* ]; v9 z1 o4 s! V# G; }
HTTP Protection(HTTP防御)" Q9 _; w J+ X
HTTP协议和本地定义使用的detectsviolations策略。
- h3 S% E5 X" y/ j8 H4 L @1 w$ J1 [- g
2 @# {4 N( P( p. x' _. B
Real-time Blacklist Lookups(实时黑名单查询)6 `, }# N9 U+ Q6 G6 ~# P; M3 W
利用第三方IP名单。7 P$ l: Z2 p2 N+ a/ ^$ j6 H" ~
" k3 s( _0 A- e* x$ U! |+ F& z0 u8 H( D- A8 W' f
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
) c( _! S8 [) c) }+ R防御HTTP的洪水攻击和HTTP Dos攻击。8 s1 j# p1 H6 ~! {) Q& `& _
. @- d/ c7 i4 _! W" n
) k. B9 ~& c" oCommon Web Attacks Protection(常见的Web攻击防护)
9 j" [2 e2 |) l' m) ?检测常见的Web应用程序的安全攻击。4 H+ X+ y: F" Z
, i/ Y$ b0 x# j5 S# }3 l+ c0 L: g# A: _6 Q. u# U' f+ T" z
Automation Detection(自动化检测), m7 g$ \% k- C- _
检测机器人,爬虫,扫描仪和其他表面恶意活动。& E; o5 Z% n, Q$ e8 w4 z
' b' K" x& `* d2 B& o, b* q' G
1 a- ~; N W$ q5 M
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)9 K6 ~) P0 D0 ?2 |0 O: B
检测通过Web应用程序上传的恶意文件。
1 d- s+ [2 }/ Z$ ^, g+ a8 g! H
$ m# W9 c5 `; B- R0 d0 U; }0 r! |9 ?& A5 K: F \
Tracking Sensitive Data(跟踪敏感数据)
4 @& T/ a) j5 }2 S6 T. y7 E" s信用卡通道的使用,并阻止泄漏。; _ }( l% Z' u M
% [8 [8 K. z" R! ]6 i! H4 H$ r! B! T
Trojan Protection(木马防护)3 _% B# V; c H" D- E" m& ^
检测访问木马。
$ w* }2 T) b4 U+ v& m9 N( N7 L5 k3 [% ^ \
: a( d! S0 }& h0 z0 |
Identification of Application Defects(应用程序缺陷的鉴定)
( C: h+ K% K8 |9 }. n" l检测应用程序的错误配置警报。6 w( i8 Y- l9 @6 F
9 A1 r# k# y5 M: A
' b4 S. x+ h" ?' {& o6 h
Error Detection and Hiding(错误检测和隐藏)# n- X5 I7 @; @9 M
检测伪装服务器发送错误消息。/ {4 S, y- ]" Y E
+ E2 V2 v8 u: ^% G0 W2 c+ K
" J1 b) q0 m7 d6 h$ n3 l安装ModSecurity
3 p3 z- z5 O: e: j I软件基础环境准备
( A H( N7 C7 q6 C8 S" M% a$ i) w下载对应软件包
3 n! z0 u, ~& ?+ x, ~( ?* \0 \$ cd /root
- e1 k( q$ S+ s$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'4 n J- ^7 N! P2 c9 W) O0 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. b7 Q7 k+ ^4 [1 K y+ s
安装Nginx和ModSecurity依赖包 d0 j# g a/ _- ~
Centos/RHEL
! R- \; R+ y- t4 b5 W' g: ~
$ v4 v% L/ h+ T! C5 e3 A2 D1 x! m/ g2 L9 d7 E: K
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% b- V# R q0 W5 b! _' iUbuntu/Debian. w+ `+ E3 p' {0 W; {' M. B
8 l1 ?( J& o6 E. C( G: |( H
+ j$ y3 S* j# E, B
$ 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- C) C% [- q' Z ^, y% f6 ^3 ?
编译安装ModSecurity. _7 p* x: y# T9 y2 B2 Y$ W6 i# C
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! @$ L {3 a2 I5 e% I
9 r2 n7 S, ~5 W) m% e9 T
' Z* n2 H# h& _方法一:编译为Nginx静态模块( X4 y/ ?) h. E% Q2 ?
3 R. W" p m' Y1 `9 Z
8 M: J& x9 H3 h4 l7 F" ?编译为独立模块(modsecurity-2.9.1)
( N% M7 M8 i0 ]$ tar xzvf modsecurity-2.9.1.tar.gz# O: X. |$ W/ y5 Z
$ cd modsecurity-2.9.1/8 q/ _* [! o& a
$ ./autogen.sh n1 |6 m) d6 \0 u! D( Z! T3 ^
$ ./configure --enable-standalone-module --disable-mlogc
4 e. S u; |$ l" H( r" O$ make
; z) x: v1 }! ?4 i9 A3 R编译安装Nginx并添加ModSecurity模块" _" E" t3 u/ K2 z0 F- {
$ tar xzvf nginx-1.9.2.tar.gz
+ q# b5 S( S$ `( M6 o: H$ cd nginx-1.9.25 x1 c5 _$ c" m m3 `, [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 n( H( o+ b; `8 z3 j1 v& H7 M; s
$ make && make install( j+ D ^- U+ k6 `0 C+ Y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块: a' X0 K0 H6 z) l
3 S) e9 C4 |( `' o
6 |1 o5 G9 m( ^' O* E1 m编译LibModSecurity(modsecurity-3.0)
" m7 x8 l- Z! o' w5 T# b$ cd /root
/ P! k- z6 H5 R0 g U$ git clone https://github.com/SpiderLabs/ModSecurity
' q! G$ l( C% z. \! ? D$ cd ModSecurity- {- W& g1 g, Y M" h ~% v; M
$ git checkout -b v3/master origin/v3/master
1 r' ~0 B* L6 Y- R( K; s& J+ S+ ~$ sh build.sh4 a; D8 e. N% _- K3 S! ~) Q6 a+ G
$ git submodule init0 q) p- y N" R$ X
$ git submodule update
$ T- l) _5 `' h8 @2 @4 z( g$ ./configure) F% z' J, g& e
$ make
8 G: L3 Y% g9 A0 X' z' ]$ make install
. [9 ], q; S% R% X2 NLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 h1 l0 [, H& D; j4 |
! g$ A% N" M, ~
6 o) q% a1 Y6 E1 N, \$ ls /usr/local/modsecurity/lib W7 v7 @/ y4 B8 @4 G5 o
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0. X/ z# |/ ?" n5 p1 Z& s
编译安装Nginx并添加ModSecurity-Nginx Connector模块
! o7 m a2 {+ v' R使用ModSecurity-Nginx模块来连接LibModSecurity' r6 N; ]# B' n
5 b& R1 | ? j! g+ I
& Y) B0 Q% }9 ~- N; i) D4 v" E$ cd /root' K9 x# S B2 P; c: }1 m- k
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 R' Z0 z) H6 n+ l, s$ tar xzvf nginx-1.9.2.tar.gz
$ w7 I# L# [% }$ cd nginx-1.9.2
R, M% y% A. m$ ./configure --add-module=/root/modsecurity-nginx
& s: o7 X! N+ `* l* d5 w2 f$ make
6 e" P! E/ T) J* ]1 ]5 B$ make && make install
- \, W$ |& E) A4 S添加OWASP规则! X, B, E3 q3 x1 n
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& N0 Y/ ^8 ~6 ?. B! x+ \% u# D0 Q+ ~% z7 T1 c) L
! C3 } ^- y6 X- c3 j1 Z
下载OWASP规则并生成配置文件
* S$ O A: H+ `1 K' M1 w" N @$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git8 a, ^; C/ r% v& n/ ]* [% [
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
+ }+ ?5 ^- s1 m7 H" i& t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 Q. b; z& t7 E$ cp crs-setup.conf.example crs-setup.conf
; L0 E& { U6 O* C! [配置OWASP规则# T- }7 M2 d0 f9 f0 j( h- a# J. v
编辑crs-setup.conf文件; P4 m9 w! {9 ?% q* F1 B, p0 }
4 M5 E0 f v. f9 I- j6 \; o. u
& F9 j. b8 P7 w3 Y$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
! r1 u- w; a6 x( j2 l. T$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( j5 _& l% I2 r$ _: I$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* H0 y3 s% E, w4 }: m
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, ~8 W. k2 O& b' n& @; J% C7 i
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。; X' b+ O3 e H5 y
G2 `8 t- ]7 l
+ ?% B/ O O, `- G) N7 z& Q2 t) k启用ModSecurity模块和CRS规则1 q- q# ]" K& c" q4 d/ k7 S8 U& e4 G
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 e% o. e+ x6 p
/ w' _; X1 Z" p" v/ |5 p1 b, P; D9 N, k
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ B3 Q% i7 c9 L8 e2 H. w3 c
( V# ~4 L4 J0 s0 _9 w6 r0 e5 m) S; z) t' V5 F* H9 x1 Z' m( ]
$ cd /root/modsecurity-2.9.1/' u; i8 x+ H2 F3 _/ T3 d
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) z4 D! K! K% f$ S5 C. a$ cp unicode.mapping /usr/local/nginx/conf/
3 O# u2 y% \) q1 ]/ B. q将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ P+ J3 S$ P3 e, [) j6 P2 U
+ R8 q5 I' n4 q5 A, C& e1 C g3 Z1 a) W0 f- m' c, a
$ vim /usr/local/nginx/conf/modsecurity.conf+ p9 w! ~ X+ Q
SecRuleEngine On9 M" q6 I: g/ y8 f h0 h
ModSecurity中几个常用配置说明:
7 o% A4 u; M; |4 Y6 p6 O2 \
4 t) O% c7 q& C% o5 _" Q) s9 g2 \" u2 |) W' c9 F" M+ J4 X
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。5 M# z8 t2 z% e& O$ I3 r
9 T9 s$ J8 R% C n
) h7 A- \4 p7 W2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, R: p, p- \" ]+ h! `$ h# ~
# G. T* n* P5 O) H. r" K l. O% z% g S- K* E: V- t
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 g! V7 u/ z- ^4 u6 h* l! ~
5 U- p1 f5 Y g' v+ w1 E
8 F( } Z! ~- u) }1 ^: E: S4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
. j) [) c2 U. v& d: i2 }9 ~% w. O
' _" k; n4 v1 ^( Q, V, Y5 b {
; ]% {% E: `! b: \ V* w7 h; l: @/ o在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
/ ~. e/ h9 n n( T( F* {8 e
- q! m6 Y0 v8 E: l$ k& m! O; N: }& D6 v( f; C/ s! R9 X8 C
3.x版本CRS- Z6 O& D. }$ _0 ?* O) G/ _5 H
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 ?3 L6 v9 V; I3 p: y
# 生成例外排除请求的配置文件* K3 x! N0 h% L/ Q7 a' y8 w
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
4 G) ^6 E7 h& Y/ |) o% Z" v# A$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
% ^8 L; ]7 t' z8 @& U, f/ O* T$ cp rules/*.data /usr/local/nginx/conf
+ ?1 e' w* l; |4 Y, a; Q4 _为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 {. P0 E# K6 D/ h2 l g
+ r2 m6 H2 \6 `- Z# |3 @% z( Q3 ?9 b2 X
$ vim /usr/local/nginx/conf/modsec_includes.conf) n3 D/ `4 I+ L+ P8 @
+ t4 A0 _) L- r& v( \5 q- f1 I[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
/ t/ o4 s$ a e' P: W8 J8 h# a( o' n+ w, v. J5 j7 R3 c2 j
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
- P; t# `7 L/ E5 G$ _9 Q8 p0 A$ y+ n2 F* t, u/ W
4 D: c2 w# N5 ~( P5 p3 W( d, h配置Nginx支持Modsecurity' `: u& C+ `2 F
启用Modsecurity9 r- K- G7 U7 c3 i, A/ V) \
使用静态模块加载的配置方法
. O I3 I0 X P) G# }在需要启用Modsecurity的主机的location下面加入下面两行即可:
& G1 P, ]! a( L
' `$ }+ S- e) R$ l2 T( [. U Y% q8 w
ModSecurityEnabled on;
& K8 L3 I4 q5 K# P- M) J7 jModSecurityConfig modsec_includes.conf;
% |; t& l7 M, ]% L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 H8 `& W9 R1 j0 T' ^ u
+ `0 L b) Q! C% K, E) f; x. B1 [- X3 T u
$ vim /usr/local/nginx/conf/nginx.conf% T" x# q: [( Z! G
. z+ h: q: f T/ ?- e ^5 D0 K* A" j
server {
6 _& J8 n1 [7 |% | listen 80;
8 D) f* G' @4 i ~" J6 _ server_name example.com; N- @. L7 W* E/ [3 J5 q4 L5 x2 ~
8 u- @: O# B3 ~5 T) j& H
% x1 l7 l% u9 i! G* g; u
location / {
4 S6 [/ C: ` Y1 b ModSecurityEnabled on;2 r) J/ {9 ^: Z# ~! A& f: y
ModSecurityConfig modsec_includes.conf;
9 w' {+ k$ H& T) @# a* X3 B3 T root html;$ F( ~, ?4 `7 g3 Q% G' K
index index.html index.htm;0 q/ t2 Q, m' f$ \) w
}7 N' L- [) u* h d
}
# ?" e) w$ q. |) _使用动态模块加载的配置方法
% H; n ?1 {; i4 t) j4 ]在需要启用Modsecurity的主机的location下面加入下面两行即可:5 g+ p4 }7 w1 B0 f
' s7 Y6 ^2 S- c! c( i; P' W/ D' h0 S: j+ }4 i% U
modsecurity on;
9 x# G! e/ H# i" _; v! f# Rmodsecurity_rules_file modsec_includes.conf;
5 a) e6 a1 A2 o5 P修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 B q) d1 l! o) x! I7 f# |
1 O4 h3 M' `8 u
+ G( e5 X2 v8 ?; o# o3 w$ vim /usr/local/nginx/conf/nginx.conf3 | Z! S9 {, B; u* T$ f' a: M
2 e4 h7 R; {" |
+ p: ^8 t6 S/ D6 ^) Z- ?4 qserver {' {& i. U2 Z4 `4 \
listen 80;2 Z+ t; t# m: r9 L. H2 S
server_name localhost mike.hi-linux.com;
" l! k: }' _9 X: I" i access_log /var/log/nginx/yourdomain.log;
5 ]+ p5 R3 X$ C ~1 ]5 Y0 v4 B% p- V# Q5 h% N" @
* l4 A* X4 P n0 g3 d+ e/ j location / {
9 c" z2 s. |* M. r$ F# g
9 {$ t A9 @! q6 V/ s' o# ]8 `7 U' U
modsecurity on;
. V# \5 p; c! A6 _; N5 K* P modsecurity_rules_file modsec_includes.conf;
8 p+ H b2 i5 g d E# P: u root html;
" L# O1 x, F& M. ^3 D" E5 m8 T3 W index index.html index.htm;
- A: O W0 l- x+ u) h}
9 q6 y* I0 V9 R% V1 }4 w) Q}
& N' }! N0 ]3 }% v9 J4 q验证Nginx配置文件2 }) ^ k0 u# q! S$ ~+ y% Z% u. o
$ /usr/local/nginx/sbin/nginx -t
% U8 _. {! t7 ^7 U- n" S- b- Dnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 S: d5 H3 ^& Y! g( ^
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful- F$ ?7 [' z! _* l1 ]
启动Nginx& {6 n6 f* w- s' ^. i3 u% c
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. U0 v$ ]. r( \6 @5 P4 I2 q
; [2 E; | g5 e8 Y& \0 j测试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能够战胜更多复杂/未知/混淆的攻击模式。 6 }4 s; O, G6 }& w' q: ]
|