ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。9 g2 x+ T2 r7 s
5 a! F/ |7 h" A! ~* o
7 O4 |4 v2 T; }' ` [在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
& o9 P$ H# L z Q$ o; w" y' ^% a0 Y* u2 h" H/ F* y
: O- W# z5 i& i4 ~& v" {7 v9 u
什么是ModSecurity
5 i9 b: ]& z4 e2 g6 SModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 n& |4 ~" {2 P. Z. @. k+ m
6 ?) D/ P2 e, p3 i- K C* U
2 Z2 C4 m: J9 \0 Q3 k- r' M5 S( IModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
c& X3 V3 D* W# [7 {! a, C3 Y/ f0 L/ L- M* o2 c/ G* X, _( T4 i" y
0 Y) C1 q' l- B( M' xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ I8 ]4 ^6 l9 M6 L% R& j4 J) f
( x0 R. J& O' p- e5 E6 X' K. v$ F# ]8 A+ t$ }. O o6 q1 a0 @
官网: https://www.modsecurity.org/; b7 @' I! I2 ^
3 g; V8 x1 I2 D6 d' y3 [; L, g7 s) F+ C# K# P
什么是OWASP CRS8 }# K& I. }# {1 @" L" J0 {) r5 c
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! `7 L) D. I" J
2 p* h, \4 M+ b2 T- S! L
$ Q E4 n7 E: Y5 Q; b0 ~ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。5 M6 ^ d6 ?% l+ G
# ` J9 r: {6 i4 m% D0 w8 _$ N* m
) [3 O7 z. o6 c1 s1 s0 IHTTP Protection(HTTP防御)
/ |9 N9 C) e8 h& G8 vHTTP协议和本地定义使用的detectsviolations策略。" v) Z: K4 y; i: y' G
6 k$ O* v) Y% z' v6 I! Q) E
6 i! s4 ?6 U3 kReal-time Blacklist Lookups(实时黑名单查询)$ ^7 W+ J8 Y+ t! \) m# l5 X
利用第三方IP名单。8 o8 `: ]& e2 V# _
, T' k0 p1 j t) A$ A
0 U# ~0 j* k# y' b+ z
HTTP Denial of Service Protections(HTTP的拒绝服务保护) g5 k# X, L7 W
防御HTTP的洪水攻击和HTTP Dos攻击。4 B5 f, J; D- y: k5 ]& d$ t0 {
+ t0 C, j# ~5 ~, Z
. ^1 z* e! Y7 ]6 R2 l. W5 u
Common Web Attacks Protection(常见的Web攻击防护)
; F) s/ A# g9 j' p检测常见的Web应用程序的安全攻击。
0 a4 j6 e6 r; T( S# W7 L- |4 ?; A$ s8 Q6 d) O9 z7 i' o
. R. u! s+ G; l9 cAutomation Detection(自动化检测): A' H# _; c/ Z2 d$ h
检测机器人,爬虫,扫描仪和其他表面恶意活动。
. {8 r# U4 f6 p2 u4 e6 x6 M7 i( D9 t
3 m: n5 Y% o8 z+ AIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)" U7 U# y3 z3 }% w5 S8 D
检测通过Web应用程序上传的恶意文件。6 N3 @# s4 U/ } b: Q
. k+ Q4 e# g# ?; W& ~* Q
# o1 _0 k% J# NTracking Sensitive Data(跟踪敏感数据)7 n7 h8 O% z' E6 k6 J2 K
信用卡通道的使用,并阻止泄漏。8 R* m0 X0 z3 |3 {; o) S% t6 L
3 L3 B8 h8 G1 y9 }0 u; L/ O g5 H+ n
0 m4 ]& G' k, p
Trojan Protection(木马防护)
; V; U" w# `4 w1 ^( y% T7 h检测访问木马。
: \! N+ p5 n/ Y i5 k: p, C) b& d* I2 O7 B5 o& _7 N
! C# ]! K/ W6 F; _9 ^Identification of Application Defects(应用程序缺陷的鉴定)4 a2 F: M+ L$ M" k& x" B. i
检测应用程序的错误配置警报。/ W- C% }4 R3 d* g/ L' W5 w
+ d* k/ v2 U* O, Z7 X
4 y1 W8 E6 w$ w. f) E3 V
Error Detection and Hiding(错误检测和隐藏)# [! `6 \& d. e; c. k$ l
检测伪装服务器发送错误消息。
- G8 A j2 V" y5 V6 I3 L+ o4 S U5 ]0 N ]+ c3 J4 F6 i; z
6 A7 M# V- q1 F2 h; o, i
安装ModSecurity
0 ]. _: [" g! {2 a! ]1 N+ p软件基础环境准备$ `& x* Q4 m6 |/ Q
下载对应软件包
( ^( v0 _& m2 p- c$ cd /root
/ }9 ?! C# g0 h! {$ p9 Z# X$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'1 R# r" L+ M: p4 ~6 V
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz4 t" D9 C, p" {) z( C$ k& h
安装Nginx和ModSecurity依赖包, y0 o( E0 t7 U7 y
Centos/RHEL
+ t' S% p: A: l, v1 [4 E4 b4 H8 x, w& C# R
& C. G, D4 J% ?4 Z, X
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel+ R. K F% F2 p: Z' Z+ ^' B8 A
Ubuntu/Debian/ G7 Z1 j: E6 `6 l2 Z9 `
9 Q9 q7 p% D2 C* V* Y
- z! P6 S; a8 A6 R, y! Q2 ~2 V$ 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
3 T$ D0 N+ w6 R/ H编译安装ModSecurity
# s2 q; L# R0 ?( MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; h7 {1 v/ R2 c# O- l
9 t. ^& Q+ b& |) {. U
) O' G) _) f6 c' b4 {& r, x
方法一:编译为Nginx静态模块7 a$ R0 A0 I, F4 e! [6 I) F) Y
- X; f! W: [7 l4 S V) j+ H+ ^ u% T
编译为独立模块(modsecurity-2.9.1)
! |; h! ^6 k, Q- \$ tar xzvf modsecurity-2.9.1.tar.gz9 b8 @0 g5 h I
$ cd modsecurity-2.9.1/
6 F, H5 P! f! ]1 |$ ./autogen.sh$ c- ~3 C# K: Q" y6 b# h( W U: E
$ ./configure --enable-standalone-module --disable-mlogc5 r, N* \! J7 `$ h9 L4 R
$ make
4 |: K; z" T6 z8 Y6 r% p编译安装Nginx并添加ModSecurity模块
6 ]2 V1 y5 p4 Z$ \! S$ tar xzvf nginx-1.9.2.tar.gz) E2 S) q4 v0 I, l0 j
$ cd nginx-1.9.2" r5 s( n& L8 O9 P: g( ?3 e5 W0 e
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/3 _! [ O7 Z$ |
$ make && make install
( I9 [. L" q" J2 T, O2 @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块) c/ R6 _& U6 B0 L" x) Q5 a
: n+ d+ I( m, P8 i8 o
$ Q) h+ h/ A; }. c0 r# }4 m. j. a
编译LibModSecurity(modsecurity-3.0), L5 Y; ^0 I! Y. l* x- `
$ cd /root$ U' J1 Y; M9 G
$ git clone https://github.com/SpiderLabs/ModSecurity
% F; A. R/ e8 l W2 H$ cd ModSecurity
L3 W% ?: j; Q. M7 I# y, J8 M$ git checkout -b v3/master origin/v3/master
# D2 m6 O! }+ T6 D- A7 e$ sh build.sh, i, ~9 w3 I$ c
$ git submodule init
1 k+ X! m3 v+ r Z( u$ git submodule update
: J% a% T9 i( w O1 c0 T$ ./configure
. i. V* Y8 _6 b H$ make1 c ~8 d/ B1 W1 l+ L8 O& {
$ make install
( t" h, Z9 \5 HLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 p5 ]: Y$ \4 K: g/ t
9 @& z4 `4 Y" A( v* H
7 x: A* F. \( Y" l8 o$ ls /usr/local/modsecurity/lib
; P @; q. k2 n$ l* ulibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
# z( U0 H8 K: Z; A$ _编译安装Nginx并添加ModSecurity-Nginx Connector模块
5 D' T; Y& V$ }" n5 X使用ModSecurity-Nginx模块来连接LibModSecurity# H4 T+ [; Y9 k2 J2 r. O! h
7 U# N- {& P( o0 r/ s( I
# A( L! C! j1 P1 r7 ~9 E
$ cd /root" p9 }: _ a% h8 I1 S" `0 t
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx. i- {5 @) W, ~- S4 k
$ tar xzvf nginx-1.9.2.tar.gz" G% H) O# r- s* }1 Z7 [2 ^5 U
$ cd nginx-1.9.2
: ~( `" ]/ g+ i1 K/ [$ ./configure --add-module=/root/modsecurity-nginx
' i$ s& r6 ]: C, c$ X7 P$ make
2 U' Y' l& _( x$ make && make install G+ |1 p* Q0 Z4 W8 g
添加OWASP规则& \2 Q3 R3 |. F8 i; ]1 B
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 H3 b; b+ K8 B8 g" |: ~* `& {) g2 d: V( R& D: s
# o' B4 S0 U8 U t" s下载OWASP规则并生成配置文件! M7 t/ |! e( f" L. ]9 h. B& G
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) j- i: e4 c" J# l1 x4 \+ ~7 T6 }
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
* y# l1 g* T* x, _' ]& P" b- j# m$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 }' h/ U3 T3 r. h6 g2 R1 F$ cp crs-setup.conf.example crs-setup.conf
" E; q% N" [1 f2 N, Z配置OWASP规则1 Q0 Q! S) w/ T3 a
编辑crs-setup.conf文件0 F! l! o8 i" a/ y, [# V* X
! Z2 R! c- ~0 m0 z4 [: [- f
* B7 H& x0 n- m, d$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf' [9 h# v' k5 ?+ [, c
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
+ ?$ a; N: M0 V$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf1 }% m4 O) Y5 ? \) F) I
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. C8 ?2 R0 p7 Z默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。# y# p* T0 K( D& _. D
+ n, ]; ^. ]+ k
' K' R; H' b/ q启用ModSecurity模块和CRS规则
6 ]. h, R( F- e; j$ j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: K# J0 ~6 {& G5 w; X3 N) }' i5 p- D: U6 b
: E* j- P7 J& F) d* t& X# Amodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 A8 |2 w* D( j1 r) o) ~
+ ]% n: g7 q* G' \& K
6 R( ?1 H# D; B, X- H$ cd /root/modsecurity-2.9.1/
) h5 e1 ~, f' B# E e9 F1 ^* E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 7 x/ Y! C5 F& ^# p9 S8 Q3 u' y
$ cp unicode.mapping /usr/local/nginx/conf/
6 O5 Z- L7 z. Z& ~% R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
# M4 X- W% K$ ^: K' ?, I
- h" H( K/ d6 N) G# Y4 g5 D6 w, P% R# i" I; H. p& o
$ vim /usr/local/nginx/conf/modsecurity.conf9 M* i% } A1 |) p' V ?
SecRuleEngine On6 \2 h7 ~3 s! Q* R! L- \
ModSecurity中几个常用配置说明:
% s7 S; U- `% b" L8 |9 R7 s& \9 K, Q1 i9 E& Y$ w: K4 k h
7 f; s6 x x8 H
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
- J! \* a7 D' P! d1 J3 G; A
0 b( T& \2 e0 t5 w! t9 l5 m
# r1 e4 a, P* |6 X0 o2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
# s; q8 K" m' c, Y1 @6 ] R) e' f1 X- F1 D, d
/ |7 [3 [* q/ b- X$ i; c0 R4 F3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 |; j# t" w1 e `! i7 ~" w9 i4 y& y
$ E6 i) R- y$ U0 h, u8 \* k/ Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* i) o: B1 h3 c2 D# r5 Q1 k
) r+ s) N- {8 e. p4 b% [
' O* E: u- }! b* d在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# d# B9 k( S! K# P, ?
7 o) R' R; Y g, y# O; i
" A* l9 i5 ^+ C9 f1 S$ D0 h3.x版本CRS% x, ^2 n) g7 ?: ~. Q/ v' H
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ C$ c- h" b+ R( e# D1 b/ g" ^) v1 _# 生成例外排除请求的配置文件% D4 P' M) p1 J
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf- ~. k: P) S! Z: C8 b) S; Y
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, j- j4 x( \4 c# Q
$ cp rules/*.data /usr/local/nginx/conf& u; L4 ^9 F' i$ d- g
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
/ ~1 `! M; D' p9 f! n) @, p3 Q, W- l4 q7 {/ y2 m$ r
$ T" z; _3 K# D& H8 W; h$ vim /usr/local/nginx/conf/modsec_includes.conf+ [: e c1 R) Q! j
! p3 P5 K- B. F; V, R5 t
[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
) C! v& O$ V9 n7 W. h1 X) s$ H8 s! f
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 J. n8 e) G+ w* s! L. _
5 d7 G, ~1 E. J/ J: x8 p
- ?% j* ]+ p& ?- _; X配置Nginx支持Modsecurity$ J. z* D. \* x% x0 [
启用Modsecurity
, [2 o/ Q9 ?# K9 c& q7 @8 K# m使用静态模块加载的配置方法
+ q5 P c- m& b: d: S在需要启用Modsecurity的主机的location下面加入下面两行即可:/ p# Q: }2 I0 g2 |9 H5 V
; D# x4 @) [0 C
% a: Y) F$ t' ~0 o9 u% K
ModSecurityEnabled on;
) P1 x3 m: k2 zModSecurityConfig modsec_includes.conf;" H# B( w! A- O5 n2 H5 {$ Y, E! }8 \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" i( z7 n3 H$ J5 `# j* A" i& z; W
( R4 J$ v0 e! U, u
/ u2 g& T6 u+ y8 _' @% X# z$ vim /usr/local/nginx/conf/nginx.conf
t# ^! d- g) g' G# g3 b* T( i4 z2 q# ^: t: q3 m0 s+ w: v$ R+ C
) S6 p8 j: U6 o0 ?5 \/ j' K1 ]! d9 ~
server {
1 K2 C! w4 H { s: {. p listen 80;0 W' c4 k& Q* ?9 V& r( \
server_name example.com;/ L% ^ Q- A& a
! Z: I3 u L! w n
4 l' v+ B9 `3 E2 s$ Z
location / {- j7 T5 l9 D1 c
ModSecurityEnabled on;
% o& g9 d5 l5 l4 b ModSecurityConfig modsec_includes.conf;
! i* ~8 e; f x5 P# Q p- O root html;/ E2 z3 o6 p7 C
index index.html index.htm;
, T' [. w) _4 r {6 X+ y }
( l$ D) i( l% ?- } l4 y+ u% j1 B}. q9 o8 N T# C
使用动态模块加载的配置方法
& I. d4 l/ c$ Y8 U+ h# U在需要启用Modsecurity的主机的location下面加入下面两行即可:' e& t7 h- Y" _: @$ M0 ^% i
; E5 ?- i: G$ y3 o
' Z7 A) f9 S. B4 C. C; q' |* I; ?modsecurity on;' r- A% z c* D1 H
modsecurity_rules_file modsec_includes.conf;
) z: l2 R3 M. V. O' g \2 g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 D% t' [& p) t6 o6 w( Y( T v
$ z# o$ P' S- E
0 M# _9 C3 o! N7 f$ vim /usr/local/nginx/conf/nginx.conf' a" w; h' a+ f* p6 t
8 E6 ?/ s* d- U0 d
" c: L0 H j& mserver {8 ?& v4 h/ O3 ~( }8 w
listen 80;
1 b6 G! L, r0 A$ M: U server_name localhost mike.hi-linux.com;
5 T& E* f+ |% j* _7 `( V- i access_log /var/log/nginx/yourdomain.log;
3 i' ^; J0 z: P9 ^
: Q9 s I" I% {. q
) W6 K) F2 @* A0 r6 N$ _1 g location / {% U, C) ^" m. D$ s3 Q
, z0 B( s8 r$ h' l
1 g* I" O( j, P: N; H* r9 c modsecurity on;
& j. a) U4 ?& A4 w modsecurity_rules_file modsec_includes.conf;' U: {8 W" T- }# N7 r* U+ ?
root html;
: F5 B% O/ Y" [/ x) ?0 N index index.html index.htm;
\/ H4 Q0 G2 `4 h& [}
! t* I4 Q5 S. j( P1 ` a}9 P6 Q: \: Z% c0 K9 k5 x
验证Nginx配置文件
" S; @5 L# v! P4 X2 K" H( c* e$ /usr/local/nginx/sbin/nginx -t
9 G) [* U7 S8 E9 k% B% X7 {, U5 |9 nnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ G+ `+ q& y$ I5 ~nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ o; z, ?% ^1 f
启动Nginx
7 h/ l9 F4 g2 F( |* I% L( a# h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
" n+ _! d2 o3 m8 _* y) w" q. m, f
' S: k7 M3 p% b% U U测试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.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
5 A6 L/ r" B) Z. c( i2 h( a |