找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12526|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: D. J  R" F' k7 W
& G( I9 H% j2 i! C6 F7 {一.准备工作
& j, N1 E2 u# `+ V& A% f
  K) p/ Y7 ]- O0 R7 C% m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 J. n, Z$ I7 c4 Y: B/ o' \
5 l4 |, T0 O. }* D% a6 Q& ]3 _' _6 btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: p# {" l" \4 o* a
$ S* g  E4 H9 ?+ `modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' a9 k6 a! R  u8 X( I" k& `
) G& A( n) O. N* d( MOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 [* y; }: @/ U9 A* o
! O( }2 ~5 a' o0 f. t0 a依赖关系:  ^4 z/ t+ i3 a' k. B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:# Z8 ^4 D# _2 d* @. P) ~
7 S0 o5 B4 u8 D' I6 G
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% L& J* q0 ~7 l4 v
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 H( ~! I7 z" n( _" t/ @/ a4 `# D

  q4 G% S! i& V! b$ G- Myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 c. X  @- H) L2 }$ S( A- I& \二.启用standalone模块并编译$ w9 p$ n: m# }8 n2 ~4 s

( N7 S% x, p- p下载modsecurity for nginx 解压,进入解压后目录执行:, C: t# a" i" x
/ J1 l9 a+ x- {% l* q8 ^! a( @
./autogen.sh: S" ?% k4 J; j$ @
./configure --enable-standalone-module --disable-mlogc2 L# a) y# Q& D9 H) I1 Z, p6 [5 I
make
8 V% Z, K* ?* Z- n3 m1 `三.nginx添加modsecurity模块
$ r8 F/ Z$ I6 L, _
" K) A) I$ B# S* y! C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, ~  E& d9 j& V2 I! E& ]7 S& \
' h$ M; N8 r, y; X* J
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ }' J0 v/ X7 Q/ vmake && make install
6 _' A8 e# P/ K四.添加规则1 E6 t/ Q* ?- h1 ~* q" X' o0 Z" J

" j! f; ]" w, r* d7 Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: p9 g* [: [- g% r* q! C+ v7 `
0 o2 \  [+ L% X8 P1 b1 _
1.下载OWASP规则:
0 V6 {: d, t2 v1 }4 \
* |9 G! D' W5 o6 M; M: a7 Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 E4 Q5 a2 C) y! z* f3 V6 h: p
( G) w. x! Y/ S8 r8 R( _mv owasp-modsecurity-crs /opt/tengine/conf/5 M0 H0 F( T; J7 Y* P' [& t' ?
# k! e) [) S) E/ u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ w& f. }8 {1 I( q7 w2.启用OWASP规则:% d4 Y' F  H' [2 M4 ]& j
8 U! k3 r' N6 d% x4 ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 {4 {. _. O4 W- L+ c% t; Y8 K& B/ e
4 T; f  [; q* K! ^# ~- U编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ b+ x: K/ C* R
1 H- O/ g/ Q) c& z) ?+ p* H/ Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; k) s& d/ q( l9 f( B) e  x
0 N- v) Y0 a$ w% [. {) i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# w# o. k' u* o" U0 }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% s$ i/ X! T5 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 Z# A4 T& U7 ^8 ]) {) I+ M  u( k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: \6 p" q1 P. g. d0 h' T. X# `" eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 t$ A( a/ V# s+ L' {2 F' [- q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. D& g  x& |4 A: zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ |+ t& Y  t+ w" Y' G7 E! @5 x4 O
五.配置nginx/ D- ]( h) ~! }2 ?" n
- N5 H9 S' C# S7 p: t
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ a+ F  ?! L& N: K5 V# D+ h4 o/ k1 Q1 ~
ModSecurityEnabled on;  
3 p8 ?- [9 H; P. ^  p& zModSecurityConfig modsecurity.conf;9 I4 `; M0 Z3 g! v* }8 b
下面是两个示例配置,php虚拟主机:5 d& b$ Z$ B2 z# l1 [- O
& h! H$ R) m+ D. o+ [
server {
# ?7 d$ a: J6 o) {/ `      listen      80;
, i  h& S5 z5 \- J% X5 \: F      server_name 52os.net www.52os.net;
* u* z0 @+ B! |     
7 G+ U, g: q. ?( h7 y7 d      location ~ \.php$ {
: W0 [! S7 U7 j5 u      ModSecurityEnabled on;  
$ g/ {/ e  ~# j      ModSecurityConfig modsecurity.conf;8 ]/ m' l! z, X! p! |+ J4 O3 r
& ^9 N9 ^& Z) {* O6 n) O1 o
      root /web/wordpress;& j) g: j, w- ~
      index index.php index.html index.htm;: h" y% ?$ b! F. H3 R3 H' _
  
- n/ L, N/ Q7 e& e3 n3 j      fastcgi_pass   127.0.0.1:9000;( [9 r& {# \" N" H4 P2 I4 H/ x
      fastcgi_index  index.php;
/ `8 ^" E' d7 L+ p      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 \( g( V# L9 {; R2 D/ [+ ?% f
      include        fastcgi_params;8 Y+ @/ l) K& k+ M0 ^" r# \: P7 |+ V9 Y
      }" p! H) A8 O  B* P
  }( ^) j3 V! j! p' y' E4 ^3 e9 f
upstream负载均衡:
4 o: |% G6 @& o  ]0 d
  I. k! k; w9 u( lupstream 52os.net {
6 c& S' f/ n7 b' j    server 192.168.1.100:8080;
+ F. U: X% }" W7 P2 f) l+ g' w% f    server 192.168.1.101:8080 backup;& S2 z( c! j, {% q1 G- ?5 Z5 p
}9 o! F, c* H+ s8 y( m1 u  |
1 [+ [0 M1 B5 S8 r- ^0 P* K
server {* v, V" {7 i, f% Z* }: A9 J; y( ?
listen 80;
/ b' j7 l* i: Z- B0 ]8 Fserver_name 52os.net www.52os.net;
2 `' e2 O) k3 a. F) i% N2 A
8 e: R1 {* u7 b( `location / {9 J( S( R8 r$ g
    ModSecurityEnabled on;  5 q5 S3 J# ^5 a- ~6 T2 W% [
    ModSecurityConfig modsecurity.conf;  % i3 k, X2 l! Y

* C! W2 B. t( G6 O2 X: `        proxy_pass http://online;" j. j4 e: `8 ?) ~
        proxy_redirect         off;* _) e% t: C% a% Q# K, K( D
        proxy_set_header Host $host;
1 t! l8 o! k3 b& J+ v        proxy_set_header X-Real-IP $remote_addr;
) E$ Q8 M1 h3 B; L; }        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;3 f5 U& Z; ~; @* R" W
    }
1 w) H7 H+ ]8 `3 m' {4 `}
- N1 e4 Y9 Q! m六.测试
4 z2 L" \; X  U/ }$ ~) s' d2 L5 t/ S  B, s  j. ?1 u! \! }7 s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ `: d6 U) s" e0 t: F2 n0 L
8 E6 A" e8 J- a& a# r<?php
8 S6 `( T; q4 O    phpinfo();    - D1 X8 D- v+ p
?>
0 e) M7 z; R% }- @在浏览器中访问:
3 e; ~! N  N! S. G+ L  l  ]( O: G8 ]' T9 _
http://www.52os.net/phpinfo.php?id=1 正常显示。; e6 O) O6 N+ r; f. b9 e
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
8 Y: z9 K5 m% m+ K2 e; Ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ F/ ~% X- E+ t9 W2 u  S说明sql注入和xss已经被过滤了
0 y7 w1 k& @7 C0 s$ R7 B
% w' F/ J% f3 d9 G8 {/ }七、安装过程中排错4 ]9 M" v+ T7 d& |. }; Z: J3 @

; I# T" t) A7 g% T1.缺少APXS会报错
' H+ T- ~2 P$ O0 A7 i% i1 J7 Q0 @! ^) T1 a' E5 c
configure: looking for Apache module support via DSO through APXS
, L& G+ R( o% D% l# ]configure: error: couldn't find APXS
0 j" ^1 k9 @! i( J7 ?0 o/ ?2 mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- F- R8 I4 `" Q" g7 a$ A
解决方法:
1 O- t7 o7 p7 a$ e5 f( r6 f; T' v0 c3 @7 O  Z
yum install httpd-devel+ `" r1 C0 g$ q% D
2.没有pcre7 z: N, }. p5 r' V9 f  R
5 ^3 G( m2 C6 p$ f8 ]
configure: *** pcre library not found.4 o1 m, S! x% K0 M
configure: error: pcre library is required
- u4 M2 }! T" S% f9 \: z解决方法:
9 ]* V+ {. z7 [( y4 Y# `
7 {7 n% E+ T* i2 O1 Uyum install pcre pcre-devel
% h/ A& N: ]: {3.没有libxml21 K3 }5 P8 Y* m4 I
9 ~/ M* c7 e" u" R$ g

% L2 i- y0 J2 [  w( G/ G: ?  B/ f5 Vconfigure: *** xml library not found.
9 s2 n5 }0 p1 b) L( f# O! w+ oconfigure: error: libxml2 is required
3 g0 p2 b8 B* P2 o4 S; ?+ f, A- \解决方法:( X' @, Q% I1 n+ z6 z# a1 e
6 W! Z6 g2 \2 T* o& E: ^. [' }
yum install  libxml2 libxml2-devel( D' A5 k2 F: _. Z
4.执行 /opt/tengine/sbin/nginx -m 时有警告# y: e6 x8 `- N1 X6 ~4 E

/ p, j0 l2 ]1 {3 ITengine version: Tengine/2.1.0 (nginx/1.6.2)
- L* v$ y. G% d9 n3 M0 P1 t/ Z$ Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( ~4 c- N, `7 q' S3 v原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# E- D5 i+ r+ y& {9 a
+ s8 E: f# O+ j; y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ Q& m4 @9 C; q" `0 V' u" P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"' H8 M  \6 ^. h; ]2 y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 S$ d0 I8 Y0 ^  C* L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* a: o  n* K( |! G# f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" P( ~9 V. @+ r1 |* N, `. V) d2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- |' Q9 I6 b2 L7 }解决方法,移除低版本的APR (1.3.9)
+ E7 C4 b0 }( _( b+ K% D
8 i. k+ c) \2 I! u& x8 cyum remove apr
! v' t1 y5 J" ^: A3 L5.Error.log中有: Audit log: Failed to lock global mutex
6 }' |: Q# L" R" ^5 Y, o) E& ]8 }2 X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( _+ t" M/ L  iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& B: N% p5 @% p) q4 O/ [$ k解决方法:: o% h  G& _: s0 B. L# q. ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. I( i1 K- {& t! e! I
5 s( \8 I* }" FSecAuditLogDirMode 0777
( B6 m+ ^' ]" mSecAuditLogFileMode 0550
1 a' B$ r, y( M+ E. u1 [# q- PSecAuditLogStorageDir /var/log/modsecurity
6 W; A' i% A& e, Z' XSecAuditLogType Concurrent: p! h" C/ U3 I/ `; |- i  W
参考文章:
( G3 B# `# `/ Y6 D& I$ ]9 Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ k7 U3 l, S( ?& K  ?
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-14 02:25 , Processed in 0.071573 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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