ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
8 B9 Y n$ Q2 j, D; u8 Y! u2 K; x
) e% ?0 Z, I4 w7 n$ e. d
. A/ W: C# U8 R/ M/ X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* ?3 T+ @- q. i- y0 R _* m" ]: C J
' l [, _* i9 b& K+ M3 H9 n! @3 r8 n8 J$ A, N, W
什么是ModSecurity. U: u: d7 C9 o' b, t& J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! N7 {1 M _8 L, ~# n6 O' u0 Q; b- F
! m* h3 ^+ G" d! t- Z- o: q* I- u
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
* L# b, E- ?* g" z/ D( b
0 E, k4 W2 p: f1 ?/ K
8 {' W/ R, W" yModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。1 ~0 `5 u; W7 S* Z
R7 t9 r8 p% y& T& P1 w2 X
& O G% s6 E) ]3 S2 O* d) l官网: https://www.modsecurity.org/' M# y. B0 S' L5 |
1 U2 }& y0 u4 ], l4 {3 q
3 g" a0 T; X) p
什么是OWASP CRS- D& l+ u( E' \0 Z, {
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
$ c2 i" M( h! y3 z! L/ C4 N+ E( v
0 K, K$ a3 M4 d" T! |/ q
1 k) |" Q" X9 ~! {' J% NModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ x: g; \( s% Y( Q: w) D( h) y( {; T8 @& f+ R
+ y3 j8 C1 O, [
HTTP Protection(HTTP防御)
4 S; f4 X$ \2 j1 _$ }# S% T* AHTTP协议和本地定义使用的detectsviolations策略。$ T g! B" v- x) T! ^. V/ r% Q
. ], f5 X! h8 N+ B) x
. ?* x }% s. ~3 f
Real-time Blacklist Lookups(实时黑名单查询)9 ^0 S2 x8 d" C$ V3 r) K- P
利用第三方IP名单。' q5 J3 m- o, S: d: s, @2 E+ c
$ N( B. V9 }6 h3 T, n+ \6 K% |/ k
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
. v& A( p7 {' e& k( g. W6 L1 g' P, }/ r防御HTTP的洪水攻击和HTTP Dos攻击。' ]9 q/ a% B/ S. @. f0 S6 n
7 \0 P+ m* h. v k
$ T+ I& U: X0 }Common Web Attacks Protection(常见的Web攻击防护)
% L# p2 Q* I; J0 G0 Z3 R检测常见的Web应用程序的安全攻击。$ v# T& ~7 d, Y" y. H7 P
' P+ O/ p5 f- ?, u5 C" J/ c0 d9 S& p8 L, e
Automation Detection(自动化检测)
3 Q& r( d" h% p# K8 b8 l4 S1 ~检测机器人,爬虫,扫描仪和其他表面恶意活动。
( e3 S( O' g4 b8 E, |) K1 E& U: ~3 i; m! T
/ z' M( c$ E/ E* i: R9 N- BIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
0 P+ Z( @$ M. f检测通过Web应用程序上传的恶意文件。2 p, ~5 |: B/ i6 I
1 o y+ h3 e( m' _7 G: I- ^9 v* I: d+ b; _
Tracking Sensitive Data(跟踪敏感数据)
' o6 H: ?2 B) e' R信用卡通道的使用,并阻止泄漏。
1 w! p, G0 l( W: `4 c' I4 P2 n/ D5 t4 t! o
5 V Y6 s9 _* T, w
Trojan Protection(木马防护)
q/ V1 K3 F, i检测访问木马。: R6 X3 L0 |4 l+ ?2 w6 [
9 ^) M# f0 M# c- X# ]) z! `4 W5 H9 M: h f% C
Identification of Application Defects(应用程序缺陷的鉴定)! V! e8 L& s. N1 b5 h' D+ g
检测应用程序的错误配置警报。, ?3 K5 X( D, ~) `
# P6 Y8 f" s4 p( B1 r2 K" ~. j1 L& p
, e+ l$ h: g5 V6 y# @/ g
Error Detection and Hiding(错误检测和隐藏)% T W& v$ W% b& L
检测伪装服务器发送错误消息。3 k- V. J8 H6 R% l) K4 S4 o
( I' O& @- t7 H
/ Y6 j" {& z$ h# h8 _1 e. E
安装ModSecurity: t$ P# E' N+ R5 q4 b# k; q
软件基础环境准备
" w3 w! s5 G8 q下载对应软件包
9 u' P5 H4 l5 l; l# z9 _$ cd /root
. z. w' G6 A# R5 F% A: T" W$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
# h' Y8 M0 i7 r2 H- I- D$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
7 }% d( ? B4 b1 O, M7 Q* x$ G安装Nginx和ModSecurity依赖包6 n. Z9 k0 d8 t4 i; @
Centos/RHEL
( r: }& `0 Q, [$ q0 C, U
2 Z2 S. B/ E r4 ]2 V
* G5 ~) Z; s9 e. ^! t" v$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ O( f+ c- T$ s$ C! D
Ubuntu/Debian/ U; H Q" a8 a9 A
}9 L- o" M1 r$ @ y2 m
6 I5 V' P! I: @5 e
$ 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, }1 h: l, r- a; {9 w7 |% [0 Z" R
编译安装ModSecurity
0 {& k6 E9 [0 p( Z7 w$ dNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ e. V, f1 E9 `: g6 K3 S8 Y! X: A' E- j5 `6 \4 t1 b8 m5 r; I
' N1 [; }7 h# S+ R
方法一:编译为Nginx静态模块
' ^1 [( C1 L9 V9 R/ e
8 I! E9 Z( r3 {: y7 {$ f0 k+ J1 q9 O
编译为独立模块(modsecurity-2.9.1)
. q7 F% H) G0 O' b. E! ?# V$ tar xzvf modsecurity-2.9.1.tar.gz: R4 y8 g& Q! u' I! ~
$ cd modsecurity-2.9.1/
- r3 t" W( c0 }) X$ ./autogen.sh
# ^8 \* ]' N3 W/ d0 q G$ k9 _$ ./configure --enable-standalone-module --disable-mlogc: J6 I1 w6 Y, O4 E9 p, o
$ make
5 k+ a# [- q. R8 a% B8 l- ?2 L编译安装Nginx并添加ModSecurity模块
; H" W. D5 S* O, c$ tar xzvf nginx-1.9.2.tar.gz
9 S3 o* S1 u: v N, t' n# ^$ cd nginx-1.9.2& z+ m) g) {% x0 e# N b1 ^
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: L1 z% m- a( y9 O( p4 _6 I$ make && make install. @1 O3 I) J/ U: D% h2 R* P) ~
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; {3 S& | t9 P: Y' ^9 E+ f+ m. x w5 C! w
; H0 i _& W) ~$ e3 l编译LibModSecurity(modsecurity-3.0)
5 G: F+ ?6 `5 H9 ], Y) ]$ cd /root( l; _2 E6 X m! N- L# k
$ git clone https://github.com/SpiderLabs/ModSecurity
6 v2 [: ^) ~; J$ cd ModSecurity
) s1 u: [7 c3 O3 u$ `4 v$ git checkout -b v3/master origin/v3/master
, o1 a; a9 F( |" o$ sh build.sh7 o: @% L$ M( M) ^8 k! q6 x4 U* P
$ git submodule init
' c& ^- c* b/ a7 k$ git submodule update
% |$ c. ~2 C1 a' ~# t9 q$ ./configure! M0 a1 [/ [( v7 J+ j: i: A
$ make
; x+ R4 S6 @6 L) g" M$ make install4 l4 e# D1 q' p7 I) ^- J
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& c4 ^" i2 c1 i1 {( ]& Z3 R/ c6 \7 s
' {" \, M% F* h6 P
$ ls /usr/local/modsecurity/lib
; m4 c: M# \% Z$ X( [( \; o! ]libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0( g, k% F0 i2 W
编译安装Nginx并添加ModSecurity-Nginx Connector模块
4 T% i/ l8 r1 H6 K& h使用ModSecurity-Nginx模块来连接LibModSecurity
3 f) }$ ?) N3 U M( O
6 J" p& z4 c; z: U' L1 P7 Q- v
2 N9 u/ |; h# z$ cd /root; A: B' u; w- ]$ G8 S! m0 J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx N! r) a, R! R) ]2 D; x7 Z& G" g
$ tar xzvf nginx-1.9.2.tar.gz
0 p4 B G* V ]3 e( C$ cd nginx-1.9.2
- X! Y+ h' i- X: b$ ./configure --add-module=/root/modsecurity-nginx
9 ^3 |" Y1 N; J# a+ r: B4 [$ make
6 d9 `# G; f$ @) @$ make && make install
8 k, J$ Y3 U# z! S. a/ o9 b, g添加OWASP规则
& Y" `& m9 P, d4 d) j0 W% _ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。% U% u& R! T5 u9 }# R! e6 U+ K
|7 ?" ?1 R$ G$ p; d
o2 P1 j4 w. @; p" @6 \
下载OWASP规则并生成配置文件
/ S2 V: t$ f: t$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git1 T# k$ a, {) I. u3 l" k; l4 X8 J
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
$ P& O' q: G7 Q" z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 P* ^4 [# A6 Z: R$ cp crs-setup.conf.example crs-setup.conf i, [/ W9 J" j1 C1 J
配置OWASP规则7 y7 u/ F V) r% {4 M( x% l
编辑crs-setup.conf文件
1 u! H t; `( P, @/ y9 N
( s' [( k% h9 n
% _) A- ]) Z5 W' M! p. ?2 h# d$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf9 O s& i& u$ [/ h/ E/ Y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* U x+ M8 X1 e
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ h" \, p- ~; Q- M/ q
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. h9 y& H$ c5 L, X- v
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
C1 Z( X8 b& j/ \2 a) T/ M9 t* i+ }/ m7 r9 I; {; o' L
' V8 R5 S+ g1 W
启用ModSecurity模块和CRS规则
& u- |( R) H% E! ]) r R复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 v' ]5 p8 p* ]) o0 w: w# x
3 f: \0 \. \! n/ u1 k9 N( E" _: R h9 J0 k* X* L9 ~
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' [: L1 T# {6 k
- m! B& C' Y' l/ {& W! h
( U" ^, L& m" t i% s9 K$ U$ cd /root/modsecurity-2.9.1/! C$ t1 I: b6 ?' W
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
0 W" {1 | B, ~6 a1 L; L: {$ cp unicode.mapping /usr/local/nginx/conf/
! N' j& J8 z+ m/ I将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。/ A* N; q" v' s2 x% e1 r
7 ~- d' a( m! K! d3 n+ S5 l
" k$ Q9 w" A: \! y8 J7 G$ w8 K$ vim /usr/local/nginx/conf/modsecurity.conf- D: I, o3 ?7 |- t6 d& m
SecRuleEngine On
2 i* F# ]0 g' |2 d9 L, |ModSecurity中几个常用配置说明:2 p( I+ u) P, I: u: P7 e: f( Y7 s
& o5 ?5 G; W0 T
" z, M6 z& J' X1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。0 R. V0 o! }* H N( W
2 ~% g3 ?1 B1 l
2 W( J$ t5 G& q2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。9 m: S5 E. E4 T$ A0 @
1 l5 P$ d2 Y7 e2 r
% t9 V! u2 W; V; |6 {# J3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
1 l% I: j: `. T+ O! ? T4 U1 S
' |. d1 _; N% {2 X, B1 q9 s" S" ^# E$ d+ W" K* p
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
$ z$ j/ _$ G1 v, ~; s# ]. @9 s
- j# V K# U4 h. p& B& [" C1 ~9 d" Q( R8 k' m6 R3 C% y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 \ ~ @. M9 s$ t) j% Y( ^
' ]! x4 X+ B0 |
1 v& n+ y+ P; p/ f M7 W, H: C+ D3.x版本CRS0 A, @) d' N4 x
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ n: \& _ S: ]* @) {# 生成例外排除请求的配置文件
$ g7 x, ^" l- h6 z9 ?7 H# W$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf6 c, O8 _/ b& l
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 e7 j) _# X# K% I- M0 I- S* g
$ cp rules/*.data /usr/local/nginx/conf$ N) S- i1 N' x6 M% |* H* b: a- z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。; O! M. {7 T" U6 Q; Q& M
$ D$ J: ]0 a+ v
& B' s8 A7 ~7 E7 q: `2 w1 j$ vim /usr/local/nginx/conf/modsec_includes.conf) S7 @* w& c3 y$ C5 C
4 J7 J3 {- A1 r7 {* ~
[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 7 S4 r* D0 A3 d# _
3 k8 I$ u# Z8 K' h- \) f3 Z1 B/ H V
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ F; `) B: m/ [% V
0 T/ U9 x; s S
0 G: |5 \3 u6 C/ R配置Nginx支持Modsecurity0 O0 C" `' i) h$ ?3 _' C
启用Modsecurity
( {2 `( Y/ z& F$ G使用静态模块加载的配置方法
6 v# y. W. {( @# v; V* I6 f$ Z在需要启用Modsecurity的主机的location下面加入下面两行即可:, e0 d0 L& p3 y
- ~1 [4 Z4 U6 |+ Z/ v1 x! ~4 ~ r' @; x. U5 e& ]% X, e
ModSecurityEnabled on;
; }5 P! {5 a4 o$ o2 b/ \ModSecurityConfig modsec_includes.conf;+ h( d# J& j( R' M5 p, y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; ?9 e0 W( |- l4 m: d& J. K: [! `2 H: V! F$ D) D/ q
8 B+ D6 }, ~7 U/ L# ^2 ~! x: u
$ vim /usr/local/nginx/conf/nginx.conf, ~ r* n2 `9 s5 k1 {4 N5 N' [
3 g* L5 {+ A# T3 n
( l9 B/ ?. x1 p' E V$ X$ aserver {2 z; Y# u! |$ b6 L, R6 o
listen 80;: U7 L" ^- T$ j. S" O( Q
server_name example.com;1 Z+ b4 t9 [; a
5 }% {9 j4 z4 p# j" Z3 y$ ^ s$ v& \6 w9 A
location / {4 r( S( B/ i2 d$ G
ModSecurityEnabled on;
/ t0 i) Z9 x6 v ModSecurityConfig modsec_includes.conf;/ N0 Q" s- F; C; b- P- b5 A2 I
root html;( ?) _* {2 y& S0 s& j. A3 r
index index.html index.htm;
3 S0 Y. O! N c# C) j. m }
$ ]# C- z$ K" e5 ?}
5 ~8 p/ z$ E8 V5 E: B7 o1 Z使用动态模块加载的配置方法
. A4 H# s' r$ Q0 D在需要启用Modsecurity的主机的location下面加入下面两行即可:3 [+ X) f0 Q4 ?; b7 w% f S
0 t$ o$ W: ^) N5 |( t' e
* K' _9 }. }. ~2 ymodsecurity on;
) ]5 `" D" I6 l7 a) g1 I& j! P1 J% f2 umodsecurity_rules_file modsec_includes.conf;5 X; J0 R0 R( }. d1 h. L3 M
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。 H* `! W* O& T. ~8 O7 U7 N
7 o+ j$ v( J% R1 @* L( h8 C( e
+ B' M! D0 }& Q. |
$ vim /usr/local/nginx/conf/nginx.conf
+ H7 n. V& u3 f& d9 I. k
5 |. P+ @# a( A' ^$ y' d! q+ e
' R5 c! [( u% C, Q, ~server {
0 |: O. F6 E7 i2 d% Q listen 80;
4 w& |: J9 K3 ]! M8 v% M server_name localhost mike.hi-linux.com;9 w7 h( J% t& q: I E
access_log /var/log/nginx/yourdomain.log;
2 E- E- S3 k# ~9 G
3 ~! k1 s2 J/ f5 ~0 c P) ]* s! ~/ t2 h1 Y1 N* S1 O7 v( ]
location / {5 b9 i" \" I9 c" M
; x5 D c& k' R' M2 D4 {# I+ ]+ q% p5 ~/ b7 l
modsecurity on;* m4 R! b+ }7 ]3 `
modsecurity_rules_file modsec_includes.conf;% S) K; Y) @1 |# l2 ]. Z
root html;
& g9 M6 L" ?0 }- w5 J' L( D' |+ d index index.html index.htm;
! o4 `( I: [( i4 ~* ^4 ]}
- z1 Z- {3 U [: i* w# _! p}5 B8 S; E8 b2 ?( ]% @) n. h' m- Z
验证Nginx配置文件
1 {/ `7 o& Y$ r6 d! C4 ]$ /usr/local/nginx/sbin/nginx -t
6 u9 {, t3 a4 J/ M# ~4 Qnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok; h2 f" v( Z5 r- j F3 \0 M- ~
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
( f) K6 |; \ `* m启动Nginx5 P) f/ k3 j. O" q
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf) {9 I# R: @- g& a
8 r/ h. }! |1 e5 @/ U! B+ y测试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能够战胜更多复杂/未知/混淆的攻击模式。 4 Q& ?# s% E" \% w0 M5 f) }% n& A6 A
|