|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 J5 Y. c: S0 [" Z4 o2 y/ c$ H
+ _/ |: o+ n5 @! W0 Q7 E3 v一.准备工作! _& e% y1 H: c4 t) x% S
* u$ E. D! C; m/ s4 q# O9 p7 T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ P$ h& J O+ D6 A% h
2 b7 T0 m$ Z8 s! [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ O6 T, I& b i5 K1 q
' S+ `4 @8 f3 X" zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: \0 u! O' y# `+ i" N) X) m
B$ T+ d3 x5 \6 D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 Q6 P/ B/ {; j3 x1 [ e2 F
; c" Q5 A9 d; n2 N1 e
依赖关系:/ A1 r- t+ X4 k( U* N+ {' L
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 b7 I0 v. G( p4 K: \2 u7 j
: [2 z$ S+ P: h" n: o
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 Y% v+ b. ]' q6 o) Y( gmodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 v1 g% ^% M3 ]* j/ S
7 z B) R3 ^& f* xyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel- x: [, L8 T9 T: ]
二.启用standalone模块并编译, O& W8 ~" s% ]0 P
( q. l$ z3 q8 h; l
下载modsecurity for nginx 解压,进入解压后目录执行:0 S$ T( @" r: m, b! m) m' d: W
7 H2 n0 [6 I! r& o: Y$ w- V7 P9 g
./autogen.sh
: G! v/ \# A4 U: `8 h7 |9 }" K./configure --enable-standalone-module --disable-mlogc% u5 _2 o7 x3 r* j( d" I9 B: m) C
make ; C: E& X. t( f* X1 Y% R0 y
三.nginx添加modsecurity模块
) P9 A% n7 v) k( `7 f% W8 @) [
4 ?+ @+ } f1 R# y! S( Y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:7 A {' S% y1 g9 d+ x: ]# W; R
?6 b! J. d: g$ `# f./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 V9 m2 ~6 V, s' i
make && make install
5 A7 S' p0 l& A; K% F. S四.添加规则
4 c+ X) |* Z# J3 ^" F; m+ H
6 N7 Q% C _+ \3 E/ H8 Emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 K; ]" c3 W G+ W$ A, {8 Y
* U+ E3 j; h* J' w6 R" U5 \
1.下载OWASP规则:4 C) E u/ n' b' H L& p
1 Z. K. i4 \' X& ^6 E/ R1 Ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
; X4 {. B5 ^, g# g T5 I+ j2 B: W8 [1 t% j
mv owasp-modsecurity-crs /opt/tengine/conf/
, |7 A c9 O P' v+ D4 u& o9 `+ M" O# q4 t2 v; B
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% n; J1 {% n: F
2.启用OWASP规则:
9 e" k- J9 V, v2 u6 k
. t H% X. ^) _# a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& L3 f/ C4 i4 H/ s- V5 y. ]; w! o6 I5 O% Y& N% b" R
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 {' V* V p3 i0 Q. J
7 _/ [3 r7 `5 Y. x1 }& h0 e6 g# q
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 F! d% k3 x8 J0 L5 C+ G# \* D2 G
' h+ h/ z @9 G; E* WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 g* u& b9 z5 ]" a/ Y- U( p* b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. k; [' S7 F ^# J% @! s+ C, G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 s7 M3 `: C7 q5 }, wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 u% n- Y# t. L" c3 _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 I* ?2 G/ { ^( g+ c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( @6 }7 ?; `! I* L! p! p9 ZInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 Q) o7 Z* T7 e3 P. ?8 I4 j五.配置nginx V/ g8 n$ X7 s% H3 Q
& @1 l6 A' ^' a1 W% Y' T+ R. d在需要启用modsecurity的主机的location下面加入下面两行即可:9 U2 |8 H/ F/ I4 i; O2 S7 V8 W) x
% _ ?1 ~3 T: H8 r. N4 r5 S8 b
ModSecurityEnabled on;
$ ~+ G% `) b2 C8 L5 p) j) i$ y9 `ModSecurityConfig modsecurity.conf;
$ N! X' \) b' V8 A+ ]; ^/ e下面是两个示例配置,php虚拟主机:- a+ k, h7 E2 E$ l1 e
* C3 h6 p5 U% g0 p- f' _server {
- `& h! E. k( L: I listen 80;# o8 V# R! Z3 k' g# q5 ~
server_name 52os.net www.52os.net;5 n; k+ C. b: ^+ a. Z$ s
, f; s: @4 `; u location ~ \.php$ {
+ Z: f& t4 n3 R z; \2 U3 r% a ModSecurityEnabled on;
% ~" ]% ?. U2 y/ ~ G7 y ModSecurityConfig modsecurity.conf;) \8 {2 o0 f% p- a, H6 [
6 e, p. l7 y. l3 N3 [( \
root /web/wordpress;( ~2 D: h( K# k. h" ~ \7 w
index index.php index.html index.htm;4 q. E( ?5 _& K* f% J
- i( @* w" w1 I fastcgi_pass 127.0.0.1:9000;& v3 Z: R6 |1 w2 u. z6 Q- ^
fastcgi_index index.php;, p8 e% A/ J9 b$ z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
7 H9 W! H; C" z9 h5 P$ e! n include fastcgi_params;. P6 ?8 B4 y9 i" H
}
. i3 r: z5 n/ J' Z8 k }- m/ @; d' I. [+ v1 G
upstream负载均衡:
9 T Z6 ^( t1 W# Y
+ r" ?+ P# V2 t+ cupstream 52os.net {
% h1 m: ~8 d% l5 [) O server 192.168.1.100:8080;
' F3 S, s5 R- l* e2 ~; k server 192.168.1.101:8080 backup;
" ^1 Q6 j# X2 t7 Z1 {}- T4 I6 x# y6 a2 ?/ @
, N+ e) y m" X) u& g: eserver {+ r- T2 K# Y& B+ k( N
listen 80;
; \- g9 j4 E$ R C' e/ D6 H" Cserver_name 52os.net www.52os.net;% U Q1 d! p& r7 a
( a2 v7 F7 e7 C# [1 E6 V; W
location / {
9 Q" Z. z. T; D0 Q9 C ModSecurityEnabled on;
5 [ ?, _( Y* N% p ModSecurityConfig modsecurity.conf; ! S4 |: `) D V* z) Y
& \; o' i" ?1 |+ O proxy_pass http://online;
1 z( }4 O- _) p( W5 S proxy_redirect off;$ t _) s9 x. L7 n; q% s. F* _( ^4 j
proxy_set_header Host $host;+ U7 n( B0 x' R& r3 z7 N5 k# G- A
proxy_set_header X-Real-IP $remote_addr;( m0 A( X" z7 J# a5 Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: {& C5 R( s6 ^+ w }0 s2 {8 r, M9 D E
}
" J7 r# z. }7 j3 ^/ S六.测试+ e( O. [$ @. J) l- D" K8 X
t* t# t" C3 _; _2 c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 o2 U2 M# v/ r: ^* D7 j
5 j/ y2 P6 `& s! }<?php
- F( N" o8 O/ m6 _+ \ phpinfo(); ) Y/ C# W: X. s$ U# Y
?>( N' e& l' {. F) }. V n0 [. B
在浏览器中访问:
1 C* m) ^7 ]: r' u- S
4 o" k) h4 f1 M* y+ }http://www.52os.net/phpinfo.php?id=1 正常显示。
; x" }/ W' m+ `5 n3 F: Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。) u9 f/ |* K( Z' n7 y9 h
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 {8 F# v. O9 x# b. R8 d说明sql注入和xss已经被过滤了0 P$ F z0 z! b) R
& V) D3 w9 t. @6 i$ Z
七、安装过程中排错
5 l- g( K& ]' w2 g2 ~& z' `9 ] v* [
1.缺少APXS会报错4 e4 j, o% g7 D) w' Y* D" n& s3 F
( [$ M/ b; O$ \' M$ \4 b
configure: looking for Apache module support via DSO through APXS
2 {6 M+ Z1 @$ X7 q0 H, j- Q- bconfigure: error: couldn't find APXS
! F9 s `; n$ h T! G6 F' t3 r# hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! T$ _- w# n: H8 N$ _解决方法:
6 ~+ E g1 Q+ k- u P5 T1 M, |3 v* p5 k1 b! C
yum install httpd-devel
+ A1 t9 b0 {8 M2.没有pcre+ o0 p4 q( G4 e/ p* E/ C/ }
& O0 R! o2 R- @. i
configure: *** pcre library not found.
) ^- i9 g2 Y8 M- Aconfigure: error: pcre library is required2 e5 P; }3 U( V; A
解决方法:3 `# G; y. E! r
: ~" F+ G' X# @6 V8 m
yum install pcre pcre-devel
' C; [# m v, p/ }* B/ U5 p' U3.没有libxml2! Y4 y) N2 o: x: m
' x1 m* X( |$ r. h8 H) T' F4 f& o$ p
7 _# R' w6 J9 k: R; i
configure: *** xml library not found.
+ E/ S: |0 @# a1 ~! Z0 rconfigure: error: libxml2 is required
; U' H: Q% @1 D0 p" F6 Q: T6 h2 V |解决方法:
* j9 ^# w, b, ~: R7 | v- z6 |: y' O* _0 f+ e: V2 m
yum install libxml2 libxml2-devel
1 P* o+ H9 `8 \+ l! q4 R! r" t& |2 L5 U4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 w0 L b9 ?: s" J& p& Y: b4 _- Q1 U% E0 a) V
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
8 O$ U+ d; Z5 L( \nginx: [warn] ModSecurity: Loaded APR do not match with compiled!) D8 s. {$ g4 T4 c$ F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 J% l% v5 K6 f. a
9 Z4 `8 P# ^0 j S9 E& r, f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; z2 G# D" ~) m3 P' g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"* M( V0 C9 f- R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! p o) W' `5 x" [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 y) z' T7 |5 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ y, i4 S) q4 ]1 k2 T- T1 l6 y% p
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 a! T6 z% j" A* T$ p
解决方法,移除低版本的APR (1.3.9)
( r9 p' ^/ [( U6 q& Z O& p9 c3 r N
yum remove apr
- E- f* \; e2 Z) B( A$ x0 X. S5.Error.log中有: Audit log: Failed to lock global mutex
9 `7 V7 m3 W9 Y7 K2 T
: d+ `3 v! g8 K6 Z- T8 L- {/ v, t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " \! `- l# b0 S. }! l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" m. d3 f+ j" I8 j8 I/ G4 ?
解决方法:
& O" J7 n9 l; F4 i, `. l! P6 M+ [& |8 l编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# A- f. b7 T" H( G% }9 D
' f/ g8 v2 U0 y! {: USecAuditLogDirMode 0777
7 R/ f+ `" Z2 b% w p( ^SecAuditLogFileMode 0550( g# N- _) q" |. @- b, g
SecAuditLogStorageDir /var/log/modsecurity. [ h; E* g* \ i* Y1 r
SecAuditLogType Concurrent
; Q- R- a9 y! E参考文章:
# E9 {0 f# L+ W3 P mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# a( `: Y5 { h+ D( `: E2 c& n
http://drops.wooyun.org/tips/2614 |
|