找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11666|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 B& l1 N0 i8 {2 d% t6 D8 g; g% h) ?2 t8 y0 O3 d2 p2 b( w
4 B/ Z" C3 b2 [9 Q5 I1 i/ o
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) k7 K- }1 G0 u

# b$ A) D+ X9 h- m+ c  y
) \+ ]7 O4 P  N; d. F; h什么是ModSecurity6 ~1 U# b! h$ ?3 |/ I
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ Z+ R& u4 g1 S9 X
8 M. E# R, Y& g) ~  S
3 |" x8 V  Q9 x/ R; W
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 i6 c) r1 c1 k% t
$ I1 G/ K' A- \- F4 Q

/ y1 l* \% _: Y- B# gModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% y* Z  w" d' ]5 e3 D
" ]8 n& D1 ?6 [, o  f# v% e8 s

' [2 X& u' T" x' b, F! L4 v9 L3 M官网: https://www.modsecurity.org/, d& Y: b+ K6 i

" q: d+ v+ E- c6 C" z9 y) X" l& N0 N
什么是OWASP CRS8 `3 n- H* F( t
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
& V. p5 y+ G$ Y1 \: b9 z
; m: A, e7 ~' x/ H" ]5 K) W4 S, M" H" W. [  O- q3 Y1 V' _/ n  `
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ L2 ^9 D. e% ^, |; p  h6 g* W) k* O( v' U
; ]( J- y% F0 i$ J7 s
HTTP Protection(HTTP防御)& B8 k* N% @- C% z, o% K) P
HTTP协议和本地定义使用的detectsviolations策略。8 H* \' `7 T0 F4 m/ X

: W" W8 f! V% g6 K4 s' b' z3 g
6 c2 Q1 O" I8 J/ [/ T8 c& B: FReal-time Blacklist Lookups(实时黑名单查询)
, [" Y6 w0 f1 N- Y: j$ y+ L利用第三方IP名单。
, M  Z9 B: V6 N8 N$ J
" R) P, T! p8 W: Y3 n( ?0 F0 \7 Y/ {- a5 I
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
$ g7 p+ R9 M. i  N" D/ u防御HTTP的洪水攻击和HTTP Dos攻击。
3 o. s4 j7 U0 u* _. b' n8 N$ @# H' K" h' e

; y" N: l4 l& x/ L/ @. W/ g) m% tCommon Web Attacks Protection(常见的Web攻击防护)
' h' Q2 L8 V8 l5 @检测常见的Web应用程序的安全攻击。# l5 W! s# i# u; Z0 I5 ~

+ h* P7 B, I3 \( m+ X* \/ |4 t
6 F% f' _. [/ eAutomation Detection(自动化检测)
# x& v' e5 C* Q/ z! f检测机器人,爬虫,扫描仪和其他表面恶意活动。5 K' B9 Q9 o- n- F
' H% f- R# L! T

/ d% P4 H1 Y% I: t6 q* FIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
! [7 O& v6 E& V9 g0 K/ a. {5 r检测通过Web应用程序上传的恶意文件。
/ Z- R& ]; A' f' I- _4 V2 G$ k5 ^
# d3 R( u" v- X4 ?* X
) D  w/ L, v0 c  ]' P( `3 HTracking Sensitive Data(跟踪敏感数据)
1 K4 c, j* H4 _5 }$ |8 s5 f" Y信用卡通道的使用,并阻止泄漏。' M5 Q1 Z7 T7 E6 D/ T$ ]: `

+ o2 A' d$ W! Q4 j  z
! F# x" v; D3 C4 j. X9 Q/ b8 hTrojan Protection(木马防护)
. `; G2 C# I* h- h8 k检测访问木马。' r' T+ ]( N/ o: H
4 W4 s3 a! n( d$ D! m( v0 u
: B3 e# h2 o# b- X) U0 v! w
Identification of Application Defects(应用程序缺陷的鉴定)# Y: {+ V* N; R( }* Y  ~2 @$ ?# S
检测应用程序的错误配置警报。
3 X. s' n7 a4 W; F5 Z2 ?, b% I& P. }! `: C% a* l" Z( y

