|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- ~4 j0 v, u: s4 e) d
% i1 M8 `- _" R& Z* y) }# e一.准备工作
0 @6 U) ~0 t0 q) M! \* H# N# G7 Q+ |$ c) h' f$ W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 `+ Y/ ?( M4 X6 Y9 Y
]# q8 O$ `5 d' Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 V9 s# ]4 p5 \+ C- v. b }% h/ i
* m0 w; z! ]+ y$ J2 L* xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) _/ H3 G1 O. A# n$ [6 h; y/ t( ]2 X, c# ^0 J1 Y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# |( g; k4 i8 i/ s. m, \2 R6 D# @( m9 x9 ~! _* _% j
依赖关系:) X( E! X5 u% [8 V) J+ ]
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 A5 I5 o+ I" _
' O F! b1 B3 G2 \$ _8 jyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& r- W; o: }1 n/ N4 @+ x
modsecurty依赖的包:pcre httpd-devel libxml2 apr& _: ^ o+ W" ?# M' V
; \1 |: w9 m5 [: I, \: c; _
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! U7 l$ ?6 {8 m! o
二.启用standalone模块并编译
4 b0 C7 V* B7 |* R9 s& ]
$ M: }# S! U3 F! S下载modsecurity for nginx 解压,进入解压后目录执行:
( C1 b) U( [& j+ [
/ G& |+ k" U- Z' }3 P./autogen.sh
" @: R5 S) H; K9 a./configure --enable-standalone-module --disable-mlogc% z/ v# q: d8 A$ S4 `0 C2 F
make ! }& q# w* c( G: r; \
三.nginx添加modsecurity模块
2 L# e5 d( Q: c6 _& N* O5 \4 m8 e& g" b& K5 ^ W
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' i' a6 A( Z5 u" B5 Z1 c, _/ A; {4 [8 L' p% h) e( [
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine& v" W0 q5 g1 r$ e# S
make && make install
k9 u g5 D( o' S6 I) C四.添加规则$ A" d5 m# u! q& e
' D9 Z! N; X1 I! U7 mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ w9 @; S6 w- }9 z" @; r, B. p/ Q; B) i
1.下载OWASP规则:0 W# [* e4 H. D: \8 r M
" B2 z# _' D# Z; W. Q, I% E! u1 s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ H+ H9 Z: b6 T' x; _# {+ O" }" D4 a: i) ^4 R" ?8 ~, A
mv owasp-modsecurity-crs /opt/tengine/conf/
# o2 o. g9 g4 v" \4 p+ S: j- Q$ R7 r1 d! Z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 P% u0 ~8 ]. R( m4 p0 C( q2.启用OWASP规则:
% x1 N: h, Y6 @; Y% @0 y t- {: E9 V% k0 b+ i6 r, f
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- B% {1 m7 a- K* k& w W% Y
3 P. R; P: A. y' M% O' x" S0 A- K编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) y/ e" Q9 B1 w& q5 V9 B8 G c+ i4 ]. `7 I
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 B1 A9 N9 w! |. x1 B
( _' m1 k3 w# DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# U- ~( F1 t j, C, p' Y8 BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" o; |( k. L) ]$ z' a/ u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. B8 h/ h' L7 @) x/ cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. P4 P4 m$ ~2 G+ e7 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. \: q4 ^( \( D: h
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# j3 X) q: a3 T, y. s- z, [& g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 ^6 A1 w1 V6 Z: q4 g# ~五.配置nginx
6 s0 W' L" h9 W/ Y
9 l' q; i+ X W' S在需要启用modsecurity的主机的location下面加入下面两行即可:+ |5 H& v1 z' \2 Z% A
/ E* N$ U& e, W% LModSecurityEnabled on; / f8 M0 t C: J) y: f
ModSecurityConfig modsecurity.conf;
1 c4 y5 o1 k# C下面是两个示例配置,php虚拟主机:
) y& ^% ]/ O$ x- \6 ?4 W7 g3 x% _- G( x. G' ~1 O/ i
server {
w1 x2 l5 ]0 t. Z ~ listen 80;& R$ @) ~1 [9 O
server_name 52os.net www.52os.net;
9 t( z1 _# B. C2 {" w3 z
- R+ W. F, G/ @+ o8 J location ~ \.php$ {
$ j5 M: M. |" ~ ModSecurityEnabled on; 8 N; Z% H* D( ~8 p, B# t, ]
ModSecurityConfig modsecurity.conf;
8 |7 ^8 b: g' T1 t7 F
( f) E. i8 x( S% v/ D4 i. I root /web/wordpress;4 ?1 K) K% `5 a0 M6 ~5 s: \& |- l+ `
index index.php index.html index.htm;
/ U1 o* G/ {; M. Y
* S, Q9 O1 U% I0 e' Y) S4 S1 i fastcgi_pass 127.0.0.1:9000;2 n# _; F6 b1 `9 p; a9 H! T
fastcgi_index index.php;
6 P% z( I# a( j; H# D fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 I, _1 {% K3 R include fastcgi_params;/ \& g3 y2 N% _: ~
}
: W6 ]2 P5 I# y1 h }
& s4 `+ n0 k- r# L1 bupstream负载均衡:
; \- Z$ x* E$ D p( ]
7 ^8 \5 u7 y; c3 [; ^ d" X' l+ dupstream 52os.net {! s# b4 G4 R D6 ]! F+ J2 b8 p# B
server 192.168.1.100:8080;7 {# |- K( g* s( W5 A; B0 a- p
server 192.168.1.101:8080 backup;
1 b* I; K8 z$ {) X}
- y7 M J# s* }$ o3 T! a6 p R3 u+ ]* {7 Y: Z6 m4 x0 O' k* w) f
server {% q+ W, q) o; ~# }
listen 80;. G7 k6 V) t. d1 M# a4 w
server_name 52os.net www.52os.net;
: u; z4 S/ o3 J( W3 n. E
# J" `: a! S4 a1 t }5 I9 \location / {) |( O( e. U0 T l. t
ModSecurityEnabled on; 6 N4 E0 B2 `$ k5 O1 S+ Y- h+ H- s
ModSecurityConfig modsecurity.conf;
; _! {" z% }) l/ K1 C, E9 h
/ _: A( \8 i% ^9 \9 x& o proxy_pass http://online;8 i$ Y) y9 S' `, v0 v" q' w
proxy_redirect off;
, @( S3 T% a! ^ proxy_set_header Host $host;+ z5 _: f6 u2 W4 X# I
proxy_set_header X-Real-IP $remote_addr;+ [8 E! g W1 y& r6 J: h% R
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 d% j! S$ t8 I7 h7 e }5 ?! a, h2 k5 d- J6 q
}
; X ?4 b- f4 Y六.测试% |) O8 K) c! x3 [* V% B6 n
( d5 X. T8 q) e) k: c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; Y- G3 ~0 G/ y9 G% `- V0 F
2 O" F$ d. `! G<?php6 O, b: G- y8 N) ]
phpinfo(); 2 A- h# z4 }6 Q1 Z2 A7 b T
?>, w' l5 Z- J1 ^! @$ J, m
在浏览器中访问:
. N3 \4 ]2 Q+ ^* Z* t- r
' p- @7 N+ v2 ~3 a8 x# @http://www.52os.net/phpinfo.php?id=1 正常显示。/ z% U/ n0 e% C5 Q3 C( ` f, ~& B2 E
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 s& Y4 I$ n. B9 A1 Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
- o4 i/ ], n1 y* T! q说明sql注入和xss已经被过滤了$ y- r5 h7 t+ j; J7 N, i4 n& x
# [' S" A. i) D% H* B
七、安装过程中排错
8 G% L) `1 F8 F; j) I9 S' u2 J% }! p+ q! P, v) E. \# O
1.缺少APXS会报错# W3 X, j3 q3 i9 P' F& {' z) D( b6 A
8 l2 t, S/ F1 W) l( iconfigure: looking for Apache module support via DSO through APXS# K6 w3 l" }6 Y6 m. ~6 q S
configure: error: couldn't find APXS4 M$ |8 C9 v n5 Y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ u5 {# r! k" D8 G4 t" D
解决方法:2 ^) y: k, n- A, v3 ]
9 i, S6 A5 t7 y |% Z" g
yum install httpd-devel. x6 t' E7 J8 r& j+ k3 p
2.没有pcre/ Q6 C. H. ?- @( {1 N; y
) v5 _. J) e1 uconfigure: *** pcre library not found.
3 z6 j% N; y$ S/ m6 ? k* L) Cconfigure: error: pcre library is required
# G, e( h- K, Z: K解决方法:
* t1 G4 O7 P0 c) E, V) @1 b
7 @) Y" h8 t& R8 G5 s% uyum install pcre pcre-devel5 t! ]9 u+ B- e, m
3.没有libxml2; }5 r: i: l! l# V/ Q% V$ g
- D. |$ {+ e, i+ X( @% Z
& d, q# |( o6 y7 nconfigure: *** xml library not found.
' P/ K: E9 Z4 M0 Q* \1 p* e. w( _configure: error: libxml2 is required' g: M+ ]6 s6 s M( V
解决方法:/ T% Z& R* C& @# s! q8 C- \% H0 H
6 I6 U* A) Y6 f( P7 }1 U- Z
yum install libxml2 libxml2-devel' k# b1 Y% Q' k- N- G8 n6 C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
; A! r1 e2 }# M4 F5 i
+ z4 V' W" i8 d# W* b8 _& w8 r% zTengine version: Tengine/2.1.0 (nginx/1.6.2)) y0 K5 y% \3 t/ L8 f, r+ O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 U( `/ u6 @5 f6 N原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 I0 R4 _2 W! g" ]
+ P5 f8 b& }8 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( L2 Q* D; M% i! j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
7 k, h' }; h7 K4 V3 I- B( I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ y. i) \4 Q6 e. {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". s; O7 w7 f6 W7 ~; [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% @- a0 C9 f) H i' m! O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." |8 J9 P$ L' j; R+ k
解决方法,移除低版本的APR (1.3.9)3 I. r8 x% i6 N
6 s* `2 s# D9 _2 E, x, p+ h
yum remove apr
; b! e9 Z: p; O; |9 V$ a7 l5.Error.log中有: Audit log: Failed to lock global mutex
6 d8 N# m+ G7 G+ u9 K* L' k
" ~: T4 y, g6 v6 B- C$ ~2 |5 p2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% i1 m+ _( t8 w- R% z4 Q, |global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 O$ D8 N$ ^1 a! c( T5 H
解决方法:+ Z$ [% k: E: @! V& z2 T
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ p8 W F& T1 r& Q3 p6 h
. k% [+ Q' J- l& {% s3 xSecAuditLogDirMode 0777* J) a6 c: w+ h
SecAuditLogFileMode 0550
- N: y; g% U8 p0 A( sSecAuditLogStorageDir /var/log/modsecurity1 c! j& r8 b# g) `3 U$ a, g3 W
SecAuditLogType Concurrent% B3 j& r, m/ r7 U% Z
参考文章:
$ f# H, }+ q' a( bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) E1 Z2 c2 h* H: ?% J
http://drops.wooyun.org/tips/2614 |
|