找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10146|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 `  B) i* t* _" O) d% `0 f1 k% \, z
: ?* B2 e5 E/ t8 |2 y4 L

" _0 g9 X# J' ]5 g% T+ t4 V9 t0 S在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* U% z9 D0 O8 s) }% x
1 X9 j" I, S' A$ [+ D

- h/ ]* H# H+ [* U; B* r3 {& |什么是ModSecurity; i5 b  q5 e. s' b9 s1 a& I2 Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。6 ?+ m4 ?# w# J& }
7 J! J7 {% C$ z- R

: F+ e$ z0 `8 w' X; [! H0 }- mModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
+ `, N# J7 v! A7 U; @8 i
+ i6 B$ |4 P! y9 C# C6 a6 J9 B! J/ o) P* H8 d2 y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
9 \; `* D* i* x+ U4 B+ R) N! ^* q( t  }* X5 ~
3 ]0 O7 x" c) f. {8 g
官网: https://www.modsecurity.org/8 D8 T% ?2 v& s- p
8 N$ @* y2 ^) d; V% k

# j; w  W6 L2 u1 |什么是OWASP CRS) ]) }0 k2 D( D8 O* ?' n& w2 ~
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% H1 f( c9 C: _3 B- g' X' Z+ `# ~! a, I

5 p8 X$ T& J- H' z( ]- F2 m0 JModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 T' \) @2 c$ w+ b
: W) S* [* Q" @

5 d+ q& O' c6 D+ h  M* v5 l  wHTTP Protection(HTTP防御)
% Y/ x; I# J! kHTTP协议和本地定义使用的detectsviolations策略。+ f; M1 h. `3 m( B, O
* a. m1 f; h' i, r2 F: V+ G& z
& r7 M$ [+ E: D1 H  O
Real-time Blacklist Lookups(实时黑名单查询)7 @9 o, c1 G" F' R+ u3 D3 _
利用第三方IP名单。
2 v9 I7 v' g; x# E. _
' j" @$ H' ^9 i% U1 S" S
% x6 J2 R8 P+ V! hHTTP Denial of Service Protections(HTTP的拒绝服务保护)" U6 |0 _4 k! F1 m
防御HTTP的洪水攻击和HTTP Dos攻击。+ F( T2 w2 ]0 Y) T0 o( P- A7 H
7 _) q/ c( t3 X' ^* [' E  o) P+ y0 F

2 X- C! b: J4 I7 P( uCommon Web Attacks Protection(常见的Web攻击防护)$ h, v% y) |$ p2 ^7 N7 n9 k
检测常见的Web应用程序的安全攻击。3 I8 g5 e' Y' |. l# n

6 O1 ?  }+ U6 b% x
& @$ j  v  U6 d$ `Automation Detection(自动化检测)
( v" k+ h) f! @& M5 _9 G( a2 J检测机器人,爬虫,扫描仪和其他表面恶意活动。+ I  u5 v( p3 X3 ?
+ Q, [8 Y+ O; z) \

. C; ^, H5 J- }( qIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
: w* j3 _( v6 S# e检测通过Web应用程序上传的恶意文件。
; G* h, Y* z6 j; a) H# M9 t1 C* m8 P

9 U, [% k8 R& E+ `# \+ aTracking Sensitive Data(跟踪敏感数据)
/ {" S9 N  }) h) D: a& W  j" i信用卡通道的使用,并阻止泄漏。# U8 f5 w& e3 f

8 d& r$ O8 U* k+ W" o* N# x# j- p+ O3 k  O' @- H
Trojan Protection(木马防护)
& E5 ~, F, d. D: C检测访问木马。
1 @$ c6 H7 V1 Y9 h
7 U; Q5 g0 R4 O! |; j! I) X0 b8 B; t9 B4 f" U
Identification of Application Defects(应用程序缺陷的鉴定)( q! f  B+ |7 ~  T
检测应用程序的错误配置警报。
$ _& u; u" B# t
7 h1 B: q) W' Q1 B
, j+ T( p- A! D+ tError Detection and Hiding(错误检测和隐藏)5 m* T% M2 O0 R
检测伪装服务器发送错误消息。2 F9 b3 E! p' h, V) m, A% U8 H
* h# u! o. L+ O: }: h5 P5 K

, i6 q% {" a6 ~, Z9 V" x" `安装ModSecurity
# M/ v$ r1 B/ F8 Y5 U; s, D  X! q$ o软件基础环境准备
& |. U, l6 Y3 c下载对应软件包
- S- ?( A6 ?7 x9 U( k2 E$ cd /root
6 z8 u4 S+ y: U7 x: O$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
! [+ E0 C3 }* x0 W; |$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
# H3 y/ |% Q9 _3 m* h8 a1 z5 L" y安装Nginx和ModSecurity依赖包2 A( s( o) v0 Y, s
Centos/RHEL
+ H" ]* J6 Q1 I8 z$ V  A: ^7 S4 h. v2 E4 |/ C, K

6 [' X) u3 K) L* l0 o. c$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel  ~4 B5 r4 b* n' @6 s( b& F
Ubuntu/Debian  ~5 A" h$ F( C" f
9 _  {" y( [8 X

, o& y& @( P9 n; o( J$ 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++-dev4 V9 U+ W9 _$ w1 |
编译安装ModSecurity( p; D( A$ C' o7 D0 D$ E
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。3 r( m( z% f2 o. y3 d. q

0 U/ `; l# c7 l1 v9 g( S2 |5 a  J. t& m
方法一:编译为Nginx静态模块3 Q& I% v  Q  J4 ?6 M6 K

4 ^/ R) p/ Y3 |7 o; t4 g' z0 C, _  z) h  d
编译为独立模块(modsecurity-2.9.1)
" D, f5 ~$ h3 E3 j! s" w$ tar xzvf modsecurity-2.9.1.tar.gz
+ w* ?$ d5 f6 v6 ]' @$ N$ cd modsecurity-2.9.1/, |8 W3 v- o# I5 W5 D
$ ./autogen.sh- t4 }$ b3 d3 o% e$ d9 z7 ^2 w
$ ./configure --enable-standalone-module --disable-mlogc
5 {+ R/ n% |& I( N. l& I$ O' u$ make/ Q3 e$ o& ^+ b1 ~
编译安装Nginx并添加ModSecurity模块
2 ?& }6 K& G/ X( R! x4 t$ ~$ tar xzvf nginx-1.9.2.tar.gz
. {+ J( _+ N$ ~* [7 q' U$ cd nginx-1.9.25 n  i0 D; a9 X  Q
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/( O! k, G$ H8 A3 e) [$ |( q
$ make && make install# \2 G! Q1 l! A% R! J
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& P- H" b7 m8 w- [' I5 t$ s

( D* g1 Z6 |) ]+ d) b: O2 Y/ X: L! O# [2 c
编译LibModSecurity(modsecurity-3.0): t& l1 P$ ^6 `" F/ ?) U
$ cd /root
2 [  g' `1 |$ @# ?$ git clone https://github.com/SpiderLabs/ModSecurity
4 U! V. U2 C4 |% n) \$ cd ModSecurity
4 G) p7 ^- |9 y# `" ~0 D$ git checkout -b v3/master origin/v3/master
9 i8 F/ Q; W$ C, E# V$ sh build.sh
7 B# ]% F4 c; |+ Z7 n" f6 D$ git submodule init1 i9 a7 l$ a/ C. F) ]+ r, y' O4 o
$ git submodule update0 _) W9 N. m3 D+ V1 E8 p
$ ./configure" b+ N% ^, V! U* M& g1 z8 g
$ make( Z3 k' F% f/ L5 v/ q4 x9 \  T& C* a8 Y
$ make install
/ E0 V8 Y  s  b( _! `' X, W8 cLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ K9 A$ m# U# {6 `

4 ~- v$ {, `5 ~5 p! h) X( k
. E7 O: k# q- P/ g$ L4 D$ ls /usr/local/modsecurity/lib+ |& v6 i& i# D5 O  ^
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
6 F- y3 @, h# @$ M! C7 v编译安装Nginx并添加ModSecurity-Nginx Connector模块$ S; \- q, b- [  y
使用ModSecurity-Nginx模块来连接LibModSecurity" {! ?" u+ C2 x' m& h/ G5 |

3 J) c# c9 f* u1 O
6 T5 Q4 A( Q, e) a) ?  c; x: u$ cd /root
$ j! s) r. N# A2 a$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
% G3 g$ Q  I, U1 E$ v4 v$ tar xzvf nginx-1.9.2.tar.gz
1 L% c; v  F: [, f3 d) x$ cd nginx-1.9.2
1 D6 M+ ~( y$ E" j0 y8 O1 G$ ./configure --add-module=/root/modsecurity-nginx
" p7 X  F- a, I8 p$ make
; @: n, u$ a8 o& \$ make && make install2 }6 l+ C- g  C' q) ~9 b
添加OWASP规则
/ z9 J9 h/ |" k0 S6 k9 \ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
" i+ k- i/ F/ V: [
# Y5 _7 d6 ^' l% N
0 e; X% C$ w: N" O  T# x( `0 O下载OWASP规则并生成配置文件0 \* M) V3 A  b+ [; R" Z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
5 A* f8 K  H# `( u" i' B  [$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 C9 ?! ]" N  `: c7 e/ |1 {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* M1 |  v' ?$ G8 H/ P  F
$ cp crs-setup.conf.example  crs-setup.conf% g( d" D$ h: a
配置OWASP规则
- `* a/ Z* G8 j7 t* }$ m编辑crs-setup.conf文件
9 k: B' i7 P; T& ^9 X
' d4 k( a# x  T4 ~6 e+ f' J8 d
" W+ e' }" ~0 J7 g( n$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ D7 }& A, n( f7 f
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& b; f( P. Q" D( W0 t6 M0 {, C
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf4 Q0 f- M; c# h9 z
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
5 ^. o0 J4 I1 }8 B默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
/ V* b- g! ^; |  S; m9 i6 |3 x- d

! r9 w* }1 \+ g+ ?2 C启用ModSecurity模块和CRS规则
0 L# W! q' O7 q4 c( _复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ U- t6 G8 i3 s3 m% f8 m: x

% r  ?' o" {( j+ t, S7 _0 ?0 F
: {5 ?; ?& b7 L8 M9 N; [9 Gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
6 t9 ]0 f; t9 v! q) Z" s# w( X# Y0 e6 J8 s
/ ]* H  i7 q) i6 k& T( G7 W) s
$ cd /root/modsecurity-2.9.1/
: i7 J/ S( ]5 k) A& L9 E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
7 v" w) F: L8 [; B$ o6 f$ cp unicode.mapping  /usr/local/nginx/conf/
7 \* {+ k, H3 r将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ {, A6 ^* ^8 k5 b
% `" N- ^7 Z' @
( M* y7 q' e6 @8 U8 c2 |# c' K5 v$ vim /usr/local/nginx/conf/modsecurity.conf
* A6 T+ Y2 J4 S( T0 uSecRuleEngine On
6 O0 G( J9 _. Q: G# G  y2 eModSecurity中几个常用配置说明:7 \4 I' B( N3 W. F" G: G/ T4 B$ `

9 B& ~/ g& \. k
. }- H; h+ b; ~) C+ _$ X' g1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 M% F! [% |4 r7 |3 P* ?  z
8 C5 u8 o" U$ @" s% [' a* k

4 i4 \9 v0 S% E- K2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
! ^. e3 s" T' M+ a, e6 ?. N* N2 k9 g0 M9 h2 s- ], ~5 J3 L2 K

; h) _7 r2 p6 ?3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- U! y+ Z/ Q: U* G' @6 {
4 S6 M0 q* {7 j. l9 F' \  }/ f" Z- |/ y4 H8 l* q( p
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& U$ x+ [- J* E+ V  m
+ \8 s. b, i2 E
& g# ~; j0 K& x6 |2 z$ J: ~
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
2 i) Z3 |( R" X) Z9 @2 [+ a+ _  X0 v/ J) K7 Q& I

% S8 }- Z0 {' Z8 x' ^$ ?, o3.x版本CRS$ K; C/ v/ E/ \4 o# Z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) i5 _$ A8 ^1 E% \8 S4 b& B) F# 生成例外排除请求的配置文件3 W9 I5 L7 a: {) l
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. _1 V& w5 {7 m  f  u- j; e; x; r$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf6 H" t1 j7 F, V+ r. e; O7 {0 y
$ cp rules/*.data /usr/local/nginx/conf
' j  W8 {; G) h! T+ @3 t为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
1 n9 T& p6 }* O1 K" T& y' I1 g0 O- K. ^6 R
& _: T& Q6 N8 c' L2 B
$ vim /usr/local/nginx/conf/modsec_includes.conf
# s' C5 K8 Z2 Y! b( n
& L; Q- Y/ j* \$ E3 h$ r5 ~7 w/ 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
! k2 B/ R; ]; q& B
9 a' N7 S' u+ z6 Z* C5 i
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) ~6 Z9 s% @2 @: E2 f7 j
5 G0 d4 c. H( C* ~5 ~) r, w- i
( A3 G5 w& I6 o8 N1 o$ |* k
配置Nginx支持Modsecurity  H4 u7 v0 y) f0 ^1 ]
启用Modsecurity4 R9 X- @% t* E
使用静态模块加载的配置方法+ v0 A% p4 X% [& L2 w& j' N/ J
在需要启用Modsecurity的主机的location下面加入下面两行即可:/ L3 o/ [; ~& U9 v7 m

* I6 F2 {! Z& U
) H9 G1 {# e- W1 {ModSecurityEnabled on;
3 j, q! W8 D% zModSecurityConfig modsec_includes.conf;/ A5 h# _: M& X6 {5 h8 O
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& ~1 C. N. }, ?1 B* s6 b- [) |% s
' C0 X" @0 ^' X. }5 f! F1 m+ `+ }3 X' c$ X8 n& L
$ vim /usr/local/nginx/conf/nginx.conf
7 G/ ^$ t! l* R& L: _4 y7 U( P+ I4 x: s, t' G* O

8 U# o7 p* M( [server {
6 Z; a9 s0 [0 o1 z' m. ^  listen       80;( D1 }: |1 S+ I& `
  server_name  example.com;2 f' Z2 i, d3 a! x) K
& Z& V+ {2 Z! @" w
: u+ z; ^; O9 t* U7 \8 {0 x) j- w+ f
  location / {
$ O: z% X( x# ?" u, }    ModSecurityEnabled on;
$ T; s) c6 t  Z( Z. H    ModSecurityConfig modsec_includes.conf;
4 z3 a' ^. |4 r    root   html;
# I3 s1 ?6 ]% ?    index  index.html index.htm;6 D2 l4 P( h" ~4 D1 A  Y" J. y
  }  A7 l9 G; y' [4 }. O
}7 S7 S8 `9 n+ r
使用动态模块加载的配置方法# u" V  D  E3 q6 }0 |8 `% x
在需要启用Modsecurity的主机的location下面加入下面两行即可:* I/ d+ x: ^; d  b

6 i( U3 o5 s1 c: R
3 N+ `3 ]. y# y9 Ymodsecurity on;
2 }/ t' j: X4 Emodsecurity_rules_file modsec_includes.conf;" D/ j) l6 E: L3 g2 [
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 I7 B  g8 g! Q' _6 P0 x0 t. s; P0 q2 F7 _! y" e+ j' P

& l$ Z5 N% n/ N& @# {7 z$ vim /usr/local/nginx/conf/nginx.conf' Y6 @( A0 M8 B: t# T4 @) G0 v

% [. V* p/ v$ d. k- @2 H2 n5 ]/ x* r9 T. G' v! u, B
server {
. P  d4 H/ o; y) q' M3 H/ w  listen  80;" }) {& w& p# v2 Q6 q$ c% J
  server_name localhost mike.hi-linux.com;
8 G2 k8 g- a& g# S7 B) t" n  access_log /var/log/nginx/yourdomain.log;
6 p" \3 N  _" @8 _* f: M
7 h" C  x2 y# q) e5 H8 \
+ l! N, P* R; H0 R% {# X  location / {
! }% s# C: ~9 K5 C. m8 L5 N+ ^1 G1 t' M& [) b6 L1 ~7 a- P

0 @/ z1 M5 a3 H9 x% G8 T  modsecurity on;( A" X3 V& K9 J& y2 h0 x+ g
  modsecurity_rules_file modsec_includes.conf;+ w5 p: d; e5 M
  root   html;
( M0 h3 w2 `9 N5 x4 B  index  index.html index.htm;; s+ P8 P( j2 {
}  L+ X0 Z- x$ k+ l% A9 a( Q* B
}1 F( T" Y/ B# E6 l
验证Nginx配置文件7 d% r$ u+ V6 y8 O9 P3 D) n8 u
$ /usr/local/nginx/sbin/nginx -t, @" Z( I0 J* G7 a) ~; E) h4 s$ s
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- v5 x4 o, v1 Y. K+ Vnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; }( v$ D* C: c1 \& ^9 U4 ~4 x6 ^
启动Nginx
7 Z0 |  N8 Q. D: s$ H# \$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
0 z; p: T  b: y) U+ e) \9 A2 Y; ]0 h1 d

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

+ l7 R9 D: p2 I! h* z7 k

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-8 00:00 , Processed in 0.101774 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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