找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12705|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。) v: L$ _3 }* K( |% ], }
# Q  B% F- a, T  O
; y/ G: @. A7 Q0 `0 v& p5 c/ j
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
  y; r- r8 ?# q6 @3 y# _( o2 V2 |! M' O; B$ U: v/ A! `. D
: k' C4 c4 d/ ~* T! g7 P
什么是ModSecurity
- P4 W) |- K5 S, J5 j; P$ eModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 {6 T+ P: d+ ?2 r. u
/ K! s) `  Z! a* A+ U$ W; }
, G% u) \4 U6 ~
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) g* a' I1 Y* X' S
" o, v1 M( u( p7 N# a) j2 l2 l5 g7 d' ?- f: h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; |4 O4 m- t8 u2 H% H; e! G+ g
: h9 Q& P2 U8 H! Y9 R& Z
官网: https://www.modsecurity.org/$ W, [" W/ P( T, @$ A* b; K
( @# l+ d" g4 I) c3 j: M

3 ]+ M: T1 `. E什么是OWASP CRS
6 s7 w/ E8 e! V. C, EOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。' A  Y; ]' d; p  y4 N/ E

6 Q. t7 x2 d# b3 {/ }5 P" U+ E2 V2 z9 Z- X, z( Z
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。" X4 S) i, O! I/ @. J  h- ^) V
" d$ B! `+ R( T3 a: n

