找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9471|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. h4 g+ E: e& R. s" k0 J

2 D, R# M5 ]1 m( b" P4 t) C8 U
6 g6 h7 W: y! s$ p0 w在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 K7 {. X# _! m3 h% j( f9 t2 Y6 M- R: {9 M. n# D% M# F$ Y
( E4 M4 _4 I- G* X) y
什么是ModSecurity
6 c( u  b  z9 w4 ^3 N3 uModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。. H) @+ c$ T" }, L0 S( ]" N
: J  f0 d1 u; A4 f( a2 \
5 X' _( |3 ~% ~. q6 t9 e& }4 R2 g
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。' C3 v, Z4 [9 F& \0 m- {6 G
, Y7 _; N# F% v

5 i9 j9 x$ J( @* A7 GModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
4 K- X5 E  u- I. p' R7 ]! v' k, ^2 G4 g1 M6 _7 u. ~2 K

1 k  g2 o/ f8 z; G4 w( |$ `官网: https://www.modsecurity.org/
5 f; ?* u6 [6 R8 Q* [. t8 z0 h3 O) |; u2 A# a3 J2 O9 R8 v

4 X8 ]+ z9 }. `3 O什么是OWASP CRS
% @2 L0 X7 o; zOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 _- ~1 K* u" a1 |$ K4 A

8 Z! V, {7 c: D; q" `) i* _, b4 m
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 Q# P/ F9 M& p9 G+ J. u# z

2 `" H& g) z5 `' c- j
3 p, W$ ~- d% q  iHTTP Protection(HTTP防御)
, y# S, H; G# X% H4 oHTTP协议和本地定义使用的detectsviolations策略。5 r5 v) i, I) c" w
, F5 b% {9 [& N- ]

1 \! G' e5 \/ ?" \# E2 o7 ?Real-time Blacklist Lookups(实时黑名单查询)
# y7 f/ L  y) ?6 U利用第三方IP名单。
4 V- G; e3 ?6 V* l1 u$ t
; L* g1 y" \  S  a
7 M6 T% q& V& {6 jHTTP Denial of Service Protections(HTTP的拒绝服务保护)
" H6 e$ m- I8 V" ~, t3 }防御HTTP的洪水攻击和HTTP Dos攻击。
" s( l) |3 ~$ ?5 R7 |7 B4 d
8 u1 D5 {* O; o
+ ?/ I3 E; c& s4 g2 }) w4 P: DCommon Web Attacks Protection(常见的Web攻击防护)
0 }1 L; j3 Q* @9 [检测常见的Web应用程序的安全攻击。
! w2 g; Q% g5 u/ o- O  i
9 ^/ y# W) q( W$ h& K9 N. ~5 i
7 z4 _8 Q  A, h' K- q  @6 l$ tAutomation Detection(自动化检测)3 W1 F  {% w0 L- E. `
检测机器人,爬虫,扫描仪和其他表面恶意活动。
: N3 h4 A) x% w2 `" Y; y  a+ X* [& Y9 F

& g2 c: X" j5 GIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)0 O+ d( P4 Q2 X. `& N, c- E
检测通过Web应用程序上传的恶意文件。
5 X$ c* a/ W# O& V/ r
( M  x8 c2 B; p! C- C' c: Z/ b" k: k5 }9 a1 Y, Y: y% k
Tracking Sensitive Data(跟踪敏感数据)
" q7 {% P' U: I' S. W$ J0 R  [信用卡通道的使用,并阻止泄漏。
8 f5 b' P) C7 E0 Z6 ~& u
$ _$ E* g0 ~3 _% F7 H( n! k! p) u! |! H8 V; f: N- o! @
Trojan Protection(木马防护)
: v6 u* T' O" x! N8 e0 {; E检测访问木马。0 q" p& z; S/ d' j( r, O

5 G6 l5 S7 t- P$ [; `2 a% N1 N! d0 v
8 g7 t; V8 K& r0 U& n2 G- hIdentification of Application Defects(应用程序缺陷的鉴定)& U0 z& y; y, i/ q" i$ t, u
检测应用程序的错误配置警报。7 t+ j5 `, N8 L
+ {; `8 h/ W5 q4 J/ {

% K: O3 F# W1 \1 C7 k) l0 ZError Detection and Hiding(错误检测和隐藏)
# K6 `- U: L; l# a检测伪装服务器发送错误消息。# P4 w" ?3 o$ j* ~3 w
- u" s% p% H# E/ Q: l5 F9 x, j1 r

# s6 M8 w$ k( i' v- H安装ModSecurity& k/ c( n* g0 f3 b
软件基础环境准备2 X# f. V0 [; g# H
下载对应软件包
- k; b$ f( s" o9 ?$ S4 T) I0 C- ]$ cd /root' w8 ^7 D& g/ {
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
4 ]! R' Q9 O% a$ ~# A3 Q5 ^2 c$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz% z/ W4 T7 j+ y- A, R
安装Nginx和ModSecurity依赖包
9 w3 E. i$ v1 L6 j/ s8 {1 \Centos/RHEL
, c' r) r7 @; R4 |  O6 R2 S/ A
( y7 V# M! W( G9 k! W
# E6 R8 q! v' b3 V$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 n# A* Z8 I3 g% F6 g* |/ kUbuntu/Debian" M  r  A  t, E3 y, _* |
2 X7 a- }: L0 h1 o; |
) e1 I- t$ F+ Y
$ 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
; k+ ~1 t' B) ?编译安装ModSecurity
( h' t$ G% i% _; l' aNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
& r. ~! W! l( ?1 b( s: r/ K/ S: q8 ]! v. R: n) }

. G; B! C- U6 h( z# z8 m, g方法一:编译为Nginx静态模块  m4 S4 d/ {, W/ z; R# v) ?/ q

3 |! F; G) Y9 ^3 n) k% k, }" v2 H- m% R! o4 \9 C  l
编译为独立模块(modsecurity-2.9.1)
; E5 _4 O2 t' t) m$ tar xzvf modsecurity-2.9.1.tar.gz0 `4 s! c$ c2 V( i5 D% i
$ cd modsecurity-2.9.1/% e" |- M. ]% T2 j
$ ./autogen.sh
' ~- T$ S) }! d3 D$ ./configure --enable-standalone-module --disable-mlogc, ?' |4 K/ x7 R4 u$ M* o
$ make' h9 S" e( l. S. R% t8 W
编译安装Nginx并添加ModSecurity模块
; n- J! L% l0 }4 v# ?1 L$ tar xzvf nginx-1.9.2.tar.gz. v0 b4 D3 h/ K
$ cd nginx-1.9.2
% ~! a" e5 l2 I! G$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity// L" |5 w, d6 v) n( z, b
$ make && make install
* X5 ]: }. t' @2 |6 ^$ ?7 I0 A$ e2 q方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; d5 R+ }8 w# x. [/ n& g8 g
- o* w. y- e( w6 }, v( ?+ |) j9 G) q3 r& J, {5 Y
编译LibModSecurity(modsecurity-3.0)# W; v8 \9 g2 p, E
$ cd /root3 @# b6 P7 r9 r+ ?  _
$ git clone https://github.com/SpiderLabs/ModSecurity
0 `$ _; r* S9 u: H& S$ Y' h$ cd ModSecurity
! b9 m3 z/ i2 }) [: s: l: C$ git checkout -b v3/master origin/v3/master
; X9 m" m- w1 T% I. `' a$ J$ sh build.sh; N5 W; f9 X/ k& R/ K
$ git submodule init  q* r  V& \2 H3 J, \' V
$ git submodule update
3 [% }6 d  e2 N' n6 [" y$ ./configure8 M5 A7 y; h% X9 F" k# _
$ make7 o. V7 p* B' k! c) N
$ make install
% }& F/ y  x# `LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
' z  x5 O* H( x. y! h$ e: a7 w8 o3 z: N! Z. e; M+ C5 l4 p4 |$ A9 }! ^

9 s! v6 L9 L4 N" U: N$ ls /usr/local/modsecurity/lib. N2 b" d" e6 Z# ]' U4 ~
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.01 U" l& k  a) x/ ^. S9 j+ \* @
编译安装Nginx并添加ModSecurity-Nginx Connector模块
- m* E. ?& c; @+ @使用ModSecurity-Nginx模块来连接LibModSecurity
; x) x* w. y, {9 t# x* n" c, c* o6 {  J) w$ X) `

. d. s: I: x4 A8 k$ cd /root
' J" h* h7 q, U! M$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: b8 }, l. ?2 m8 {5 Z) W9 F& p$ tar xzvf nginx-1.9.2.tar.gz
2 A) ?$ k3 x8 Z, i, k$ cd nginx-1.9.2  a, Z* ~4 F3 ~+ j- e: z' g
$ ./configure --add-module=/root/modsecurity-nginx
" l, I0 h; {: P$ I9 e0 Z& U" ^$ make
, T9 h+ I1 j1 ?9 v2 N1 V$ make && make install
. Q/ Z5 X4 q. i7 {+ j2 N  p添加OWASP规则
6 E( Z9 ~6 _! Y4 M6 i" [9 {ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
# M( e$ x) k1 T1 c$ K+ m( y
4 s( _; f& n6 I3 X; i6 Q  P1 l9 m) w- E: n7 u
下载OWASP规则并生成配置文件& S% }7 W& ?' W5 c! i' O' C% T
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) r( x8 X2 M5 ]( v( I
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/+ i' Z3 P$ |5 }6 q# E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" y8 ?( i: _# g5 |: Z
$ cp crs-setup.conf.example  crs-setup.conf, ^2 d  n4 \8 c; g
配置OWASP规则  ^4 N- O0 `: y' W5 B
编辑crs-setup.conf文件- Z$ R! n: F" g6 A) m% `# U
. [6 r9 t( `' t0 }
; Z* ~# a1 B: @4 S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
- M. @  u# ~  k, v% f$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* Q" M5 h" J5 g2 s
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: I/ E+ k. X5 D3 _: k. x* l7 S$ p$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# P; ?! c7 h) b默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
7 |" M$ H$ L5 Z+ q' ^
! e/ u9 x( b: ]1 |; m7 c! s  Z8 t& L7 w2 S7 ^
启用ModSecurity模块和CRS规则% }( z. j6 a; R5 h; H' ]
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( w' l$ R; G/ e) @! R/ T( {+ Z9 @. ^1 A7 T+ U7 D

  U5 |' l# h) b# R+ A2 |" c  Wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! d7 \% m0 X; I9 X, Z* o# v8 l0 v: X

7 q  |1 [0 K2 ^8 ]" |1 `+ D) z5 V0 y3 n4 |
$ cd /root/modsecurity-2.9.1/
6 D/ z9 k5 Q" |7 V$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
4 G" S1 p8 G& Y$ ^& k$ cp unicode.mapping  /usr/local/nginx/conf/5 ^" J( Q) _, Y; q3 L* E" S
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 p* j5 T8 U7 z6 }

6 G& q) r8 {( p  O6 \
5 ]4 m' Y# }3 H& o$ s$ vim /usr/local/nginx/conf/modsecurity.conf
, z" D6 J$ w9 rSecRuleEngine On# t' j; Q. b4 S
ModSecurity中几个常用配置说明:
4 D, x: j2 X& B  h' L$ S  ?# i6 s) G! R8 V, Z5 Z8 ^

8 |* U' {; J" C  ?1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, H) |" p+ Y" V% \  S" O4 ?" s" w3 W" Z& H
6 m. s% N' S  {) [7 Q2 R
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. q( X3 f% z% L( F$ U
- b* l: {+ Z6 I2 P' O: Y

8 e6 R, L3 Y7 o; g3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
2 L  E2 j2 l# O4 G+ z' E2 T: Y7 n, B+ j
) j4 p+ i4 L/ v3 a% a0 n: e( ^% @, ~+ F& A
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" S" W/ C  \; b8 W
* W9 J' `& U- V3 A* ~* u( H
! s  h& t9 \+ X, w( j在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。5 \+ Z, }1 P& ]7 o
+ O" O( M, D/ y+ N4 b, J

6 L2 {+ Q& ]. ~; H9 f  |: Y7 p3.x版本CRS  @& f6 ^) Z2 Q3 Y6 t
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* j8 }, a& k- b  w( q* K
# 生成例外排除请求的配置文件
& E' G1 g) ~; I* Q" I- [$ `$ ~8 i( z$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
0 u  Y9 y& N! @  l5 [$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
0 Q3 D  Z0 r; E, @' r! q0 X1 D) Y$ cp rules/*.data /usr/local/nginx/conf
5 j% s. O2 ?) l) a为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
& h* q* h/ _: o0 ^7 B6 b
# {7 L7 h$ E2 |
3 a3 B" `, t/ W9 d* V1 L$ vim /usr/local/nginx/conf/modsec_includes.conf# r: M( c* Z' |$ b. C. S
/ T( n, K! b/ C6 z# K- e0 Y
[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
" w# V) E6 q; |* @7 [8 e5 C

/ G/ `5 U5 ?% I& \注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
" c. ~$ @$ U- c7 `: q& Y- H9 t+ I7 r5 n8 ]+ D$ w
0 j5 v& ^4 l: a$ l- V6 M
配置Nginx支持Modsecurity" Z* Q  d% ~: P9 U/ C
启用Modsecurity
; J/ Y7 {5 ], W+ f/ ]- a" i使用静态模块加载的配置方法
3 h1 [6 g) B7 |0 P- B在需要启用Modsecurity的主机的location下面加入下面两行即可:
! b5 C- p2 t' _/ V5 r0 l" f
" k+ P" ^0 y! p  Y7 Z/ O) B: N# F) ?* C& H- J( D# g( |  _$ f
ModSecurityEnabled on;, s' B5 T8 e/ X# z8 |
ModSecurityConfig modsec_includes.conf;
% N  ]" @# Y) S" K8 a5 Y8 e( P修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。# @( n' l6 K7 \6 N* l

5 i* y& Y2 y1 [9 s! x4 Y- M' T" c3 T' U9 e) [; j
$ vim /usr/local/nginx/conf/nginx.conf0 P; H4 o$ m1 I: M3 f" |
4 I: a6 r9 J5 l* C

" O* L7 L' Z# m+ rserver {" a$ N7 n/ a$ w. x
  listen       80;- r) @1 U6 F& i9 H
  server_name  example.com;
4 Y! v9 |- f/ d# @7 Q8 e' L& j$ h! @8 |4 I$ c( F8 L* j

, w9 [4 h* H  l7 D; z, `  location / {9 R' `: C2 Q/ ^) J1 m* F
    ModSecurityEnabled on;
  ?8 b; @- l5 q$ |( D    ModSecurityConfig modsec_includes.conf;0 P( F3 K( N$ ~8 r! S
    root   html;8 L' `# O) m3 ]$ O! o
    index  index.html index.htm;
. ?: U6 e  v& y5 J  Z4 I( ?: q7 v+ J  }( ^' W' Y/ ^5 B8 H4 M
}. ]! s( K# z) l
使用动态模块加载的配置方法; r  q- Q! v; U6 F- S
在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 \3 R4 j$ j) S$ J, {& ?; Z8 O$ @0 R6 z$ N; T8 v0 m

" V5 v  u' W, ?$ z) ]/ \3 I% Lmodsecurity on;
5 a1 S1 E9 b- m4 d  a2 [2 Q; J6 Kmodsecurity_rules_file modsec_includes.conf;
/ v- t/ g$ x2 E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ h  J( m$ P. `; ^9 S
7 }- H% F/ [# j0 u8 H4 c
$ g7 t! {: T& P# _) q5 k# Q3 B$ vim /usr/local/nginx/conf/nginx.conf3 ~+ ~" ~8 f( U7 f, P9 l7 r* d. k
# A0 {3 ]5 x* B, ^

) ]1 R$ H7 ~  j3 nserver {
  ^9 a* _) _" B  listen  80;, Y; v% z2 P$ }: L- b. c8 j
  server_name localhost mike.hi-linux.com;+ S1 Z0 X5 t3 ~) I4 G( m/ C2 s
  access_log /var/log/nginx/yourdomain.log;9 B; k3 m. X% x& x% W+ L7 T3 e

( [# x: t# \) P4 m3 n& c
9 C0 G$ V  `; A6 B/ k9 Q7 _  location / {
; z7 f& W7 o. I) K4 Y# p, a
1 i4 g5 x( E/ o/ y) T7 |% P+ b6 {' \
  modsecurity on;# W; j2 w7 Z  q  Y5 J1 D
  modsecurity_rules_file modsec_includes.conf;8 c* y) V, {6 f) F' R( C
  root   html;
: B2 b3 h4 @* @: d  index  index.html index.htm;' W! x( U  W6 T! s. c# Y
}
: X) u# c; k! u! Z/ K5 z}
- u* ^) H  v5 r. G验证Nginx配置文件0 t# f# C# q8 c2 t5 @& E4 u! \
$ /usr/local/nginx/sbin/nginx -t
% I* G2 L" I$ J$ A) qnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
8 p4 h7 a' V# ^& a+ ^9 M6 wnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  `. k( J8 n& [; b. f8 ]& X+ T- f3 ?启动Nginx
' ~. r" e! ~7 [4 |8 B! N0 l- j$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
" `( l( c2 ]8 f# I9 `4 q$ B6 o2 P) p2 n# O# e0 _4 X% x: K! e; L

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


+ N% x* G, X1 X8 b* f

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-11-8 01:15 , Processed in 0.077129 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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