ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! I! k9 y6 f3 a: A; y
3 w# J; b6 @9 P( k0 e$ ?" k
# _" N i: Z7 [; N
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。- E0 M* W+ s+ `2 } v
& Z$ P6 }* c8 v$ D
) b5 P- }7 \# y. z0 n. v; w0 t
什么是ModSecurity' }% j9 U5 Q! w
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。$ ?# n) p. ]- q0 d
% I# f" |) p* J& `# {+ J1 ]: E3 D3 u( c. R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
+ E; E+ k) ]2 |4 j8 r
2 n9 L: `% l Z3 Y) v
& }" c( H6 I6 O! x/ e% @6 L/ IModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
* }6 g. c% O& o8 c% a
* d8 u$ R8 d# s- m
, N( B- R2 U2 m) G9 l' z7 W官网: https://www.modsecurity.org/
) u1 Z6 y J G* x: u* b: x( w0 Z4 q7 ?9 M' G
/ c+ \, n$ E1 R. [什么是OWASP CRS
+ N9 U1 ` E) `; \4 ]; JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# x( z! u3 p; J3 F
4 E4 g! ]; j0 S- v T4 @+ P! w$ j( M2 P) N0 j0 x4 [
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。2 y4 B1 x, V8 h1 V: x
: C [: \1 `, y+ X% g/ c& |
* |! c) V" E) O$ @HTTP Protection(HTTP防御)
! e4 t) _, @2 m! d$ @' R% ^HTTP协议和本地定义使用的detectsviolations策略。0 ]3 U0 m& @! m- E( i( e5 I
2 S7 U3 w0 h& b! `1 N+ r0 r! c- N
D5 c* R& {$ X6 P( ~* vReal-time Blacklist Lookups(实时黑名单查询)
+ R) M; A' v w! I1 h利用第三方IP名单。
/ j, L+ f: ^% A' P( s/ t8 U _) ^9 v- [/ p: W% g# L
4 j. g3 B7 w0 B% r9 }7 I3 s
HTTP Denial of Service Protections(HTTP的拒绝服务保护)$ j4 Q# U9 s. |5 r
防御HTTP的洪水攻击和HTTP Dos攻击。( _# J9 v9 J! E: g0 G5 C8 Z' e
$ \3 k2 H, D' T4 J
8 I: G6 Q' Y2 q' q. D. N5 yCommon Web Attacks Protection(常见的Web攻击防护)! v- q; j# }5 }# S
检测常见的Web应用程序的安全攻击。
+ f [1 e& E: _3 ]; j$ F' w9 d* H, [6 y
( L9 M: E. m) D. RAutomation Detection(自动化检测)
. b, M" |; g: g* B5 X: e9 P检测机器人,爬虫,扫描仪和其他表面恶意活动。8 n. @4 P" R$ x' S& \1 f; K
2 O! z: _( W0 k7 |/ u. |4 N7 W
1 E5 w) e: v8 W2 x- D0 o
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)% R# U2 a* R: ~
检测通过Web应用程序上传的恶意文件。
- X2 y0 K: f% |# \' F6 @5 K- o
3 F* v' h0 m% W' c
4 e) d0 `. i: D, B, V4 `Tracking Sensitive Data(跟踪敏感数据)
& ]% {; I* H3 n信用卡通道的使用,并阻止泄漏。
/ H5 R) l q7 m" N5 k7 ?
( w/ g( H" f1 Y0 ~* @7 @+ @: R
0 { f5 y% Y6 `" t0 D8 s, zTrojan Protection(木马防护)
( S, l& P" u4 }1 T6 d; k3 V检测访问木马。8 a+ V8 R4 v- s0 U# m9 E" j
8 m. c) K6 [7 `
8 Y: R+ e( ?8 k. HIdentification of Application Defects(应用程序缺陷的鉴定)0 l. N& F' y5 U& i2 ]+ Q
检测应用程序的错误配置警报。 Y9 B( ]5 p, X
& u- c6 K( ?: C+ v& S, r2 a z. q8 B: T( Y/ ^$ s) w. U( }
Error Detection and Hiding(错误检测和隐藏)
6 \4 C6 h9 _! ~; V2 l- T检测伪装服务器发送错误消息。 g; x3 D! V# s
$ X6 U, b O( O9 y
4 A; p/ H0 {# Q+ ?2 {$ y# d4 V安装ModSecurity3 V0 T: H- v4 q& | M5 H/ `9 r
软件基础环境准备
4 K: x4 Q- |8 q3 s/ C3 A下载对应软件包
' |8 H5 }" L* ^& G' \) ?$ cd /root' ^; E0 t' |, A B9 l" {2 }7 O- I
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* L7 o& b! D$ k
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' C, t" F2 o. r
安装Nginx和ModSecurity依赖包# q4 _: m# M; G( t% d& k* C
Centos/RHEL
V( Q, _2 F p. k# o! w
1 c5 Z3 i6 C! W, R E1 k) n( [. ^9 b
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ {) Z8 V4 ?, M: a% J* s
Ubuntu/Debian( \8 {# i/ i0 X
0 {2 R& g: L) Z- P( L5 H! q& P
7 w6 w3 X, p8 D l3 P( B u, ^$ 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
" W' T- {& H6 D; {8 R- Z0 S* |5 B编译安装ModSecurity9 f7 k+ Z/ ]% k! `. o3 x9 A
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
( V$ V+ Q7 t# L" S% v7 z, M$ \, h' B& x, o5 R
( Z: K3 r# {8 b" R# X2 p方法一:编译为Nginx静态模块
& ^+ R% B3 z4 Y; ?' D. E$ ]4 S7 o) h/ ^" ?" [
1 L W7 d# `) y; L8 t编译为独立模块(modsecurity-2.9.1)
/ U1 O0 r7 Y6 a# _: m$ tar xzvf modsecurity-2.9.1.tar.gz& w1 S( V; q" l6 }+ Y- \9 o
$ cd modsecurity-2.9.1/0 F& c% n2 Y! t) U8 i! H
$ ./autogen.sh0 H0 d9 H. C2 C5 b
$ ./configure --enable-standalone-module --disable-mlogc
$ X3 S* C, z# R5 x7 k3 n$ make
. D4 F* q9 C- l: {编译安装Nginx并添加ModSecurity模块
0 j B3 k! x7 k# Y! I7 G, j' i$ tar xzvf nginx-1.9.2.tar.gz9 _) S( W0 K' t* H1 t
$ cd nginx-1.9.2
$ Y2 i2 ^: R+ s* L4 p' M$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/7 G/ n8 V: F6 T7 w9 W) X
$ make && make install7 N+ Q. [$ l* M" M
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块9 a3 o5 h8 Z/ z! u: u/ }3 v
, [2 A* K0 X }/ _1 r9 Q7 B
5 r# i. M. G) R- | J编译LibModSecurity(modsecurity-3.0)1 R+ @3 f( X$ x$ `! s1 g! r- P' E
$ cd /root0 f/ F& ?; q4 x& ?9 |0 p6 }
$ git clone https://github.com/SpiderLabs/ModSecurity
& Y6 m- s8 U* w$ V* \4 \$ cd ModSecurity8 v2 B' d& [ Y3 @- t4 P1 M( o" m( o
$ git checkout -b v3/master origin/v3/master
0 ^, D/ J' ~5 r# Q3 A( H$ sh build.sh1 S0 o# d8 q4 [0 {2 [( A' m2 E
$ git submodule init
: r/ O6 _2 g3 c9 A5 g8 a$ git submodule update' w5 }& Z. [, f* X0 _8 D
$ ./configure0 j: J" Q; o3 I
$ make* t' R- W% P4 P* Y; _
$ make install
0 J8 V0 @6 @/ ?# I6 C' g/ eLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。 T$ d% J6 `; T j7 N# L) t- z9 l
" f9 ], `4 z/ w% C
7 W2 W) M. ?8 n q$ ls /usr/local/modsecurity/lib
1 E+ v# ^" o$ C5 ^: _; i1 Alibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
0 s7 o$ K4 G5 J6 I0 R" V' ^编译安装Nginx并添加ModSecurity-Nginx Connector模块, m8 q5 n0 u. o B# k5 h
使用ModSecurity-Nginx模块来连接LibModSecurity
2 B2 Z9 ]. E( Q% Z2 q' o- ?& L9 n8 Q) |: E- I" e0 S& N
9 Y4 y, k# i, x* `
$ cd /root
9 S5 [! d' t9 o! T$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* u# P8 y/ _( e ?% U5 c2 h$ tar xzvf nginx-1.9.2.tar.gz# v5 f4 I3 o& a9 Z7 M A
$ cd nginx-1.9.2
* U. j1 b, D) q7 o7 d) R C$ ./configure --add-module=/root/modsecurity-nginx$ A0 M& M- J& c/ t6 Q! K1 y
$ make
; R5 N1 h) a6 \$ make && make install
r5 h1 V! H4 Y* e添加OWASP规则1 W0 e, z5 i; W; u# K' ^- a7 d+ N
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ y) Y; |9 O) S6 F" h4 I4 ~
% _- i' G' h+ {0 B7 {
- K* [# t% c% ^. }& g0 B ?& f- ]7 U+ d下载OWASP规则并生成配置文件
$ p7 C/ `0 E1 ~" U$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git+ j# F1 u# e2 F
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
8 \7 G" t8 d1 u! d, |" n$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 L+ O$ T( H4 t9 F, H7 f7 S
$ cp crs-setup.conf.example crs-setup.conf; [7 M0 |! c2 X
配置OWASP规则/ O) ~" i+ Q$ O: J9 k
编辑crs-setup.conf文件
) z) i' l% K$ j/ {* p* L! r. @1 F' Y2 Y4 s. ^+ H( K- ?. O% U
: C2 }. G2 ^- c! q+ `
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf3 d9 N5 c7 X3 C. M* i
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
3 N9 \! b% L% o" q, |$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* L4 d# r2 J% r: k
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
" h" v+ g2 S6 Y0 b/ f默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 f- j. \( `( Z& A) f# Z/ D2 ]- ~* ~ Q# F
; b8 J! A3 Q- y. _% r" f
启用ModSecurity模块和CRS规则
0 Y7 U3 |; X6 F) Q复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) r' I6 I! n0 \. z/ ^+ E. J: Z
/ M. q; x7 o6 w& y0 {$ m l* B4 j3 p
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
" F% m, T! q8 P! s: e6 ]7 W# a. z. Z) d5 O
: Q- ` O9 m2 S' r4 K$ cd /root/modsecurity-2.9.1/
5 l5 S j9 _' ]/ E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
* N, d, f4 [/ Y. W* Y$ I w+ ]* [$ cp unicode.mapping /usr/local/nginx/conf/
0 E8 v3 \0 n6 Q! i将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。! u' S# q& c8 v) c1 A7 S$ r2 c
( t8 K4 V& M3 A) w
4 ~ \3 F5 C. x% K6 L' T! O) J$ vim /usr/local/nginx/conf/modsecurity.conf
0 g( b2 m, l1 DSecRuleEngine On
) k0 P( N3 R: z! s# P2 T6 i- r" v) DModSecurity中几个常用配置说明:
0 {1 V* `8 t4 U0 x" j4 d$ x2 f& a
) f8 I5 C: S8 B: h9 G. U' a
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. L+ g8 _% T% `6 U" o+ E3 ?; H9 Y. m
* A, R0 ]" h4 Z2 C' j* F* h# Q* ~
! ~7 f6 U4 d- ~4 I$ y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. k j; P9 Z7 x' i% s- v5 ^1 n
4 o$ n* ~( X* j* _
1 t/ a9 ~1 l& s( }
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
$ A G, W8 H9 K! a8 r. T. x. L S. ^7 |8 C
: V% L8 Q2 g i4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
, Q& [3 z5 s3 v: V9 y8 H& B* g# S
# V! p4 o( s% ^ P' G2 {9 z6 n+ x D$ V# l4 w
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。4 S6 E+ Q" U- r T" y0 D% \3 g/ {
0 c9 R1 x4 Y: T2 A" V4 A4 S& G! X, u [& P' W5 Z: a: B
3.x版本CRS
* g: v$ C( b1 k7 |$ {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" i9 Z7 }/ O- m: }6 k- H3 Y' b M
# 生成例外排除请求的配置文件5 A1 l( G. [) l& {( M9 C
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf# j& v$ b" ~0 I: [- P' x
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf( Y& `# }/ s g! K
$ cp rules/*.data /usr/local/nginx/conf
- }$ X9 l3 [5 g+ N为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
- B0 R9 H9 B2 o3 u* L% h+ r. u: W3 x$ y' C @( \, ^
4 ~/ a+ h4 l! V" l
$ vim /usr/local/nginx/conf/modsec_includes.conf' R+ M8 v; D X2 q: }6 \
. v& K2 T1 @; i) P( x0 Y/ H) F[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 ]3 j+ w- J* `7 F" |
( p! L+ N8 U8 t- b9 f注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
6 k7 y/ g- ]2 Z* v; H& h5 k& a) v& D& ?. B
: r8 w- T, f7 m- d# Z8 G# O配置Nginx支持Modsecurity- Z' _3 d. _% N& N
启用Modsecurity
+ { h. Y+ s u3 l2 G5 V r! u使用静态模块加载的配置方法- l5 W# Y9 `3 c: C. s f, Q8 P1 e1 b
在需要启用Modsecurity的主机的location下面加入下面两行即可:
- `: p0 A3 R# n1 _' Y* Q0 i, C( s$ j: W
) E# |! ^4 g, f4 P6 o3 f2 k6 }" |ModSecurityEnabled on;0 q. B% D" j1 p2 I5 |5 i6 r
ModSecurityConfig modsec_includes.conf;. p B @; U( N
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 i1 N4 M" W+ I2 B; P* X0 S% ~
+ E; t1 M7 C7 Q( M6 s
5 _8 o' _2 H% t, K) i
$ vim /usr/local/nginx/conf/nginx.conf
0 C2 n; \# I, G8 t1 n& _' Q1 i: w7 W0 U4 v
1 u/ b+ w1 p+ a9 O; L# ?; mserver {
V6 W% t6 z+ W5 h0 ? C \% q! v2 Z. Q listen 80;
- z% b# q; C# ~, ]# E server_name example.com;' w/ h- T' @9 e+ T& U. P
. X8 N9 n- l5 _! F! ?! F7 F
* K0 A8 z, s3 [. x: p+ V0 V! _
location / {
. F. ?* `, G% r. v ModSecurityEnabled on;) A! I8 T2 m; L$ h
ModSecurityConfig modsec_includes.conf;# n" A/ f$ w' i- ?
root html;
7 g- q M( o% p Q0 e index index.html index.htm;
- I. C/ f4 Y- W: b7 @/ v7 p }) u5 m) @3 u2 }& U; n
}
# V2 c+ t" a* l3 D2 X4 i使用动态模块加载的配置方法2 `( e" H- Z. |' u8 w ^6 _5 }
在需要启用Modsecurity的主机的location下面加入下面两行即可: Z9 F' c- |* b" B) P$ P6 p( N; k
& Y" F5 p/ k( R, H1 t6 N* [ {; S2 r) C
modsecurity on;. t. [' h* c4 j5 O5 H
modsecurity_rules_file modsec_includes.conf;; I" R* I, Z' o* H/ e G
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! j: w+ Y5 t8 F9 p5 h
6 q: o6 Z6 ~6 g. ^5 \3 m b
9 G: K/ R: P4 H: N n* J( o$ vim /usr/local/nginx/conf/nginx.conf$ F6 v% b1 I4 n- U W( P) R+ J \/ L4 B
0 {: ^" f t. ]8 N9 ]1 G+ R. ~% T
$ ^, ^8 u8 }4 \) z) l( l: @% Y7 ^$ Sserver {# ^7 ~ w4 M7 `6 s# P& R
listen 80;4 J' ^1 L& d0 Y; A! t; J
server_name localhost mike.hi-linux.com;2 E: G" m, S- G. E m8 Z
access_log /var/log/nginx/yourdomain.log;' X% u8 r$ a5 {; V& s) ?" d
3 U% ]0 e0 T# b& l5 G8 M: x7 N
6 y6 \; ^: R% z. c$ X/ y location / {
1 _9 K, _1 u' I& G. C3 K7 B
1 k* }: [! q% \! z
4 ?. O- O" m4 K* m3 ] modsecurity on;
- z J g* D2 C6 B8 S modsecurity_rules_file modsec_includes.conf;, O7 \- D# }, k/ y7 l
root html; B6 j4 ~5 M1 [( p' F( f
index index.html index.htm;
) K. R+ s$ F8 s1 D5 O5 K}1 P* ~, v7 h2 Q
}: p/ S0 |0 M7 W7 \" {
验证Nginx配置文件. x3 h! x4 \" Y& s; N6 P: ^
$ /usr/local/nginx/sbin/nginx -t* ~: z% E! u, c
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok' I" w. X0 T: a; f
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ M" g$ o# @( j/ v7 Q+ n启动Nginx# ?* S1 l3 n" ~3 z& e4 X8 v$ H
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& u; ?! o* ]2 G/ |+ U
" k$ F1 z4 {: C, M: U0 W6 X测试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能够战胜更多复杂/未知/混淆的攻击模式。 / b0 ^. s) k4 R/ n' k8 i
|