找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12680|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
) Z! u, r- u1 e6 F: z% y7 _6 K5 H: X+ l3 |* n

: ^- Y! k, ^( U, H, t7 d在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ H0 r5 j* K+ X# W1 |" e- S
& ~) [: b' [/ A4 b0 P2 m# F5 F% w) v4 Z
什么是ModSecurity
1 t4 n7 z, H, a& HModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
5 A7 n: O0 G$ t. f  w; F& ]. L2 Q2 J! X5 W. u

/ h+ `& o: {' t! l# HModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% U2 ~- X, B9 `
) l( B- g* l  V3 N$ i& h! }* A8 ~0 x* {( _, l
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- _5 Q" a3 v+ M9 S

1 y8 e' o  t, K4 f+ D: h% c% a9 |, w3 d3 j3 C% d; N' `
官网: https://www.modsecurity.org/
1 \) F) b; H( W' p! [) M0 o8 c' H1 F# ]! ~, n

2 ~) I& B! E  C% Z! m什么是OWASP CRS
* [. q' }% N& G8 {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  @7 b0 @" I0 i" w4 y; n, V' Q: C$ Y% E8 f

7 m; t7 f; F7 E+ P- a& WModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
; F$ c5 U6 E9 t% H" r! v3 b% f  y9 V# ^

& ^/ A1 ^. G; g6 k: ]3 Z' c0 V# nHTTP Protection(HTTP防御)! z0 i7 R9 Z$ z4 D; c, W) B/ Q1 P
HTTP协议和本地定义使用的detectsviolations策略。
2 b8 C4 H5 `. B1 a$ J7 _2 I$ P' A+ G% K5 K4 ]- X
# B% J( ]& g) W) O0 X
Real-time Blacklist Lookups(实时黑名单查询)  ~7 S! q5 o5 T0 b$ m6 P" t
利用第三方IP名单。1 D9 s, S+ n. ]) |1 z3 o# f

