|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ C- d* k; [# `; j. m, r# }/ l4 X4 [7 ?" K& q- T
一.准备工作; B# q* ]# r$ J
" T# ^" a% U7 M w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! z7 a% f5 S8 U2 X: y4 K% a
" X1 g0 U, c- Y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) C9 p4 C3 f0 ^: Q8 R) \
8 g- e! O. n, ^8 Y, z- Y
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! ]/ R6 ^3 m# M
1 Z1 V6 R8 ~4 l# t3 y& H6 `( P/ P
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! d* j5 }8 G3 Z9 G
- j" u: A9 f7 [" I: V8 M依赖关系:
. ^! Q' R5 ^( N* i( x8 X9 ]! btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 r" k) T0 A& y* B) S
6 | b1 d! H( J4 w! Q w
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# g/ w$ V) s- u1 a- f$ `
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 X! q+ c9 P! _5 W% d" C8 d) m& k; D6 ]
0 _5 b1 ^8 e1 d' ]yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
" U" t3 \+ S3 I1 N二.启用standalone模块并编译& B, F# k% E1 X4 I0 C1 ?
7 H1 N1 k7 b# k4 l下载modsecurity for nginx 解压,进入解压后目录执行:
8 o9 d; P) w. S8 M4 ?+ m% R, Y% V8 q7 q% r: V2 E7 L
./autogen.sh
4 I% G4 o9 U+ b/ c$ ?./configure --enable-standalone-module --disable-mlogc
m( r- U8 A% X, n6 cmake
8 x% z6 M, U' O# l% ]! ~三.nginx添加modsecurity模块( Y) r$ {$ e' q# I( B
- H8 ?& r) }) R/ k# \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ u4 m: l3 i2 q7 `0 h
& k3 E2 Y0 o: B3 ^8 P) w0 m/ @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine4 w& ?$ K, X, i/ g
make && make install& _1 c) M7 [& ^1 B
四.添加规则# ]6 \) ?' y0 `3 W( Z" \8 V7 p
( W/ ^. z4 Z( K# ]
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& g& L6 | \( G
3 \! ~) y* \" `6 g" o6 T0 I
1.下载OWASP规则:
: r+ U) M5 ?2 G0 V, d( G2 ]" |9 ^0 W
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% t/ @1 v8 G( I5 L" O0 N8 P
8 |9 `& S7 ^7 I5 n( ?$ p8 D
mv owasp-modsecurity-crs /opt/tengine/conf/
$ [* i1 w" u6 T% k% e9 a& R/ b" }3 D; A6 ]2 d, l; v5 n
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; N: v3 g3 G& {- L% w3 @2.启用OWASP规则:: {! `! ~$ w1 b; V! @5 r
: r( {) n$ f; S/ t( R7 ~, }; l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 K8 o5 s7 C4 b+ c& u9 h% B9 |
% x2 u9 p. }8 D5 x' P/ U* [编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ @: N0 Q# c" @: |0 A& I/ M6 J X& p# O; N( @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, X4 ~ R* _2 e ~- }0 {
6 L( T7 _# O2 `: |2 u* P9 f! _# U- D5 SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 T& s/ ` M2 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' U H4 t( U {2 @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ }* s+ c# k; b& ~9 B* |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# Y- h# `8 g5 V+ s( y! n; ]8 P G% N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" Z( F% F- Y& Q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 L& O. n& X& c% W0 J2 q+ v3 p! C, t/ sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; M8 P4 o# s! s5 }
五.配置nginx" X5 j- E: y# z
0 v% X" k- ~0 Q$ x1 ]
在需要启用modsecurity的主机的location下面加入下面两行即可:5 I' }' b, I2 A# r! U+ Y2 q
4 |) s2 K; _" a% w( a
ModSecurityEnabled on;
3 ?% E. P9 `$ {3 p) \0 EModSecurityConfig modsecurity.conf;: f! g9 O5 y, y6 x2 f# \) A
下面是两个示例配置,php虚拟主机:
2 ~: I( H6 h6 \0 n" P: d3 A$ P/ U: }- w/ l" c
server {
8 w* V+ U: z1 U1 |5 r2 X2 Y listen 80;
+ [% k* I% ~/ e& S* \& x1 v0 s2 I server_name 52os.net www.52os.net;
- \* v/ q( s6 \
/ r$ J C0 f0 X location ~ \.php$ {4 Q9 m* g% }6 A$ i. y
ModSecurityEnabled on;
$ }, k$ J& S! K ModSecurityConfig modsecurity.conf;% N) i9 K8 ]5 f0 I, ?3 \2 q
( @; ? Y0 C8 A* \+ h) d3 b5 B root /web/wordpress;& A# k5 D( S: c7 U ^; D4 ?
index index.php index.html index.htm;
! P2 q; m. z0 K0 V( ]' V/ i
0 h. X! M0 ?; j/ P fastcgi_pass 127.0.0.1:9000;" ^, m* O, g" b" Y* o
fastcgi_index index.php;+ _! {# Z: c5 ^: s( d# Q
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
X; F k6 F. O$ t" R/ ?: y include fastcgi_params;
; r5 K2 w( t# @ N- z% { }
$ u9 m q2 ~7 w" F }
# H1 e9 D/ I, ]. V& ` [/ z+ a/ supstream负载均衡:
4 H: W( C( J( R+ `' l% U5 T+ r" ~* Y$ n
upstream 52os.net {& a. {9 c7 a) n9 A
server 192.168.1.100:8080;! H. H6 K2 X5 Y# K4 d' R
server 192.168.1.101:8080 backup;
$ R8 d5 Z- p2 a}* S6 B. y5 @3 \: k0 t
& A9 \7 |( k \server {
) @: I8 i( E1 {6 Q8 A3 {listen 80;
+ d5 j. D5 @) yserver_name 52os.net www.52os.net;8 j) m, Z2 T% {) d
& G' u- a7 A% G3 M3 Y6 H: @( f0 x
location / {
F$ T- A5 _# t3 ~& g; u) Z ModSecurityEnabled on;
" Y; o3 y- G; \) z. x( X. V2 W" Q ModSecurityConfig modsecurity.conf; / a% ?3 J0 I$ A" ]" F* q& j; T
, ~+ o7 U7 W& P5 F5 v$ L' Z proxy_pass http://online;- R/ o* ~0 C+ v) ^
proxy_redirect off;
; P \. ~2 q5 h( @9 L3 {4 V proxy_set_header Host $host;
! H( C: t& @* C" A& _" v g, g) w proxy_set_header X-Real-IP $remote_addr;
( y( h8 c2 X9 ~) s; w; x- I* E proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- F7 m3 @+ ^( j# \: I; D+ j$ C
}4 g3 T6 u5 t, e
}
% u _" F: F* p* G) I8 v六.测试4 y4 I0 E% b3 r) Q: h' m
- l; s+ F7 L5 O- n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 N$ F! x! T X1 ], s/ e: _4 D0 B9 H4 l9 a) H# p- E% ?6 ^
<?php/ Z' e- V: c u! t" n' I- H% ~" |
phpinfo(); ( k1 L2 P- @: C
?>
7 P% j" g' P- a' Q7 q在浏览器中访问:# z& i' j5 {0 Q5 W8 ^5 n
+ M' }0 O6 `5 W, z. m! p$ }! thttp://www.52os.net/phpinfo.php?id=1 正常显示。
5 R3 X7 L# ^, w! `. Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。: F/ S1 C6 ?. q0 d, {7 j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
+ g) b4 D) U7 l* I4 j' O; T! e+ m说明sql注入和xss已经被过滤了/ P% A, l" E( Y) n4 I6 H9 B
) U2 D3 x; @1 y七、安装过程中排错
/ s( Z+ e/ U& z Z8 J1 T
$ _/ c1 |8 P3 e2 z1.缺少APXS会报错
! M9 ?- x. z; l3 C h: V6 q# P' p& g; q. d
configure: looking for Apache module support via DSO through APXS* k& l# M, ?; Q; t
configure: error: couldn't find APXS
$ a* ` `/ B9 c* Z, Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 l6 R" B' a# n4 G: E h+ ]# z6 u解决方法:( M i8 }% R" Y' E
& h$ I2 ^3 R# Q" {9 e! E4 S4 ]
yum install httpd-devel3 t6 D1 I/ T8 e% Q5 _; B
2.没有pcre. c: C- c1 K7 B4 R( u9 O1 E
7 ]7 ]; Q0 o3 x2 H9 j; L& }% {4 ~4 B1 i
configure: *** pcre library not found.4 @6 L, D H3 O& G) q8 N3 r
configure: error: pcre library is required
- n9 f, _0 s7 W$ M解决方法:
, ~' g' p8 o. e
- F. @' R# S) g3 Q! qyum install pcre pcre-devel
7 m" S% J; [, m4 f& C' F5 o3.没有libxml2& L) k# k+ s, G: y) i2 W) t- d) R" I
6 S% d! h" I9 U; T5 K$ y. T
' s$ i1 R- K i
configure: *** xml library not found.+ M3 Y$ r; y) j7 U
configure: error: libxml2 is required, m6 k! A$ W8 n+ {
解决方法:
7 m2 ]9 z# j, Y% ]& \# ~+ G) e5 m" W9 a$ z# \8 E U* H
yum install libxml2 libxml2-devel: I! O: ~: ^! [7 V1 w2 r
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 Q$ _3 V" w* w" C' l: [
, f( g4 |; x" F/ \6 R. w5 |7 YTengine version: Tengine/2.1.0 (nginx/1.6.2) L: j2 ^) w" J0 X
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& _$ _! \# @- N' V. q, j2 Q0 T/ i原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' [! H8 L1 _# G$ T8 Y1 @6 {) z
0 |) c& j% E- Z' o" R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. V5 y1 E5 v4 O' u; v6 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": k7 \$ G' k0 {0 Z/ |) p
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! B9 S" f+ [$ ^ W1 B$ e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! `. l: ]& [3 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" r+ \/ L0 M: ?7 |: s* H1 N
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 H4 p0 X) @6 n解决方法,移除低版本的APR (1.3.9)
' g$ i3 H5 S) `* m7 T
5 [) T1 ]% o; g! v: r$ Qyum remove apr
( ]" s& O4 y% H/ g/ e( ?$ d5.Error.log中有: Audit log: Failed to lock global mutex, `" K0 i3 z& E! `' G5 G$ s I0 P
6 @, X( \; T& h4 }/ a9 j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 @! q2 h; U* Y# Nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* `) R: a+ w5 \$ N& H9 ~解决方法:
8 h3 Y! ^) f8 D+ a1 I( M9 D编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
y; ~9 m. ?' n& A
' N8 N2 X6 g" g4 V/ {3 D$ p9 s4 }SecAuditLogDirMode 0777
- d. E# G) y. O k& OSecAuditLogFileMode 0550. I' A2 N0 ]. h5 P9 O, x
SecAuditLogStorageDir /var/log/modsecurity* v, v; N: q T7 b- X
SecAuditLogType Concurrent
. T5 E" @% H" ?+ M' G参考文章:
! Y, E/ [( M, s4 r; `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 h6 O" m& Z* K
http://drops.wooyun.org/tips/2614 |
|