找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11946|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
0 [3 K' p- g% `  B: ^# C5 k8 D9 X+ ]& q
% S' S: u. M7 u! @% q  r( G
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" D. e* O' v" {, J9 \9 a5 b/ l$ i0 Q
) T- Y, R  w; X) M2 y4 ]! d6 S  T# N6 u; D
什么是ModSecurity
$ Z7 w, }6 d4 o3 T% k6 \( {ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
6 C5 t, b! X1 N0 o& `' V" J( R( g9 r/ ^& J

4 ?6 j" d; A+ ]$ J/ XModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。3 b* f* _" w$ e  ]  d
3 Z3 p8 E7 V+ X' y
6 p, g* d4 J8 z# Y+ L6 W
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ c9 w9 n7 P/ a$ m

. x6 H6 Y; n: d* i8 O
4 k, T7 o. |% R; E官网: https://www.modsecurity.org/
8 i$ Z! u3 |, f6 R" I. g% C: s- ~5 k( h" |2 D- O8 Y  @

* M& [0 V: o+ j6 V什么是OWASP CRS
. o6 L  i. P* W8 x' q+ y& KOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。7 P. U' }; X' c3 Q% F3 }, \# @& V
: I# d$ g) ?' s( j3 W- E* W$ y* o6 C
  ?6 J3 c( b$ E' a2 M
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! X6 Y8 _7 C, d) y8 p4 H0 Q7 S& t. B6 C
* L+ x2 z1 F0 I3 n/ S+ i& E
HTTP Protection(HTTP防御)
5 R9 a$ d" B, p/ O. B0 l% |! @HTTP协议和本地定义使用的detectsviolations策略。
( x8 o0 e5 p% j
" c$ ?$ [* M( |* t2 r7 }  I  _2 I7 f2 F/ L0 W
Real-time Blacklist Lookups(实时黑名单查询)
/ `% A* p- F0 d利用第三方IP名单。
3 e/ a% I1 |3 |; H) ~
* [# t5 ], x6 d- y2 d1 b. X6 I, z3 [+ y5 u
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
- T1 U1 P, j: A5 k- Q防御HTTP的洪水攻击和HTTP Dos攻击。- F- H/ Y( e. q9 G; C
/ {0 F( E. r) j% q

$ D5 H( `- ]2 w0 v/ RCommon Web Attacks Protection(常见的Web攻击防护)! U# I; J; q: N, F
检测常见的Web应用程序的安全攻击。$ O$ Z/ I; ]3 x$ F0 v
$ O% A, `+ `( v5 A6 b( l& v  ~

+ ~' F5 {+ m6 E' G, u4 b3 _Automation Detection(自动化检测)
  G7 `8 }1 y! D0 l6 C检测机器人,爬虫,扫描仪和其他表面恶意活动。
3 t) ?$ C; x4 R  C; N
: t/ l8 k) t9 a% x, m. l: ?
2 A- P4 v# W% C& ~Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 S( |8 }$ L1 h检测通过Web应用程序上传的恶意文件。. Z% o) l0 W) J/ h8 l+ R6 K0 i

: a$ X+ \: j. S( W6 S( M. {+ W) O, e/ z; j! [- d4 s+ o
Tracking Sensitive Data(跟踪敏感数据)! t& }5 f) A6 J$ n7 q
信用卡通道的使用,并阻止泄漏。& @) F  Q0 l9 M/ c' P
) z8 j5 E$ r  G! J/ ]
* _. B! I! `" G
Trojan Protection(木马防护)
7 W" U/ D0 K  o2 u: Y# P检测访问木马。
/ e8 R9 Z6 B' f& D) S' e  \. @
" O2 I  P: I& C
$ O: M1 C. u/ P/ |* G- A9 Z* lIdentification of Application Defects(应用程序缺陷的鉴定)$ {0 q* ?" Y( }# Y/ L  q
检测应用程序的错误配置警报。  f# D. R' B) K; K% E! d1 i
; {7 }! i- Z/ {" v

6 e7 G( S8 q/ j* H# z: aError Detection and Hiding(错误检测和隐藏). I6 W3 a* e. A' ^) v# Y" @$ {* @
检测伪装服务器发送错误消息。" y# P1 e- c5 y
9 m! n" r7 ?) O. k( t) w* R5 m3 \
0 ^3 S0 R9 B" h: W
安装ModSecurity  U; P8 Q4 j8 T4 y1 N  ]
软件基础环境准备
8 j1 b/ ]# \  O- X% [8 L3 d下载对应软件包5 z3 S) |/ a6 L, `' X* L9 W
$ cd /root
6 F" d" J* e0 e; P) ~: `$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 s1 r. `, p. }0 v
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# c. @6 e6 m) e+ g
安装Nginx和ModSecurity依赖包2 l* J# |) f5 i  w3 X" i, f4 g  t7 w
Centos/RHEL# x1 ~! s* u2 c
+ P* F$ U! A, o
" M$ W. K) s. W% U5 P( }
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, X  U6 W, l* Y: |' s- |1 W
Ubuntu/Debian
, W9 R* `: D( Z! }, f' H! a8 N+ E" G' p+ \" q+ n

5 O  N: s+ ]8 K1 P$ 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/ i, p. `0 |& X! l# p
编译安装ModSecurity( S  b( F: c& ?) L) }1 d
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- h+ b# D; y  X: v