' P2 L1 i" m0 i  z' T) p9 |& S. M9 p2 Z6 |% N) {
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
& [" W) u( R# a, d: v. {防御HTTP的洪水攻击和HTTP Dos攻击。  v( X! Q# G0 |) E3 h: z3 g% T
+ T9 k( M, L+ {5 O5 B7 ~
$ y; D$ {8 v$ |( W6 \
Common Web Attacks Protection(常见的Web攻击防护)
# R4 c# T& i1 W0 ?" d6 L4 Q5 |检测常见的Web应用程序的安全攻击。. K% ~; j0 m0 T" {& x

2 y# [/ {- n' }% b$ W8 n9 R5 t; E
4 @7 m# G7 ~' A1 o$ |: g% sAutomation Detection(自动化检测)
/ V: P0 i) B# e% j检测机器人,爬虫,扫描仪和其他表面恶意活动。5 l; t# S2 d# _6 P  i- Z
) r, n& J$ d+ F; x

7 R: {8 r/ P. R: OIntegration with AV Scanning for File Uploads(文件上传防病毒扫描). h; q9 ]2 I* j5 \
检测通过Web应用程序上传的恶意文件。0 j6 C$ v4 m/ k5 u0 w  Y* ]

  l" o3 x, T7 u7 L* e  p$ S# P
+ E+ k; V- v/ g5 U; LTracking Sensitive Data(跟踪敏感数据)
( q5 B; T1 x6 I0 h$ m8 r信用卡通道的使用,并阻止泄漏。
: R  F# ^2 ?5 ~2 a
( _% @: ]9 `1 E0 B% @! ]
% J7 y; s9 o! ]9 t0 TTrojan Protection(木马防护)6 I  i( H! I/ ?7 m- s
检测访问木马。9 ^5 T4 n+ {: d

& `' P& `; y8 l4 N/ j1 T: p& A6 R' `! E& u4 d* Z+ u5 u. g% T
Identification of Application Defects(应用程序缺陷的鉴定), x' w( ?: }+ U$ z/ e
检测应用程序的错误配置警报。  ^8 e: C3 x. U  R# a

4 O/ q" t$ J; U& i: N6 p+ O  f. ]8 G7 u( h
Error Detection and Hiding(错误检测和隐藏)- f$ z" n! p4 [# g" H" X
检测伪装服务器发送错误消息。
) l; x( n0 J$ x! {
$ a& M* E  c3 E. H- M) N! d& B. F% |( y) ~" `1 r9 v0 T% H
安装ModSecurity0 N' `7 C( t% H* i8 U) z3 U
软件基础环境准备
/ i: u& Z5 D) B; c: U; D' m下载对应软件包# ?; n1 r. o# r
$ cd /root
8 w$ A3 s: z7 z: t3 C; S' ~! M$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
+ M1 k4 m0 \5 F. r$ 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 r7 m, n  D& S安装Nginx和ModSecurity依赖包8 d; M# E4 i. n2 N. n
Centos/RHEL
1 u; ?; p3 T5 E1 u; q- {7 p9 f& x( D  E* W, W+ R' G

7 Y) p: ~0 h* T# T" i3 g1 y: X; P$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
  ~2 g: H: h6 j/ YUbuntu/Debian
& u* P* d0 [1 j7 b& M
0 q# u' T8 T! z" q* a* Z2 J( g+ g/ x+ o. o& 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% A7 i6 p- |5 ~1 ~
编译安装ModSecurity* I6 }3 X2 e) b
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# z& Q7 Z' K/ X5 m/ C( x; t
  X& @' D  ]5 h8 ^+ f+ u4 i

6 h# z# ^' f$ l$ q方法一:编译为Nginx静态模块
/ w  l- z! ^8 s2 A: F: H3 m% W" [+ \6 q2 _6 Q- T& R

0 L5 j  V9 t( S6 \4 n: j. v编译为独立模块(modsecurity-2.9.1)
( O. L- y8 N+ z! [! q$ tar xzvf modsecurity-2.9.1.tar.gz
2 o+ }5 S1 |: \1 w0 \$ cd modsecurity-2.9.1/
' P9 A$ G9 t2 r. F  g2 V$ ./autogen.sh5 ?% F# Z. f& c0 |. c+ i
$ ./configure --enable-standalone-module --disable-mlogc% \6 R% g- C# P3 ?- [; |
$ make
! o" b: o6 f+ l( Z0 \编译安装Nginx并添加ModSecurity模块" }& K0 P4 e; G
$ tar xzvf nginx-1.9.2.tar.gz
1 N9 w; ~. R1 A! u& A$ G$ cd nginx-1.9.2  |* x0 W5 y3 i! r- Y8 x0 L4 B
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; _2 q/ y9 D6 Z$ o# m, |$ make && make install& \3 s) N$ J' V9 O" ~; |
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块$ s' \4 H) T, n

$ e* C+ T1 H$ I+ u
% w: X4 J; [) J. |3 V5 \& S" x! o3 o编译LibModSecurity(modsecurity-3.0)) B6 ^# m% u$ k6 \! ^# D0 I, X
$ cd /root2 v  c% z, x+ |1 {
$ git clone https://github.com/SpiderLabs/ModSecurity5 ]/ d* h! L' x% D$ T1 d4 D
$ cd ModSecurity& z! t3 Z6 L8 |9 f" G
$ git checkout -b v3/master origin/v3/master% Y7 u7 f. K+ _  M! v4 s
$ sh build.sh1 k: l$ f2 Y5 |3 E
$ git submodule init
1 ]* j* ]5 w& ^% r1 \; B2 L$ git submodule update% n1 t1 A$ ^$ d; K
$ ./configure$ l, c+ ?: f2 y+ [, N- N! |7 @! U
$ make
. |! \0 H7 K/ e8 w$ make install
0 @4 {# A6 d. H( Y! \. b: uLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ r' K- G/ [2 I. R$ ?6 M7 ~' v
& J) G0 g6 V' Q: q  Z' @$ [5 X8 k( E6 f/ e: U, ]1 Y
$ ls /usr/local/modsecurity/lib
4 {6 K+ U  W- i: o# _2 M7 ?libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
/ H& A! q5 M7 U9 ?编译安装Nginx并添加ModSecurity-Nginx Connector模块4 v4 y8 O1 O( {% v6 Q6 r7 t# A( W+ x- d
使用ModSecurity-Nginx模块来连接LibModSecurity
: V. A  H5 n5 k0 a9 j) t& h& b1 s/ Y5 j( C- M0 v, W

2 l  l" ~3 A5 N* f& T$ cd /root
7 f5 p; X1 R4 t+ s6 K" l$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
9 t$ [! n3 q1 n7 u; k* X$ tar xzvf nginx-1.9.2.tar.gz
3 V# `) W) M! h6 `5 ]+ C8 Y; u4 `$ cd nginx-1.9.2
8 B% H0 q! g$ K2 A$ ./configure --add-module=/root/modsecurity-nginx
# H% z7 n& ~( p6 _; ^4 s% M$ make
7 Z: t( ~+ m! _, I# T. W$ make && make install
4 a. E0 {- F/ c添加OWASP规则# z7 M, p, F5 G. q5 |$ |& D
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
; {% k, W9 r# j& q* l! e* C6 |
3 X1 }( O( `% Z* L
/ _: P; o1 z; h4 j1 f; l+ S0 H下载OWASP规则并生成配置文件+ m) ^0 Q/ c$ W6 z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) F$ g, m# @1 U  u. r
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
( y# k: K  ]5 W2 v7 `6 \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  w4 H0 Z1 U# B9 V$ W6 t+ }* P6 v$ cp crs-setup.conf.example  crs-setup.conf
& y8 d! L5 L: b配置OWASP规则+ N* i" H3 Z: v7 ~  v3 I) R: v. ?
编辑crs-setup.conf文件- Y% o3 f" `# Q5 k, g- ?

% l5 e/ K: X' g4 v# M0 l7 `
2 M6 I% c0 i. `* n; n1 }' p$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf# W; t. A& Q* x6 s4 d" ~1 D9 X$ \
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: d: V$ v5 ~5 ^& z, o6 \! Y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
1 E+ g: t' f% ]1 }$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( a4 {6 K4 b8 w! V默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
4 r# g" p( Z5 F; S
3 H& ^) i1 `& l5 @9 @' ^
7 p% R0 U. n. O; k6 `启用ModSecurity模块和CRS规则  E. u, T" n& _, c
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: a* l: `7 K7 f% r: N5 q- R+ k: S5 k, r
$ h0 g; p2 M- m
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' ~- g8 u; Q" A0 W: a
9 O6 ^& e0 x( a3 `1 Z1 g
! X% A( h2 Z  ]$ c9 H+ `
$ cd /root/modsecurity-2.9.1/
4 W9 T1 `) s5 a8 _, q* u4 C$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
4 I3 K- l' s# P3 T4 Q+ B$ cp unicode.mapping  /usr/local/nginx/conf/6 g4 U# r  P9 U7 U' f* \& p& |. M; y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。, w0 ]1 G. o1 b) v
# p5 |# c" f8 T( I1 \* p. o
9 }/ l8 H( r# y0 e& t
$ vim /usr/local/nginx/conf/modsecurity.conf$ V+ b& c5 z9 P" Y5 y. _
SecRuleEngine On
" Z( H* Z9 @6 K0 ~8 N: k9 ~ModSecurity中几个常用配置说明:& |: R( @0 P" n7 v; e

, Q8 d. _" m; P  w  F6 [3 m) A8 L% C
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
  V$ o- \; T1 [1 o% w
- W& w$ t2 Z$ w6 }6 M6 p" y
2 D0 l6 H/ v* _! r2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 {/ F1 W$ O, c6 I. i  W+ S2 _9 U( T3 h* E1 J/ }1 d3 c

. i( g, K# g+ n3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。4 m7 O. S# B' I3 w3 T$ e, d) @0 R

$ W& x2 S" ?3 n* m$ x+ `8 {
. A, ^3 G0 x* C" ?$ `* X4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。9 L3 D6 C/ q$ z- {3 Q
( H! g8 p8 u! L0 ^; B3 s7 x
1 \3 ]3 Y% p* D% I& r  Y8 m, E; {; X
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' {) n6 y7 ], d7 u$ v1 C. Z" A" S& t( |9 R$ r4 y( S7 E
5 {% O0 u  ?% |" ?5 U5 I! M' Z
3.x版本CRS' L/ ~& }- G, o
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. Y; N; p$ }3 l/ S# 生成例外排除请求的配置文件
- H/ c; v5 c8 A5 r$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf- i# q1 R1 o# _' t
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
+ V6 i1 S! R8 w9 l. T$ cp rules/*.data /usr/local/nginx/conf
0 J- H# z! s, S) f- \: K, p为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; h$ Y; i& u: ?" V1 r
1 K8 N# P- L/ c9 B- @0 R0 ^' F/ ~1 ~
$ vim /usr/local/nginx/conf/modsec_includes.conf
; w7 I4 |! C, V. E
$ M+ `4 R( X( i9 C- d
[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
- G) b1 _! \$ a3 O, T
0 S7 ]2 I% p; m9 v+ a" B* ?
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。+ J; o* J# i% P) j# m
$ B1 J4 ~, H8 y% U1 e+ V; `

) C2 D7 H9 q. b4 P. K配置Nginx支持Modsecurity
/ x$ ]! S- N" H; f+ P启用Modsecurity# j- y0 B1 }; i9 {1 |, G/ a
使用静态模块加载的配置方法
+ R: S3 F# \  S在需要启用Modsecurity的主机的location下面加入下面两行即可:
' j. p) T6 _$ J0 S3 ^) k9 m# X
$ s, M2 d/ f) }' m% K& a8 Q6 Q$ ^* L- c# R) q6 A
ModSecurityEnabled on;, b  _2 ~. `% X+ `' O
ModSecurityConfig modsec_includes.conf;
, C# ^! ?6 I7 i9 ^8 t修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 B& s/ s$ }& w* h$ l# q5 z7 Y
; N* L( W% U- T4 J. u, }' ^0 x$ e6 r  x+ z0 K! _
$ vim /usr/local/nginx/conf/nginx.conf
5 K1 u8 A  `( ~8 F
$ \. P2 ]3 x$ g0 H
% l. |7 c. \9 L; M* \server {
2 u; [+ K1 Q7 W( i  listen       80;
: y, X7 z+ U# |  server_name  example.com;' y# h0 N) O* D5 L8 K$ X

  @4 X! r/ P! I8 S
! A, H3 I) O* Q# K, M' a  O  location / {2 K+ m% \% @) g  b
    ModSecurityEnabled on;
; F, ~9 x. [/ u7 z) g' ~* n    ModSecurityConfig modsec_includes.conf;
) k# E4 u% ], Y5 h    root   html;4 k6 K) e( b" @% d1 `( ^; `9 o7 A
    index  index.html index.htm;% V$ Z5 i3 w* x- I4 |
  }6 l5 W0 j% Q! e) T  Y, w
}: n* J. I" V* E; \2 U) v& {0 u2 F2 h
使用动态模块加载的配置方法
' C+ ?1 z9 b3 g1 y在需要启用Modsecurity的主机的location下面加入下面两行即可:& |5 h7 `! Z' f6 _0 o0 F$ a2 b

& s; r; [1 C) j: G3 E9 a4 t- |& {0 p0 N. b7 F. A5 O
modsecurity on;0 \: D7 {/ t3 s
modsecurity_rules_file modsec_includes.conf;9 i& P' k! W: M: @6 d) x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" E+ l& [/ Q9 B3 K  i

" y9 s, h5 |# ]: C& n9 i( \! r- x, O6 q$ p
$ vim /usr/local/nginx/conf/nginx.conf
8 b8 p; X8 e. r5 p" ]; X
4 r+ F& n1 C2 Q0 ]( y4 l) G: `! i8 z' c5 t. k! \7 }
server {
* d2 m6 Q7 f6 W% |  listen  80;" W/ P! w4 Q, I. E2 B
  server_name localhost mike.hi-linux.com;# f* p  o7 u  m& o
  access_log /var/log/nginx/yourdomain.log;
! Y) i3 h  m4 R' R1 \6 ?# b, H) m' e+ P4 J! t8 ]; S

9 s# v- }3 c# M  location / {
9 O0 ^- r7 t$ j* B! l3 F8 N  C( v6 g8 [! f- k/ [

* n) g0 T  X. {; e) q9 S  modsecurity on;
0 j+ U" y8 \# }; Q$ P- S; K  modsecurity_rules_file modsec_includes.conf;
  [2 L/ [0 I3 y. K0 C  root   html;9 i/ T% x  Z1 j/ a# a0 Z8 S. a9 C
  index  index.html index.htm;6 V% a" }+ e3 t, x
}
# f& c( x$ O* p$ h}
$ `% N9 l/ w) j; K验证Nginx配置文件' o+ A/ W2 b3 q# G  C
$ /usr/local/nginx/sbin/nginx -t
9 ^; S) u9 A' O- A$ [nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 z0 V& @4 Y. L8 G( \nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
. W5 ^7 @! ~: F  F5 R8 v启动Nginx
* S1 P4 g8 H! U$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ q; j1 |0 r6 g; J! T) V; P
( _' r! A  [3 |

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


& N$ p3 @/ c  ^# D$ _# p( t

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-29 10:19 , Processed in 0.089636 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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