找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12624|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' a( m5 G8 e# q+ C  N/ S

4 N  |9 @9 [8 _( y+ g, ~一.准备工作
' r5 F' |- ]0 J
, y8 A9 r& A  k1 @5 P+ D4 S5 [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: ?( t# g4 I2 L$ U, ?

0 p0 C$ Z% \. H7 }: _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 u. ^" u" ~$ @
" x: B6 \' G3 }/ o6 \' L) H) H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- ?! D/ z4 e& o3 I4 D

* T* {+ Y- \3 w# Q2 `# LOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 j- d) r! U' {$ F+ t7 z# y
) [7 g. t9 H# ]1 c% T  P依赖关系:
3 b' z" G0 Q: y0 I& R4 U) ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- e3 n. b+ t$ o2 h8 _) M) T- q# F* |- G( ^. f6 H, W4 t, E
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel0 r. r' B) }: e0 U2 O
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 x0 y# J7 ?2 U" _8 f
* ^3 a( |1 l1 M+ a% @1 zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) j7 f$ j; i! b# ~二.启用standalone模块并编译
0 {' k; f+ C" q, C
/ m/ X6 {  g' g$ a, v9 M下载modsecurity for nginx 解压,进入解压后目录执行:3 k  M/ \; s6 }& h
/ ^6 h0 Q) T/ |! U9 R' m# i$ {0 S
./autogen.sh6 W  F2 T& [3 \8 \7 W
./configure --enable-standalone-module --disable-mlogc' T- U  L7 E7 x6 W, f
make
: ~! _$ G' K4 G& M5 x7 j& D' F三.nginx添加modsecurity模块
2 A" w! D% n1 B; }8 s; y- l' ~/ _2 \+ y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 o/ H0 H9 k$ m4 I' `: h
7 `0 E% U6 `' }2 u
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine# u0 m) p' ?1 ?1 |1 J
make && make install
2 K$ }  l* ~$ C0 ^6 E) T四.添加规则+ h# E% ~1 A$ z5 h( L; b

. @" v6 j. X" mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- c1 g1 @* F+ T0 t5 E
% X, f1 ~' o1 b1.下载OWASP规则:
3 J9 x) d9 F* r+ n# ~, R
( r0 @8 n1 p4 }! b7 K3 [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 n5 W% Z% D  o" a2 U

; U' ~- p8 t1 N9 Kmv owasp-modsecurity-crs /opt/tengine/conf/; w  x4 F% F$ V. {1 c6 k

8 X) K% v" r8 Y  E; Icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, }8 c' F7 ?; r1 X8 x9 g( W; O
2.启用OWASP规则:; o" p, H* r7 [6 P( N' O0 H
1 S4 r4 }) N( J* J# A8 Z: A+ W5 `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 S# h9 b6 a* S1 Z, l% T* O: ^5 T& N
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 H& V6 w/ q. ]

! L+ P$ g9 ], C) ~/ lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
1 R: V' }, e6 x( W
2 Y4 p& I# D+ K# M9 R2 `$ fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 g% p7 X8 k# Y) q4 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# D1 R0 e% l1 o' }9 {% |: J1 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) D: ?; u1 t% Z6 y' X5 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; s5 p5 z" n- Y/ d8 L3 M; TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! S) Z, _- d) X# H8 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: F  v# M/ Q& i; RInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. o+ U; b+ B/ x0 _9 c4 p* l
五.配置nginx
. A" U  x0 D: K! f+ z; V
4 H, U, d8 P9 U" f. A  E7 Q在需要启用modsecurity的主机的location下面加入下面两行即可:
8 G- r6 ^. \  M: R$ U; a+ V# ?* J! [+ ?1 d  I4 D( _1 y
ModSecurityEnabled on;  ; x; i# E. m" l# k) v* n
ModSecurityConfig modsecurity.conf;
8 q, b0 L9 j3 ~下面是两个示例配置,php虚拟主机:# o. x6 c( p+ m, k" j/ f) |

