找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9997|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ C; r% T4 P) T7 M- \
( ~6 ^6 D( ]# [% t5 a8 P( Q
& R8 q: i" F. [  n( T在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
5 V* _0 c; B( }$ i  U& k4 M( x! |* F) ]8 p, x% o
5 W$ F7 i# z- U+ H, B
什么是ModSecurity# I+ F; j$ ]6 R9 j5 f" a# x3 i) r% Y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 p9 P( g1 S) Z5 ~0 l4 b- Q0 I6 a9 P
' H% C3 Z: q7 o
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。: \- X' S( l/ s  E  c' }
  ], K  z8 \9 o9 \# B! C" @6 \4 P
! L( y( v( z2 M1 S% R# _
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
+ j$ O8 h8 G2 f2 ^5 w0 Z, Z2 ?& i# v2 ?! d( D3 a* ~( {' ]' \
' [! m/ D& l0 P* S- Q
官网: https://www.modsecurity.org/
' H6 X) x0 B% H+ Y1 m( n; U8 W7 x) X) L
! A1 t5 V1 e, L* f# t, n
什么是OWASP CRS- j8 e( ]8 ?' R/ m9 ?4 Z( C, u; s9 B
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
' s: E* ?3 Y& c/ d. R0 d0 V, B1 k: _. y- J

6 j0 g( @) o6 d1 QModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 X8 f# x# m1 Z; Q. F/ M; f3 {1 H: m' ?- R, Y. g6 [
, E7 B. A9 ]7 h$ g
HTTP Protection(HTTP防御). j. R% F& M6 J+ I" Q7 w4 A7 k, q& r
HTTP协议和本地定义使用的detectsviolations策略。
) O9 _6 @4 d4 l' [# f
; P  Q( M, R0 D- Q, ~
1 o6 I( S( [0 A$ r# j+ n  Y  bReal-time Blacklist Lookups(实时黑名单查询)
" l7 |' B7 Y2 K; p0 A0 B! g利用第三方IP名单。' X# p" j: p3 `: T, o* ?8 Z5 q3 L

& j8 g! \1 c+ m" S+ _
8 g# }: q  R2 u5 v+ r/ @0 ]HTTP Denial of Service Protections(HTTP的拒绝服务保护)
/ G7 H0 i- }# h  f4 X防御HTTP的洪水攻击和HTTP Dos攻击。
5 S( i. q$ T) L& \' i, O. ?
& V; b' k' `4 }6 g4 H6 U+ n' D
* x( w% G$ r7 e) J( wCommon Web Attacks Protection(常见的Web攻击防护)
2 a  R  T. w' K0 R5 V检测常见的Web应用程序的安全攻击。
" h7 U; H2 ~. `' J8 a2 t' _/ t: z( b0 O" L3 F
4 x' {7 {5 [, |
Automation Detection(自动化检测)
6 T; m' C; H# Q$ W/ Z6 g1 W9 v. I检测机器人,爬虫,扫描仪和其他表面恶意活动。, S+ t, u+ z( f4 t+ ~4 ?
: N: d# |- E( V, B2 S& b6 Q0 E% u$ ^

) U; L6 Y0 G* Q3 w6 IIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ j% Y3 u- F$ P3 t6 V: c+ p& r7 w! S
检测通过Web应用程序上传的恶意文件。  O& o! x" }. g. ^
" k0 j; t4 M5 }- Y0 b: K; j5 t

. \, F" ^5 `* t% i" UTracking Sensitive Data(跟踪敏感数据)" T5 B7 f! c' a$ E: ]5 y
信用卡通道的使用,并阻止泄漏。
4 a3 @9 c/ F* P$ B
( X0 t  y* l7 ^4 @4 G! {4 X8 f+ n) D2 U0 c+ w5 F
Trojan Protection(木马防护). w5 M; y/ B& W3 A) V% H
检测访问木马。
! L6 C" H1 M* o6 p6 i% z6 @" _# f9 e% K& N6 ^  q  `

& W& d. a; Q9 l4 \$ e. ?) z' V& EIdentification of Application Defects(应用程序缺陷的鉴定)
2 n" v6 e3 O$ v. e检测应用程序的错误配置警报。) Z$ a' j1 X/ T* \: s
7 Q) J; O+ Y/ U% c$ N

6 I  f7 @0 y4 h& d: jError Detection and Hiding(错误检测和隐藏)
) S9 |% z0 h" N9 X; L检测伪装服务器发送错误消息。
9 H0 E7 p$ |: }1 G  ?
7 D: s# s: x: i! C( X  P9 k$ G, T. f4 a$ X: Y# G9 \# O
安装ModSecurity
, p9 t2 F" I: M) G软件基础环境准备* ^. J9 j  Z+ ]: }$ X9 G8 S5 p& w0 r
下载对应软件包
9 E  ?: K7 s+ R. P' V; s$ cd /root7 t5 G3 g% f" v1 M. ?6 \! J- t
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'; W% h: m: S6 P" 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 [; p# e: K3 x: }# n4 g安装Nginx和ModSecurity依赖包
* J3 v3 g/ s' E" r  e0 \8 B& cCentos/RHEL
" n5 x: \0 `9 {: V
' i( C# o3 \# W7 K. W& y* M5 z: ?+ [9 H2 M, `  n0 ^
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel* D: |) g# S. l+ }) [
Ubuntu/Debian
! H* ^+ x) O* Q( Y, p* N1 `" I
4 J8 X5 Q" ]! U' l  `4 ]* K) x/ K& |7 A- Q0 q+ t
$ 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
  U8 a- M" Y% ]8 f编译安装ModSecurity