2 x4 U- P" Q* k; j+ a( R
1 @) Y+ _, I0 V! N8 C/ K6 y方法一:编译为Nginx静态模块
/ l, x, s: f/ K8 _4 y% m$ s) b2 Z9 A) T6 L
8 O+ s  h- m9 r( y
编译为独立模块(modsecurity-2.9.1)/ g; k! Y9 b) H& d' s7 l* h
$ tar xzvf modsecurity-2.9.1.tar.gz& g  K1 i7 X7 P( O! Y
$ cd modsecurity-2.9.1/
( A6 `9 l1 H# u8 j$ ./autogen.sh
, s+ M: Q3 G5 l- j* |7 w2 T3 [$ ./configure --enable-standalone-module --disable-mlogc
. V( C1 G6 Y1 ]0 `3 S, N# T$ make
% ^1 }0 |0 V! w+ q2 W编译安装Nginx并添加ModSecurity模块
$ o4 h+ o; K7 _( h* M8 `$ tar xzvf nginx-1.9.2.tar.gz
/ p/ D+ r+ I) b1 G9 H. l$ cd nginx-1.9.2
' X# Q" v- t  j% O6 ~$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# r; O7 B1 W9 H
$ make && make install; k; W) \3 L2 [* t4 Q- ]
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- Q0 N$ K) N+ C5 `
3 d4 F+ C5 K; T
. o6 ]6 N* L2 q& `* C编译LibModSecurity(modsecurity-3.0)1 ^4 q$ J, A2 ]' v0 `7 o/ q) F
$ cd /root8 d; x0 H3 r6 Y
$ git clone https://github.com/SpiderLabs/ModSecurity
4 t6 z6 T8 A" F% s% g$ cd ModSecurity
+ Z& @* b# P( j$ git checkout -b v3/master origin/v3/master
% B6 {* `+ c( \# r; k3 m$ sh build.sh8 ~+ k9 W7 |6 P, X' a# p
$ git submodule init
* `' Q) ^" R; r$ git submodule update
  o3 M9 |4 w9 H/ e9 G& x' @: h1 ^$ ./configure9 T! s0 t, y* T8 U+ _
$ make" u. ]* C7 T/ f+ i; }+ }3 ^8 I
$ make install6 Y+ ^- a% ^  z2 c* m6 u$ |/ a5 `: W
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。* c' @! S7 J4 I* b3 D) x1 M
0 n3 p, M1 r6 y, f/ j$ t1 d, M
7 t# N6 z) H( a6 C6 s7 g
$ ls /usr/local/modsecurity/lib. V1 |/ i! g/ v8 f6 j
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
5 G2 j* b5 ~: Q' r+ a8 m编译安装Nginx并添加ModSecurity-Nginx Connector模块
. |* C; R9 B0 ?' u: m6 s, p! R使用ModSecurity-Nginx模块来连接LibModSecurity& Q# U$ V% [- ]

! B, ^& P5 m. G8 Q
* Y, d* Q, l0 {" Q. _$ cd /root
+ m% o% z$ Z  D- W& j$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
9 S6 G1 {6 j+ i$ tar xzvf nginx-1.9.2.tar.gz& u# j( @4 j, x7 q. M8 O, T0 _  e; C, d
$ cd nginx-1.9.2
9 k1 p9 S9 b3 h6 }1 v( E$ ./configure --add-module=/root/modsecurity-nginx: l- O+ F0 c8 S8 D
$ make
6 f0 S7 s$ M/ C3 n3 j$ make && make install6 p2 s1 Q+ \4 Z5 N, I0 |9 K
添加OWASP规则
  i4 L$ n3 ~# X5 ^: ^1 V& IModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。$ o: T3 }- _2 V6 M6 r7 j; r, S

5 D1 y( a' B6 j; G7 [6 t
4 G# i* B8 D2 y* r( z! B0 J下载OWASP规则并生成配置文件! M9 B* B3 h2 I  Z! l+ |. j& Q! g
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git8 j- o* @  t& U) x1 z
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, ~7 C" ]0 c( _, @% V" U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 I/ I7 j8 U2 ?3 ]2 E' y! b& W& E
$ cp crs-setup.conf.example  crs-setup.conf
+ `) E8 E; P& ~9 |- O7 B- L配置OWASP规则( L! ~6 v3 W; I4 V7 F! S8 h" Y7 ?
编辑crs-setup.conf文件; q( n( Q& N, F. Q. K
$ J! [- d0 L" [% D
+ `. z, R& u. G7 d9 v, Z' U. Y5 t- G
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
- A  w" I; L# ^7 r: s$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. r* j; Z  z% \9 d! b$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
4 k, E; C4 f' }& A8 i% L$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf6 y/ u9 R5 N2 ?7 Y+ h$ \
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) A; h& Q' j' p3 n3 A& q
/ I9 v+ W  ~4 Q: L: P: j
" e! d' U3 B8 @4 ^启用ModSecurity模块和CRS规则
% F* R8 R& s- ~0 j; C复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 y3 q- \( o2 o9 y8 `, ~4 f) l- i5 f& B, N5 t. d4 [/ a

# R! W2 A! |0 J% lmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。( M& s& \/ j# v! q

1 Q- w0 }, y$ P+ }+ V5 N( _  `, F% x
$ cd /root/modsecurity-2.9.1/
2 N7 J; |5 F1 b+ @+ e/ q$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  7 t  A$ V' D5 q, `
$ cp unicode.mapping  /usr/local/nginx/conf/
3 ^/ W4 A9 d/ U! x% C6 X1 o将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 r7 Q; [; R% O! E& O6 q
. p/ Z3 P# @# i- H

: u9 u  ~! B+ I$ vim /usr/local/nginx/conf/modsecurity.conf9 N2 Z  Y  P/ _* [7 U- i
SecRuleEngine On/ t+ I9 c, @& A4 _0 ?" n7 J, l
ModSecurity中几个常用配置说明:
' [2 A5 e0 ], J5 G
0 F! g; t0 J! Z! [  l4 D3 H2 l# J- a/ \1 B6 ~
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
6 I0 p* F7 I" K4 y3 E  s& q! g  x+ M4 P( R( ^* f# c5 H

1 q  i8 y& Z. B2 T5 S$ l" j6 B2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 d" D- w* j7 M# E/ S  G& O2 R6 n1 J, }- |- \8 u! z

- o8 ]" v  X( F3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ E1 c* ^( B! b& `5 w; t
! b2 D/ x% Z: ~+ y% A
# F$ `4 f* b  l" O# Z7 m: I5 L
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。- P( m, `% n8 o: d8 g2 Z

0 n* X- x/ W7 j/ ^# t! m/ L) C1 _) D! q! i
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。" s& B# ?! O3 e! |
2 e0 g( q6 z) r$ K

* V  K* d9 {7 i& ^" Q6 C# w6 o3.x版本CRS9 Q: w# v" g8 a5 I( ^+ F
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" Q3 c7 p/ i" N2 ^# g
# 生成例外排除请求的配置文件
. A- I5 B- I$ n' t$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf5 O( t" M1 ?2 c6 ^
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
0 ?7 b* O6 u  [7 F$ k* q5 A8 X$ cp rules/*.data /usr/local/nginx/conf( B# w1 @- {0 I1 R
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
% D( \$ ^1 x( d1 o: a" d& S
. H# Y/ `9 l2 a( E' U8 L5 h+ z% q* S
$ vim /usr/local/nginx/conf/modsec_includes.conf/ Q  v5 E; p. S2 Y' H1 A' Y0 I

5 X% T" O7 F- r5 a  A0 W
[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
- V6 i. c% w" x$ i) t% ]: `

1 y  Q1 ^8 C4 z5 }6 W. p注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。( `0 I: O  B6 ]8 E& t
8 D2 n% l) c* r. B4 R2 u

( x! \* R* g, {3 y配置Nginx支持Modsecurity
" \6 X. {7 B& z  ]- @! l# X# c启用Modsecurity- N" U) B0 p- o$ Z
使用静态模块加载的配置方法
& y% I8 R+ c. r; G在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 U( b' p9 ?( N3 ~: N; v) n1 p$ B1 x1 M0 y) E$ g% c
1 b& N; u2 ~" T9 e4 h. X
ModSecurityEnabled on;
5 a9 `+ t( W% w3 ?/ T; tModSecurityConfig modsec_includes.conf;
1 _2 q( t1 J0 \0 W8 K: o" x3 E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 o1 Y2 A: U7 i9 {

. k5 [6 `  U3 S; {- ^6 T" t" f9 n) n6 N' u2 l: c6 K( V
$ vim /usr/local/nginx/conf/nginx.conf; o3 r% ]' t% L" G8 R9 B+ ~

' v) f' n  o7 ?2 L! P6 I( A
$ ]8 r$ @- m; \0 Lserver {( o+ L; x9 [- f$ v1 j  z
  listen       80;
; A; S0 E: N3 P& f  server_name  example.com;
. `& e/ h* @# z8 M9 E8 R! h% M+ l7 G" e! E! |
: k. e2 x5 B* C# R3 x; _8 k. r
  location / {
( s, d& H8 [- K; U    ModSecurityEnabled on;9 n* V4 r- ]1 s% ^. F' Q
    ModSecurityConfig modsec_includes.conf;! j$ X+ x6 r5 K: q! u
    root   html;/ c/ {9 a- E; d$ u
    index  index.html index.htm;8 V: o! w) ^  V7 h5 D& j9 t7 _
  }
4 Q. P3 U* Y2 ?& c8 }7 \" |+ f}
& Q$ H* p; u* S3 N使用动态模块加载的配置方法0 ?! q0 c1 {5 m$ c. M, @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
& k, C; x1 F5 ?" X& W: o7 o% P( n" s# P& S# o4 P4 i" Y
# i' a6 B$ x5 ~& S# u) r
modsecurity on;. B1 U/ V% r7 c0 e) V0 Q
modsecurity_rules_file modsec_includes.conf;
1 O! y1 i. h$ z0 E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) \0 Z; J) q% f5 |
6 r. H, M# `# a6 r2 I
) F, H) k* c# l9 f  j! V; w
$ vim /usr/local/nginx/conf/nginx.conf
% F& }  ~, H" s7 Z* O) |; x: @0 J3 E# q. U2 X2 E# c, ^
7 z2 E% O) N0 v/ M/ V! V
server {
2 B; v$ S& S) q  listen  80;% m, e/ t' J  B
  server_name localhost mike.hi-linux.com;, T2 c9 g3 O  _
  access_log /var/log/nginx/yourdomain.log;
' i. f- Z; A) B6 n/ j
" l9 m0 Q* {- z3 e8 _* }/ u4 Y! x$ D2 \$ S- b0 x3 e& |# X- E# f
  location / {
' C' B5 X" i- s
( J+ B3 o3 I1 p( v# x+ ^- t  \) `8 n$ |2 R+ J* ^3 P
  modsecurity on;
( \6 ^" T7 k- R/ [2 E8 b  modsecurity_rules_file modsec_includes.conf;: t- R: q: w' A( p- f; K
  root   html;
! @: K& Y" }, ]* E  index  index.html index.htm;
4 k7 A0 ]; v9 w! G2 g}: t8 S, x$ h+ M! E) G
}! b* _/ b! l) U- u0 |* E1 }2 X5 w# v
验证Nginx配置文件
* m4 W& ~* S, `$ /usr/local/nginx/sbin/nginx -t" J* t% ]- e. h. z; B8 T, H
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ ^2 h3 d* H) s3 }( S
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful2 u, N' |8 {9 Y; h3 o. r: u
启动Nginx
2 ^& G% C. @1 s2 M( f$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 ]/ c  m' k6 _! ?& {9 b+ l
/ A% l$ O- w! ~7 C3 M

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


; h0 ~+ n( r3 x; |- a

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-2 22:49 , Processed in 0.078009 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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