找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10710|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。+ d3 {- C" }7 t) t; t0 G
8 e" T/ I3 ~: A- i" l
3 h  N, h  K9 ^& n; y+ W0 @, }
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 A/ [/ }" u) ?1 X: b) a: `2 g
+ `6 T( `( B* s; M5 _7 u
+ _4 F* [6 s8 J& M  K' ~什么是ModSecurity4 h0 y& Y( D+ }  J: m
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 A- r7 X2 n3 i' G. p' U9 C; }% d# ~5 r; A) n9 U4 q+ |$ J( A
. n+ x( ~: B. G" j$ h3 d! ~9 y& G
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
( l* g' g; v- l* e$ e5 A+ ?: k5 G3 y4 G+ Y1 _
. [8 U6 \0 Y" i- B
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
0 B) ]0 H0 u2 R. B5 _3 X& M
8 z2 P- p( Z5 h, E
, `' S% e, u. F& X* f官网: https://www.modsecurity.org/5 T! f/ b3 Z+ Z* ?# {

! h4 p3 S; E/ X) {5 g$ g- t& Q  E2 B: A* m, V* w, c# t/ a2 m
什么是OWASP CRS
9 K. n- `2 U" C5 T4 L7 ?; k! I$ @OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
, e# A) @8 O3 U& K- H  b/ x# Y
; G1 u! ^' \, [, W% S1 Y8 ~, U! a" t
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 d- v6 K; e! s4 a/ M! P+ T
8 L: ~" `3 r4 x# x2 E6 c( X+ S- ?  m* z7 F1 |6 E
HTTP Protection(HTTP防御)
. z1 g1 y# A: F( c* r. \HTTP协议和本地定义使用的detectsviolations策略。
" M- B3 ^0 b. [2 p4 [! j" ~5 b6 {/ R
! [0 A' i# L/ g, O  b
Real-time Blacklist Lookups(实时黑名单查询)0 B" Y9 a- n, s/ c- e! I6 k
利用第三方IP名单。
2 S% }1 f* E. j
! Q# Y8 X- q! W8 ^4 @4 _
$ p7 \& L) T& }  o0 v; iHTTP Denial of Service Protections(HTTP的拒绝服务保护)
+ X1 Z* G7 v% ?5 g7 E: j防御HTTP的洪水攻击和HTTP Dos攻击。
9 n/ i) o0 S' v6 V6 S- W7 S6 F
* G& e) z8 T- N: F: D: h) T" u: V: F* W) t
Common Web Attacks Protection(常见的Web攻击防护). J6 [  ?3 `  q, X/ L7 w
检测常见的Web应用程序的安全攻击。, s7 b# Q1 @% l" `/ i+ T' ?

) U" k0 D; W% z/ P9 n* M: F6 o! I" n
Automation Detection(自动化检测)
3 O+ L& P7 ~, }5 X% T: _检测机器人,爬虫,扫描仪和其他表面恶意活动。
, h+ v# Q: p4 u7 o- V) j0 m. f  U; V9 {) |' {7 t; Y& W5 e
& x2 j3 m: a6 o! G$ o  A3 D
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
: p+ t0 G& X& q5 `4 ]. n5 _检测通过Web应用程序上传的恶意文件。2 ~& @1 |( w+ t  K3 L/ B0 L% k

- g- f6 m6 ~9 R2 g' ?2 ~* S) k$ E2 U0 a- {
Tracking Sensitive Data(跟踪敏感数据). J$ R1 B" A" }
信用卡通道的使用,并阻止泄漏。$ s5 Z2 N# D! m) F8 M# C9 j
# I, [. Q- ?! i: @

3 e. o9 m2 \. STrojan Protection(木马防护)
. C9 X" R$ t/ ~0 i! t. E% j检测访问木马。/ Q0 c: y8 }0 E+ n& ^

) i( S) l$ _& e3 D4 f/ t4 r7 c+ V7 \: w. X3 @5 D
Identification of Application Defects(应用程序缺陷的鉴定)( x3 K  u7 D. k% n1 c+ c. w
检测应用程序的错误配置警报。
& H7 K1 L) Q. e/ Q% L3 k. ]; j$ }% K- `9 s

