找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12038|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* X; t$ {  R2 b  ?. Z+ Q& i

4 \! [: a& {) I7 ~一.准备工作
0 r* a: w' l2 P8 D1 A
8 D" d1 J4 N2 l# n8 v6 M. B# p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- k% Z! V, U/ Y
/ w9 w9 k, N4 {" N6 r4 |& otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( t* s4 _" @/ }7 u
( Z2 j- T$ t; C; H6 b; l' Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" ]  k/ y  N$ [, ?' `/ V( M, X3 a
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- x1 [: B  ^+ s9 g" I

6 h# q' W2 N. G! S依赖关系:( K7 p( y6 [1 w4 T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, \9 c$ N& Z3 z0 o1 c

. V! ~! {; h, W1 h7 ~+ Gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel6 @! {  R) P; K6 b3 j/ W
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; J% s  |& I) p9 d, G
5 M6 `" g6 F$ o" R6 Oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& b+ I. d' x1 m( L  \, s$ C# U二.启用standalone模块并编译
3 O+ L' b* V1 Y; w7 k* f' n6 b, V# L; W8 T
下载modsecurity for nginx 解压,进入解压后目录执行:
9 w9 f% W% q, E& G# o
! l1 t" a, u' j! u, w* q% Y./autogen.sh" g+ s8 B, _& h) b& |; J9 W- {% ~
./configure --enable-standalone-module --disable-mlogc
  a, i% q4 H1 p+ w* e3 |make
/ H) `( ~$ z8 `  s; K三.nginx添加modsecurity模块
2 a* d. E6 H, p* j
$ D4 L" v' l. m) L! O( Q1 Y' D" h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% I" M* t" N- I; M

) v7 C! P) Z( |% a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 O  g* |; `) T1 _) R0 \  rmake && make install
6 ]. |9 ~  h, z; q0 O四.添加规则+ @: p' U& x! w4 s  E; L8 y$ {# i+ G

% x+ b3 O" g. B7 w/ ~  Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ N& Q+ O9 P+ S

" c  }7 d( v9 y7 ^. [# P1.下载OWASP规则:
2 E' {* ?( I$ e. B/ O8 u1 }, ]0 i$ c( [! Z: n) t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ R0 D9 n4 C1 @9 }
. N! A- e! Y  @
mv owasp-modsecurity-crs /opt/tengine/conf/
" V4 x7 a$ ^8 u' h% F8 m7 t* [" \5 j1 l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 S$ I) i% x: E8 C/ q2.启用OWASP规则:- N! O" D  }; W) e9 L

* r7 p6 z1 {1 Z8 a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 w/ X# G, w2 F, F8 O1 v7 }# X- `3 Q, u3 ~6 G; Q# e6 W" J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* P5 v6 A+ W# V7 S# G! B8 M- b" ^* @7 K4 n, i8 U
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" D( n5 M7 x0 y( c  ~- m$ }/ b% k( G/ C- Q0 S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, K+ b, i# J  MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 h, p, l. `9 ?: @8 Z) xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ @' |  X' h7 t; z2 o3 g' I& @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 a' W1 z' s' E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 x' W4 g) W8 A6 c( D- M! I5 F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 L! R0 T8 s" Z* c# A2 {+ \; bInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. v9 x  a8 s3 V4 B$ x. ~五.配置nginx
) k. V: {% _6 }' a! _& |% T4 @  ^( }* L, ^/ g6 I! \4 Z% [
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ f* F' i* _8 I) p, X" v1 z& }" [* y- c% d
ModSecurityEnabled on;  
. m! R- c6 {: E9 W" |ModSecurityConfig modsecurity.conf;( s. ?3 ]5 @" j0 w$ \
下面是两个示例配置,php虚拟主机:
+ U. Y3 j7 z  c# i, |7 o8 D
/ g( g: Y# m1 T  C% Userver {1 S* E0 d6 e- o
      listen      80;
  n5 r/ h# `/ Y      server_name 52os.net www.52os.net;# T; z+ C. n& @5 E. e+ F
     
5 h& |5 `' o+ d+ \      location ~ \.php$ {
6 h* A1 c6 ^" G+ a' q5 j# J      ModSecurityEnabled on;  
. F0 f. Q. N- [" m, O  y      ModSecurityConfig modsecurity.conf;% c; H" D+ q7 M2 C

5 a& a; n4 @6 a; b7 o( o: ~      root /web/wordpress;  z! Z; P& Q' ?
      index index.php index.html index.htm;1 \% D5 l( @' B; e  B( V
  
4 L5 l3 F6 r& x; g" C      fastcgi_pass   127.0.0.1:9000;
- o3 z+ d$ n. b8 C$ c% Y      fastcgi_index  index.php;
: A) S7 `* A% [& Y( ~! k      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# g  {5 \& w! ]) l: C
      include        fastcgi_params;
0 a& S% t/ `0 `- l0 e" T- |8 b      }  q; I4 _8 N+ t/ \0 ~7 p
  }
0 q0 S1 s8 r, T5 bupstream负载均衡:+ i- e6 e! k" Q, s( N- r
' ^/ }2 K# `- {( N9 R9 \* a
upstream 52os.net {
6 @/ `1 B# H1 X) ], o! n! B  {    server 192.168.1.100:8080;# @- |, }2 r, T: j, V
    server 192.168.1.101:8080 backup;; ~$ i! c0 N2 e+ @. H* F
}
% j6 X9 x* \. b4 b: Z
8 S% s  q, }, V! X& fserver {
8 Z& `( f/ }; I0 a: b: ulisten 80;
6 g6 D( M, f4 w4 i+ u, Yserver_name 52os.net www.52os.net;* i$ c# J7 @0 b% F& a: M
3 _. H- u( O; }* u6 a
location / {, E* D( [4 K, c7 z% z
    ModSecurityEnabled on;  
& a4 V2 F4 t$ O" {    ModSecurityConfig modsecurity.conf;  
8 `% J% z  `0 E& D6 N$ j9 U( N, \# \8 s  c
        proxy_pass http://online;
! J& f( `! a1 U        proxy_redirect         off;
1 l% H4 W* p1 B. [! w# e6 S3 F9 r* l        proxy_set_header Host $host;
+ b8 k! {9 G/ @- d        proxy_set_header X-Real-IP $remote_addr;$ M1 ~! T* L1 P  M
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
. C* A* r* A6 P8 x# O    }# ~- n# O& b, F( a1 s& f
}
8 o4 y8 x: m* u六.测试
2 {( v! w8 R6 x7 ?1 }9 q. ^& ^: y# h1 J7 p" x
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& n1 p: ?+ z& r( h) c
% \1 |+ K8 B( F<?php
7 {1 Z- w4 f) T) i9 v9 M2 v' u2 q    phpinfo();    & h) R0 D4 k( h
?>1 d- J3 N. k' q/ _
在浏览器中访问:
0 r7 _0 p/ S- Y& e; w
$ L( ?1 {% W! B7 z2 N$ l, i, u5 dhttp://www.52os.net/phpinfo.php?id=1 正常显示。7 m; r- X7 _% N* y: m# L
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ g+ A& `" Z1 @' V" U: Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: k2 \0 m9 i% L& j/ _: z说明sql注入和xss已经被过滤了
0 b% p# `. Y( ~0 p; W- D0 Y. o) S2 e; f% n; Z
七、安装过程中排错
* M7 N  e% S- w/ s1 O
  P  O5 k4 {3 _! ]/ G1.缺少APXS会报错" ?  f  O8 [! p8 O) p6 ?

8 R4 O1 K9 V/ o* Z( b5 zconfigure: looking for Apache module support via DSO through APXS
8 G: H2 p4 |' D" j1 Nconfigure: error: couldn't find APXS) A0 R. H) c2 M/ y: O% h+ Z8 f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. p- g& r6 H2 v+ d; v解决方法:
' k! c8 R. Q, O4 y, R8 w+ \8 _" S! L( g; V  j+ H1 C* y( ?  x/ C
yum install httpd-devel
3 l4 H+ {0 o6 j$ Z4 c- x2.没有pcre, Y5 R6 ?% F6 [6 G7 P. c
" f3 n2 k/ ]: w3 p- R' T& A7 w
configure: *** pcre library not found.9 a/ N/ e- R, p& _
configure: error: pcre library is required
2 u9 k8 O4 R3 Y* _. }% ]! }解决方法:! H4 j! t- x2 J8 X
/ d3 y! n# T0 M* R3 l! u, Z+ Z
yum install pcre pcre-devel
7 d+ C% y3 I6 ~3.没有libxml2
& V& ]) U+ \1 g- g! @' ~; l5 j/ ?9 w& H2 i2 @3 Y; i  I6 h; i4 j

4 D9 M% f% @- h! ^' ]# {2 Cconfigure: *** xml library not found.
; E; U2 k1 V/ Wconfigure: error: libxml2 is required' e0 e- l6 q) W' m0 f
解决方法:
/ j( ]$ `* J4 C! H( J) p* U: K  I; @/ j+ g# Q! d: i
yum install  libxml2 libxml2-devel$ P3 }. k( H& l2 a  v
4.执行 /opt/tengine/sbin/nginx -m 时有警告8 }5 |8 L: k* ^
( t' _- r; m, m, I: D4 T9 m
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* e% \6 ?$ C; e/ G: j% anginx: [warn] ModSecurity: Loaded APR do not match with compiled!% {: t9 |% ]# r3 l: b  i: [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 l0 [# z* w8 a; Q0 o' w! j

: c* b! C/ e" Y1 G' K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( z% j9 u/ i/ K  O$ m% K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! O( L: ~  |7 J) ^6 w: o5 E2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 o* g. v3 Q% B6 @/ @  G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" F7 R6 Z' w; }- U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# d: s! d+ y$ V7 V) ^2 w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ d( }7 O* ]: J' A  z8 X解决方法,移除低版本的APR (1.3.9)* q6 A+ l" Q" L! V& u) ?( f
( ?) p, K3 @4 m4 R1 d/ F7 V
yum remove apr6 F  ?( x3 x# N8 e
5.Error.log中有: Audit log: Failed to lock global mutex
% ?( g1 d8 Y6 S# [( z; \% b1 u6 Z8 M! E6 ?+ x1 e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' g* Z$ q: |7 p; `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) d! m4 E& s3 M3 }$ w0 ^3 m1 [) s解决方法:
8 @" J" l* N- c& U9 R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ t/ m9 a) w2 d) [3 x. _) s
# Y6 H- y- A* ^. e3 l4 iSecAuditLogDirMode 0777
9 t* x5 |8 V8 M* c% JSecAuditLogFileMode 0550
1 T, A' u3 a* L0 T# L# ^+ sSecAuditLogStorageDir /var/log/modsecurity1 Y" ]  y7 x' R$ o
SecAuditLogType Concurrent
% ^+ P% }0 d9 w3 |/ u$ ]. v参考文章:! P3 A; M# I9 K2 a' ~3 r
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. Q  h3 Q/ a8 b5 M5 ghttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-5 05:35 , Processed in 0.064631 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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