找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10766|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 A! Y6 V  _" \6 V; M
1 Q, h! H! L9 k7 l: g9 I一.准备工作5 f% D4 B+ m4 r, A
3 P# m$ x* g  t0 J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 `  p& |6 u+ r2 K
9 B% o5 }7 t" A; U- o% ^% Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" I6 _" Q/ t# z
# R0 u# y( v" ^7 D( w3 Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: [) r, N: D8 p* j4 q5 T8 Q

/ p' m! C8 d  g! J# }  g: j& yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. W/ ?- V9 K; ~  B+ z
! Z4 ?2 q$ m4 C3 g' t
依赖关系:  F& e. U" h5 O6 e1 Q# \2 {0 F; B* p/ S
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 o; W* e: r6 D# m
- A0 G& w$ B9 n* c/ k) N5 Jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" ~6 L: d5 n6 I3 ~8 i! j3 h* G
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' X) b: W4 l* l5 I. W* ~: s
! s+ m4 l/ z. B! N! r. L5 \yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 w+ L8 E/ q0 e2 p% v7 q0 P
二.启用standalone模块并编译
" q! K) r( p; I/ J5 G. R1 R. b5 M* M7 g$ c- |4 ^5 K- E- D
下载modsecurity for nginx 解压,进入解压后目录执行:9 q$ q, Q- X6 |

