找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11070|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 W* o3 r& f' @5 a% A/ R6 H
* t# {* q3 {. u$ q一.准备工作: O- i* H4 \: b: y2 L
# }$ V0 n8 j( U4 |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 l% k/ o3 I1 r+ L$ A% P

5 ?. v7 U' W; P' Ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# N: i; C# j; ]* s- H
/ |% x5 P2 Y' C' }/ ?) r1 N
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 a! d! e5 d+ v4 k
9 z& v8 W: o  i7 H1 A, i; ~% ]& G' kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# ^* j; U" A  a

+ v  l" ~% L3 |# u2 n, {2 I, X依赖关系:
+ z/ p- \9 V0 B9 M- c( H- Z& @tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& q* M/ x7 h' o7 w7 ^' |
: e0 J  l. Z% ]$ x
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ }! E0 T/ Q5 o7 Z# o/ i
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- N5 g: Z3 h' X" h' @1 y2 F0 M7 k- ^$ ]/ Y4 w
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) A4 {# A6 z4 H$ S# Y" E8 d
二.启用standalone模块并编译
7 J# C1 a$ H9 g+ Z6 R! I  n$ G, \' X9 j/ S
下载modsecurity for nginx 解压,进入解压后目录执行:; w9 v0 t. _+ J
% H" `. w3 W$ U$ U
./autogen.sh
' W* t2 R  e: H( ?$ T$ d! d& \! o./configure --enable-standalone-module --disable-mlogc
) i# Z  G* g( {: Y1 {1 _make
. m5 ~0 `, x: p8 \" E9 }: N. j! i; E$ m三.nginx添加modsecurity模块7 r7 i  f2 A( o5 g% P: [9 a- J
: {0 s5 _0 Y/ F
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* ]" ?; J3 Z% S1 F) f% i/ ?9 X/ `$ T! I% U( W) c2 Y0 u
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 s" m* e/ A8 x5 Z0 Dmake && make install" ?; l# R8 r5 \% J
四.添加规则' |2 }" Z! Y: l" x
, d7 c7 K9 e# g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! ]6 |% t. m. W. @7 u/ I1 Q% ^5 [( c' E, |
1.下载OWASP规则:
. B; X0 z, E) y# d3 W" Z: b7 R1 A6 r, I
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 j6 ~* d$ `: S9 n; C) c% i* K) t
8 y1 j! C5 Y4 ~4 Z; qmv owasp-modsecurity-crs /opt/tengine/conf/7 [! t" R* @! v  l

; t! q- ?( n7 X# w" X$ vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' `* D% \* E' l5 V4 d; k0 F
2.启用OWASP规则:2 [! b. K8 V, b0 C' B2 D
. X2 K0 v2 E( ?+ d
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  w2 q2 j+ i  p9 M. Z$ ]) o) [4 u' A, Z' K
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 i, ?7 i# K8 u0 Y4 g
5 l! B. B" V1 h2 Cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' l" y" G  u. i2 J5 [9 S7 @, l8 m* s8 c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ B6 d7 v8 K9 \, O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& h6 _( |: Z" ?& Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# ~! n0 r/ i2 W; A- P7 o% m$ {* uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; g5 b6 I9 J# g9 U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  C2 P7 H$ U7 I5 g7 i5 |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; R' z; T! T  [6 T, V7 o9 WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ K, |! ]1 Z' a* `1 L
五.配置nginx
9 R" C( s3 ^& b% q$ v: ~/ ~0 E2 D0 p" q8 c2 I
在需要启用modsecurity的主机的location下面加入下面两行即可:# `0 I' e2 W: y. }7 k' S

5 J& p' w; h- R8 }; u% p% h8 ]ModSecurityEnabled on;  3 l1 o! F3 y% J
ModSecurityConfig modsecurity.conf;
" ^. f; k+ K# F- F# r下面是两个示例配置,php虚拟主机:
) q2 |5 X3 O% L' k5 J$ p/ o/ I
/ j3 q: ~" G) Q/ P% f( J) _1 I) Mserver {# m- G2 B* Q2 [+ L: h
      listen      80;
+ \- {! V& X# u% c3 ~      server_name 52os.net www.52os.net;
* a6 J, k1 g- H" K( J( S     % R5 a* f% Y* @- ^8 Y/ ]. q* J" r
      location ~ \.php$ {
- g# b1 ]* Q! n3 B% X      ModSecurityEnabled on;  
3 P, Z6 Q7 G$ [/ [9 `      ModSecurityConfig modsecurity.conf;+ k  \& b- X! f0 M, D; S

: Y. l  G" a7 Y9 e9 B( `+ }      root /web/wordpress;. I6 s% X/ V+ Z3 Z& `
      index index.php index.html index.htm;/ P' j- [5 d8 f
  
" L! ~) U) e/ l6 W) V      fastcgi_pass   127.0.0.1:9000;
5 K& E# ]8 L6 Z9 @* I! b# K      fastcgi_index  index.php;
* Y- R8 J& A; J; L1 K9 L7 S/ C      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' \& H- C& E! H% T3 T
      include        fastcgi_params;; y- w2 y0 `2 t+ M3 K  d$ D
      }
8 B$ H8 G2 G0 I  }
' Y1 \; O; d; `: ]% f* ~upstream负载均衡:
1 ]/ Y0 i) M) l' ]$ x: j) `- J8 h  M; j9 V7 f
upstream 52os.net {+ Q/ t! }- z) E+ U
    server 192.168.1.100:8080;
