找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11807|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! I' @% c$ a: p/ V* ?1 [4 S' L* P
一.准备工作, e/ X0 [8 }* {! _$ Y& E# |
0 G3 E1 X! p, ~7 L
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 ]) \/ V) H/ s+ r
% Q' a$ }9 v; W- ~3 ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 T6 a  e4 F) t  I( v2 H$ e
0 x5 {. ^  c/ Q, O& Y7 M' U' y! ~
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- V/ h; W6 }3 U/ Q0 ?  N7 A

  L3 ^& h. _. _- O, gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, K- B9 \# s  c/ x8 P' b; q( s/ Q9 E+ f# i4 ]6 Q+ I* X+ Q
依赖关系:
% T9 `0 G; L6 q7 K8 J" H" htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ E5 X) D3 m% a% X+ ~/ x( W
5 E; y1 i3 r' }( wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" m* N: |* I: z0 ^& l
modsecurty依赖的包:pcre httpd-devel libxml2 apr: P$ l) G& j0 k) `" q/ f
. Y. [) ~1 |% }& W) n7 T" o
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ @* Y/ a3 A* w1 X3 @$ d, e9 O5 {
二.启用standalone模块并编译" f+ f1 W- a8 F8 h

' x2 J3 @! }; P2 [% o5 h下载modsecurity for nginx 解压,进入解压后目录执行:
: e( J: u- ?. Q8 w  [- Y
  D( Y2 @3 B# t# p./autogen.sh" |$ z- R7 G- H1 x
./configure --enable-standalone-module --disable-mlogc% c+ {; z. O# d( V  V: i
make ) U5 L( I/ ]0 i* J- M5 O* a
三.nginx添加modsecurity模块% \" }$ w7 p7 G$ ~5 A) d

  O9 T8 e+ B! x在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:7 s# l3 p' z2 F+ O* Q0 W
0 a3 A$ c! `' `5 ^% C0 a8 N" x9 w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine$ H( E; x% j* R; u
make && make install
$ e; }7 t4 d% k' X# W& `四.添加规则
' A. `& p5 M6 ?9 r- n; i
( e' T0 `5 _2 |* C' lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。3 l# Y2 j7 B: |; o" U$ d
9 ?) Y( D( ]0 s  v' C
1.下载OWASP规则:
& e, U- @3 w& T: Z2 g1 \/ W
6 w6 |& E- X) }- Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 E! X$ H9 Z4 R8 U# i4 Q# p7 L6 j; l; Y6 N
mv owasp-modsecurity-crs /opt/tengine/conf/' Y, L8 ~" ~. L, ?. `
: \3 a. y: @/ c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) C* W) t. Q5 {$ W! d
2.启用OWASP规则:
9 V& v5 l# h- P. f: R# Q3 i5 x" m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ l' x0 j9 N# z( D* _
( p* }# M4 X3 b! C# m
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 K# l& d; c' X/ w& Y4 Q
3 u* {' z; ^" v; Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 m$ z* L, V/ g1 `
, T6 S) Q  {* h
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 G- x$ `5 l9 B5 Y4 r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; @& a) C# t4 M) J# u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  Z! s) X3 ^0 X3 n: |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 p' J  I$ ^$ X, \5 b/ L4 B9 I, r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" T6 s* t5 Z1 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# A; ]0 e, X: a  o; b" ^' a6 _
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 T% K- ~. J0 B3 [) Q% c% v五.配置nginx
0 I* t, I/ |, t$ z; V
. U* D0 m3 x* B0 N7 R. V* N. f2 q9 x在需要启用modsecurity的主机的location下面加入下面两行即可:5 ~* f. @: Q' Z3 G  o7 ?6 m
" w- z+ J  h3 C: R
ModSecurityEnabled on;  3 V% M% K$ ~9 ~* d5 J
ModSecurityConfig modsecurity.conf;
( i$ c* R+ U8 P7 `7 {  I* ^* v下面是两个示例配置,php虚拟主机:
3 a' V! [1 X% c0 K7 A* d; j" {2 w( v* N. M% q: e
server {
& p: Z% _0 S2 k      listen      80;
4 Y2 Y2 l! U7 a7 O      server_name 52os.net www.52os.net;
- n) o  |9 ]* a7 X/ ~     0 |2 \4 ?+ ?( ]/ l
      location ~ \.php$ {+ o8 o( Z6 I8 m! g0 k7 u
      ModSecurityEnabled on;  - L% ~+ a2 ]3 N" \
      ModSecurityConfig modsecurity.conf;
" n' G% ~" {( [9 ?! @' d  u7 z& a$ U2 z! [7 j9 i0 m0 c
      root /web/wordpress;1 F; n/ O8 G% s
      index index.php index.html index.htm;% ~& b) h1 L) Y; J# z
  
8 J7 }$ Y" L7 l; r" i# P      fastcgi_pass   127.0.0.1:9000;
: L  U. m+ `5 x      fastcgi_index  index.php;7 S; R1 Z& o6 K: I
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;0 @) N6 H, C* M- C; \3 k' i
      include        fastcgi_params;
% y! C7 Z& _. j; u, k( E: }  R% j      }. c0 \. `1 C% @9 X% b5 k5 Z: q8 m
  }+ k% ?  i4 ~  M2 G7 {
upstream负载均衡:
0 J" T4 e  [5 N2 |, |
9 r  H6 z8 G4 @8 ?4 supstream 52os.net {3 m1 F' k. c/ r$ x$ l" H0 M
    server 192.168.1.100:8080;
% V4 q5 [* E1 x    server 192.168.1.101:8080 backup;
" N6 j7 S& C9 y% s7 @" z. N' y}
6 R, D: R' _0 l5 t2 B9 D& C
5 G9 A2 J4 r; E& H& ?) Lserver {& \$ t; W$ ^4 H1 S. G' }: p
listen 80;
8 c0 Z5 \& U, }& m8 a- k  e* x0 Hserver_name 52os.net www.52os.net;( b) V2 m* J6 r9 B7 |

( w8 t, H8 B$ ?0 z" ^( z; m) M" ulocation / {+ N4 V! I! Q, [$ O8 u. |6 p
    ModSecurityEnabled on;  
2 B4 a  X! m" H/ ~2 M( g) k    ModSecurityConfig modsecurity.conf;  ! V: g0 w9 n) Y
. {8 O6 k! a/ Z: S0 _9 t9 C& C
        proxy_pass http://online;
: l* o/ C7 X$ Q: q" w7 [# D( M        proxy_redirect         off;' a1 p8 m% f0 e* K
        proxy_set_header Host $host;5 ^! {# Y6 F0 [/ |
        proxy_set_header X-Real-IP $remote_addr;% V7 e7 b' K  K! V+ a
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ j& J4 Q, n" Z+ F+ @    }
: T7 I7 ]/ P) |" n1 c4 {0 |}
3 C, j: o; G( N3 B/ ^六.测试8 Y, V' I) q6 B' T; Y0 d( a

  L7 ?  [! Z5 d+ _* |- T1 f) t; ]我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
  Y8 \5 Y0 O( v  O8 A8 v* T8 S' n; K& R+ a
<?php
! N" J, E6 Y; }9 P' Y    phpinfo();   
3 P, S0 L4 e9 _# |- f  c?>
2 z: v$ q! @9 L7 b" H8 {! \: a在浏览器中访问:% T7 e/ n0 Y" ^

: U6 L, k" w9 `3 a" F9 xhttp://www.52os.net/phpinfo.php?id=1 正常显示。* j. t  \! k" Y! A4 U
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ n# q3 I  L7 A, q1 xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 X2 e- s, ]1 w说明sql注入和xss已经被过滤了
1 t, \2 s$ p* p6 Z, j) c( y1 f. y$ m# R9 G1 F$ A1 o
七、安装过程中排错
8 `$ F6 T& Y8 L% d: n8 K7 j0 S2 k7 d' E% ~; h0 {
1.缺少APXS会报错
  }% w5 ^# _! [$ O
) M! v# ^& c6 H9 K" e0 Qconfigure: looking for Apache module support via DSO through APXS
1 D# U$ Z* ^2 D& A9 P. u8 G7 l3 Xconfigure: error: couldn't find APXS
9 E% u% u; u: h+ fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% I% d# F/ `* y3 j' x% t解决方法:- b1 x* Y$ }# L/ b+ f2 ~
; {3 ^: Z! ~8 W/ T6 H( n
yum install httpd-devel+ M& `  _3 A% G. `3 @( S/ c
2.没有pcre3 B' h0 q$ [- `6 N) M' q9 I

( t6 L  Y. B- T# Bconfigure: *** pcre library not found.
' ~: p. U+ |& B, _4 ~configure: error: pcre library is required
7 T* f; l( n( a& x& h' k( Z解决方法:
$ @& e1 V& [& b- U. o7 K; [' b1 v6 a5 E7 p( e
yum install pcre pcre-devel
) M, b# J) j: X4 ~) O3.没有libxml2- |# W9 M: O& R+ ?8 {- ^: M! [

. |$ W$ \- Q% u% n) m* b1 `6 s
0 H; e/ S( V9 C2 q- u  ]configure: *** xml library not found.9 J( F7 a- F4 `% w
configure: error: libxml2 is required
2 ]0 E$ E8 J& \7 r2 g5 D/ a解决方法:
% Y6 J2 ]! q! z8 J
1 S! J9 a  X* n" L6 ~, a% S& yyum install  libxml2 libxml2-devel* P4 ~' w9 L% P  J7 b
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' Z2 P8 q) G7 D& J2 Y! |. h. q+ q. Z7 s4 t. i
Tengine version: Tengine/2.1.0 (nginx/1.6.2), r; d* R: C: H
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( a; Y$ l9 y* \1 L; s# H/ X' d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ i  c- b, g0 x: l4 D
6 _$ @: [9 w8 p% ~# L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& b+ Y2 v( y. y" a* k. f6 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 H6 u" ~) a* u
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 c4 ?' |" t% w' a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ v4 T% I/ o& H; K4 K, O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") Z6 z! z" a& [8 l; J( Q! y4 O. @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' _7 G, H9 l3 d/ b7 d/ Y& U解决方法,移除低版本的APR (1.3.9)
* {$ g, ~' d" L1 ~$ }/ h2 R% N( ^. `4 H% [
yum remove apr7 `+ b$ ?8 m) P; V6 V4 m5 n
5.Error.log中有: Audit log: Failed to lock global mutex
9 x" p: M0 ^: h& k
, h* @  C- W6 \, F% y3 @* q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ; D) N2 n: O8 I. H* X9 c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 s. z$ F6 a5 ?; D2 Y解决方法:
5 O3 |$ k+ g% f" j4 ?/ L2 J编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 ~2 H  d) M, \1 L0 Y1 k$ `
4 |# Z1 Z& L5 |6 d7 P* y
SecAuditLogDirMode 0777
: u* l& t, G( P: Y% M0 `SecAuditLogFileMode 0550
# b' |. v, x' [/ D% d4 ?# I+ Y& L: ZSecAuditLogStorageDir /var/log/modsecurity/ r- ^. m( Q- {  R! @6 k. g
SecAuditLogType Concurrent3 y  |- T9 r1 Q, x
参考文章:( j9 r# M& w3 }/ G7 x3 X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 c" X9 t1 K$ Whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-15 19:25 , Processed in 0.072576 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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