找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12451|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" t5 Z$ h. k& x' `2 c$ J0 E2 O0 G9 M4 i9 Y: o- q

' v; D1 K# Q- M$ }在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。% }! z/ i; [" ]  L

* a2 N6 @% i  D' f* `, l/ Z: x" l
什么是ModSecurity
5 }; b/ c" Q9 k4 t2 X6 iModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
9 ?! m# d* E! C% G; V- e; t7 u/ o, W+ v+ ]2 x
' B6 @% \) S2 L) N
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
' R9 W, e$ y+ z+ J2 K& D2 j: a) q* B; a8 l5 a: m% Y) u/ z

) J* G/ @2 g4 t+ C# aModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 [! n4 h( G, h  ~9 `6 H7 B
: ~" W1 V  t7 O8 U# v% A

* o" m" c, B5 s官网: https://www.modsecurity.org/% D. f; n; H) j7 T1 k9 R# |

' ^- J& n1 h2 i# [* u' M% d
9 q6 i. [) {# k" z9 g什么是OWASP CRS( B4 b0 J9 k3 Y( [" N' r( P1 l  w  t1 G
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
2 z0 ?8 _+ A3 }, D; f" t
) e) E7 Y! G+ e8 L. |
' ]# ]) P' ~. @! _ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。) c' k8 Z: _) d2 Q4 f/ `1 I

* ]2 z/ z, a# \$ o1 [* p8 O/ T! ^, B6 s" B; n
HTTP Protection(HTTP防御)
3 f& S; O8 G( e, QHTTP协议和本地定义使用的detectsviolations策略。
* t6 D" Z( R# D7 r9 ~2 }. W3 C* S* [4 F% [

" b6 t: G7 x- c' KReal-time Blacklist Lookups(实时黑名单查询)
' J0 D1 s+ V* c: W: t6 d利用第三方IP名单。
' Y) ~2 a$ z" k/ V% |4 }# F$ n- \4 O9 h& ?' A

8 n1 a7 G6 }+ G+ ~# IHTTP Denial of Service Protections(HTTP的拒绝服务保护)  M; B( ?- Q5 H" H. D/ h1 `$ u8 l6 @
防御HTTP的洪水攻击和HTTP Dos攻击。- D" m: n8 H! ]2 [3 L
" @9 ]3 Y4 M1 e7 B
/ n* J0 q, h0 e9 t3 m% ^- ]4 p
Common Web Attacks Protection(常见的Web攻击防护)
& x1 u* z! h6 k8 H检测常见的Web应用程序的安全攻击。
/ D/ ^# S% ^) _8 X5 Z# K/ P4 c! k3 `. H

