|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. L2 ]1 p6 F3 D: Q/ T4 X/ ?. N7 G9 u* k. x9 a
一.准备工作
8 E- l6 Y7 ~9 Z. j9 F) X8 }; u+ R* |5 [5 O- B; @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 p' u& V# t. @0 j) g- M& V/ X" f, ~" \5 \4 B
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. [/ j& d; y( y: ^
" D% ]( E, Y7 { W" C
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. A; L9 f7 b- v3 e8 D5 ]1 z
# S- P1 {' R; d- z" I% s/ h% U5 j
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 {( R; K+ `1 R' O
2 b" l. f7 h2 ]' q, V3 ]
依赖关系:& }. H: G \9 G$ f
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% S# b2 [9 C% s* O8 L
7 f3 r# |! H5 T: e' P; _yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
) y9 y+ e; Q t2 k8 i" smodsecurty依赖的包:pcre httpd-devel libxml2 apr
, {( ~2 M# H. ^' c. S
5 b+ \# t# ?% X- f- }/ Ayum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; z# D# j z1 P n- m& T二.启用standalone模块并编译
8 M5 X& z& L- n9 a3 |! J6 f; a+ X" I( a! r4 v+ A) r( V E
下载modsecurity for nginx 解压,进入解压后目录执行:; Q1 J: R: p/ |+ i, m
( e: n3 q4 z/ `: C7 W! P3 M
./autogen.sh% h1 c, k' m! U5 B; G' [" O
./configure --enable-standalone-module --disable-mlogc
& j" V3 X: L' u+ D% E5 {9 d. ?make 3 N/ e! u0 U2 F2 _4 y
三.nginx添加modsecurity模块 u0 W' l$ a1 Z) v) P( w
; O$ m; Y; Z) T( K$ e6 J: m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 @/ V- M4 S) c7 V" V5 l) Z! h
4 p f5 \ g3 H9 k/ T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 B/ Z3 l( d5 b5 S2 Y
make && make install$ T! n4 B5 N! l. E3 J' s4 m
四.添加规则( ^* c/ R" t, U
+ N0 S. k. `+ R8 b" o( E. z vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ _. \. j. z3 O; B
; y4 x4 {% l, G5 S
1.下载OWASP规则:$ k3 p$ b; y$ b
( J7 q, @( n( G& F% e3 }7 mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 J1 M' V* o6 f$ B1 ~( o
5 a! B. T0 H# S; G% H/ G$ Smv owasp-modsecurity-crs /opt/tengine/conf/
$ F+ A1 G( S0 t* N3 h1 s c
{/ ^8 X7 q) Z: tcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- k( l! y* R( f. y# x* H2.启用OWASP规则: N3 D( D* y% [2 F0 A
9 t3 M0 b' c. b" U复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 W* D o1 }2 k. q0 l
( x0 _9 K! u$ u+ R# r- a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& L/ T- B9 U8 p& i8 p
# O6 W' }6 D4 P5 O) y( c4 p
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, ^# q1 c! m8 u! x" Q
# ?$ u: Z8 `4 f" t8 W! I( a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) y6 X; L, H% {5 D0 {0 S6 LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 s5 |/ W( {, M' m+ wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 _3 e; X5 t2 @- ^5 X+ TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 P# W0 o* g" |+ a7 g2 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( b, y8 j6 `0 T7 |8 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" A1 f( o* c$ }
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 p) _5 t1 j0 {五.配置nginx, I2 }1 a3 ]9 D6 K" H' F7 @
/ S6 E! J3 H3 N) y( \5 q在需要启用modsecurity的主机的location下面加入下面两行即可:" s! K8 u& p0 ~& V
/ k8 n4 }" ]- h! O2 o3 B" ?: CModSecurityEnabled on;
) f' C) S7 \; a& ~) s7 ?ModSecurityConfig modsecurity.conf;
8 m8 |; R* V# L: H1 X下面是两个示例配置,php虚拟主机:8 w$ c8 T7 z3 }! P+ \2 {+ g9 f
9 l" O3 N; H6 h! j) `9 r
server {
, C9 A$ C1 D i3 w4 M Z3 \% s listen 80;
0 B6 U9 H0 v) G; I server_name 52os.net www.52os.net;
' ~' e6 X1 I" \0 L0 Y , W6 [3 K% k) t" q4 C
location ~ \.php$ {
0 J$ d% y8 ?% M! x ModSecurityEnabled on; $ S8 t$ Q' @5 J- \1 p5 x
ModSecurityConfig modsecurity.conf;- u5 h8 g, `0 b5 |
! c- r5 n f9 I" P
root /web/wordpress;4 `" E0 U" ~5 t- {7 c: k
index index.php index.html index.htm;9 S# }- H. @, _# Z% A
1 x: Q4 V' n6 L, {8 S
fastcgi_pass 127.0.0.1:9000;! r- q% @9 s% q/ E1 h
fastcgi_index index.php;* O6 N/ E' R: Y* B0 d* P% b
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& W% \7 M3 I$ |7 [6 p6 l include fastcgi_params;8 C; ~3 z* b) X$ ^7 Z8 s. R
}+ e1 J T2 K+ u& d) z; d% N& m& }5 I
}
8 ?- F7 B" k o# b% Mupstream负载均衡:& `) A; P, E* {' r& p+ u6 N
" w/ N+ {2 N0 E/ s, S& I! {
upstream 52os.net {- m1 C' D. w! L
server 192.168.1.100:8080;
0 O+ \" X; F# V6 _+ R8 d* P$ Y, _# b server 192.168.1.101:8080 backup;
- K/ a u% C2 s# n3 n( m# f; \}
( o9 K3 r$ r) E( ~( |
) a* f3 X: K' B9 f( t5 v1 bserver {
: T" L! q' @' X( ]8 K/ {5 N3 Q1 flisten 80;
6 A9 @/ f; D2 K9 Y: fserver_name 52os.net www.52os.net;
/ \6 v5 @9 v. S) n# t5 R9 b* c& ?( N* {" w) i6 w
location / {
! s& S' F( |% D# c% V5 C# B ModSecurityEnabled on;
" D7 R& N6 r' [* R: j- K ModSecurityConfig modsecurity.conf;
$ q$ {$ K- Q# ]# r; z4 M, d
$ N8 G0 R4 q( @3 J proxy_pass http://online;
% b. B0 ]8 V2 y& l. y; F proxy_redirect off;8 }6 g2 e- n& A
proxy_set_header Host $host;
2 M! i T- u0 n0 k proxy_set_header X-Real-IP $remote_addr;
2 _* Q9 K- ?. Z" w* { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; p$ x L1 n6 @" ~5 B4 O2 B
}
( d! Y& g6 X0 _' {5 X6 V. f# q# I: [} ^2 _8 l2 I. C: H
六.测试
" y) [0 g# b7 R# O- D' W/ j# \5 x( y3 x- C/ F3 b
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 y3 w$ ]6 o$ L+ v' P3 [& `4 E/ ]5 `7 L1 P3 H3 _9 @9 t* h
<?php
4 s( ]7 n- c" F! J3 l) P, y phpinfo(); ; v* j/ S% V& i
?>
" O$ f: o N' U. J. ^6 f: ~在浏览器中访问:
& m* H+ [1 t8 B8 x! _' [& {7 t) u! u
http://www.52os.net/phpinfo.php?id=1 正常显示。3 N8 h0 `# k6 Y G; g* z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, i/ m: y, c! U# ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
) q# T& }/ ]( t$ ~说明sql注入和xss已经被过滤了
; Y/ w& j) e; D/ p- p3 |. ?+ g6 ?# r: z) a/ h% N
七、安装过程中排错
6 J& _7 \ a$ k2 A
$ w9 O! |& G: N1.缺少APXS会报错4 ]3 u; Q. X! d& y
$ l2 T4 s f% y ?- L y M) S
configure: looking for Apache module support via DSO through APXS
9 v* E0 d5 |$ J3 z3 I- Xconfigure: error: couldn't find APXS
* s3 t: C- |: E2 Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 {. z) y5 U$ K- M& n2 j
解决方法:7 r( T2 _: Q( K2 T! {% n
a/ q" P! K8 M8 c9 O/ j9 Gyum install httpd-devel' G) G( c- }9 K6 F
2.没有pcre8 a. P+ b+ y A5 F( m @" r7 M
4 N& W+ l# `) K
configure: *** pcre library not found.# _' J w% X6 p
configure: error: pcre library is required
) n! Q% F1 g6 |/ ?解决方法:/ X3 a) {4 N- T) I5 U1 d5 _. W7 V
$ ]0 `# X1 S. L K2 y
yum install pcre pcre-devel: N+ ] `4 K" u3 H* I
3.没有libxml2/ E2 k4 Q. F! C
4 U, {: J1 O: k2 M8 C
+ q e+ f- h. D: v% q
configure: *** xml library not found.
. {/ T- A& s. O' }; Cconfigure: error: libxml2 is required
' ~( c& `7 D1 x2 Z6 i+ j& t解决方法:
# ^; O! j, ]" [" p- l) Q& J
2 o4 v0 v9 i5 ?: Jyum install libxml2 libxml2-devel. a) i: X6 H# t [; T' q
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 v4 u3 I6 u, ]( J; R0 L. M D8 l
2 G' F# @2 H" ?
Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 W& q, ^. f5 Q. Z/ J/ e5 l
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% o- ^# Q6 @4 j2 h/ L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 [6 R- p J" B& l; g8 n& v
! W$ x. A' d* a8 v; [' |8 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ N, B2 y* m. w" \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 |/ j3 p- i2 l. R0 j2 g% F9 s4 l2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; E: A/ o; T( [( B' @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ u- O9 `: x* C* D! g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", T$ \6 H! f9 ]1 w1 {' X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) w9 ~- N. @" r O; w' q: o
解决方法,移除低版本的APR (1.3.9)
$ s7 ^4 j/ x: {% \+ Z/ s3 c$ b
% S, |7 f G: K2 s- M) J( m- j- p/ Gyum remove apr' F3 Y% p6 }4 R8 i
5.Error.log中有: Audit log: Failed to lock global mutex
, z4 K; A3 x% e, s# j5 ?
7 y7 V& u0 n: c+ g* h! ?2 I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% ]+ L& `: i/ ~5 z! Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% G' |$ }$ P4 o6 c1 P0 L- `解决方法:, ?. F9 o( ^5 `# S2 ]9 N8 {8 |* p
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* y2 q) d* j. v$ d
# I2 F; _4 o9 u( h3 a' ~' w4 tSecAuditLogDirMode 0777. B2 C9 ^& ^$ p4 H
SecAuditLogFileMode 0550" [! j. D$ `4 g; }) d
SecAuditLogStorageDir /var/log/modsecurity
6 \3 Q: C8 {6 A: [. ]1 F) x" RSecAuditLogType Concurrent
0 U# {3 \: W( I1 X" Q( M2 r4 B参考文章:
% b' ^ U3 u0 S b* T- B# Uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- s. r. E7 N2 W6 `3 ?, _# {http://drops.wooyun.org/tips/2614 |
|