( \$ \9 O; w: R. b& HError Detection and Hiding(错误检测和隐藏)
, i' L% W$ m& N8 k检测伪装服务器发送错误消息。0 Y3 m7 u1 p) K7 j! S
2 r2 p+ X3 O+ [+ H/ G
$ j6 U7 B7 ?+ l+ ~/ u4 t; N
安装ModSecurity, u% f9 p0 o5 h' Z) q" r4 E$ X
软件基础环境准备0 d  E1 U6 n# ]  Q# N+ T  |8 j. y
下载对应软件包
0 V6 ?: Q' e* V* t, l$ cd /root
" I' I+ u' |! V: U+ h- }0 S) b$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'& f+ F% x; W0 I2 ?2 B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
# m/ ?7 B1 }0 k/ L. Y安装Nginx和ModSecurity依赖包$ w4 {# d$ N: \* b4 ]5 j- t
Centos/RHEL
" c5 Z7 X, _  A, x
+ A. {9 v7 v6 K  o$ o
: s( I( ~$ Y' o1 I7 E$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel- P7 p* `9 B' o4 a9 y* z
Ubuntu/Debian* W; \3 ~3 V& \# C
4 D: K' g5 ^- r# m7 R

9 N' a3 R. n" f7 i* }# p& F% J! e  I$ 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
4 h1 V& }& o" m+ f* c& z编译安装ModSecurity2 F2 F$ l" ^* e5 v' S; ^
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
+ f* \( ~0 R2 a- o1 u) E3 u8 J9 [' ^% n1 w" {% B
& P5 ^; y" P; C9 L
方法一:编译为Nginx静态模块
: S% l& f! F/ W- \9 e& x& k
) c; d; w- t3 f3 y$ k" H+ p. I0 `1 D( i) e9 t
编译为独立模块(modsecurity-2.9.1)- s4 x. L- y. M5 g1 r+ p$ l
$ tar xzvf modsecurity-2.9.1.tar.gz
: C; j6 P% s+ L, m$ }- j8 f6 w$ cd modsecurity-2.9.1/
6 Q$ J, L/ w* X  Y. u) D$ ./autogen.sh/ ?( k9 p5 O" P% q8 g4 J
$ ./configure --enable-standalone-module --disable-mlogc
% M/ `. ^  X5 I6 p3 |% B$ make' f' A1 i& m6 Z* m
编译安装Nginx并添加ModSecurity模块, A) w! {6 b3 ]( F
$ tar xzvf nginx-1.9.2.tar.gz
1 \+ e9 T3 Q/ E% Y( C$ cd nginx-1.9.2. B+ Y! c/ x, g) S- F
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- T5 x$ E8 w6 f4 b; a0 s0 n
$ make && make install
( k9 U- R) w/ g3 o4 [& X方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
) Q9 [' m) j* o' Q& h4 @) K+ B3 G) d. u& Q

7 }% G1 Q, y: N$ a$ y' Q3 y编译LibModSecurity(modsecurity-3.0)
1 t- E1 R4 b6 W0 \. _/ K$ cd /root
; k( s6 J) f, G$ |: c2 m$ git clone https://github.com/SpiderLabs/ModSecurity
! _% j4 A6 c# t$ cd ModSecurity
3 A% h5 E: w, d! L5 Y$ git checkout -b v3/master origin/v3/master% n. S. a7 ?& W! O
$ sh build.sh! h3 O! u, y9 Q# B& K, h
$ git submodule init
+ G1 H" D% \7 [1 U0 U. }$ git submodule update
) w7 @' J/ O: A4 y8 Y$ ./configure+ b8 J" e5 P% D/ ?! G5 \  b
$ make) m' L: A# G; O1 J: O' x" n
$ make install
& s1 |1 J3 _- b+ x2 ~3 cLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。" ~4 i! E( v% ^$ W; ?
: J% u( M- S& Z

& W9 d- d5 l" {3 ^% Z* t( e$ ls /usr/local/modsecurity/lib
% A" o# L* Y9 p* H2 \; d+ l% }libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
) x. \4 a! [$ U% ~' [) s编译安装Nginx并添加ModSecurity-Nginx Connector模块  ?- M. A) P" r
使用ModSecurity-Nginx模块来连接LibModSecurity
% u$ k  L/ n0 }1 l/ ]1 s$ \- V) O. I3 I# |  @
! j/ }( e8 X/ ~9 s. T- ]: E6 m
$ cd /root
+ g$ ?# f( e  |+ C$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx/ V1 R" ]; R, m- ^1 Q9 g6 a) t
$ tar xzvf nginx-1.9.2.tar.gz& F: ]4 X+ n- r! u" f
$ cd nginx-1.9.2
- h3 J; d4 ]. K3 |$ ./configure --add-module=/root/modsecurity-nginx% c+ F2 q( _5 F. q( j( `. `
$ make
+ j$ H+ m+ A6 Y1 S" i! H$ make && make install
& `  q  h& ?0 I. A3 t添加OWASP规则
+ N# k9 M$ B  U5 gModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 n% H, O1 n- \! i4 Z$ @; e9 R
8 |% r. J. b! O& S& h: e! I
  ?0 \; o0 N& h& j下载OWASP规则并生成配置文件4 J3 F1 x) f: v% ?
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git* U$ u6 u, c: K+ m/ I/ u& \
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/; d# b' t9 q! T+ B* X4 P4 n0 ]
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; H$ Z9 V: \2 f0 i7 x1 L* @$ cp crs-setup.conf.example  crs-setup.conf+ y3 O" ?" |' N7 F. F0 [
配置OWASP规则. G" M' w4 _) \7 Z
编辑crs-setup.conf文件+ V; \8 P. Y/ n: D* p. |# b5 _
; J9 l, M, J1 F
, y. }( u# i( e  c
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
6 E8 z5 m/ G* ]+ p, L* v$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
! o$ a) `/ ^  [; A$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
% |% L' i" P$ g8 B- d$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 W9 i: t( v& L: u, v' V: r默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。: Z* T) j8 F* \
: m2 R7 z3 N, T  m, O: c* }  S

5 w5 x% Q) v4 u1 M8 Z; z启用ModSecurity模块和CRS规则
9 G# M* Q: f5 p复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. e0 q7 T" V  q& f' ~( o9 v* }( i. f) b; ^
0 _2 E6 V& S, G! ^
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。# G& H: M7 A8 h8 q; W
% ?1 T0 ], f: F
/ W$ T9 V+ |" P2 q
$ cd /root/modsecurity-2.9.1/
6 w+ }+ j/ N# }9 \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  # B% a4 ^3 j9 [
$ cp unicode.mapping  /usr/local/nginx/conf/
% n8 U( ?2 I8 \" n% V将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" ?: C0 E: y6 a; \. e0 L5 a: M, x4 b' V: d/ T
% l; y' k1 F0 I! @- C0 F
$ vim /usr/local/nginx/conf/modsecurity.conf' N" |4 y9 t  f$ T" p# w/ l7 y: P
SecRuleEngine On
' N9 b& r" ?" E. z  [/ aModSecurity中几个常用配置说明:
" W9 m4 ~6 }5 O9 g) L0 a3 L7 s+ f8 C% A  v' i$ S

3 ?7 q. Q+ a% H: m" L1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
6 C1 ]' |. f5 x, x5 k, s5 P3 f
+ }- d$ o* X8 g9 b7 [0 l3 O8 L3 \9 L% }8 ^7 K( p- K; h  _$ d
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
( H+ I4 i4 I& c& C9 d; u; w7 x1 X9 e, j4 `- h' Q
) w- e: I9 n4 ^% w& Z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
& y' j. \) i% q2 q7 F" Q2 p" J5 f  m# g- S2 u( N

