ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 _) Z F7 u/ I7 A$ M' I; j8 f t! d" w0 H9 h- g+ ~
- g4 [, k1 s% I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
$ R! |/ [$ P2 U2 N$ n" Z6 Z, j: \+ [, S9 b
; P' S7 ~; h& }0 @) y
什么是ModSecurity
' C' Q7 Q: w! X4 |' P6 pModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
9 L* N% e- i( v/ F8 ?. r9 l9 s4 P- y( T* f/ w
, ^! O6 o' H% B
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 m- P3 q" h; B
& n" `) w+ l; o7 R) m. [! y1 g2 c# o* y, ^: g5 |
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% J$ z+ }! `$ o' q
2 @* b+ j% a" j$ G- ?
1 U; q; U$ z) h& e! L+ [& q
官网: https://www.modsecurity.org/
L% J, H/ v) {' j) d3 M3 B9 h0 [
# N2 [# q O' a- ~; o) W2 y
1 l& K# @' ?( L6 F4 [: w- C什么是OWASP CRS
) L Z+ K( ^1 m7 Q. c- gOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# M K$ t) U R, b8 J" |' R( E5 s
* Z0 y' z8 \2 x( g
9 U2 _$ T+ ^8 j! M' y# R8 cModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 z3 C; h' G8 }7 w- i5 x
* E, z6 W( Z; N5 z$ t6 Q- m; O8 @) S" O- L
HTTP Protection(HTTP防御)
F1 r: _. e' G6 P" y6 q3 \% vHTTP协议和本地定义使用的detectsviolations策略。
; p' w& f7 S$ V" ~0 |3 o- Q
1 I& o+ a, g' m* k4 k9 F$ A. M, _/ A, u+ H" ~: Y# {
Real-time Blacklist Lookups(实时黑名单查询)
5 k7 Z' h2 r! e" T5 y# ~利用第三方IP名单。/ k& ?; M/ d _$ K) v4 c7 \) R
8 @: c! y' p5 D8 L8 Z' ~+ h! X' a0 I
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 g% y) d; v; C. C3 k" }# B防御HTTP的洪水攻击和HTTP Dos攻击。
) j, }7 O$ r' b1 \9 ]4 z, ]! U" o# R. v' u |4 q
4 J+ m! z$ u+ Q2 n5 x
Common Web Attacks Protection(常见的Web攻击防护)6 ~( K# k# W* s# _/ k
检测常见的Web应用程序的安全攻击。
5 h' V; Z# z/ X+ m- _: g
7 g# G; G9 R+ j8 ?+ x5 e3 z" |
8 q$ ]2 H* o; L& B# r: y% CAutomation Detection(自动化检测)" ~* q2 X7 f' v/ [
检测机器人,爬虫,扫描仪和其他表面恶意活动。 F% K4 V. A9 Y' V
; X+ p: q- h( Y- a
! V: H3 A' q9 K/ l* ]
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
5 s; [6 |% {- x7 ~( ]检测通过Web应用程序上传的恶意文件。3 L" H4 i8 R; s0 d. W. C. J, C& h
) H3 Z. K8 J2 j. X) @( P& U* ]/ ]* ~& T
- S% i" I' Z" S( w' R0 k; T4 ETracking Sensitive Data(跟踪敏感数据)7 O. h3 k, [$ _5 Z1 q. v& D2 f% x
信用卡通道的使用,并阻止泄漏。8 S. i R0 k% _: S9 a& {; L3 ^
9 U8 `/ [% k9 ]# S. E3 e( k1 w. x# l
Trojan Protection(木马防护)
& E( P/ ^2 i% n检测访问木马。
, [7 b3 s' {; U u3 h5 q5 g; p$ v8 ~4 ^: A
4 Z1 Q. |9 p8 L V
Identification of Application Defects(应用程序缺陷的鉴定)
* s T9 k& A' u2 H检测应用程序的错误配置警报。
, m. c1 l8 L% W/ p
2 H/ j- l" `0 O d0 @, ?: [
8 E- l# x% q" Q: d4 oError Detection and Hiding(错误检测和隐藏)9 g$ t/ B' F0 c0 h; j2 ?: B
检测伪装服务器发送错误消息。4 w1 D8 c0 X0 l% I# R# m2 H z
0 |! {8 [; J2 y# M$ v! ^
; E% ~0 a, [- w8 l# Y安装ModSecurity
' [1 J) H' H2 j5 c; I. G软件基础环境准备
* V5 N# W. l) r) `* R下载对应软件包. g2 u( H! }/ B4 C' t
$ cd /root
4 _# ~' W+ _* N5 M, I! x5 I$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 u% T( b B' d' M" W6 U3 J
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz& e$ p; x: V+ R0 s
安装Nginx和ModSecurity依赖包% q0 U; {% \: Z( N) {
Centos/RHEL+ F$ s" ^7 V1 V* z Q' u
* R: y5 J: e) {. U% l7 i- c" i0 O" P9 n3 _" b' X+ w
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 q" p- F9 d4 L QUbuntu/Debian0 Q. d4 l8 \0 Y# X8 I" [
9 I" [- J" a% ]* e8 K
# i6 H3 k: p2 v* 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
& N5 q% Q9 N/ J! N/ o# b- K, M' ^编译安装ModSecurity. \0 s- v9 X9 E7 P% n/ z# t6 ~$ a$ D
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
( W$ n4 m5 k2 n. j* {# U$ s$ w- R0 C7 R a
6 ~$ z- |, o0 h3 n- r' ^+ L. X! ^$ v" K
方法一:编译为Nginx静态模块9 k* J: x% Q9 ?1 y& j# Z
_! r7 h" b% p! Y( z
" x0 g( Q3 M4 W& z2 o: C B编译为独立模块(modsecurity-2.9.1)
6 M; v. @- J( a( m5 @, ^2 a$ tar xzvf modsecurity-2.9.1.tar.gz
( V) z; k& U. _" H$ cd modsecurity-2.9.1/
- S( h5 D1 n* Y7 g7 t5 r$ ./autogen.sh9 ~ m" \, c% N" L
$ ./configure --enable-standalone-module --disable-mlogc3 y5 Z0 R+ D3 b3 }% @
$ make+ q* V( v, U c
编译安装Nginx并添加ModSecurity模块, I! ]: [7 W3 ?9 M. G
$ tar xzvf nginx-1.9.2.tar.gz% F; l( i; O$ b5 X" S
$ cd nginx-1.9.2
1 a! r; V1 P$ A5 K7 I. v7 `. R% ]$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 P4 B& K5 q2 y+ N+ j2 a7 s
$ make && make install8 s8 s0 o% H' B
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. t) w/ b! C& Z; j
# V- L* u5 k4 T
5 J% e/ A8 q* @编译LibModSecurity(modsecurity-3.0)5 v; o# M2 B5 B+ I$ ^6 d2 e. d
$ cd /root) G/ f/ h8 y4 M" i8 v, ^4 f9 E
$ git clone https://github.com/SpiderLabs/ModSecurity
+ v3 `0 W! G9 G$ cd ModSecurity
8 D8 `. x5 o& I& ?5 W5 h$ git checkout -b v3/master origin/v3/master
8 Q+ i+ b+ N, ^- x$ sh build.sh
. @; T+ v4 \' R- t4 }' O! o0 S5 b$ git submodule init- f5 r$ C8 C x" N) P
$ git submodule update
& N8 U$ [9 C5 W3 J& g2 _+ I$ ./configure
|( p H: G2 a) Q% n0 g3 }! x9 _$ make1 x: z1 j. r! F. A% X" J. r/ c+ l
$ make install
8 I. X# n7 ^# M) {9 jLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ j7 Z% w* G# X$ X/ W! M' v1 @7 b8 E6 ^
0 ?+ H- K$ z- g; Z
$ ls /usr/local/modsecurity/lib2 W% |& T# z1 i* ^% j Z1 p) m
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
$ p+ u+ d+ j$ }: o- Z编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ B; i/ k( m/ V1 j使用ModSecurity-Nginx模块来连接LibModSecurity8 N6 j$ C0 `! w9 `& `
" X" ?' X$ ^! Q- P# Z' q/ X
4 Y# n/ D9 g: v
$ cd /root
" G2 x, ]9 z" h3 @+ n! d6 j$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
! v7 z9 Y- ~* L, h4 Q% f1 `0 \$ tar xzvf nginx-1.9.2.tar.gz! K* T p5 ~/ \! x/ }
$ cd nginx-1.9.22 k6 C, U5 ~+ k. P9 n; S. W, c- r
$ ./configure --add-module=/root/modsecurity-nginx* @5 O* V& I( s/ u! f: `) }5 H
$ make! k: o0 \% {: F4 S: \2 |8 r5 G
$ make && make install7 u$ j8 B4 `/ q- N k& {
添加OWASP规则3 M) t& U/ \; i
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 h4 q; M! W0 w% [+ U$ q5 H6 M
" k! L/ u) |9 d7 H% {! p% S; ~- ?* O& G3 x/ w
下载OWASP规则并生成配置文件+ R7 S1 k7 t4 L4 S: a
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: w5 p( ^) j+ W( l$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/# W. g1 S: V/ n+ j; V, Q; E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- ~* _7 d! M' [# q; P+ u3 @9 I
$ cp crs-setup.conf.example crs-setup.conf
# B) m1 X% Q2 Z配置OWASP规则4 ?6 U8 ^) D2 T% P/ B ~# G
编辑crs-setup.conf文件" b( i& q$ _$ A
+ Y- H- \$ _, I' n
% V# Y7 D$ q* I- y3 z5 i
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 l* E M) b, k" r7 P6 g" w$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf6 X4 h6 C* H% j% I' m- o7 {
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
]* B8 d8 K" b- D6 }, }; ~; l$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. I, a7 s0 \5 b- ?$ @' [. N
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
2 I' N: R% Z8 Q5 \8 ?* n0 g- f
( o- e p( I3 h' o, x5 F1 D/ y. J1 y; W: j" i
启用ModSecurity模块和CRS规则
8 n1 u. j: k7 E$ B9 F) I复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% g; W5 A8 c' |; ]$ Z }. m+ {3 C, T" ?
o! L9 u1 M: Y/ e" bmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
# w( a/ ?, q6 \. G1 ]7 s3 Z7 P4 G6 D% i1 g4 ?2 k5 ?2 I
- A$ u @: V0 q$ cd /root/modsecurity-2.9.1/" G; f. w. T0 A$ M
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
. d# |6 H9 ~& E, v& h$ cp unicode.mapping /usr/local/nginx/conf/; j; X* n g& F% j* _. m) n
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。" V& X: x% X1 g! W
: X: h0 x* o3 P1 e6 h1 F
( m+ X6 l& l: D* k5 a% b/ q
$ vim /usr/local/nginx/conf/modsecurity.conf
3 _+ K7 _* C0 T6 u/ ?6 OSecRuleEngine On C. I }+ a" i4 N3 I+ |
ModSecurity中几个常用配置说明:
+ F$ E$ q7 L$ h- \4 f# h- _
0 s; i# I4 J! q. n- o# w+ }
, ` h8 V/ Y0 t# ]4 x6 K; ~; o/ ?1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。 z& U' Y! _2 O/ y! W6 e
9 ^" K; }# F6 c& Q
( x' ^$ I$ e1 ]+ s2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
+ B3 ]' y/ x3 g1 d
1 ~, U" V( X& q& w: f2 w. Q5 ~4 _3 ] O/ x
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。/ {% E* c3 O1 v% E9 o. G
( I4 a4 M( d% b9 E+ S( X
- u' F$ H* |! X# t# P
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! N, _8 l# v" n; D
2 N% [8 V2 K9 J8 X% n( q
+ d- ?. U9 s+ p! A2 C( R+ [) v' w
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; D0 H* p" z; _1 _! b u( v! w9 M& o# o3 d0 _
9 E3 Y# B$ ?# ~% m& ]
3.x版本CRS
7 I1 O8 R2 X" j; d( G" p5 W/ c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 m; F: c/ r: N, g. n0 A# l# 生成例外排除请求的配置文件2 u2 Q! m) T: C1 B
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
# u- |, ~: @* L% v$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% j% d) v M3 R
$ cp rules/*.data /usr/local/nginx/conf
9 P, l9 {" v; x0 r& [6 G为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。! Y; ^$ A, T1 I
6 m2 P! G- T' R+ ]+ a
2 k! v) [$ `* ^% u+ Q* H5 G$ vim /usr/local/nginx/conf/modsec_includes.conf
$ E0 _7 B6 W. y* T1 P) x% V# W- q5 w& L$ L6 X; ], 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
& d9 n& X9 D6 _% J3 j* ]' j/ c g4 E( L1 U& V, W8 x- q; J. s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
0 [* ^ {, ~8 d. o2 v9 ] Q- r0 @; {$ A1 l1 c
% P3 f e3 }, w9 V; r! O9 |配置Nginx支持Modsecurity8 U: A7 d0 k3 P7 k5 J; s9 N5 e
启用Modsecurity. J- R0 ]- t$ J+ U, y9 n1 ^
使用静态模块加载的配置方法1 \6 j, v" Q5 c+ n
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# }# G6 ^% k' a& c/ l- d ^. j7 a [
4 W+ f2 U$ d" [* W" I
ModSecurityEnabled on;4 {- l/ d, g+ t1 m
ModSecurityConfig modsec_includes.conf;4 g5 `# x0 r7 Y E
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 o( P2 U7 }$ u2 S+ \
* |0 t. x' E: B/ x( T7 D, b$ E4 D
$ vim /usr/local/nginx/conf/nginx.conf
+ B3 O/ B6 d& g! R9 Y4 \1 G. ~% ^# Z t
% t7 L: { x# n0 D5 @
server {
- {, C! O: e6 J \ listen 80;4 e: G4 l- V+ V, r! L1 V
server_name example.com;2 _, ]3 v, i' V& s+ P4 D
t' d3 s$ q. ?- i0 X3 Y o* a
& D; k- H( d9 r location / {' V! ~' I; k8 p+ l' Q
ModSecurityEnabled on;
0 }) d9 X3 {. {8 q ModSecurityConfig modsec_includes.conf;3 z# O) F- N( j& v5 j$ c1 T
root html;8 p+ Q) {, r) h0 e: h
index index.html index.htm;5 D& h+ t. H. K+ G
}/ {) }# O: s) T4 [/ s$ N$ t6 F, `
}
$ d( x2 D6 H! E$ p) e4 p使用动态模块加载的配置方法# A+ @: u! c3 e5 @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% W% H8 L* h" [7 r! H3 o) f4 K
8 G' r' P9 O ?7 o6 B f6 _- \6 A
5 d% r2 \+ F! |9 m& {modsecurity on;! k* i7 M: d- ^; N2 m
modsecurity_rules_file modsec_includes.conf;& {3 D0 G/ f9 b* J. p$ g/ ^4 S \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: h- X7 Z5 i6 G6 U2 b: g/ C
! m1 e( G# l; m1 M/ ]
+ W% y) n6 y5 X4 O1 |$ vim /usr/local/nginx/conf/nginx.conf
6 s8 F% x& Z9 T* \4 ]& K: F* u% Z( e3 {& ^+ B
* r/ X( { _2 `# y! V7 d
server {$ l# I% C* g3 o1 ^+ ~. O6 F. Q
listen 80;
+ F( {) m& F( [3 H server_name localhost mike.hi-linux.com;
$ ] t- O( k- @5 s access_log /var/log/nginx/yourdomain.log;; c+ j' u+ ?2 ]% ] P% e2 R
$ X0 n& k' ~5 t3 c
" N4 Q9 m% H! ?' e
location / {4 B, o3 T* j8 Z5 O
6 @6 _2 V9 p. I0 w& c0 Y* V% H; }1 B! j0 V" I/ _
modsecurity on;
+ P" @7 P& I. G6 y modsecurity_rules_file modsec_includes.conf;
5 O( w+ D( |' h) q: W/ e1 [, Y7 H root html;6 w3 M$ t; e9 X F/ c
index index.html index.htm;
. W% k8 d) G1 ^4 q) E/ P l. M}
+ j# P) ~8 j4 u}
/ c, k3 J; q/ ?; S% i+ o8 h验证Nginx配置文件
+ k) S# ]/ ?- t; I5 u# L2 n' |9 L$ /usr/local/nginx/sbin/nginx -t
* X$ C8 x" x" w4 Mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok% H( l. D; L7 o0 t
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful q! o9 V3 r$ z# {3 _
启动Nginx' r/ J" R8 ]9 V* Q. F4 L7 M
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. O0 S( K$ d( `5 _' F
o0 v! W( A6 V2 A$ W4 E
测试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能够战胜更多复杂/未知/混淆的攻击模式。 # |$ {! K! m1 @5 E9 }
|