|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 \' h, M# N' v: m5 t( {
+ T1 l0 M# T+ I% X8 \一.准备工作
) m& b9 U4 j( S" S t
7 K4 M4 W% h8 O6 n( }5 c系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, r& j# b4 L! E1 ~0 [
& Z- r. \: Z' ]3 @( `0 k
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; B3 W8 o& u E q& D
! {2 T# H$ a3 j& f; l# W- o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 @7 W6 a; g7 y% N* I0 W2 m
0 K6 |6 u& f: V( VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 K! z3 m; N, u: b$ S3 n) v- r* k4 }" m- s- t3 X7 @" w. h
依赖关系:& i( j( _1 ?# K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 F. p8 n0 F/ P" x. T! k1 F
/ b+ y4 T( O0 z- H: M/ Iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 s4 X; z% R3 p2 Q- I7 Rmodsecurty依赖的包:pcre httpd-devel libxml2 apr- b7 J E0 m, K4 `; k" \0 D. w
' ~: K8 G" y5 Oyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- d4 n9 ?8 m/ C' I) E* c二.启用standalone模块并编译
0 g/ h: A. b+ z. t. ~* i# U! _+ u' c# H) T. W
下载modsecurity for nginx 解压,进入解压后目录执行:
' h; N. P* w- {+ V3 \
8 P) [8 v3 E) ?9 w* A, I5 y7 N$ u./autogen.sh2 b) I0 ^6 o h+ `$ a1 w" k2 A
./configure --enable-standalone-module --disable-mlogc. p/ {/ q8 t8 e+ Q% ~8 x
make 6 [4 p# e, R$ W9 @
三.nginx添加modsecurity模块6 k6 i' ^) d6 b5 P
) W/ S( w, _! `' s7 T5 t7 P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, K% e2 u5 \0 n# {9 y. j/ `" V
' S7 M" b ? Q. [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ i1 I9 N2 t3 D# X) `% C
make && make install! P, X) G5 e" X' F
四.添加规则
# W# @6 O* k% a0 s3 V
& m4 P0 k. C, x& \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& @* W! ~5 j) s
3 Q6 c) T: L s0 U8 A
1.下载OWASP规则:' }' ?9 j) u- Q- Q7 T
( y7 f1 d' \3 s: P% B- ^5 ] Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' F4 M% n' m& V
2 w0 i/ ?. P& A& gmv owasp-modsecurity-crs /opt/tengine/conf/. V5 E2 g% n; B
% K# ?/ W7 [, [7 b$ acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) u8 i2 V! q2 j- {2.启用OWASP规则:
# w: y1 `! f! B# T. ]5 n# W; i4 X* ?
8 [' o7 W0 M4 d2 t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: K# O: r- R/ z; D5 n
- d' y. I: a6 U( S3 ^. M' ]) g: D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ J, J( I: `0 X& y( i' o. }8 r) [: O7 |( ^
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' @4 F4 u+ J: D& \6 n
$ [) F% I5 z. {% gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' N# ~5 v5 p* b E1 z- Z% O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
_" d) L1 ?' n4 i; aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ R6 |! L9 f5 e F; V. y+ `+ n! GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# Y: ]5 m, H3 u# L, I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( D+ e' i1 Q: V9 ^2 E }% jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: F1 D: [7 h2 t5 M# R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 i0 y! p! s) ~) F# E+ S0 W: J, H五.配置nginx: K0 u5 b- Y6 H7 C
' y2 L- p2 c( E) Z0 e& M在需要启用modsecurity的主机的location下面加入下面两行即可:
4 m- W+ K5 j1 v+ f
z7 G4 P4 X' r0 S( t! C1 uModSecurityEnabled on; : b' c V7 r! e9 V- e2 g) f: [1 p
ModSecurityConfig modsecurity.conf;
+ R/ u4 h- I. t下面是两个示例配置,php虚拟主机:
, F1 A0 u# t# w* {1 \& k4 f& b& _; Q: U% B% L
server {: D4 p! ~% }: I) `" O5 X
listen 80;! q m0 S8 K$ V
server_name 52os.net www.52os.net;
- x, ~. X# _' T! @ # w5 c& }- y( _- }5 F: J5 r$ }4 I
location ~ \.php$ {
4 O r8 o* k0 I6 l' S5 T$ _ ModSecurityEnabled on; 4 z4 N7 N+ X% E
ModSecurityConfig modsecurity.conf;$ P3 ?5 j H# R! B' m
]# N8 b6 Z9 [1 \
root /web/wordpress;6 p& @8 |: E7 E$ X
index index.php index.html index.htm;
; o! l7 s! K0 w0 k* \/ T
6 B% _5 T. `" A fastcgi_pass 127.0.0.1:9000;
% f) _6 _" I# m3 x& V0 I fastcgi_index index.php;" _& M0 f& ]3 n6 c) |2 M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# g2 n7 L% Y1 Q* s' X& i3 i1 Z
include fastcgi_params;4 a; X! p n: I. q
}1 P* V) x8 W0 i# r
}+ K# i: u# ]# y9 U+ n' D5 ?
upstream负载均衡:
+ _* x, T& n; C4 V {; N8 e7 s
6 ?0 l' T. E9 a! G9 Qupstream 52os.net {
9 n4 j& _% ^& Q7 g) ] K( H server 192.168.1.100:8080;
@# J# v2 c! y$ K0 b* s' R5 u9 j server 192.168.1.101:8080 backup;0 T; v& [0 { \
}
7 S3 I$ i4 k1 Y" b6 K0 k
6 e; Q4 d% c. i& s; P4 \( Kserver {
7 h$ h' l: @( A' k0 zlisten 80;
5 e c5 h9 T% w6 Q( X* Q( W5 ]8 Bserver_name 52os.net www.52os.net;
0 j0 n( J8 b8 X, X# S* M- {7 P( p, N8 y& y: d2 Y% P
location / {3 U" l1 N' P* P6 W* ? G
ModSecurityEnabled on; W! Y% _, y* e( d8 ]: v
ModSecurityConfig modsecurity.conf; 8 d7 X3 `$ I! g/ ?5 ?1 c! n
& m0 X' @7 U. u" K proxy_pass http://online;
# ] D# l; e& e3 R proxy_redirect off;' H: h8 o# m4 M0 k/ I# L, x
proxy_set_header Host $host;
7 Y7 V9 \7 ~! h% H proxy_set_header X-Real-IP $remote_addr;
1 {3 I! }0 x. o proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;& z- D. H* F. N' e
}& k3 m1 V7 q) K0 ]2 A
}
1 ~) z- p5 u( R2 j$ ~5 C# A六.测试. v4 T; A& ^% u4 m- H! p) J: {5 Z
$ R% w- `& }- o- G% `4 w2 ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, B0 y6 h y* N2 e# G+ K& _0 g& B$ m2 W1 R- x/ Y% O
<?php2 Y' b. x( ^6 D m6 q
phpinfo(); 7 T5 k2 M: Q+ w# m
?>% r& a' E" b4 |0 w; L9 c) B
在浏览器中访问:; `8 X5 Q# ~7 m+ z/ ^
& v/ D3 B( O+ X) _1 ohttp://www.52os.net/phpinfo.php?id=1 正常显示。6 ]0 f* |7 @, D! s
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' D6 `( A# c6 ]' x" e& dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 ^/ G% W$ `: a& ^) B6 R- q
说明sql注入和xss已经被过滤了& j( v8 a% `1 ~9 W1 F7 R
3 F9 p _) H# F七、安装过程中排错. v4 T+ ^: u$ h+ @' x3 |$ Q, D
( y% ]# u! L, _$ S1.缺少APXS会报错
# R0 v9 _, |, g$ _4 D
% f' s7 m D$ j; I$ A: }6 ~) O \3 {configure: looking for Apache module support via DSO through APXS
5 Z4 h% y/ u6 O# Econfigure: error: couldn't find APXS0 ~9 f2 U. q6 w0 G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ ~( k% P3 i& X0 B8 M1 r解决方法:* }- k- Q. i( y* o% F
5 h% W, Y% Q# U+ @' [) b6 `
yum install httpd-devel% q8 ?! D) v/ b$ ?! z
2.没有pcre5 P f% u; c' w7 V* Q* Q
* h( `9 Y7 I5 l! _8 e: L+ jconfigure: *** pcre library not found.; h# G/ n+ @' J+ W+ e- A# w. U
configure: error: pcre library is required) O* f4 g+ I* d- e! A" @1 i7 G4 ]
解决方法:, b9 o/ h# v% I/ F0 q. J
: I ?5 a" |* q, I2 h3 C
yum install pcre pcre-devel
! \+ b, a! T1 c8 Y$ f" |* o3.没有libxml2 k5 U& j" c. A6 w7 U
- W% H; R7 _* ~6 X1 b# a7 j4 i7 i
6 n5 F* L5 R8 g7 n- ~5 {configure: *** xml library not found.+ E" v7 V; Q/ \/ {3 U9 N* m' l
configure: error: libxml2 is required
) u' b/ C+ U) r, I- C& Q解决方法:
- v- ]+ o0 J8 p$ H$ F% O
! m+ Z- p. _9 G; _) E4 o6 ]yum install libxml2 libxml2-devel& q3 l6 B' h$ A! {, g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 r# U" }1 i- j' [% k3 O7 O. j+ Q* M0 e6 X& }" X, ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 |/ F; H7 f1 N& {8 P7 z( I: znginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 k- V; \9 d" N( l# e- ?9 S) D; V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 \+ q; }, K, U5 t: a# f' u1 R/ ^
- l9 { i6 b; o2 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 L# U* L0 P& g- H# R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 i3 \1 }# c- }! `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 W) p% ]2 H. g4 T+ r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 f: ]( i7 Q* g3 w6 h" V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 ?$ V! `. [, W( ]
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 G5 C8 w' v i' T3 ~, ?! j
解决方法,移除低版本的APR (1.3.9)
: b. O G) R W; F2 t! c' t0 P( `. |& o, ], |3 B3 g; ]
yum remove apr
: C6 |1 Z! V/ {5.Error.log中有: Audit log: Failed to lock global mutex1 l2 f x9 O- [/ S9 Q. t8 ~) ~4 W# s$ h
$ L+ ~, [! j" w' _
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ' k. W# S- U* R9 y9 G
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. A% h) J. a+ g解决方法:
/ e# T R; Y4 N5 t8 y5 b6 M编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, W5 ]3 m, F, m% t
1 ?' j( l0 R! ^- q, X iSecAuditLogDirMode 07778 l p( b) v" w! g9 i& u c4 N
SecAuditLogFileMode 0550
# d( _' @2 j' |SecAuditLogStorageDir /var/log/modsecurity
, a8 c1 w# _0 l* s" U% uSecAuditLogType Concurrent& ~5 f0 ~$ G! \1 _: K# k8 y
参考文章:. w& }/ ]: v) H- a
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ a2 e: y9 y! D4 f, n# b
http://drops.wooyun.org/tips/2614 |
|