|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) s0 A y# t9 P) \/ w5 _
# M* [' L0 F: B1 D) A g一.准备工作 C9 t$ X6 C8 Z3 R
# h& W& r( J/ o0 l, Y; z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 p" g: h6 x5 I7 n$ m
- f9 z6 q( c+ i* `8 A# V" k4 Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' B2 p6 l( x- g; O0 G) t, C- f5 A$ Z" D) {8 |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# k/ U! z, W! \- C6 p$ K' w+ \6 a
% W7 J9 k: v- z# J2 o6 p) iOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- L6 R8 I- ]3 t% `: M# f; @
$ x% M7 R# R! g4 j) }* R
依赖关系:
6 u0 W1 t' N$ w9 |0 ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ `: _/ o8 X, t3 ?0 l1 y
4 h3 H+ D% N# c5 b Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel' [# k: t3 W5 t0 R2 h1 f
modsecurty依赖的包:pcre httpd-devel libxml2 apr: c& d9 c1 @7 {. f, x. Z7 w
9 a5 t/ y) P( [3 O/ ]( U
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# u0 o$ N8 u7 _7 @二.启用standalone模块并编译
" q( J# H! i( x) j7 O# i
# v' P9 Q! P, ~) Z+ ~$ k' y$ t下载modsecurity for nginx 解压,进入解压后目录执行:- A, I6 V7 ^; N4 M3 G- }$ w& V' a
/ H: L" }2 q0 g2 Q./autogen.sh
' ?- {4 K& i! g# J8 z./configure --enable-standalone-module --disable-mlogc, j% U$ s* b" R& m
make 9 Y2 l4 F/ N6 D4 r9 R
三.nginx添加modsecurity模块, L+ D; E' `: z7 b& v' v
. ^( v4 }+ S4 `1 R在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" W7 |- A$ {! _( ^( d/ _3 N
/ j6 \7 u2 d$ a: X3 }./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' B9 y+ O' y' o" f6 E0 t/ S4 Y
make && make install7 P( Z) ]$ S1 ~+ \
四.添加规则3 s+ W, z \, D. ]* t K
( h! D: V0 L6 b" Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 g- i9 w. D) b4 l) }
% t& m* K% k r1.下载OWASP规则:9 a E* ^+ W, Z; E6 H
& A; q6 o$ L% f& v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 E+ M( d9 d+ U- w! { n D$ w
# a0 _7 }; M( E2 U' @9 ~mv owasp-modsecurity-crs /opt/tengine/conf/3 Y1 K' \4 \- L. Z% r* h# q3 X
. ~% k0 T+ V# W1 R9 K) O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf& t/ v' r" o2 p/ B8 \5 @
2.启用OWASP规则:5 G8 Y6 Y8 M8 F. o+ n
( D1 I! l4 _, _: u. n7 D4 U9 J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* r: P9 i/ x! ]8 H% h- N2 z9 Y4 s1 e
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 I( A0 a9 Z0 m9 F) }
+ I* W- d0 E: R' ^4 R. Dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, b5 p, F' m `) J
* B: u' d3 ` [" O
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- r2 T+ Z: f _4 k! p" b! ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
T6 p6 r5 N& A1 d/ nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 a" P( P" a+ C9 U. q7 Q% J7 P- O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: R& T% A7 b# b% T9 @+ | P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 L6 F* a9 x6 G' z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. `! b3 Z+ O8 Y8 D! OInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 c; ^- w( E9 `0 n- ^! v* [/ {五.配置nginx
9 S' z( y% z3 a; f/ `) J: y; u1 S$ A
在需要启用modsecurity的主机的location下面加入下面两行即可:$ a! ]" _/ |! G1 |3 k B1 @
) W: @& h" _/ d% |- I
ModSecurityEnabled on;
8 r r% Y) l( l$ q: ~& \ModSecurityConfig modsecurity.conf;
% j! J9 n) I, z9 m- C$ q$ b: p下面是两个示例配置,php虚拟主机:3 {$ y8 A$ F4 I( x+ f
. X5 r0 n6 }/ [ K& `
server {
9 r B' U: y+ |5 K0 i listen 80;
* Q& |7 I" a$ a# S$ Q0 ]$ j# d9 N server_name 52os.net www.52os.net;
% Z6 F g" s/ m0 f. | ' K% I% A/ ? Q4 I
location ~ \.php$ {
( q' g; }4 e- G) U ModSecurityEnabled on; 8 `/ J, [) i& n" p# k
ModSecurityConfig modsecurity.conf;
i" Z4 L0 ~, B3 |2 s- Y. B+ R( I* i+ s3 G4 W* S& t; H
root /web/wordpress;
- U, @/ i2 S5 e& j5 L7 J index index.php index.html index.htm; o- M2 h. p6 ~/ W+ A4 u
* ~& F; Z: F6 e" z
fastcgi_pass 127.0.0.1:9000;
2 m: S" M- {" j8 g3 ?9 | fastcgi_index index.php;
1 Q* Y/ Q: {7 H4 F' v fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 q. R: w! O7 t# M) y
include fastcgi_params;
" A2 a! {* x' q+ } }0 o+ j0 _/ S9 o6 U" K" d! y
}( @6 V9 l! v- m) Y0 L" `* r. w! ?* J
upstream负载均衡:
$ G- _4 V& z$ n' H4 A3 O
8 T0 \. ? D4 p0 A. ?% a3 n) eupstream 52os.net {
7 X# C; h. W( T1 B [1 K server 192.168.1.100:8080;
' O$ X& o2 c* x9 b. u2 F1 J' B$ l" y server 192.168.1.101:8080 backup;( k% e- s$ j* c/ L. l3 {% |
}' b1 h. F6 [8 j9 ^
; O0 Z6 o3 N \* d; Q/ X
server {$ @ H, H2 r6 g+ r
listen 80;
3 ~3 l% s! n& v; B' j, g ^! l7 Gserver_name 52os.net www.52os.net;
5 J9 \. h* p6 i& ]' F, y" s. c* u& d' e2 h& u( B7 j' \% z
location / {6 o6 l* I# S3 p5 |$ T9 F
ModSecurityEnabled on; 7 i, o% `8 }3 P+ ^* h/ M
ModSecurityConfig modsecurity.conf; 8 r5 |' {+ _ \& j2 N. r: o$ \9 X" F
8 D0 F* r+ v' D6 V
proxy_pass http://online;
2 K0 J: g2 A0 P$ ^4 z# A( k proxy_redirect off;
( C4 n, o+ u2 Y6 }8 d proxy_set_header Host $host;
- z$ m* ~# T) Q4 c% X, f proxy_set_header X-Real-IP $remote_addr;
E2 a" F& @' o7 o J* |/ P% J proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 B4 |- O9 U" U8 D }# e: X( M4 m4 d8 ?9 K
}
: v% _ B9 @6 r2 M六.测试
3 [+ f8 }# a1 J k& l2 Q6 f3 p$ T( r$ G, W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 g+ E! ?5 M3 M
, E; V5 a/ N( A3 S; B) g- h) c
<?php4 G6 D) ~. S* W4 |( |
phpinfo(); , M& q6 j1 ]7 i! `- b; D+ B4 }6 `
?>. j8 Y9 l; l' c+ L$ C
在浏览器中访问:
- f* G" s1 k( i1 A, l- e8 {5 M
, ]5 z6 O" X5 thttp://www.52os.net/phpinfo.php?id=1 正常显示。
% C6 a0 @- R; G4 Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& |2 ?. M! j3 {' W3 @6 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。# Z7 X* q; M" r# s: w+ {$ ]
说明sql注入和xss已经被过滤了
1 n s9 s9 e# q$ _/ b/ L
& k% j2 n# N' A4 K七、安装过程中排错
( J) H: U# e+ L( f
& D- F( R! _% c& g* A' P1.缺少APXS会报错3 v+ a6 W2 w$ h
9 s/ i; Q$ {' q; G! a# B2 Nconfigure: looking for Apache module support via DSO through APXS- j% o- n) F* X! T2 l
configure: error: couldn't find APXS
! H) X- }% J I: ?5 a" \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' y% e8 R) J: B解决方法:. G( a. j" r3 R
x9 K8 y& V! l8 v( G& H: e0 W& w
yum install httpd-devel
4 W8 g7 V9 t2 E: ~2.没有pcre
" ^! K$ f" l/ h+ i- S; Z9 v3 [% W% g7 n+ G
configure: *** pcre library not found.
0 M" e. }1 c1 k" y) H- Z% ~configure: error: pcre library is required
) T* k) n3 p" I6 X解决方法:5 d. {4 S D9 S' W i2 o
% Z9 Q5 x/ P5 W/ zyum install pcre pcre-devel
8 n% k0 n! p/ `+ Q V% H+ h3.没有libxml2
" V3 f8 Y1 l: q$ g, E+ @. @
2 N$ N" u/ ~& S7 |8 w. _- t% R1 ~7 e& D$ ~
configure: *** xml library not found.& Q& l L9 x; m @7 Z
configure: error: libxml2 is required7 ^" Y5 i0 \# w, z- i
解决方法:2 l8 `: I, G1 q- D
' y+ N+ H9 U. n4 r9 }* z, F
yum install libxml2 libxml2-devel
8 B# T- z* k9 c& |6 v% e' o4.执行 /opt/tengine/sbin/nginx -m 时有警告
" u' a) N0 Y! c' K5 i' E
/ `0 U# d' L3 rTengine version: Tengine/2.1.0 (nginx/1.6.2)
* k# r' C; R' cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( J: \2 u' m) M% C8 o" X原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 c5 y$ q4 N8 V5 K/ u8 O
8 K z& H8 [/ r- B+ D# U9 `7 P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, N' Z/ {! T3 B9 o: q* A0 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; g( n1 O. C! Q @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; [+ r3 [; K) j c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 @; g* O; h& K6 b' N/ \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& o- r: j$ ^! m4 F9 p6 N- n' C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. B6 w! F( T1 a& J; L4 O) y解决方法,移除低版本的APR (1.3.9)& w# n& r8 F3 p: |
$ e: J6 o) E" n8 v% fyum remove apr
8 }1 V4 `( f) N/ N! d' [0 d5.Error.log中有: Audit log: Failed to lock global mutex# p& V1 |8 i6 ?
4 A/ D' v6 e4 o! j. W- w1 n2 f' n% a/ ], M
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock & ~* e) v p9 X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 d$ W9 Y8 p5 `! X5 w9 X t
解决方法:; V% O5 Q* n3 C0 c3 V8 Y4 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; b {. b: [9 J1 u! u/ H3 ]. F2 L1 r" [& Z4 | B' g$ {2 a
SecAuditLogDirMode 0777) j. Y- H% e" K; |) i+ U
SecAuditLogFileMode 0550( M- I% Z( O0 B$ J
SecAuditLogStorageDir /var/log/modsecurity
7 V* t7 o @1 G/ BSecAuditLogType Concurrent
; b0 W% {# l) v/ b. h+ \参考文章:* G. {; @, ~/ n1 \3 F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 T% c9 |$ r$ h" S f' y$ P" F+ E- whttp://drops.wooyun.org/tips/2614 |
|