找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10860|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。: a4 @4 i8 L0 w- x9 ^! t# Z  G0 W
4 ]" n0 |1 x8 }: Q0 Z( ^" s

3 X' E1 x0 O+ J9 A% Z- v& b在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: T. q4 L& u8 s$ ^) U( q
+ `1 d2 r& p" L1 {, w6 ^) t* u! Q/ ?& H! T. _" e
什么是ModSecurity7 w. i( S5 r4 n+ p# e. e" e
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
/ |& p0 B$ V; ^
- ?9 b  q) i* K8 T+ x6 k/ M' E+ t5 a& x6 e7 ^
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 v! _* T4 {% Y0 G# N* n6 f: i
  N# R; S, S) e& J5 b, g+ L; B

3 v) @, d( `6 |  lModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
& t! W3 i( d& g  w6 h' ?# ]: S# e; U

  a+ v) q" @5 K* }% u6 b- }& f& @官网: https://www.modsecurity.org/
1 H8 A6 Z4 k# H& C9 ?" v. [0 k; X0 F: e5 P+ G
% w# k/ g1 Z& n5 K/ U
什么是OWASP CRS& ^  a! z: u, c" [  c- b
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  d' _/ }9 R* B7 R
; I0 e1 G- I# @5 u1 ]$ Q9 _7 l  R6 O
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 O. z  |  n7 A/ P7 j+ S* X; D% N' E% u4 z8 V& ?) L
8 _1 J( ~" u, [0 c& }1 N! a
HTTP Protection(HTTP防御)
4 e" x; h, {5 Q6 ?# {HTTP协议和本地定义使用的detectsviolations策略。
: g) Q% q, F4 t  G
& M" l* k! e/ c3 V" T6 [* e! o0 E" F- f  g, G& d, X& `
Real-time Blacklist Lookups(实时黑名单查询)
( k( v7 [+ J8 U1 T2 A# d8 K利用第三方IP名单。
$ F- i% e, ]+ F0 S$ B, [2 J( |5 u0 Y& d1 w( S
# G5 v# W1 d* E0 s5 O& R% K
HTTP Denial of Service Protections(HTTP的拒绝服务保护)' ^# O& k; x& X8 d. b0 ^% _/ w
防御HTTP的洪水攻击和HTTP Dos攻击。) k0 T! Z, m; r; c7 F! Y

- |: U; z/ V' O2 g" h! ?& E
4 |4 }' N( v6 S9 }' L% m/ }Common Web Attacks Protection(常见的Web攻击防护)8 o5 {& Z# u+ H. i" q/ `
检测常见的Web应用程序的安全攻击。
6 k' s' O: V: A/ x  J% z& r9 N# d8 s* M& s  X! \

- Y0 A) R6 o+ L- T9 GAutomation Detection(自动化检测)8 f8 p  v' I4 ?. X3 y
检测机器人,爬虫,扫描仪和其他表面恶意活动。# |, ?2 T1 x9 k2 q

% F! u4 y+ `9 m; e; `0 _, x1 {# c# U) W8 `9 h3 l0 W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描); D( M6 O& t) p
检测通过Web应用程序上传的恶意文件。( b* j; Y% q9 l2 p6 D

2 ^( B. `4 ?, M/ h  W9 u5 {! k
- v+ u; }8 P8 fTracking Sensitive Data(跟踪敏感数据)  w+ ]; a  L/ h2 {+ M/ C: W
信用卡通道的使用,并阻止泄漏。7 m2 B# n5 \8 W4 h

! A: e. L3 {% u* a& h6 u% P( q, r: T. F4 M  S3 q4 K" \# v9 f6 g
Trojan Protection(木马防护)
6 l# F/ g+ E# P6 j, u* m+ O5 n检测访问木马。
' x! f- f! W7 ]1 W1 W+ r& E. q. `2 t, M  W0 N
: C3 I5 i  E% C4 Z8 [: f+ l( b3 z
Identification of Application Defects(应用程序缺陷的鉴定)% `+ q0 X8 q3 l7 t* a% c
检测应用程序的错误配置警报。
* B# I/ u8 j2 H5 u6 H# y( Y
- x' v& m' }$ A4 H, i" }* B) Y# Q6 R
Error Detection and Hiding(错误检测和隐藏)
% u+ A$ Q; w5 V/ a( t( g检测伪装服务器发送错误消息。- [0 V+ l- I8 b. n% X; f" ]2 |# i

6 c7 k. A" M% e2 X( m6 _: J$ p7 D. U0 C6 f  G0 F
安装ModSecurity
6 Q7 x. b4 i  z! ]  M! G软件基础环境准备: f( Y! ~, d4 q7 T6 ^& q* K
下载对应软件包, u$ M9 x3 x0 [0 V8 _& P$ v. @' F
$ cd /root
6 X! m" z8 W. Q2 F& E$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'7 d. s! ?& ~+ Y) q& m
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
+ l- Z. ^) J$ @- S8 d. y+ D安装Nginx和ModSecurity依赖包7 ?' x- X7 r5 {) Q7 F$ U
Centos/RHEL! i* R$ g$ d' {; @
1 M# T+ X$ K5 z9 E; A
2 F, |4 ~: y$ u3 {
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
, j& g6 _0 J4 o0 Y$ ?5 s6 sUbuntu/Debian
! S1 \1 U( U: S$ c* U# g; b( S3 M' Z
. u! b2 H6 Y' O8 V# g& J* @1 q6 X' c
$ 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
( F4 D3 C. j! L& D% h' W" }% D编译安装ModSecurity
4 n' O& q; C4 ~7 m; A( U6 c/ d, yNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
2 r% k+ U$ ?2 r# ~, Y/ p# w8 u) d" V- h5 }9 c  H* P1 Y
2 i3 Z# N5 ~) J0 z: U
方法一:编译为Nginx静态模块
" \5 O% e; Q! U2 o1 F
& e$ W4 U( _( T: D" k( c4 l5 N0 Y3 E9 i  w0 }  n4 s
编译为独立模块(modsecurity-2.9.1)
- |2 i2 s, ~% k% w" l$ tar xzvf modsecurity-2.9.1.tar.gz8 l. R. A7 K, E" q! g. y8 Y
$ cd modsecurity-2.9.1/9 G3 {& r( w0 H$ F1 G
$ ./autogen.sh4 l5 }6 e4 P3 C
$ ./configure --enable-standalone-module --disable-mlogc# p* s7 b! n( ~+ z& G3 H+ C6 l
$ make
3 x  @; U- c9 M6 l1 I编译安装Nginx并添加ModSecurity模块9 v/ L- Z! Q- ]9 j$ b* _/ m
$ tar xzvf nginx-1.9.2.tar.gz  r& w* e' Z, y; \6 V9 c0 A
$ cd nginx-1.9.2
/ h3 X/ ~4 L- I2 X5 M* u$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 {0 `. p& ?4 H; Z' y# Z5 [; C" I
$ make && make install% d; }+ q/ }) Q% J
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块! K9 U1 X8 j0 z0 V

, B& ?2 s* t. I. b/ Y1 a# O7 h1 t; T, i% F8 c
编译LibModSecurity(modsecurity-3.0)# Y& Z2 Q% O& Q: r( F. {
$ cd /root
  K7 s  }8 ]  @7 @7 w$ git clone https://github.com/SpiderLabs/ModSecurity
0 [* n' E: z9 z! u) P$ cd ModSecurity
; T: K9 c$ w7 u( w# N1 O: Q5 T$ git checkout -b v3/master origin/v3/master
- A+ V$ J  E. C% `; ^9 C$ sh build.sh5 N; @& K+ h& p2 ?4 P! Y4 a; Y( c0 L
$ git submodule init
: T  K& R0 j- H9 x5 p; s3 y$ git submodule update' {' t  d$ S: t3 x; C2 [
$ ./configure" j9 h' C" W! q% N0 F0 {" w
$ make
+ I) m+ m6 T% t% k- X6 X7 {% I4 |- u' l$ make install
, I+ o: j) u7 f4 sLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
2 P. x( K( Z. F6 d- N
$ b7 y$ T) B' y" ^8 t0 h$ v% n% y* ~- n3 H- t
$ ls /usr/local/modsecurity/lib
9 ]3 ]/ U+ m1 r3 Olibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0# ~  b! o% e& |1 Q
编译安装Nginx并添加ModSecurity-Nginx Connector模块
  ^/ x$ \4 ^3 X( G) ?使用ModSecurity-Nginx模块来连接LibModSecurity9 Y0 J& c4 U, _

6 J- q& A5 A* c6 Z, Q8 F$ L$ {
9 C) [- E% y4 y/ V( m# k$ cd /root
( W5 {/ N; ~3 n3 V# Q* o( v) m$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ ~3 D4 U6 ~" j' @) W4 p
$ tar xzvf nginx-1.9.2.tar.gz
! ]' c4 {: z0 M5 j$ cd nginx-1.9.2
  T" V' O! K) V7 H! L! t* L. [$ ./configure --add-module=/root/modsecurity-nginx
3 y# G6 c; t7 }* u4 N0 g$ make+ _& h' T; T; ]( G( n4 Y
$ make && make install& r6 l3 C- I; f% i) I1 Q
添加OWASP规则
7 f* `+ d4 T9 H, W) E( R* {+ GModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 U1 `, t% }* O7 |0 Y
. T- P% T" K; W4 m) D- }9 V* ~3 h" c" |7 V1 f1 ^% O! n
下载OWASP规则并生成配置文件
3 y8 e3 W  H( w* [9 @$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 S' s* j) W: V2 H/ [$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/( v, o5 C: H7 R) C7 P
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 S' m. O+ P( O1 W5 `$ cp crs-setup.conf.example  crs-setup.conf8 c. {$ s3 M% A: S9 p/ e
配置OWASP规则
1 A; b) P9 N+ u3 Y编辑crs-setup.conf文件
2 N) v. F# l4 r0 t. w
7 w3 Z6 a/ I  \5 m! D* I$ ^# i& x8 k( N& B/ b' n
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: ^5 r1 e' l0 L7 t* t: I
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' ]6 a+ B0 O& w- k/ @$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf& w3 T/ Q" ~  X+ T% p
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
1 j! q( z. w9 P" O# H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。; e' ?( A3 u- d

  L8 P9 }# M7 v- h# C1 a
0 v$ Z' O/ G8 R  h启用ModSecurity模块和CRS规则; |8 H% \2 b; ]9 M
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' V, o: ?; e: k0 `6 j" Y
7 `( l$ @2 ~$ |4 ]3 t

& r8 K. d: U9 ^* Z' E2 bmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% T: j" ]; }( e* M% c
. c2 R$ l; S5 W) D& K! f; ~* S
/ V: Y1 B- K7 b  Q# d2 _
$ cd /root/modsecurity-2.9.1/
7 b- g( E( h5 f3 t: }5 `$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  9 g7 V/ f1 C9 d1 h% u
$ cp unicode.mapping  /usr/local/nginx/conf/# E/ l6 A( B8 Q3 [  [) e
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
- M* _! l( p4 R# u
+ r: P+ q' {# |
2 t4 l9 p4 q, O9 E) b  l" U$ vim /usr/local/nginx/conf/modsecurity.conf, q3 u* }: C2 w6 d
SecRuleEngine On. r7 ^) B. k5 C8 `8 e
ModSecurity中几个常用配置说明:2 n. e; S, l/ a7 F  e

5 k! e% p! [% U" v! i- k$ E8 j& {
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。" N/ g* w4 ~) B

2 r# V# P. z# n' i7 x# m! M6 Q. |. ?8 y8 m8 i
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. A- j4 w+ z( ?# T0 ~+ ~' T
* k8 A" c" V7 u5 I0 u! d6 @3 x
: Y; }# r5 }4 V. ~) a
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。5 y0 j3 ]8 W, q* v$ m

/ L: Y6 I4 V, w- {3 N7 F7 h, z: S3 x- l% _- M8 y* {* }# m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。1 ~' g1 e* L, T' g6 m

. |4 g1 Y+ F1 B. Z
! k( i& V; z! b% U) s) x0 X+ Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ [: z' l! d$ u* _4 A
/ D" ]5 E% v% t% X# S
; y; u) ?) ?, A" g9 A
3.x版本CRS
% i: b* d  e' I) G1 t& z$ t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! D$ Z* \- Z! i, q7 g
# 生成例外排除请求的配置文件- W: I9 `  A1 |4 k& s
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf; @+ k, F/ Y+ D1 e
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 u! |4 o) {6 x8 b4 P5 X* W
$ cp rules/*.data /usr/local/nginx/conf0 q0 w( P$ l( E
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! H7 M* Q0 [! m) B7 S; y6 ]/ D$ L; K

2 M5 m! ?! o3 b$ vim /usr/local/nginx/conf/modsec_includes.conf, I8 P# B. }! v' t! |# f8 r

1 a! m6 c+ r9 Q; G, `4 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

1 I8 u3 f$ r1 G8 Y, |: e4 w% j; k# |, C6 S
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# Z- b: Q0 H" Q  d" F4 j) k' V& Q

2 ~% j/ X( j" r. a配置Nginx支持Modsecurity& c% L# ^8 E7 L6 S" e
启用Modsecurity$ U( V- ?% s1 q1 J* B; v* Z6 T
使用静态模块加载的配置方法# M" U9 }$ a2 o
在需要启用Modsecurity的主机的location下面加入下面两行即可:
( h! [. i; _+ S. x! W
. q/ U3 m' }% K8 ^5 w
' T' ^3 D* E$ W8 sModSecurityEnabled on;
7 p1 }# m  S- S4 V9 n8 W6 @ModSecurityConfig modsec_includes.conf;* v/ ]& }, c# f8 ^& n
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ B# G( o+ o. j3 K5 x. F7 m0 ~: k5 r$ e+ q

+ }# h( K) n5 L' T$ vim /usr/local/nginx/conf/nginx.conf
; ]  T! e  g# d+ G6 j7 d+ L, \- B( ]8 R$ L. g: f8 d7 g
$ C; W1 _: h, k
server {
- ~5 e$ Y- W( n  listen       80;# B* g$ b+ Y8 j! S( ~& I
  server_name  example.com;6 d5 P: h/ }  C

" r  r* w1 P% D* _% y# O: v" U8 D' W* ?) |7 k$ R
  location / {0 I" E) h& e$ _) V
    ModSecurityEnabled on;+ A0 a) U7 n" M: {; c# s
    ModSecurityConfig modsec_includes.conf;. I: g( S( ]. S, i# n! x( g
    root   html;
& `$ e5 }% A+ {6 [, }% S& S5 o    index  index.html index.htm;! f8 A# L" G" }2 m. p; f4 d
  }
% e) o0 Q% C" f5 f}9 s- s7 f3 J3 a0 `! Z' [
使用动态模块加载的配置方法1 R* S: S. `- _  @3 @) h
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% f- L! K# L1 |8 T" e: _8 D1 A+ ?# `) w8 [; I; r4 _- a" y

/ j" ~3 b# D, W' C) Y; R/ Mmodsecurity on;  |' ~+ A0 Q. d/ I: z, b
modsecurity_rules_file modsec_includes.conf;
- j# }! l; R3 e/ z% q9 a  T修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 ~  L) l6 S/ e1 Z0 y0 o
1 [2 O/ M8 t9 D7 n' ~* ^. X/ h0 r& X

' e! h: E3 f& L: ~' `+ O$ vim /usr/local/nginx/conf/nginx.conf
8 |8 ~: X* g5 p& f; P
0 G1 @" B) ^/ Y. X1 k
% U- J+ `$ s& i  i. `# M$ Jserver {" r6 s% @* a4 |! _
  listen  80;
2 Z1 h- o$ V5 n3 Q% l  server_name localhost mike.hi-linux.com;% e" n3 E% m9 N8 y) D7 s
  access_log /var/log/nginx/yourdomain.log;8 O) `/ k4 L. n( X+ ~# R
* u$ J9 L* w0 s( D3 C' `

- t, w% W4 \% n# D- Z& z" L- {  location / {7 p( Q( T7 E- i' q9 [6 g5 z: J
" h2 }7 l2 [0 `. [

, E1 W2 b: a% _& e  modsecurity on;
) M. q. G% ^- S  C8 x0 t# Y! X- s0 X  modsecurity_rules_file modsec_includes.conf;
- C+ O, f9 \5 ~0 @  root   html;
+ O' B1 k. _. J2 ?9 K  M5 l; r" V% i  index  index.html index.htm;
/ I! V, K! V( s$ h) s}
6 J, Y/ Q$ Q6 A}# d& [+ z, d. ?% v+ Z
验证Nginx配置文件
) `9 e5 z- t; a; [2 O  T$ /usr/local/nginx/sbin/nginx -t" y! {7 _! b! {, T
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
* |8 ], \) @: ?* P3 g9 ~. T* rnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful1 M3 y& `$ w7 Y
启动Nginx
9 d. L# E5 z" d$ N! F( h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  ?) p& O8 ]# Y7 {& D9 O5 S- D
: i5 m; H! _6 o' P. |6 s

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


. t! b: a" J( I" x; g1 J

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-16 04:27 , Processed in 0.076934 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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