找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11966|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 D: w) C. ?$ ]; P) p( h2 X6 H. ]

  n7 o8 m/ y/ l) X0 ?: @一.准备工作" a. P/ z4 p* b2 u: T; N& R) _6 l
( U( T3 b0 n+ f4 ?
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 |7 m; }# [3 N) ^
# u% {; y% [) X$ @) ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 o! o2 F; k; g' E; s

1 i2 C. L0 N2 y" N' z# @% _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ O2 x4 W: W) S+ f* X) J; {3 h: Q, M
& F- N7 f: z( B& D( VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 r# `' `# k& U& k
  i2 @) ~/ V  T4 u0 ]$ F依赖关系:
. I1 a# p2 ~( vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- l  ^6 `: G9 T+ ]8 _
, _4 O- c  d* a% A; v6 myum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
/ v2 d$ @+ ^2 k% a, qmodsecurty依赖的包:pcre httpd-devel libxml2 apr
) f5 p: A  Q( E1 b, E* `( }. Q: M* P1 n# L. }+ C/ S; b" Y
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ Y7 m- `: W1 \0 z" A
二.启用standalone模块并编译: R' b' v. B& h" X7 @1 h, }& M/ _: @

: W1 x, X% a! r. l下载modsecurity for nginx 解压,进入解压后目录执行:& y- e- v8 a. F% g; U6 d

, C6 l# N# q3 x( r/ }  a./autogen.sh- a: C% S+ v# F
./configure --enable-standalone-module --disable-mlogc0 H7 [$ C. q4 l9 e. A* p: _
make
2 I% h0 X* z; E0 V三.nginx添加modsecurity模块
  [' s/ e$ o$ T& ^; H
, }2 |3 {' J* z# D在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ a, T1 W) y6 U- e: M
' X2 l' ]1 L  C+ [) D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* G. T5 W+ g' @3 u6 U
make && make install+ T( n/ O  w; B, [8 \
四.添加规则4 T8 n3 P; q4 p# m9 c
/ t% J% n$ P6 K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% i9 t: i  p- z. q" J3 g$ C8 W* r' r1 G
+ z- @) ^# f6 o1.下载OWASP规则:
$ g; s2 F1 {' F% R
% j5 }9 U7 V( X" _/ U! egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs* f" q9 O9 K* K/ s8 D$ G) v: F1 r

/ E9 ]6 [5 q) _6 Y! g3 @' Pmv owasp-modsecurity-crs /opt/tengine/conf/
; {, s2 V: v7 A# y, e# O1 y3 a0 |7 [7 \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 e3 R: ~2 @, N3 D$ R4 h+ ]
2.启用OWASP规则:# i1 H7 _$ z5 `9 G0 q" s
6 @* U0 A' i8 Q( u& y% r6 Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ J3 L% i0 |7 i# K9 m0 y+ Q& s" p3 n4 L3 g1 x  K
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, {0 |% @7 m* _+ W6 \7 s$ r8 s' e" e
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 I' L" d3 Y8 w" n$ T4 z

* i: C* L# F2 j$ QInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 R1 Y8 H2 H% p+ M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( Y; M/ F0 Y2 D9 o" _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  h( P3 y: O6 A7 S/ qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' |9 Z1 r$ _# [
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 H6 n! f3 r+ E% k* P& mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 ^5 E  _0 C, V% Y4 eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# T. x' v0 P! C( u
五.配置nginx
+ ~/ m& y$ f7 H  g' U: ~9 u3 P: ~0 C1 J/ i$ T4 T* _
在需要启用modsecurity的主机的location下面加入下面两行即可:  f+ s/ `& |' f; {
* g7 l; J$ S9 J
ModSecurityEnabled on;  
  @" j. f" \1 L6 VModSecurityConfig modsecurity.conf;
3 W. X7 K) `* k下面是两个示例配置,php虚拟主机:
  f3 R: k' ~# w/ |. p% e  H  u: o% B# [. w; g8 f
server {
$ `4 O4 ?* s- [0 i1 Z& W/ b+ N      listen      80;$ O9 w0 I: G* f
      server_name 52os.net www.52os.net;
* E) Z3 Q6 H( x. m( F7 ^     
2 m# w/ U! L9 a/ {" M4 |+ g      location ~ \.php$ {, D+ d9 |/ J4 Q1 n- I
      ModSecurityEnabled on;  
: [; n0 s4 H* G) y7 e7 m$ r      ModSecurityConfig modsecurity.conf;
+ \  b7 B+ y+ f9 c
' \# W3 |, P) I6 w# K      root /web/wordpress;
: t( x& I5 T" c$ G0 h; B      index index.php index.html index.htm;+ w* U( ~; [' b" B9 E6 G# R1 d( O
  
; n0 h, u+ i3 k' n      fastcgi_pass   127.0.0.1:9000;% H, V  k! ~/ d' f
      fastcgi_index  index.php;
9 S0 Y; X( m3 c+ A+ {+ T      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. x) _4 m$ f" l  r6 d6 X$ {# M3 g
      include        fastcgi_params;1 a/ s1 D0 [. @
      }
! K& P$ p& X) F9 C  }5 `9 h6 y. l1 g# x! E9 Q$ f
upstream负载均衡:" k( A* \, `9 p0 m( B1 A

$ K9 L9 J" N6 mupstream 52os.net {
* x3 g2 g! Y3 ~2 j( ~" R* G    server 192.168.1.100:8080;- m* {+ _2 p& A9 g& Y( @
    server 192.168.1.101:8080 backup;
5 O2 {: C' p: X}5 k# z; A6 D# A$ m+ C9 W2 [

3 G) r5 T' K6 Vserver {
7 x! R  C) ?' S0 s1 L4 Jlisten 80;' a* ?3 T9 N2 T; l3 @) x
server_name 52os.net www.52os.net;
. a! Q% R0 d1 o9 b1 @+ p$ f. A6 _$ s6 a6 Z
location / {
: m0 K. n, X% t4 d0 s; M    ModSecurityEnabled on;  
* P/ h5 X1 S- S" r  `    ModSecurityConfig modsecurity.conf;  
+ T& C# C5 l5 n. t! R
# D2 H- P, l- P/ U* |        proxy_pass http://online;
/ C4 E: t2 e4 M6 Z8 i3 H/ A        proxy_redirect         off;
3 D7 O7 V! H+ A* B: H6 a+ M+ K        proxy_set_header Host $host;
) F# A' E* a; D  ?9 s        proxy_set_header X-Real-IP $remote_addr;9 H: {) W3 v- g, Y+ ?  Z9 |' S
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ W& V* K" ]& c7 t9 l6 b    }
4 x  E0 H3 D+ c0 @9 n5 o}
+ X9 I# P2 G! f' o9 N1 w, `, q六.测试5 A+ C7 J$ n- _" ^( e5 u
" C  M' D2 ]$ @0 ~, u5 e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& S6 i* ~, C% W# _3 x

3 O* f- ]( i( ~; h3 T9 T<?php
0 W- D: r2 L" z: A8 [    phpinfo();   
# u6 U' m7 }, H3 z& O?>9 n, E4 ?) ]6 k6 f$ m5 p! x' N$ x
在浏览器中访问:7 s( H, A8 I0 e8 H1 t: S
' E' N1 B" D3 r1 `
http://www.52os.net/phpinfo.php?id=1 正常显示。
. G# E' i2 c. Z3 p. ~% k, s3 k' `http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, Y* c  C/ k/ u" Z9 yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' i) {9 l0 {1 U% Q" r8 b3 J
说明sql注入和xss已经被过滤了
% I9 N5 w% R7 J6 [: P4 d  \8 C% r& |. J
七、安装过程中排错
+ F9 U% m) n0 o; a( b' [  E  @* j: e1 B) X1 y
1.缺少APXS会报错0 {- `9 t: t! W' ^1 h( R4 Q

! ~6 r; ?0 p5 S& A( c+ uconfigure: looking for Apache module support via DSO through APXS( S' h7 ^" N- a
configure: error: couldn't find APXS5 `( L, O9 c% a; N3 f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" x# s4 h( ^4 k+ B
解决方法:3 w, M5 k1 e) z9 A1 R; l5 r
# Z" a( C9 ?4 o: W  O0 {2 |
yum install httpd-devel
& M' G% R6 b9 c9 u2.没有pcre+ z7 q9 q8 f- }5 l. D

+ ]# O% e' k/ O- }  oconfigure: *** pcre library not found.
* |" e7 b  o  I- `* _* S4 Q/ ]configure: error: pcre library is required+ W# e/ c' D( `; I" B$ J5 T, M
解决方法:* E4 ~  @3 F" x

) o5 T" O3 q/ H. y/ Eyum install pcre pcre-devel6 M- i+ o: f9 t9 f4 K* v
3.没有libxml2. W1 w# G7 ]- c9 a! s
+ b6 U" g+ i* g
- ?# e! m$ x8 `4 ~1 ^
configure: *** xml library not found.
! M. q% P: ^* K# G# x' y7 l* gconfigure: error: libxml2 is required
; w8 F$ X+ t* [3 V9 R( F解决方法:. O& N# ?) g- i' A/ Q

& s# m4 Y# |$ ^4 R8 K) `yum install  libxml2 libxml2-devel: {0 y! ~/ u6 c. P0 I
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ _$ p4 @1 R# f8 i
- t. `: S! D, p/ ^, I/ ?6 U- k7 UTengine version: Tengine/2.1.0 (nginx/1.6.2)3 z- ]; @5 W9 a/ V0 e5 O! d& a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 h; M4 f* U7 p; ~- o原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 }0 m, s  s( m4 H2 h8 ?- l9 E6 [# |7 ]4 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 Y' X' B! {& X8 ?! ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 X2 @! v2 `, W; j, u3 R4 Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) u  ], k6 a6 w' B4 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 P4 V6 Q. A: c( I/ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( `  z) D: B3 B1 P" ~2 i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  {) ?# @8 G9 \# E$ A解决方法,移除低版本的APR (1.3.9)3 t& J5 u8 K! H# B
/ W( g4 e4 n" y9 t' q
yum remove apr
# X3 H2 D# |# o" [: W( L5.Error.log中有: Audit log: Failed to lock global mutex
3 i, e2 C% Q0 l# `$ t8 K
8 p6 H( ^3 ~* f+ m3 U2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 q# W8 a4 l* _" M. M6 I0 n7 Q  lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: p& O- B- o4 j
解决方法:1 |5 j( u  u. n, _' l2 l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 s; F7 b9 W' c9 Z5 p( v* e
3 v3 |3 M9 b+ E6 U$ B+ SSecAuditLogDirMode 0777* p; Z# u* n/ i1 y5 K
SecAuditLogFileMode 0550! x  `  \1 S# J' w( p3 E
SecAuditLogStorageDir /var/log/modsecurity( v# b+ D; T  E; d
SecAuditLogType Concurrent
- A6 D  k* l8 X" J; q( f参考文章:2 H3 P# q+ D1 P" E6 R" z$ [- ~/ o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 D" `7 m9 q& E  e9 @" I( shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-30 21:12 , Processed in 0.071513 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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