|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, r+ N: C1 e# ~/ d6 ?' C7 ]5 O; n) e8 I( v
一.准备工作 {/ e$ ]8 j* {3 q* n' V9 o' H
" m2 A9 Z) g; K; E, N. K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ u0 [8 n( N) X- l( a
1 `. I2 Z q v, Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 W" r) s' B" L8 b3 u) d
" M$ q+ I- M8 d8 smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# E% b$ D! e/ f; `
% x& w" T! \3 p& |% gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
}- t5 q: E4 G0 n! U' u% j/ e* Y9 v
依赖关系:6 O+ ^) o- G7 N3 n" o. g/ j6 E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& Q' K: Y* u( c# s( s# T5 V7 h% O# }9 s- m0 z$ G7 P6 S$ I0 @, b
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- ~, ~0 p- a! V' P0 \( y- _
modsecurty依赖的包:pcre httpd-devel libxml2 apr" q# o$ B( S# Q: ?- o, u/ i. A
: E9 b: l$ C' c8 }
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel/ Y8 m' e1 c' M+ j
二.启用standalone模块并编译
; O& Q7 N+ i; F1 R6 m
1 v- t( \- m4 p# U% n. J$ ?下载modsecurity for nginx 解压,进入解压后目录执行:/ I+ Z7 F* C2 u/ L4 L
8 Y7 N' O& M3 d
./autogen.sh5 x4 c: Y$ N( h# n" E
./configure --enable-standalone-module --disable-mlogc
$ q6 e3 h0 G$ G9 [make
( }$ a% l& B7 U; |三.nginx添加modsecurity模块
2 }! C2 m' m; s- p0 h( s7 t9 b" U5 E3 J b' W* m7 o+ s! z1 R" l& K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! r. \) G6 c: [5 d5 [+ D' O
: D9 e& B& k: o' s+ K+ J./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& H2 k* H; H8 Jmake && make install
( d: I: ~9 s% G. d0 K p% C: T四.添加规则
0 F) {8 |1 H! { x1 y( \* r$ U0 g8 Z* g3 p& ?6 T1 o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 U3 _0 |# |* N6 l; L3 n" S8 q. p
1.下载OWASP规则:9 i0 Q8 T8 D$ Q2 S q
9 _1 ]2 r3 y' {
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: h/ x9 V# M6 s* [" g y( q& S% P. ?
mv owasp-modsecurity-crs /opt/tengine/conf/
v J5 \! n5 a
/ B; }& r7 z7 Z& c7 a3 v- zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) ?3 A; H" ?4 j( g+ r2.启用OWASP规则:
% Y) }, A8 H! Y! j5 Q
, m" {. K" s. @复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- _, L( M* [; I8 v: O
. L2 }+ ]4 e1 ]5 A6 e
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 b$ ]! r1 B+ K& o7 E0 F
4 k0 v+ H' l! M9 y. e* l4 {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 O6 P! X, Z$ d- T& m* Q
: R5 J0 s: z4 N) S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ @7 F5 m' ?3 B8 ?5 o2 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; V. H1 ]/ {* @5 @! U& j2 K9 D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
` d2 @- h: k N( A# iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- P" S5 b) I7 s# }" r% cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, E! j* P* J n+ e8 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 m9 G7 X$ K1 g D: b8 s! iInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! B2 Z5 f7 D1 {& l& A8 {- F- L& S五.配置nginx2 D9 {/ B# q1 g. P4 P0 l# d9 \
# Y: r1 K( H3 J( }) e; K" v在需要启用modsecurity的主机的location下面加入下面两行即可:; G5 i- Z/ ~+ G
( z$ |, Z7 A( m: a/ F l* [3 g+ GModSecurityEnabled on; $ N. l& w" v" s/ c0 s3 j
ModSecurityConfig modsecurity.conf;
$ Z1 m3 x5 n/ {0 l# v下面是两个示例配置,php虚拟主机:
0 s% r/ j7 i+ J, h9 E+ F3 X
; P# J' H* F6 Y% C$ E' u7 tserver {% D0 j: T6 i) i9 f1 e& m
listen 80;3 K/ k9 Z0 u% Z+ Q
server_name 52os.net www.52os.net;. @. Y4 o) L: w: ^* R& I9 p
0 `; L1 l4 O U8 R( L6 C, Y location ~ \.php$ {* |5 S; ]0 d" e. s! I6 g
ModSecurityEnabled on; 9 @# P5 g3 o/ z' c) y
ModSecurityConfig modsecurity.conf;% |- o& Z- X |' ]8 ~' W+ L
/ M- E" \ L( H! d' d3 N6 Z
root /web/wordpress;+ F4 x7 {; D' J
index index.php index.html index.htm;
! D* n/ `0 {( q7 S0 n: n
; X' x, t" v4 |9 ?7 ~3 `9 a fastcgi_pass 127.0.0.1:9000;
0 y" n0 b: [" S3 i0 c fastcgi_index index.php;
2 O& r; _6 j" O. J# v" K, g fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;6 w3 l6 m3 a! w# X% n, W$ e6 \
include fastcgi_params;
8 I. s( F; o( g) n8 I }
9 ^4 ?4 M6 S" z# w0 ^ }) E3 l. k$ O Y7 ?" y+ a0 k
upstream负载均衡:5 S* ^ ^) E4 \( f
O. Y# \! E5 A) Q, }% S6 r8 n
upstream 52os.net {
0 |* |% k' u" I& j2 B+ Y' i- I server 192.168.1.100:8080;6 t, ]' t9 e* X9 J) D) E" Y! U
server 192.168.1.101:8080 backup;$ ?; A! ^5 B/ W
}. s$ A( I4 z( l
; C# A- c1 Y2 u- d4 M; |9 ^$ F, U
server {
: \+ y$ b& b! Y( Jlisten 80;
% H E# S8 r- I7 c) P( Lserver_name 52os.net www.52os.net;
7 q+ g" ~5 i& }6 v( A, T4 }2 J; p' O' N5 T9 N# T8 f
location / {
$ r. R+ ~* i1 A$ A4 S7 k% W ModSecurityEnabled on; ' j1 t! q0 Z1 c# x7 m
ModSecurityConfig modsecurity.conf;
+ S8 t2 U7 s# E! E; b7 T" H, g! g% v$ }, J
proxy_pass http://online;
4 K+ n4 b4 \. w: J6 B proxy_redirect off;2 s8 w# Z; f9 g# H4 Z
proxy_set_header Host $host;
8 |6 B3 [9 s0 G! J proxy_set_header X-Real-IP $remote_addr;$ i5 F; E6 k# G0 J6 ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# P$ h# q6 }, h' T' @8 [0 U
}
1 T( M! i3 D0 Y, V}
9 f! T- t# t8 g0 L! E六.测试! b7 _: U8 m! h5 _8 }
, X' _! Q/ r$ ~
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 |( f; l# x0 L; J8 Z, _7 a" \5 I2 a
+ M7 B- N' s/ G; k+ H x/ H/ c<?php! e. M0 ~$ Q; w7 \& S2 {! x( D
phpinfo(); 2 B* a q' J, w* L/ Y
?>, k% V& ~ D, o, e) a
在浏览器中访问:
S- e4 V% k6 A! n8 X' t, e1 V8 K& E5 I
http://www.52os.net/phpinfo.php?id=1 正常显示。
5 L$ t# N8 X1 i1 g t; ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& b7 G% Q, N$ Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 C: V0 F# M/ R, q说明sql注入和xss已经被过滤了
f; E& q3 z, t8 V
% O- E: s8 f/ G p七、安装过程中排错
2 p0 u7 m N/ x
1 K5 ~2 L/ b, k0 E) I3 S1.缺少APXS会报错
2 S/ R4 n- W, x) ]: p
* S2 c& c% n5 y0 f+ Sconfigure: looking for Apache module support via DSO through APXS& d1 Y0 X# g2 b
configure: error: couldn't find APXS
, M' r- K1 R Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 a$ f; W0 N o' l x- x
解决方法:( v9 z. r0 L4 W6 b
1 o8 ]! E, S# x% G
yum install httpd-devel4 I' B; U: A3 B: ^
2.没有pcre
: n2 b2 W2 d* h! y8 t, b7 M3 X% g7 t H, e4 E- j
configure: *** pcre library not found.
2 o: @8 i5 U0 G6 `7 X, ^2 k6 U6 mconfigure: error: pcre library is required5 j$ J a( n% r' P, t6 `
解决方法:
" M. q3 F1 X) ~) p3 q6 q7 D& L' |( }( V' ^: [. f2 z4 u& r
yum install pcre pcre-devel5 Z g8 f E/ ]# ~6 O e6 r
3.没有libxml22 R* a s% ~$ Y+ H% j! \( `
6 q0 g U0 G0 o0 V! N# b& ]
, ?* z7 q! ]0 H! ~
configure: *** xml library not found.
! }0 H9 R# J! v Hconfigure: error: libxml2 is required8 r5 l% q2 U+ Z8 G5 x" o% [
解决方法:
( g; w4 o. U- w- Z" W6 _; q
4 R' _( n" L; { y7 `$ E4 G4 byum install libxml2 libxml2-devel
% [. S* l" b3 L' O, c$ M4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 p" n7 H/ o1 D( F# N, s& O7 M& q& R9 v1 ]* e0 B2 E
Tengine version: Tengine/2.1.0 (nginx/1.6.2); b" N6 O8 q8 h+ p" G; y& z- D1 }
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. s7 [- b1 g8 c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 G8 n' n* [# u7 K7 J c9 j" y# T. J- O: h* {# P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' ]1 @$ W3 @3 j7 E& O, I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" w9 w- N1 N" @7 H& B9 P2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 z6 W; y; p' U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 h: V) h( B3 D: V6 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! n1 ]# y* ^5 n/ E: l/ C" n+ ^% s2 y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 ^# w" R1 e3 V* d6 F3 _8 }7 J+ N! Q! `
解决方法,移除低版本的APR (1.3.9)
5 V' W: Q) W% L/ [% m/ A9 B- Q, s4 q2 Q( a6 v V
yum remove apr4 v/ Q" V) \+ @! h1 Q
5.Error.log中有: Audit log: Failed to lock global mutex
: V0 l) J8 _9 I
( G$ F- x1 {8 E- A0 s$ b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' {! Z3 J! L T( o& D0 R2 D- h- Xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# d" S# I8 R' _) X# h
解决方法:1 n2 Q+ |8 {4 ?+ ]1 I3 ?8 U3 i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 ~5 ^7 D+ }& C: j. O
/ P: e; T- \( A- ISecAuditLogDirMode 0777
\7 r* h2 v0 n; GSecAuditLogFileMode 0550
8 z& v0 p/ y' G7 ~+ g; q$ LSecAuditLogStorageDir /var/log/modsecurity
- ?+ _, v; G+ @9 L' YSecAuditLogType Concurrent" r7 V% n' {; j, v& W% B: N. C
参考文章:6 W, ]& G8 T! ^! I8 Q3 g9 z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- Y) B4 q7 f8 F# s* qhttp://drops.wooyun.org/tips/2614 |
|