|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* r+ ^4 X% Q2 L6 s ?4 c: w3 Q6 c( V% d8 P( X; l* i
一.准备工作" r: k/ ~' `" _- y
; o! |$ q( X! R3 W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* M+ v6 x+ N! |1 f
: R. ]/ x2 F- Z8 h; X/ Rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& a- m0 K. x2 I/ ?2 l, o
+ {( ~) R# L9 ]! Y' r5 wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 J/ Z! K- _ s4 E J4 U/ |4 U
' {" W6 @* b: c1 A' uOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) Q1 b2 o$ w6 P1 ?1 a# m( ?2 `; O5 }- G J
依赖关系:
5 C7 t* e2 v* jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& Q! r/ k' Q8 Z1 M$ J
( s4 J: J* b9 ?0 L% l9 v" `1 u' O% gyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 k+ r. e; O$ a. lmodsecurty依赖的包:pcre httpd-devel libxml2 apr: b, Z/ u+ B( N5 L
+ ]( a( m' D+ F! p5 n4 Cyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* _# w& ~: _5 ^) I0 b. |! }$ `
二.启用standalone模块并编译
% Y: a$ L1 m8 G/ k% J/ S$ v, M+ J- i( x
下载modsecurity for nginx 解压,进入解压后目录执行:
. S1 i/ U& i# z+ @* J4 g5 N8 r
) [1 @! Z B" W4 e# S./autogen.sh
3 T: ~; d. q g" I x./configure --enable-standalone-module --disable-mlogc# X4 U$ L+ X5 v2 p$ c) v. F1 ^
make
" a6 D/ J( A$ J( U# R7 a三.nginx添加modsecurity模块
& O; ?" n3 F" r0 h( f* q- l0 L; `3 E/ `) ^8 t* E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% s+ Y2 O/ W$ I7 L" c7 @/ b
8 K& v, C4 g/ D5 e P* I
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. ] \+ e% x, ~$ a# {( i: M
make && make install
: ^' K7 G) ~0 k" W7 j5 K) _8 B四.添加规则
4 G7 q) @$ Y; b' U" q, Z" `% N: I; k1 |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 P t6 w$ N" w, H) q5 o2 B* d# P: D: H3 _2 U2 s
1.下载OWASP规则:4 ~ e5 T' Q8 k. r# r2 _# Z
& z9 _$ b* B! L" ]% Y3 S- r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 C8 P5 [' P y5 Z& j2 \" J
% k" x- ^# C) v1 g$ Y6 @mv owasp-modsecurity-crs /opt/tengine/conf/' R7 m, i+ n2 d
+ n( C) C; u$ U9 d6 I) ?9 Q& j
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 d/ `& Z# f* a: `( W
2.启用OWASP规则:6 m' ]7 X7 m7 [) w" d. j, l x" c
( R' o7 |' {# n8 x$ K复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 ^/ k3 A! U" o" K. [$ S. _6 |
' I) C! U! r4 Z! R# Z8 k
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. P6 I: J8 ] u' z7 @1 Q& U: ]! ~7 T* ^) W4 k: f5 C
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 f: f( y o' t/ C M9 W
, {, O) \( J& j+ H% {6 Q
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! v3 R! A" _! E: JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, y0 \# H; i. u/ e2 m" v0 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, v: o" |: \! L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. @- _/ l0 Q5 U. J7 F3 `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 F1 S2 c6 j* DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) t S6 P' L- n6 x1 x% P
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% ]& I, z4 B1 B# p5 L/ h* D五.配置nginx
7 ]+ q/ W8 [5 ^6 g* N _2 I, C: q- y! {1 ?; j. _
在需要启用modsecurity的主机的location下面加入下面两行即可:; P- _% W! }4 ]6 p2 y
; S5 _1 ?+ [ u- }) d1 |ModSecurityEnabled on;
o* v7 t Q, t' Z4 @5 lModSecurityConfig modsecurity.conf;8 N: w& ^% g5 ]7 l' d: ~: A; l
下面是两个示例配置,php虚拟主机:8 J: m/ ]% D# W# M* K4 B
# {8 I9 g. q- z8 ~% b
server {6 c z0 [5 {+ g0 Z5 I) Y
listen 80;
$ Q4 T- o: A0 r6 p" L- l server_name 52os.net www.52os.net;
2 t+ t! s9 T* E! h; H6 _1 L& t - O$ Y5 _, n* l0 l4 k1 f4 p+ l
location ~ \.php$ {
4 x& L5 o5 }, W" C- |+ v$ o ModSecurityEnabled on;
' l- \% S2 a) k8 ~ ModSecurityConfig modsecurity.conf;- F- [9 O% A8 a+ M+ D9 r: D0 f0 d
. l; @7 A, P; t' m {0 N root /web/wordpress;
& b: j0 ?" j+ W& Y. x8 H) y index index.php index.html index.htm;
. |& J1 x+ Z7 ~% E8 j& o {0 w 6 M* s" r9 {3 y6 e8 d- ^( ?5 C
fastcgi_pass 127.0.0.1:9000;+ d1 s, `& @2 B
fastcgi_index index.php;5 k9 ^ v! g! \. Z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
( F0 P3 t4 t3 @9 t0 _( _, r' U- c include fastcgi_params;
7 y9 R( n. Z1 |* n" z: P }7 t5 B( k( T7 s8 V
}0 V6 _ Q$ W: g
upstream负载均衡:( l5 ~! ~- V) `5 A! c2 z* A
/ h4 q1 e% B9 j/ z0 L& qupstream 52os.net {( o8 M, [& D1 ?- ~% N5 z- }
server 192.168.1.100:8080;; g2 x f( q" G2 W* \
server 192.168.1.101:8080 backup;
' I4 V% ?) |" p0 y2 L}- C. E2 `/ e( z k2 `: }
( Y# t2 e3 v9 A/ g/ ]1 sserver {
4 G h+ B! |$ |! t0 f2 L( y9 \listen 80;. {& v" b: p9 l' ^
server_name 52os.net www.52os.net;
- h, b5 P9 X2 D- s
: [2 R5 b- s- _+ v/ J. plocation / {7 E Y1 l! s5 s+ f9 N: L
ModSecurityEnabled on;
+ P7 Y0 z; I$ n! Q7 Q8 x ModSecurityConfig modsecurity.conf; ( x4 R) A. Z9 r& H: X; a6 M+ H
; Y' v3 S$ Z; J$ R& I
proxy_pass http://online;
: d' [6 C- [6 X6 S U. } proxy_redirect off;- W7 s9 C2 B9 U% U+ }3 v$ H
proxy_set_header Host $host;' W: J4 d( p! [6 o H( f* q
proxy_set_header X-Real-IP $remote_addr;7 R- R2 z4 v9 o1 e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" y2 t2 s, [) T& s0 v
}
2 E4 a/ P+ l7 p- T6 z}
# x( C/ f6 C; S六.测试9 [# [' j" M; r& I4 @$ p
5 k G3 l5 f" R* s. s/ l% @
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 S3 n* N* P# }( }2 {
, S, ?% A; A+ g<?php
! M" i; i9 P. L- F+ J phpinfo(); ' J6 q) ]) ]* [8 J( l3 Y, m
?>
. _1 x7 k) o) \+ ~( Y9 T+ R在浏览器中访问:
3 e9 ~: q; B# e" ?, i+ n L9 {1 t
7 U! ^5 j0 d- y# i8 s8 N, U0 Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 S& B5 T! Y* a$ s! { l0 l4 M2 Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, g' O& `# o S7 rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 t9 F! N/ h$ f r5 F说明sql注入和xss已经被过滤了% ]2 ~& `2 H. @2 N
' ~7 ] J! S# ~* t, |
七、安装过程中排错
( t; w+ X4 O, U4 D1 D$ q) X# B' }( e) o. b' a. j/ S I
1.缺少APXS会报错) u: ^/ M9 E$ P: G% {3 n
/ Q3 u0 a$ o8 x+ Y, Wconfigure: looking for Apache module support via DSO through APXS
% ]+ g1 ^8 t4 U* Oconfigure: error: couldn't find APXS
o. r" I1 E, \$ g9 n+ japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 b9 M2 l" u# u9 W4 v m解决方法:
4 b$ _5 f E6 B* N5 A+ q) O4 T7 _2 j
. e1 w0 l% s# {- L3 [) [. H" _yum install httpd-devel
' R; L! n3 W( f! A6 n( k1 _2.没有pcre
0 m. {$ D8 f: h9 |5 l9 o
& t: {& Q' a* O9 b8 f0 uconfigure: *** pcre library not found.
4 d: h; Q* t: f2 {; R7 _& k# f0 Zconfigure: error: pcre library is required: ~: Y( i1 k" X, f5 E9 B/ V# n( v) `
解决方法:* z- d2 x% ]' d- h: p& `6 w
8 q- W# W* o1 E2 Kyum install pcre pcre-devel
# G% k L: o+ I& Q5 b, ~- G, L3.没有libxml2
6 J; c: ~9 i& [5 @) n C- d# A: c$ m! C
$ U/ k3 @/ D* T, w, hconfigure: *** xml library not found.
0 ?8 o6 @8 J( F. }configure: error: libxml2 is required& F: q# ^- W& ?
解决方法:
|! Y" K9 E9 e: s0 F2 w3 Y- r6 o/ {' @3 l& S( ^2 |- B/ G* ^
yum install libxml2 libxml2-devel. Y" h) X: U! o: j5 x
4.执行 /opt/tengine/sbin/nginx -m 时有警告) m; i& O1 A$ Z+ E/ P
5 X" a3 V- Y( M) r" i5 G
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 T3 U, f ~9 w, i1 `7 K9 x2 {
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 ?3 N0 T/ t p8 Q8 |+ K原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ S5 @1 I+ O* O( L; G
0 \5 W- j( P/ i+ N u1 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ J# y, Y6 t z' }9 P" }) A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"/ P: `. } J+ }' Q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 {* `1 I+ @$ V9 W) |5 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( h; G. {$ V: \# a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 N9 D; W/ q# r% G8 D$ {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' q2 P- Q" ^3 B! B) e# x) v
解决方法,移除低版本的APR (1.3.9); C. h. M* a. ?3 O4 ~* d
: J2 h3 k- g. n7 I. Dyum remove apr
: w f" e' O8 a: ^. i5.Error.log中有: Audit log: Failed to lock global mutex7 q! Y* }5 |9 Z1 O" l- ]4 L
0 ]! E8 U$ y, r* ^* D2 {5 L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 t- [- P M3 iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 D% Z$ ~8 S) a, V3 N/ K
解决方法:
7 V# K: _1 o' d; Q$ K$ X% ?编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 `" m( I/ C0 |2 V' h
8 Q( m2 ^9 F; Y& q% ]: LSecAuditLogDirMode 0777
& h; a" P) G5 uSecAuditLogFileMode 05504 c! A: G$ T0 c/ `+ r
SecAuditLogStorageDir /var/log/modsecurity1 H6 ?. M; {" i6 P" Y
SecAuditLogType Concurrent
/ F) r6 s7 ?& C( o1 q4 r y: c参考文章:
) O/ H$ o" O3 Uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) O% m# W% b; J- l7 N# T! T# L% }http://drops.wooyun.org/tips/2614 |
|