|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* Y- ]) V8 }1 u- e w$ E
/ v" O2 |+ P" k6 Z" t/ d一.准备工作
+ b- a9 ~3 M/ \: z0 n; r6 z% G9 Z1 S, {7 o+ r
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& I2 {2 M/ q# f# f: _3 g
% P7 k. [8 Y) L$ Q$ K6 E; `1 ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% V6 O& C) {" E8 y: j; M6 L; c% c5 {! c. m) j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 `4 w" N' c! D
0 Q4 F1 k4 A* G1 P8 ZOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& ~8 S' V5 q, {0 R" r9 j/ m, M/ k3 ]" K+ o5 |0 Z/ b# g: Y3 }* d, ]
依赖关系:
1 \) ~( i1 {/ M" ~) Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ \9 g2 _" C: e/ M% x1 p& p
1 p. w0 H" z t9 Lyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# J/ N7 X9 o8 }modsecurty依赖的包:pcre httpd-devel libxml2 apr! c" m+ O7 E# ~6 T) Z0 S$ x- \
, W% F" \* N( g
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ T- V" U% { S$ O. |% z
二.启用standalone模块并编译
8 n; r! Y2 i3 g9 ?6 p0 O) W# i, }- b0 U& `8 I% ^
下载modsecurity for nginx 解压,进入解压后目录执行:
/ v# m+ y: d# p3 ~. P; U/ U
/ n% z6 W+ F, N. Y8 c7 L: D1 r./autogen.sh
9 ?" e! I; ]) M# P( \$ ^./configure --enable-standalone-module --disable-mlogc2 V3 F6 t) F8 W8 D( {( C" n! R* h
make , U, R) x4 i$ j. g
三.nginx添加modsecurity模块$ ]1 I5 n6 s4 `+ ~9 B
; j3 I* s3 C. }4 E9 Z: ~在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, [0 P9 E# K1 P$ i. a
2 C3 y1 k1 Q2 `8 i* j./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 l$ e" \2 F: d: C$ kmake && make install
# m- j, ?. |" W% Y四.添加规则
7 `2 Q( m" G: u: I |
# Q8 A5 {. B: I* Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ v6 _3 ~" ]: L+ K% H4 u$ t( z! c
: i4 ^: d. H, o' a% c. P/ r' [1.下载OWASP规则:
, l* @4 M) v9 g7 X# D) e/ o- d. o+ R/ O
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# [ w# c* S6 c6 }' I' g4 ^# ]
) @; U3 e) N$ X) ~/ Wmv owasp-modsecurity-crs /opt/tengine/conf/
7 a- n3 r, a, u
5 B' x0 a. c$ V+ u1 Y# a4 Rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* M% k* N2 W3 Z* V; {/ N7 j+ Q9 n/ K
2.启用OWASP规则:
2 I! f- R! Y' x' v5 b; u/ |9 l! |( `! V) ~4 \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ W+ p# n% P$ N
" G1 i. U. C2 m
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. {0 G$ s1 @9 N5 k
! L @1 V A8 u; v2 m. Lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% d) J' o1 O* c. K' b9 g' J
. `! _# I; I1 m/ DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 a/ P9 c: s$ b; R6 y1 k- C0 {' E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 P& F1 B2 v: X! H- ]9 z" d- eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 Z/ T- m% E2 k" }2 RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
l" ^ K# n; R c- ]" ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" _$ Y: p" Y9 P5 g; r$ X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# Q, D* X v# z/ LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 S/ l O, Y5 K7 Z3 L五.配置nginx, `8 w, W i' H; [3 I
- R3 \% f+ E2 {; E, k Y
在需要启用modsecurity的主机的location下面加入下面两行即可:
! b" E) b8 S, V# ^7 o: K, X, g5 l, W' X% U5 E2 ]; W5 U0 M
ModSecurityEnabled on; ) N/ p7 W2 r& c. i2 G) |- N5 h
ModSecurityConfig modsecurity.conf; e, L, w, a, ?; e
下面是两个示例配置,php虚拟主机:, ^9 e" ^; L" X' c1 d4 f
( ^5 r+ f( F, x+ Z* _/ h4 Mserver {+ w: U \0 {0 W1 _
listen 80;) @. f7 I4 j, l) A0 O) j
server_name 52os.net www.52os.net;
& v0 B2 [0 p$ v) @* B i1 ^ ( z R+ G7 n2 J5 ~9 i9 J
location ~ \.php$ {
6 a; \5 s- @( f+ T3 \" w( E ModSecurityEnabled on; ( ^& n9 J" @! g% [6 ~" X) B
ModSecurityConfig modsecurity.conf;" f* _7 c. ?, i- E1 A
) k6 q9 \& [; F- `4 t$ W) d, {3 o
root /web/wordpress;
" M9 B/ \" ?# R index index.php index.html index.htm;, [7 |$ v6 t5 C3 m
! {( \$ I1 w/ N7 A$ k# L
fastcgi_pass 127.0.0.1:9000;5 c3 }( d$ z8 O o
fastcgi_index index.php;
v( I# T' F: P fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 h1 G$ H6 E* n( O/ {5 N i2 Q include fastcgi_params;$ E0 m, r4 b$ {) v4 L1 a7 ^! n
}
% b$ h! t/ b% d5 C }
3 _& ^! G% J% vupstream负载均衡:
1 ]; A: {* b* ]) }4 b: o
8 A3 f' i b7 X3 N7 L7 {9 c' O( Zupstream 52os.net {
# J/ \" Q5 G( X5 ^3 _ server 192.168.1.100:8080;
; q2 L& q$ ^. a. X% y server 192.168.1.101:8080 backup;: X+ h- a- U: F
}& `8 o1 m- Z% {+ g0 x. ?
# }% Q+ I5 L3 a! F
server {
3 u4 J1 m% K; |# I& t" c N& `9 H* dlisten 80;
7 |8 w0 W* x9 C3 r2 Mserver_name 52os.net www.52os.net;: l0 ^3 R8 n8 e* m8 B8 P5 R3 {) A5 M
; _2 {( D# t7 P; y' qlocation / {
7 S( r% W2 P {8 `) H) f ModSecurityEnabled on;
* E) S, q: m8 U+ d ModSecurityConfig modsecurity.conf;
5 N1 D. @. F( F; R% P. F7 R7 {) f8 G3 e9 F6 U6 w+ Y' n8 o
proxy_pass http://online;
- @4 W% B4 K l2 f proxy_redirect off;
" N0 l" [. P, O# D/ P" c proxy_set_header Host $host;
b' c1 k8 I3 b proxy_set_header X-Real-IP $remote_addr;
, J7 x8 K4 [& j6 g4 g, c: R# G proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 J. x" d! \: I2 K) ]% @8 u
}4 R/ I$ ?' y: H, K" s* `5 \+ x; x& t
}
- w* r a( w4 C% A- w) q9 _4 ]六.测试) ]% |; ?$ e/ }& V2 b$ c1 }
+ l/ w" F( w4 {0 b我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( q: |0 N* q9 r9 J h: A8 n8 Z, M- j) @) _1 l
<?php
3 F9 A0 y# T# a phpinfo();
" Q: {; @+ F3 H5 n7 H/ g9 v?>+ Z. S. D# Y$ `" @
在浏览器中访问:( L' k# E8 A+ p- k) {# F$ e
2 K, F# [& _$ s. k, t6 }
http://www.52os.net/phpinfo.php?id=1 正常显示。
, W- i1 r$ E, M. u. Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ E" K8 k% r% t# E& h2 G- J
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
+ s* ~# s4 o2 F! I: R- j! K说明sql注入和xss已经被过滤了
, h: |6 l, L. d9 x" r: r1 P6 X) `
' {+ z! \5 ]# p! X6 E' l" x七、安装过程中排错' @% {, G# L/ M, m, \1 i% ^
1 k, Z& d7 c! s; _9 b" {
1.缺少APXS会报错
. a' W1 {; U4 U$ d3 R8 Q; \( A" ^) @
6 p, p, z' e2 `configure: looking for Apache module support via DSO through APXS
& C8 C9 k, F# r9 d6 Z" J3 ^4 \configure: error: couldn't find APXS
$ g" C& ]- T8 s4 L( p, Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; G3 C o: L& y1 ]" K, a4 O解决方法:
) v, m8 G( t) a
3 c& }/ |4 u4 i! e7 P4 a. eyum install httpd-devel0 t3 N. G: F) @ j: L+ Y
2.没有pcre
+ S% ~) ^0 ^: `" [ y$ v; L7 Z5 y/ U! H! U. l3 P/ e
configure: *** pcre library not found.+ E7 Y0 a' I) P8 d, W
configure: error: pcre library is required+ ]3 ]$ l- i& ?
解决方法:
% c: C& H9 o$ W4 e/ G- a# y8 [* c! f& F: K6 {
yum install pcre pcre-devel
1 Y7 d' R* I u, a$ @' \3.没有libxml2
, `; [% o; W3 q5 `8 L3 W1 T& G/ e- |; b1 s/ d0 G% u
+ g/ T( \; g. @4 k: Kconfigure: *** xml library not found.
' P% U" L6 X; [configure: error: libxml2 is required* F9 C7 h7 d; }% b' A, I F
解决方法:5 a' d0 S8 W& B C
+ B, Q2 x' w0 P* K+ b8 {# `
yum install libxml2 libxml2-devel
5 `7 j5 t; d7 L Y# p% `* N4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 r/ O1 a/ g( K7 p2 [3 q* q: D3 ~6 V9 I7 A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' p! e) E$ x p& enginx: [warn] ModSecurity: Loaded APR do not match with compiled!: ^( m N# l8 \; `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; m1 I* U" L% c, v y' W/ q
* |: ~# K* J- [* O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; |2 N: S* a# @4 S( o" k. G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; g; ]$ s& I8 S5 o( Q: C& G" M! z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* p0 q5 N: L0 ^# \$ ^ F' C6 u' M8 f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 z+ y- `0 h0 k+ r2 p0 c. b( }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" R$ z5 s" T% L1 b: T7 W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; B" p5 c! Z' A9 J解决方法,移除低版本的APR (1.3.9)5 H5 x2 P/ I6 |
# }" Y$ }: U& A+ I6 e3 @+ [
yum remove apr9 C! m! r: {$ T
5.Error.log中有: Audit log: Failed to lock global mutex
, t) x0 K# C+ s6 ~+ O
- E1 R- S: j1 [ z8 }3 C5 y) m2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock * Y% z8 U2 h: S% ^0 l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; G7 h3 P. l: I/ @解决方法:
$ \% W3 [1 g; y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, G& k/ `# E) `3 }- f/ E1 G6 ^
+ p- t3 b" m" a- TSecAuditLogDirMode 0777' [& g3 A5 @: r- f$ ]( i/ T
SecAuditLogFileMode 0550
% h2 n; b) R4 fSecAuditLogStorageDir /var/log/modsecurity
8 D. m$ i g# y* J. zSecAuditLogType Concurrent H. z- M' N2 R( M+ A b
参考文章:# ^& R/ }( \2 i' H9 j" Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 p- c* p2 _& r0 S2 T0 `http://drops.wooyun.org/tips/2614 |
|