|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" o7 ~4 N: u7 x5 _" r8 g
$ f" u2 N' m# G/ D- I" m) c一.准备工作
7 l2 J! n. {' g* p0 P% O, S" Q8 v2 V" _
6 ~0 c; D" k/ |& Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 W& T+ {# Q, ~$ g8 X( @2 m
& Z% g* H" M( t. O" Ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 r; S$ y8 L# H# p" a! o1 N
5 K# o4 @8 y1 z3 G' [5 _3 ^9 ?! C
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 X! j' J/ U Y# G s! O
: X0 {( O5 y, H+ B% Q1 {OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ j0 G8 q! q( u
( ?4 G9 ?) O$ X4 w I依赖关系:
; e, H. D7 M) s, Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! {! Y1 z# P% L1 y$ [: f& f; V
9 q& C1 d+ }- @: k; ^ Y5 _8 W6 Nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, C! N7 V9 s% S% d( M- z
modsecurty依赖的包:pcre httpd-devel libxml2 apr
1 N4 y/ v/ [& B3 F3 x; `
$ u' y$ w, P1 J# ~yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- S4 \, s, G7 n8 R二.启用standalone模块并编译
" j- \, h9 ?+ B% x
" f w0 N x ~# k! N下载modsecurity for nginx 解压,进入解压后目录执行:
1 N) O6 \) S; [# _
3 D1 v1 i7 @" D* w; `5 {+ l./autogen.sh C' C; [9 @4 v
./configure --enable-standalone-module --disable-mlogc: ?+ t9 u. ^0 ]& |
make
- N. a$ X2 y, B" O* ]三.nginx添加modsecurity模块. s+ v" n# W* X) W/ v9 J0 T8 X
8 B, @8 q* D! F4 W2 Z( {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ R; N6 {3 m, I7 z; E
+ l8 f* [- H/ v; E% K& L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' c( o7 h9 o4 fmake && make install1 N) V/ o( [3 H- i3 {9 K* O3 p
四.添加规则
# U0 X, j/ j3 c! i. W) @+ ~2 t
. T% V* k2 M' r9 Q! }% B$ |1 G& l- Zmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& `% h7 |7 D2 A: Z
$ a1 w2 Q* |$ z' y! a J5 W1.下载OWASP规则:
' } Z, S% D( W2 ~# ]: M" w1 W; l8 M5 O$ ?* w' l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 V/ h& J4 L' A$ h" U5 I5 G3 I
0 q- o3 F9 C3 {& h8 i4 x2 K
mv owasp-modsecurity-crs /opt/tengine/conf/2 X' y2 D' ~5 o+ Y/ M
) T7 F5 G! _* r3 Zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& H7 F5 S( W9 U% g/ @1 j2.启用OWASP规则:
' ]! G7 [( X: i, x3 j, ^! u% Q0 c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, |+ p: l+ S& q2 n' A& O, U
2 K, N5 v3 a3 }7 T. v- H. U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' i. O' @8 G S" E( B. Q$ F# Z* R; E) E4 }" Y) X+ p
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
1 ]7 Z$ l* `! I) w- h7 U+ Z: M; I/ T/ i$ \0 t# R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# Q6 R* A2 \% A$ `9 r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( W9 A, A% K2 @0 `/ T$ v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 e! }7 R) ~- C3 L& [5 I( w' kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
z% e+ ]& T7 N* G, \( }0 DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 E* z$ O7 Z7 x5 l5 r; N1 m5 A- `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" G4 Y$ W x# O- v* _3 I- Y# aInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) D% V1 ]6 e4 L: Y五.配置nginx
) X- z% Y5 R0 F3 u1 R
4 `) t" I1 M/ T6 `2 F7 F* e在需要启用modsecurity的主机的location下面加入下面两行即可:
0 U6 N/ @6 c! O9 v* S
6 E6 ^* Q+ b3 s/ C6 |0 zModSecurityEnabled on;
# { x V2 \* p$ R% h# sModSecurityConfig modsecurity.conf;9 W) r' S) }- l, W) q
下面是两个示例配置,php虚拟主机:3 h8 V ?& S7 G! [2 D
" M# g; g4 H) Q& [' r. i* V0 P9 K
server {) W( I8 a' m% h, p. x8 B9 j3 Q
listen 80;* @3 h: @6 U- Y# ^, c
server_name 52os.net www.52os.net;1 y: `" G; ?) |6 t* P0 [( R
* I: @% T9 X" y location ~ \.php$ {
/ F; w( P! _. @" P5 m5 M5 A8 u9 v ModSecurityEnabled on; % Y) ~/ U/ u: l2 A
ModSecurityConfig modsecurity.conf;' w( i* v* s6 E$ `+ u' Z0 x
+ v9 d" s5 n$ g* M( O
root /web/wordpress;& Y7 J3 a# k% ~9 z" x' ~2 I+ p- p
index index.php index.html index.htm;
% @3 F' d5 A% h* M6 `1 g- [ & H9 D8 Y W; I* b. H
fastcgi_pass 127.0.0.1:9000;8 t$ a E N0 ?1 A) t
fastcgi_index index.php;: M5 t! J& a; |" d6 e
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ h& e# Z9 V; f) [! a ~
include fastcgi_params;
4 M; e1 @' j1 Y6 L) V* { }
# @! }# i+ Y- g) m }
) u' ~, G. C7 z" y) cupstream负载均衡:
2 I* Q+ f# ?4 c! O1 D( j$ f( x* s+ c0 L+ ]% G7 {& s
upstream 52os.net {
% {. {" ?, f3 ~" ~ server 192.168.1.100:8080;/ A( x2 V0 K' J% c
server 192.168.1.101:8080 backup;/ d E2 X2 l- G j0 k: p1 x
}# W1 A0 X6 Q+ D( s/ K% x
% S8 A$ {/ ?( j' j! M; ]server {9 o2 ]" O! f" N
listen 80;: g% q) k; w9 J, ~
server_name 52os.net www.52os.net;4 B, U3 W( Q& b1 E- G
% z: h2 ], v# n8 {- `
location / {# k2 L0 x7 q# Y$ ~' }
ModSecurityEnabled on;
4 s3 q3 z7 H% y' V- A! y ModSecurityConfig modsecurity.conf; ; _4 x( e) ~# A- p2 ~) u# j1 q4 N8 O
% O0 z* u$ J4 y. r) O5 e proxy_pass http://online;
' q" M4 b- C. S( r8 ? proxy_redirect off;
9 P. X8 D; w: r: i- _ proxy_set_header Host $host;1 H7 w9 l1 F/ X) a
proxy_set_header X-Real-IP $remote_addr;
# _" q X/ A. m" u& z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ m* L& S; a0 l Z9 V7 k( z. s8 `4 p }* E# }* t. `% {" E1 d% V4 R0 d
}
( Y9 x/ Y" J. o$ v六.测试
- _1 n, B. o$ y
( p( `$ A. G' ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# [' Y1 w5 C/ r& I, }
: u7 @8 F: O( c8 t" @
<?php9 j+ X/ {' e& L% S5 p% G \
phpinfo();
; T3 i, S6 D$ ~?>
+ C. m" n Z/ @2 l在浏览器中访问:) {. J( h; N: t7 g4 y6 T! S
* x& M9 m! k. P3 L0 L
http://www.52os.net/phpinfo.php?id=1 正常显示。
- i# q9 {2 f# t& {3 w! f$ ?3 Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 v) _) z- e8 L9 R. g0 d6 x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 p [2 O- k2 x t8 C说明sql注入和xss已经被过滤了
7 C1 W/ a. q3 O7 v! t7 M" L) O+ L
9 j2 F5 q" B. Y七、安装过程中排错
& }0 ]( g' F# j2 j2 P8 [ w" q- H# H* o9 N+ ~) V
1.缺少APXS会报错
* q1 o5 R% m2 ^. {. C" p
5 U( }9 r: Y6 Nconfigure: looking for Apache module support via DSO through APXS
# V- x9 p& `2 rconfigure: error: couldn't find APXS
3 A' L/ f6 c& b4 i3 F) `apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* o; @" I2 a" }' q8 ?解决方法:5 L! v' k5 p: Q' h3 n C6 h
2 V) f: K. E& ?$ B& |& X% y
yum install httpd-devel6 U4 N8 O5 |2 c* W7 m& h
2.没有pcre
5 S' a" E7 p( \4 g0 `. T4 E. S! D, u/ d
configure: *** pcre library not found.
* ^' V% p* C5 t" nconfigure: error: pcre library is required
: Y9 i1 ^% v$ h, P- K4 H4 h解决方法:# _# n& W# T2 J: x$ }1 p7 h9 T; L8 D8 g
& D h& \. R9 C1 s2 _( A. P7 uyum install pcre pcre-devel9 |' |! g0 P5 j% J" r: m5 S$ E
3.没有libxml2
. s3 ]: V! f+ l, \* ?2 G+ ?* m/ v
6 n7 K/ x0 m# Y6 v
+ B" U/ T+ _. p7 A( j/ jconfigure: *** xml library not found.* d# V! s9 G+ x$ s6 q/ h$ r* d
configure: error: libxml2 is required( ]/ b: a- B! O2 r X: o
解决方法:
/ v S' W1 d/ I# j8 C! K+ E5 A+ ~" s e% s( \: c
yum install libxml2 libxml2-devel
: z3 r; ]- [$ ?0 A4.执行 /opt/tengine/sbin/nginx -m 时有警告! @, M# r/ j( g4 w
! ?6 i! q: _ Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
4 l3 U- J6 W6 |9 \) Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( I; r( j; D' m8 `3 {2 A. d原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# v3 e0 K7 p7 `8 d1 v2 x7 k" u, U% U& q' U, b/ g: g M( x0 u1 R$ ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 e' t3 R. |8 [' e. m7 {$ g! @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: c1 K3 h" b* s, V2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! E# O% X" h: S0 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) I& D. W3 P$ m& E( c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 ?' x' Q" x; m3 y6 e' m. z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* `6 P% V, E; W解决方法,移除低版本的APR (1.3.9)
7 }! ?% q3 s. c- ^4 K8 _/ z: u0 ~3 W/ A4 U$ a- g+ S
yum remove apr
/ u5 a" L. j6 p+ h ~6 H" A/ y5.Error.log中有: Audit log: Failed to lock global mutex9 d1 @! W5 g% p3 Q2 D# @, t4 P
- q3 R* d! j V3 T: s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. G1 F0 g* _8 T( lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- O; L J" m# ]# V
解决方法:2 R, J+ R) s! I# e: D- M4 f: ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. `, M5 v- Z+ B8 g" r
7 {8 {! x) o- N' F; aSecAuditLogDirMode 0777) p) b: v' o" D4 W% c
SecAuditLogFileMode 05509 a' s4 {8 O8 k8 Z1 _6 \8 ~
SecAuditLogStorageDir /var/log/modsecurity; @# a0 ~. Q3 E4 z b& [; s
SecAuditLogType Concurrent, i, E$ Z! a; N9 C; V$ W
参考文章:6 C1 s# e2 N: |. K
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* q' f& q' \0 l3 ^: R P U4 V
http://drops.wooyun.org/tips/2614 |
|