ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
C2 l& K5 d- T+ n. N J5 p0 t" `& C1 s1 X
9 e" f% L: |" s' d在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 F3 _6 T; V: Y
, V. z3 F! c. l( \
2 `1 ], a$ @4 P
什么是ModSecurity
- u; E& |6 B* D- C: a" BModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
4 ^0 a3 {* y1 K$ Q6 u/ r; z6 Y9 N: M% m5 _' H" N a' @6 a3 Q5 J
& {$ h: l/ Z6 \0 t4 |) i
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 S( V' ~2 k1 @ Y: r3 e
5 L! |: U' [- Z1 N$ J' d0 P3 g
5 `7 F/ r# }1 gModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
2 v9 |# u% _; P' d% u6 f' C
$ R# u3 V( P |6 j- o* i! V( |' w8 f& I) w. c
官网: https://www.modsecurity.org/ g' N* G+ t1 E1 B/ Z
z9 ?. }( K% L8 j
$ n' }. D( ]3 P& c, k4 r6 r
什么是OWASP CRS# h0 Y4 k3 x2 D2 R% o9 W
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
, c: ~+ b. b$ C/ ^- w" D1 g, ^ C/ h5 R( t; `
3 Q( B N' P+ ^" H; VModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。6 D `' E( J# Z9 A
0 ]; g p: |% f! G9 w- x
! g1 w7 `! M1 B2 a' J, eHTTP Protection(HTTP防御)$ L2 X2 {8 P2 H, v) a* N& z" M9 h
HTTP协议和本地定义使用的detectsviolations策略。# m' v3 z% c" M- h7 B0 x7 C
! s4 H2 M7 g* |8 p1 ~% T+ z% ~* }3 F$ c+ c
Real-time Blacklist Lookups(实时黑名单查询)# H* w! w6 u' l3 x/ ^, j, V5 D! U
利用第三方IP名单。+ b, a3 s/ O8 v
' d& i1 c. ~" v/ B: H
( x2 Q. L' g) v. d
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" K B" k/ u4 n: c2 `" p4 n防御HTTP的洪水攻击和HTTP Dos攻击。% {8 K: O" Y) L* Q
* p* e/ z" n/ C% c
2 X& Q) T- F% L$ S0 p6 m! a) P* V0 H
Common Web Attacks Protection(常见的Web攻击防护)
/ Z) Y# E! V& [4 O& z% |0 L; M检测常见的Web应用程序的安全攻击。
& r5 y4 s$ K# |- @. U3 Q/ ]. }" r S: {3 `0 b
t! O1 v7 e ?0 N# b! |, BAutomation Detection(自动化检测)
" f3 J* F# J; ~检测机器人,爬虫,扫描仪和其他表面恶意活动。) V2 Z4 N1 C3 @$ k
/ x0 P: h* ?, N1 N2 f U
4 d9 S3 ^% n" _- Q4 G
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% C; [+ U+ x7 H' D检测通过Web应用程序上传的恶意文件。
) K5 w! B$ m, {/ n2 f6 X2 x4 G
3 B! {5 W: _: X/ @! N. l# [) [! H
8 t2 c6 U/ P+ K8 jTracking Sensitive Data(跟踪敏感数据)
$ g% S9 w( j" R9 G% j信用卡通道的使用,并阻止泄漏。* }( W9 l' J2 u
& \' [% {- D" y* W9 @* k
' C" h8 {( I7 J0 U6 _# cTrojan Protection(木马防护)* _+ q) c/ m5 [2 b
检测访问木马。2 S3 C9 s* ~8 ^( [2 C( h2 A
0 f/ _& x# J0 W ~# [6 V9 r; `$ V# B( c5 v( r- t
Identification of Application Defects(应用程序缺陷的鉴定)
. h# [/ \* H6 b检测应用程序的错误配置警报。( H% [. j6 E6 _& T, c
# B1 F9 s& ~ {6 K3 X/ ~/ s
' A# ~9 [" i0 y9 ^+ L( ~+ \9 Y; f
Error Detection and Hiding(错误检测和隐藏)
`7 ~. n, u% \% O检测伪装服务器发送错误消息。
9 r0 c; D' `0 S- U3 K
" y, Z, `9 c x8 N
# ]% V; B9 \' z A q m2 A5 Z$ y安装ModSecurity% ?6 i( @! z J/ U o
软件基础环境准备+ s$ I1 P. ~5 z- a, o1 i
下载对应软件包
, r- R3 T! n) Z0 J# V$ }) y7 [+ k x$ cd /root* I) x. P6 b q: t. x) ^
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 w5 ?0 u! G& \5 Z. y9 _& b$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
' z+ R1 |% J! f% F9 F4 C安装Nginx和ModSecurity依赖包4 Q' S4 a' d( d4 b
Centos/RHEL
# U u; s* D, G3 c/ d; k5 ^3 Y1 ?& O' }" P4 P; v$ u
* e) K" [7 O8 A7 n# f
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
3 S$ T* y! @8 [7 dUbuntu/Debian! I u: t S* g4 e
+ }' Z4 v) v: P# A K' x1 x8 E
, r5 ~' C$ p$ U) ?) Z/ o3 H3 T
$ 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
8 O& Q* M0 k# a g编译安装ModSecurity$ }- Q& T7 Y: Z$ n1 X9 f! \5 ^
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
|5 n" h# |- v6 X- M" _5 r/ q* n* y3 G: Q
1 Q7 W: S& `& j! j4 R方法一:编译为Nginx静态模块
. n- e4 U# a5 h/ N" V8 D6 f! ~* B d; W- w
+ O7 V( T1 m c2 v0 i4 q! x3 o
编译为独立模块(modsecurity-2.9.1) E* o% E1 |! p. J3 S
$ tar xzvf modsecurity-2.9.1.tar.gz
' r5 ?5 }( G- w) m) ^$ cd modsecurity-2.9.1/. w5 _, i: S- ?0 w8 V; V9 m
$ ./autogen.sh
3 f [# X, F/ _* m/ Z& l# @6 u) p$ ./configure --enable-standalone-module --disable-mlogc# @+ r- y. e! J
$ make
1 [9 C ^: `2 m1 N编译安装Nginx并添加ModSecurity模块
9 K( e0 o( v& ?& l: p1 i( L$ tar xzvf nginx-1.9.2.tar.gz
2 G" f2 V. l; C$ cd nginx-1.9.2$ _" m' z( i5 W' }9 o+ T: K
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
s9 y" Y: Y. h' ^- t$ make && make install9 q0 Z& C/ R$ {- P
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块" J8 L" v9 C2 i4 H4 [0 Q9 r
" M& F9 z) }4 E8 p- \/ a. [
/ b, @: _* ^! ^, X
编译LibModSecurity(modsecurity-3.0)
+ ]5 U. q- G! \7 z$ cd /root2 s( q1 W& {+ Q1 v5 Y' i5 {
$ git clone https://github.com/SpiderLabs/ModSecurity3 p: J# }" Y% o0 ~2 e) x2 Z
$ cd ModSecurity
1 T0 p9 o. \% {$ V# Q9 N* g0 I l$ git checkout -b v3/master origin/v3/master
* p0 }) Q/ U1 ?4 S" c8 L$ sh build.sh
5 a. L$ @9 ~9 c$ git submodule init2 G/ w% M. J. U5 n4 o$ O
$ git submodule update
8 I* S0 k, U4 v+ z% B, u+ W- o$ ./configure
% {- q2 }4 b) ^8 ]8 C$ make/ y! w z' Q# `4 D s" O6 f
$ make install8 p$ h8 H, f3 ^1 x4 O$ D
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。; C, x6 s/ s1 P3 h2 k
1 R% l8 Q4 U6 R- E% z% B7 e4 w* M% @" V( C
$ ls /usr/local/modsecurity/lib
: R3 a1 N" B0 n7 ]6 }libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.09 T; G1 u; f: E+ @* k8 A
编译安装Nginx并添加ModSecurity-Nginx Connector模块
`( q3 t2 u3 d' [使用ModSecurity-Nginx模块来连接LibModSecurity
. S: Q0 R2 l6 Y- b8 J
1 T8 u) z' M1 X6 i2 s6 [$ o" I- q1 U5 K ^8 b; U4 @) v( b9 p0 N
$ cd /root4 ^- R: H1 J8 y
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
& Q' ^7 \& R1 @0 ]( x: c$ tar xzvf nginx-1.9.2.tar.gz
8 V+ E" h) D3 P% A1 p0 P$ cd nginx-1.9.2; L3 x( b+ M3 X% U% ~
$ ./configure --add-module=/root/modsecurity-nginx
s( Z3 G4 N' O$ H$ make
- C& O7 @: C6 w: R. v5 O$ c% T& _# _$ make && make install
; K+ S( G, h1 A$ ~, H添加OWASP规则# Y, {1 g. E6 d& A
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。7 G4 T; x; b. |: r }& M
( Q! K1 B$ N8 j! G/ L( E$ C: f9 q6 T9 t8 l' |9 a: H
下载OWASP规则并生成配置文件
: [4 Z6 I3 _3 G& N" ~2 O2 t8 N) W* H$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
3 ^$ d+ _! h* s" ?) ]$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
* Z4 i9 x! o. L6 Q- U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 ]% Q: u, x! u* l
$ cp crs-setup.conf.example crs-setup.conf y0 f! E- h. Y, s4 a! z8 U
配置OWASP规则' t1 p+ t8 r" D4 h- f
编辑crs-setup.conf文件- a [/ f% v7 O8 [6 k. r, ?& S; C
/ k4 c& s# f7 M4 H% o% K/ [( T
, M1 J5 {! k: _8 q, [. P
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf2 V! T& a Y# w+ D9 u- s
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf; J" q- V7 A2 x9 E* ~- e* F
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) @1 s' K; ^( i6 ]1 L2 B% X! L7 l
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf3 H) Q) g5 S W( a _( D
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. S7 ^# p8 t. z# p' ?- k
8 m: K$ b* N+ l7 P
: D( O3 }+ u6 A0 n2 o启用ModSecurity模块和CRS规则. T7 T8 w4 R; v. y S
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ U& p2 U0 [& e. K3 P4 q# F! {; _8 W }2 l! [- ]
: v* O; @* ]5 D) F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 ], `. o+ i6 ^
7 E2 O) x0 w- l1 I! m; x. @
/ ~( A* H6 V; i- }$ cd /root/modsecurity-2.9.1/
8 `& G, i. Q9 S& M$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf % i. {- e" W, e/ z% n) c9 b
$ cp unicode.mapping /usr/local/nginx/conf/
/ w' S4 H$ u, Z5 }1 M将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" G, O# y8 \* V; F5 L* M5 X" d2 A+ Z3 o* W1 J5 V. V9 t7 d* X
1 b$ U0 L2 |4 o
$ vim /usr/local/nginx/conf/modsecurity.conf3 l' w, a# I2 B0 t9 |& y
SecRuleEngine On w+ |1 X) V s
ModSecurity中几个常用配置说明:
* u0 z& u; J5 B* |
/ f6 }/ _9 U- [: Z7 z9 {4 o9 k! l4 D, V
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。4 C3 J8 ?9 u8 I
/ _" N' w" k+ W- f. a
6 m3 l- l: w0 }. k0 S+ D" n% D
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. U$ `# K/ P% ~' g6 T g
( i$ {5 \/ v, x& Q U( I
1 H D7 Q) V- E8 A4 B% y1 {3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。; ^, j! K0 ~( M2 h8 @
4 z& [# M" n. n
! ~# i [7 w$ Q$ D4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( a# \/ K- u% ~6 S1 h1 Z: v. L4 {% D1 k9 N* _
6 r) o4 Q: K! l1 [- W/ W3 h在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- D. L0 J7 x& |0 r# R: L) D1 e1 m% a* h. h6 K' Z
4 m% l; Q2 Y! r4 j
3.x版本CRS
/ D( S8 ^2 Y2 e$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
( P2 b! }! B% ~' f- j1 M5 R; {# 生成例外排除请求的配置文件' N X: G+ I- Q$ Q0 V7 q& s1 r
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf) }+ b0 b# l- A9 d
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' o. |) o# F% v8 c- j
$ cp rules/*.data /usr/local/nginx/conf
& @; N6 _3 N* t- v/ Y0 @为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
r% _2 W5 d7 l& S# q; J& s
& ], L8 e6 o( j4 u4 P8 i) V4 e
( ?' v1 w& c5 r+ r% U$ vim /usr/local/nginx/conf/modsec_includes.conf
( T* M" o8 \( n$ b5 H. A9 N) l0 f0 F
1 y$ L/ U. v e9 J5 H b( 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 ! t2 W! v5 w$ X
: X b p, s* K5 D$ s: Y) K
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。- m s+ K7 p# n: I# c
5 i) t2 V- D" _# v Z
, D- S' N/ v3 h
配置Nginx支持Modsecurity
C4 U3 N; c& m S. h启用Modsecurity
. l# h/ _6 K+ {, z' {0 L: F5 E; `使用静态模块加载的配置方法0 W4 v: Y" d3 X6 x3 f
在需要启用Modsecurity的主机的location下面加入下面两行即可:- D2 u; i2 `) ^
) i' s |( R# O' H- X
% ?' f' v% m. M) h5 i% {) b
ModSecurityEnabled on;
" P. j% V% G1 z, [/ mModSecurityConfig modsec_includes.conf;, k9 d$ z2 |' o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 X' ` d$ A- [
" g$ g! Z$ X' r% J3 I! V
- ^) \( g" v5 C4 W1 Q0 p$ vim /usr/local/nginx/conf/nginx.conf
# ?# J- t" i8 a8 i& C3 h+ W
0 t3 k, @* M2 ?7 O: G. C) {" W( y# R$ |9 E) _
server {
( } S' T2 J: ?- h: i& Y( l5 U5 w$ _( S1 d listen 80;
& f: w2 M: B o/ N4 L! x+ P server_name example.com;
) z2 |) O; z: D# P( d0 S) ]/ K l' i1 p9 g% u7 `
% S0 {( F4 l6 c6 ^* X1 F& O location / {
! z- I1 t4 u( U4 a ModSecurityEnabled on;6 {4 w0 u" ^$ l1 h# O
ModSecurityConfig modsec_includes.conf;
/ {- X) Q6 K% z3 B/ y root html;
0 P$ `" E) ^3 @ index index.html index.htm;
: A+ Q6 V' ~1 ?, d" E# S; m }5 y% d& u# A0 l- K" j
}
7 ]" t, a& ~. |3 Q+ f1 C3 u; A% ^使用动态模块加载的配置方法3 E9 q: Q! C5 K5 @; p* l" C" N- F: o: n, P
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% U% v! N% s Q% [1 m5 i' I0 A; Q; a$ J8 C' ?; ]2 _# c8 X
- l( }+ G2 s0 Umodsecurity on;! i1 l9 D# K% G/ E# ~& q
modsecurity_rules_file modsec_includes.conf;
7 p8 |6 }) |4 W7 K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) D9 S! N* _; c! C8 M1 P: H2 x- }# s" y% c% m, r4 U: _! p
4 t4 u: N+ J+ e: Z+ A$ vim /usr/local/nginx/conf/nginx.conf3 G Y6 j N; {; n2 @
9 t$ B! ?5 u! ^' ?1 Z, C+ _ X# K4 j( A3 P1 X
server {, ]# x6 J6 {: E- R- ], Y
listen 80;
% N7 a% s" r6 M8 n% ^+ Q server_name localhost mike.hi-linux.com;
1 q2 J. X9 C8 R$ u8 x+ O5 p* F access_log /var/log/nginx/yourdomain.log;
& O& H' b6 w9 b$ x# Z0 h& q1 b% L# |3 a. Z+ q
! H% l+ ~2 w m) X0 V0 ^+ H: }
location / {( y8 l$ r: W! B4 F. }
# M0 f- B$ J: S, A. {. b
# M: D B; K0 n' F: H. \3 ~
modsecurity on;
p% j, j3 o Z6 `9 h" p modsecurity_rules_file modsec_includes.conf;4 P. I4 l7 w! e9 W) F* H$ u
root html;
0 {- {; x; X' O D/ G; s index index.html index.htm;
3 N$ C. k* U- k" P}
0 {# X, _( o2 z% A. t0 h1 w}
8 y8 N6 o" t% B. l* |( x# q验证Nginx配置文件
1 \: w/ Z" X& k, S0 P& `3 W$ }+ b$ /usr/local/nginx/sbin/nginx -t
& J+ U1 W+ p( O: `. Mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
5 i, o8 T$ ^# @% U' f2 E. x! hnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' \" Y( Q! `. r7 l& F& Y: U {7 b! w
启动Nginx/ Y# K0 u! F. d1 C
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
" O* V; [2 @1 @2 D1 J( Y: J7 c7 t3 O7 _! G
测试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能够战胜更多复杂/未知/混淆的攻击模式。 # Y# U; G k& K6 a6 [4 m
|