|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# m& `3 K. [. j* n" T
9 V8 D5 r0 b; {6 K* v
一.准备工作
3 w) j+ y( x( r, Q* t3 U8 i+ k& X/ H/ x) o1 ]/ S
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& Q- w! K2 V$ z9 H: J& o2 o
/ U9 d+ _# R! l! O" Q' ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# T0 ?0 h5 d9 g
+ Z& m% n: J% b0 H) [+ imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ o" X$ s7 F- F; g2 _2 ^
" R1 l0 B* ^8 o, s8 {; w, KOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) z, x( F7 Y) ~# K# @
; l' r9 g$ e3 q H依赖关系:1 C a T* R- |9 j' x) I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* N7 f3 c( i9 s2 r( w+ R
; M! _; v- G8 ]3 f$ U, u; Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' f1 Q0 y8 m- N4 y# |0 s; pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
3 l1 p# ?7 U" E, @
7 o& z8 P3 I4 w+ e0 G6 a% g3 [yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 h* B* D' h& H
二.启用standalone模块并编译+ N9 g) `. ~4 }% Z" {8 a4 E
3 R' E g) u$ y) `: ?& [下载modsecurity for nginx 解压,进入解压后目录执行:
- F! n' z( c( [- ~+ V! w" o3 {/ n- H% E. ?5 N% o+ }! B8 c
./autogen.sh3 i) e1 A$ l; B* t1 F) w
./configure --enable-standalone-module --disable-mlogc* {3 W/ q! x, y5 H+ N
make * C0 F0 ^! M' H
三.nginx添加modsecurity模块
; F# P: r6 w$ @0 O L/ |. p5 J) o9 W5 ~0 m! ~- e
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; x" i( `6 \- b" B! t9 N' w3 S0 I+ G. L9 r: i D2 c) c. P% Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ U% L4 N5 E8 A, ~
make && make install h" Q9 r8 s, Z6 {2 [! n
四.添加规则& R, W% K; F& f# V5 \2 T
5 A v9 l* ~: g( H w5 }0 g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ A$ z- Z# b3 p9 R0 ]" w6 ~6 U
, j5 b+ R. t0 \7 [# X2 \. n
1.下载OWASP规则:- s. \+ q2 n( T2 `( ?9 `; a
# ]3 r! L4 n! a/ ^/ B6 y7 agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 z* r" n y5 {$ v, v5 q6 ^
1 O9 [ W; o- emv owasp-modsecurity-crs /opt/tengine/conf/' s F( g$ O% ~6 _+ i% g& c
1 \2 R: y9 s- y; l, U& Hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% M/ X- [3 O0 a
2.启用OWASP规则:
8 J% x7 K' d% J A; I; i# J
' m0 {+ ]& G: i) V j3 w3 u! k2 U复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ O n1 Y3 m6 }% M/ s8 l$ ]
/ f# y0 F# R/ l: L编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ h) C( t7 k3 s
% x% G* p, a3 \8 uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( J8 L2 H) C! V
; [( D' D6 E' d) F) nInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* R" f- ], I" X" cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% K7 L9 e. W: V$ r% l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" @% x% H' b+ cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* l# D1 a j8 e7 W; @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 J6 q% p/ |! X; {: P9 ?; A- a% ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; Y) u6 v2 Q& `) d8 Z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 w4 ^ n i1 ~* o" f* \1 c. a
五.配置nginx
& v/ K. g% b1 a" D9 q* D
+ J( J0 N$ c+ J% r在需要启用modsecurity的主机的location下面加入下面两行即可:
3 Y, j# I( s' s6 w
7 ^0 t0 f6 ~ R$ v) m4 }8 N; WModSecurityEnabled on;
" m( K) _$ f: {ModSecurityConfig modsecurity.conf;
- K* m, p7 v; ~% F0 Y# R* b' k下面是两个示例配置,php虚拟主机:
/ U- p9 O4 ^5 Z) _. x4 w8 h& N" |3 U
server {
. K6 D! ]6 {7 i3 }8 [- H listen 80;2 a' Z( c" z: h* p5 k+ E
server_name 52os.net www.52os.net;
& Z, m- j5 R- Z9 S6 a. Y L# | ) @1 {: D. A+ K, N2 I% [
location ~ \.php$ {8 T; U& v# l& K) P
ModSecurityEnabled on;
9 P4 d8 G' j6 u ^4 c' h ModSecurityConfig modsecurity.conf;: R' W6 c6 w |
; I) R. z# {9 k6 R1 O z
root /web/wordpress;
2 W2 |0 j1 H( [3 ?8 k index index.php index.html index.htm;/ | r7 n9 n) i9 H6 A* X0 C
+ g J5 Q! x7 X9 g! f/ Q% F
fastcgi_pass 127.0.0.1:9000;' h) ^ f5 g/ F: W3 n; }1 o/ V
fastcgi_index index.php;
) s# Q Q' H" I& f; i; T fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& ]* g% I# A3 C9 n% x( K5 v0 K
include fastcgi_params;, t4 D: |4 j k# A
}/ s" {# ?) I2 b9 K
}
% T+ x% Q5 y+ C+ e3 v- N) \upstream负载均衡:
8 b9 ~. Z5 Y( R. b. r
1 }, T& r. l+ Z( U' c% M# gupstream 52os.net {, [7 s! [2 [8 K$ j% V! u$ s- ^9 H
server 192.168.1.100:8080;
5 D9 R0 T# k5 z5 a- m! u. q9 J server 192.168.1.101:8080 backup;7 l- p- M$ ]! D# ]$ X! v/ [* S
}
3 q/ P% u: t" c" e
$ a- H9 p4 j( v5 aserver {
8 N6 R: s$ ?/ Z- N( Ulisten 80;
6 U- e3 D% R3 P- x& Q) aserver_name 52os.net www.52os.net;
/ p1 [3 [& W; n8 ~
/ [- f9 g+ Y: b/ \# }, N' i( q9 }location / {
' ?) E9 U# @- W* @ ModSecurityEnabled on; ! Q) Z9 g" w7 t% E' M4 |1 n# d k
ModSecurityConfig modsecurity.conf; ( D7 n; t9 |" @ N3 N9 E$ z
# Z I# }/ {, Z3 q2 _ h, b proxy_pass http://online;! f5 _- K+ Q# P" P4 P
proxy_redirect off;7 Z4 u1 O! h, a. g l7 h
proxy_set_header Host $host;
" W! y% [! ]( x" h# \$ e proxy_set_header X-Real-IP $remote_addr;
- ~0 _! R8 a i. j4 |. T* w) R' n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 N! x. C9 F$ m6 Z% k* J* s9 Y4 A }2 Y" C$ d4 I: b( S5 |
}! V+ M8 Y; o w: k8 Z+ e" Y
六.测试" n/ v7 }5 l2 h2 u% n" W
/ I B& Q+ K' B; S! Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ v4 H& }/ p$ j4 ^: F% P8 ~
" l, ^/ E) g/ R4 n/ l% u ~<?php' | |0 T. H# V f6 G& M4 K+ I0 ]
phpinfo(); . F" \5 z/ Q/ s
?>/ I- Q/ u. o) l: e
在浏览器中访问:7 c* J4 F; R& t# K7 u
% T8 `7 l( l) ^9 a/ |; {% ^& xhttp://www.52os.net/phpinfo.php?id=1 正常显示。
7 i. m# Z' l/ e+ X8 B8 |http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# s& s0 b6 W4 p2 _, F. Chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ u1 T7 J" b$ h. u
说明sql注入和xss已经被过滤了
! T: p/ V, x1 N3 o5 V2 }/ G/ B1 k; t* R! `0 m& N, P
七、安装过程中排错' @9 r H- L0 [6 v4 V
; x" Q$ I6 ?( m" b
1.缺少APXS会报错
! T$ w$ M+ J9 |5 F
7 G/ h9 `& ^$ rconfigure: looking for Apache module support via DSO through APXS
4 e9 o; v J& D" O" |configure: error: couldn't find APXS2 f) F, x4 [" o$ F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% }# S- T- ]$ ?8 I解决方法:
$ O! J- w) ^$ m# J8 m1 x6 b4 m C0 b0 e6 a
yum install httpd-devel2 z7 ?- j* Z$ e. S0 `
2.没有pcre3 }! E0 G8 y: {; A' X3 _
" N: D5 P( Y. D, Oconfigure: *** pcre library not found.
' {7 k: H5 A5 P+ N7 U3 z3 `1 pconfigure: error: pcre library is required |7 h: a% ?: U s- g
解决方法:$ z" C( H& E; j' R! U! v
( f5 L$ ]/ H- ]6 F7 Oyum install pcre pcre-devel
! H1 N/ c) ?! r3.没有libxml2
8 ?! G7 d, M" g1 E5 L, Y* T
0 W+ E& M% w( c: h
) G5 R" f, n g1 M; Aconfigure: *** xml library not found.
$ h+ D" A; O* R4 wconfigure: error: libxml2 is required
" K" ~8 B* |6 r; t: O3 F8 H p' \解决方法:# {0 v# J7 n& \. f- o: D
2 n2 {' g# F# n( A+ Q& g# j( u) y
yum install libxml2 libxml2-devel
9 p, b ^+ M* q. j& p; h4.执行 /opt/tengine/sbin/nginx -m 时有警告% i, ^5 C1 x, O, g7 H P K: A
$ d2 A9 v L- P% w {6 A# y3 ]& @$ FTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ i; [' Y+ B/ q: Z' Onginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& O- J) F* f/ N" g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- w0 v* @2 n) ~, B, u X9 H) ~& e" }& G# N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. ~8 g( a; w- b( U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ f S' j. W* B5 a& ^
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) q z) D$ Y z- H- u% d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) T% c, b; J o# b& K# c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( v1 f- C$ U' y/ H! P q! g/ g2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% ? t2 r) }& w2 E解决方法,移除低版本的APR (1.3.9)6 [7 `1 i3 d0 e9 v5 Q
. C* n( I6 t' }; E; iyum remove apr
4 d o5 f6 |7 A5 N- ] B3 k, i4 [5.Error.log中有: Audit log: Failed to lock global mutex
# m0 Y6 g0 D& `! u* S9 O- L! n5 M3 J! {6 i5 _9 p/ m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 1 l @9 [6 [' P! m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 x5 J+ ?1 l9 i9 \. {
解决方法:
/ @* j9 C; g" |- h7 X( `编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 h1 E! Z4 Q' [! x/ A; D
1 g6 `& D8 ]2 E' P( J
SecAuditLogDirMode 0777' X5 A. X) k. _1 f' o7 `
SecAuditLogFileMode 0550) v5 x+ a" K3 O9 P. P- {0 k) m& D) }1 @
SecAuditLogStorageDir /var/log/modsecurity
7 |/ _3 Y6 a# [2 X7 ]SecAuditLogType Concurrent
! j( u8 U8 n! M4 r1 n参考文章:% ]2 W$ {) N0 f1 B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' Z/ R$ ^$ E, e3 J' b
http://drops.wooyun.org/tips/2614 |
|