|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! g z4 k5 f( @, S1 A% j) b
) M1 N. C+ L/ g. q! f9 E s& H
一.准备工作
: N% J. P! E# o$ k; w1 J V
9 m' v8 z1 K8 C0 u. U+ m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* N. X4 ]# X. i$ Y" _! R! V" p9 u7 G1 W3 L( r; i( _9 h
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 R; m# j# {* V% r7 D
7 E5 E x3 L" h7 T Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 Z& q) g* n& P9 i9 x. a
! H8 o5 ^% X7 L9 }$ A; T' XOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* ~7 U# }* }% B1 B- u
" _$ C, r0 \; P1 C" a3 f依赖关系:6 Q! p: b q& A0 ~
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有: n8 [) V- F' X! n
2 ?2 K# H5 D* n8 |1 U- [6 L+ Iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel* `3 C4 C) {7 F3 \0 u& q
modsecurty依赖的包:pcre httpd-devel libxml2 apr5 q3 H: d7 t' N: z
; }2 V+ A& f/ s( K9 R
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel; _0 S) I. E8 K9 M# u
二.启用standalone模块并编译. M( H+ D8 e9 B. L9 J7 Z
, y+ S' G9 r9 ~9 a) W
下载modsecurity for nginx 解压,进入解压后目录执行:
* x- ]1 C7 N* j
# ^9 ^0 D+ [# G" M5 o+ K- T- b./autogen.sh( O) Y2 R. ^: b6 h
./configure --enable-standalone-module --disable-mlogc
9 L; z3 x' F. s7 w% Hmake
' {# `# f2 t0 u' p! s三.nginx添加modsecurity模块
# I. h$ P& j1 C7 |6 U h$ X
+ |, T/ M9 o P. f$ J- q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 C1 u& W Q. _1 k3 [8 p
% A( B% W5 ]) `( P& G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
7 }0 H! j: G3 Q8 W( Nmake && make install. F9 `5 P' _+ V
四.添加规则/ W1 d B1 Q. T& F7 T0 _5 B7 Y5 g
. C2 N; a2 d! k3 Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 N, O, F. N) h, K' ~
* g# t3 f0 |# }1 K
1.下载OWASP规则:5 a0 e0 l! p i, e
' o) {3 c' B1 fgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& m! G; T4 p" y; x$ i
5 ?; R" g* ~6 N" [; e/ ?5 r) z8 mmv owasp-modsecurity-crs /opt/tengine/conf/- `1 ~$ S% D/ C
$ W0 E# Y6 f4 C. B1 Z+ ?6 H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, M6 i# R: y; v' o: h# w! E' j2.启用OWASP规则:. L4 L5 g0 `" H( y
9 ?4 m; Q r# n I: B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( N* ^! ~; W' J1 n! o( m7 G+ a: y' r( @- p0 k
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) r# \: n0 O# C a0 k. ^- f
/ [/ [, K2 W0 W* z. i
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 \6 Q) p% s9 M1 k J% W- x' }
8 ` B7 P# r8 y9 H0 q7 x6 K, kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) }5 {# T) {0 G. J4 F+ l/ t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
z$ @$ j' M& w, Y8 dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' [9 ~7 |, z( M, h. ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 a- M9 Y% p+ X, W& E9 uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 T* t4 C; E$ N E1 s* n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 C; i# ~9 N. b% ?; b
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: Y0 _& O6 r+ z5 M
五.配置nginx7 m) r0 e: ^) Z: J& p: i
. j: I+ y/ z, q- ]1 Q
在需要启用modsecurity的主机的location下面加入下面两行即可:+ ?6 D$ |6 d) n1 X
6 J6 _' s9 j- k: D4 t% T* K! [& J
ModSecurityEnabled on;
! @& ?4 v2 s j( l hModSecurityConfig modsecurity.conf;
7 n# |# C' c8 `* ]5 r4 V下面是两个示例配置,php虚拟主机:
4 d: U2 O, ?+ p0 j _
. ~5 k# F% j# f$ L7 \# R- Gserver {! R6 g# `6 h0 T7 _/ R0 l
listen 80;
% e( a7 E& F8 o' n8 N% z ? server_name 52os.net www.52os.net;
; G* A8 I% M: G" m8 r; E% F. P
' T$ I% ~- x& P; S" i8 h- A ` location ~ \.php$ {& c2 v( w$ q$ f) S% G
ModSecurityEnabled on;
1 x7 s9 Z+ ~0 z. _( K' ] ModSecurityConfig modsecurity.conf;+ L# N! a8 o2 Q' n, |
Q# i" P" y; ~) |$ `+ v1 |
root /web/wordpress;
- I; b. c8 `% e+ A9 u% D g& T index index.php index.html index.htm;7 }: O3 X3 P! j: M8 _+ K4 ?3 y' {$ Q
J6 n5 Q; Z$ k& y/ e F$ \6 N fastcgi_pass 127.0.0.1:9000;) m% }# u; y. a+ O W! f
fastcgi_index index.php;
* Y$ g: t ~$ W fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: w( o; `! {6 u1 v include fastcgi_params;
9 g$ o9 }7 H% _' Q }
+ y) _- L& n% e+ \; | ]8 t }
) A1 B; G) h9 s2 e1 Bupstream负载均衡:
6 n+ a* d" I: n \8 { Z; G/ w+ D) u6 M$ l' {8 l
upstream 52os.net {; o+ q# J: i7 l( l4 E
server 192.168.1.100:8080;. s! g! g9 g; y' _. H0 u, p" O
server 192.168.1.101:8080 backup;
0 s" v5 ^0 v( t5 \% U% m}
6 z2 I& s$ ], n' _3 S1 Q z' @7 }3 q |1 Y, }
server {- `3 L) k& @" z/ k
listen 80;
, ^, s% L! R4 p9 m" [server_name 52os.net www.52os.net;
/ g# ?) }; f' }! W, O
4 v7 B9 {( p$ u( klocation / {
7 w7 S2 H) M" c ModSecurityEnabled on; / i( z7 G+ e! C
ModSecurityConfig modsecurity.conf; # `3 d5 o) M- ?6 d7 |4 s X$ T- G
8 R7 S5 ^) o+ o$ k proxy_pass http://online;
" O% q( h5 w. g! \5 o4 J proxy_redirect off;# ]! o2 a( `+ b7 e( [
proxy_set_header Host $host;5 A% \2 D. m8 s8 D
proxy_set_header X-Real-IP $remote_addr;% q7 c4 I) @+ R6 [5 c X
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& }4 M2 [2 m( j6 s! B. p }
+ y3 h4 ~. F- b6 Y- M}5 S% v! g- i7 n' w* ]; T
六.测试
/ i8 W; _7 T# i7 o
' h2 F4 T. S* @8 z7 ^" o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- o& P4 p+ s0 E5 Q; f7 m
" O; {' a1 G! H, I7 h
<?php
2 e+ \0 i. @$ r phpinfo();
p( g/ d* o1 U7 _. u- e, U8 S$ F" ~8 Q?>
! D A# V) G. T在浏览器中访问:
. M4 t7 q2 S* B/ X- ?3 i/ h) e c/ a& C0 ?
http://www.52os.net/phpinfo.php?id=1 正常显示。, ?" I% _0 C/ q9 m& [! K: {
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。9 e" \! N: }+ y# b0 y* t" b! v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) i: ?- e) t8 A$ z! r
说明sql注入和xss已经被过滤了
: }' m1 ^' C7 G$ S
; c- h/ T4 Q" E/ Z7 A( W七、安装过程中排错
) c; E: W$ b" R* ^( E/ x; p( a, A( i, G, r
1.缺少APXS会报错
: S5 m0 E- Q- v8 l! }/ Q1 J3 E# Q" a& a+ `" Q) r/ d( z- q! X
configure: looking for Apache module support via DSO through APXS
2 j' j9 }0 P( [$ Nconfigure: error: couldn't find APXS
1 i/ ?: l7 b7 S5 {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 H. c! p. l$ L3 V) }解决方法:
. S1 a1 a0 B* `% g @& c/ Y. ~7 w( h& [( ^ D. e0 o1 c* l
yum install httpd-devel v- Y4 o6 \ e4 t! g
2.没有pcre
6 n( ^1 O& l" b% q/ X" P3 w
2 ~6 d4 q( Z( K( f5 |configure: *** pcre library not found.3 c: B3 {# ~& V' q
configure: error: pcre library is required# H' L$ B! P) W
解决方法:( o* p4 }( U, x: j; b: y; x
. R" F `9 v" D1 b2 N. C9 Lyum install pcre pcre-devel
6 B$ Q3 Y0 ^* }" E- j; R# d3.没有libxml2: s8 R8 ]" {: r& J. {. L" Z8 v: Y
' p1 u) y. e2 i. _7 l1 i! K
$ ^7 k% I6 Y5 ]1 m& @configure: *** xml library not found.0 e" a- y* x$ J: Z; e
configure: error: libxml2 is required6 g2 a4 d. z% T$ Y- P: a6 Y+ j7 G1 Y
解决方法:
/ j) w8 e0 q2 P w/ ~9 l4 Y5 T9 T3 b, M& y
yum install libxml2 libxml2-devel
6 ~- Y3 V7 d2 p2 o2 O1 v4.执行 /opt/tengine/sbin/nginx -m 时有警告) O+ T( s3 e; P& J7 X6 m5 T
2 \& F; Z R* M" D8 W% i1 I' |
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- H* b' Q. w/ d e4 G& w6 _4 E9 pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' l- J A# O. Z& c5 R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 h/ s+ t" S6 v& F5 i
/ y r! E% Q9 i% S3 @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 D7 y+ b/ y2 n! x- @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) T+ L- Y: i" Q. P- w7 ^2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# p1 W! E f0 n. o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 M1 m: c" F0 i* Y' k b) w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& ^- y2 }: V$ ~) N0 L/ u, E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ W* n& X% p0 b9 T1 m6 S, M解决方法,移除低版本的APR (1.3.9)3 y6 a7 E6 t1 w$ {6 v% s& f c- m
( v- H: C5 _! Z% @7 j0 a
yum remove apr N4 j- d$ ~: x* N5 R" y+ l+ ]" U
5.Error.log中有: Audit log: Failed to lock global mutex9 g" Z; \+ u, f9 T, S- }
) j0 w% V% a( L9 F/ z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! k0 R5 Y: m" m; K+ Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 {8 }" E3 z' z$ F( q解决方法:
$ I8 `; K. f% R5 D编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 ^' }# Y) l V( M
! x9 v: l2 q9 z7 H. z+ R
SecAuditLogDirMode 07773 Z7 n5 z1 w1 i) K+ g8 ~$ l# l
SecAuditLogFileMode 0550/ B! }! [$ Q' K0 Q" t
SecAuditLogStorageDir /var/log/modsecurity
9 Y7 \3 [3 y3 b5 L f r$ W2 JSecAuditLogType Concurrent; N4 c( i, i- Q+ A! ?4 D
参考文章:
1 h o0 |0 C1 _4 v3 Nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 j6 @8 u+ _: S. Q, Dhttp://drops.wooyun.org/tips/2614 |
|