找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10439|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 [+ \6 E' Y& r! w  \4 ~1 G2 H! g8 ], R

: u1 @7 k& E: p% N0 O) m& J+ ~" R7 m5 A
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
' k# W* |/ N" E- l& k, t. M, t  ^" M  ]5 Q. F* N# n" B, u
. W) k) a" r! G+ f. k
什么是ModSecurity  K( v2 m0 D6 Z. J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& u% J- Z: x$ R- `! B* C, G3 S5 I* D8 ]/ _6 l/ A; {
- f- {) }" B6 f( c9 k) g
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) ]; b! e, ~* U4 I  a5 F* V: |# x2 z9 p3 u4 h& D

" g3 T; y2 ]: [8 E( _ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
/ ^. D3 b1 Y8 z! [2 J( L* [! ]- t7 ]
4 g- Y9 v1 h$ W  ?7 s$ g. u$ Q) X* k% t
官网: https://www.modsecurity.org/
6 x6 z/ b% C& ]4 Z" Z& [* x( [2 p( B9 C! z0 z5 ?
: [3 J, _8 I" E3 `' i2 a
什么是OWASP CRS# b) A5 p1 X# D4 N# L* [4 s
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
, m0 H; y, P$ t
& e" P# F3 U, M$ B( t% ^' `2 O+ R, Z1 c
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。. V% A- [" S/ W* F7 }
9 |4 \0 I; }( N0 V& @+ B1 F
: ]/ C9 e' q# t% f( o* @
HTTP Protection(HTTP防御)
" [6 x% Q. @" k% UHTTP协议和本地定义使用的detectsviolations策略。1 J* [% b# E/ r- M7 J- K2 n4 R; w
( s6 K' D) S- T+ |1 E. i

5 |9 f: `/ e1 \7 R4 eReal-time Blacklist Lookups(实时黑名单查询)
/ Q" }4 E2 @$ Q8 U( O4 u8 y3 [利用第三方IP名单。7 M4 C( h1 J3 o  j

2 u$ J7 B) ~, O7 w- r& f$ r" Z$ p8 s( M; g( y2 ]& J3 p& ]! B4 h8 B
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
2 o. K6 o* e. d- Q( C7 p防御HTTP的洪水攻击和HTTP Dos攻击。
8 v. E: C3 Z4 g3 H- @7 a: h1 ~; k- S
8 t9 }% P2 {6 Z- P2 P4 S2 Q
Common Web Attacks Protection(常见的Web攻击防护)
% i& i" [. `# v" E2 ~检测常见的Web应用程序的安全攻击。/ ^; o0 \. ~. L) p

: s; ^- e1 W  l  T% Q+ t  L4 w* m9 c$ y
Automation Detection(自动化检测)
8 E, @6 p$ w( K% v5 A. e0 v+ u: c4 D9 l检测机器人,爬虫,扫描仪和其他表面恶意活动。. q* P, B" r1 e' `. @- O

/ _$ W- z( P7 o; F  e
8 j1 u8 A: u# ?* q# J! ]! PIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
; r8 N% J/ A! A* U9 M) x) I! d检测通过Web应用程序上传的恶意文件。( `7 J0 Q7 s4 N% c* W+ \$ K
9 m  g# A! A) E* h: Q, w( C

* d; X. M) n! z9 l  A8 R; y* RTracking Sensitive Data(跟踪敏感数据)
' v- U3 ^% c" S  i" ^; I+ C信用卡通道的使用,并阻止泄漏。1 O/ Y% h  T4 X' c  f* z- y

6 @; P2 f* L6 ^: [1 K/ ^6 d0 i1 h! R" p4 f& s; w
Trojan Protection(木马防护)
; k0 q, n$ D3 ^7 J0 J3 k& _6 w& d检测访问木马。
: X% [5 z, @; k! M& d* M& U- X, ~+ \6 G
( A' V: k: N' Q$ D1 I4 p0 a% s
Identification of Application Defects(应用程序缺陷的鉴定)! h# |4 n: m( [0 @- A) G3 W
检测应用程序的错误配置警报。
+ K$ m) l" I4 e  Z( t
9 c$ r9 @, Y$ N8 z1 Z
, k- C. O0 ~" f; P. v$ h  GError Detection and Hiding(错误检测和隐藏)" X2 l2 K% l+ i2 x& i- h
检测伪装服务器发送错误消息。% b/ u  h4 `6 c

3 s% r- m- v$ ^$ e6 V
, t9 L" v1 n- u  [% |+ \) G安装ModSecurity
' E$ j0 v  C6 [2 O% T软件基础环境准备/ J& H) d9 P$ ]8 i3 \" |
下载对应软件包
1 B6 N7 ~  S+ t: f9 t; N1 I$ cd /root
! [( @0 j" X  N$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. E! J% e$ [1 M4 k; l8 ^; i8 H2 U
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz! k3 g/ l: t/ @; |4 q+ g
安装Nginx和ModSecurity依赖包
, X; E8 r: @+ Q4 jCentos/RHEL
( A) Y& R; d) e3 i& `) H! s% t' u! A  h7 G, M8 u) @
+ w; b6 f% K/ s- Q- z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 q4 Q/ k9 z: ?" M# U" w! N
Ubuntu/Debian
4 r% }5 p+ \$ P$ d- p! Z" {* G1 `* @' M% m# F

: K6 l# J1 {3 h+ k; ]$ 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
  a8 |' H7 J5 t编译安装ModSecurity* C! B8 B4 [+ l% P# s
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% O% D$ m& `6 ]. B/ K
3 @- Z% p" \* {7 L" b# r# t

) ^( e1 R/ e; _方法一:编译为Nginx静态模块
* C2 {4 J- K) [6 A% q
4 q& K) s0 n  o* a1 L
% W6 t, B  B) `7 N8 s编译为独立模块(modsecurity-2.9.1)
' Y: S0 ]; `1 F$ tar xzvf modsecurity-2.9.1.tar.gz: f0 p2 g4 g# {
$ cd modsecurity-2.9.1/
$ n: |+ C# ], I7 D% _$ ./autogen.sh% D7 F" |* q+ ]4 |
$ ./configure --enable-standalone-module --disable-mlogc" D& ?, m% F# H% X
$ make
  n7 ~7 q0 p( {" k' b3 I+ |) ~编译安装Nginx并添加ModSecurity模块
9 [" n! d2 M& A2 s$ tar xzvf nginx-1.9.2.tar.gz
* x- ?4 }- [, K' ~$ cd nginx-1.9.2) u* q" R$ W( D7 g# {
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
+ W3 J( l5 `6 W/ b$ make && make install
+ f( [$ P. b! g, i+ N2 j方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
  P# N; [- I" O& R0 q3 v( \) U0 T% t2 j2 w
5 s7 L" `4 x+ A1 Z
编译LibModSecurity(modsecurity-3.0)/ j  {( U4 x, ]  W
$ cd /root
. V7 \( _# E/ e$ git clone https://github.com/SpiderLabs/ModSecurity7 z; p9 @' y  I4 Y7 M$ b; J
$ cd ModSecurity3 f) M# ?3 H- i, ~- L
$ git checkout -b v3/master origin/v3/master9 f: B4 `3 M4 r: V2 H
$ sh build.sh
% [- ]$ i2 B( g# u+ {$ git submodule init
3 ^# s) I$ e& O$ git submodule update+ @! I4 T' G, N0 |4 J2 e' h
$ ./configure! g; x" r+ Q, t0 M' |
$ make
7 ^' x7 Z5 E; Q) g0 N$ make install
$ U  Z+ L0 R: ^: s; |( o1 jLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 g, Q' h- x; R( a. u: J  @5 c! X! w" Q

, x" |; T8 k) U! P0 N: ]$ ls /usr/local/modsecurity/lib* t- P2 V* C8 S6 Z6 W; Q: K! @
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 V; @' ?1 s4 o* l/ P
编译安装Nginx并添加ModSecurity-Nginx Connector模块
9 K1 I- n" T$ D* b# L! [使用ModSecurity-Nginx模块来连接LibModSecurity
7 S6 p" d. ^" \5 k( w
6 p# o- {0 i9 S+ ?& \0 Y% d/ j5 e1 w- k
$ cd /root
( D+ M1 q" b2 a# G' `$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
1 n& n% C! @% v0 ^% A( }$ tar xzvf nginx-1.9.2.tar.gz
0 `& h+ m; {. S; X; t/ w# L$ cd nginx-1.9.2
1 W" i) e$ L  f' W3 W) b; U; z$ ./configure --add-module=/root/modsecurity-nginx) v; @* M' j8 V2 o  C7 c
$ make- J( L1 X$ p0 T" A3 B1 m1 u
$ make && make install
+ I8 H/ ^, e- q  _* n, A/ m添加OWASP规则+ x( G7 ?( l( }9 f
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
5 g8 V( `7 E0 P6 y* p  P0 |) p! P, Y# I9 I% y4 m2 o

) I7 N7 B; N: t7 T2 A5 h+ `  Q下载OWASP规则并生成配置文件' `8 s3 X/ H/ K7 P( G# O2 v$ ^( N' @
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: G& p0 W' c  p! K8 _
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// E1 V0 w: `% B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( G" N+ ^/ T5 P, F
$ cp crs-setup.conf.example  crs-setup.conf7 x7 b3 O4 K) a' t6 s# W
配置OWASP规则
$ `, t, L/ c$ `; \* r& ^编辑crs-setup.conf文件. G! q) Y+ ^+ A$ e4 s9 r# O3 o; {

% T2 C6 m6 Q6 w5 p
- G1 k, Y* L9 Y/ h$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 X+ V; ?7 b( w( a* X  g$ u
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 a- B( n' w! ?# _+ Q$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
9 R, m  o2 g( }$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 K" K5 i" b  j: T7 F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
! I2 j& {) Q7 z# ?( V! B, C0 c* y; R) d, O1 B7 z

0 V5 U" }8 U3 G8 @8 R1 d5 F启用ModSecurity模块和CRS规则; A3 S, I& |, [! y/ D" b! _
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) X; _* K$ B* D, T& z) e6 S( l6 ~5 [# v2 K0 \

* ^+ G6 ^1 r2 B- p8 O, ?3 H( h( @modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' b: a( ~5 A# s0 z
) G" C3 W. d. h) I
" ]6 h5 {+ v$ X
$ cd /root/modsecurity-2.9.1/
# c! ~7 |  C0 a& J0 q. d$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
7 d0 }, {) o- k$ cp unicode.mapping  /usr/local/nginx/conf/
1 @) }( W3 ~9 Y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 D+ F* E; Y3 |7 p" Q1 q

9 Z( y0 i1 f" R: V6 [+ y5 a- c1 e" I
$ vim /usr/local/nginx/conf/modsecurity.conf
" V* D# a0 A% G- w" K6 F- J/ Y9 |/ u& c" qSecRuleEngine On
" O) O& U* ]) z2 z: \( v9 {ModSecurity中几个常用配置说明:
' `/ l& s9 Q; l( D/ ^& T+ [
; a- E! T# _# O* P
( L" I$ J% [4 `) U) r; d* p; ]1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。  H; i6 M, F! ?. E5 N' ]; F5 J1 O% ?' Q
' S  y. T& u+ c3 b/ ^* q

: I* l" `0 ?9 Z! S: o2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" o$ r: c7 Q* I. Y9 S" U

4 z$ u8 N* G; |' d8 U+ R: ~; W, L; p7 y: H
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。; h0 }" N) [% S, L/ t7 O
9 L) H1 O# F% }& ~) X6 `
+ Y& b3 X$ e' v! u# d, T/ Z& h
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* g$ X  {: r9 T. v5 |6 ]  p; A
4 G+ [0 P" ]( j+ @, E) E

* o- j$ ?. x- v( a3 \2 z8 H& ]% o在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 p3 K. Q1 [- \2 Q: w& `7 ]4 {1 p. t& x
$ ^+ s5 [& z8 @2 o
3.x版本CRS7 Z6 n$ s8 Q$ G9 |
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, x3 v4 T+ k* Z7 d9 l! I
# 生成例外排除请求的配置文件
# h3 A- X6 H# p$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. w+ a+ E% P, V0 ^* V$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 }2 c: V: ^4 W0 o
$ cp rules/*.data /usr/local/nginx/conf* d& e3 j/ T) A/ n) `
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
1 B2 k& X' l* D. r( |  K
3 A( |+ g) N, W" M+ ^* R5 N0 c/ q3 w# q% I0 k
$ vim /usr/local/nginx/conf/modsec_includes.conf
( E8 L% G5 T& \% U
- I% O% v! e' ~2 b# c5 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
: c8 G1 ]4 d% W
9 U* `% Y" H' u2 Z
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' l( U! E" C3 H  N0 m$ Q# O8 l! j5 {0 O& [2 Z9 S
3 J8 {6 O( [( M: P: C* j6 S3 c
配置Nginx支持Modsecurity7 M0 P( Q7 Z6 V* u$ g+ }
启用Modsecurity
- V# _- N% Z4 P使用静态模块加载的配置方法( P- l. S% V% A* g# x
在需要启用Modsecurity的主机的location下面加入下面两行即可:- |- @/ U' [8 F8 m

: o5 \8 X& [- u1 }0 a" r$ ~0 U( e' m1 g  |- P5 a
ModSecurityEnabled on;# m0 ?0 A5 q  x9 v3 j' I
ModSecurityConfig modsec_includes.conf;- l- w3 w* A! M& s4 N8 x2 |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: s- a) M* f1 j7 |
$ H: N6 w) X: L! {0 h6 M2 ~6 n/ m# d7 z) ^/ L# e
$ vim /usr/local/nginx/conf/nginx.conf4 z3 D. \: b4 F6 `4 L

" Y8 p5 w( ]) T. Z# {5 S
  h/ u" ^7 [, Z7 D9 Oserver {
8 v) a3 Z( L7 k. V1 e  listen       80;6 m- }6 I; J2 X8 t6 x
  server_name  example.com;# {5 @5 u+ Y4 y( c
7 [7 B5 K# U; _7 n' H8 `- l: p5 W. K

6 n  l$ e9 I$ z" E! c0 O  location / {
3 Z3 M3 b) S4 B' t6 V& h$ O. n# I    ModSecurityEnabled on;5 }- E( S( O! b+ E- [+ N
    ModSecurityConfig modsec_includes.conf;6 V  H" K" k! F* f$ l  @
    root   html;: l" U$ j  @$ V& @6 x3 [% a
    index  index.html index.htm;
5 H" y( b# L$ s$ G' l+ K  }
7 X/ @7 j, U. [: y) }- d}! m  n, o( f: ^
使用动态模块加载的配置方法* w+ i2 I4 A% H, B6 a  X! N3 ^' |
在需要启用Modsecurity的主机的location下面加入下面两行即可:+ Z* j0 s, {, C# C. e! a  x
; J" a8 K# _! K& N5 L
; ^& S: s) I' k
modsecurity on;% Q* M# Z: Y8 n* Q
modsecurity_rules_file modsec_includes.conf;5 z- t1 y3 ?1 \& ^
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* i+ n. L" x, w0 }' R
- O) K. E: Z$ {. {- f$ A( l5 w+ |
. w6 d' C6 y6 S/ ]1 c) k5 I; v$ vim /usr/local/nginx/conf/nginx.conf2 z# F3 Q$ F* ]( }/ a: W- m+ j; n
( E& ?. h0 j1 _7 i- [/ O: W1 ]! X
4 S2 k3 L% ~( A* b# B% z! `; h
server {
1 \% S! Z* n3 t  listen  80;$ |) i! }8 u# v
  server_name localhost mike.hi-linux.com;
* a! t8 {9 Z* ~% u) R" H+ k  access_log /var/log/nginx/yourdomain.log;" c2 g8 i6 v9 H0 v/ \
" ?6 N6 @4 |5 B9 o7 i1 N  s

( s2 ^2 ~- r2 _7 Z) Z" {* _  location / {1 p6 t& B) E, X7 @

# T& i. J2 w) w6 t$ m- ?+ S  f+ R2 K: t
  modsecurity on;0 w8 v. x4 z( b+ Q
  modsecurity_rules_file modsec_includes.conf;, J- H) N1 e- L1 W  S. [
  root   html;
3 y. l: m: h5 r( B  index  index.html index.htm;% q: y/ k5 b5 B4 ^; S5 l8 X
}
+ \; C2 _  W( ^1 }}& f+ V3 t+ p" H) g. r
验证Nginx配置文件1 W. @0 j: w! r- z9 P9 l5 r$ c  l2 U
$ /usr/local/nginx/sbin/nginx -t# k$ u$ ~" J1 Q! ?
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) r8 {" e( k# @1 y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4 {8 W3 j9 s1 [
启动Nginx
7 ^. `2 o) _& v$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 \$ ]2 ~; l% ]% Y- A9 B7 l

/ D  @, Z- p% i1 |" B6 T

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

5 _4 o, Y7 }  `

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-7 06:02 , Processed in 0.072619 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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