找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9336|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 c3 E7 m2 _5 t/ t- S
8 I2 c0 f8 u- i# u- R( L一.准备工作
* |" E2 H& a. {$ W: u. [+ X7 ]$ O5 _
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  Y/ t- g  Q7 k9 r, V/ B: U  N

! W0 w* K3 C/ T* {. V0 htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; M7 P0 u0 b1 p, E/ |: y% q/ ~  j* J  o5 A$ W; J
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  Z6 I, T1 \& V9 ?+ m' @6 C. c2 d* b* @' b& I, K
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( F* k! Q+ A2 m8 B% d9 T2 r
% A; j7 [7 x: R5 w% G依赖关系:
0 F' [, \, y% v" }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& k6 T2 i3 n4 f7 h! Z2 C( d  B4 S
) {4 T! U; k4 Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel  a4 a* c; [5 z2 C
modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 E& ~5 B# J! P1 a9 l* k( {$ E2 r: b9 s+ |6 F8 k' w! B$ k. ]
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
% D3 X5 ?  o9 v二.启用standalone模块并编译
* q9 ^  A: A4 L; h. M
! H2 Y" |( V  l, Y" O( I下载modsecurity for nginx 解压,进入解压后目录执行:$ k* p5 q$ ^4 X' B, L% ]- b

' s, {$ @9 \6 ^3 m./autogen.sh4 v) z1 R" S6 O) M) o2 e
./configure --enable-standalone-module --disable-mlogc2 I/ U7 l" y" S( r/ g9 O- m! d
make ) v9 _2 _/ q: V/ v& Q7 V9 g$ h, ]
三.nginx添加modsecurity模块
$ c$ A$ |" I$ j' A. Y: n
" }6 y: U3 @2 B( I& ?( a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& Q% w/ q1 F9 D* u. A

0 r% O8 m) v4 V# `6 i/ v  r./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 u1 y* g: K' I* l: z! r2 J: {% j
make && make install0 {  T) |. [- _& F$ ^) t4 Q0 S
四.添加规则
" d2 o9 n/ H% f# ?6 N. {  Q6 P8 S
2 }$ w- R1 ~! S+ smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ o/ \- g1 Z5 @, m% ^9 U. ^9 T% X
3 V8 i& s. o2 `3 h% _, |
1.下载OWASP规则:, x7 T$ a5 ^4 q/ s8 M0 D
/ ~- s' p  g% K, a
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 z4 _) V! Q( ~& U% i4 T- X1 P1 @1 c2 O: j9 }
mv owasp-modsecurity-crs /opt/tengine/conf/  y$ D& c$ u# z

9 G4 t$ h2 H8 V. u( y) Fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ p) Y3 J3 x- X( J4 o* j2.启用OWASP规则:
  J, m$ R# l6 t
9 I2 a$ r' t# r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. ]. }' F2 A- ~, V* G+ `$ K' l: m$ p6 r; w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( Z+ L! _" A( A+ `+ M( i- U( e* _8 i2 k. Q7 J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* B* H+ @2 y7 L  P. ^9 u" `

( n( r; r- u! P' RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( @4 c9 Y" T0 s6 A! I1 o& u( SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 P7 V0 j+ Z5 T, gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 J. T- w5 M" |. m, F7 ^" [* R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 F6 a8 ^3 q5 a. i4 l, ~1 q- e) oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 ~# n; U5 M- A+ ]4 E7 s, Q; PInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 O1 u( y1 x. T- ~Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! R4 n- z/ E/ x& h% l: b3 n
五.配置nginx* n; E0 J9 G/ \

, s  x% M& y# z! G- z在需要启用modsecurity的主机的location下面加入下面两行即可:% L' s9 D9 o3 y
9 }: W8 y6 u" E) h4 s( ]
ModSecurityEnabled on;  
& P5 T% z; D2 ^3 e/ q# F4 wModSecurityConfig modsecurity.conf;
& T- g0 @! k5 I下面是两个示例配置,php虚拟主机:
8 S: H/ A+ Z2 v! k! H8 P8 n/ o+ O9 m  G: e( i6 a
server {
8 [8 ]% ^: a' g, {- L      listen      80;( w' f* S* G1 x* C& L* F% A
      server_name 52os.net www.52os.net;. Y* L9 f. `5 f3 ^! i6 Z) L
     , z4 ]0 i! I1 P) G
      location ~ \.php$ {
, T$ y! n5 P# I- T" B8 q      ModSecurityEnabled on;  
, `  E; J: L1 c& v      ModSecurityConfig modsecurity.conf;
- M) C# y- d5 ]2 f( {6 j' n7 f9 F/ p
      root /web/wordpress;
) {8 i8 s% S  |7 P0 n) t      index index.php index.html index.htm;1 s! C* N& U1 x. _3 a
  # f$ Z8 Q) D; p. ?8 `6 U8 V5 a
      fastcgi_pass   127.0.0.1:9000;
: p: p. \4 {8 d4 R1 C& h" U      fastcgi_index  index.php;
# Q! h9 Q6 A& x0 H      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# K8 @/ b& ^2 f& i* m! s8 @
      include        fastcgi_params;
% n0 a! V5 B1 C8 v! g- r      }
3 d4 V# u6 d" j: a" q1 _  }
) `- }& y4 ], q# Hupstream负载均衡:
: ?! b" U0 |; d; |' l) Z, F/ ?2 k
* d8 k+ ^$ E1 ]0 g2 vupstream 52os.net {+ {( G" v/ n- k8 x; T
    server 192.168.1.100:8080;3 K1 r# ?+ z2 l0 }( i; V! T1 m
    server 192.168.1.101:8080 backup;
1 e, s5 x3 _, z1 |5 Y}
6 a3 l1 O$ ~+ F- ]: v4 a# Z+ E* h% k5 ~: m' \: e) Q* w, N% ]/ g
server {3 w8 C& o# F' m3 n; B4 ~
listen 80;2 ^7 M; F( g/ e1 x; K- g
server_name 52os.net www.52os.net;: b( h( y7 g: }1 ^
  g8 B: C7 \/ v5 h
location / {
/ K  f; a! E1 U. u    ModSecurityEnabled on;  
% I4 o5 E4 R) C9 D) l1 v* a    ModSecurityConfig modsecurity.conf;  
1 n- L0 x2 i. P+ s4 m' \. s
) q$ l. U. B* z/ X5 b        proxy_pass http://online;
9 ]0 p8 O, x, N, d1 r6 O) k        proxy_redirect         off;$ i& o/ S: g% E/ \  q: x
        proxy_set_header Host $host;& Z" _  ~8 H8 V
        proxy_set_header X-Real-IP $remote_addr;5 c9 z! `+ z5 O1 i/ X
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" W1 q! l* B. m  t  p, y/ G6 S    }, f; x# h  s9 ], B2 n
}, R7 X& U. e) [7 Z/ t
六.测试7 b7 W- Q& y9 y3 L6 D

" P0 X, g& z1 E+ }' ^1 V) e) }4 A我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% ^9 a* H1 p4 ?# m
( D+ e. x5 A, B6 ~7 V, G<?php
5 m3 Y# S8 h% h, [# p    phpinfo();    6 t3 @8 C( ~. M. @+ U4 [9 p' |* l
?>6 s2 O7 P0 d: ?( Z& K1 r
在浏览器中访问:4 H& y4 t1 c0 z4 z3 x3 d

. {9 [' H" d; Zhttp://www.52os.net/phpinfo.php?id=1 正常显示。3 G$ {1 g% Y* R% O" P! Y, K
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 z: ^, j7 X' b) e
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ m( `  U3 K$ \9 M$ [说明sql注入和xss已经被过滤了
2 a7 X; s2 I" _; c4 t+ G
9 v% Q# F, u1 ~: J# s5 }2 }% w8 j七、安装过程中排错: y' J! w  z9 k) Q% V3 \

+ Y4 N" w3 j% r: ?4 a1.缺少APXS会报错
4 j4 `6 y& P! d, X/ p) d
4 ?* ~& q+ M# j  s* x2 J) tconfigure: looking for Apache module support via DSO through APXS
1 U# F: Z2 V6 D+ V2 t; I) @$ Iconfigure: error: couldn't find APXS
6 d0 k" g9 `: B. Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。% W, ^( x( F, Y3 z8 z
解决方法:* {" D+ V, _2 {) a% D1 d# x/ g
2 v8 o$ H% u5 N+ N. M" O. X
yum install httpd-devel; X. o4 b% y1 P- ^( Z
2.没有pcre4 A) H. ?7 T# j% {
* u; ^; @- ^2 i  u+ B
configure: *** pcre library not found." S; I! i/ |4 x" x& E
configure: error: pcre library is required: w% E2 l3 }2 a, @
解决方法:
# g$ ]: _8 S& f9 i* w6 p* `
2 d9 q  H& J0 z4 z1 Y0 y; i, dyum install pcre pcre-devel6 ~( T5 Q8 @! e9 E/ m
3.没有libxml2, a/ }4 O. K: m

  [1 W+ y! m, Y6 F, b$ y1 T8 e! j/ e2 P
; Y& z5 C1 E  iconfigure: *** xml library not found.
* p& u7 \; \% q8 p% Jconfigure: error: libxml2 is required3 d% E- L  {; v, C; w
解决方法:
7 K5 ?( K: H% ]4 B; Q! I- r- O
+ T) ~- a) _+ p5 G+ }8 J( d: ~0 t; vyum install  libxml2 libxml2-devel
( @6 Z; t% w7 f8 f4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ n# X& I2 J% k
+ y" S  C; l: O( {5 O! L, yTengine version: Tengine/2.1.0 (nginx/1.6.2)
# ^0 L' `2 Q( N2 H' x" Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 P! E5 Q# ]2 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* h: m) ~2 u, ?, w, P
( b  Q1 v& s. o) J" [( X+ h, d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." I$ k9 Z# E; B8 {5 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 {4 n" m5 T2 \1 p. ~! ]/ ]; Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  U) s: d0 |! p. _+ P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 S. G& T2 @& s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") x3 \4 b: [" E: L: }/ z4 c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 r: M# [0 Y, K% |8 ~
解决方法,移除低版本的APR (1.3.9)4 B: w7 Q0 W, n2 [( K) ~# ]5 x
; I* x4 S1 V+ ]* d# z3 v
yum remove apr, {' d- |2 k0 M2 G4 E( c
5.Error.log中有: Audit log: Failed to lock global mutex, P7 x! d7 E* H" }# _
5 ?8 c# H* y* y! g4 I; D9 e# j2 V
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, `0 s8 m/ ]1 eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( }/ x5 r; `' j解决方法:
) r4 ]+ P8 H& L% d5 s  i编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 ?# q, Y/ ^  Y0 }: @* i
7 y; I% w+ T0 O, L+ L6 \SecAuditLogDirMode 0777
! M1 m. B( v- ]  v, @0 SSecAuditLogFileMode 0550
( \4 M$ x; V; `& Y" oSecAuditLogStorageDir /var/log/modsecurity, q5 b* u  d0 f& D/ |# f9 U, w
SecAuditLogType Concurrent4 c: n# ]9 n! c1 u* S! c7 b4 |
参考文章:
+ y$ ~! Z8 B, \7 Q, j0 z4 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ r2 ]  X: J9 I+ B" c! S) @http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2025-12-7 10:22 , Processed in 0.062987 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表