, e6 P4 h, k. j" ?, gError Detection and Hiding(错误检测和隐藏)  E9 e  _1 Y- d; o) H
检测伪装服务器发送错误消息。6 `6 x3 A8 U2 K4 U8 y. N5 j8 t8 o
8 Z- M9 {& j* K/ }+ c; X; E
; g+ V% ]2 q* g# p% G* b
安装ModSecurity
' }  D% a+ h6 c% {软件基础环境准备
& X' j5 C: u& c6 z2 T/ c3 e+ d下载对应软件包+ e+ @  |/ m+ T1 l, |! s
$ cd /root( p' c' g5 V0 f2 R/ P
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 y7 |% ?9 p: L* L6 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
6 L* P$ p- o3 E) t7 O+ A安装Nginx和ModSecurity依赖包: p: N' q8 [, G5 p% P# S, t
Centos/RHEL
- L  S: f+ Z+ ~# E1 N
# ]$ U  q) s6 }; V* P' X
6 [8 K+ Y- V' b2 ]1 [( T/ c$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
, M. L3 k4 X% kUbuntu/Debian2 X# _7 }) W' z( e
; ?7 u: S8 f( q! H" E
: s0 K- I$ s" Y) m( @
$ 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$ q: Q( |* Q/ N' H, \* J
编译安装ModSecurity
4 @" F) c# z. B' k' X4 ]6 uNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ _$ L4 @% R0 I) }
) E- y6 Q7 F# H4 R$ C9 ^, |) \( `3 m. U: h) F5 P
方法一:编译为Nginx静态模块
1 y( v* M( M& F" E6 b( A, e6 X, A% s# s
7 X9 r- G2 o4 t1 k* ]! c1 h
编译为独立模块(modsecurity-2.9.1)8 D5 w' R* o& g$ D6 t
$ tar xzvf modsecurity-2.9.1.tar.gz7 x) k" q7 ^! p4 H- F* c5 K
$ cd modsecurity-2.9.1/
( a# x6 u% Y+ ~2 S1 t$ ./autogen.sh
2 m& `4 ^5 [8 H+ b  l  a2 ~) f$ ./configure --enable-standalone-module --disable-mlogc+ M  z+ p, l$ A  x
$ make+ k$ w. m/ h7 D! k) h9 V
编译安装Nginx并添加ModSecurity模块
$ x/ T* Z" A3 b, I1 t$ C$ tar xzvf nginx-1.9.2.tar.gz+ X: T/ g( e1 h$ a& l& }  H
$ cd nginx-1.9.2
% y0 B2 F, y' w5 [+ M$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/; P) k2 Y, w' c0 q
$ make && make install
6 h9 ?; F6 |+ V方法二:编译通过ModSecurity-Nginx Connector加载的动态模块. p5 l  {6 X# ~4 {4 T' l

  d- ^6 c/ g& f
6 g7 O2 D7 ?0 H2 p2 N, @3 C编译LibModSecurity(modsecurity-3.0)
0 u: ]2 f1 K# m9 X- E5 J- ?% w$ cd /root
% w- f" s' S# b* Q; J$ c- _$ git clone https://github.com/SpiderLabs/ModSecurity
. p) |, S3 v! j0 F2 G3 J8 X$ cd ModSecurity
& Q& Z( D) l- \$ git checkout -b v3/master origin/v3/master
" b- ~8 k$ F5 r* b9 w$ sh build.sh" _; P& c7 R& t+ I4 \# x
$ git submodule init" C* S! v4 r6 t2 c! q  f  s. @; h6 M
$ git submodule update2 f& ?: F; D0 n/ U" i) ]# A
$ ./configure
- u5 b# l2 c. g$ x$ make
3 H2 l5 u7 l; c7 j$ make install+ o1 c; e7 T, M$ J
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 J+ j% N. Z2 O0 X
0 O" {- Y; J* h0 U: ^$ c. |# u5 g1 _9 n9 b, {4 d0 C
$ ls /usr/local/modsecurity/lib+ ?) K' z: N) {# e
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
, u+ N+ J) d' \) V$ r编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 Y. G$ d# O- Q# h2 @使用ModSecurity-Nginx模块来连接LibModSecurity
0 I& B7 v$ a/ [% h/ e: G; p5 ^, a4 R  E. [* C6 ~7 f. q) F5 N
3 O- n* B* r; `  p
$ cd /root
  D1 c' o+ \6 }4 Z$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
9 K2 U! l. {- a( J; s6 n$ a/ h9 M* {# p$ tar xzvf nginx-1.9.2.tar.gz
( V. v5 K' B4 Y$ cd nginx-1.9.2
& }$ `% S. V2 b  u" T" X+ v$ ./configure --add-module=/root/modsecurity-nginx
" s3 g4 M7 R# k+ e5 ]$ make# V& ?- E  u0 E  j$ ?
$ make && make install# t+ y9 n, x% D) P7 F
添加OWASP规则, P+ y- \5 ]1 E) T( E4 q
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。# ]) W7 Y( o! A

) e1 |7 u, [) d6 I% g0 ~! q) C5 U6 N& ]( k
下载OWASP规则并生成配置文件
8 N& B% s( V& V( b$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, u! y* E; w2 j0 w
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
9 ^* K+ {6 ?2 f" Q, n% Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 [& M5 L* `- r4 ]
$ cp crs-setup.conf.example  crs-setup.conf
$ g- }2 o: B  S7 L3 y$ [; v配置OWASP规则. S, C+ Y7 K; o5 S/ C5 _1 G; Q
编辑crs-setup.conf文件
( a+ t6 r" f3 Q& i% [/ G6 b( L! D! E  ^& V5 z8 N! B4 u
0 B1 j% X; Y! k. e9 ~$ w
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 \. P$ ~* ]0 t4 N" E. r3 q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
: E& T  B9 P; n, }) n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf4 X% B9 w  q" X4 O, c4 B
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
" Q8 `/ ?9 r( C! O6 F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
" G& L# v+ G7 O+ o0 L0 d& Y
7 R1 U! |2 w( e5 w* L2 D4 `; j. D% ]2 o  l+ O, u
启用ModSecurity模块和CRS规则- ~! |7 J! O" A: S  ^
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& i* A' K* c1 [: J+ Y
: `7 T, G, ^/ Q" G$ Y0 @
& h% l1 U+ a' w3 I3 i: T8 L. }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ y( P! n; h6 @: n/ n$ S

2 s, C% H$ z: p$ B0 J" a  O6 M: O( E8 x. ^0 c% u" C4 i3 c1 H
$ cd /root/modsecurity-2.9.1/% F8 v3 {8 l: C* ?- {! c: I
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  # ~5 f7 t- i/ D+ x1 s
$ cp unicode.mapping  /usr/local/nginx/conf/
* h* W; ]4 @3 r5 c$ Y% w( u* j将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# j. k  o5 {6 R2 Z* i- U
! a& i9 p7 [' k) H: i7 k' ?

0 V  h) f; L4 N$ d# R, C3 _% l$ vim /usr/local/nginx/conf/modsecurity.conf+ w4 f. L; g3 J- f8 ]
SecRuleEngine On# W" C9 T/ D5 \3 |8 C
ModSecurity中几个常用配置说明:
! r. U% b) h) B2 \# J& y: N( ^
, e% P6 r& q+ P, Y1 F
+ w' f- V- W- B% l" R% T1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, m; S9 W! o. ]! P) s% s# q

) j1 X7 H/ ^4 x* P8 i4 M" C+ @& @1 U1 k4 |$ Y2 d+ M
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# p7 A7 p. A; a
- q8 X/ i6 g3 a9 |( B2 K
6 a4 X' W0 L: u- o% M0 y
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ t& x: b; `0 t7 w9 `5 V
' `) a; S7 w8 U
9 K5 G; d# w. G" F! N( j2 R0 [
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
8 R: A+ d1 f" g: f* I* r8 U4 X) W* h( E& Z4 p! L) a
1 o" {' f8 D/ o& m
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! ?2 Z. F$ h4 H/ E: p
  C9 R( ^' y& R( }
, \+ _) u! g0 W, |
3.x版本CRS" @  W, u$ g9 v9 D# o+ n- L  w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% T. n% ~' V* M5 x. c4 ]
# 生成例外排除请求的配置文件- g+ k9 x4 [# T2 A6 I
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* }/ y$ g3 H8 v" @$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf$ f/ V) @4 U0 Q5 y5 Q+ r
$ cp rules/*.data /usr/local/nginx/conf
4 i) ^) Q% j9 ?* C' j3 m1 u为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。, U& k" {0 f2 L
. l, X. R4 U" c
5 M. y5 ~3 L+ }# @# |
$ vim /usr/local/nginx/conf/modsec_includes.conf  z% m" ^/ L; W, [) F2 H2 Z3 r
& N$ _# g; }8 {9 M
[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
- o* t$ K5 t, U) @! u  G- W! ?) Z
% M+ v+ z$ \6 k0 U. v
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。  q" Q9 M) `# H/ S; U" a

# R; p& @* J7 P9 s5 a7 B2 q
  r! ~+ _' v3 M7 Q& g配置Nginx支持Modsecurity
- q2 n* n- |1 P& w% M启用Modsecurity
* \; ^; Q- T- |* X/ W使用静态模块加载的配置方法% E" _* B0 {4 ?- z
在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 R7 x3 ^  u9 p' M) h3 b' k9 s3 g$ X/ J/ m* V
) J# D& v, Q  T' c9 r
ModSecurityEnabled on;. C: p, A" p, V- Y& s- `) R
ModSecurityConfig modsec_includes.conf;& Q' u, Z& q; z3 T% C
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- J: W/ z6 T6 A8 C8 _  g  d+ {

7 O7 {/ T0 s) D$ Y5 r
( N3 Y7 F0 r& g7 N$ [4 K6 U& E- s$ vim /usr/local/nginx/conf/nginx.conf6 T( u) p& A, Y1 S/ N- R4 C
! a$ }/ O$ R, f! D* A& z
+ K, d* m/ V! o4 ]
server {9 [0 R1 c( L8 a
  listen       80;
. X# k; l% V! _8 J, p' o- c  server_name  example.com;
) y1 s5 Z* e9 G! l$ c. D- O
: A( h  z# a  c7 }; X" W: D( m' F& w, K" j9 ]. D
  location / {  p( [) n  b0 R, J* U
    ModSecurityEnabled on;5 |* s; {! _1 Z4 g. n% V8 d% A
    ModSecurityConfig modsec_includes.conf;
5 o& D3 n2 y5 K    root   html;
% m! {* U% T# z# W+ }    index  index.html index.htm;+ p* s4 a' ~+ R2 {3 ?
  }
. \; I2 b* m, N* Q5 W}
) s# k3 V% k; W9 u使用动态模块加载的配置方法
! g+ [8 \3 x* T在需要启用Modsecurity的主机的location下面加入下面两行即可:
( v0 j. [5 W" \! U
8 z0 b1 m- P; y5 f  L5 y+ c) Q, ~) Z. q# U7 K/ \
modsecurity on;
9 ]$ F1 n1 _  e* Nmodsecurity_rules_file modsec_includes.conf;% c$ b* t8 a! n5 ?" K
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' V9 K: U: X8 }

8 I6 _, ]# i4 `$ X( p/ ~& J; h' g) ?  ?& @3 k' m
$ vim /usr/local/nginx/conf/nginx.conf+ u" ^% h! ]$ [+ A, d- D4 u: ~  C) h5 N) F
# F3 e5 l( M7 e: U4 m
5 N4 x5 @" \) b- z* m, f  J
server {3 U3 W# P( ]8 F. O# S/ k$ w9 h) i
  listen  80;, `( X6 ]2 B- Y/ r, X
  server_name localhost mike.hi-linux.com;
$ ^. h( j  q8 i# Y& H  c. j# h: x  access_log /var/log/nginx/yourdomain.log;* S# g% q3 z( q

0 E7 J! c% g, C* n
* J. U, ~/ H$ i. m$ c  location / {
2 b0 W5 [; h( l9 M8 O7 y& J( c
! P6 S; j  W3 c& J3 ?7 Q6 U3 R& R! B# _( H/ ~- c" l
  modsecurity on;. z& G6 \9 w' q) A7 W( _- [
  modsecurity_rules_file modsec_includes.conf;. y& W/ i# b+ Y- E0 m3 [
  root   html;+ N/ W3 R6 K/ I3 h5 s! f
  index  index.html index.htm;- _9 p3 ]5 K( }( H, [. _7 I
}2 y" @' w. D) O- c- O) N- M8 D
}" K1 R4 d1 d3 t
验证Nginx配置文件6 p/ s; C6 k) r+ U
$ /usr/local/nginx/sbin/nginx -t
$ V; P" {% y, z  W# C2 ~nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& S5 F" X. e, H% ~/ knginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) l5 S3 l% J: \. B启动Nginx
+ O( M" n  d# e* N$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
9 E! A+ I' B+ r! D$ j
% P. x* Z' R9 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能够战胜更多复杂/未知/混淆的攻击模式。


" F' h0 c5 X7 F0 \8 }

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-3 18:04 , Processed in 0.062405 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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