|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ Q+ @ f g/ I5 Y, @# Z
2 ?! B. | S S3 r一.准备工作! f1 M- W. n2 V1 B b, @* h
% `2 x2 ]4 t' W* k2 }# O: {" I" S系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" u* k$ m8 S7 A% z! x- F
2 a6 \5 ]$ S. W9 atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ G" B2 B! s4 M% F g; H% i6 r+ D+ ~: `+ j" ~ n2 ]( A# f
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
j/ @# D0 n& A6 W M2 W4 }. k8 l" u$ Q+ t
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 ^" n5 g# x, g5 v1 l S5 f* y8 N4 Q$ O3 c& s, V; C
依赖关系:) S" P+ w$ `' j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 K: h/ t- r) E9 j# g; v- L9 T+ H5 B# W# n1 D) {2 |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
, G- }8 Q3 i$ S6 ~modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 |. h- T! {: {6 U! ?
: {7 W; g, }; Q* ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 ^1 g. W! |( h2 e+ W0 v% E二.启用standalone模块并编译
, \4 E. w3 E3 _% O6 P% T; r; R3 t4 ]) J( e
下载modsecurity for nginx 解压,进入解压后目录执行:( t) [* W& ^: @! U3 q" m" h
" m$ `, v3 Q6 B v( G./autogen.sh: [$ T" l. ]5 Y1 f! p: S( ]
./configure --enable-standalone-module --disable-mlogc$ O* ^$ ?$ K ?
make " H& V+ D4 }( ]# X
三.nginx添加modsecurity模块
& q" @7 x. h! a) J: M# ~# U
4 ?. D) i8 T* t: E9 V% J$ c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" X6 p0 i( x/ p: V9 u3 T# z; z# A/ d8 d0 D0 f, v& w( y0 F* B, m5 |5 P- L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# j9 ?' g6 }, P$ B3 ?& _
make && make install, ?0 H6 X a+ v& \0 A
四.添加规则6 k. m* y7 _6 v
3 P% O* I: F; ]# Nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* ]7 I0 m, `( W c
2 e u: F$ Z- ]3 T7 i1.下载OWASP规则:$ C8 Q, X1 n+ V' y/ [& v
& K% g0 v( T- ]. Z* Z" ^
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 S! N+ b. W3 p& [* e( c) l9 N3 K
$ Q$ k$ k7 E+ C+ M0 j
mv owasp-modsecurity-crs /opt/tengine/conf/3 I9 b O; A1 z: a' T- n1 z# P& A7 Q
- _7 P( n$ A4 e6 {- pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 s& n. O8 I# C4 Z2.启用OWASP规则:
* u$ p. y1 A6 y+ c( I7 z- x6 e& [- Z& q( _6 g9 E+ h) O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, o. a0 g' ^; V3 L) D: h- P0 D2 A0 {, J7 H) q! g& _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( b" ^8 V d2 p8 k; L4 e+ s" h7 c1 H6 {# R6 R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 n/ z% o. g" ~3 F8 H
- C! ^0 y' R! [. l% s! zInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* P+ J, e% m3 U4 j" k- ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; U7 X+ z @6 d' y7 W; X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, L8 g D4 y! N0 N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 l8 R8 N5 O0 r. D6 {8 fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
, \+ _4 ^5 J) Z/ n: r4 MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( W1 `- L1 I6 y5 \6 l9 o
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- Q9 M" N3 ~( N6 S( R$ t- O- q
五.配置nginx
9 s; E, M! y) e- V- u
" j4 ` i8 |/ Y3 R6 B在需要启用modsecurity的主机的location下面加入下面两行即可:
) L! h( X$ [- f- N/ U1 g
7 }9 X4 p9 J' F9 U U' bModSecurityEnabled on; ; v0 _" E% V& |; d3 i1 T
ModSecurityConfig modsecurity.conf;
& `* a1 a5 c3 o- s6 r/ |$ }下面是两个示例配置,php虚拟主机:
; |, T% } g0 _, e! Y
5 P, J e6 x/ I! y9 A( hserver {, w) G. k! @6 e& q* a& U+ N# t5 N8 i
listen 80;
2 h2 Z+ C% T. k3 ^ server_name 52os.net www.52os.net;4 N1 Q$ E: B8 E) E
! ]& m5 p$ Y9 A0 o
location ~ \.php$ {6 v, Q! i+ z2 B: A
ModSecurityEnabled on;
# r( e& u) b& c0 b* ?7 S; f/ k ModSecurityConfig modsecurity.conf;
1 x$ B) r# \$ T2 Q% o' Z5 }5 k6 o- J; ^ ~1 z9 C
root /web/wordpress;
6 V( `% x4 D6 ?2 u% h& O index index.php index.html index.htm;/ q4 g a4 o+ t7 B. i
, `9 r2 I$ Q! O fastcgi_pass 127.0.0.1:9000;
" B5 V; i4 G3 W _- P5 F; z fastcgi_index index.php;) n) v( f9 ?( h* d, p4 T* Z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;2 e% T$ W/ |) C3 [$ n( F* @
include fastcgi_params;
. J( L' W/ T0 S! ^8 x0 \0 G }3 z6 \) X; M J, d
}! P# {7 J. f. d5 Y7 _ u- W
upstream负载均衡:$ {6 [2 o9 Y4 v. G/ K" h
- F# m8 J# r" }0 Bupstream 52os.net {! l' _7 K* d3 \7 D$ M
server 192.168.1.100:8080;
- q! D5 ~6 F' J# [+ s server 192.168.1.101:8080 backup;
9 r* d) }( f/ ]+ c- Y4 d}
* d7 v! _, e& R5 j$ f6 l, `/ `* z+ c5 O% e
server {
, Q- x4 l3 `1 A9 ^5 ^3 Jlisten 80;2 b5 ~* p' f* `# q5 E
server_name 52os.net www.52os.net;, M6 W; H! Y: ^$ _5 ^2 W7 r g
: z! p6 l. v* K
location / {, g6 V( h1 T. Z/ |. Y5 g
ModSecurityEnabled on;
, Y# H. A S2 j# s/ E ModSecurityConfig modsecurity.conf;
/ y2 y; Z4 N3 I; z) R" k- b/ R7 |- I6 c& `0 K3 N8 M
proxy_pass http://online;2 ?% a8 c# U* H1 ~$ N7 w
proxy_redirect off;5 M! W5 B& ^0 ^& n2 d K3 P0 L
proxy_set_header Host $host;* n& Y1 T3 l8 D# p- B0 R) c0 ?! S
proxy_set_header X-Real-IP $remote_addr;5 D/ g$ P6 L) T" [5 ?5 D
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 O4 f! r. ` e Z" V
}
8 F7 x; S3 ~2 H4 K u2 l}0 r) F# [7 F7 K/ m6 H& b5 d, g
六.测试
0 V5 e t4 |& ]1 j+ B/ `0 `" a
3 s/ n* n o; o5 [! O7 X+ i* Z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, W, q: I6 k, B
8 o6 y7 B; U. b6 c. x+ L. `5 ^
<?php, s, g6 a5 |: k4 ]% |- C7 `5 ]
phpinfo(); $ N- p! u3 a5 L& E. `
?>
' \: ~8 p' G$ e4 x& b1 p }在浏览器中访问:! w1 Z% l0 b/ j9 P8 k9 X
0 u8 C1 F2 i8 F! i2 L9 phttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ C- t5 w# ^# J: k; y: Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 @& x: m8 L4 u3 C9 H7 Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( [/ [- v2 Q3 `& Y! |说明sql注入和xss已经被过滤了
, c' A1 @4 W p$ q# |3 K c& P9 _2 \
七、安装过程中排错
0 {' e% U, t6 i% M
* P+ D5 f) r( r. M' ?% |5 u1.缺少APXS会报错
: R4 S. ^. n. L5 `2 b0 p5 w# j7 `4 [' i- v, p
configure: looking for Apache module support via DSO through APXS
0 O( t$ L! h6 P+ e4 Z# jconfigure: error: couldn't find APXS' h' l3 t# C- s, q+ H- S
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ i2 O6 z/ R& b0 r2 a& {- Y0 | r
解决方法:/ ?& W) G# E+ Y1 \1 u
]6 Q6 a: l/ p3 S7 B1 C6 Jyum install httpd-devel6 R& g0 i- B5 T1 h1 s) v
2.没有pcre
# S1 S" K3 q; g3 e" B' a
# u9 T3 d+ r* B/ V# Fconfigure: *** pcre library not found.# \4 Y9 k, \* n$ [5 f7 J+ n" ]
configure: error: pcre library is required8 {8 `% A4 e, o
解决方法:
9 v1 f+ N1 T0 t9 u5 c+ B
" T. ?+ r+ N/ ~, |7 byum install pcre pcre-devel
) n) s6 w" I$ [5 x" g* K2 w3.没有libxml20 _8 ^- t: w1 K1 D: O, {4 R9 B; u
, |3 f1 d2 Z* M$ _* m7 ` D/ \3 R5 w& v: l1 @8 U
configure: *** xml library not found.
1 o" w2 H7 }- J( I5 K1 |configure: error: libxml2 is required
( P6 {' S0 D- `2 T9 S6 C解决方法:8 {# h1 C3 R4 S0 O) d# @ y1 ]
0 L- T7 `. _" _" W3 |' E- l, g% yyum install libxml2 libxml2-devel- p: w' B- t' p, A" u9 c5 R
4.执行 /opt/tengine/sbin/nginx -m 时有警告1 _4 t/ L/ Q8 U+ a
8 I( z" j' t. \ B5 a( f) x+ ?Tengine version: Tengine/2.1.0 (nginx/1.6.2)# A, @) y# N# o* k, L7 o: K# Z/ G
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!* S( R5 X' c, t s2 ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* G H/ @8 B! l
) J8 }% K ^7 e* q8 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' O9 |7 W0 {! A# [' K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
& ?9 E/ m! K# P5 O' G: C2 r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, y( a2 v* f* t# H- {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 m7 W# \ {& M6 S# l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", l6 L3 G: {. {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On. U4 @) A7 c% g- C8 {7 @ F
解决方法,移除低版本的APR (1.3.9)
# R6 t# u/ U+ n! D: V! Z0 n: P, p$ ~& N( n, D# g0 b& _
yum remove apr
8 w# |0 B0 Q" u. n! m. b4 i6 a0 q s5.Error.log中有: Audit log: Failed to lock global mutex
5 v' v/ @7 d3 A w( b ^) C. M7 h1 i4 [2 @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - h6 c# p- h/ ~: i: p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! K; O" t0 E' M1 G- C% A$ `1 Z解决方法:4 T8 ]4 n. j+ W1 ^: G
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 q/ U: R4 I( V4 I/ R+ @7 k) J/ Z0 `: c, ^5 q
SecAuditLogDirMode 0777
" G, Q1 A$ h' n' S0 TSecAuditLogFileMode 0550' ^( z7 O2 a: Q+ F% k4 l/ A) O
SecAuditLogStorageDir /var/log/modsecurity
6 Z, Y2 a. C1 `% M2 z) pSecAuditLogType Concurrent8 D" t/ U6 K2 O( _. X" D7 E. m# T
参考文章:
6 a) R* T& B$ j) F3 vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 F. v( l5 C, e0 Y
http://drops.wooyun.org/tips/2614 |
|