! t* W, l4 @7 D' D: |# H    server 192.168.1.101:8080 backup;: N$ f7 o( y* G: ?. `
}
0 R7 J2 B1 Z4 ~8 H, G1 C! D! G  H; K% R! `& |9 y0 b" y
server {0 Z% X6 B6 i7 M" u* z( `
listen 80;: r. a4 W2 X1 H$ s! r
server_name 52os.net www.52os.net;
. x6 v7 H# G. G' {) z( f2 b! x* D# P9 t7 x
location / {
6 D' O$ V3 s  L, l/ k7 [; F    ModSecurityEnabled on;  4 t) I6 G1 P3 D5 M5 G' L
    ModSecurityConfig modsecurity.conf;  / v# q6 Z* o. T3 w1 Q# Q
0 Y6 r0 `( g2 ~% C
        proxy_pass http://online;. r0 Z% c! H* w: o
        proxy_redirect         off;
( B& ^5 f5 p, b& o1 C7 Y4 `+ k        proxy_set_header Host $host;; r5 _2 M! @5 U* Z
        proxy_set_header X-Real-IP $remote_addr;
# w, P/ g  m5 t% O6 k5 C        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
; N- T) w6 g% h- I. z4 _! a    }
1 N' [+ [. o- X" o}* \8 O5 ?9 J2 J: S& B) ~
六.测试
# i) [) B# H6 V/ Y! {: g; q& E1 }
1 [9 v' Q. f2 ~9 G( j我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; Q  X5 t6 |* v

" T- ?+ i- `# S<?php
& c5 r1 L6 |7 O% P! Z    phpinfo();   
2 @7 p* n+ [; d' H& M6 _?>
! u2 B# N) N/ k4 x  c" ^6 B在浏览器中访问:  f1 C1 f" y2 {* n# e
* e# E! J% p" E2 g( C, l+ L4 H
http://www.52os.net/phpinfo.php?id=1 正常显示。
! X& {, H" s' h. g" zhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, f( C- t" C+ I+ C7 B" g! g/ f1 m4 l. D+ r- lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ b2 n$ ~9 M. U. _$ b说明sql注入和xss已经被过滤了
2 f3 u8 l8 v/ {. `' J5 ]( w
. l9 `4 X/ t5 X$ D七、安装过程中排错
3 n1 L% J/ c+ G' o$ n( ], K! \6 s9 Z& |8 d% X# }) K( A( U# H
1.缺少APXS会报错4 d/ _  i/ P. ^1 P' u, x" |

8 A; U, o" Z5 ~+ q$ Z% uconfigure: looking for Apache module support via DSO through APXS0 U+ H9 Q' R- i' A5 C! `
configure: error: couldn't find APXS
4 m. b2 l% x% _apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ @& @* B/ x5 G) M
解决方法:1 I) e& A8 D4 ]  j

1 @" g7 H5 w" N" R4 T) C! qyum install httpd-devel
; I$ k$ x( A" l( D; G+ f8 U+ z& j9 U2.没有pcre
- S- P9 ^) j3 \$ V3 d
2 \( O: l6 d: J* y  ^2 W1 Z$ `, iconfigure: *** pcre library not found.2 r( x* Z. t  _+ D4 F; f9 G6 O
configure: error: pcre library is required, a3 ?7 E$ n* B
解决方法:
* Y) s$ l7 i$ v: E8 B- t
0 E( ?- k9 C  J% |7 K3 Gyum install pcre pcre-devel, T; r! K" u0 `  C% g# A
3.没有libxml2  X- ]# H; A3 O" O
; w0 A6 g/ A8 E3 _3 ^

7 k4 U3 y! J6 }" Y7 y2 Y; Tconfigure: *** xml library not found.3 |  ~2 k, B) q! }. S. w
configure: error: libxml2 is required1 r4 @* D& O# C" y6 Z' S
解决方法:4 M7 }, n" \: F  X

! `7 j' Z7 c6 ~+ [* p8 vyum install  libxml2 libxml2-devel( H9 k, Y) l0 C: d
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 D7 o4 m% [9 g( k& c

# Z' V& b9 G) ^' o  V  N+ t! tTengine version: Tengine/2.1.0 (nginx/1.6.2)9 D: d1 W( F( m: w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 I/ P/ f$ Y( V# q# p: N+ P3 Y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! {4 S2 J5 }) {& l& l7 ^5 f
8 s( n* K' j: C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) o1 }8 b: l0 h- F* ~& w2 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
# ~! n1 \0 m3 W8 A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 p# I0 C1 E: D$ o5 G- f2 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ Z5 z+ i* W, a1 v: ^, b; C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 H+ ^0 N( A9 O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! L0 M7 A# b+ }解决方法,移除低版本的APR (1.3.9)
  U6 t' P. \, b9 o: p" V# |- Y8 r1 t9 X# O0 ^
yum remove apr
8 a2 W8 i4 A2 h5.Error.log中有: Audit log: Failed to lock global mutex+ ?/ f' s! _4 `: \. u3 c& y

( G) r  I1 x( U: ^4 U. Z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 w/ ]. S% s$ g
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ n" N: m  \- F3 p/ v  s解决方法:0 O+ R) q. V; z. ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ b/ Q, b$ i( i" A4 ?4 E7 j2 f
' y/ H% H7 A' e7 H3 Y5 S+ mSecAuditLogDirMode 0777
; h: Y+ ^& `- U' @( ?SecAuditLogFileMode 0550
( U1 e8 n! |; q' O+ pSecAuditLogStorageDir /var/log/modsecurity3 p1 b+ P# }; O* y  r. {; z
SecAuditLogType Concurrent. L0 j% ~1 b, o
参考文章:
* i) G, I6 C4 Q" I; rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 n4 Y. {& K( g1 u9 b
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-17 15:32 , Processed in 0.067978 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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