找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11560|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
. ?% r- v5 M# H- P$ q
) |! [+ Y( b/ }# c4 k" S7 n$ |! l2 ^
6 z$ s; ]0 j; @$ E* f在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) E8 @7 M% O+ i! ?  x9 v, i4 k8 V0 v* p; I# m( q

) c/ L5 v1 H9 Z# n  m% `什么是ModSecurity
8 J/ Z# Y6 v6 PModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。  r. e4 S* o) V1 ?6 d6 j$ F8 F

# y; K+ \8 z2 _: g2 K
7 n+ c4 Q: b1 B3 QModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。1 k# A8 B& \0 k/ F5 j* ]

' F7 I: X# \3 b% M6 G# b/ u0 ]* B1 t
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。! }3 c8 J. R; Y* ^3 ^. V" P7 m
$ C, N# ^& v% E6 H5 @8 _0 e3 P. t) k+ m

, p) R% C; Z# c4 `" ~- |3 o官网: https://www.modsecurity.org/
( L: @+ A3 ]6 h( Y% M  ^
% W: Z% O4 H0 [( ^0 C: T* O( m$ ?; \+ z6 @- x
什么是OWASP CRS
- f! Z' T. q  T& Z9 s& n- O. AOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
+ W: l( b! j' ~6 V$ c& m) `
. P5 c7 _$ g1 Y' |5 |8 @0 N  z+ Y+ J# }  E, ~; i5 \
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 ^. O2 T) \: g3 k$ X, W, V
) M5 C6 S$ U$ W- [8 B+ H+ d5 J- c8 f# _& O" N" n  q
HTTP Protection(HTTP防御)
3 D1 O. K; Z/ }  `% W  _' ~/ a: ZHTTP协议和本地定义使用的detectsviolations策略。
+ e4 _, k) P0 F3 T* G4 H3 s" @: _
% `2 F& q& Q$ ]6 o5 L7 Y6 I
* f) m; B$ B+ L0 X) @# @: p: MReal-time Blacklist Lookups(实时黑名单查询): o, \7 T. G- o* m. P% }6 ^
利用第三方IP名单。+ Y1 i% i9 W! T1 i6 `
! o# s+ _* ?7 I' Z( D4 i. B$ ~
5 _  C$ ~& k% F
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
* [" U* O% I; r, K+ p" D( [& Q防御HTTP的洪水攻击和HTTP Dos攻击。
) M. O' v8 h( `" R6 B# X0 ^% r; h! N4 A3 c: J
- t4 w2 M8 V$ s4 Z) |7 q8 ]$ m3 @
Common Web Attacks Protection(常见的Web攻击防护)
: S7 u/ j/ q8 \9 t1 o( R( D, ?4 p检测常见的Web应用程序的安全攻击。) R7 a7 S7 o9 a4 r! _1 t. M+ L
0 z+ ^* Q+ d+ H% F3 u7 z
) u7 X! ]9 P) l! o; |7 ^
Automation Detection(自动化检测)
" m/ l% i/ T; ?$ c- W- c( H检测机器人,爬虫,扫描仪和其他表面恶意活动。1 Y/ Z# m; Y3 h+ C, F3 t) _0 X
" g5 _5 B/ y& d1 v( I; V+ x
% `4 }' H4 Z; c( F! F; v( z0 }
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
! j1 b) I9 Q4 u: x检测通过Web应用程序上传的恶意文件。
* U  C" Z6 B( J  f
# p2 ]/ r8 n9 q" M7 e: Y; d+ x$ J8 s' @9 b, R( e
Tracking Sensitive Data(跟踪敏感数据)
' _2 J; r0 `& B信用卡通道的使用,并阻止泄漏。
9 d: B0 ^( s; {9 W4 w; o7 H6 e! i5 ]# a! E2 V8 X

" H8 a5 J- {( K2 m1 ?. ^/ NTrojan Protection(木马防护)
4 e. \/ V$ M5 \2 y检测访问木马。# x4 `- O* T% \1 ~3 C. |( q' I1 f. v* D

& m, v) M4 I3 O
1 n& m7 s2 L* qIdentification of Application Defects(应用程序缺陷的鉴定)
! ~  P0 ?/ O( |: g检测应用程序的错误配置警报。
: [$ E; l- V' R/ g/ m. C
$ N1 M& |  S9 Q8 q& F
1 _( j' g* F9 l* N9 UError Detection and Hiding(错误检测和隐藏)- c1 J" {4 G" w0 _1 g1 A) v& K- \
检测伪装服务器发送错误消息。1 y1 z  I2 O- N  t
; {/ A% w! z" d6 Y

1 T: i! I* b. @% j) c  |安装ModSecurity6 z% U' ?8 w& b3 r
软件基础环境准备
3 o4 l; d1 J. ]: D下载对应软件包# G, W: K! `2 c7 u
$ cd /root
; b8 C  m9 w- R+ H* t" V7 e3 h$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'# W! D* i$ R, V' Z" g* 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
3 @  A! F/ R0 C5 N: C) Q- y) H安装Nginx和ModSecurity依赖包7 `3 Y7 E7 J' e" E3 ~+ c5 ^3 f
Centos/RHEL
/ b' W& ~1 S! |8 \) N1 ~3 z5 G- E& _/ A

" H4 w+ X+ j3 i+ K7 x7 c$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
/ l, R( R# ^4 g2 `/ S# IUbuntu/Debian* i) M& e) v6 x' w/ n
1 p; f) }) R& c( N

6 e! e, n( c, \: r$ 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 C; }4 d- A8 o$ {, L! f
编译安装ModSecurity
( k8 P, ^/ D+ H) b4 KNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 e) B- N4 a. A9 |( q5 m( k2 i
( i+ ~: J& K$ f  j# w( T4 Y
* |) l9 f' o3 Q7 X方法一:编译为Nginx静态模块* Z) ^; [+ _9 m8 N

/ ~3 I+ A) ^( S0 T! U
0 q" ?0 h3 Q2 b编译为独立模块(modsecurity-2.9.1)( E0 Q  E+ \/ Z1 ?7 e: E. A, g
$ tar xzvf modsecurity-2.9.1.tar.gz
  B4 T' k4 c3 E7 |+ p% X9 y$ cd modsecurity-2.9.1/
: z+ Z2 R4 g' y; Q$ ./autogen.sh% ^7 ~& F4 c, p4 I, G% r9 \+ M
$ ./configure --enable-standalone-module --disable-mlogc
! p( F+ R7 ]' V2 S" ?$ make# V! S/ \" m  C
编译安装Nginx并添加ModSecurity模块$ S" t4 H7 F) k6 |1 }( C
$ tar xzvf nginx-1.9.2.tar.gz7 v: p; A# M# l( X
$ cd nginx-1.9.2$ [: h( e! C. I& V3 t( U
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/! `! E9 |; h. B7 R! h1 l  l) G
$ make && make install8 H. l5 t3 p( [  F/ x7 N. M
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 T8 {9 I9 \; j) w, C
6 c0 r: a+ q9 K7 f6 O
7 ?5 i. ?  }0 {2 c编译LibModSecurity(modsecurity-3.0), I' z* S: J8 W7 Z6 R
$ cd /root
8 R9 k* `& \+ h& i0 v9 _$ git clone https://github.com/SpiderLabs/ModSecurity
3 J3 A7 M" f# v" v9 d6 I7 |$ cd ModSecurity
9 i  M' {) U6 g" C0 a5 h  D* J$ git checkout -b v3/master origin/v3/master$ E; F1 `# p6 [6 u  y. C' s$ V: g$ X
$ sh build.sh/ [) M' a, v( E; i9 e6 V! ]0 G
$ git submodule init
( {, @3 o2 r/ v$ git submodule update" C! X9 V) I9 `
$ ./configure  G. d7 i5 x& c& P. v+ [; B2 p
$ make
6 T# t) I6 K4 _3 E$ ^$ make install2 K8 z0 ?2 s6 ?$ }* I3 s
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
; w- N& r- J8 H/ B0 x# W; s8 m
& ]& k8 C* Y0 h3 T# k2 h% }5 P) r
0 R2 q/ ?( {. W0 T% y7 G+ l$ ls /usr/local/modsecurity/lib9 {! w/ Y: n4 y" a/ Z+ T. r, ?# z
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
/ p! `3 W4 ?4 C, d! k) }( k4 a编译安装Nginx并添加ModSecurity-Nginx Connector模块
" m) M5 F  a1 ^- E- |使用ModSecurity-Nginx模块来连接LibModSecurity1 |2 E" ]8 Q. t
0 e' S! I+ p1 Z5 r( s$ g

# o8 _3 v5 j$ p6 j* q+ `/ S$ cd /root
  A( i! l( h0 j" w$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: e3 f6 @2 q" e
$ tar xzvf nginx-1.9.2.tar.gz4 x  Q1 D5 m8 P: p. m- b
$ cd nginx-1.9.2
$ C- S3 @& Q- ^( ~1 x$ ./configure --add-module=/root/modsecurity-nginx* g9 Z0 I5 @, V! c8 h2 ?
$ make$ z/ R- D  M5 X1 ^( m
$ make && make install0 o6 A: r% a: F) \$ `* i3 }
添加OWASP规则
- K# j: i8 |+ }2 l# Z/ wModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 D! w# E: Y$ V" d

8 ]( ?$ _) j- k$ @0 C& n
0 W$ l# S4 H" |* @& F! x( ^下载OWASP规则并生成配置文件/ S- q; e2 D  y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# P* w! t" ^4 K$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 R8 y1 g" h, g3 x' }0 _
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 I8 S* o/ U; N" {8 K$ cp crs-setup.conf.example  crs-setup.conf
9 @6 G. f, W$ p1 V配置OWASP规则
9 C6 s9 C2 D, J% k7 \  k9 Y编辑crs-setup.conf文件
+ k8 \1 }1 z6 d" c* U
6 S2 k$ a' H  d) w: u5 D: c+ d# D$ v
% m# }2 Q/ l6 }7 J$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 e/ G+ S# e( K, m0 y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf  U. w. ^/ e# h2 ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 _% l) m; R/ \+ K: z
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
7 W! ^+ x) P8 b: v$ E& J* `+ Y2 ?默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。: J' [2 O  c$ g0 w5 A  x' G
  `2 H+ x- d/ H% H- c6 ?2 a

- D, N5 Z- J* n+ U$ Z/ I& a, `启用ModSecurity模块和CRS规则+ q9 `, E& u+ a' ]! _  }
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 _7 a# q5 I) E1 W# |) e+ s" }) @1 R% H  ~1 k- K1 \

9 E+ v! B& [1 M' f+ Q. ?# V% }" hmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。  {4 R7 f0 S: E' W# m3 \
' s4 B+ \0 ^- A- ^

5 s6 G1 A' @: n3 y! j* Q$ cd /root/modsecurity-2.9.1/
/ c% L0 y4 E0 y2 ^; i) `$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
( o! t- W5 s/ h$ cp unicode.mapping  /usr/local/nginx/conf// b. [8 o/ m% v6 E3 G
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( R/ Q; K/ i- x( k  F% I! k; L. U3 P) S! u% T# U$ r3 f
$ R+ `. F# {% C
$ vim /usr/local/nginx/conf/modsecurity.conf+ ]4 F& {. f: U& m( }7 h" r6 C
SecRuleEngine On
1 c* ^4 C8 i0 sModSecurity中几个常用配置说明:# L0 V4 F* S/ v9 A

$ r6 o0 |% v5 c+ O4 p- K/ z
3 v, v% N- [- X5 G5 h' \2 d/ F1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。$ j, ~2 R. D: s3 P! Q

2 h3 V# z9 I0 ?, p$ n2 c) b8 V4 I2 l# ^* `
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. T6 j5 r( E) q* y: X. ^$ _# s
) M$ ^* M, H. f; a6 E, h0 F
/ ^0 T8 s5 l! r$ k
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 z& K# S' K* R, F$ J& e( z+ S
. D' N2 B# P. i9 W* E! @1 D$ ^/ s
3 V8 D/ ^/ E1 N8 l) ]
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! _, P0 Q. b2 M

4 Q/ `. J! @# U/ p( s- P
: D; Q- U! n- s& x+ f7 k1 u2 E0 p7 u6 v3 x在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& l* Y7 V) G% j. F% Z/ o' }" R' M6 V! C

9 k2 g: Z6 m+ g9 ?3.x版本CRS
* ?4 k+ w9 |/ i( N$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- \, V$ {* H; X  z1 c
# 生成例外排除请求的配置文件+ {: N2 q7 ^; d5 Y/ X
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 y. G- p, w* b$ \$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 m2 P5 r' U5 E- ]5 U7 o
$ cp rules/*.data /usr/local/nginx/conf
( x" \) ~/ G$ F0 g! p- S为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' \* K6 b5 V1 x8 o

  C# }, N: m5 z% w, ]/ s
: E# d, ]; R- n, [- d; y/ _$ X, R$ vim /usr/local/nginx/conf/modsec_includes.conf
" D4 a" K; ]* [% V5 T+ k; h# Z9 n7 B5 j, R) m7 i7 |, f  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

" f1 S8 j- v/ V; i" }" }! L; @2 C1 I( \7 T+ p2 F
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。/ E2 X, r9 Q: g) {% j& U! g9 x6 {" {
+ O( P/ b# {1 b9 B& o

, }; g' ]* A  f: a" n+ N% N配置Nginx支持Modsecurity
6 O: u% V- T: C# ~7 D: Y启用Modsecurity
7 z8 B; t3 ~5 ?9 R( R7 W使用静态模块加载的配置方法
: t" [" A, N0 p+ P在需要启用Modsecurity的主机的location下面加入下面两行即可:7 u9 c3 r, G1 E$ C5 O6 \

; |2 F1 Y& {: ]3 {0 B' }' B7 R
$ v/ D, ~* E: PModSecurityEnabled on;4 D! y" T8 [% `: P! q5 X
ModSecurityConfig modsec_includes.conf;7 z" @' w+ b4 F2 \# S. H
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" _. {; J4 w8 k

% x3 j# c+ J1 t' U5 v; z; J* }) b9 F6 k
$ vim /usr/local/nginx/conf/nginx.conf
- }( [9 T: [/ S* p1 g8 }1 P7 O9 d! k( k( T" U
+ m) S+ O( a; |% K* k  h" z
server {
0 V6 i8 y7 J+ G! B- x: T  listen       80;; N% f# _/ F5 }0 M2 e3 J7 \
  server_name  example.com;
! f1 V5 }' D2 ?2 _$ d/ @
4 i* p' _, V8 F8 J8 ^
0 \" t" ^7 ]9 L% M/ u, @1 u  location / {
/ Y/ T; n2 Z9 ?5 ?6 C* M" B    ModSecurityEnabled on;
" S4 x! a" M+ Z# O) Y( S/ |    ModSecurityConfig modsec_includes.conf;* b/ j' `, e" {
    root   html;
# \0 o. s" \: f    index  index.html index.htm;: c/ H3 ?7 G4 B" {
  }" w# ]; A, \; u; f6 G
}* [! [7 o, m0 R, Q: P
使用动态模块加载的配置方法4 ~! a, S" J2 l5 a; A* k
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 M& m0 M8 n' S9 j: T  U
: r  c' c. F% x$ U
$ @8 j; Y+ ~! Y2 Kmodsecurity on;5 r+ u( X( j/ }
modsecurity_rules_file modsec_includes.conf;
! e3 u7 ~  w) N, t1 k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ d7 P5 I$ M$ u8 r; |5 D7 n/ V) `: J3 f- `$ ]/ n

/ s# o& C+ G* M" Y# M$ vim /usr/local/nginx/conf/nginx.conf9 z2 u. a( v( x5 ?' }* W4 f5 k% N

0 ~. a' ?% I1 \1 m! K# R( r& o) @  s4 t0 O
server {0 d8 f4 `* }3 }) V0 |( g
  listen  80;
6 E8 f4 {& u. x+ D* N0 Q2 V  server_name localhost mike.hi-linux.com;
4 A2 z( J- |. C9 [$ F  access_log /var/log/nginx/yourdomain.log;
9 b' s9 t. ]3 c- a
9 p' D: a1 `0 C( A/ `5 O9 B# o' `, e, W
  location / {
/ Y  V$ `( @$ i& }/ B# m  Z# O0 x/ R( S
4 J- i& P! K9 J# P) ~- x% V  G: S8 P3 \( X: s% {6 g" H) W
  modsecurity on;
) l0 k. o. h, ~  modsecurity_rules_file modsec_includes.conf;
2 y# N3 k( f/ @* G( r& ?& o$ W% h+ D  root   html;* \  E& Y: _% N( h. [
  index  index.html index.htm;) E. O. b3 t3 {, W6 e2 b- V
}
$ e& Q" z3 S0 Y- K, P6 Y5 p8 ?3 X}+ o& l5 X; @; \; P# H
验证Nginx配置文件
9 I" \2 I0 [! ~! m$ /usr/local/nginx/sbin/nginx -t0 P; i- d- v, i  K9 I- g3 }
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok+ ]9 g5 ]2 R  P& u; g, n: m
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4 i4 P+ U9 N/ N启动Nginx
6 J6 Y, ]) G# T3 G+ Z/ k6 l$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- o; h' I0 I4 b0 w/ i
0 ^) c# G0 [; O0 x) r3 C$ y! W

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

; G& g+ {% R7 c0 b- j6 \% K6 w; w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-24 07:13 , Processed in 0.095901 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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