|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 h) k. o4 \# `) {( q' F
( x! I% v/ c, f- p1 `& S9 u* U
一.准备工作
7 g. p. P% T5 W! M( n, x! k# H
5 h4 T2 G. n* I0 \3 M3 Q+ K- f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 `& g; @1 r: v+ F1 q: e. _* F, B
7 A3 s$ I/ a' B. ]tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 E! Y* C+ V: s! ^) B& V3 P2 U+ @" O' w
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 s& E$ @% m* O* v) b% J5 R8 n0 b, a# V6 y- p4 F- A9 X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ d. x6 V/ i* O
* {/ L2 j& Q0 g& X V1 a6 b依赖关系:$ e4 S) h! ^' U4 l3 q! z1 o
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% k5 F% G4 X+ a0 _" a1 T
4 B+ c! F2 J4 T# ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel a% {: L+ O$ U* {9 ~; _
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 G: |. P/ O0 a Y1 [/ m% ]+ `+ u
# B4 j5 q0 C- ~+ O" M& d/ j7 f: d( i" _yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
: S9 B8 p+ F5 v+ q6 A Q5 b二.启用standalone模块并编译
( c1 R) z: |+ a8 u/ I0 }, a* |3 t- N. ^, v6 y8 |! z, c9 v# D0 I1 ]+ B& y5 [2 g
下载modsecurity for nginx 解压,进入解压后目录执行:. S( b) V- Z6 @: Y
1 e8 y: J5 O' m
./autogen.sh$ s7 Z5 h6 @0 V
./configure --enable-standalone-module --disable-mlogc
7 |( H4 u: E' s' vmake ' p1 A. \( H; ]( |/ G, T) E
三.nginx添加modsecurity模块
J& F% I4 Q) V$ W3 r1 B, @+ v- K% O: U7 t6 n
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! v* w, d. _& D$ D( B1 d$ P
7 N1 D i: D4 W$ w' m. x2 ?, u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ X/ ]$ {! D" z0 _6 j- [( o' hmake && make install, X, o4 q' ?2 l
四.添加规则
: W$ n% C) k, N: k+ I' F: @" n% j
+ a" x5 ?& G: x4 `modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 y; [, }( M. P: a7 |3 O
. l' D) c0 O' v/ c6 L
1.下载OWASP规则:8 f( ?) ?: X* z1 \! `
5 A3 x o- e# g5 }git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 K% ~% d0 r" J( i
) {$ B2 k) ~' N9 Wmv owasp-modsecurity-crs /opt/tengine/conf/" X# h4 F/ W9 ]) h( e; m, X ?
( b7 X8 j& y& I/ @3 E' l0 V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: i) l! g6 N/ t# z+ p/ q2.启用OWASP规则:4 Q- E& s( N: \2 _, F: z
3 U: l$ X$ j4 j( G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' D$ }; B. z5 e
; p6 |/ }5 o3 z5 |+ z- O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" K/ {7 g# h( \8 u& j, ]7 p3 o/ U
o- Q/ b6 y0 W
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) F6 j6 u6 c3 r' D+ P& ?
' J& e; j3 o# ]! @Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ }& h* g; \; ^4 N3 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: ]; b9 g4 L+ n+ F1 w: M" y4 U! Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) e5 {; f/ M; P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& _$ o' D! R: y u) _/ k% c6 q) X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( u6 |+ S4 C* M$ h% q/ F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* E; ^/ J* V, G$ zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- {' W! z6 I6 d3 n/ g+ M: [, E$ J0 W, _
五.配置nginx9 B k- L5 M! [* Q* m
, M0 ]) ]+ P& E; J6 h" N' z. Z% d- z- {
在需要启用modsecurity的主机的location下面加入下面两行即可:
: ~, r2 o, Q. o( B, g7 X, q# Z1 f1 x$ K/ _* o ]
ModSecurityEnabled on;
9 D$ I; Q1 H' l: ?! q4 m2 sModSecurityConfig modsecurity.conf;
/ [' o, F0 K! e1 h7 j4 t下面是两个示例配置,php虚拟主机:( I6 o. ?% C# t4 |. C, _
* n5 Q! Q- J0 G- B; [+ `! ~
server {: g" ~& s+ K5 t, B
listen 80;
6 y4 [3 M \5 ]: H( b$ c server_name 52os.net www.52os.net;: P6 {, R6 ]; e. D4 n
# r+ @7 H7 X" [ location ~ \.php$ {
1 w- @, S0 c4 _. w0 S2 N# K ModSecurityEnabled on; ( a# K6 z3 T( M v
ModSecurityConfig modsecurity.conf;. l J0 N" u6 X0 K2 y! x
+ `! t" u J9 t& [
root /web/wordpress;7 A& H! a, g( I4 Q; U! o
index index.php index.html index.htm;
8 E2 |! @; B; |
% s; M, E3 o9 ?+ O, z fastcgi_pass 127.0.0.1:9000;
9 g: Y! y4 X5 I% k! {$ }" i+ U fastcgi_index index.php;- n2 I6 n! _% M$ _& d* s
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" w t+ R3 M& Y; A- [
include fastcgi_params;. {+ i W+ n: G8 U% f. _% T9 Q
}* s# ]! R- b4 c0 k1 m3 m
}
. k+ \' k7 P2 Q8 S W9 Wupstream负载均衡:: ]' w3 J9 y$ C9 s/ v
* ~+ y# ?! `; \! A" j2 V
upstream 52os.net {; q. ?. [% v& _ [
server 192.168.1.100:8080;( ]9 {, |% J+ s3 s" w& A* C6 U
server 192.168.1.101:8080 backup;
; H+ H6 ?$ c o}# X5 q" Y" ?3 X% l$ I1 z
! X+ O0 k- p8 @' ?, _server {) x; V3 d3 A0 q, J- c! Y. G" R
listen 80;$ U p* D! v1 R
server_name 52os.net www.52os.net;/ r& V1 y" f: x$ d
$ P/ U7 w3 |" ~ c$ T5 Elocation / {6 D: Y. @ z* x6 O
ModSecurityEnabled on; , b0 _3 I* L5 O% O! e* v0 g
ModSecurityConfig modsecurity.conf;
* q1 d; J. S6 w, u0 V* m) K/ l( d" B6 t* x, E
proxy_pass http://online;
5 Q0 }' E( X' K% L proxy_redirect off;
1 y8 v m; p+ C1 { proxy_set_header Host $host;
% S/ }0 \' v5 k& ? proxy_set_header X-Real-IP $remote_addr;% F5 h7 B S) K4 ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- @+ R6 _! f# P% W6 I }
& s, X2 f, i# C% H* H9 S9 R% P" ~ C}
7 O$ o0 C4 W5 m$ {- i六.测试2 d0 L* b% G( [5 p! E1 ]( ?# ~! o8 f) Y
0 ?3 i2 V# U3 x9 C, S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" E B3 F: W# M! a3 c6 g
' m. T0 v/ E/ q0 R<?php
( j2 c; H4 X1 I phpinfo();
! h1 ?, d2 D4 J; x" u?>- T5 t. X( N/ [- v) E0 Z
在浏览器中访问:: T& V9 g; e' G$ H" `! e" I( |
2 W5 m9 f4 K4 `; t ]
http://www.52os.net/phpinfo.php?id=1 正常显示。
6 S6 N, p S) |2 qhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' O" J' \! q, A0 F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# _/ C \7 I1 K% f! t p说明sql注入和xss已经被过滤了
) C( i) f8 X- a9 ~4 I. C7 C: E% `' Y
七、安装过程中排错, i Q8 Z- w8 }* I2 {
$ s8 U" `. B' ^" j! ?3 J* f' n
1.缺少APXS会报错! O& L: U" I/ P+ N5 D- y, N' Z
% I; j8 ?( m; m' y: ]
configure: looking for Apache module support via DSO through APXS
" Q' d" S5 N" L% C2 r& Y4 Uconfigure: error: couldn't find APXS
( Z, ]/ a3 s. w) o- T2 a0 I) { xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 R) E+ u# w- Y" W0 k {$ y& L" I解决方法:. E! x6 |% [/ s7 M C" `$ e
7 H! l2 N1 B; `* n }0 k
yum install httpd-devel
+ n: u8 d* h6 \4 b* [2.没有pcre/ ^( F7 D$ ^* Y) |3 s
) K+ N, q! t: L& s2 s* H V4 @$ uconfigure: *** pcre library not found.
8 |* \) r7 x# econfigure: error: pcre library is required
9 n# y4 e0 Y: ~- m4 ~解决方法:5 w4 Z: j' C+ g! D% A. }
) m0 n* j7 g( d3 C$ gyum install pcre pcre-devel$ ~6 T- {7 o- u5 M2 h4 m' w e- Y( l
3.没有libxml2
: {3 ^3 g( p/ c% }
' v7 n, L0 } T# g" L& j/ `0 i. q% W0 a! z: F$ D
configure: *** xml library not found.
7 V. S2 h) [& s; K0 Sconfigure: error: libxml2 is required! X0 z4 w" B3 V# ~
解决方法: b( v9 ^9 F4 b) y: T! a
6 b0 P& R9 L$ C3 n4 A( I
yum install libxml2 libxml2-devel
3 g" f; y& k) b6 ?7 J, K4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 ]. F, `* R6 S% c, J" i
$ a( M( X; A' dTengine version: Tengine/2.1.0 (nginx/1.6.2)
7 {. j2 V% O6 s( inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 Q5 K/ w- K- U8 G, n, ~* y, w原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 N6 Y/ j! B9 f% C
$ h z+ Q/ p: @1 J% F( U: R8 D8 ]) g7 W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 r3 a3 |: I, }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- I3 I, p4 G: z7 G9 R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; p( h. }/ D) D1 k+ Q' ^6 s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. Q1 H( |$ ]6 t# |$ T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% m! X) U: o o0 k* T! U1 b$ U% |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ i9 j2 H0 B$ X \3 c. N/ H解决方法,移除低版本的APR (1.3.9)
" j0 `; a3 v6 z+ z
6 e; `2 I; |! ^/ ~# s% Y7 k5 oyum remove apr
3 j/ m$ ]6 R+ F i- E3 k' L5.Error.log中有: Audit log: Failed to lock global mutex
1 X, M, \$ q5 J- w5 N% L! [7 F% _9 g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
v1 S R* y1 d# n! ^5 d- C, a; nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" f* Q+ t( R6 J3 p# k1 f
解决方法:
- N* L3 @/ h$ {, z+ n N9 ~1 ^编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容: g' c8 p4 C* @7 }9 R( j
# T0 F" l/ T7 M5 S( wSecAuditLogDirMode 0777
# L2 w/ j$ }$ i, j, [4 v% O. BSecAuditLogFileMode 0550
: j8 h* H( A; k9 P5 N% i( @SecAuditLogStorageDir /var/log/modsecurity
3 G6 q1 Y K# H9 s, t. T0 m2 s4 HSecAuditLogType Concurrent
9 _0 {; x$ ^ j, v: f. b参考文章:
) Y8 ^9 X7 q8 F) j5 H- V2 z; rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 Q4 Q: S& a/ m5 n5 `4 |1 x& u$ [
http://drops.wooyun.org/tips/2614 |
|