: q8 n) T  t/ r, _./autogen.sh
) j. d" H7 m% n3 t# |./configure --enable-standalone-module --disable-mlogc; w5 t# L; w" U! D( O( d  ~
make * U1 w" n# o" q: Y9 V
三.nginx添加modsecurity模块
: Q# H) `2 u: @& G$ b+ m" S
: x. f( b( {% O' Z2 t( ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# y* u/ a% [1 a2 M+ y4 a2 B
( J) t' k- p% U; p  ~: \
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 C1 s7 U1 H. k
make && make install
9 @+ ^* z+ Z9 ~4 j+ a" s/ A; S四.添加规则' ^! E# T0 R1 G
3 O( H5 {9 u/ n
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 Q- K% l6 M+ @5 m6 ~0 ^4 o  H0 D; y/ I3 q* ^
1.下载OWASP规则:
6 n% g  n  m6 U+ [. s) w; m- r& \) b( m  w) x6 m# r& w3 {
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs. _. z" e) h/ }+ _
0 o; S% J/ D+ x# d" z1 a
mv owasp-modsecurity-crs /opt/tengine/conf/8 _8 U1 Q( l. U; U

2 L, j7 a4 @1 d$ z  B0 Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 V6 Q' W3 c. z1 d$ e3 f) l4 @$ ?; }2.启用OWASP规则:
6 l$ ]; n$ s1 `# m+ n9 Q: V7 g; G, ]* ?
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ K8 W+ H% o4 M6 K: @1 [8 ~+ g8 h1 q4 p3 d0 ]6 c: A4 p7 V) s
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 C& n  ]2 c* O2 c5 p8 D
" g1 Z" ~9 f; _9 J% V5 @6 j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 J/ T% R$ v3 C4 @0 Y6 ~
, A: A* U. l! Q: q: B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 R! {: P/ G6 w. U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* d* _! \/ @2 ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; B% @. v+ l) C" `1 s$ g+ KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; u/ P4 p2 P  l" ?( yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ A$ I* J! i" }1 T1 e! x" [  _7 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 a) s3 F9 s0 Y- x0 Y/ Q0 WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ L3 u# l0 }4 P1 b$ Z/ ]
五.配置nginx
. e: D3 ?% S2 H; W& I/ X) v0 h& k* M6 U! w) N& ?& t
在需要启用modsecurity的主机的location下面加入下面两行即可:
; I5 |) D% i; e
* Y6 l' g7 U( V; S# j: c! M* y! IModSecurityEnabled on;  6 _, ~$ t% `; R/ v3 C
ModSecurityConfig modsecurity.conf;8 @) D0 M$ r7 p- ~. ]# c
下面是两个示例配置,php虚拟主机:
4 p* m1 w+ C( s  z3 K! W# @0 V
- L2 z% L- r( e2 L3 q1 mserver {
+ _  m2 ]% D3 O6 T3 f$ `& g      listen      80;8 H! P$ y" p+ [: W
      server_name 52os.net www.52os.net;
" \) t4 S$ }4 g. x4 n% b     
9 J3 z. B- Z- R0 ~      location ~ \.php$ {
5 f, [# i" y* A* g9 h: [% }- m+ i5 ?      ModSecurityEnabled on;  - c6 s6 [0 L: b
      ModSecurityConfig modsecurity.conf;# B7 d& A* a* K
# z' ?, I0 C- l/ O; F2 g# j4 A: y
      root /web/wordpress;: i; g# ]# i# ]! h7 r: N; k
      index index.php index.html index.htm;
9 ?8 n/ Z# m# d5 d  
( w2 q) @* p( G/ v      fastcgi_pass   127.0.0.1:9000;$ N& Z8 h6 N3 q- I
      fastcgi_index  index.php;
+ @& `$ W6 Z7 f. t. m! }: N8 K7 a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 f% M+ e) C. v6 n) K! `/ v      include        fastcgi_params;* n' h: ]9 m3 R, n& b! E9 y
      }
# R' l; C9 R8 q  }
: _- P0 c! y5 yupstream负载均衡:  x/ u$ H6 n6 ^& K( B1 f

/ e, H( X7 v+ i/ ?! r6 Dupstream 52os.net {
! y( J6 d- E& j$ f    server 192.168.1.100:8080;# d8 d8 Y$ R& I; G3 P" S
    server 192.168.1.101:8080 backup;* ?: e4 ]! l% s( b  a* J6 C9 A
}
; r2 b6 N0 z0 c  d2 X+ S! z
' d) B/ H9 A9 d5 l& [server {9 k' u8 C; w' t; n
listen 80;7 O6 [2 _: {6 E$ p2 ^7 U
server_name 52os.net www.52os.net;
0 g* C5 J0 s  H! b+ @: I, {/ n# i0 C6 [7 Q- {
location / {7 o, }6 o1 z2 Q# T5 U; P) A
    ModSecurityEnabled on;  
# U( I0 v, O! a' i* }8 ]* F$ f    ModSecurityConfig modsecurity.conf;  4 p0 q: r4 h1 ^* y# n2 j* J4 J& Q  R4 Q

: H& N( o/ l- j# K1 u( g, _        proxy_pass http://online;6 O& T2 e( c, C3 u! F* a
        proxy_redirect         off;
5 l8 R# o, B- A) F# H) y2 Q        proxy_set_header Host $host;9 R2 U, D* {/ l9 @1 v1 a& ?
        proxy_set_header X-Real-IP $remote_addr;/ w  V9 b8 ^6 b% V3 ~+ N
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; H' [6 J& v' x. _
    }. R; |" Q  s" _2 `
}$ Q& b) ~1 A4 f5 F5 W
六.测试: J8 Q; M+ ^  d# j& |

' U9 |, N  _/ K$ N9 ~0 A我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 o* \3 R9 Y$ T/ d. J: G( R! \
5 a2 }! w9 {: g3 j; d/ D! d<?php
  c8 Q: k) l% M" X3 }    phpinfo();   