+ X! s8 a& Q3 f) k4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。9 M! X- k) p) e/ P0 c# b9 }$ b! a
( K3 x. @1 @! @5 d: V
8 \' l' B5 }$ F1 K. @$ F
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 M5 L2 @0 M) l4 I) j6 _* X' u
; n/ d: Z# ^7 }+ o* p* D  \' ]$ r  I  m* `+ ^. d6 F9 A0 j
3.x版本CRS! g& U* B: e; z1 Q$ R( H' c. p
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 a( ?- Z5 I' @# 生成例外排除请求的配置文件. t- ~% i: ^  u* W7 O  U; G- W
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ w5 e! T% w2 N8 I5 ]$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  D9 i/ c! t7 U7 o2 P" O
$ cp rules/*.data /usr/local/nginx/conf
8 g6 ]6 s: B( |为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 U" ~, b$ ^/ z
+ r0 N* ~2 K; X9 C" r
/ t$ Q. }  M% o3 u* w+ i
$ vim /usr/local/nginx/conf/modsec_includes.conf
5 W9 f: W9 {1 [4 Y9 i, p6 A! M3 F. P( U' e$ W% h! a
[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
) n2 ^+ U  g& Q# ?$ A) _# t4 n

: k7 q3 r) }# l% c- Q注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 y) S1 [+ N5 w
( x; g7 u' \! a* Q. B- a9 f8 M
1 V8 `8 P5 ~' ]; |9 U* h
配置Nginx支持Modsecurity
: p- w2 c! Z7 d& k启用Modsecurity8 r# a4 r" U0 z
使用静态模块加载的配置方法
! S6 V  c7 \6 A; o/ @2 h; Q( E2 e在需要启用Modsecurity的主机的location下面加入下面两行即可:7 ?8 Y( d) N" i& M5 L( ^& i. a
2 \7 g% d7 V# j- m$ L# S$ A

2 [; w6 H( d2 T8 XModSecurityEnabled on;
* z* \# G3 X" t% n9 S' wModSecurityConfig modsec_includes.conf;
: K& x. T8 C" a8 G- ^7 W修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# M; N5 r. ~) ^: L' S/ \6 x+ [' ~% a# K" O  K

6 b  Q3 Z7 }3 @" `$ vim /usr/local/nginx/conf/nginx.conf' v9 Y, G9 {& T1 r6 S8 F4 G! k) I

7 s* `, ]! G! N- Y& D1 V! Q( ]7 R' [6 b  N
server {' N6 \+ `  m4 E5 D3 T7 ?
  listen       80;$ v# f1 e$ I. m5 Z# H  p7 q) d/ u  _
  server_name  example.com;
3 U( r5 a/ I& m5 m$ P; x  j1 ?! R/ c9 @
) f" B" b- I$ [9 R- i
  location / {
+ G! E1 H  ]7 \$ o9 ~! U3 @    ModSecurityEnabled on;7 @# a8 k6 c/ \$ z7 e5 V( j
    ModSecurityConfig modsec_includes.conf;
, g! w1 M: F, c    root   html;
! j3 }/ m+ u; c% i; o: b    index  index.html index.htm;
0 X$ l9 t/ z' w9 ?4 l$ W  }
! h( h' I" f% d& v( n}
; Q* x6 V) K* r9 c' i0 [7 ?/ G使用动态模块加载的配置方法( O0 a" `. C' B& P
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 \: e/ G% l+ G: n+ A( o
. r: {9 J- p# F  F" [0 {1 |+ ~( @1 V
( X* S7 L, G* r' z, e, S
modsecurity on;8 A( t* J1 z9 G& b$ c- N
modsecurity_rules_file modsec_includes.conf;
/ B" _6 K& T. ]0 j& A- p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
4 @& u  x1 E% t/ v; h
1 V* J# B$ n; [- R8 q2 s8 @# J$ ?
. o+ s8 a) u% T% J9 u$ X: E& h: @- l$ vim /usr/local/nginx/conf/nginx.conf' I3 z, ^- k% O- Q
4 Q! G9 N+ V/ ]* ]" K4 Q& u
. D; h, m  D# X- F
server {" N5 _5 x# f$ {
  listen  80;
3 k8 \/ N3 N0 F  server_name localhost mike.hi-linux.com;# r8 {8 Z6 Z- y9 ^% ]4 A9 b
  access_log /var/log/nginx/yourdomain.log;9 X5 c  j; ^& W( M9 ^) m

! a% [/ x+ H  |# Z# c8 [8 ~: S$ P4 M6 p
  location / {( {" g  K5 q3 d

( s3 M; o( y" L3 h, X
3 d% X; \- M  \5 Q  modsecurity on;) e, ^2 r7 J* l, e9 d
  modsecurity_rules_file modsec_includes.conf;. b+ u: [5 e* `- z* ]" }! c
  root   html;
2 b0 ~: _$ w1 p  index  index.html index.htm;3 F3 h8 }, d( R0 @' D" q! R
}8 ?) w* ^0 z2 z/ e% e
}
; ]0 V1 l+ p6 G! _' _& e7 K4 w$ }验证Nginx配置文件
/ D7 E8 m' z# n" b$ /usr/local/nginx/sbin/nginx -t
3 `  Q1 ^% n) N# r- @) l4 ^5 `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
( U; g) z; G4 Y& |7 Unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) G" D7 ^* `: ]0 P2 h( W启动Nginx9 s) r; w/ D( {* h
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 `. f. ]3 \- I5 ]# r; \0 R% P
' z* U) R( C3 G  T/ J/ x8 }

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

5 C! n$ @# G0 Y" g; G) k/ I

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-3 23:09 , Processed in 0.082411 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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