|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 o z4 s6 g) ^* |) ~
- f5 l+ ~- I: u2 S一.准备工作7 y& I9 f; n9 p# ?
5 H$ H. P: V7 p% P* b+ X! @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# L2 |4 ^4 j/ T/ t1 P- e
$ K$ i; ?, {, O2 T$ v/ htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- X- w& ?! o& G+ j3 T( @& k& U W! b, d' P4 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
3 @% k' }. G0 @. s0 f
, i) z- o2 s8 J$ \* q% A5 w# H- cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
A& B M! H5 ~ G3 s0 `% q# a: z! u
0 K: e: J$ s/ T7 \# ]( h依赖关系:% t& _/ d+ b5 c) d) ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% o# m- ^8 [/ C6 K$ W _; Z2 X- T3 v4 U
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel4 r6 D& \9 E- y0 U
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 ^1 E5 ~2 l$ F/ U
/ o. U+ `! f8 z" Q0 O3 c2 Q9 Oyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
6 I2 y" n z2 z. g二.启用standalone模块并编译
) b+ O5 u3 W* L/ X4 [ b
K: p. ?. j" k( p下载modsecurity for nginx 解压,进入解压后目录执行:
: v2 G- H7 e0 w# V, K4 r! {1 p. Q- [; X; W
./autogen.sh
9 R/ a& G2 l: V6 y) {./configure --enable-standalone-module --disable-mlogc: F. \/ @" n/ y( @
make \. V B& n$ X" `* s/ w
三.nginx添加modsecurity模块
' G9 ]) s: A# M# ?6 [# ~! {* A8 e3 f. T% \0 b' f% p' @9 q4 u( @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 j M. m% \2 Z4 d. K
]0 n* W8 [- n3 c. a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 v" |; j' L: P+ G( g* @% q
make && make install/ C4 P" _' ~# J. G8 X
四.添加规则8 v. v2 r7 {% g, N. R. ~
6 {! C5 b+ t; b- ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 l) b# f+ E2 n! a
+ ?% \8 w! d6 J2 W% d2 }2 S1 y1.下载OWASP规则:5 Q/ x. B0 Q0 W
/ J- [6 {, W* C- @
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' w! G+ z7 Z: N- s5 @
6 {4 u$ N P Y0 ~! k8 ~# nmv owasp-modsecurity-crs /opt/tengine/conf/
% I6 R) X) E5 o6 F* s. a2 K! f7 L" y# J2 {
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: ]/ Z z1 H( C: x% H2.启用OWASP规则:
! [# D' m! @2 `$ M: C- I
9 q( p3 {. f { a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' y3 m& T; F1 a
2 h+ K& r0 A0 X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 i0 t6 \0 Z8 f; B' G$ e# B
! G! e& w" U: b" Z9 K
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) f$ v* ?6 n. f4 K
4 F. D$ n. R+ }/ P8 k/ J4 cInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% V3 u8 ~, \7 T$ k- ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 H9 o! X4 D E$ v7 Q+ K$ iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 T$ E1 Q1 g- R2 M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- W4 X: x; z2 o3 r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: G: a- w. A* O3 L& aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 _. D, K. B: t* g% O2 ~4 u4 sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ l) m' H* T T7 s; e: {1 X3 n
五.配置nginx
$ h6 ]0 b. P! w! L& d. a3 N2 H/ ]
% i) J' ^. N4 v* `7 s. V在需要启用modsecurity的主机的location下面加入下面两行即可:& {6 O& o& T' y2 m8 k
% a2 p' D/ G* d$ Q0 v& T
ModSecurityEnabled on;
! e; N8 g+ A7 H; o( Z2 YModSecurityConfig modsecurity.conf;
5 e) S/ u$ Y2 R' M1 O0 {9 H7 u下面是两个示例配置,php虚拟主机:8 K$ A/ k+ Y+ a: R: J: p
/ X- @6 Q; K4 A0 H1 E, A5 xserver {
7 r% y7 I2 ]9 o ]! l% n9 \ listen 80;
' K, t! N/ V( @5 H2 r" I/ ?: q server_name 52os.net www.52os.net;
" K- v! H" V1 x$ f0 W1 k% h
" d' l0 ~1 @5 J+ b$ F location ~ \.php$ {9 e% G: U$ r9 Y
ModSecurityEnabled on; 6 t( d5 R% E9 e% U
ModSecurityConfig modsecurity.conf;1 [9 s: u" c' J! d/ M# V
1 O4 b+ D4 z! X0 {. J# M* E( I
root /web/wordpress;: h( s0 a" j$ }& }& z# a8 |! ^
index index.php index.html index.htm;* B2 }! G6 j1 |
- s6 _& W I, X
fastcgi_pass 127.0.0.1:9000;: r2 K7 e- o1 @+ b' D$ z
fastcgi_index index.php;% L. T* ]) K8 H$ z2 R; Q) a
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 I; L4 P) B0 C8 d7 V9 f) a: z( }& h; Y include fastcgi_params;
3 v# K6 c' b2 E1 r" P }
2 ~* x/ s; c/ B& G* J9 B }
! y! y% R- @" i' Yupstream负载均衡:/ u! n. _* x& A; U
, @, } @# A5 ]% ?$ K# Q; i
upstream 52os.net {8 ]3 w3 \" `" m( Q) j
server 192.168.1.100:8080;0 L1 E5 f5 s7 P2 q
server 192.168.1.101:8080 backup;
0 a3 I/ t7 n: q2 W' Y% v" F}
$ m# G" ~. K x- M$ p6 S
5 z. A9 K6 h* Q- M( dserver {& `$ M* l- p; x0 s) Q# _: }
listen 80;
+ x, b5 h; n7 l: Zserver_name 52os.net www.52os.net;, ^/ u" F/ d( W' V T& {
9 L- w3 r! F, d6 J2 q- ]$ h5 j
location / {
% }9 q4 y: a5 Q% s+ M0 Z& } ModSecurityEnabled on; - d* q; h% J! U+ }
ModSecurityConfig modsecurity.conf; . V# ^3 g& m% W0 [
& `. @- o( ]( Q; G# B( T
proxy_pass http://online;
$ y/ i$ @" V0 y3 @ proxy_redirect off;
$ U$ w |/ [- P5 B# {5 p# X4 T proxy_set_header Host $host;* Q3 v4 d" n3 m2 ?8 g0 ?
proxy_set_header X-Real-IP $remote_addr;
4 C" B+ i( g; S: g proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 B0 ?$ E$ T$ @& A) U- G' c
}! V% G' f8 [5 N
}! Z8 ?- q J4 V/ V
六.测试) }8 F7 T; B" [/ C, A& R
& B# S9 e" O) Y, E$ `2 O2 ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 {: `5 A4 E. g1 `9 r- i8 {/ f: y# X; k
<?php
# t5 t9 ` C) D' d phpinfo(); ; h& |8 C- b/ w1 \$ ]! |! t8 g
?>
+ a9 e8 Q7 A; i5 ]- y7 v在浏览器中访问:
% S0 Y! m# M- K0 g+ h$ _4 h$ d" Y+ ]6 b5 o! W0 }/ b
http://www.52os.net/phpinfo.php?id=1 正常显示。
( F/ K( R6 B& n9 Uhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 s3 V( v: u) L1 R* yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
- z4 M8 R1 q: Z2 J说明sql注入和xss已经被过滤了
. m+ m- _& N0 E$ R& m+ B
6 L: j D# T' t6 Y- J. }" C七、安装过程中排错
/ e& n- V( n, j/ `5 D3 b) u3 R
3 ?- x4 }/ h9 d A) F* f" I1.缺少APXS会报错7 J8 Y, G# A+ x' R: e- b
& V0 H. b* y1 B! Xconfigure: looking for Apache module support via DSO through APXS* A+ @. U G! H+ y
configure: error: couldn't find APXS
: y8 T4 d8 x! d+ lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ q; j: \% a( w, O# r( ~- F
解决方法:$ Y6 b1 r5 _5 {7 a9 Q! U1 P7 N( u
( _7 p# s4 Z% Z0 K# s- H8 Myum install httpd-devel* |* x$ w- {# j6 {! H$ e+ B
2.没有pcre
2 r" b1 ]- `5 S3 S0 l @; N% r
9 | h2 }" F. Xconfigure: *** pcre library not found.
2 E, P# |( M* Q. t' Y+ pconfigure: error: pcre library is required* `4 I& t( V, ? u3 L1 W* i
解决方法:- w z3 M7 r! d: d. t3 ]; D% Q, i
; v: C7 y& y( }- dyum install pcre pcre-devel: `7 s1 C1 Y* t% \9 W4 p
3.没有libxml2/ R. Z) L/ `! r
8 Q- v k; s: N' I, _
8 V' r6 m; B5 N1 n8 Nconfigure: *** xml library not found.. I0 S0 e% @$ D/ m1 k' |
configure: error: libxml2 is required
# d$ H7 x; T. E7 a) Y解决方法:; D# G! R8 ?. r* }' n
" F8 B; n/ i+ _) z, _; Dyum install libxml2 libxml2-devel/ S+ ?; v- d' t! ^- ]9 I* Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( D9 Z+ R; {3 w8 y, c& H; M4 }8 b, k4 P" n# Z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
7 ~- ?( {. |& k! knginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ N7 k( I" o' H2 I% t/ o原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 c3 G1 M8 W2 h. [, K+ E: Q" R) L$ V& `) u4 q! O4 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" K- o1 u, F% x0 W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
; s- J/ G, @ \' U1 Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, y: B2 ]2 Q1 A2 q- C& B9 t5 R. a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 `1 q" t3 Z8 I% Q9 u; O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". h& Q! f5 h6 E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 w0 j7 Y9 w& R5 \" m解决方法,移除低版本的APR (1.3.9)( [. x- Z" R. ^& ^1 l; R T! M
' f& m# ?6 v7 a
yum remove apr
1 B, B6 R, r; a1 w$ \1 L5.Error.log中有: Audit log: Failed to lock global mutex1 Q7 Q, \7 Q& h9 @1 P! C) d# n/ j- b
- P; G7 R! g$ ^2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 t! S: N! h K; U1 W! L/ k Mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. g0 b& _, d: y& L) R解决方法:0 [3 T* m( z, [0 Z y2 n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; V% P0 D" k% t0 ?& D/ N% P
8 @3 u' F$ h! E) e1 N6 ZSecAuditLogDirMode 07778 \' Q9 d3 \' P) g2 H6 W2 I5 j
SecAuditLogFileMode 0550( E, N% I3 x, F0 U a' N# k% g/ T
SecAuditLogStorageDir /var/log/modsecurity
. l+ l5 Y" h, O/ ^* A8 O# k0 E- U$ ~SecAuditLogType Concurrent' F2 g6 E! {0 ]1 d+ s: @
参考文章:
' ~# e" U U) Y4 xhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& \' k3 {9 }: [6 Q: N$ e$ i4 a- Yhttp://drops.wooyun.org/tips/2614 |
|