找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9726|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% ~$ u/ w4 Z% p
1 z( a# @, N# x3 \
/ [& d2 G! I, N" M3 j# p6 P! b在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。" z& _% @, a# X3 J7 K+ {$ k

: G) K' L% p" t/ G; ~) U. c3 S5 v' \$ G3 F0 m: G! ]3 P9 i
什么是ModSecurity
9 e  ^( c( x; T5 ?* g  ]5 l; YModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 F, y" K3 d9 W: i3 ?
" c/ j8 W/ D. ?: ~9 O) X+ m

/ d  {: x6 N. EModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
; R2 ~( C5 ?1 F7 U( w$ N1 O/ a0 ~0 L: S0 ~( U) d

5 V  b- ~' R. h* i! ?ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
  g5 J, i% `) L" N- p7 a& _( C! n
& c  s' }& z& _- f. A, c8 N  t2 K# M* [. r  L- X3 {
官网: https://www.modsecurity.org/
8 |, y; a8 H' i! M6 q! `4 w
# y) m/ `1 l4 o8 Y7 o' Z
% K% L! E' r( `7 @2 q- s什么是OWASP CRS
9 @. h. o3 b- r$ fOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。7 S. q- L5 t0 y% c' L3 j3 p

) B4 r) G: J+ Z7 Y& O
8 j+ c) X  a! }5 M( IModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; @& \* Q  i7 X5 z' i& J; R
* N$ y" \+ M9 u7 v5 o& g

1 s2 [) ?. V1 `( m  E( ?HTTP Protection(HTTP防御)  h* S  ?! j- l: t) j9 n
HTTP协议和本地定义使用的detectsviolations策略。
4 H7 X' T8 n. m  @! O
4 F# x  P" d( [2 R/ S3 M1 `. A! g/ g( x2 `0 U* t4 U8 f
Real-time Blacklist Lookups(实时黑名单查询)
- `, X$ [/ T" i, v8 p2 w利用第三方IP名单。
2 U1 W3 D/ U: y/ {8 b- t8 E
, \7 O# N9 d+ I. ?8 Y. k7 R: O0 K) }
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 x$ ~4 Z4 G& n2 r4 f防御HTTP的洪水攻击和HTTP Dos攻击。' s" m! @& j5 `. k( j1 y0 s
# I- _* {6 G/ V1 d  e* Z# |  ]' `
! W3 |$ p% K4 Z, J, n
Common Web Attacks Protection(常见的Web攻击防护)
# y. m& P- i# X5 I检测常见的Web应用程序的安全攻击。. O. E+ ~! E( r7 n! B8 o
# e) ?  _0 n' W* k

2 I: t7 y) ~/ G; dAutomation Detection(自动化检测)
7 w& U) D3 F8 K2 {8 G检测机器人,爬虫,扫描仪和其他表面恶意活动。
+ ~3 W' P- i4 e; k# ]2 u
; l  w8 t* H& |
" u* W2 H% q2 H2 xIntegration with AV Scanning for File Uploads(文件上传防病毒扫描); `% W" H* M" J  f$ M3 Y' f+ A6 r
检测通过Web应用程序上传的恶意文件。# G% D* b( `* b& O

6 [* A+ {  m$ U: ]; q
* Q6 O' w2 O! ?8 O2 q+ [4 UTracking Sensitive Data(跟踪敏感数据)
+ |6 o$ \' U- L( H2 a  w信用卡通道的使用,并阻止泄漏。
) h1 `8 t, k0 Q4 }0 a" ]$ x' H  h3 X! M5 M+ |+ A$ M

, j3 m0 T2 r3 [) @. r6 Q- QTrojan Protection(木马防护)9 Y& \$ N3 F( P6 \6 c+ X8 o! [- E
检测访问木马。
' j4 u6 X8 X3 s" x% X% f/ a
; V! ~5 A8 `4 y( o$ h1 ]* q; Y, L- b6 l# @
Identification of Application Defects(应用程序缺陷的鉴定)& Q, H2 k% {- Z3 h: ^
检测应用程序的错误配置警报。
* F6 o* p: f& i5 K4 q6 _2 ?) b4 m7 Z* |7 d& s+ h

$ z/ |8 d* K) cError Detection and Hiding(错误检测和隐藏)8 H0 @4 v3 F  a/ |! K
检测伪装服务器发送错误消息。
2 h* D8 ~& D- R& F  i0 A5 ^. ^$ l0 i- J/ [1 {

5 H9 T, {$ m8 @3 r0 _安装ModSecurity
! O' x, q: v' v软件基础环境准备
9 X# @' g$ h; J. f下载对应软件包! W6 b( F) K% C* D- R. M8 r
$ cd /root
  A$ \  j& l$ D+ y, M: W$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 x* e) l: I3 X: H6 I1 h( T
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz( K" U! Z  v6 J' D/ J, N  @
安装Nginx和ModSecurity依赖包
% B2 Y2 J: O. K$ pCentos/RHEL- I3 f% \" k- o6 u8 A

" b  E& ?3 p+ [
% X% s! n/ d4 q$ Q( L" T. ], ?! D/ L$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
3 T1 p, {7 q  ~( J5 \Ubuntu/Debian
& N. n/ Z1 M1 {. C0 j6 ]* e# g# o( r* ]( \: i, l6 |

8 r4 O: H+ u; g% a' U$ 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
9 c5 c/ b$ P* ~: \9 m. j编译安装ModSecurity8 l/ g& }* N6 d
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
. \. J$ p# v1 h" |; Z5 `# @: E5 Q7 T
4 X3 V7 ~* ]% Y& r2 s* U
方法一:编译为Nginx静态模块6 W: P& e/ m* b2 O; g4 l2 V1 a1 w

) {5 P  f. n& ^' D3 Y  K- n& [# q, n8 a9 g9 f, J! @
编译为独立模块(modsecurity-2.9.1)
' ^: v  n2 E1 e3 c$ tar xzvf modsecurity-2.9.1.tar.gz
7 D# c4 \: f: m+ r/ f$ cd modsecurity-2.9.1/
. C$ [$ }1 K* i) L$ ./autogen.sh
* y6 ^# Q* S# D2 W$ ./configure --enable-standalone-module --disable-mlogc, X2 y, F, ^3 H
$ make
( c+ S9 ~9 k6 N2 H- j* Z编译安装Nginx并添加ModSecurity模块" o: x1 z7 W$ k0 K6 m
$ tar xzvf nginx-1.9.2.tar.gz
% R& T+ x. a, e) y+ O% T$ cd nginx-1.9.2
  J- y, o7 A4 I: o; ^" `" G/ {# N, t+ Z$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- a5 W8 \9 ?) P$ A, I4 t) }
$ make && make install
2 M# n1 \% y2 n& H方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! l, Y4 K( e) G: ~8 n! y$ J- c" ~* s$ Y
! [: d0 P+ C! {& L4 r) o( w) c7 A* x
编译LibModSecurity(modsecurity-3.0)
& e7 T: l+ |- ~* R$ cd /root  Y, L% W- Q, _; t0 p
$ git clone https://github.com/SpiderLabs/ModSecurity; m  N% f1 I0 y3 X
$ cd ModSecurity, I# |8 z! f' U6 \; x- y' O. F
$ git checkout -b v3/master origin/v3/master( K  @. V( l4 _1 a% P
$ sh build.sh
5 j6 K  \# H3 a  ~, C  ^, w2 X" G. s$ git submodule init4 U; Q( R) m+ S2 P6 Y
$ git submodule update
- @, H3 _0 D$ l8 Q; u8 K9 ]! j$ ./configure
2 v: }" ^5 i& b8 c; F* b1 h6 Y$ make
) F  b" ?1 V* o$ make install% _$ s6 L! e7 x* ^9 w: G0 F
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。( J  o) ], h; U0 F8 N% P5 z7 c; ?

7 u. `; ]  m! A: r  ^* t
! {0 b: t" j+ L+ x& f$ ]$ ls /usr/local/modsecurity/lib  B; z) }) A/ z. ~& i
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.01 r, T* ~1 K& k1 _* M8 B- Y+ n) H
编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ `: Z+ f- h2 k+ }: R# k$ I; j# A使用ModSecurity-Nginx模块来连接LibModSecurity
- ~+ W1 ^; J9 }- B7 C  A" `% j" _  n4 x2 t: A1 m

8 |# C7 ^& Q) `, Q+ j" d$ cd /root
( y) }+ f) |( {/ W2 f( v- ^: X$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
; e& S( U; [) l5 `+ P% h! `$ tar xzvf nginx-1.9.2.tar.gz
/ O4 P4 l* ]  c% C' h! H$ cd nginx-1.9.20 T- h, P; S1 t2 E8 I4 D
$ ./configure --add-module=/root/modsecurity-nginx
+ e3 n5 d" J" U( y  P0 B$ make& s6 X! ?4 Y- V$ U+ h+ r
$ make && make install3 l9 F' n/ L& a  s; ?1 ?. M
添加OWASP规则
6 I- b1 w8 g5 i' D  ?' f5 _' [ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。) ^2 B4 g: t: }
% T4 I: ^4 Y( {/ J9 |( T+ W

! ~5 O' ]" M; Y! j下载OWASP规则并生成配置文件
5 N' h( D0 p# }% ~- o$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git7 a5 x- U* q: N7 ~6 C$ ?$ g  Z
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/) _8 [& u9 v" S; [4 I* U
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 q: u3 V4 M7 I- R$ cp crs-setup.conf.example  crs-setup.conf
3 z+ }0 N9 k" h  P1 Y1 |配置OWASP规则
3 @8 }0 }3 T! b/ t编辑crs-setup.conf文件
4 c& f9 F' p9 w
& L+ T" D6 [, F& i/ ~2 {5 j) F; ?" Z; U0 F# B7 O& n. G
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
5 p8 d% _, q# B9 R$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
: L7 |$ O  y2 I- r# I$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
1 `- Z. R" q# H. A$ G7 r$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. Z) e) f8 Q% i% X$ e默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 x2 l% n% Z, m6 j) ?
$ D4 h$ T7 U) P; h& o. h
3 ?5 D4 p7 q) k! G& y% t/ E
启用ModSecurity模块和CRS规则, ~1 y8 X2 r7 K: _6 H# J  s
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 E! h. t3 q5 a6 _  I
6 Y8 ~8 u) n8 {9 D3 I! t5 p

( x  V6 t( v! Z! fmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& Q" N/ W& ?6 u2 Q: K+ E) M
5 J7 N. Z+ P6 z- d# n
- z) c/ F. ~/ I2 R& X$ cd /root/modsecurity-2.9.1/$ w4 a7 u3 X4 X3 r5 \& X3 s* v0 G
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  - u. W# Z& Y, _: [
$ cp unicode.mapping  /usr/local/nginx/conf/
8 y) h; B! _- }+ n+ G将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。5 e3 T3 X/ k! |* G& p" r

9 C( T3 v% n: P3 R' ]' q1 F. N0 @, z/ b0 W# \" H
$ vim /usr/local/nginx/conf/modsecurity.conf
0 `# f1 K) O' Y) ]4 K, b; Z. cSecRuleEngine On" z7 O) b8 u* w- U0 o! T- a; z4 a
ModSecurity中几个常用配置说明:3 A4 _& Y+ E  S' m
& ^3 t3 z2 ?9 j+ ?8 e9 o6 e) g

3 y1 X: K" z2 v1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
) q/ J$ a4 V3 ^
- r- W3 A) y) X2 ?9 Y
4 e  j8 R, _/ X8 h2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  N& {5 Y; t4 K* K  R* V# C
$ h6 f7 d' v$ z; b
8 ~. D) H; h! ]8 z# p6 q+ i
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 |4 Y! R- N* _& j" A
3 G4 N7 N& D3 o( }# W

' A2 }- L) `: c% |1 p4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。3 {9 U! G3 p7 K# q

- w. u! t) F+ g4 ~+ P/ P" m
; E. w6 P7 S0 h1 z8 |) m1 E在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# b; y- w6 R" _0 y5 i
, F/ u+ e% F& |% o, p! W" m; T5 S- _. d  \; F! W
3.x版本CRS
4 J* U. c- h+ v0 z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: j$ F: L4 w$ I. l/ X* Q8 u
# 生成例外排除请求的配置文件) X" C) T6 d. j
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf3 m6 r/ @8 Q2 j' R+ T# o
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
5 W* n3 ^- V; ]0 m- I  l1 Z! l( Z$ cp rules/*.data /usr/local/nginx/conf% ^2 Y6 i: r, T7 L+ {: ?
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 r. h. x+ w1 T$ T5 z. s8 d

# k& [) Q( [# M4 Z# u6 L0 W7 R  U; ?" `% u& J2 T
$ vim /usr/local/nginx/conf/modsec_includes.conf
- r# W- q+ Q: C- Z1 h. P8 x( Z9 q( A$ B! j( B) P' \* V
[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

: X5 x* K' c9 C. s8 F& E7 b
2 a3 |9 L2 n) q/ b" `* _1 z( Y7 K注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, o# c1 v& x7 [# j# @- |

! e2 t( D. v& ^' t! S! t" j2 m" n* a; E/ U8 Q$ \) c/ g) Y
配置Nginx支持Modsecurity
* M& C9 p2 W( c8 G2 _; O& w! I启用Modsecurity
# `6 z6 c  i& G使用静态模块加载的配置方法5 O' l% ?( B# ]1 y$ g% _* A% Y+ J
在需要启用Modsecurity的主机的location下面加入下面两行即可:/ R2 J( M+ c, i8 c, ?

- _5 X5 h; {9 u4 T, a
! j) w4 S2 e) y) i0 c4 JModSecurityEnabled on;
, Q" B" t( O  W0 V1 O& AModSecurityConfig modsec_includes.conf;/ K' \9 l3 r$ }4 p5 c: j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 N0 O, a3 K, S
3 _% N. r0 _1 i6 B

0 U) G8 U+ M% ^  _+ \# N1 W- G$ vim /usr/local/nginx/conf/nginx.conf
1 f: G5 b/ ?5 [
9 \' L4 b" S7 ?2 @* X5 ~& D6 D/ D
& H+ j2 P" ?4 x( _! Cserver {' b- k, b! X: p4 p
  listen       80;
+ ~3 c) U8 L& {8 k  server_name  example.com;  Q' P  k3 o/ e% f
+ C5 S, c& N3 }5 I
2 z: ]  r1 |. m
  location / {
" P6 [+ }4 l. O' Y( K; R    ModSecurityEnabled on;7 |* b( K' r" O9 m4 d) c! `7 O4 S
    ModSecurityConfig modsec_includes.conf;
; ~* t0 a/ ]. }4 A; [    root   html;
$ Z/ a9 _4 c6 [3 a    index  index.html index.htm;
0 m* y2 ^& m4 f) R$ b; K  }
& _1 Q7 N( T7 p& X" t}  D) f1 R2 u6 M0 ^
使用动态模块加载的配置方法; Z2 N, s! ~  r5 ]4 C7 ?0 a* B
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 |. q7 P# K! @" ]1 _; s( s& [
' w, }0 y3 R  e( d( |) M

: }  f# h( R; _* w8 G" Bmodsecurity on;
0 A# e  w0 S' Y$ x% Cmodsecurity_rules_file modsec_includes.conf;
9 Y5 ]8 P7 z7 A) H7 \1 X. L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. P; z/ \" l" i4 \

" O5 _1 o& N9 ?
6 T  Q9 Y& U) F2 u+ w. Z& o  |# l$ vim /usr/local/nginx/conf/nginx.conf
1 _5 U! K5 A) [2 z  A) F5 l& I/ e8 Y& T5 p0 z
) ]$ _" s! L9 v7 V6 j
server {# x3 Q; H; K& `" n/ g$ J
  listen  80;
9 C9 i9 l& @1 E6 K6 S" {  server_name localhost mike.hi-linux.com;
8 e" R8 W4 k* b% i3 @  access_log /var/log/nginx/yourdomain.log;+ R) i3 J3 B( f: k7 K

! I: z. C( h2 Q$ K! G+ h( x
7 |# Y" y! E0 ~' d& s  location / {, d5 V0 A) `: |

3 {- }7 u& n- R. J9 E4 |0 I5 }2 j. g% @- l$ o3 P$ `" Y" y
  modsecurity on;
7 t: l% d% e) ?5 y) A  modsecurity_rules_file modsec_includes.conf;. P4 [9 {, N/ u) d5 F  K8 V4 I* z
  root   html;
2 V- N! o+ q8 `* @6 z1 e! b* |  index  index.html index.htm;/ O6 a$ a- O: [8 h" j4 O7 e) p
}! R! G" ^+ u" _" W0 y( W1 E
}) u# X. S6 H* h$ h4 ]9 }
验证Nginx配置文件9 D! y" ], S6 w% n, [
$ /usr/local/nginx/sbin/nginx -t# B4 D8 b- \$ B. T
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok' X# i8 T% @6 b% {5 L
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8 X( c5 z7 Y& K$ t启动Nginx% ?, {: u( y1 o5 d; B# H% l
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& y) j+ w3 }; D! W# ~6 k1 s1 F3 l
2 |& A8 v$ g% j' Q2 j

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


, x6 ]. |" W& o5 g7 l8 c

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-14 07:20 , Processed in 0.082123 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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