找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12842|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 ]* U# U" P1 A- W
, H# z/ x8 R7 e, `- \( I一.准备工作
  R3 ]1 t. |! |4 P% B5 y/ w, V5 P, A" }: i( k
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  X( X0 [, Y% t9 ?2 N# `
7 G. F2 k+ t5 W- |1 Y, A
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ J  n& S# q/ \& u! u- |6 ?/ h2 o9 [; l6 S7 q/ ^# s
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, d0 P9 `$ _1 r9 a/ |# v  h0 F. V( w" b" {# m  z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 H/ ~8 l9 Z. j# B: \% B( s7 Q! w! H; R5 q6 X* P
依赖关系:* G" `3 Z, F' R5 W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 R& }; L$ N5 W* N- l! ]7 L. ]. b1 z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. E# l1 ?" d4 N  {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 r( g6 l; _2 O9 }! k0 ]
2 @/ z5 j4 z0 V( hyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& K1 t+ N( x0 n5 j  e* e9 e2 I二.启用standalone模块并编译9 W! K) z: ~, O  q- K" z; ?6 {

9 ~" F. ]. }. S. @& s# Z下载modsecurity for nginx 解压,进入解压后目录执行:
4 ^$ q: ^. K2 R
0 ~3 M; y) Y/ J, O3 J3 k9 Q./autogen.sh
# M' H5 ]7 Y5 c' i$ R3 @: e$ r./configure --enable-standalone-module --disable-mlogc
$ o2 Q% H$ B; R0 }1 C* ~make
+ i3 y- g( {* P7 B三.nginx添加modsecurity模块5 C" n& j; t- Q0 ]$ K) U

6 X. i9 b2 P1 l6 b3 F+ m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ s3 h/ u2 X  _) x% R
+ Z# l3 t, X3 s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 T6 d/ K2 _- E% t) F
make && make install
' t- D& P2 K7 j四.添加规则
) W+ V; p3 ^; b3 E! r
7 D- z* K( k$ j5 w/ hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- I# e+ q, a. i. C5 F# N
6 i- D5 c# \, Q2 X
1.下载OWASP规则:# t3 S% o0 F! o- X& K) \

% `) r. p# _7 \: Z/ \$ ?. Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 J% U# t$ h: ^% k2 j9 C
1 _4 R4 H+ N2 D, G" H& t' D
mv owasp-modsecurity-crs /opt/tengine/conf/) Z! \" Q8 `+ k3 s- ^- C7 f

# m# B/ D* T+ Y: A- @3 u1 ?% dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 `. U: ?% c* s' C2.启用OWASP规则:
2 B6 S; ~8 ]! Y5 p2 Q) j; m+ g+ A, G' w# w( m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 E# \$ l* ?7 Y9 j$ q2 p1 U+ o! R" S: V& ]
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" s7 k# X. ^. K, T
( D* I* \* B9 l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 w* R; D1 U- C, }8 g% ^. B7 k; H8 k' S& |
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 \$ w7 c6 z, QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 f2 D* }  f# q7 e$ i/ PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 I; V/ ^( z5 q; i. d" C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 _2 `& c( P9 \* G( S6 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 i8 z% G1 S$ N4 A, C1 K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ g# Z+ ^! m; z* v# x& P# TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 x7 O5 G' S8 r4 U2 w# F) [) Q
五.配置nginx
) f7 t+ w) I4 V
2 b* I9 W/ C/ o/ n在需要启用modsecurity的主机的location下面加入下面两行即可:
# O3 b8 d+ ?& C5 }
  G/ T$ H! i$ lModSecurityEnabled on;  3 U& }: f1 I# w" R8 _4 m