1 {8 X% Y/ ?& |6 T  h8 A, w  lHTTP Protection(HTTP防御)
; U& w; N8 `. A% k2 ~HTTP协议和本地定义使用的detectsviolations策略。
1 [7 U: y" H$ C$ ~8 Q: p9 n. n
& h' O% V6 K9 H, i9 d- A; h6 T4 v* {/ K, b
Real-time Blacklist Lookups(实时黑名单查询)0 r4 G8 O$ T$ }* A7 e
利用第三方IP名单。
5 o9 k$ p& M* n' j8 N( ?! k
2 o; f) u) N2 r) V1 H% U6 n% Q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
# ^1 G0 P3 `$ [8 o, P' r防御HTTP的洪水攻击和HTTP Dos攻击。
) v0 m% u  a! z: ^* b  T% T5 @! V! Y& B' ?- p2 l- K% S0 @

: E' q2 w+ Q0 S0 C9 x8 `; gCommon Web Attacks Protection(常见的Web攻击防护)
: V9 C7 P2 Q4 j! B: z3 b检测常见的Web应用程序的安全攻击。
% {% R5 v( n2 c* m: y
1 l# R/ Z  q( H& m- L8 h7 \1 b8 X1 R5 J$ E
Automation Detection(自动化检测)# I  X7 W5 s0 Q. I5 M
检测机器人,爬虫,扫描仪和其他表面恶意活动。+ O9 l. P8 J$ O2 ^6 c7 g% u

, o, f' R6 g% R# B( P8 y
7 W' N/ d  M- x( e! G5 B% ~/ tIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ B3 B* m- [8 m# H; _/ O+ `& c
检测通过Web应用程序上传的恶意文件。
" Z1 p) R7 z5 d$ i& H5 [& P
8 k; \* e0 \3 b6 L% y
/ w# Y) p3 G/ I6 j7 L. b$ p$ ITracking Sensitive Data(跟踪敏感数据)
4 V. b, P6 E& E3 G# n+ F# m4 h信用卡通道的使用,并阻止泄漏。
: J* G8 N, j/ r2 I4 \& I7 [- X
: J" C5 |! T" _9 o/ t1 t4 S: @8 V8 I  |* f* p8 A: `# @
Trojan Protection(木马防护)- b5 S  U/ X; C5 K" C
检测访问木马。
' v% u( |/ y  `' B' f- B3 m
& N0 i8 P1 L# x" y
( {6 s; ~; e2 r; b( P/ V1 ^Identification of Application Defects(应用程序缺陷的鉴定)2 K# Y$ J# L7 S% h: J5 [, t8 J
检测应用程序的错误配置警报。
% m8 P6 R* l0 Q% r
: b: j( S! h2 O5 B% ]# T, N8 [1 W4 n6 y4 V( r% @' w
Error Detection and Hiding(错误检测和隐藏), a/ j8 |2 ~7 k! K- K7 ~; B
检测伪装服务器发送错误消息。. X: X. L5 ^( q0 `
+ a" G4 o% M% l5 W# T0 E

& `5 C$ K0 P2 x# k4 Q# q9 L安装ModSecurity
) j( B4 o8 s; \* p* w* F2 C软件基础环境准备
$ u" C( d" W; ^, i! O下载对应软件包7 m& X6 q6 p5 }* Y+ E+ i: H; Y9 N& c
$ cd /root
8 n& D7 h' y7 f  T4 r, Z9 w$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; V) H& T. b4 f. A$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
$ ?0 I0 `# e& l安装Nginx和ModSecurity依赖包
+ a! b. N' K  W: e' T8 uCentos/RHEL( j" D- p) m) R. y! r3 L
8 A* ^% Y6 h  [# b* n8 w
3 E- V' S& @$ G' G  ^
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel! f" C% j3 ^8 k! X1 [
Ubuntu/Debian5 C( x. a% q8 M/ f2 P( {; G/ _

# E! t- G, p- U: T9 ?1 J# d) s% R0 G; _# n- @
$ 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
4 T1 N3 s' E" x/ E+ a编译安装ModSecurity& X% Z2 \) e6 q- r8 k+ x. }' S" ~
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。8 L2 i* c( `- @6 Q3 H" W  C4 q
# O7 C6 B- b% e" L( g
! l' J4 O1 f* v! W" E5 a  x5 l# q+ f0 U* U
方法一:编译为Nginx静态模块( Y8 t1 y  @8 j& K

8 W+ J' N" T8 Q" \: P7 i6 ~
6 s2 M0 m* Y# K编译为独立模块(modsecurity-2.9.1)
/ [( e) \  F! Z( `" k- \* M2 B4 U# b$ tar xzvf modsecurity-2.9.1.tar.gz! f; Z* T) X2 h: U
$ cd modsecurity-2.9.1/
3 F) J' P2 ]5 I( c. ]$ ./autogen.sh
& D" V0 X7 b, Q$ p+ ]  V0 i! O$ ./configure --enable-standalone-module --disable-mlogc5 H% U) ^. D0 r
$ make# k1 N7 q3 g! l; ~5 N% q
编译安装Nginx并添加ModSecurity模块: t; L2 \- V' a" h$ E
$ tar xzvf nginx-1.9.2.tar.gz, m  A9 S# r* t: z
$ cd nginx-1.9.2& L& L. b! O( V0 C% L% D
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
) u, y' d# ~! E7 _6 c5 X+ p5 s6 g( \$ make && make install
$ K+ t% j* ~4 i, [% G方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 B  P, J7 \* i! t& A
- l% _/ i* H7 P8 _2 O* c) r" q; k. |" ~+ E, D: l% P; w( m
编译LibModSecurity(modsecurity-3.0)
' {+ e' F  l' x2 V# L/ }) R$ cd /root
; ?- a& v  ]; }  s$ git clone https://github.com/SpiderLabs/ModSecurity
) G4 o8 J/ W1 L# R( u5 _, B$ cd ModSecurity; ~- d1 C" b2 W2 |( z
$ git checkout -b v3/master origin/v3/master: k+ @6 v. R& P& `. H8 [( k' m" B
$ sh build.sh
( |% d1 r- u1 F% B! X* O$ git submodule init/ B0 k* L$ S# R$ i3 B+ E
$ git submodule update% C' V1 x: U# y. T8 i% l7 w
$ ./configure
' `% U7 V# I1 U! u/ e$ make
* F. d, j# G5 @, p$ make install
6 n  ^: u1 f8 Z) |$ ~5 ZLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。) z9 d6 k& i& P! N1 K9 @/ u5 f

5 u* o. [7 Y* w) }# U2 o% l7 g) t! y0 z* f) q6 g# ^+ x
$ ls /usr/local/modsecurity/lib: M3 R4 U7 t3 B' T+ a% p  G+ m
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  C& S/ i( ]7 @0 x. O, A
编译安装Nginx并添加ModSecurity-Nginx Connector模块1 W* F4 l! G0 t% z
使用ModSecurity-Nginx模块来连接LibModSecurity
# J2 l  e/ \/ F3 W( J4 S  K. U$ H: S: T8 G# i

. S$ D; b( m1 A( r# Y& l2 v$ cd /root( e9 h/ d( E# B6 t
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" o$ _0 m% _( }
$ tar xzvf nginx-1.9.2.tar.gz7 `  p1 ^  u4 L9 G# y/ h
$ cd nginx-1.9.2
  Y" U3 \  [/ p) t' u% n! r$ ./configure --add-module=/root/modsecurity-nginx
5 h9 k% q5 H' \- @% e5 X! C8 v/ E; @$ make: A% [$ y; @: O% D; a* V$ W
$ make && make install
/ H" C; ?% f  U1 g$ X. U- D: U添加OWASP规则( [( G3 A; B4 ?. [7 L" `; b4 @
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 S5 x% y2 p9 c3 W5 ~, }: g/ w. j2 I  `

; h' y! y0 J4 w* m: m: p- @$ @* t! |9 i下载OWASP规则并生成配置文件
2 V! ~9 b2 ~" @3 [$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ l4 C% b. r/ I1 w( _$ O2 L$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/0 k9 Y6 M$ {7 C. T3 t# D5 @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs# j, {( I3 j8 x8 C. w: n
$ cp crs-setup.conf.example  crs-setup.conf2 c2 g- G) h* Q- ~/ B6 w
配置OWASP规则: `0 N8 k! k* O0 P$ ?" x
编辑crs-setup.conf文件% M' Q1 d; V9 J

  d( e3 N5 e5 ~; t: r" {! w" Q. ]- S+ \6 E. K! K5 q
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf$ f) g4 D" C1 Z6 |, m5 z8 G. A4 S
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
4 `. t, Q( P& h% z5 l$ i& F. F$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
8 m2 y+ i3 ^" A2 W+ r; Y7 Q0 Z$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf  ?1 S* j6 B. r7 Z# G6 h) P
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ ^+ o# _2 d6 B, q

& h4 @' ~/ Y  O7 q
, N7 g( ]8 C" y: j! Q! H8 F启用ModSecurity模块和CRS规则
' m! ?" X# j# t复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, P1 h1 E' U  i4 e$ W  U3 A; a) }+ l+ h! n/ y' u" t
. ?# Y1 Y0 d. a
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
; w; V8 Y+ t4 F+ J5 s8 b3 R) q# ], q* u

7 Q6 q9 e. C) R/ g$ cd /root/modsecurity-2.9.1/- n; \$ B% O* z9 D. p. D) N
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
  s9 b3 l: _9 K/ u$ ~$ cp unicode.mapping  /usr/local/nginx/conf/# k# r2 Y) V, ~3 S* B6 Y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
$ ^) F8 b' X% |! B+ g' V! t4 V- \( ~- r8 `; v) t1 L& z+ l! B
& x: u6 _7 u7 z4 v$ O( N
$ vim /usr/local/nginx/conf/modsecurity.conf
; F  X, [% l$ f; k+ j; V' wSecRuleEngine On* m+ K. x2 ~5 y6 g, R, z9 G
ModSecurity中几个常用配置说明:+ l- l" F" o: R3 m, f

/ W% h5 H7 t. m$ l! ?$ T
. @! A( S/ D; G. b1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。' r7 H; i) D' U" Y
6 t& ~; l% D4 y. P9 L  g7 b

6 N& t0 a, z4 l. q8 `2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  i! c, l/ L6 n( X' Y4 X

- j) X$ v- C6 j' f' R8 Y6 w# M7 X3 i: x$ E, c5 g
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 n# O) {- E" Q& C
/ r8 _: M6 t" D" A9 v1 O

4 X2 Q! s. O6 d4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 L% b9 k5 f% T( j
% r, g3 E& [6 z4 t
( q% ^. E$ E/ _* Q在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
/ J4 ^; Q  q3 \- w% C# f+ n% U& c% n+ F$ ]: t& ?2 c
; }5 C: t, h3 q% t1 C
3.x版本CRS5 }, S! J* X% F6 g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ G8 `9 d5 G+ L4 E5 [. D1 L
# 生成例外排除请求的配置文件
2 K5 h. d0 O/ J: o& M2 r$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf; ]. }& T8 t* h$ N; D& I9 x
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf: L% S+ h! E, H! Y8 u& q
$ cp rules/*.data /usr/local/nginx/conf
- @9 H6 s% x& O9 n8 d/ ^为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
( n1 W  r# Y: I4 c% ~1 L9 v5 D6 }8 {! P. O2 g, l& h# Y: p. R- y6 V
$ d& N1 W2 K9 [8 W
$ vim /usr/local/nginx/conf/modsec_includes.conf
* h# n9 J, ]8 b7 d2 o+ y% Z3 c+ e8 n  E. c
[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

$ f: ?8 g3 @5 e8 A
6 c8 e# N- [, S3 l. [注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 i" }- y3 ^' s& w/ t. G) q6 e" v
5 V. R3 B& q7 X1 s
' c# O: m/ b- e: }. j! j+ E
配置Nginx支持Modsecurity
$ U/ o0 O- B6 x& A# O启用Modsecurity  y% w! P% c7 H' J3 n+ ]
使用静态模块加载的配置方法
+ o0 G4 j9 e$ |在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 p) c' k, f' x+ W3 r
% b+ a. d- f: ^8 n+ U$ U1 O3 L. v0 b0 v& A5 n6 a/ Z% D
ModSecurityEnabled on;5 c0 r5 W; h/ i$ R& `
ModSecurityConfig modsec_includes.conf;
7 L4 g! n/ B$ e7 u) p  ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 M& K. U! h3 u5 H2 o

( n- M# q( |/ {
* C2 l: ^$ @, M( n: a$ vim /usr/local/nginx/conf/nginx.conf
0 N2 U' F! W$ p# ~5 ]
. [0 r3 M% [; X0 @# o3 _6 b
! b- l0 m( J) J- l( D7 Qserver {
$ S% M4 G2 p3 a$ I2 p5 L  ?  listen       80;
. h, U& }9 U7 G5 Y# W  server_name  example.com;
# t/ A7 \' ^# e* y/ R7 P' M- x! d) C" v4 @
) H+ i9 o* o9 k# ~5 E, u3 x5 m
  location / {
2 H* d$ U+ p% f2 w" K( Z# X) N    ModSecurityEnabled on;
0 }. C2 y4 B# Z2 O* K( p    ModSecurityConfig modsec_includes.conf;
1 _% N5 }9 Y- {! e    root   html;
* W2 p6 s- |$ ~7 s* W9 m8 L/ T    index  index.html index.htm;
4 X% L$ w5 H; X/ b  }
( Y. o1 x0 g9 Q7 b}$ |, q* ^& _+ E5 N% Q
使用动态模块加载的配置方法
0 `7 B2 J9 z2 q* n; h在需要启用Modsecurity的主机的location下面加入下面两行即可:) v8 J3 C  F+ v3 z

4 E' y1 F% m  W
' i1 Y6 M7 T3 C4 j" a+ O* ymodsecurity on;. j6 v3 _4 ?0 G: O& K
modsecurity_rules_file modsec_includes.conf;
( m' }, c9 k* L7 p" ^! u4 a- f! F修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; C0 f% D8 S; O' j2 V

! S. y! A2 `4 v! k- e) S8 `5 r+ d$ L( s- T4 W
$ vim /usr/local/nginx/conf/nginx.conf. d! x1 H! _4 ~7 K+ Z  k/ Z# \

+ V- ~  c7 q$ \9 S+ f& k- E- S' f* {6 R6 \: f
server {- D' E$ c! k5 \
  listen  80;# T! f2 n( r# N0 o% Q
  server_name localhost mike.hi-linux.com;: ^; T0 |* A% y9 }' T, y, B
  access_log /var/log/nginx/yourdomain.log;$ M( ?" T7 a+ j, m8 T4 o# {
; d7 }  G' L& n- p, |

4 Y$ |; ]! B$ R& D& x* Z% d. {  location / {4 B- U# @5 ~& ]; ?2 \/ U

0 E0 _0 f& y; w- j; V+ S/ E; b$ K2 `' I) `/ K' {( G: b
  modsecurity on;: W- r; F) M8 f3 C. E5 ^9 I. E1 }
  modsecurity_rules_file modsec_includes.conf;
! [' _) b+ @4 t" i9 |  root   html;1 H% E8 o% f9 ]; i) M* `. y6 }( J
  index  index.html index.htm;# k9 _7 x2 E" D' r; ]
}/ c3 q; a. o, y, R! z
}
! \. F8 g7 s+ b1 n验证Nginx配置文件
: W8 U6 A1 A7 r' Z; J$ /usr/local/nginx/sbin/nginx -t
& {) u& h  U1 W  E0 X1 [" hnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- T0 G6 S$ _- u9 ^, V0 U8 f
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ H2 }5 O+ E& a/ y+ U" x' J
启动Nginx
% c5 j3 A6 K* P( d$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 i" L9 ^( w' i* @
" d9 _4 I- Z; g+ A+ l* P

测试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能够战胜更多复杂/未知/混淆的攻击模式。

- t% z! B1 b/ v: \

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-1 10:45 , Processed in 0.074644 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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