ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。1 S& G% m: T' p0 M4 M
* Y" E* W/ `3 v& h+ x% ?6 Q, x1 s. v8 N% A- T* B! T
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; i7 P6 u7 z$ K- Q% F# w
7 E- b; w, h8 g; i) J3 g7 n; f
什么是ModSecurity9 y8 d' p) [8 K8 G7 T: s9 y; G; }
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。0 Y. R% y" r! k
3 a* a% K8 ?6 V4 E* L5 R: m4 K" \5 |0 q* t+ W
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
, { `& _2 D4 U& _$ p$ J- U9 `+ J( X! P5 u8 u
7 g7 ^! G% c- A" {- P' {ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( ?1 d) b/ W/ S2 \/ i( x- E1 \1 k
$ t8 a- @) }; b2 h* u! Q- j+ {: ]4 _8 b" o
官网: https://www.modsecurity.org/; r( V3 q: F. [. y# h
, [$ p, G0 Y1 L
+ C; [! @! Y9 G' s4 Y什么是OWASP CRS5 j: y4 M J/ f) E0 A
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
' S1 u" U: D" N. U0 i. ?: f3 q& g1 w" b( W- O. C+ m: P& I
5 T9 J V5 Q0 S% s0 R: Q" |
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。' n1 f. s: g# e. ` ]6 |
4 s! P4 H9 G0 {# c; k1 p$ ?3 I* W+ S9 e3 p8 [0 S9 q8 D6 v( U
HTTP Protection(HTTP防御)
: a; @9 t# K c8 Q U- g/ ~HTTP协议和本地定义使用的detectsviolations策略。" o" e) g# r! J7 C8 F5 D
3 W" }- ] c. x' Y# f$ |4 k/ R, g
e! T5 F$ Y$ n7 g2 K- xReal-time Blacklist Lookups(实时黑名单查询)1 }" M, _) w* E; @; J
利用第三方IP名单。
% R6 B- ]! t$ D% }3 x# b: z
3 t; ]$ K, i. f+ Q- q- F' P
" m! L: }- W% {HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ Z D7 h" v6 t
防御HTTP的洪水攻击和HTTP Dos攻击。/ ^ G: v E) z% s! c
8 T0 Y0 Q c, m- V! F# s
/ {6 s2 {& O* _; k& ?Common Web Attacks Protection(常见的Web攻击防护)
& T1 |+ m7 O4 }: m7 l4 R" {检测常见的Web应用程序的安全攻击。/ H. ^8 j( c( B% {# W4 z, B9 V8 G3 a
' }5 X5 J4 U3 N* |# N* g% N
W# X3 M6 ?) A. z* ?5 cAutomation Detection(自动化检测)
6 W( T" P8 u% a2 H检测机器人,爬虫,扫描仪和其他表面恶意活动。& g1 g, ]/ c- y
9 v4 g, G8 n. X) n1 j& I8 B$ v0 p e- N& \5 }% D4 o$ H" g
Integration with AV Scanning for File Uploads(文件上传防病毒扫描). S T. K; ^9 }' G2 _
检测通过Web应用程序上传的恶意文件。$ I/ y. ? A/ {) C2 U) V
4 T3 ]" z' |: e* G$ T4 g
8 \( U4 |* a% m8 X5 M$ UTracking Sensitive Data(跟踪敏感数据)8 } u4 [5 B- N a$ @
信用卡通道的使用,并阻止泄漏。
! ?3 O% N# U8 [9 @3 b ~5 z* J$ u3 z9 d& a: {( p( e
* s' u/ Y$ F/ h5 dTrojan Protection(木马防护)
% j) [ z9 B8 \) E1 q检测访问木马。- P* `& |/ V4 E' L
6 T7 \$ {- X' I+ i; I1 A' P& ?8 E0 L6 X; s" P$ o
Identification of Application Defects(应用程序缺陷的鉴定)8 @$ D& a8 m6 i" `; q3 d: |
检测应用程序的错误配置警报。$ ?" C; ~( z! h! }% j; i
r. D( Q* L/ ^" v* X* ]* D8 E
5 x g# [; m, f X6 Z" }, V
Error Detection and Hiding(错误检测和隐藏)1 V, ?# n' f6 A `
检测伪装服务器发送错误消息。
# {1 b% }" f% Q* S! U: D" \- u* p; _- B+ r! g" j
( V2 L- K$ b8 I( y安装ModSecurity
! `9 W5 w! M8 c/ X# {软件基础环境准备
% j+ P/ f- \! l( q+ d. e: _下载对应软件包2 o V. r! ^, w4 m+ o5 R% W
$ cd /root) X- ? c( H1 [# J( V* B' E5 m. j
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
2 {; U& f8 |: @1 B/ v5 A$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
( M1 u% r4 s9 q+ o. C+ G安装Nginx和ModSecurity依赖包
! j% G% t: o) q+ TCentos/RHEL
0 T1 V2 s, y: E6 S9 l2 N4 n, L# Z
6 ?; ?: I: G& b: J
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
' Q x: i! U% B' V. M! t+ o& oUbuntu/Debian; {2 u/ P8 a. L: e5 P
# v0 Y3 w; A4 `4 O. \: t5 p e* C% w6 [! S
$ 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
# l D3 v1 D2 v编译安装ModSecurity6 C8 D: p! X0 ~' \ _1 y
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
$ T$ z' Y& a+ _! s3 I; V9 V+ T+ _" x. _8 W; I3 y9 ?' v& U
; R. G3 R7 I. ~# k2 H方法一:编译为Nginx静态模块
0 n9 a% `. @1 I5 S9 S7 G. r4 n1 r1 J& D5 T" X2 n3 l
* r9 i4 D& h" }/ B* d
编译为独立模块(modsecurity-2.9.1)
6 d! a# i0 R. {. G+ r5 u; i- r$ tar xzvf modsecurity-2.9.1.tar.gz
" ]/ X' t2 _' Z5 `9 s$ cd modsecurity-2.9.1/: d- V" t! K+ s
$ ./autogen.sh* I$ h7 K: }% d
$ ./configure --enable-standalone-module --disable-mlogc
' J' u" a/ L. t: r$ make
& k# Y6 @9 V+ z% e: h( ^编译安装Nginx并添加ModSecurity模块% H" p3 f* a0 }+ r
$ tar xzvf nginx-1.9.2.tar.gz
- O5 x7 `! Q* E: s9 ?! t& m. o8 H$ cd nginx-1.9.2
3 j7 |! S- b6 P2 V$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
- |/ x3 A7 o) I+ E5 z* |/ \! D9 k2 |6 x$ make && make install2 ]2 V: m/ i: F
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块9 C- @# N a& Q$ K- j% c8 k
9 n4 P8 g- x1 Q* t8 a7 [" n
3 L3 ~$ }- F. Y6 k7 {% u2 ^6 t$ ]
编译LibModSecurity(modsecurity-3.0)+ w/ i% Q" t+ x5 s% ?4 K9 ^! Q2 A; G. n
$ cd /root
2 A" I% H6 k% W3 P& H$ git clone https://github.com/SpiderLabs/ModSecurity
- b% Q6 e+ r5 ?9 o. m$ cd ModSecurity/ ~( t: e+ i& F, T
$ git checkout -b v3/master origin/v3/master# v8 \5 f3 a2 i. z( J
$ sh build.sh. G0 M; U g( }6 ?, _1 [
$ git submodule init9 W/ p# q6 \3 U7 ~0 y
$ git submodule update
5 S3 ~1 M' t, d3 K+ E$ ./configure' r% w' ]( Z& N+ H; c/ c
$ make
7 a" L( g! F. r( Y' f- R4 V$ make install' I6 L% Y" ~! w* C/ [" [+ j& d
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。, z; |: ~% v- h2 H n1 t0 w
2 N# l3 f( n; l1 Z& v& m: s" i) K* H' ] o! h: T: V# K
$ ls /usr/local/modsecurity/lib
! [9 q( G- i, elibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
/ [' t, d! @7 e1 g- \. z: Y$ b' Q' U: G编译安装Nginx并添加ModSecurity-Nginx Connector模块. y4 S3 a! @% p
使用ModSecurity-Nginx模块来连接LibModSecurity6 Y7 ^8 r \* t, P% D
6 Y: t0 V3 s. ?% p4 {# q
& J( {, A9 W9 C6 b, c$ cd /root5 f$ {$ b" @6 q. K
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
1 G( ~7 ?! _; r. H$ tar xzvf nginx-1.9.2.tar.gz
% E) L6 \8 o' p a A6 c$ cd nginx-1.9.2. u6 h9 y) f& c4 U z
$ ./configure --add-module=/root/modsecurity-nginx, X0 m; i: l! n I; M1 r+ b7 A
$ make
; l% M/ c0 a' O6 X- q$ make && make install$ \; X+ d2 `9 K* E1 l$ F
添加OWASP规则9 ?5 W& F [) M2 {3 e/ e- U$ L, j
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& g4 C5 |5 D. v9 T& k/ d! M9 ^% `) M* }# x0 B
- h. u) e6 m* J6 `; L) T3 q* `
下载OWASP规则并生成配置文件
4 Z0 {- E/ L3 M( K8 n" ?$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
' Q A* z3 K5 u3 U. c2 R$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' e! V3 I9 s8 o, |
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 E! b( s. E* A8 q
$ cp crs-setup.conf.example crs-setup.conf
# n! j ?; j/ W3 d7 d& Z& m配置OWASP规则
* W- ]7 r: l: j, F编辑crs-setup.conf文件
3 l2 t. a* A* U. d( j+ [ l; @. A6 |% K5 T7 O2 l1 P
- l5 w: q$ R1 y) a7 Q3 K
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- Z* O9 K: r/ u( p- H
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
% E% k2 x: Y" |) e$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf, L* C) w" _4 y4 `
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
% ?6 c0 _3 f& |默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
, ?! i7 W& {4 R2 X0 ]) S9 ]' V j, Q, Z- g. R
& [# D2 i7 O G6 u' v3 M! D启用ModSecurity模块和CRS规则, I; g2 Z; q- @; P# O" D
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, R8 O2 j! V" t' s" _1 D. z% `6 J3 D7 \2 I. B$ e* L
! x3 P. C/ K0 Q Z- b& `, Dmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
, g( A/ _. S. h% ^, @( x* z2 \% M; e8 P( M0 Y5 g8 w B: Z5 g- K" N& R; z7 }( p
2 U6 F: _. P; |5 B& Y$ cd /root/modsecurity-2.9.1/
8 P$ @/ a* V& x2 `1 ?+ ?4 v$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
$ A& A: M$ l5 x1 ~/ G u$ cp unicode.mapping /usr/local/nginx/conf/! b$ d* P* u- g( K; N9 v
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。. Q- [% Z8 {7 [; S
0 N8 M4 W& d" v
; [2 u1 U8 `5 _( G; h5 k2 z) Y% a$ vim /usr/local/nginx/conf/modsecurity.conf
# \' ]; Y7 t+ J" `0 M9 pSecRuleEngine On( X7 u6 U! }: m9 B& U z
ModSecurity中几个常用配置说明:
. h9 E$ x1 N2 X; Z9 y+ Y7 H6 ]/ E* m; `+ k+ e
/ A, b2 t, G+ Q+ Y$ d1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& X' ?7 j1 Q/ ~8 H. P
0 E( o/ r4 I; p$ r) A$ g$ ]$ M& W$ Z) @% s& _) Y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。9 K3 ?. h5 \8 q1 b% ?' `3 B
8 |; Z! {+ _6 I0 A* g8 T, d- I7 p9 x+ q4 R
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。) C% c: F: d- R3 [, H
# T3 W% ] G8 h+ L O9 S+ e$ b/ C5 f g) D1 ~, o1 i3 G X
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
5 p7 [' Z y. r& g" V$ v* w" `" V& L/ Z! _% o$ |% @
- V& J2 i3 |7 A+ p& Z在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 p3 v0 b9 e- P* n8 s5 i
/ u2 T, {2 y0 k! T/ V" x
) v$ I5 r- h. g3.x版本CRS W' k1 l) F* Z7 e- ?
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. ]5 l% k! k7 a0 }3 T
# 生成例外排除请求的配置文件# C' N& d/ q9 N: B" z2 f
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" J, k4 M" P$ C8 v$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf5 w0 \) z L8 l# e o+ {- `( v
$ cp rules/*.data /usr/local/nginx/conf* e j$ m, t. N
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 z. T/ k2 ^. @8 {
* q6 `4 C1 ~2 G1 S! Y. ~' t$ h2 O6 K- g @6 w, g
$ vim /usr/local/nginx/conf/modsec_includes.conf5 Y# ^" W! h% j$ `, Y
, N5 R/ K2 R7 k B0 \* o" ?( X
[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
2 k ~* }- h0 g% \. b% Y/ v6 `. F4 O- F7 L8 r$ z2 T8 _) M# \
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。& x$ y9 a- O& A+ q, J
2 Z+ h( @+ ~, X I* c1 g# c
* s7 I) M G, V5 E配置Nginx支持Modsecurity
8 y7 V- a0 p. |; p/ h1 L启用Modsecurity
. \; v0 n2 t. Q7 u& g; p使用静态模块加载的配置方法2 d) M! V7 Z# R+ ^
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: |# M% W5 X: d! P% `1 O# B) n4 N, i" |: c% w% t
/ I a+ N0 P G# r$ R+ y
ModSecurityEnabled on;
% L( a" g* x( z! e8 G: n' _: mModSecurityConfig modsec_includes.conf;
1 Q# B' W0 q5 g1 F, L P修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ B: K+ G. T, h9 J; h% T* ~8 i; Q4 Z) o$ n$ l+ v
$ f) F+ d G! ]" D! h7 z$ vim /usr/local/nginx/conf/nginx.conf2 [* J9 n. k$ }
, t$ H6 G& o q$ B1 l4 @5 H: Q
& Y- `* H+ {; r( N2 e) B$ Nserver {4 Z0 O0 I8 R5 \$ ~' P
listen 80;
) I; f& L$ S7 D( s server_name example.com;' I1 L* H9 Y. r& Y: d* [7 h
; Q3 o) R2 f1 l8 X- k y8 }
2 K/ `5 S% P% ^) X1 R; L location / {
4 Z" R. U3 [* v/ `4 ~# O2 D6 K ModSecurityEnabled on;) y! A4 h3 \4 z1 F8 C
ModSecurityConfig modsec_includes.conf;
; w* ~( ^, t! K, R root html;" ^% s- |! @+ l0 {3 U
index index.html index.htm;
: N2 |4 @) W# P; }5 m( d }& p; Q' t* g' }- f7 X
}
1 _. N1 P1 Q- m* ^" ?- z, d使用动态模块加载的配置方法; L" ^2 b8 W, E& k# v. k
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ K) f" S5 R! B( { p/ R& A- H/ x2 |" \. x( s
s: l/ D6 I7 n S9 ^
modsecurity on;
) P3 O1 O9 Z2 Z9 o6 amodsecurity_rules_file modsec_includes.conf;) W% N- W+ a5 ~( [' v. Y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% \; R. J5 ?: A" r
7 |0 ]0 o5 ~! \
) \0 \' H: D$ Q, [2 ?$ vim /usr/local/nginx/conf/nginx.conf
" g! K0 w% y8 D, u9 S9 D) G1 y% _; L9 z8 q1 `3 a2 r. z' m/ T
' D; Y0 D+ r8 q" }+ e( C$ mserver {
1 Z: |: Z* V9 D! i- K$ v listen 80;
# O% [3 o0 Z0 F6 w server_name localhost mike.hi-linux.com;
4 Y6 S; Y( z, H- w8 x access_log /var/log/nginx/yourdomain.log;. v& c% p7 c$ H7 D6 h1 G
7 m" X* \5 Z3 c/ W g
* }& G% E- i9 p- v: k- W; s+ a, w
location / {/ R. X% ?9 l; o4 }. m9 p0 q: P
& i6 t( j$ V0 {2 i& J
# f4 Q( s# |2 U7 E* N: d; ? modsecurity on;
, \4 [# a# _* u5 y( h- ]3 I% t modsecurity_rules_file modsec_includes.conf;8 o; Y5 E v6 ?$ d s$ J
root html;
/ ~8 |) ]/ E+ |1 j4 }, D2 _ index index.html index.htm;
! y6 V1 E; z# a}
# _& H G+ D2 v/ K$ X}
" B( D' e* S3 J. l验证Nginx配置文件
. V9 T3 n& l9 R7 A; C$ /usr/local/nginx/sbin/nginx -t
$ x. [ g5 w; z- Y3 z9 R: Xnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok ~. _3 M3 v( u, [& p6 ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful( A' x4 b2 r, E# t( p
启动Nginx
; @ y; u2 j; o! U/ K$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
( s9 R5 m! S2 `6 L, M
0 @9 z4 `5 U8 [- Q8 I+ v! q测试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能够战胜更多复杂/未知/混淆的攻击模式。 S) G2 w4 c, p" M
|