找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12585|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 W! D6 _6 h. d  E9 ~8 i& i9 b3 ^/ }+ q" z( P# m. v# P8 d
一.准备工作8 V2 q: T$ R# L* R  X( p

* X5 U, f' t: J  A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# a; D2 r0 W- }- u5 L. x3 R

1 P- e9 A; U7 |! w/ m2 {4 E- ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 ]8 b5 u$ X& U) J
& H7 S+ W2 B1 ]2 t- {* V( k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 M3 A1 {6 j7 x/ S  L2 B- i, \- m3 d- n/ V2 ?
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 d) V3 B: z2 g" ]1 H) V1 O- f6 W. P' b( R' j, v: w
依赖关系:
1 L3 {( p- _- c" V/ @$ j. k: K6 dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 O! m. P9 J: p6 f' {7 E

6 n: z$ b8 E9 n2 _) ~6 \yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' e$ n/ j1 R' V2 {' c  p
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 g. \8 r8 y1 L: J  y0 I! q! s8 `
: b+ s6 P: {$ U7 M1 Y/ s, o  ]  Dyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* `+ W: f. ^; p3 K+ S7 {
二.启用standalone模块并编译
) Z1 M: ]$ p2 q; _% F( n; U5 }5 F  x3 }
下载modsecurity for nginx 解压,进入解压后目录执行:8 W+ }! L- l. C* E0 b5 K! j
" B2 |2 r" y# j7 {0 E4 A
./autogen.sh( Y( h* k4 p" l- t3 G& h
./configure --enable-standalone-module --disable-mlogc. m( G# h  B0 y  f2 f" ]
make
& L" h8 f7 i1 P* h三.nginx添加modsecurity模块
* n( |' q0 P7 \' E! g7 H2 O* L* s4 R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, d3 H- o9 X& j8 G
$ j# M) H, [; U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& M! V/ x$ d( j3 l5 T$ W: ?# Ymake && make install
4 l0 c- @( H5 S. e- H) B! M四.添加规则
4 y4 I) k  r. {) V  a5 d! S
/ Q6 z. S: b5 h& @9 t  Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" b2 S) [/ c) s; W2 W3 c& V* Y* p
$ P8 T: \% q$ ~
1.下载OWASP规则:
% s/ T4 ]9 j! i
3 Z$ Q+ r: @! D! ~/ @git clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 D) D( {' @5 }' @
" t/ U9 z2 q! i( Y0 ?7 S- a
mv owasp-modsecurity-crs /opt/tengine/conf/" g% L7 g% o  y4 x2 Q0 S6 v' X

& W2 u' `# j7 @& R  scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; ~% d' Y) f, K2 f" d2.启用OWASP规则:7 L- S  x! X, y% {

* Z0 M" W  o' k+ N6 i" r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# T, Q# t3 O: @8 o# d" Q. m/ E

$ e+ r$ n4 K+ L" G! N# y: s  q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
# Z+ |$ e: M9 m1 D' r
" ?& w3 y) T1 Z$ S2 Cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: O" l) _/ I& G" ]# F/ j% O4 P5 T" r  [. W( ?# i  w; z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 E8 G; _- c1 u. g5 [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! o7 T5 f  g; J) l0 r' W2 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 W" @% t- e% b2 e' Z7 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 n+ m% \+ F' G6 H/ |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. i  R# T* C" g. ?+ v- }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ T9 h' p4 q$ M2 u; c; t
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 w2 B. ]9 l1 j6 o, _+ i6 s/ p五.配置nginx1 u$ B% Y% Z$ Y+ r

# p. a6 F1 i( R: u3 d( _2 }* ^/ n在需要启用modsecurity的主机的location下面加入下面两行即可:/ k. ]6 H$ r& M' I: Z

: s3 f7 F: t) rModSecurityEnabled on;  4 x- K4 N9 e- D8 Z7 ]8 k8 x
ModSecurityConfig modsecurity.conf;
1 `5 v  R# |6 t. k下面是两个示例配置,php虚拟主机:
$ J: x3 S9 w6 y3 ?$ D+ m
8 D& y$ [3 B* s' i1 Z$ @6 Z  Q9 u" Iserver {
; m& s/ W* n7 U+ P" G/ G5 b      listen      80;$ w) D  m+ N* q
      server_name 52os.net www.52os.net;" q4 S6 `/ o2 n/ n7 y2 E. e; S1 l
     
3 C* e5 [' u5 B6 _6 A      location ~ \.php$ {
" ^( p1 \8 Z) I! z; E. i1 L      ModSecurityEnabled on;  
6 l% R9 S5 C: N( ~0 X      ModSecurityConfig modsecurity.conf;% n! G( m; ~% @7 {( X

- e5 X" @' n2 a$ V: W      root /web/wordpress;  B# A2 z+ J' \8 |; E7 H+ g% O
      index index.php index.html index.htm;& w2 z& `% a% v: s7 e8 `7 q) _
  
( N  T9 }2 G* F5 x      fastcgi_pass   127.0.0.1:9000;
- D8 @' U  q' Q      fastcgi_index  index.php;  T! c# h' J0 D# A, y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( f, b' o7 Q# i( D* K
      include        fastcgi_params;
* r" t% _3 f9 p4 `* y      }
5 H/ p8 G& y( c  }
& D7 L4 {% S. n) _upstream负载均衡:( V- R+ c6 y6 r# B1 J: p/ j- ]
$ w5 n$ k' j+ m' h" j, r6 G6 a1 V
upstream 52os.net {
# v1 @  W9 s" Q    server 192.168.1.100:8080;; q8 G) b* h9 I) [, B" S
    server 192.168.1.101:8080 backup;
! b. D" b! E- F) e( `8 b. P. C" Q}
$ m6 {! F9 i$ X2 b$ G3 Z( S& P  o* e, y' \
server {
+ d  A  m) G0 t8 ~9 |& t$ Qlisten 80;5 p& F1 r+ x6 E
server_name 52os.net www.52os.net;
; h: W8 d9 D, E, O% y5 s0 }: {0 |4 G' I- @) r' K& `
location / {
+ Q5 [5 h& O) _, ^/ U7 {1 V) f    ModSecurityEnabled on;  7 M0 t  q; _! z
    ModSecurityConfig modsecurity.conf;  2 l+ U  ^% S% q6 ^! D4 _$ I
" q- M7 I+ O7 C" M
        proxy_pass http://online;$ l/ A1 `- G0 ]9 I; v  T
        proxy_redirect         off;
  R% s( C9 u3 K' A3 H* G; {        proxy_set_header Host $host;! O- H9 k: V4 w: N9 v$ @
        proxy_set_header X-Real-IP $remote_addr;( s1 o2 [0 Y7 a4 n
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& Q3 j7 o  I" J/ E3 _
    }+ j( c; a6 d: t
}
) D: v, ?: X" g: v: [9 F/ n" T% e5 G六.测试! Y- `$ o1 Q& ]. `' ^3 b

2 r6 w2 ?( l; i5 f, i  Y( V' x5 N' B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& y, d7 D, d6 a* h4 j( B' Q# c  j0 \" H  T: V3 |4 y
<?php
" p0 O. m8 S) ^9 I    phpinfo();   
, A$ a8 A+ x7 t- U?>
2 L+ n7 l; B3 S* j在浏览器中访问:$ ?  x( d) A, i1 p" A1 h, H
7 B7 m$ w9 Y: |
http://www.52os.net/phpinfo.php?id=1 正常显示。4 u7 @6 j% ~- h2 R5 \+ f1 j
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* w6 c/ i' \9 U
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。, r; M3 j1 O" ]6 y. |' c1 G+ A
说明sql注入和xss已经被过滤了1 f& [( Q) l; w3 z$ g$ P
  r7 G0 C8 q/ e- g! g- C
七、安装过程中排错
: q2 W* e% V* C+ d8 ^( n: @1 \/ ^
1.缺少APXS会报错, P/ ^% ~/ L) }6 z

1 ?: ^% m! R2 u- O( Zconfigure: looking for Apache module support via DSO through APXS
5 P( y/ z: h- x& N$ ^; ?* V- Xconfigure: error: couldn't find APXS, k5 T7 `$ Q( e+ H
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  w- Z. s# i" n7 l3 [' C解决方法:
- i: H: _1 B. U+ I* l7 M% s
! s0 G; d0 m$ u9 ~: zyum install httpd-devel
/ t) ~# p# e* X! x/ j3 \2.没有pcre- p& }5 R4 M) Q/ b
5 }, E3 y8 H( [# P" ?1 a" n! f
configure: *** pcre library not found.
% Q$ m: n" D4 C! V* yconfigure: error: pcre library is required
# v8 r' {/ z' B/ ^( Q- [: R解决方法:8 R" C0 |: K& A7 F

* I/ ~3 F" |) M: u( P2 D- {yum install pcre pcre-devel$ i& s6 y2 b& @1 T3 [! T$ F3 J
3.没有libxml2
0 i" k& \$ x; y9 N% Y2 d( j3 Z' ^& Y$ D' o/ v: i
& A* e, p6 z, n% w
configure: *** xml library not found.# v$ @) y& p% v" K" e) f" y
configure: error: libxml2 is required; s5 W! }3 E' k% [
解决方法:& j7 w+ e: t% z$ Q' Z
7 m/ E/ I& n+ C) k1 |5 E0 d
yum install  libxml2 libxml2-devel
2 X) X! R3 H9 J& t& i5 Q% _4.执行 /opt/tengine/sbin/nginx -m 时有警告
& D2 m4 p, K! C9 Z, z5 t% P0 z7 d
, e* A! D; F& _2 h( I8 u) o' u8 QTengine version: Tengine/2.1.0 (nginx/1.6.2)
7 J& l. G+ ^5 Q% C. I+ i0 W/ `1 bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ S$ S" B1 e0 ?3 @8 x& A
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" `6 E+ z6 g( K2 a* @# p$ i
* c" f. O& I& ]8 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 O# q  D0 q/ k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ n/ v/ g" _' E' N: J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 y# [8 t0 ?% N) V# W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 c" D8 m9 p0 J5 h7 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 U, |- H& x# e7 V! S
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 I* W. E' d- w: p6 \0 `+ ^7 F解决方法,移除低版本的APR (1.3.9)
% ]3 j, F- w6 Z/ b+ d) n
, J* n' k+ I( z9 ~3 w. L0 Ayum remove apr! w7 b6 h# i* p0 ~1 h
5.Error.log中有: Audit log: Failed to lock global mutex
' x! o, m7 K' R6 y3 t6 \4 F7 c1 h+ u& v+ d6 q  Y: e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ m4 e. @0 M$ V
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ O$ ]7 `5 m8 I; p2 k- t$ I
解决方法:' h+ ]8 N7 Q& \! l, w8 J" M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( ?8 F3 b, E) }! C) A) a4 X8 X, d3 N( T
SecAuditLogDirMode 0777
; V5 D3 F: g' v! v' CSecAuditLogFileMode 0550* ~2 B0 R# K) K+ x1 N
SecAuditLogStorageDir /var/log/modsecurity  x- \7 i: z$ Z6 b6 E  o" H6 V
SecAuditLogType Concurrent: u7 z3 A4 e6 z7 c* P' F7 k
参考文章:. y* ?  A4 n2 A, k% J& Y/ a' w0 D# y. M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 p" L0 m+ F& P' J0 d0 o! [( _
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-20 01:51 , Processed in 0.061865 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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