找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9221|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% z1 ^$ d3 y6 r6 v7 n, Q' ]) S/ j4 l6 H
一.准备工作
  }, S$ m) F1 N$ }9 Q/ B, J# Y2 Q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: N- f+ ?' S8 o# `
" D7 H; W  v+ c5 x3 [( |+ vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) J8 m4 \) R. {
) T8 J3 J5 i" u  Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  e$ v& t8 Z& l# |7 }( o0 o& O( K
( t9 L* u& e) ^: D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ R. e5 k$ A# W; `) I; I4 C
- w% i8 ?6 D7 H# t! F
依赖关系:
% f% I+ j; |# |. Z  c) ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 _. p3 k8 D: [0 {4 S% f! ]+ L9 C

4 H2 d  m) b+ K; d; ]% R+ x6 {yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
  Q+ J. C( l9 q  _* c" j  }modsecurty依赖的包:pcre httpd-devel libxml2 apr
  P) W  e) C" W! w. b
( E0 ?: y, j9 }  X+ v+ yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 T/ F6 I: I  F, K& |
二.启用standalone模块并编译
8 R  i5 J+ y7 p! S% v0 ~2 f+ W5 }8 p5 {
下载modsecurity for nginx 解压,进入解压后目录执行:6 Q3 v; y5 |! y0 u' z% T" g7 |
5 x7 ]! H( p! x' ~- R# F# J; }
./autogen.sh5 X# Y$ ?4 e! n  `. O* {
./configure --enable-standalone-module --disable-mlogc2 T0 K. l4 a' d
make 3 g8 `5 H( l( o
三.nginx添加modsecurity模块
4 l1 k% R7 M* @! @$ P9 x. h! S( S8 W% e: Q  @' ]6 K: r: }$ Q9 |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, x9 F* d8 a1 S
% e1 H! ~  J+ X  U( H2 [; a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
0 T, {, E9 I* K4 ymake && make install
7 I0 X+ `1 ~  @/ f四.添加规则
& Y6 {. S+ r2 C
% Z% U6 n' `/ w# g# \# emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 L5 t2 O" j: O$ o
; [9 v* D5 X2 X# V
1.下载OWASP规则:7 n$ d: a' @8 B( m: f- d' ]# d  l

! s& x; @# U3 xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 i' {( o- V, q/ w. M( ~2 d3 y# P! v
mv owasp-modsecurity-crs /opt/tengine/conf/9 c4 ^; s9 u7 B; ?& M6 g& O# ]( T' S

0 A, d, Q" A1 b' W# b) Ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* T9 S6 {! O9 H$ p2.启用OWASP规则:
5 G7 ^' E' U8 x* ~1 n2 P# L1 H; M* Y, z( \& s; b, F0 n+ {
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* ^8 U% X# O& m2 G
& p3 c1 `) ~' I
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ L8 F" y; f3 [4 N( Q/ M
# [  S  w( Y2 m0 uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 E( s' r* `0 G9 D
  \: f3 j5 O* G' V+ d* e0 S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 ~( G8 V; p. F' }( N: M1 n" ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% C$ {/ m4 l, j- p/ @) k, j; ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ `4 \7 V- f4 h( J; l6 P$ {" C" D' VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! g5 j& s. t% `4 p9 a$ l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 o8 B% o; s4 V8 P# J0 r# [  L$ E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; k5 W/ u) B# s" z2 ]( E" q: [5 o
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ S  \" h8 t9 s8 [4 a# _0 p
五.配置nginx& O5 c* P  n' o4 J
6 U5 p9 `' P+ P5 c2 ?4 {
在需要启用modsecurity的主机的location下面加入下面两行即可:
2 m) U$ O* d- o' c
: R) l+ _6 i7 N3 ]" E: AModSecurityEnabled on;  
9 Z8 J* S! j, y- DModSecurityConfig modsecurity.conf;
$ f- V* n/ W/ s& w: c下面是两个示例配置,php虚拟主机:! l2 _8 r; a1 P3 \$ ~% c
, F  Y% u  `# o" r' |( o9 l7 J) K
server {0 y5 @: L9 k* [4 G
      listen      80;* P" a# u* T& c* n9 W
      server_name 52os.net www.52os.net;% E# a7 K# s4 W7 E5 D5 m5 X
     
- J3 i) B, I; ~+ W      location ~ \.php$ {
0 w, ]& ^% F$ B      ModSecurityEnabled on;  
8 c; x5 a* [, l" Y4 W      ModSecurityConfig modsecurity.conf;. P+ h' N' G3 r$ l9 q& x* ~

# u  A/ m9 Q, S) c  |  l, v      root /web/wordpress;
0 O3 R% u! Q& _1 u; s      index index.php index.html index.htm;/ Y3 c! t$ b8 @8 @0 u2 p
  5 C. N+ h5 [( C' L
      fastcgi_pass   127.0.0.1:9000;
: ]% Z) Z4 @4 S& A% Q& T3 S      fastcgi_index  index.php;7 n0 T/ x: i/ V9 j1 w# s- s/ ~5 |9 r
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 F1 U& Y: p/ s1 E4 o) [
      include        fastcgi_params;2 [( v3 m* x4 Q  X2 R% `/ C
      }4 N* H5 ^" k4 Y$ X, f3 ~3 r4 s
  }8 j4 T1 x8 J$ m* k4 c
upstream负载均衡:
8 z$ ?7 k$ `: u& M4 R' a" a& C& `8 T/ j8 {4 X& l
upstream 52os.net {
2 l" d: \, T* a* m    server 192.168.1.100:8080;
7 a8 o2 b2 M& E0 l# e$ Q/ y    server 192.168.1.101:8080 backup;7 ~1 K8 O5 K+ ^5 ?3 \8 d
}! {# o! ]) S+ q! |( _
: u7 `* d* [" ], W
server {1 Y6 V9 w. n: \4 A2 `
listen 80;
: y, \8 q( v# _) V8 `) ]6 yserver_name 52os.net www.52os.net;
4 ~$ E2 c/ w. Q5 @8 K
+ t1 v0 Q9 T6 v" i. y: x  Dlocation / {
. _: E) `! @$ K7 C. ~( k9 I    ModSecurityEnabled on;  & I3 h; R" c; j3 ^7 A7 T
    ModSecurityConfig modsecurity.conf;  : F* C, y, F  j, \# _' B. V

8 j( H2 p( n- Y/ S5 H' @& W        proxy_pass http://online;
. |1 \/ O& o1 q- t5 g6 C3 u        proxy_redirect         off;; e9 F: L8 w# b7 [4 ]) f. Z
        proxy_set_header Host $host;$ q6 ~1 j; l  K# D+ m
        proxy_set_header X-Real-IP $remote_addr;
, }/ U' x- f/ u1 z1 p( [+ n$ `6 m        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;" @# p( W* w% \0 K8 I: I
    }
1 Z$ C+ n* I" _# S0 ^8 A}
0 @5 s/ F  d2 K5 ?) U( Y5 A* n1 G" t六.测试
; ]0 y% w- Y9 c1 o8 z* }4 O+ g* ?/ G" r1 N
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% K2 k1 m$ O8 G4 E1 i6 u* Q

* f/ a  E5 O6 B& x' K* ^# I) ?! v2 n<?php
6 |. J4 b5 {& X3 n    phpinfo();   
( t- Y8 Y7 I3 u) d?>
8 w4 b, w3 \9 i/ |+ {; t在浏览器中访问:
) m0 A$ ?" C7 l( {% b
5 K5 c* h: e5 M/ F1 u5 Z# n) Q$ mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
8 R6 X9 [5 r/ R+ Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。# A! U- o+ r. T" ]# A; w1 Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 @5 Q" _: X1 z- x说明sql注入和xss已经被过滤了
( n/ j; S* B  G4 W) E, u7 g3 e: ?6 @  F; _
七、安装过程中排错
# H, ~8 P7 x4 K! Y
) ]& j4 |6 B5 c5 M1.缺少APXS会报错+ o' e9 o9 M" z
& }& a8 I' K" `1 L' n7 i, Y
configure: looking for Apache module support via DSO through APXS+ _: t2 N7 F2 |# p" ~$ P  ~
configure: error: couldn't find APXS
2 S( _9 p7 s( c. i6 @8 f% uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- c, U" o4 L+ r% U* z) ~0 U解决方法:
) r% X3 ~1 x4 o( q0 q
7 B+ v0 _% ?8 x1 d% yyum install httpd-devel' E8 Q0 v% Q, v5 _
2.没有pcre
4 J3 o& p7 J) I# t
: p: Y  ~, J4 g5 ], Kconfigure: *** pcre library not found.
, v; ^6 Y& K5 t9 iconfigure: error: pcre library is required
/ G, e- `- G$ q9 ]# b/ q解决方法:
+ z$ w+ \2 [, x; F5 q1 T' D0 A6 A7 P% S+ s0 Q
yum install pcre pcre-devel' A- M# G4 u' E6 k/ y0 y
3.没有libxml2+ @4 O3 L" w8 W# W. o1 G. S
- j9 d8 t7 S. H

& k  Y& ^6 Y1 Aconfigure: *** xml library not found.% ^, G1 Q6 B) E# Z5 X  c
configure: error: libxml2 is required8 o% ~5 r& U7 D" K) d; A
解决方法:# s" m) Q5 {( L
# S0 W& I0 j# e6 G9 g! f0 `
yum install  libxml2 libxml2-devel2 @; E$ E' E+ \. _# r5 i9 @& u
4.执行 /opt/tengine/sbin/nginx -m 时有警告' u8 C# m- [* a6 z3 Z3 K
* @! p& w2 a: x1 `9 d
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( y% E; `& U4 @# }1 z  q8 g% l
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 v' o# L" m$ |* K( E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 w$ a- U/ i9 \' i8 B
& \. G: t! Q4 \0 ^( {2 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& B# t" C1 _" }7 L0 Y/ {! ]9 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; W7 G7 O- p! D# z% ]. K, P
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* H" C  k7 U. o, v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- R% c9 f2 o/ \6 Y2 V9 o0 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& M7 [, U4 }6 _6 q. {+ G. P/ a7 W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ r, v6 b& Q- c1 ?' T
解决方法,移除低版本的APR (1.3.9)
! G& `9 v+ c1 i1 o/ d$ p1 g
" S9 {# @4 E0 D' gyum remove apr* E- ]& c9 n& J' t' K
5.Error.log中有: Audit log: Failed to lock global mutex
5 u& ]! v7 y0 g* `9 i( W/ w4 }  r: p; S, g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ M9 e3 p: b# B& d$ n) p- w# F& {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 N5 v8 j+ u; ^, O% Q5 r3 R) p解决方法:
$ H( Z! U0 I# c, A2 F5 l! {  ~! y9 s& T编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ x, q$ D/ e% M  W) `3 o8 M; z, I: R3 u4 C' Z* @1 D
SecAuditLogDirMode 0777) v2 u2 [2 Q+ U8 q$ y
SecAuditLogFileMode 0550- z+ z( o+ N) W# v, R0 b) H
SecAuditLogStorageDir /var/log/modsecurity
  x% k8 `% \' R) xSecAuditLogType Concurrent7 A  D# }3 x, N5 \
参考文章:
2 }+ M! ~4 _6 z/ ^  @! t0 Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 E0 {) d8 A, o4 d8 }+ N! \! ?7 Qhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-28 00:34 , Processed in 0.051828 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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