, f9 ?% J  s/ [" L7 tserver {4 p& J/ z; O8 Q4 N' A! L) k, V  _
      listen      80;) [* j% T" a- n2 a  `, r1 A
      server_name 52os.net www.52os.net;
- h- F$ I$ B3 }# }8 |) U# v     
, N0 [( Q: g" p) f" K/ c      location ~ \.php$ {2 ?* G0 e' c8 @+ e/ W7 c: x8 W8 `
      ModSecurityEnabled on;  
* u. M/ \& {. o; B# a      ModSecurityConfig modsecurity.conf;
# }7 {2 R* T) X# r, y' \' w: j' ]- X/ H: @+ f7 d
      root /web/wordpress;7 e+ l1 N4 e- m2 k; d
      index index.php index.html index.htm;/ W+ ]( c! s& x' W
  / |+ z, Q8 N/ Q* C' l
      fastcgi_pass   127.0.0.1:9000;+ N! i2 [. i7 r2 ]$ p
      fastcgi_index  index.php;( q4 d( m2 a+ v6 `  j5 S
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: J- \% Z' ], B) k" R! y5 e0 t
      include        fastcgi_params;( S0 K4 J" h8 K7 l, p
      }
/ L1 |  `( ^& K8 x  }
! T( R1 F* Y7 r2 W% Yupstream负载均衡:+ _. W* G, Z  N7 \# g5 }
+ J0 p5 g; F' B( g( R- ?
upstream 52os.net {1 N. m# @$ l+ J
    server 192.168.1.100:8080;$ h) @- i# k, v$ r
    server 192.168.1.101:8080 backup;
8 j7 y& l5 A- u}& q% I2 J0 S" s: V1 h7 ?

( y, M% w. k. D" S$ R: o. a1 sserver {% u4 c( O9 j1 J" F- d% H6 d/ k
listen 80;
& c1 e# z, s( d2 U9 B, j5 tserver_name 52os.net www.52os.net;
6 V( L2 C* H- S8 h7 ~
% s  E* q/ B" s7 Ulocation / {
/ o9 U# T. D7 W+ k    ModSecurityEnabled on;  
5 l" {% r  I' Z" s* y, b9 D1 n    ModSecurityConfig modsecurity.conf;  
* N8 ^+ x7 Y* G! N3 J. p8 ~0 r) G( i/ c; }
        proxy_pass http://online;
& s9 w2 l' m/ L: s        proxy_redirect         off;- _- ]3 }6 f. E$ }$ C" p
        proxy_set_header Host $host;; ^- {! U5 L8 O0 b' K
        proxy_set_header X-Real-IP $remote_addr;
, M  N4 \1 J: t/ Y. n. V& }  m1 i! n        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 S& p! j" }7 ?  O    }
( h( F' c  Z& H, |& E}" ^9 Q. I7 z& g$ A$ r4 d. d2 s: \
六.测试2 I' Z0 l& z! ^% u* d5 O
: B0 P+ Y& `* @# q$ s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' l3 G) U$ @6 j% }4 G
. Z# o7 K' V' c4 N2 {9 t, @. X4 G<?php8 ~- p* c! q! `5 \8 K' [
    phpinfo();    , D0 D- n8 F* T! t7 `4 b
?>
6 q0 b4 P2 ^/ A. Q在浏览器中访问:; l' w; R0 o7 d+ S9 L
% Q' K, `1 S' g- R; Q  Y
http://www.52os.net/phpinfo.php?id=1 正常显示。
& k% E0 C! M$ E% H* o! Shttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。; }; M7 v! N3 ?3 D( {) N. m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。" r# Z1 H! X1 g/ X, V
说明sql注入和xss已经被过滤了
5 A) o# n, y* H$ m2 N; c3 _; e) Q# @
七、安装过程中排错5 M: m; Z, H7 M

$ \! _# B9 H% {# E2 \. Y' ~- _" \1.缺少APXS会报错
9 {& C* C% T* g$ o0 w1 a9 L# `" b# a) g* _% U
configure: looking for Apache module support via DSO through APXS  }8 R2 F  c& y6 t2 u
configure: error: couldn't find APXS
* j2 p" q! h' t, e# napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 B/ O1 U) e3 Q  L+ J+ _! `  B解决方法:
3 `* Z' H! Z) d! n1 `$ J1 B; o* f" N; k4 |! e3 h, v: w
yum install httpd-devel
, t* F4 `+ O) u2.没有pcre" A0 ?+ k# W1 S# a
" T& x1 Q! L& r
configure: *** pcre library not found.
$ i0 }. @- X8 ?5 d  Econfigure: error: pcre library is required% W: D$ O& e4 A, H. ]* P
解决方法:
* H* p% U) k7 c' }/ |
& O$ q+ Z' l, h3 B) {( yyum install pcre pcre-devel
$ ]- V( y' X5 r6 L: \) y# k$ f1 `4 N3.没有libxml2
: ^' ?& Q; m* a% V6 `$ I9 X
& h  R# d5 [  I) u) R
6 \9 _. K2 c9 o6 L) @" U! cconfigure: *** xml library not found.* N4 k/ N- ?- U3 T
configure: error: libxml2 is required4 V4 z  C' B; _3 z3 e
解决方法:" x; U9 g5 b* `+ g, M3 d/ h, E
; U& O7 H, j# H9 N
yum install  libxml2 libxml2-devel
+ |- e7 v" z( c6 k! }4.执行 /opt/tengine/sbin/nginx -m 时有警告
% t1 U& U- k3 U( L3 P5 ^" `% n( D3 x3 i
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 T% O; S1 w( ], W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 a! {6 F  \$ y  R+ d8 q5 [: D
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' I6 _8 N# z/ |

  ?3 W% w6 m, b. P- X% i5 Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% u' r6 q4 q$ y4 C3 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 [- E3 Z1 o# Z& A. m% {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" g! M5 G" e; d9 W( n$ m. r# ~6 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 J+ A7 D' e) ^. D+ L9 p# ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  g+ [. _3 a$ w8 z9 N( i3 e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) l3 r* t: [$ q* J3 ~; ?7 o解决方法,移除低版本的APR (1.3.9)0 w! j8 a1 t& P$ b

. N# U4 i% b( K! T' B$ Lyum remove apr) ?! S( x& m7 ?- n. ~4 Y
5.Error.log中有: Audit log: Failed to lock global mutex7 M$ |! \+ L  x
, _6 D( _$ U- q1 A
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 K- [6 C1 W" M& ~, f: Y8 Yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' u2 d9 y, Q8 N+ l2 D
解决方法:* H. Q3 H7 I, Z9 W; r' t
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 z% d0 }! ?8 |

' Q8 z# E" s0 a6 P. p. gSecAuditLogDirMode 0777
- U) H6 z9 @* U  y% M1 Y3 aSecAuditLogFileMode 0550
9 x7 |1 |% p" ?1 @% W/ m9 c( XSecAuditLogStorageDir /var/log/modsecurity8 u( Y6 X( f8 V
SecAuditLogType Concurrent
! ^+ X3 X  V0 z+ U% r参考文章:7 V( {% s- V. f& S& M  U! V
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ y. o, l7 E6 R; m2 w
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-24 01:46 , Processed in 0.051714 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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