|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ c# v5 i8 M0 E6 [9 N8 i9 }. e
, C8 i6 D" H( \, T- s, }一.准备工作
$ C) Q9 l: h, f$ U& j k2 P
, v1 y6 R2 u! H; r1 j: ~系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- R3 U1 a6 z2 v3 g( G
% `- F3 P" D; `! mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- U: h% @1 L/ j1 X7 ?9 N( D
! [$ ~6 |! U1 c+ c7 X% f8 p4 G/ S: dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, e3 Z4 H/ Z; S* T) w K& N! S g. ^5 }: P# h0 x$ c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! J' g* O& p8 A) b! V
1 w- S* m6 e5 o& l! p依赖关系:6 ]5 j; s9 g. m% Z9 X" t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& y$ N- D2 O6 @
9 S0 W- _( }( ?/ _( K3 B
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
) C" D( ^, b9 k& ] z5 k( mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
K6 [4 H |8 c$ e# |% F( l7 O: o2 w- f6 o' Z+ j7 P* l
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# i5 m, W D+ i0 u6 k
二.启用standalone模块并编译/ s# q% @* q, ?+ D
9 Y. ~) s( a: _9 E8 `/ `2 u7 K下载modsecurity for nginx 解压,进入解压后目录执行:
6 f2 ?+ G. a' k4 r& N- i1 X8 I E# ?8 b+ l1 E
./autogen.sh5 ~7 I8 m0 n5 z; r# _! Y- ^. h- |5 D
./configure --enable-standalone-module --disable-mlogc
4 ?1 X1 m2 l' Kmake
1 i" e; V0 a' K9 X. }0 N4 a& r/ a三.nginx添加modsecurity模块, i3 ~2 `& X7 Q9 F
7 }& d+ f; w) Z& b0 }( v+ ~7 r, b' i4 w8 P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' @. m) [1 e1 a- R+ f9 D
5 |& p. ?( f+ H2 n3 D) I& ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine& ^: [, Y5 J) M/ M
make && make install
Y3 x; h3 H1 t四.添加规则
! C% }% `7 D* y3 Q6 x/ U9 u( n8 g9 A1 b% V+ t7 b) J
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; L. F8 Q2 O# o4 t' ]1 _
7 a) ?; [ ~8 S# ^/ C$ f! M1.下载OWASP规则:# p* N3 R7 @) S6 J" F1 t: k5 ]
) [4 ?; D& g! ~, C
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! H m4 N! V6 l# K
- z( {# ]! u6 I
mv owasp-modsecurity-crs /opt/tengine/conf/6 \$ y% ^* e1 X; e9 S5 p8 x) i( u
% q, o) }4 T w& i( d% R/ }* D/ L
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* U2 M- C" n& L8 A2.启用OWASP规则:& P. }4 X: f5 V0 }" n
% D7 R# Z; o! U4 u
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( {' Y) l4 ~7 `* P
( c1 d. v+ g3 r2 S, m D6 V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; v ?& a4 Z! W
, N$ W" n4 x. T% w: W8 |4 Y1 i) rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* x. o8 [3 x* n( |& S+ K
* k$ c6 W6 x& j2 Z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
C# T3 I9 e. x$ g/ _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 Z; y8 e3 _* }8 U2 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 ~- V3 [. G) q: @, ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; n( o/ {' i1 C, |0 O& V& C; F2 ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 p$ m) J8 s# _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; x$ w0 p. p* G9 E- SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; q/ [, T, x. I1 ~- b) c" \( j2 E五.配置nginx
# {! p- r/ ]) Z5 N* W- ?3 p, C; M
+ r% H; L$ u8 C( Z在需要启用modsecurity的主机的location下面加入下面两行即可:3 n9 b' H- P- v
3 i" e* y" z) A6 J1 i! A+ mModSecurityEnabled on; : Q+ H" P2 |$ t+ O% Q/ j2 a3 ~
ModSecurityConfig modsecurity.conf;+ Q5 h7 }( `# l5 ?7 p5 I: V
下面是两个示例配置,php虚拟主机:+ T" \0 f" a6 D# u9 C8 h
. f |3 B8 M2 \" u" I: Oserver {
8 k4 l( k1 E" Y9 E; o5 @ listen 80;$ y/ K! e% B, w) \4 C6 R, m
server_name 52os.net www.52os.net;
" k9 L) s! u0 C. A, L% b8 ^
# V- p/ K8 g( @* `4 X7 _9 @1 d* Y location ~ \.php$ {
$ J! B# ~+ X) _# T8 C& k ModSecurityEnabled on;
6 B [1 M) L& f. M, d, z/ D ModSecurityConfig modsecurity.conf;
8 y' U8 ^9 R6 O0 I6 z2 K% f; b
1 ~$ \; ?1 n. }* R6 l root /web/wordpress;/ i6 z1 n' h6 x8 c- |) `
index index.php index.html index.htm;
; s8 R w9 g# ]) g3 y" k
0 q# n p& D) ~6 y6 p fastcgi_pass 127.0.0.1:9000;
8 S! j- f: X% }3 ^7 [ fastcgi_index index.php;' N8 F9 f/ i6 o W; o5 n
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
F) T2 y( _0 @" u/ D: @ include fastcgi_params;' C4 h& x# G# i- } ~$ N
}' h& ^! F1 x( ?. o9 e. a0 Y
}
& H- g8 p6 i( Z9 b! _ z- Fupstream负载均衡:
, t& \. b- w0 @
* K8 y/ N. F7 A& E4 S8 R% Lupstream 52os.net {
% k, w* b4 D. ]" ~: l6 V server 192.168.1.100:8080;) `( _, k2 f6 _) E8 J
server 192.168.1.101:8080 backup;2 Z1 J- t2 j* {1 Q+ m V* z
}1 Q4 o2 O8 w# E6 ^ l: h
! B! c8 F. [, L, P6 R$ }1 C" |
server {
4 C: ~* l- ^. x+ ]. clisten 80;
& ^$ ]! b; Y, A g0 k2 @server_name 52os.net www.52os.net;
S+ S' u2 `: V* D3 K0 a: G" a2 A$ Z% l5 [4 q8 n
location / {
4 `" O" z1 S9 N3 l ModSecurityEnabled on; 0 w( Y7 S, Q. a" V- c: l
ModSecurityConfig modsecurity.conf;
- Q0 i3 O% n, ]8 {, I: C; b
4 P3 ?, k% ?- C" s' k' k proxy_pass http://online;
/ X# m8 o( S/ z9 d) \ proxy_redirect off;7 a' c4 X; J& W) c4 |
proxy_set_header Host $host;) F- A1 `/ y, X& Z Y7 P: w% y
proxy_set_header X-Real-IP $remote_addr;4 a4 ^5 L8 r- Q T9 q
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 c0 y+ u' A2 T& u: ]. H% d }
8 h' V* P; E' [. o: q4 z( V$ O}2 C$ S6 m4 n3 i- q2 B! B
六.测试
+ Q7 T' C5 u# D X
1 t# K- \+ t6 m- c; o# t* W我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 Y3 t: {/ @. `3 t$ c0 t! F5 }- w, A
* `+ C$ z" O1 [! [% j' g2 a<?php: ]! B' } ]" n( C- `9 M% v* O
phpinfo(); 8 |+ k! D9 p1 q4 D8 x; J8 Z
?>
+ H7 E0 Q' w# h- N在浏览器中访问:
5 _" e, m9 R& o! V6 c' c# C2 g" H; r, J$ M5 J
http://www.52os.net/phpinfo.php?id=1 正常显示。- U0 l5 d% I7 |1 b ], p1 e; D
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: o- u% F( {6 r Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
- Q `3 @0 e6 M7 X5 s! Y0 S. a! \; O说明sql注入和xss已经被过滤了( u. J W' @* ?$ |& Y ?6 o
& W* P# A/ W1 d" N, l d# S
七、安装过程中排错1 j- {8 R) b5 d3 B
8 R, Z& ]1 f+ V+ d0 D; ]0 _1.缺少APXS会报错
) ?) f: `$ c2 E8 `2 o; R$ H! s- i, a3 Q [; G6 \
configure: looking for Apache module support via DSO through APXS1 a2 l* V8 e9 w+ f! q
configure: error: couldn't find APXS
g" N+ c' f+ N3 ?$ Japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ O9 m, l7 {+ Y. r- U
解决方法:
0 ^& `. o8 D2 ^5 B2 j% q g- u w3 m# A2 U/ m3 D6 o! u0 A
yum install httpd-devel. N3 o( o5 r5 j# A/ Y
2.没有pcre
: e- W1 Y8 `% K0 p/ T8 k p1 m7 Z4 l$ I a3 I2 G S' i
configure: *** pcre library not found.
( S* ~1 }+ M* c) l# {, P0 l/ hconfigure: error: pcre library is required7 }! |9 x; L, I/ I6 ]
解决方法:
# P9 k4 `) E" |+ b! T0 E( @3 x" v% ^ e- W9 {& _
yum install pcre pcre-devel
# G7 i5 l$ w" N! c( V F8 ]3.没有libxml2/ b+ a. \6 K) w1 {. n3 X
' D7 I9 e& A5 R: Q& ]" P A( l% X* U' S/ B# Y3 A6 ?! ?* ]
configure: *** xml library not found.
2 A& o7 _% i( ~configure: error: libxml2 is required( `2 H( i7 |7 O# {( @3 N% d6 j
解决方法:5 k% C4 A! U, _' Z" P7 p
5 Q9 u& j7 G' e1 @
yum install libxml2 libxml2-devel
- Z% g) m$ p; |" M, Y4.执行 /opt/tengine/sbin/nginx -m 时有警告5 A! H% ~& W2 Y' [
% j1 O. g* g* X7 H- A: s
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ |' ] _7 [# S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!; `5 @' \5 G7 |* S# E8 ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& F. K- z! X+ P. n; f6 S
; w; X; x9 H* Z2 M9 w" i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) [9 G+ }& t8 e0 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" c7 d a8 o, A9 z* w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( U" Z0 _- j3 H/ R$ f8 j( Y" G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 [% x T9 u+ S7 a5 b$ f4 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 A! A4 i" N3 o& |/ Y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 F6 o) [9 R9 y7 l) T$ b* C. y解决方法,移除低版本的APR (1.3.9); S* t) K9 Z; G, ?2 F4 Z0 ]% ^7 S
, l6 x9 q6 e6 A( o9 y! Yyum remove apr
C5 O4 c$ m8 p6 P/ ~5.Error.log中有: Audit log: Failed to lock global mutex! _3 A4 P' N3 h! `9 d' C3 P \8 P
: E( X/ t0 d) ~! \1 p, B @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* e; }1 B2 L: fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 t1 r4 V; N$ z: L/ u" \2 f S0 @解决方法:" f! s9 l- {0 b( R
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- n1 U2 X) @! B- U( s; k* C. O; y: O! Y; n% }4 p
SecAuditLogDirMode 07773 j4 U7 u" v. l( g. e/ r& h
SecAuditLogFileMode 05505 Q4 r3 N. _8 E7 l1 I
SecAuditLogStorageDir /var/log/modsecurity
4 ]% n2 r0 T$ `! k2 aSecAuditLogType Concurrent
: q B$ U* z) ~5 ]/ ?参考文章:7 c$ w8 W! S6 w+ u7 \$ y; h8 R {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, X! r: l9 \% v( h* E9 Chttp://drops.wooyun.org/tips/2614 |
|