& v% D* [4 c$ `2 k: l* J# Y0 ?) c?>7 I* T6 Z5 Q4 E( G
在浏览器中访问:, L- @$ }6 r! r  U5 Y7 O

3 ]% f1 d; |0 f* ?http://www.52os.net/phpinfo.php?id=1 正常显示。$ T3 D( p: o" h" ]) c( t" t
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
  ~, P( W8 `' _; dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; S$ S! r/ l0 Z
说明sql注入和xss已经被过滤了
8 B+ a6 I! i4 f; d" B, `7 T, R4 k$ w  O1 F& ~& N
七、安装过程中排错. R2 c  Y" s8 N$ T7 C# B1 `. `  H

2 [9 g' z; ?' `. |/ U: T1.缺少APXS会报错6 {% @3 s  U- w

# F4 l0 ]7 N& lconfigure: looking for Apache module support via DSO through APXS
7 N* [; q6 N. h) W/ lconfigure: error: couldn't find APXS
5 X$ ~# F5 C; h5 |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 E' x1 [8 u$ \9 e' c$ [5 `/ ~. L' Q
解决方法:. I& k, ~. k# T4 `2 X! F/ M; t
* D- a+ {" |6 e% L" a) J" w) b/ u/ d, E
yum install httpd-devel
$ b3 A; x5 x% g* G2.没有pcre
- ?7 ~  ^4 A/ U  v
! `9 e7 i  ]! Lconfigure: *** pcre library not found.' y" i9 u! i% c
configure: error: pcre library is required
0 s1 q8 u2 F3 K) k$ A' g解决方法:2 J; r+ i. ~& N! q4 z

9 ]- P. G/ C4 E- k. vyum install pcre pcre-devel$ p9 D8 @( R5 @- }: K2 L( J9 x
3.没有libxml2, \$ R( `0 p7 ^. p( B
& P+ t' a; u+ {( L& ?/ {

. g. Q% v$ `4 y8 uconfigure: *** xml library not found.' h$ d% E! n' |. S9 _2 v
configure: error: libxml2 is required
+ _! K; R0 W; e% a9 k; z, ]解决方法:' w9 H4 G# v# f- M, J" ~9 c- A
% B& ?6 r2 H# s% P
yum install  libxml2 libxml2-devel
- Y# L% Z* q- T" P! N0 ^4 Z# W4.执行 /opt/tengine/sbin/nginx -m 时有警告5 ]6 f/ A4 J: _# g2 q( L" Q/ l1 y7 F

0 ?2 L9 X/ z+ K. {8 o* x" yTengine version: Tengine/2.1.0 (nginx/1.6.2)
9 o) X$ @  O$ Q( C) O/ f* w) f5 ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!% U- T! c6 s6 Y( k5 M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 j8 k) G4 G/ f
6 [9 V, C! B1 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( ]2 D2 v$ v* h: W/ V- Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! @: J# }0 ]- Y3 P) v6 n0 \2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 ]) r1 P# X0 `& k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 j! ?% D$ D7 S9 H6 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' l- U6 Z; V4 B  B
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; b; g/ C7 h, H" y/ V解决方法,移除低版本的APR (1.3.9)
: o2 X0 {2 x, V* e# S
5 m  Q( _' ]) m% v7 Z$ M0 j0 ayum remove apr
; t4 O4 u" y& c* m9 R9 x5.Error.log中有: Audit log: Failed to lock global mutex( D, @( A* ^+ @' I9 [; I6 W8 j

2 A1 |4 F9 A' s/ ]% T  Z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* W; S, m2 E9 l) }global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* x9 Q) E3 s( c. V8 X解决方法:
/ G% U+ C7 w+ @) s) {3 T4 \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, o7 Z% Q( u1 a( S
! m0 Q& G6 [& o
SecAuditLogDirMode 0777
6 K1 g$ {* d+ E/ _3 M( LSecAuditLogFileMode 05505 }" m. a$ z3 F' v2 E! {- c
SecAuditLogStorageDir /var/log/modsecurity/ ^+ S  a3 V" P  t) U! L! p1 Q$ X; e8 u
SecAuditLogType Concurrent+ ]* v& J' m. u$ i- K5 `
参考文章:! {) l1 w6 R  ~
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, Q+ K/ y) ]0 \; V- o( I0 l0 t
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-25 18:17 , Processed in 0.064726 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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