找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12563|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 w+ U( e5 j6 ]' m, n
" ^& Y# V5 e$ R  g3 F' I一.准备工作$ R' s0 [5 o: F4 |9 m  j0 y
' F  T) m: s: K2 i' \0 @- _6 m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
  F  `5 Y$ d* u: i$ u
& D5 C; f7 y, d% V5 _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 M  j4 @7 J8 Z$ x1 Q
& V& Z: t! h0 ~/ o4 m) p) X/ p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; v* Z+ x% N* ~0 b6 e: M
# k  k; I6 C# f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" e5 z  B/ |7 S5 ^
) B; U4 f5 }( R9 H9 w0 O3 P
依赖关系:
$ N2 o) m9 o- k- ~8 N1 h6 M: Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ L$ K6 v' H( o2 }
  }& s% W) d/ r$ p* V3 d
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; ]$ [4 x3 F' J: L: wmodsecurty依赖的包:pcre httpd-devel libxml2 apr5 S3 e8 J7 f. n) Z" z

  L2 x! Q6 a9 pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 ]' n0 w. q0 j) ?# E3 R. M
二.启用standalone模块并编译3 y2 A" k% J0 ?. v7 O$ K- D
2 b# |6 l- C: `+ z; q
下载modsecurity for nginx 解压,进入解压后目录执行:8 t, g. B/ d5 }& f
+ s% E) N; C1 ^" N
./autogen.sh
1 ]% Y5 Z$ e) _4 g. P5 Q; [./configure --enable-standalone-module --disable-mlogc: P/ X" ?  q3 H6 T+ r5 B6 s
make
; D! \( P) }+ Z# ~* ^4 Y" L" u: G三.nginx添加modsecurity模块
5 z) i; d; c& q8 O! G8 }  n, L8 ?" E4 V
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 v* b0 z) z- S' g/ d6 [- Y5 b2 c% _. A* C3 z. l
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) y6 ~0 y1 }$ q/ r. \+ g/ m
make && make install) t  _; F9 ^( Z2 r. d
四.添加规则+ a# b7 F6 k# s& g, ^$ o
& g* P* U9 i9 {0 Q2 g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 l! ?- N7 |$ ]& B
  L7 E% O6 l. I" u% g% }
1.下载OWASP规则:
/ J3 X  D) b. s6 {9 Y1 S4 A9 j- U0 O" e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs- D* W: ^& b2 J: n

( u! ~- Y4 I7 U+ }( |mv owasp-modsecurity-crs /opt/tengine/conf/
: Q1 ^/ _8 S6 T0 C) ~
2 F# H8 T. v" j. u0 p4 Ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 s% y2 P: h8 J3 Q$ |/ B. A  _& j
2.启用OWASP规则:" l3 x( r- l$ Z" M5 N" D  x$ l

$ p  J; G& _: s2 Q" R$ K' w复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# i. W- I( E2 K# P0 W9 X7 O9 {
" y, v3 t( S2 e编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- c. b) i7 l6 k+ Y1 e5 X; |) q. `( d0 S! |  H# J' C
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; H$ J( l0 U& f9 N* @9 M4 h; r3 V
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 |) [/ W* [+ x/ J2 U9 Q9 eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* I8 i+ w( Y  [7 O& u- R) d; xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" @2 b# V0 o3 z3 @- z7 h* x3 s2 [6 ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; f1 Q" P5 W& {% G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 u, K- L0 ~1 j, ]# y: M' C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 [. w3 Q3 [  L9 K/ Z/ l# m' d" p% p
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% H4 s2 r$ {, A) c: K& @五.配置nginx; c! ]8 |/ B. \
/ b& E  A, N  j; y- t' z, [
在需要启用modsecurity的主机的location下面加入下面两行即可:3 m, R# E- M+ p# r8 p& o

# {! H- i' ?9 ?! JModSecurityEnabled on;  
, s( Z3 Z2 [1 m# B2 U$ AModSecurityConfig modsecurity.conf;  _: N. H5 J  M
下面是两个示例配置,php虚拟主机:1 d# t& y+ F- |  ^, ~
4 x6 ^) e+ c; Z8 _2 }0 I5 F
server {$ w9 r  ^/ K" V& ^+ P5 p
      listen      80;
8 i; c1 R2 Y+ c8 v6 Y! j      server_name 52os.net www.52os.net;
1 `, q( x. y  Z- v1 H7 A- H+ O     . c9 r* j0 _8 C2 T% m
      location ~ \.php$ {+ g+ C0 g/ i/ L1 p& ~: z
      ModSecurityEnabled on;  
' W% S8 k' _! \: c$ Y4 U      ModSecurityConfig modsecurity.conf;
4 i6 M; C: G4 V2 B) ^3 B, X. ^9 G' [: i) E, P5 P
      root /web/wordpress;
7 I6 F# W% ~- }) ~# H* w      index index.php index.html index.htm;
- s5 t8 m; X2 K2 x* |" ?" e8 v  $ I6 E- c: e2 N
      fastcgi_pass   127.0.0.1:9000;5 Q6 I! Y7 F+ a& E: T3 u
      fastcgi_index  index.php;* R5 \$ x9 l  Y1 o4 b: N  D  @
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 T& c( X5 P, Y8 H5 c$ C! }
      include        fastcgi_params;( g$ W' @8 E# K4 x) f
      }
" z+ f6 H* R5 Z! A- s) z- [  }
0 k" d. \1 ]* v+ oupstream负载均衡:! |  @2 W) s! @7 o8 F, v1 [
1 B" e3 p* w) }# T$ K4 F- A
upstream 52os.net {
, ]+ Q- N5 J8 }/ f9 ]    server 192.168.1.100:8080;2 k/ M% @. r5 l- G, }, P
    server 192.168.1.101:8080 backup;
7 H3 z/ B% D% C# E5 c2 U! `}7 K9 Y: P1 L8 B4 Y, L5 J' O

: L% q& x- h" Y# i( ~0 Z6 C0 Fserver {
: I+ m* X6 E7 e: rlisten 80;; r) M# S4 H' h1 M# O3 ~: X
server_name 52os.net www.52os.net;; g4 b7 n6 l" {2 H, M. F

6 O7 t  ?+ b0 X9 e; P8 R$ ^& Jlocation / {* D; ^+ |3 n, t9 F" z% Z
    ModSecurityEnabled on;  
/ W; O; Z2 u/ \0 K    ModSecurityConfig modsecurity.conf;  - {: {# b# ], f6 N; P0 A; x

7 P6 v) S1 ?1 ^        proxy_pass http://online;
1 K$ r; f; B+ R. U5 a        proxy_redirect         off;
6 e1 j) W: q: J) j; Q: D: L        proxy_set_header Host $host;" `+ n+ N: d: W& e7 g
        proxy_set_header X-Real-IP $remote_addr;
: k! g, d8 r' ?        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ O/ v$ z8 E* {8 K    }; s% Z2 @* Z5 @# I/ q
}  `: R+ B5 m7 [1 d. M4 o( ]
六.测试5 a$ D6 T, ?; h+ N" T; `
: {0 L. ]( r2 L2 m# @5 g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- X8 L4 G% \) @0 j: ]/ I
* `( J  p! {9 e9 E+ s; L
<?php
, F5 b$ U- f; r1 o- |& u" i    phpinfo();   
- e5 O$ L6 o! _! t?>
7 z& O# U. m* T2 T在浏览器中访问:
. x2 ]" N8 ^0 q( D* q6 i  t. W0 @& G; x- @, r
http://www.52os.net/phpinfo.php?id=1 正常显示。! Y4 l3 m$ S; r' S* S
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" p: c3 i" R3 y! {( }3 phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。. ~+ Y& C+ c- V
说明sql注入和xss已经被过滤了
9 B1 G- f1 w4 C/ f$ G) x2 a; w6 _' H) F4 N) p4 _: O2 J+ @5 h7 x; r7 J
七、安装过程中排错, W& ?9 D  }, s& c9 _- T& s4 Y
0 u- T7 e, @& q9 T* l: t* o
1.缺少APXS会报错
2 r) |3 w' [4 o1 E# b& _1 Z: T0 j& ^1 B9 Y
configure: looking for Apache module support via DSO through APXS  }4 e5 G: O' d+ J4 K. h6 `" D
configure: error: couldn't find APXS
# r) t1 B# `+ O: X, {) N# Y$ Q8 Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& _! o+ S6 R) X- F
解决方法:
4 ?( W( A' T6 W: ]5 k9 ?" p
9 O% Z9 _8 L4 \: ?! }: F/ A% A5 Nyum install httpd-devel4 L6 n: S& E8 z. K
2.没有pcre
% q) h* j- l0 f1 A) |* [
$ m& i  y2 S- `  Nconfigure: *** pcre library not found.
  F& v0 A# `. @0 O+ Tconfigure: error: pcre library is required
# G6 ^1 c6 C8 a解决方法:
  h( Y6 t2 l7 d0 i
8 {# T+ K- r# V5 s- t8 g; m3 \. \yum install pcre pcre-devel
: _, T' x7 |1 e: u3 [& a$ v3.没有libxml2
4 D) x) V0 Y  M- N
) c' u7 n; o( G- T4 k+ ?/ Q1 J( G' w# s$ m# C
configure: *** xml library not found.- a/ i( \: [0 h
configure: error: libxml2 is required% r; L: W+ L  S
解决方法:8 Q2 q, I0 Q! |7 X9 x6 ^
' a+ u: y$ b9 z7 \0 g0 {1 `: [
yum install  libxml2 libxml2-devel! N# F4 Y8 @0 Z
4.执行 /opt/tengine/sbin/nginx -m 时有警告2 @3 U1 k" l7 x! s0 ?: |# C
# H# N8 O, @7 X) y7 Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
7 @* ^# Q: o* w. Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 f5 o9 @3 f2 j7 r: D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% P/ P$ f# L9 }( b* g7 ^7 h5 k( ?
) l& f% |6 X: T3 U% P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ H" e! K) l) s7 ]+ H2 q4 X- k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". ]  z$ z4 ]/ }& M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' d, v3 V, ~% t0 A5 k6 f1 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# K/ a0 O- N7 K! T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ O, v5 j. X$ o$ \$ m
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. y% H; G, @% \! A" Z6 g; @解决方法,移除低版本的APR (1.3.9)
- j5 q. A; L$ @2 b, A3 M9 b" A- V
# O* `- |% f( R* p3 V/ byum remove apr' w, R' d6 S- U; W! `0 I  ]% j
5.Error.log中有: Audit log: Failed to lock global mutex
1 l+ ^  E, I5 D( _3 ^3 u8 F& A
' l2 y+ {. [) q% b" _( |2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
! X8 I7 k  n  G, x6 y* yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 N* y/ k* s- \+ F2 w解决方法:/ `! {7 _; [. [% @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 M0 ]4 M4 Y8 C. G0 w0 Y% K: j
7 L, t9 A+ ^! |) |2 gSecAuditLogDirMode 0777+ R- p$ z" X; c. l
SecAuditLogFileMode 0550
1 S2 F+ P, l& Q: oSecAuditLogStorageDir /var/log/modsecurity0 B2 E( U; b% o  R5 o
SecAuditLogType Concurrent
8 i8 `8 W! x& R: J参考文章:/ B- Q2 H1 t6 {2 Y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 z1 G* j8 |5 l: z4 \
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-17 16:02 , Processed in 0.067642 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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