找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12833|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。. v: T( X" L+ {6 k# p
  D/ q5 V# u$ g3 ~1 }( P
一.准备工作4 s3 B: j6 L6 j8 T: @5 j
$ Q) ~0 A0 \0 Z. N9 ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 H% y2 z. Z) ?4 Y" m4 g- v, b: n/ {# n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 ^' K& x, H% o" n1 Y! }1 q$ Y7 i- i' i7 w- W
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" }4 w7 A5 b% u" @. e; Q! M# w/ E/ u6 }
: N0 Q2 z* t( x$ S/ [; C# ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; e/ S- {5 ~* B" m" x' f3 e0 N" m3 T$ R! h. l
依赖关系:! W! R( r# K' x5 i4 ~8 A4 |1 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& E2 M% R1 |+ R/ \' q" H! f1 w
& X. L% y" x3 e. f' j& i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! ?- @; ^: }- j; B& ?( k% Imodsecurty依赖的包:pcre httpd-devel libxml2 apr
! V3 i2 P3 a* x! f  V! c8 Y
4 N+ L- B* c9 j  D' @0 Fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 m5 m1 s# f3 S: q4 H1 a6 ?- H' t* x1 d
二.启用standalone模块并编译9 _; Y& ~  f6 V
: {; Y) }8 U" C* r6 X# c
下载modsecurity for nginx 解压,进入解压后目录执行:
( r% z0 k6 b6 K2 t
7 p) t/ E  ]  l7 R8 a./autogen.sh. u6 i$ k7 u2 J( K* I3 N
./configure --enable-standalone-module --disable-mlogc
$ P8 e# U0 v8 Z3 A" e2 U9 i4 Rmake ' j) }- X' {( L" O2 m
三.nginx添加modsecurity模块
5 p2 B) E4 q) ?; M! D# w$ x$ Z
, `& x1 t% z9 b( Y% W' M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' N2 P3 c% s) h  ?  W4 P8 C* R: Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% O2 L* N' h  E$ o+ hmake && make install
7 z6 \* D4 t9 i四.添加规则$ o7 @4 w0 D+ Z0 T5 n$ M
! y& b( y, U8 D: J: n( S# e
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 Y( e; ~2 K+ g
" p, ?' B# \; @
1.下载OWASP规则:0 q  ?: [5 q7 b
4 |9 K) M. ?7 L$ X  l# E/ b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 f; a# J. |$ l+ i' L

, @3 M5 ?: q6 ?) a& Jmv owasp-modsecurity-crs /opt/tengine/conf/
& F9 h4 a. p% h8 i5 U
0 [2 V/ ^' `2 x! y/ T0 Kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; [8 ^  ^$ m9 {) B5 V) H5 d& ^2.启用OWASP规则:
: U) J0 B) }' [6 f9 J: J: z9 M5 r0 e8 d: c, z! s8 H* Y- h
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 H, ^& @4 s$ w' E0 `1 h3 Y3 e/ b' d8 _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 l5 U1 s9 R/ k9 N
4 Z+ Y+ N2 t- f  H7 Qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# u+ t# V( h$ f& h* N5 T0 k* r) m# t: q
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# y8 E3 x: H$ Y6 M% X" Y+ FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 x/ J2 q# D5 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; ?. ~6 f  n( x2 l! b; N* lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; S$ g+ Q8 x3 W7 S  |& e) j+ b1 vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 m" c2 N6 P) s) |" Y% U  c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; i/ K) [. T3 t2 ]5 X, h
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ ]1 b9 G& R* Y/ u0 ]7 p* }
五.配置nginx1 i. D9 n: v, p) _! R
. ?. @4 [8 h* F
在需要启用modsecurity的主机的location下面加入下面两行即可:. m! {# s/ j- w4 L0 L
: V/ i& {0 K- S
ModSecurityEnabled on;  % r3 B/ s6 R' Y$ l, T
ModSecurityConfig modsecurity.conf;
% z- C7 n" S, \下面是两个示例配置,php虚拟主机:% W! Z! O( v& `2 B

, K& Q: X, [& C; \; `2 fserver {
/ A( P: @. m1 N1 |8 n) ~      listen      80;
2 `  _/ N* l. U. a; U5 W1 Z      server_name 52os.net www.52os.net;
9 B- C/ r$ T3 j6 [, ]9 w* k     $ v! ]+ g6 ^  O
      location ~ \.php$ {
/ ^( U% d2 O7 f. @! E- X. O9 U7 l' \      ModSecurityEnabled on;  5 d# j3 x" t# a4 t
      ModSecurityConfig modsecurity.conf;3 d% L0 a" o: f

8 ?0 G& k9 N+ L! p* d      root /web/wordpress;9 G& |6 G+ t) [1 ^1 Q  C
      index index.php index.html index.htm;& `8 B, T  ?' j0 A( n, x: E* V# ]# _
  # j! C9 R% q" S
      fastcgi_pass   127.0.0.1:9000;
! ^( |. D* A) T: L0 S      fastcgi_index  index.php;
' I0 H! M% t: G8 f7 {8 D      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, L1 i6 G# w% k' B8 r: [      include        fastcgi_params;- W7 k( r0 a( f. o& G: ?/ D# `4 d
      }
4 O, w& d( D0 \5 {  }! U# T; B4 v- |( O1 R1 ~
upstream负载均衡:' e* U6 ^& h- |0 J0 `% a, n; r

0 R) T  g1 O3 b! r9 s" `9 \, hupstream 52os.net {
) d0 x: ~" l/ d' i6 W5 D    server 192.168.1.100:8080;9 r0 |. p/ l9 B) N, m/ k2 ^
    server 192.168.1.101:8080 backup;3 q  Q3 x& u( H& {- ?
}5 z( t2 K! p4 }8 }$ O$ D9 B
- D; v/ G. h* }# G
server {; S8 I! j, U' G( f4 U- a* P0 F+ w
listen 80;
9 e' g3 J  [: Vserver_name 52os.net www.52os.net;
& a# t* v0 F+ V
: t. T) E% I. Nlocation / {
5 D2 P6 P% c. m( F4 p    ModSecurityEnabled on;  
+ _) _3 J; p  V3 }9 `! w    ModSecurityConfig modsecurity.conf;  
6 L' A" l, T5 b& Z" T; n) D4 z9 d' Y
0 [  r4 H  R" u& f! B) N        proxy_pass http://online;
/ A7 t' m1 J4 t( n$ j0 S4 b0 {' H        proxy_redirect         off;
' A1 j3 A/ N1 a! l        proxy_set_header Host $host;2 B# x8 z5 _- ^( s5 |+ S
        proxy_set_header X-Real-IP $remote_addr;7 q) E8 d' y4 D1 l8 y5 r* i
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;+ p+ l( \# o% k6 X2 q6 G( j
    }# R* g% _, Z+ f" T- S3 o( U
}5 D& X, p0 J3 V+ e. \
六.测试+ ?: d6 B1 n) o1 R4 g
5 x- q& j* P6 M6 v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& J! V9 R8 P/ K0 j9 u6 T7 R8 c4 m) `1 D* [7 c, C+ G
<?php- {; `# c4 M: F2 T( t# I" D
    phpinfo();   
9 i3 n& S+ @8 @( v+ d?>1 i1 I+ G0 P. ~3 @
在浏览器中访问:, }8 C- j- o; |, U* Y

% J7 Z  r* _6 G  e* V- qhttp://www.52os.net/phpinfo.php?id=1 正常显示。
: f; D: I2 }! J: @http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; ]" ]% r" |' @0 k+ u( x- c) Y. Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。3 n- s: ~* ?: L* X, ]
说明sql注入和xss已经被过滤了
1 K4 K8 X3 r; a: e+ L% g& I, E' h% d$ y+ r2 m0 S5 H4 A
七、安装过程中排错- e* R- o4 f6 D" ]

; Q4 S- {- z" N  J& M. c. ^+ m1.缺少APXS会报错
& J1 \" C' a; _2 O0 x* t+ Q
- m: _: }, R: A( kconfigure: looking for Apache module support via DSO through APXS
' x- K6 b1 f* S" A, |/ Dconfigure: error: couldn't find APXS+ F6 H3 R) U; f. U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# @1 d/ F1 y: L7 D7 k
解决方法:
- G/ ?' P, d9 b0 H& }5 c8 ]
2 r0 m+ H# P1 f" I# \1 _: v/ tyum install httpd-devel
4 R' H. J- k6 l! m. A. C2.没有pcre
- l( U$ m% y+ h/ m5 e9 p; W
( M4 X0 R! q* Z+ Econfigure: *** pcre library not found.
; }6 k3 F* E" o, X) y7 p* @configure: error: pcre library is required' i: A- S" E3 ]- v9 W
解决方法:
, D% H# ^6 R2 \
1 P/ F0 m6 a+ u- L6 }+ Yyum install pcre pcre-devel3 ~; [! f; a% U
3.没有libxml25 F: O: u3 P- q+ ?

. g% f) u! |7 }6 f5 C  I2 P. u# _
! ?: w% v2 ~1 u& \/ u/ n1 _9 Aconfigure: *** xml library not found.# r- R; q1 a2 `3 C* M
configure: error: libxml2 is required9 u5 I0 |2 I) {* p% m+ x
解决方法:
- z' T( P! X* K+ h" q* w4 Q6 [! y' j) G" g' m- _* K# y
yum install  libxml2 libxml2-devel
: i& x- T# _  g: f8 _, H4.执行 /opt/tengine/sbin/nginx -m 时有警告
( w' C" g  ?8 S8 p+ o) T, [: V" n5 ^7 Q; O; C+ v( _
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! H- I0 C; s) Fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! s/ i4 r/ q2 p4 @9 p. a原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( B, N7 v$ e1 X7 D- ^1 U/ f

$ @' l0 C2 A7 ?8 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 Q+ W3 S; j7 r" r9 G( K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": L& l$ t) G% s: R5 _
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 B& S* d. _" F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 @/ l+ b8 `$ J+ @9 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ t8 G0 q/ x% L! Y# g2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 L% l5 l7 y5 b+ K6 z解决方法,移除低版本的APR (1.3.9)
0 m9 O& n8 j9 o. F& v: I
- x2 \4 y0 @/ M! B7 c! Xyum remove apr
) y+ s8 D0 ^; t; b" I" T9 @5.Error.log中有: Audit log: Failed to lock global mutex, s! ]* j/ f, R

* N$ F, C  `& }* B2 R" r9 `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* \  J- z" e1 u* Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 D0 t; r! |. Q& ^( ~& `% M/ n! Q" P解决方法:
  @% i5 B- ?6 U" |8 D) y" m编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; |3 n9 `! s& h- w; P) Y0 E& }. Z" G/ ?
SecAuditLogDirMode 0777
& M9 t* Q- v" U! s3 z% @+ HSecAuditLogFileMode 05508 J3 X' `1 Q6 |- l+ ]& `& b
SecAuditLogStorageDir /var/log/modsecurity8 i1 _* b" R, l. N" R
SecAuditLogType Concurrent
$ u. i% P1 y: K( D) K参考文章:
1 m) F0 y' l. A8 \, |https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: Y% p/ X3 P1 y, {+ ~( Chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-16 14:20 , Processed in 0.072443 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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