- q0 a! x3 ]8 A* d- ]Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) W6 Q* z" D2 S7 T: M& X

9 y; s- P4 S( Y- O5 h
3 Q4 |  g, Y9 A$ Z方法一:编译为Nginx静态模块, t( x& i  K+ c& T
( j7 \+ Z* S, l- u) c4 M
# n8 q9 i, z6 F" a3 k; {
编译为独立模块(modsecurity-2.9.1)$ @) x* b% }4 G: x
$ tar xzvf modsecurity-2.9.1.tar.gz
) a/ f2 t4 l; f7 u3 P; E5 @$ cd modsecurity-2.9.1/
7 O2 b5 e* d! `/ S2 |$ ./autogen.sh
; G7 n! }5 o% Y8 F: t8 g, z& Q; T$ ./configure --enable-standalone-module --disable-mlogc
3 P0 X  I/ \; W( i. u5 r$ make
' ^" D  h6 r% Y8 T, X编译安装Nginx并添加ModSecurity模块+ u) t; Z" E: ], `
$ tar xzvf nginx-1.9.2.tar.gz
) \4 s1 }- v& n$ cd nginx-1.9.28 E% w8 n; `' E9 Y: v5 v4 ~
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; p  F3 ^8 d6 I. ~. h  q. @) U0 u$ make && make install
0 m" v/ E( j8 h8 R1 A: o方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; N" P' R$ p0 Y% Y4 d: x  ]0 O
7 K& B6 _+ e; N) y" z: R
" p% N  T. F, S0 u% R+ g- q! u" p编译LibModSecurity(modsecurity-3.0)2 Q& H6 Z0 k; b; P8 d* H7 I. ^9 _
$ cd /root* B1 s4 ?6 j& \- t
$ git clone https://github.com/SpiderLabs/ModSecurity
* z5 w7 h3 J9 _! Q$ cd ModSecurity' T/ M, Y! L2 |1 f& s
$ git checkout -b v3/master origin/v3/master# e4 w8 B8 D1 ~; D+ H
$ sh build.sh
, V5 f7 X# M$ \9 S/ a6 j$ a+ Y  j$ git submodule init
4 i/ w7 M1 N1 m  U$ G0 j0 g3 n' ?$ ?$ git submodule update
: c8 W3 X5 y) x6 d5 w$ ./configure8 p/ Q# o1 w0 J+ u
$ make
; q; q" K' B3 N+ q# U6 A/ j$ make install8 S1 r3 [7 C, _' A3 w
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。/ W/ R! n' c9 T4 G: S) K
8 m) s  k: n6 k% N  o. r. A
7 u; `' `: F/ V" {3 M
$ ls /usr/local/modsecurity/lib: a* J4 x1 d6 u
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
& L5 N1 R9 P9 v编译安装Nginx并添加ModSecurity-Nginx Connector模块- Q6 f" e( Q% c9 D
使用ModSecurity-Nginx模块来连接LibModSecurity
6 U$ J" ]$ K/ v) H' \1 P
* Q( `6 E2 g- W9 e) T4 S  v# B& M; _5 l; O, U
$ cd /root9 ^+ C+ _1 e. b8 m, U6 o# X: [
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
! G; J+ i) D$ I4 b$ tar xzvf nginx-1.9.2.tar.gz7 P2 r* Y1 T5 H. e( u) w
$ cd nginx-1.9.2/ G: y) J( X5 Q3 J& c" Y4 x
$ ./configure --add-module=/root/modsecurity-nginx
7 T" G0 P8 Y& X6 I- A9 a$ make. }( U, H8 {* I" {  T4 @9 ]% H$ o
$ make && make install
7 Q* ~4 q# L6 Q, m% P: d0 s添加OWASP规则
. N& F( ~; v8 [" w" c. B. rModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。; [8 ?6 u& [' Q# k

1 x# q, d/ ^6 i$ y5 `2 t2 W/ g4 \7 r. y" k: g8 k# P2 ^; t; b
下载OWASP规则并生成配置文件4 o( X+ u( L8 I) F% V/ N, C, o
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
& w& V9 `; }8 o7 s1 r6 I$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, ]; h# b7 I6 ~( x7 s7 J! `& {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ W' e; a* {- |$ cp crs-setup.conf.example  crs-setup.conf
& T' [1 ^: b. [6 ]( f* [配置OWASP规则
# w2 i6 ~1 L/ _编辑crs-setup.conf文件
5 A# n  ?' h( v
& X1 r: ~' g% x7 N3 }: R+ F3 k; \* h
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
9 }- \6 s2 c# \* ~$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, Q' V/ s1 O4 p- c2 b- T/ M$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- k7 W5 M9 A; g# x6 c) `
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* I9 h- L5 p4 }0 [
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。- O* l3 u; w( I0 _. n* U
- Q0 }- j: H2 f; _, E9 p
, a- M; O: i- I2 F! l4 R
启用ModSecurity模块和CRS规则
7 U- e( |# l* h复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 s" t) D1 h0 a
1 w8 o, j1 }, C# l
. V. h" A# J+ u5 imodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& h. f) s. a  K2 ]( O! v" F6 k! j0 P+ O
% p% L' o% W  |, `% b, Z- D# l3 R  s, V8 K, P
$ cd /root/modsecurity-2.9.1/
: p) w1 L6 _* v$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
# g  A7 ~. i! _' N$ cp unicode.mapping  /usr/local/nginx/conf/
# O8 s6 d( i+ U将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* P$ t5 h$ `  b6 Z. _% f0 j  ?. X  u: @5 j
$ u, \' Q2 n# O  N
$ vim /usr/local/nginx/conf/modsecurity.conf  S; ]& v; B. h& r7 e6 i
SecRuleEngine On
9 Z0 [4 i, T& `4 \$ eModSecurity中几个常用配置说明:; {' o: M: o3 e% k! y
! d7 F8 M; ]5 Y/ \
& M4 d7 M3 @3 \& M
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ U- n5 b* E0 p( c
2 Z+ ?3 N6 _! |$ }, b) P$ E6 g0 R6 {$ d0 x
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
& M6 Q( x7 @5 t8 Y' N: V" Q  J4 a: U, x% E8 S2 j+ w

3 f6 N$ a$ U. x" r3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 ]9 e( p1 h, i! m" G

# Y$ n6 P- c$ ~" ]) ^% l+ ]+ ~: u( C; f: t8 W+ \
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* B! R* M% x9 y# O( ~3 q; q% B2 }: _9 u2 C) k% ]$ g* E

% i5 E, U( ^3 w8 r* R在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ w0 N) ^7 p  a) [' R; k

4 o! r/ T/ ]7 E. C$ V9 L5 T( X- n# h) r  z* J! E
3.x版本CRS
6 ~, |4 i. @) v% Q( Z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- M4 m# m$ B  ^! U9 P! a
# 生成例外排除请求的配置文件
! o- K4 u* [$ y5 A$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf3 ^' _2 w+ u7 ?& T: _, C+ R* V
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf: _/ Q% P4 }' m' }7 g9 s* G1 A
$ cp rules/*.data /usr/local/nginx/conf
0 l  T+ s, f. S8 O% e+ g% z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
6 `6 b& L- F9 m/ y2 o0 [4 Y9 V' L4 s) {' `* g
3 b8 Q$ C, E3 z# h
$ vim /usr/local/nginx/conf/modsec_includes.conf* R! R. g- B+ Z1 ^
" |% a8 f7 b: I! m; P) 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
. O8 g& ~6 B6 h( s8 o- V

) b; l, M8 }9 a# p& g1 }. K5 }注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) Y/ t5 p* `& a
( q, W% I. B4 U/ V/ v
6 @! m8 ^. Z$ _# o, E9 j配置Nginx支持Modsecurity
  |  {( d* M' r/ w& p启用Modsecurity
! I7 e: ?, x( a; E3 I使用静态模块加载的配置方法7 Y# m0 X8 O. M9 p7 H! q$ B9 H
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 W; w# X% ?6 H8 `0 a

3 D6 u5 r0 x/ N/ {: G
5 ?- Y0 U0 B" uModSecurityEnabled on;
! d" \+ {# ?5 T9 c: N' s2 \0 ]ModSecurityConfig modsec_includes.conf;
; ^2 p/ @, U  g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。# N: E2 u' q1 g+ i

2 N8 e! _/ }9 ^( C
. r5 z# }0 K$ C9 t; i5 y$ vim /usr/local/nginx/conf/nginx.conf# ]- M! f' N# D+ a( [; P
  G/ ~4 z; {' C8 P% ~0 C
: f$ L! U+ E: {' i' y
server {  R' S* z& }* F% B4 x
  listen       80;
7 h9 W2 z9 I5 N3 n0 W/ F  server_name  example.com;- I3 P+ E+ i) m* {( X' D, z

+ ^! ~  Y2 V' {4 F. p9 h: U% N+ T! N3 m8 h* H9 @: F4 T# [, f+ ]
  location / {
! K; u! b" n( o' A    ModSecurityEnabled on;
0 q2 u$ t+ R& O* v8 J( R" h( j. D    ModSecurityConfig modsec_includes.conf;% j9 H- j1 Y2 Q* A) V; A. P" O
    root   html;
3 z' j# d# w9 @    index  index.html index.htm;8 P1 C0 n1 K8 z9 r6 B6 ^- E$ n% @/ U/ P
  }
$ n& [- F1 k  z3 [' J# g}
) k* J1 r* n  X9 _3 D! o9 b使用动态模块加载的配置方法
3 d7 F4 J. Y: K7 p! o在需要启用Modsecurity的主机的location下面加入下面两行即可:
! V4 k8 _2 I+ l% N" F3 c
4 I, d$ k$ o$ J; `) U
% d1 J4 l4 X( `: O) jmodsecurity on;
6 W' V. q/ e) f# j9 ^5 {) }& F6 @modsecurity_rules_file modsec_includes.conf;
4 m# W# I: {5 d! I! g% T* C; ~! y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( d0 j  B- Q1 b1 d: t1 e( h4 j5 v7 y9 H' {) D6 e9 _
3 j/ Q2 E9 i) M3 Q/ {5 h& ~+ q
$ vim /usr/local/nginx/conf/nginx.conf5 D" |3 A! p" p$ y

% {: a- L, K8 P1 r8 O. u; W
+ ?3 W' U- T% B$ x( i" {server {; N/ k" x' [% l4 j8 ~! r& `
  listen  80;! @) ]- z. g- k( o( C6 R0 s! g
  server_name localhost mike.hi-linux.com;
/ m" r: U, u/ g/ e1 n  access_log /var/log/nginx/yourdomain.log;
8 ?# |. I' y' L. P+ i) w; c7 W7 A5 }5 v5 O1 H' Q! v

& y$ H/ X. \( i/ e  location / {
" V2 L8 O4 Q& g7 w+ r! P2 D; B: o3 j+ T& E6 b) n9 Z1 N

, @9 x; J6 f) `2 J$ a4 {  modsecurity on;, T0 Z8 y7 X. X) c, ?1 x. {
  modsecurity_rules_file modsec_includes.conf;$ i' ]. e& D1 F: Q% R3 M
  root   html;+ N2 m  x& {6 w& w" J; ?
  index  index.html index.htm;
( x1 X2 e: J9 G  r6 V}" m" |2 s7 |9 D8 [% m) ?3 f2 J7 U! \
}
' k3 f- s3 L/ L/ p8 O+ R验证Nginx配置文件
; v! Y, G' X, K; A9 i$ /usr/local/nginx/sbin/nginx -t
" t2 r4 T: `6 Jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok; h& p- E* E5 K$ Z" J2 P
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
. G' g- [# {. {' w. U3 {6 R' J启动Nginx
; Z( h. ?2 Q8 \/ F# u7 e5 o; f$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf1 R4 ^- Q2 ~5 M9 M6 d2 Z! e/ E. Q

3 I$ y: F8 C# z; N) r% V

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

8 _: k% w+ |7 z1 o

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-21 10:31 , Processed in 0.033854 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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