找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11722|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。: S9 J6 E1 f5 L- X" a: b* d

9 ^0 s4 C* L6 l/ E8 ]$ j: F4 r* ?- d$ [4 M/ R- x& B
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: E2 G  |0 Z/ V

8 ?/ [; h/ {2 M4 r- b7 `' Z% w6 Y. p- z- c) q' j! _
什么是ModSecurity  }3 \" ?3 y4 L5 n) i5 |7 W8 u8 i9 a7 \0 S
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: p9 V9 k+ i  Q  P
7 d: O3 Q$ t8 B9 ?; I- E" H

3 `  o, d- u1 O; Q3 QModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。$ e2 _' s) V; H; R, R! ~
) K1 l6 J1 W' N% A2 x" z
6 O% U' O& N4 u
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。' p) Y* M: X5 h5 r/ W+ r
, u7 k+ w# o' }& I1 w0 z
6 q" m$ m* E4 E% Y7 r7 [& }. Q
官网: https://www.modsecurity.org/
8 t6 w* R$ e3 ]) S0 s9 f7 j
" q; k$ y4 J' N( u8 h. L0 t8 M
什么是OWASP CRS
+ R; }: }& }; k% ROWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" L" O7 _, n+ J, F5 {: l7 R
# U9 u! t% q( ]0 o+ w' [& o( S- a$ w/ K
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 ?% {5 m+ k) _* q+ N% _
, e6 o: ~( N( G) y1 }& ^6 [+ {* j6 i0 E% [% u/ l" o  Z: W! O. d
HTTP Protection(HTTP防御)- r$ [0 N7 g+ ?+ R. N6 V
HTTP协议和本地定义使用的detectsviolations策略。. N$ |# [, V: G& C$ Q% O2 l& C
, F2 h" K  w5 L

- N) z! W& v# \$ F2 w0 F4 uReal-time Blacklist Lookups(实时黑名单查询)3 f4 O* _9 X- P' c) b) s) r
利用第三方IP名单。
; H5 {9 z8 J5 D- Q
2 R, r- T1 |2 E2 W2 J4 o, r, K$ a. H' R" c9 A
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
: ]! m6 |/ ~' T# [" V7 J防御HTTP的洪水攻击和HTTP Dos攻击。9 c3 i! S# P+ h3 e" b$ N/ ^
* N) F3 k8 }9 M1 k7 f
& v/ l9 j, F1 |; r  v' [7 C
Common Web Attacks Protection(常见的Web攻击防护)' F8 f8 U! z; B
检测常见的Web应用程序的安全攻击。
0 h& |* F& K- {
1 |1 B' H% @8 [& \) z) x9 a/ B- `' g" j/ V; i) N7 c
Automation Detection(自动化检测): }6 P0 G! e3 v5 n2 O
检测机器人,爬虫,扫描仪和其他表面恶意活动。
$ q7 \, U* {5 _! J  {/ m5 i2 _! F  ]2 ~3 n' `% {4 q
& D5 T% v8 \& [# J
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)8 K' \/ T2 Z2 A4 n, l$ A! P
检测通过Web应用程序上传的恶意文件。! V5 L+ p% v' o& f6 Q1 \8 t: }' `7 C

3 P: |& c4 |7 u' O1 a, ?8 f; s% w; `. W8 z
Tracking Sensitive Data(跟踪敏感数据)! j  d/ p; A$ M' N0 t/ N# H
信用卡通道的使用,并阻止泄漏。0 E9 m+ S% R" v. \5 L5 Q6 ?& B
+ R' z; p, q. v+ ~9 A
" b! u2 a$ v/ B5 P9 b
Trojan Protection(木马防护)
* G$ E7 c# G" E检测访问木马。
! Z7 S4 J4 ?7 ~( ^6 u% n& `# @  j7 W
: W; j) l% E8 ^8 u+ E4 P, i/ o
Identification of Application Defects(应用程序缺陷的鉴定)5 Q; {! ?/ s! ^$ \+ N# D7 T7 H
检测应用程序的错误配置警报。
0 f5 P6 j$ u; }# A  i3 k% U' [4 Y- s! [' ]7 u& B

. A( G2 K% I, v, c2 ?% N4 n: p& tError Detection and Hiding(错误检测和隐藏)
1 P  K7 x  g$ `* l- t2 N检测伪装服务器发送错误消息。; V7 l* B9 ^2 Y5 s6 ^
- c8 D4 |' S/ [* N+ o

1 G, L* I2 z2 E- D# {, `安装ModSecurity
2 {: f% s0 f" R软件基础环境准备5 q) B2 T( W+ o' s8 u$ L
下载对应软件包3 H0 Q$ l/ S" T: D
$ cd /root
5 h( T2 _# b$ C$ C0 w$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'8 h, A4 {: [) i$ `- E4 ]
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
+ v$ n% e7 C8 z2 K- ?  I) |安装Nginx和ModSecurity依赖包( m; V" s, `3 T
Centos/RHEL
, d/ E& m7 B# C9 u" `1 f5 @
$ C' g3 f1 h/ y* h3 \
' `5 V  c/ L% v7 w* _0 ]$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
: o! m$ q; Z8 }" s! Q, u1 jUbuntu/Debian
) `: }; V  R( T& `
8 V+ F( ]- e4 J! o- Z9 X1 ?3 j# F2 `6 d. e
$ 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% a; _4 F( a4 p0 u
编译安装ModSecurity5 e7 r, f/ I* L; J& i& p. H" b
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; h, G0 `' n- q7 Z1 y- v* S
1 k; k3 s" C- b+ ?% n; k
& u8 m: Z  Q$ M$ o
方法一:编译为Nginx静态模块8 z9 u+ y" z5 b( \, [/ g  Y

8 G2 B) Q6 T, X: _9 L* f1 S/ j( m5 ^- \4 p- i6 a" ]* L% M( A
编译为独立模块(modsecurity-2.9.1)- c$ [$ w$ E- K, X
$ tar xzvf modsecurity-2.9.1.tar.gz3 q! Y2 K. ^+ W, J; U; p; N
$ cd modsecurity-2.9.1/7 W6 Q" s& q4 i7 b) ?
$ ./autogen.sh
: V1 a' z- F. y  l, n' I$ ./configure --enable-standalone-module --disable-mlogc2 }* L/ i- q1 {( Y5 `# n- |
$ make
' ^* w) M$ |# }1 t3 f编译安装Nginx并添加ModSecurity模块: o' n, R0 |4 Q7 Y
$ tar xzvf nginx-1.9.2.tar.gz
1 b- \7 j% F1 v$ F$ cd nginx-1.9.2
: V' U6 h' r" y  @) B9 M+ {. E$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ R4 u* c! l* U
$ make && make install
1 M" @* w" |/ Q' ^2 c* r6 E方法二:编译通过ModSecurity-Nginx Connector加载的动态模块/ S" K0 p7 l  D& ~4 p& ^" y$ H
+ A2 d: I. J5 T. i6 t# T$ o
" y* O- H& F3 |$ S* P* V
编译LibModSecurity(modsecurity-3.0)% i# j3 n1 _7 L+ _* `$ m! B
$ cd /root
0 X" i4 a2 G6 x$ R$ git clone https://github.com/SpiderLabs/ModSecurity# j7 H3 _' `2 \( k: i
$ cd ModSecurity/ p2 W6 p; C7 r, M: Y. b
$ git checkout -b v3/master origin/v3/master2 _) v2 T- K3 \: T+ M3 c) m% u! n
$ sh build.sh
7 d7 ~. q- p8 a/ N& Y7 ~$ git submodule init4 [0 s8 o( [) u
$ git submodule update
. a7 O& e$ H8 E% s; d+ V3 j$ ./configure
; k) C8 X( ^9 W  W4 |$ make. Z! Z' a5 H; I0 N
$ make install$ ]" a2 G2 {. d4 `# v5 n* Z
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
  w. R3 ^+ W, M+ s, O* x
7 D) F1 l: t  k9 l! ~8 Y( `' Q  T/ z
$ ls /usr/local/modsecurity/lib
6 h2 @/ @( g: Rlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
' F* W1 S1 K% t1 g  S+ i, Y编译安装Nginx并添加ModSecurity-Nginx Connector模块
3 J! ~8 r' f2 r1 ]" x; W使用ModSecurity-Nginx模块来连接LibModSecurity
- v2 Z* M9 n+ Z/ h0 v. L
1 d$ ^, V" o% o1 X/ C
9 L4 G+ J6 B" r* D- K7 F" m  F$ cd /root
' Y* L4 W4 `3 Q+ K& v$ I: {$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx5 z- A1 c# Y( N) j4 v$ B! P) R
$ tar xzvf nginx-1.9.2.tar.gz$ r6 ?- a% D" e0 B' P; a1 @' q
$ cd nginx-1.9.2! j9 s6 s) C9 E
$ ./configure --add-module=/root/modsecurity-nginx
# W' U2 R! K: Q1 z- W$ make
  D2 E; y7 Z1 S$ make && make install
. q8 ]: V# s  ~" N添加OWASP规则3 u" G4 T& B" o/ M4 v
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。" h* H" T8 J5 _( `" P

" ?& y* O( U2 S& H# N; D0 s
0 O* [7 _9 N  e9 S& z) t0 X下载OWASP规则并生成配置文件
: f( u. I( I+ t& E  @5 G2 k- ~$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 u, P+ i2 b; w7 D
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/% h9 G; z6 V" ~: I2 W3 S
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 K  Y; s% y4 n% y! W
$ cp crs-setup.conf.example  crs-setup.conf4 n3 [+ _6 c7 v5 w) J: _
配置OWASP规则! O0 F1 J. t9 k) o
编辑crs-setup.conf文件
% a3 r" ^* _* M; }5 V4 W+ D( z7 ~

- N; e) e. ?+ Y& H- e1 s$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
) b) p' A9 b" k  v' p# T$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
& |' J* H2 p/ Y% A  V8 a! O$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 W: N: v8 ^: J' }( D$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
% `7 X0 O. Q+ Q4 t& V默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 r% F5 x4 m5 x2 \4 w. r

. |3 d; T2 y) h: a, O1 v5 x! w6 {& M4 _: u" s1 z* v5 |
启用ModSecurity模块和CRS规则3 Z9 m, ^- G% C2 X
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 ~; Y0 U8 C/ n: f

& F- P" Q9 W& b7 t! y; }
" F0 Y7 r8 W1 v  G: U) bmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
* e$ v. T2 h: K) `1 u1 \! _
7 b; d7 I1 X7 w' Q) `/ ?) p5 m1 S/ h% N7 }8 z1 Y* @
$ cd /root/modsecurity-2.9.1/
7 {8 S9 Z; J3 Z5 F$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
; z8 L7 c& O& J+ f+ Y7 a/ W$ cp unicode.mapping  /usr/local/nginx/conf/4 T# H1 K8 F, z% R; m" k/ S
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
% c3 }' x% g: z% U$ r2 |5 |4 S; [2 n/ N

2 P! C5 G3 \( l: x& f" @$ vim /usr/local/nginx/conf/modsecurity.conf4 {" X$ [; g- D2 ]4 O8 D* [
SecRuleEngine On* f8 g# B$ V4 J6 o) t6 G/ R' k  Y4 ^
ModSecurity中几个常用配置说明:
4 X- s# R2 ]( p& t5 }6 S1 g5 e% D# ^6 s( ^0 I' Y

, D+ F4 M' V+ T. \- t" M( r1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
7 N- z3 g6 d; W9 R- J0 t
7 ~/ |+ H. e9 s' X  u8 M
4 `8 |# x$ u5 q7 i! X2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。- v4 b6 [8 X2 m; }

+ S( E& a, C# s# |
+ i) N& {  n# o, q  U+ p3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
) p5 E: e/ c1 y$ l  _2 f
- y  I+ c( t% S. e+ t+ p4 H0 V7 h; @, {- X
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
6 l2 a; W0 y4 W0 d6 @, e, C  N6 Q. n. Q6 N. z( Q2 ~' o0 U

$ m) n* B4 h0 v' c9 I在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
: G0 h  @5 \1 b. r9 V' r! p  a! ^, I5 \

2 g# z. D' o( b6 G' w4 o% A2 z" ]( V3.x版本CRS
9 `& C( q7 S6 O. Y. ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; S( n$ L/ r9 c! s5 A6 G; f# 生成例外排除请求的配置文件
& v" y6 ^/ [, P+ L/ A$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf: k6 Q) e# T" r0 t
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
4 W. ]1 Q2 k( m0 z/ ?# [$ cp rules/*.data /usr/local/nginx/conf+ U+ G) h# f" B! G8 X/ i/ t
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。9 G/ H$ W* z% S' ~* X% e8 w1 c
* w/ b- T( g/ N2 }( ]* |6 E
3 r1 O8 P, N+ P; L1 D$ L
$ vim /usr/local/nginx/conf/modsec_includes.conf. x- }1 [9 ]$ T3 m: ]) N8 Q# U& V
# b& L- Y9 L) ~9 t
[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

  r( }4 J+ C  s* T9 B& t; `1 [
3 f/ m' B+ {& l3 D- J注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。# [2 Q3 j; T9 j; j( H1 o

; M  z9 I" B; z5 i1 |9 k
- J8 P% R# T1 r配置Nginx支持Modsecurity
5 N) X# [) g6 a- @) ?启用Modsecurity
) `+ w) M9 \3 I0 a使用静态模块加载的配置方法
1 V3 v& c  |9 U. A# G' {0 Z4 j在需要启用Modsecurity的主机的location下面加入下面两行即可:4 q; `5 g) `8 ]7 D
. \7 ?& D- g, _$ }/ u

% k  e. E6 L5 y8 a% xModSecurityEnabled on;3 \# m; a( q2 E* o
ModSecurityConfig modsec_includes.conf;
, j& A* q$ v( S; n6 R' D修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. S; [+ Q% H( a2 I* g% }

& q. Q& D  ~% f" Y. e3 ]6 M: l! b9 ~' p1 w% z
$ vim /usr/local/nginx/conf/nginx.conf0 e5 N9 G$ Z- Y+ V! }

/ C, }7 B3 z# H4 Q$ b
( Y  x+ l4 n( E8 j6 W9 [& Wserver {7 U$ r4 e% c; S/ Y/ i
  listen       80;3 @0 z6 N- ?$ Z$ T. x
  server_name  example.com;: h. a4 p- ?7 U: \& Y0 O

4 z4 F, Q: I  a7 }( n) B! Z( Q3 s; Q3 V$ E0 g0 R
  location / {
" c) J7 m' B$ v    ModSecurityEnabled on;
$ `( ?5 E" n3 ~0 B" \/ n$ \    ModSecurityConfig modsec_includes.conf;
. P6 G% s* l$ F6 w) Y    root   html;+ o1 b5 x* c  R4 q3 M0 ]3 u
    index  index.html index.htm;
, r  m$ X& Y. Y8 W6 e8 ^" @. }  }+ K# G& V0 q; r7 `1 i: h- h* E9 ?" O
}, Y+ N7 l4 J9 u
使用动态模块加载的配置方法
4 Q; X7 R8 u- d5 ]; a1 o) H在需要启用Modsecurity的主机的location下面加入下面两行即可:. M, p2 m8 Y. q0 N# \+ s
( k0 I2 V" E3 V
1 T8 r+ W; ]" o# G
modsecurity on;
# ~. X0 S8 k4 H0 ~) C! Z$ C6 N( P- ^. u1 g  gmodsecurity_rules_file modsec_includes.conf;$ [" \& E) v1 D# D9 b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 U" W! l& x$ }/ Y$ E3 q
8 A0 i1 D9 @5 Z+ y

% ]2 P) O& K9 s5 N$ vim /usr/local/nginx/conf/nginx.conf
% O8 Q6 C- M% H4 h6 y) ~
2 W6 x& E1 E0 g6 C+ h4 H& l7 {: T# i) _
server {
  u* T& ?/ d4 Z! O  listen  80;
7 ]1 C' y  g$ F5 C  server_name localhost mike.hi-linux.com;
" Y# `. g8 N0 s/ m! ?! A0 b/ S  access_log /var/log/nginx/yourdomain.log;
2 k& A3 M, \2 H* [
+ l* h* @0 h. q& L
4 N2 @, B, }; ^/ Q5 g! ?3 c  location / {. t' k3 @% K2 B2 M
) [: m: @9 j6 j" m. t, V

! w, N& r# E, ~( P: n  j  modsecurity on;
% c' R* O3 ~3 v( g  modsecurity_rules_file modsec_includes.conf;+ g) M1 n$ Y8 _+ U+ z
  root   html;" J. Q* ?2 h- I( X/ ?3 W
  index  index.html index.htm;
& J$ n6 g) |- d7 C# v/ A}' M6 d* n+ D7 B
}& }7 ^9 L( y; v8 D4 r
验证Nginx配置文件2 J3 T: _1 Y4 _8 {2 n
$ /usr/local/nginx/sbin/nginx -t& G& V& T; v- n% F0 J$ C
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
+ }" j3 c9 r6 R- M) anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
0 t0 n/ Z5 P! h( D启动Nginx; t- x6 d0 A4 m, V2 v- W
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf1 I# H* ^: z/ I; g$ N5 q$ W" g

8 u5 d, l1 W( W; Z

测试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 U" K2 [# p1 G  x+ ^7 D/ @, p

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-7 19:36 , Processed in 0.078998 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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