找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12172|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
8 R4 R+ n& t: G% Z7 y- h: `  v$ d- |5 Z* D  {; T
4 s0 t8 V9 f# h2 }, p- S% r! A  W' n
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 Z& n" k  a! {& J1 t& K: i  s9 w
8 T$ B' l3 `9 f: s( l( v3 N: I# S( T# i3 s
什么是ModSecurity
9 A/ q% C8 [: g6 {  r* u! E8 MModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! _, X0 W- e0 z* K; ?
$ [" r. V4 T9 O0 M. m

8 ?) M; c* M" [& }5 @ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。/ A+ _' _$ R$ J8 O2 s3 }1 r& P/ X

) O" D$ t2 m# l' H$ r0 D
9 I$ V" |; E% NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
. c% @. a6 s0 d/ W+ J7 F
7 P; r$ W. @) j+ `0 A; z+ F/ b- G6 K+ A- V9 A& b) V" n
官网: https://www.modsecurity.org/
5 m6 d( R* X; W1 L$ {8 G
0 K/ a( }- |9 y4 Z
! n4 p' X8 Y5 H什么是OWASP CRS
  ?2 k4 p; o( b2 `: h4 aOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: d- l9 i0 ~! s, E$ h2 k, x

2 ~, R; @) T3 R( d, P9 ~
# B0 T- H7 v  l3 H; kModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。" m6 i0 S7 V% w
- f( b! y3 x# B( I! m* Y9 M% U
: |+ Y' n6 ]0 q% E1 `4 S
HTTP Protection(HTTP防御)
8 T# d/ s& L1 Z2 A- qHTTP协议和本地定义使用的detectsviolations策略。
; H; g2 p/ u: X# C7 E( _& A: `; J/ Q! z. I& h
5 S2 }4 W# m( W9 Z6 U# P7 A! e
Real-time Blacklist Lookups(实时黑名单查询)
' X% T. ]. f* c* W8 f利用第三方IP名单。! n1 z2 }* V$ e8 I8 U" C2 R
& {3 _# P& k! o: s7 O
6 _: n: [6 \' }' a0 {, p$ |
HTTP Denial of Service Protections(HTTP的拒绝服务保护)7 A& a$ R! n& j9 q4 r9 @/ x2 I" x
防御HTTP的洪水攻击和HTTP Dos攻击。
' K2 R7 T. K9 ^$ @" H
" Q5 _/ u& g! U: q8 u: t9 J, |1 ^, O. @! G: X0 L
Common Web Attacks Protection(常见的Web攻击防护)" }9 o4 ?) F( p2 c: O9 j
检测常见的Web应用程序的安全攻击。8 w, X0 [! l$ }  I: K

: s+ T+ L; ~: D0 [; M6 S4 a! x1 i9 P  c& a
Automation Detection(自动化检测)4 L$ C6 c2 g6 y5 ?* c4 p. C4 ~! S- E
检测机器人,爬虫,扫描仪和其他表面恶意活动。+ B& G- D- f0 F0 d* J: b
5 c  _  U* @& e$ m, N

9 K/ u8 [, o5 d% Y* ^* xIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
* ^! i0 Z7 i3 V" x) X& T检测通过Web应用程序上传的恶意文件。
7 ?7 A2 D4 p! Y9 Z5 R$ I" r9 N3 V. [& W! T/ R
* L& W$ S0 ^2 J4 i# i1 P$ o
Tracking Sensitive Data(跟踪敏感数据)
7 v: L- r% w8 r' b6 Z4 q2 T信用卡通道的使用,并阻止泄漏。
0 g5 x8 `8 M+ G" F: b
( t  J) x! ]! s* m+ Z" K* V! U% q* E7 ?9 u" w. X
Trojan Protection(木马防护)
# ]2 ~/ q/ K4 N. L/ t检测访问木马。% J- ~) B, ~" c

& M1 @6 H0 D& Y, k% }% G2 ^$ R. [' I$ @" P* E. Q  T) }
Identification of Application Defects(应用程序缺陷的鉴定)
$ w6 p( W6 M- N$ a检测应用程序的错误配置警报。
3 K$ W/ d2 U3 N: b
: B. l6 d& o- [2 y
9 g6 T! {. w# x, ?0 k) L1 eError Detection and Hiding(错误检测和隐藏)
- e  k1 [3 N5 |* s! }6 F检测伪装服务器发送错误消息。
# m" Y) ?  s) D# ~% j+ Y
+ q7 x. l/ L6 l, r5 B4 s* w  w
( z+ G  w( S' }; U0 I' ]安装ModSecurity
  f6 d" p& X6 k% K: U2 ~软件基础环境准备
$ y# ~& R- O8 i5 s* ]下载对应软件包
2 D+ _5 G4 ]1 n% h+ v3 r$ cd /root
% `5 `3 u0 }6 u) j$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ x" b# @5 b6 L* i# U3 E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 a$ @7 U% F, q" y* W6 o安装Nginx和ModSecurity依赖包
; x1 y6 e: U; e: a$ R- aCentos/RHEL
. p' E  |9 v6 m
5 P: `( J5 S' m7 `7 g1 J
$ i2 t8 j2 m* j( g9 o$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
! d7 x% X# Y3 JUbuntu/Debian) ?% Q. b# z6 q4 g$ N( Q

1 ^3 }# V. Q$ ^/ \3 ]7 K& d+ Q
+ Z: F$ l% L  K) d5 a9 s7 p2 v+ S$ 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++-dev3 S9 M( G* F% G( P/ X7 h
编译安装ModSecurity$ |) _# K' `" j1 B% ]! D- T
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。8 }1 L* L; k. o, z% J3 h

; o3 P4 F) x) q; E4 b& T9 i. K
1 h+ E/ U4 ^/ ?# ?+ M8 h7 g方法一:编译为Nginx静态模块
% Z! l3 B1 W4 w- M
  r2 o* S! R% c% `
, Z$ u( n1 V" F5 E/ v编译为独立模块(modsecurity-2.9.1): v7 J  d% T/ n7 ~
$ tar xzvf modsecurity-2.9.1.tar.gz7 y( w9 G. a* b5 r7 p9 E- M+ v
$ cd modsecurity-2.9.1/( V3 a4 l. v! v
$ ./autogen.sh# M# ?' k2 a. C
$ ./configure --enable-standalone-module --disable-mlogc
+ e: n0 |! D( d! k- F$ make
, I7 J* ^% f% N+ W4 U! [# Y编译安装Nginx并添加ModSecurity模块
: g$ _# [. n. b9 s1 I$ tar xzvf nginx-1.9.2.tar.gz$ H; J1 L& I8 D% y- E
$ cd nginx-1.9.2& U, G; s- k! A) \
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" r4 }5 t1 a% V2 w  v$ make && make install/ f, R( i) P( Z! |
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块7 u+ N) @& U0 C6 R5 H% M0 Q2 D6 X
# |" e# {2 f% U, l$ w0 }2 Z% ?. Q5 K5 C

% p  `& |" C5 G% l! ]! i4 [+ K编译LibModSecurity(modsecurity-3.0)' g7 \( w& J' r* r5 n+ g
$ cd /root5 |; S5 k5 n% j/ H
$ git clone https://github.com/SpiderLabs/ModSecurity
- U" A1 K% _6 b1 X# f$ t$ cd ModSecurity4 }: L+ f" x9 O8 Y; u
$ git checkout -b v3/master origin/v3/master% P. S; P7 ?  T7 O4 Y% \, ], o; A
$ sh build.sh
# _1 E4 g# M5 t+ w( z  z$ git submodule init
' X- k7 Y( K& G/ d/ h$ git submodule update
7 w3 l* L6 K8 l5 {& |5 a$ ./configure
9 R. t) p( w- {; E$ make+ k; [5 N3 `( w! N8 P* A
$ make install% f, P& Q. t5 r* g1 l
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: B; T* P- x, y% X, O% _$ g" e6 w4 p" S! {9 N' ?

) h6 q* S: g# s$ ls /usr/local/modsecurity/lib
6 P* ]2 I+ n  n: f- C0 W) K% hlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0. y$ \- _. F3 Y  @2 X
编译安装Nginx并添加ModSecurity-Nginx Connector模块4 g+ Q$ H& Y+ c
使用ModSecurity-Nginx模块来连接LibModSecurity( @( O1 \5 _: u" k- q

' D4 b1 E8 i: |# d9 _
7 v: ?) y  E; h6 `$ cd /root
& U. }9 S2 q0 L7 b, ~$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, ?# N; b2 j4 A5 N$ tar xzvf nginx-1.9.2.tar.gz: M& |- D' H  \) h) ~* u
$ cd nginx-1.9.23 m: V9 `; ]& r5 L3 J& ~3 j& e& ^* J
$ ./configure --add-module=/root/modsecurity-nginx
# B/ |! i4 p8 O' ]$ make4 D2 n. ^5 ?1 M: e
$ make && make install
3 ?% ~4 |: x* u  T( y% \' A添加OWASP规则
& Q. X+ N2 `. `) CModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。( R( I, M8 f2 m) Y6 x; s

# j& O/ b" U8 Q4 y5 O6 }" \" E8 I& M) a) j& m! `6 t7 P; s
下载OWASP规则并生成配置文件, E2 h* W% b. P7 F1 f
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
3 z1 A4 t7 }+ U) v& [$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/) e! o1 I2 W) a0 n, g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 D, C5 O% {; V1 w6 N$ cp crs-setup.conf.example  crs-setup.conf+ G2 W) s/ v, k& l) a3 P
配置OWASP规则
# a3 r% v' j) Q; i8 p编辑crs-setup.conf文件  {* b( Y8 _# ?% a

$ V' A, L9 H+ p, F( u
( ?% l6 `# G, T! D) g$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
# v/ Q7 B4 g+ I& Y4 J. d$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
4 R5 _! a0 d+ m# W- S4 E" D( m6 q8 @$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( O& Q: W5 s; q7 o' G1 f% A/ M( O
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
& Q! Q0 r( p" s默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。1 M  j; V6 a; `4 B4 c; D
" |! z! m4 e" h
$ K  q" V, S/ s. R1 ~
启用ModSecurity模块和CRS规则* ~+ Q, k; b8 c. p
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 K3 A8 A. }* l1 n& C# s
2 z( J! D# a  P$ l) d" D# `
$ k2 P2 i" @# f8 R& [  y) g1 l6 J
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 k# K) d5 r5 Q' v) v

6 p4 D) P' h! L* ?/ q" ?
" L+ U8 B9 Z, F' p( ]$ cd /root/modsecurity-2.9.1/
. F; j9 F: a9 H( z' X$ J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
) J: |3 e: i' L4 X5 s+ ^- J$ cp unicode.mapping  /usr/local/nginx/conf/
/ [  ~) a( k% q, v$ U将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。% A/ Z" I- ^, \' l
+ n- Q6 }6 v4 F# O7 V' y; g/ f/ ^

6 G; |# b1 o- O9 x- q$ vim /usr/local/nginx/conf/modsecurity.conf7 x( B. s" G% f1 G2 o
SecRuleEngine On
5 Y' D- X, M" Q- _  {) H, OModSecurity中几个常用配置说明:4 w  C+ N; w( [, N$ M! W+ W8 g
9 z7 d; v8 N* D! Z  \

( E# }1 D9 {5 }# s! e% l1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 t6 ~* x! `* X  e3 {
1 P% I+ {; m# [

4 P: Q! d* X4 W2 F( L: H2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 U2 S" G' ^  M  i2 V" W9 A
+ Q+ _: U$ s3 U- y! T& X
1 D4 a4 o7 h% x: i4 r: }3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 Y) `. ]0 e4 t! l6 z5 m7 Y0 c
" e8 U. D: [- ?0 O+ U

' L; [4 S7 A, D6 [4 }3 {) q- Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
% @( I/ Q. R$ Z" v8 m( w* c
  ?  [) u4 N4 [* ?6 h1 L
/ K7 S! S$ l+ v在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- n- v0 d" v) O& L0 A- |# Q3 @" V& _

3 a$ T- z# Y7 V5 E# |% \# k# J5 \  E3.x版本CRS
% O: Q$ W3 [4 X5 H/ h0 ^8 H" F$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 M/ {1 b  D7 K( g0 w7 m
# 生成例外排除请求的配置文件* T( J" I: t. S* Y; g( \
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ N! e5 k4 R- z3 I. Y
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
5 {0 N. Z; R/ K7 O8 @- \3 D$ cp rules/*.data /usr/local/nginx/conf( ^; I  y  |* D1 B
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 i: C# j& A7 z. [- G$ E( z
8 J  a! }5 o9 O2 C% F" [! m# }5 ?% N- V2 ]& \  B/ O
$ vim /usr/local/nginx/conf/modsec_includes.conf
% ]+ b$ I+ c5 ?6 ?: H$ n* H
- \, D/ ^& `8 U. u. W
[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
& Q7 X( G, U7 L% L0 ~% e

& a: b; s- v$ L1 Q  _注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ A7 p; R6 p. E1 I
3 A6 v$ L) J" I8 V8 U4 {
5 [. i* k* [0 U: b# {
配置Nginx支持Modsecurity
9 m* h6 n2 d* X& g+ D* J( q/ `启用Modsecurity
$ m1 `- I0 N& }- N$ v使用静态模块加载的配置方法
- c  Q+ f: F4 O  |6 `' s. {在需要启用Modsecurity的主机的location下面加入下面两行即可:
* \- @1 h7 ~; [; m7 i7 p4 n/ r9 H4 R, z- {. u
! ^, R7 M( z3 ~1 [3 o0 {0 O; |4 q! @3 W
ModSecurityEnabled on;
5 x" q+ @* u# k2 }; Z. [ModSecurityConfig modsec_includes.conf;9 @1 f- s8 p0 F/ h+ D
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 Q. g1 ?7 u0 I, Y2 E. B6 g

9 |6 E+ D3 X0 h  c
/ F) ~, f* `9 P( J! I$ vim /usr/local/nginx/conf/nginx.conf
+ U5 Q" r# R. \1 L0 X4 J, r1 ~1 U! [  |+ x1 w6 u/ j1 c

6 Z0 g, H5 @$ B- [" Jserver {8 n( e! f* n5 @# d7 y# `1 S
  listen       80;
5 |9 H+ X; \6 H" }# |  server_name  example.com;
; v: P% q8 z3 M6 M* O
  e5 W0 @, d3 J
8 V* g& [7 c9 ~  location / {1 a6 `$ F. w/ }
    ModSecurityEnabled on;
" ~+ _5 O4 a# o# F7 J/ |) |: ~0 `    ModSecurityConfig modsec_includes.conf;5 Z: C7 [& ?$ m9 Y
    root   html;9 O/ [1 q, i4 V) F3 i$ x+ z0 A
    index  index.html index.htm;2 D* I# J1 ]2 g  I$ N* M
  }
6 g* _# m& w) X! s2 a: \. K}
# x0 U0 o% U$ Q0 k: X* g使用动态模块加载的配置方法5 ]- b9 Q, i( p
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% U3 v# {2 }7 R6 s" q' ~* \' l- K. X" |2 L" w5 t5 Q) r/ \
$ `2 I3 F! z! t' b
modsecurity on;2 R( N' f2 N3 o$ r2 `8 j
modsecurity_rules_file modsec_includes.conf;4 ^1 T+ d  D5 `# H5 W9 V! _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 z* [5 \% V' ?

5 c- z; [# p0 |8 B. O2 W9 {6 S4 V) s# ?
$ vim /usr/local/nginx/conf/nginx.conf
) r. h( Z: O9 |$ z/ C7 q' A6 Y' T
; Q- t% C6 x) S/ U+ V0 q) ?" ^( q9 o& M: }
server {
* |0 @- t8 q% I3 X* U( e  Q  listen  80;- o5 i& y: y* V; k# v- f  @2 j4 _
  server_name localhost mike.hi-linux.com;
& d# R4 B+ @" g0 e1 w  access_log /var/log/nginx/yourdomain.log;' ]' z+ ~6 r9 ?! r2 P
( J' J/ T8 [! W

. d. s' R! q7 N! X. V0 g  location / {
, {" A4 {/ f* k( _/ s' f
& Y4 F3 O. B6 U- A3 j4 j! Q3 P
" W, L1 R2 u! \, d/ ?: Y* t  modsecurity on;
7 r# F9 o) z, }7 |+ @: g6 w  modsecurity_rules_file modsec_includes.conf;
* k" k& }* J  `" Z8 V, y8 O  root   html;2 ?! t/ e/ K& J7 L
  index  index.html index.htm;
, Q6 i) u8 h5 j: s) H}' v6 p; `4 s# K
}1 f: j* B% z. ^. h0 P
验证Nginx配置文件
- d  d5 W7 ]" n& j* ^& D$ /usr/local/nginx/sbin/nginx -t
7 x6 E: B0 [$ X5 ~( Rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. i2 x7 \/ w- `" N( J8 w
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) ^% E: j3 @# @; \9 {4 l0 E* P
启动Nginx9 S2 |; X- S- a
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  u5 [$ ~) I7 n6 O0 _6 t
+ w' `, m' f  @2 G! `& K7 X2 {) U

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


. k5 p. g4 T+ y* R% i

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-25 16:47 , Processed in 0.076780 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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