$ c0 Q7 {! P5 h/ tAutomation Detection(自动化检测)
/ w. a( Y8 Y  ^! o' r. r检测机器人,爬虫,扫描仪和其他表面恶意活动。
: b5 T2 J% t2 x) E/ T$ S( r6 l/ R2 V9 u" U7 ~( l

1 p$ S  c" }8 a( k& r; NIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)4 g8 L- q3 s; R& O4 M  a% l# F9 w
检测通过Web应用程序上传的恶意文件。
" I5 t( H  x( i, N: g9 W& U' t
( e/ N  S! f$ L; B' T+ \: }( R1 _5 P$ ?0 q/ G
Tracking Sensitive Data(跟踪敏感数据)* O4 f3 E; c: Y3 Y: |% e9 I
信用卡通道的使用,并阻止泄漏。! I0 ?1 d9 P0 p

& @6 e# a5 A) F7 O; u; M
3 t, D% Q9 h5 E6 \7 [+ r1 wTrojan Protection(木马防护)9 g+ ]9 r% {( R1 h
检测访问木马。1 U$ g. a+ y4 U& {. A. b

) q1 ~: ]- @$ \! a/ [% S0 ^( u  I8 O+ Z# j- f( B
Identification of Application Defects(应用程序缺陷的鉴定)
7 c# U( S) q( Q8 i% Q检测应用程序的错误配置警报。
. M9 P" ~) `* r, s$ ]. z! q# v5 X5 U

' D" U8 f% R* d* NError Detection and Hiding(错误检测和隐藏)" m4 y8 Z2 [# [2 ]1 p) |
检测伪装服务器发送错误消息。( I1 z" Q5 H+ k9 r8 E# b# C
3 N3 S: o9 |9 M4 D
* S! n' V3 r- t! k
安装ModSecurity
; g( t6 V6 l4 \8 \" i" f9 [软件基础环境准备
4 A8 k# a4 u' l( h# e下载对应软件包
9 K) X5 r4 A& v) {7 V$ cd /root5 e  ~1 z9 w& ]
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', d) A7 e4 y: n
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) H- b3 Q3 C5 t1 Y6 \! X安装Nginx和ModSecurity依赖包
8 e* Y3 e% m- R" ]( D3 h  NCentos/RHEL
" k# w. w$ D+ V# Z
" M# l3 p5 }& B1 c; w) Q0 w. h$ s. b$ G* A
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& S4 r* a' Y" J6 o3 Q/ {0 T
Ubuntu/Debian
5 D) z& t+ b5 X* o9 @/ X$ ?+ V' J9 o
% r9 _3 w. U) P! n' @, R& \4 a0 n" U& z
$ 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
5 a0 ^' f3 ~7 [( `5 S1 r编译安装ModSecurity
: W8 J6 P' p6 k" C, V5 hNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; C; v9 P; Y8 n) f  |

  n* s1 t$ S; ]" p; L2 \
9 S! o* S2 ~3 \6 G方法一:编译为Nginx静态模块
% y3 a( Q0 M+ N- g3 C* ?) D6 ]" Z- k. ^4 B$ ^$ O( J6 G6 x) l

$ D! [+ \/ q9 Z9 C) d编译为独立模块(modsecurity-2.9.1)% j% ]/ q( H) |/ a  k
$ tar xzvf modsecurity-2.9.1.tar.gz- r6 M3 z+ Y" O  P% o
$ cd modsecurity-2.9.1/
' i4 y) G0 u9 I/ _7 G& a. k$ ./autogen.sh* A/ Z- Y# y  U( f" F& B4 n2 W
$ ./configure --enable-standalone-module --disable-mlogc0 e! ~. N$ u/ k$ C5 ]# ~/ M
$ make
' a" l& V# U2 K4 s: h编译安装Nginx并添加ModSecurity模块' b8 E" o; o  G% r8 Q$ C- z
$ tar xzvf nginx-1.9.2.tar.gz
, Y9 p8 Q7 x8 e$ w5 W8 z! [8 K$ cd nginx-1.9.2
( Z% c1 e  Z/ B# U- K  c# C; d$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/8 h7 f$ D: I/ q( H9 g
$ make && make install. ?& f1 v. E9 {
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块: k7 D; O5 _/ m/ e$ S

' @* B- j) t: G9 R, A( {" |( u: M6 c! N' R. W/ N
编译LibModSecurity(modsecurity-3.0)9 p! z2 \: o# ]$ x) t! U9 ]
$ cd /root5 O, |5 k: V/ J/ T( O. S% K
$ git clone https://github.com/SpiderLabs/ModSecurity1 U& C) r* X# F) Z
$ cd ModSecurity* N. j- |* P6 x0 q0 o6 n7 i( `0 V
$ git checkout -b v3/master origin/v3/master4 [7 s% D2 u8 j' v& e
$ sh build.sh3 C# ~  _3 K. G6 c
$ git submodule init
3 J- e. e; y2 n. Z  l; |' ^$ git submodule update
3 f- M0 n4 [5 X: k" U& R; _) h$ ./configure
* J1 V& S5 ^  R- T$ make
9 ]  `- z  ]& o: y6 N" P$ make install
* I1 K$ M9 ?& J  M9 vLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 k4 b/ G& L' z
: D! u0 {; i0 T$ j. L  Y# I
2 D4 ^  N8 R8 U9 b. N3 F$ ls /usr/local/modsecurity/lib
" f; {8 g! D- n8 blibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
+ w7 S- }8 _( K5 x编译安装Nginx并添加ModSecurity-Nginx Connector模块: |! _; i) V8 {; p  q
使用ModSecurity-Nginx模块来连接LibModSecurity
7 f4 z7 N: n! j  Z2 x
$ G$ q* \. F6 ~( i; |4 x8 Q" c' ^0 q' f- @9 q, f* m
$ cd /root, k" `# \# l1 L
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ }9 r% d' f8 h$ N1 d
$ tar xzvf nginx-1.9.2.tar.gz
0 L* s4 s6 Y) I' @! b* r$ cd nginx-1.9.2
$ d9 T; s. k4 x; V1 Q$ ./configure --add-module=/root/modsecurity-nginx
; P- v7 Q4 F2 s" M% l$ make
0 Y" Z. u5 w) T$ make && make install+ O* @8 R& X& c' D$ h
添加OWASP规则" {, {5 @: {/ m! M' @
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. ?8 c6 D' _. m2 F9 M& v" h; q! v# _1 ~5 [, |& _; L  t1 `  u

& |6 a2 y. u/ I8 |$ ]3 {" L5 p5 q下载OWASP规则并生成配置文件
6 n% V: H. E' h- O4 X# _( {' ^5 p* L$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ C' G# t8 u$ t
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
- Z+ a( G9 k0 V& j$ C# s$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" n" r8 Z7 b+ V. G! M3 ?3 Y
$ cp crs-setup.conf.example  crs-setup.conf9 C- f7 Z1 r4 B) T* U5 A
配置OWASP规则  ~' v4 o4 g& w/ n& L: J
编辑crs-setup.conf文件8 @5 n8 W5 ?! f% M8 q) I( ~/ N

" y* w+ t% E: |/ @
4 C  l" @" s+ t9 |  e4 @) N$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf% O, f# g% o7 Y  l0 L9 t4 i) l
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. u# c9 M7 ?- l1 x( R
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
# k2 S  j' F7 e/ W/ P3 X$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 Q5 k7 ~, z) ~* J默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
& b+ e$ }+ Q$ {- @- n3 D% Y' |( \; E3 ~" K9 r0 e$ v

3 ?/ J$ z; Y  ^4 z3 v启用ModSecurity模块和CRS规则! n+ Z8 X$ t6 A5 _
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* ]8 ~: q; [0 H: A5 u/ }

( g( e7 Q) F/ ^' s2 m# U2 R2 k4 r2 Y+ n8 F: J  T
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. Y1 I! v' k# g2 V
8 o8 J. h  d' h* w( x$ P8 t3 C# v! [9 M
$ cd /root/modsecurity-2.9.1/
5 z4 o5 x5 `3 ^$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
( |; v: e2 T0 R% p( }7 w  a' W$ cp unicode.mapping  /usr/local/nginx/conf/
+ E. c2 M6 c& w4 N将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
' ~5 o% `( L3 X4 {
. p2 a0 i5 p- I- Z* _! e8 I% K+ D) A6 n5 A8 @
$ vim /usr/local/nginx/conf/modsecurity.conf* N" w6 o9 T0 S6 L
SecRuleEngine On- J; Y2 |1 _# o; U
ModSecurity中几个常用配置说明:
9 U3 \0 I$ @- I3 m( A# \# Y
4 s3 J8 f8 p" B' k- u$ ]/ F' t7 c) F4 G' u: Z; c) L
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。4 b3 V$ d, G9 l# G2 {

! P$ s' a9 G1 `2 ~9 ?% d. N
& _+ I4 i% B" P( F. R2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。* Y! f4 d) I: M2 L! [+ Z/ d

. `9 J( }6 S4 b5 m; ]8 I- Z0 [5 w7 H  \, i' S
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
4 ]! r! o- m, |: V
% |1 {6 l; Y9 x% y1 i1 k
! }9 X0 g: W) X# P0 X4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; D' x7 _* k: ~2 X, K
  E* V* K, X& ]8 F$ f! Z* U8 R- Q. b1 D* H
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; s9 h- }; n5 s/ m' @; r! ]% Z
& E  Q5 i- H. K1 Q, k: K
6 ?3 L8 N( Y$ t+ ?" ?3.x版本CRS
% L  n1 ^, {' o, {$ [5 E; ~$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 C6 \0 y  t; C! {( E+ X, h# 生成例外排除请求的配置文件7 ?- y9 j. D) `) U
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf9 o+ J+ T- I  W, R: U# [- J' `
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
, h$ p" r) g( b. S% z) S/ b. H$ cp rules/*.data /usr/local/nginx/conf
  e/ `/ D# g: j! X1 Y# @为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。3 I+ O$ X( j5 \+ o: H

! @  u: p% _' r9 K
9 o. e! f9 M# X$ vim /usr/local/nginx/conf/modsec_includes.conf1 M" j1 }: m2 g9 z
* G" \9 H+ b3 `0 @" D, P* o
[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
, M; w1 q( a/ Q8 q

* h' ?" w% _1 U7 K* x注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
8 m. s* x# X+ j- J+ e$ c
) T* @* j) U' O3 e
1 [$ b6 P$ z$ Q4 z+ i, B配置Nginx支持Modsecurity. u4 _" {# ?7 j4 M, o8 J, |/ z8 u
启用Modsecurity
" V! e- C* j( W  j  p/ {6 w5 E/ `使用静态模块加载的配置方法
/ ~6 p& r7 H  z6 \3 e在需要启用Modsecurity的主机的location下面加入下面两行即可:
( E! R1 \/ y) g3 j: Y$ L: n2 A: A8 ~% V

1 l2 D$ j) G! |7 BModSecurityEnabled on;3 F( i: N1 y1 n
ModSecurityConfig modsec_includes.conf;9 x7 [. K4 f5 n% Q$ s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 |  K! A" W# b5 c8 R3 o0 I
$ j  ]; E- I% K4 \* o4 n# o6 H" b8 d
$ vim /usr/local/nginx/conf/nginx.conf
( D% B0 L0 D0 O( l" q: E6 [* O9 r7 X6 ]; C! G

3 l1 @; K; }) C; u- t2 u; k5 nserver {& U% v0 I* k; V
  listen       80;6 A5 H# Z% O+ ~( c" A) B- _! q
  server_name  example.com;% R) o+ O3 ^! T& j; Q  R& O4 }9 u
3 r- C& ]" {$ h2 i" ~( ~, `

: @# Q. o' Q# I! U7 X3 |  location / {0 T5 p2 b" _7 v" |/ d: S# m4 f
    ModSecurityEnabled on;6 s' r3 C/ i! P6 e; g3 h" G/ V
    ModSecurityConfig modsec_includes.conf;7 s& A- ]% \) F" A$ K/ g
    root   html;
! I4 {) I+ q! C6 t    index  index.html index.htm;
) Y4 z0 v! W6 B  }
5 p$ ?6 q2 k( u' m; l}
9 B/ M% c; j1 c: G+ h使用动态模块加载的配置方法
8 k5 P1 d4 s% n( {5 T在需要启用Modsecurity的主机的location下面加入下面两行即可:
; b4 ^( P# [  t5 {( m% h8 |# J; }& p& @: S* m* C

8 O, t3 ?3 z. U2 p/ r+ ]+ a) Umodsecurity on;
0 H( \4 i4 S8 p, E' Z+ b8 X8 kmodsecurity_rules_file modsec_includes.conf;! }% x2 V0 c+ U( C
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 ]1 k' {/ ?9 \/ f2 `) ~
( R$ X0 a1 J$ @
3 B6 j7 ^' I/ a; Z# q4 a3 A; c$ vim /usr/local/nginx/conf/nginx.conf; V* c+ _$ _& H& N( [

5 W* j  P6 c2 V7 u3 |$ q, r
( q# N, V+ i, O  a3 K& K3 iserver {
6 h# u+ @7 J) u  d  listen  80;( T& f9 F4 K5 [. G; X0 j! |1 D; I% r
  server_name localhost mike.hi-linux.com;
( V5 A" V' r) R  access_log /var/log/nginx/yourdomain.log;
0 U. s; t7 Y8 [0 A" l: w2 `' h4 A" ~% A7 |/ [

" V8 D. A1 M: M+ ]* P4 N; I  location / {( l' O6 x! W8 h' w9 `* W( C

) Y! }6 q1 s# }' ^0 E4 Y8 q
" w/ ~  H5 O) e' l; J. Z+ I  S  modsecurity on;* X! D: ]! l+ F# u7 B
  modsecurity_rules_file modsec_includes.conf;
4 p4 X# f, Q) x2 V1 L; W8 S  root   html;
! \0 o* K! m: G& [4 e4 g  index  index.html index.htm;) u+ J: h+ c" M* K3 s3 K' G; L
}9 }7 n0 m% u9 W1 \& m6 H
}
8 x, f, x; z  g+ ~# I验证Nginx配置文件
) G' Q, m+ A7 Q  `3 m& Y& x' C$ /usr/local/nginx/sbin/nginx -t
) K/ G# [" f+ [nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
. B2 ~* s6 A2 p8 `  N% L1 \nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) v2 ]' Z5 W5 Q* r3 t
启动Nginx
8 o1 I& q9 h! t1 h: A, ^$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf) \& ?: }. o: ^

/ m, q6 y% M# m* n/ h1 X7 F

测试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.log

Modsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。


3 T9 e: J4 B5 D, _% z

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-9-28 05:02 , Processed in 0.083497 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表