找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12061|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) K, l1 h6 \5 |% `( }
4 t2 o8 k$ @$ W; ^! r6 x一.准备工作7 X2 B- ~- a# D4 p0 U& s& {, n; O

4 [* S; i$ D( S, R, r系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( J2 y1 X5 X- J% K/ j; y7 C* I! b) d: I4 ~. b
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* F$ o0 c% x2 g

% {* R: b' S0 m6 K: @# w5 H  g  Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 |8 d: L% P- `$ @5 z8 o/ b( V. H9 E1 q* i! o4 G! ?: ?& u  b3 @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, t6 k: C1 I% b$ h
: s* m+ o" F1 k5 _% _, c) J9 G
依赖关系:
* @5 H9 I' ^+ C  Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  m& ?/ `$ N: H' W9 A" Y
- I8 X; T. i3 u6 M  Dyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 M- U( p) y, @8 Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr9 o7 F- T; b9 q) h4 `
; r. M7 g& a5 H  S/ l* q. n) e' F
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
+ Z6 W: i' M* i7 \& b3 P' d) C二.启用standalone模块并编译, c( o( Y, `( ^/ T$ X% L: [
3 Q0 f- `# s. t! Q3 Y
下载modsecurity for nginx 解压,进入解压后目录执行:
2 p3 S' p" `" i. u3 X* k3 @3 x# D6 J/ k# A7 P& L1 `! ^
./autogen.sh2 [5 o9 H7 W& u( L2 P7 x8 ?$ d8 ?
./configure --enable-standalone-module --disable-mlogc
' O* e, D" z+ P9 f% @- ]1 R. B* smake
7 Z( g" r0 M5 X+ _# J1 d2 [三.nginx添加modsecurity模块
# N' |# S) l/ x4 i( V: H) S4 V: A1 `6 f- m6 L. G/ f5 X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) }* v7 D" Z  ~: H% ]' C& W/ N, ~
5 }# r5 F5 c" h' V& a4 m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 _( h  V6 c' `4 d1 B3 R8 [
make && make install
% T2 W& u- J+ @四.添加规则
/ z4 P6 J! H- z2 W0 k4 S' w+ ~, w. d$ `1 a! F: p' a' D, @. N1 x  w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( B- }9 t4 }% ]' t+ m! p! f1 B
9 d) i) I! D3 `2 b* s- m4 R5 H9 u
1.下载OWASP规则:4 J/ s# [7 h, g: p. s* A0 i

! c& u  U+ s3 U# C3 |) {3 Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 u9 z4 b" s5 M6 g) Z' K4 s
; M% X* I5 _* y3 S1 V  R$ d
mv owasp-modsecurity-crs /opt/tengine/conf/1 R/ H' _. j' S/ I

5 o" q' k7 S. G- [1 Bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 ~" H  t- W! o9 S. u; O' ?9 L2.启用OWASP规则:' K. G: E5 m' [- \( z* j7 v) Q
# h8 h7 f1 z; y% k7 l4 u% e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* B2 R0 P3 f- c" |4 t3 C1 `1 ]* ?7 {9 J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
# {/ m: N8 b# b2 W  {4 @0 T6 \, D$ t+ ^$ b! P' m) ^0 T
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 y( L# ]& t2 i5 G% x2 u

4 ~8 f( H9 |( S6 ^/ B" LInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ x% p0 A, u! z# o1 @/ I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; o) r+ b6 g8 C) M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: L8 G9 @# ]8 q0 a/ P" M0 BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& h4 w7 k9 o5 r0 |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% d8 w& _5 |8 e, N$ LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 `  n# J8 ]+ _; T3 C8 X
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 W, Y( |6 [+ t1 E9 |0 e8 w
五.配置nginx) A. w  k$ Q0 K- h
, \9 r" L) X8 [* n2 D" Z
在需要启用modsecurity的主机的location下面加入下面两行即可:, `" Z3 o" P  K

# J3 \( i8 G2 s  B: H% z" bModSecurityEnabled on;  ' c3 B# W9 u% W
ModSecurityConfig modsecurity.conf;
$ k; q. b# B* k1 O* c8 w# {' l. N下面是两个示例配置,php虚拟主机:: A2 E! V; b4 j2 [  p

) Q+ y1 Z1 Y3 L: D* T+ v( i) c% Pserver {
. o; ]/ r3 B1 U( c* U/ W# j4 o6 [1 h      listen      80;
. T7 l4 X% r3 J5 h      server_name 52os.net www.52os.net;
& b) ?4 C4 A1 {' d3 s+ k# w     
( M4 |9 _: C1 z. C/ m; h, u% k0 ^      location ~ \.php$ {7 y: o1 z8 k( K) g+ J, w
      ModSecurityEnabled on;  
% T; w$ h# f: s2 U. ^  t1 Y; o      ModSecurityConfig modsecurity.conf;
: i3 d5 `# z5 \8 l2 y# g  H- m
      root /web/wordpress;
- n% i- \* `; H      index index.php index.html index.htm;2 q4 x: Q1 \3 ~
  
! _& t9 R6 i- X1 B/ q      fastcgi_pass   127.0.0.1:9000;
0 C3 r$ N: F- q( U      fastcgi_index  index.php;, T! q! V7 t! f5 ?
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 ^* e% M1 ~; J/ `; r      include        fastcgi_params;
7 K# r+ E+ N( A; v% e" M$ O# w! G      }
! p3 j1 x: |) e: B  }4 t% |& ~! ^# i) ?% ^
upstream负载均衡:  P* |* j, o" t' b
. P, n  B: W: x: u: s: Y- [, a- Z; q
upstream 52os.net {, h( E* u) O) V0 U: _- a. |
    server 192.168.1.100:8080;
( f5 y: f' c" @  h    server 192.168.1.101:8080 backup;4 A8 g- O/ v9 Q$ m! l
}
/ L, x4 }4 q' n; {* @3 d- ?7 f/ g9 ~
1 x$ r" B) U8 v0 Userver {
  F9 V$ @! K& E. V2 B4 qlisten 80;
" l  r( z9 s$ a, lserver_name 52os.net www.52os.net;7 `3 O; h+ t; V1 \( Z& f' P
; v4 q; J+ O5 T7 }. K
location / {3 g  s7 G8 t1 W& X4 V
    ModSecurityEnabled on;  / U9 u7 L+ q$ w! F9 J" a
    ModSecurityConfig modsecurity.conf;  
/ f( m% j; o0 E" w- W% `9 Y, i8 c/ b1 k4 S) f! D
        proxy_pass http://online;1 r! Z6 o5 \4 c( E8 e5 S
        proxy_redirect         off;  U& B! \3 r" v8 w$ L& L9 J
        proxy_set_header Host $host;3 K6 c, d6 ~2 l& p
        proxy_set_header X-Real-IP $remote_addr;
) H% e7 u% |+ J) v% v        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;2 B) M7 W" _# l6 B* R; ~
    }
; ]0 j2 A% B1 a; F" b& v}
1 p% I/ `5 y# ], b- a六.测试; S  w9 a* a( i$ [- j
7 T' u4 X+ z) L& E
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% G/ p' U4 J* Y
1 r7 n0 p$ N5 {- X' d0 Z2 {  e4 _6 s
<?php
2 o9 n2 c; t8 e# O! _    phpinfo();   
2 z* h, c2 z! Q% K0 [- k8 S- E?>
2 Q2 i6 V* |. p( T0 A在浏览器中访问:% n! E! H( q$ L' @0 M" {

2 s6 B& |4 f$ S( rhttp://www.52os.net/phpinfo.php?id=1 正常显示。
) n8 ]( [3 @* W5 _1 Uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 N- a9 w9 y# V1 f9 D7 O$ l
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 P; P1 C1 D# s: s说明sql注入和xss已经被过滤了
8 J) Z: k, B5 W5 ]$ ]/ z; C* ]: G' w5 V" e
七、安装过程中排错1 h- H% ?- u# U" d7 |

2 o0 \- {( T! ^2 t9 U1.缺少APXS会报错$ Z( t( Y% @  }( J. q( d; h
  J: S2 W+ \0 }4 U' B5 m
configure: looking for Apache module support via DSO through APXS9 C" X3 `2 L8 {7 d- @
configure: error: couldn't find APXS2 {& d4 o" ^# Y! r8 W  R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 Q$ |! n) O' C0 |/ F: r: ^& i
解决方法:4 s$ Z% }  L1 z# n. E2 x
  A$ [6 O4 v; m! x/ u
yum install httpd-devel
& X# b7 P3 n; S# N# S/ N/ c: b2.没有pcre
  p; `- t% e4 _4 z& q- \
8 }5 [, g6 j- j8 Y. q8 e6 Jconfigure: *** pcre library not found.
. A; Y$ g+ f- N7 T1 K, fconfigure: error: pcre library is required6 \3 m% e: `0 n2 N
解决方法:
/ k1 Z1 r+ s2 B! d# q
( t% |- I( w* J' }1 m3 z/ B) Syum install pcre pcre-devel
9 \5 x7 p+ Q  p; p3.没有libxml2
) @2 e8 e7 t! ]: S% I$ e0 F( N: s/ {
& N  F9 h( P4 g. I  n& N# J
configure: *** xml library not found.! P+ j7 H+ w6 h1 K
configure: error: libxml2 is required! L) W& w0 j$ @2 n( Q3 @
解决方法:4 ^, u- R6 t* L6 J- I5 y' {$ C- x

# Z/ u- e+ t# Vyum install  libxml2 libxml2-devel3 w4 `% J: y# W% M" Q, A
4.执行 /opt/tengine/sbin/nginx -m 时有警告/ K* s' A5 G( h% a1 V! [: A
) Q/ _6 [) _) R8 K; ?, V+ H( V
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! J: U; U# K% _' Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 j7 ?5 @/ @% O, d/ `3 ~: N; ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# L( g# t. _, D/ B. x1 `

' `: L, G: L( T! }9 q! V  J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 Z4 F) t2 Y) k2 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. {" B, O# `: n, d2 k6 U2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# h9 m  p8 L0 ~" N3 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# N) z$ E5 \  e/ O) s8 m% }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) x7 i+ c' t2 R* i$ G, Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  C9 n8 e# P% \# E, O/ k8 H
解决方法,移除低版本的APR (1.3.9)
8 |. G$ a6 i# P
( Y/ K: P9 w9 ~! G2 _0 Wyum remove apr
0 f+ j& \9 U1 f& G5.Error.log中有: Audit log: Failed to lock global mutex
8 }( f" R0 D1 X9 w# t; k
$ {% q" q) M) X2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     6 l' q. ^7 \* J5 N' O! i: {; T, ]
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 U4 Q  M& H' m# q0 H解决方法:
4 e  h" T; E7 B4 n& n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:  K  u& j) c" s6 j  j* {" o5 D7 r
( {, B3 b6 k  P( S% x* j
SecAuditLogDirMode 0777! c% M9 F/ `( T3 R2 a
SecAuditLogFileMode 0550
$ S4 q! i7 q9 M  qSecAuditLogStorageDir /var/log/modsecurity( L; d: {' g# [; R2 [
SecAuditLogType Concurrent9 e/ v' O* C! N, |6 O% |. Q! I
参考文章:
) Q' h* t. x! D  R. Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 C; L; ^# {; v% R: W$ Z0 i+ Phttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-7 09:14 , Processed in 0.068340 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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