ModSecurityConfig modsecurity.conf;( Y) u" {7 f! y- X3 Y4 K9 H7 x4 b5 G0 }
下面是两个示例配置,php虚拟主机:
( b: ]4 Q" I$ c
8 W0 D+ j% p: t7 v9 r% P* Sserver {
5 J0 \& I; Q; B1 m3 F      listen      80;  s  j6 l8 A3 p* M0 y" r
      server_name 52os.net www.52os.net;1 [5 F; z6 B; p" x- G) D  _
     
/ ~7 m2 \8 b' q% ?, t# L      location ~ \.php$ {
( Z6 Y; H0 N. L" N      ModSecurityEnabled on;  
* q0 r  Q! x0 k; G2 E% R      ModSecurityConfig modsecurity.conf;
4 R& X& H( b. c
; q, \' t2 `- N* {; b& R" b      root /web/wordpress;2 e  B, p, m5 ~* T
      index index.php index.html index.htm;/ g, M  [( K5 E% w* U0 C
  % `+ E+ {- q: i
      fastcgi_pass   127.0.0.1:9000;6 ~. l/ D+ W$ ]( C0 \! ?
      fastcgi_index  index.php;4 f8 O# @, c) P1 W# E* i
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. F1 A3 n* }0 c' s4 d. d      include        fastcgi_params;1 h% Q; j, a" z' p; `5 a3 {
      }
: D  [9 c* f; t. m1 J5 T  }% p; r4 }4 A# d" O6 @
upstream负载均衡:
; X1 g! j+ i9 F; r) y$ r- X3 Y& ?' s( L6 w3 C5 O; l& Y) y
upstream 52os.net {
; {6 o* _' E- ~6 z  Y5 [    server 192.168.1.100:8080;) T5 [* z. L+ J( ?. d
    server 192.168.1.101:8080 backup;) w' q/ }) S1 I1 g
}; g1 h: W  [' ~4 T5 g
: q5 U, J: h) }- g, o2 Y; p: [
server {) C- E6 t; b4 u' @7 V3 Y, w
listen 80;3 {, V) h& V2 c' l
server_name 52os.net www.52os.net;
& Q" b1 K" C* \
: |  r6 f  ]" }/ }* Dlocation / {
* V4 O! K' f1 P    ModSecurityEnabled on;  ) \  i6 |1 z, H4 w. ~2 H6 Y; Z; P
    ModSecurityConfig modsecurity.conf;  
2 A1 g1 V$ K) p3 |9 D( K( n: s9 P( Y0 a7 C6 b6 k8 P' l
        proxy_pass http://online;
' M7 Q+ c# Z- {. k3 b7 h        proxy_redirect         off;
9 K: V( w5 a; h' Y3 F9 B* Q  B* |        proxy_set_header Host $host;  H4 X( ~$ D) M$ d+ @
        proxy_set_header X-Real-IP $remote_addr;
& B- M) z4 g- I5 [6 w8 l        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 }5 I/ ?. K/ M% c! y    }
. z( I+ F! e% W5 C: s0 Q/ I7 D}* D- H% J0 {- h( D
六.测试  x& d# {" T$ J  ~7 K
# [8 _! I) Y. ~3 [1 X4 v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ L2 w9 {0 j0 b
+ F# d- Q+ N: p  Y" y: t6 t  G3 G0 V<?php" v5 e2 I8 `. O( h4 c& a/ O% t" S9 e
    phpinfo();   
" j% i% M. S( o* \' x! T( {?>
4 M1 @7 C5 W4 V在浏览器中访问:; Y7 N0 }. b$ U5 w: v$ r' ^

5 U, ]: H& w  r# ?8 T( A. Chttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 j! ~- D( ~9 n- G" F, Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' |7 |( k7 t. Q0 Q; T3 D$ Q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 d$ w9 o2 _5 e4 j4 F  U% W- W! T
说明sql注入和xss已经被过滤了) F  m% ^# L( Q) m+ a
, T& e2 |: a( i7 o# c
七、安装过程中排错6 t( D( t  l- z. `8 K  w% R5 g3 i
! ]5 d6 A4 Z5 F- I
1.缺少APXS会报错+ G6 A/ P9 |7 |3 c0 U( y* f
; ~0 n% F1 \. g7 L+ O, \' P
configure: looking for Apache module support via DSO through APXS
: i* i9 ]( w, `2 F1 Q4 E% pconfigure: error: couldn't find APXS( V8 P. e& i8 d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 G+ s+ ?9 }4 \$ W; @/ z
解决方法:
& `+ H! h% D; [( w
) a2 G+ ?' _5 N4 {6 ^! Ryum install httpd-devel
) n  f  n: E- t0 j8 Z! A% r8 p, b2.没有pcre' u; w+ F0 r3 p( l
% O# J9 x- O8 R! ~1 O
configure: *** pcre library not found.* x; I  X! y2 e
configure: error: pcre library is required
7 f% R: }3 _6 M6 W+ }解决方法:; E9 F& l& V% a( l0 W( d: q+ w
3 U4 u! l7 Y, v6 X
yum install pcre pcre-devel
- R+ \2 J. W' h3 ]& J( {3.没有libxml2
% b6 y/ r# h, F) Z
# b6 o! |! d% Q& S- m$ c* p7 D1 P, \  o" R
configure: *** xml library not found.5 @2 g: {% b) a9 _1 z
configure: error: libxml2 is required( x7 }" R0 Q  _
解决方法:* d( x" Z& Q# N$ s( f

" n8 W. ]# |# ?" i) |$ [& Gyum install  libxml2 libxml2-devel
" ~0 r( E5 z$ q9 R9 G) a4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 a  O. b6 ?2 `, @2 S
3 a( F# G# {% Z$ z' S+ |, NTengine version: Tengine/2.1.0 (nginx/1.6.2)
1 W, g- `6 E+ _- Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 C* [$ H* Y2 ?* n) T
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( W4 i* t! ^+ i5 k# z% `  u- N
0 [0 W& [/ @+ Z3 \; k7 u& f! R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 U3 h5 o1 Z: j. s* k' d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
+ `, U& r: g$ B. @  q9 x+ b/ Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' {+ \7 _, l6 d% C) }5 R) |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! ^3 m% t. |3 W, M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! A4 y, o0 V" c! k& n8 L+ z+ s: L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" x  M  n$ K$ F; b' Y- E  O1 S解决方法,移除低版本的APR (1.3.9)& Z. ~6 @  t" u3 e! ?2 r

4 V+ h5 Q5 {9 x  A" t& kyum remove apr
3 o  ?+ w4 h0 s  v5.Error.log中有: Audit log: Failed to lock global mutex
* b7 ?$ q6 R$ L" [' r$ w4 L' b+ G+ Z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * y" o  |( F: e  @7 E- C' D
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" J+ v; V0 [) }2 K+ S' k0 A解决方法:0 X! L0 \9 x! F" q' }. n$ H
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 ^' [/ q4 E% h0 ]/ z: a

: c3 \4 C! r: d) \SecAuditLogDirMode 0777+ S8 ^5 {" b+ I$ q8 u1 d: M
SecAuditLogFileMode 0550
9 S; F  S( I7 ?3 w5 o+ ]SecAuditLogStorageDir /var/log/modsecurity& t; I) [# d  x1 e8 ]6 R
SecAuditLogType Concurrent
! z6 R5 M3 B. J参考文章:
! T4 T7 H8 r9 e9 a  w0 Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 Y: X7 O9 _% W. V# w8 Xhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-18 00:17 , Processed in 0.081993 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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