|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ D+ o: y ?4 n; A( n: [8 |7 x: A# P& |
一.准备工作
$ x9 H. R% d" W6 i
( a* z$ j( x/ O, ?, b* | E2 g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 j2 {' {3 L/ D; j
5 j4 T6 |: |6 H9 `4 ]tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ y% l% ]0 b- h7 Z8 x0 V5 N: j0 g2 ]" _. }0 `' y8 z( ?# O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 {! [/ Z: g; y
4 _! _5 f# x" H. z/ c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ M$ u0 Q. x2 R6 s
: i1 o& F5 o5 _% v& E% L- f n9 }% i
依赖关系:/ T) |$ p- t2 R2 s
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ m; B* }' J4 O I1 T$ V
8 O$ h8 k" U% e+ ~% v
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
! h& r+ _" m# S. bmodsecurty依赖的包:pcre httpd-devel libxml2 apr, N, V) h! n/ g8 m0 f) x: O1 ~
" C6 y: q2 l+ l
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
0 ~3 b$ m- h" A二.启用standalone模块并编译4 r. Q) H* W3 W( b
) c; g$ X. w1 q; W; s6 x下载modsecurity for nginx 解压,进入解压后目录执行:8 Q( a- d! c$ W" B
) W, S" ?0 R6 D6 l
./autogen.sh
7 l- y, t; j$ \( E) E$ ~+ h" b./configure --enable-standalone-module --disable-mlogc* Q; }/ s, {, C9 u# {
make
: P$ {2 T9 J% g$ o; A# [三.nginx添加modsecurity模块
, |( [+ S5 f3 R: k. h$ {0 J9 X* H( C4 A* S% M. S3 w9 u: U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 L& I' ]( Z4 h' C8 F+ e0 V, \+ R9 ]6 u3 G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 ? ?/ \/ z0 R! Q/ G+ W( m
make && make install
' r8 p: A6 s8 o% E6 m四.添加规则
' C8 v/ r7 k4 x4 m
* g X# @8 B* }+ U- d Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; ^; n, t8 L" Q2 T
3 U: `4 B5 b# U \. V7 W8 [1.下载OWASP规则:
( m; J2 B- j! {2 T& f( h( c: E* ?- |
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs& i: m0 w$ Y7 v* ?0 W
7 j" {2 s4 G, N& l2 H# `6 Xmv owasp-modsecurity-crs /opt/tengine/conf/7 \, t9 B9 f1 W5 R( m! J
% u9 e6 U+ q" {$ l: g& I) O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: y2 H% q" h6 I) I" O7 j' {
2.启用OWASP规则:& C J% t9 f4 h# T) Y; c/ e: Y# G
$ _/ R4 J$ j" }& O" ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 ~- b/ ^6 [/ {) H8 v P0 S5 t' [" _! v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% ?: p3 H1 M; u) {. b. d; H
2 S2 _" s$ Y) d. F+ `; Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 `; q( D, a; o M
0 q1 y5 ~" F) d+ C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ d, X1 M4 ^8 H9 r+ l; iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% y/ p( ^" ?# l; k* b) O! e# k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* @( E( f- c2 h9 w1 S' G; X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* S1 c8 \; G( d! }% D5 I e8 E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 k% Z5 d4 l# ]; _" s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ O3 J5 V1 S8 T$ L6 ~1 ?Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 [$ d' f+ ^! [" a, J' d9 e+ i
五.配置nginx% G: N+ W( N! h% c
1 i9 _ h5 z7 y
在需要启用modsecurity的主机的location下面加入下面两行即可:7 a& D8 @. d8 i) \) ~7 W
" H3 f5 ^- H! k0 Q, ~ BModSecurityEnabled on;
5 u* k- j2 Q9 nModSecurityConfig modsecurity.conf; y0 M/ D" p+ W0 V
下面是两个示例配置,php虚拟主机:$ ^5 C2 t7 l* U: W7 ?4 U* t
$ y) }1 X. f2 g9 R
server {
* k6 r/ ^9 X+ Y' ?% I* N listen 80;' T5 \' s+ [- S
server_name 52os.net www.52os.net;
% ]5 A) Z" N# C. J$ j% l# ~
- L' _8 G% s+ W; N9 O0 q+ u2 B! { location ~ \.php$ {( Y9 f- j* {( A9 R/ Z% |7 S* A* A
ModSecurityEnabled on;
$ n- d+ H, l, d% P4 J4 J, C2 b" K* s ModSecurityConfig modsecurity.conf;
; b& x/ d7 ]) r! N- k/ a& K- j" e9 `& r. l
root /web/wordpress;
0 R; o5 o- q" b2 v( L) J index index.php index.html index.htm;
" E5 k% Y6 z4 T3 b, y+ x+ d( J
4 }/ N/ \ O8 F. B; h2 K4 ? fastcgi_pass 127.0.0.1:9000;
- G5 }9 M" j) W3 q" I fastcgi_index index.php;
0 ^9 ?9 w) y0 x fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 O& ~( o) H: G; I8 `' ` include fastcgi_params;
; j1 E3 d" p& M4 C( y0 T3 f }
?/ f3 g3 I# H3 A$ f$ q }
% ~: Z& n7 p! L$ C, h: O* M. oupstream负载均衡:, V( m2 N$ n+ j2 w9 O" F
! ^" J5 D i9 }, h, J3 K/ J) m+ Oupstream 52os.net {
8 G1 T+ ]: ~) @ server 192.168.1.100:8080;" Z% d3 J1 N; p B p% E* z
server 192.168.1.101:8080 backup;
@, M( ~, c: |}
- t+ Y1 A" K. Z
3 w5 X" u; k( q$ H' N, B+ _. xserver {
9 x X; }& H9 M* E4 B& Plisten 80;3 T' B3 g, k ^5 R1 s& f, C8 M" w8 G
server_name 52os.net www.52os.net;
7 d! ?$ S4 C; j/ n/ H; }- i, E* ?/ E- J
location / {
6 E6 _( c1 `* I* o ModSecurityEnabled on; 8 _/ B4 o- {, a, P# y# |$ E/ X' {
ModSecurityConfig modsecurity.conf; ) Q+ b7 k! H# g+ ~ c k. d
2 E0 q) H& n) K3 E# W- M; d1 _ proxy_pass http://online;. n( p8 | f5 k6 e
proxy_redirect off;4 z8 ~+ Q3 h: W$ G: Q) i, I7 O$ G
proxy_set_header Host $host;* r6 i% z% }3 q' k4 { |
proxy_set_header X-Real-IP $remote_addr;
. U; J* I. L) D# Q proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 S( l$ _+ K0 r
}, a; @ t2 t' ~0 O
}7 e0 ? R# \% c+ v! V6 m6 N% n
六.测试
+ y& W3 w8 F# w" a( f9 A, d
6 W4 ]5 q4 Y4 ]8 H/ p1 Y' y* u) V我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 Q5 ^% F; ~% t$ m, K6 ?' y
& M8 b% ]; j+ d% G. K<?php1 j3 A+ Q: |7 s6 B+ y( g9 ?2 l
phpinfo(); 0 S# ~+ T9 ?' {% D7 j
?>
: g/ M. t( z7 \在浏览器中访问:
% k! M/ U" x) \" i, l, i9 T0 U: F
% k, r" }. D9 d/ V4 chttp://www.52os.net/phpinfo.php?id=1 正常显示。3 x+ j7 s u+ r! M
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 q7 N# F% o" x% d
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: ?8 b% l/ i0 w6 Y* T, v" U
说明sql注入和xss已经被过滤了
. X+ Z0 S* Y9 P* Z
6 l c7 o: K% R/ @七、安装过程中排错
8 u8 n% w4 A5 Y1 v. E/ N/ J
8 P+ x$ z- T( z! ^( x! h1.缺少APXS会报错
! |8 G. n# p; u4 u. K C
# H, ~" {4 u- Pconfigure: looking for Apache module support via DSO through APXS
; X. W! S4 |! ^$ n7 wconfigure: error: couldn't find APXS- ^- C( n" X2 o# W) v
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 _) O6 X2 n# }2 e' a解决方法:9 Z( y- K9 v0 `: a
* ~/ q: ~& I# C4 V
yum install httpd-devel
, l5 v. {2 U2 v `# m) b( M0 n; m2.没有pcre4 v) S3 O1 I& X" x% }
: P3 r* l% H7 R' y4 }configure: *** pcre library not found.+ d2 p" W# r( N6 d5 S' q1 A$ h
configure: error: pcre library is required
& K( c) q, v, O9 n7 y- d6 }8 P% O) w解决方法:0 t/ u+ Q8 j9 Q* y4 _
7 K+ l8 O% }5 |& Ayum install pcre pcre-devel& H* [0 l+ c. e! ?* e, s
3.没有libxml29 d' \; }. I6 q4 R* e% h
+ P1 u9 }: `9 {2 {6 u
# U- C1 f/ @+ {+ dconfigure: *** xml library not found.
6 j. e# P3 z. ]* u8 W6 Y1 O4 q4 hconfigure: error: libxml2 is required
% O% C( C: Q; e6 P解决方法:% k( H, R6 B, p& ` h
S3 I' e' t7 W/ ] D
yum install libxml2 libxml2-devel9 s, C; o, f+ j t
4.执行 /opt/tengine/sbin/nginx -m 时有警告" z/ _" y) }/ r/ w$ R) g5 I' Q
: V& B& _; M4 k" a F9 O# VTengine version: Tengine/2.1.0 (nginx/1.6.2)1 n# T. p0 H- x/ c5 Q. [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ g/ m( K3 c. F8 P原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 L6 J+ v7 L( j; L+ P
, l! u5 A1 |! A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, L( c+ f$ p- b) w- ?* r$ {5 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ x% N" C1 D4 _: M2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 I: {) O, z$ S* E; U. v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! H3 a+ `8 ~$ T2 J& g# E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 f0 V% V7 ^1 s; ~% q, R8 {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# @% s( N* r, E# _解决方法,移除低版本的APR (1.3.9)
" u2 z: ?* _) h: m. l0 _1 N+ Y3 O0 g/ J0 {: K) r8 y0 R) H3 Z
yum remove apr
' y5 u, D+ M( c9 Z# z5.Error.log中有: Audit log: Failed to lock global mutex
# c& O) W/ U' v3 Y# `& W7 o7 C- C2 @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 2 t* V" G/ u2 ^0 u
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. U s" V" A. H# m
解决方法: [, ^: i! p$ n; }/ y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 D1 U& f/ L3 y3 B$ g0 I5 Q0 L
& y2 @' j( `% o1 v2 }6 b* D2 i4 {# MSecAuditLogDirMode 0777
* U- B! i7 p R" O5 F) C7 h& eSecAuditLogFileMode 0550
% K: K; e! E7 V: T, \' |% G9 kSecAuditLogStorageDir /var/log/modsecurity! o1 W" Y }; h# B
SecAuditLogType Concurrent' _& ?5 e8 D0 n( M& T: g$ F3 n
参考文章:1 S8 ^" A4 v& F6 V
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) P4 S7 I$ F2 g3 p( uhttp://drops.wooyun.org/tips/2614 |
|