找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11221|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 F1 E- u0 v5 `8 n& ?! i

- p  b$ Q# p0 K' D# {8 K一.准备工作
) Y* I* @' M* ]8 h  Y8 o+ M' W
" d3 m3 K, F$ o* R# B- w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 k$ J7 p$ ?) d$ g

. O6 T/ |$ |, B4 h6 N% Ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; d" {4 K8 t% _6 K& X6 }  I

9 |1 Z% e6 O1 \! E2 gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 u+ {! x- j  m' i& q* U2 P

# X9 ]* q5 ~. i$ j9 V3 d( yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 v6 J# E; c& M+ d7 k
: {% R% G& f3 |5 W4 D% W" @依赖关系:
. q1 y2 M1 G6 r' _" Ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 ^, _" K+ C) @0 M3 D: d! S6 E! m4 c4 ]  e3 K
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( d. P2 _, h( L) @+ nmodsecurty依赖的包:pcre httpd-devel libxml2 apr7 f" }$ d3 w" C
% H4 n1 G7 q( W) f( C# S9 T
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel. g+ @8 }) L; u3 q% x6 @8 D
二.启用standalone模块并编译2 c6 S) M! ?  J
* B- ?' z1 I, K! k; {: M$ C
下载modsecurity for nginx 解压,进入解压后目录执行:% m6 `$ X- x  a. G: C) L

; z4 L& B+ i& I" I1 x2 N9 K1 Y./autogen.sh
# ~+ h5 k( |! `: R. K& _- k./configure --enable-standalone-module --disable-mlogc
  o% \$ A4 H% |, k8 J& V! Emake
7 D3 K0 R% x% ?7 C! K9 ]" q三.nginx添加modsecurity模块
2 J$ E* \" `' L) X% ~* M
' G' u( x. v% a3 H- w8 T  }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 U( L0 M3 g& `/ H4 m* z
" g- `8 I! k4 B) H) d$ E
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine& P6 x" e5 I; s# P5 O
make && make install) ~, p/ _6 t& j/ e2 B6 C
四.添加规则
9 e5 p# Q5 p3 s& d, s1 T0 T, r, j4 B0 a5 i4 S$ B) l
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: H. K+ Z4 y  @' j/ ?: C% o7 O

9 G  s! E2 d* G& D7 P3 ?0 l1.下载OWASP规则:
2 e1 c+ ], Y4 [* P# V
# R( `% @  m! Q, T# {7 Y4 {' M+ wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 V2 K# {+ s1 W

9 ~5 V' @3 x4 c1 e" j. E7 q1 u8 ?6 xmv owasp-modsecurity-crs /opt/tengine/conf/
- m+ Z3 z8 }; O$ Y0 y/ Z7 }3 D/ Z
! c+ u8 L, X: ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 @5 G8 D' f3 o' x2 C! f( s: K
2.启用OWASP规则:5 i" y6 Y' T& U* N# ^# C/ K

4 J5 S+ k* R: ]" a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* p& V( d, }+ z- O) u+ [8 n
* l3 b! o. c# V& t0 |! d编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ S. e3 v2 ~$ m
1 c  O% M  k7 k/ h3 t+ J/ powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 B% p6 ^0 K& @( L* _9 v
- z5 G& g! @& {0 O. t; v( M" C3 W
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ X2 |& s( ]+ z' t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 u5 O: ^0 U% uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 q, M. w: ~9 `( NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' q6 }0 o# p' @2 L; ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! O0 B7 p+ L  E/ @) Q8 D0 i; oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 \: O" V' E) _
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ n" a) z8 C' ?1 v' n4 l+ I五.配置nginx" \9 G' [$ Z) N7 I; N3 |$ h' w

6 X( }4 f7 K  B1 t在需要启用modsecurity的主机的location下面加入下面两行即可:
' i1 J& x# r* M* N* b! `& D$ C/ _
* f/ I( M) V$ _( o  `: mModSecurityEnabled on;  
* O- n! b6 w- x* GModSecurityConfig modsecurity.conf;5 Y; E; T; G& O' L% ]
下面是两个示例配置,php虚拟主机:6 Q* m; Z* D( O* o
+ U& f, U# h2 J/ P3 |
server {3 |# W% `3 D, ?7 o! V" L6 P
      listen      80;
* U" z/ Y9 `1 `% Z$ Y  [. Y7 u' u      server_name 52os.net www.52os.net;, G& P1 y/ W. k0 K7 l
     
$ u" @+ u1 {. o4 f' V      location ~ \.php$ {; W0 O% U9 I& Q# {1 h
      ModSecurityEnabled on;  5 e& o: A4 [$ a; _
      ModSecurityConfig modsecurity.conf;0 l+ d4 _+ F- |/ U
6 F" z. M. j: R2 `8 o4 R
      root /web/wordpress;
- `- s! i* O3 r8 B% I/ B* z      index index.php index.html index.htm;
: X; ^" J! D+ h5 `0 B  
+ i3 {1 s# w2 H: ~! C9 [8 \      fastcgi_pass   127.0.0.1:9000;. u% l2 p8 v3 L& J! i
      fastcgi_index  index.php;
: c7 {3 c6 a" n      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;! E8 R( V3 z9 X; j6 S
      include        fastcgi_params;4 L0 T' I' d# P- C* w+ c
      }
( I& d# G: n, O  }
' ]7 E# w) `; W( @5 O! nupstream负载均衡:. h( o1 I# c# f/ Z. A! S+ ^! [% S

% X$ }  c$ [/ ]upstream 52os.net {
* r* ~2 l# }; s* v    server 192.168.1.100:8080;
5 z+ m0 T& k7 y9 B1 s3 |    server 192.168.1.101:8080 backup;3 N) ]- c+ E% u( Z, s
}
4 _; ~3 n/ u: v7 N" c
9 c: |9 Y0 Y# Q, N5 aserver {' ?# X  L6 b) a' R& m
listen 80;! e3 S! e6 C; r8 q8 U8 H8 R
server_name 52os.net www.52os.net;5 T' l  S3 f1 k# J1 U4 f

5 p5 x) ~9 p: B# ~" dlocation / {
( ~& }; X9 f& v% F    ModSecurityEnabled on;  % ]& h2 C  @$ A" D' j% N" D
    ModSecurityConfig modsecurity.conf;  * I7 m# i/ }! E) @9 [0 w( p2 i* S
1 O* M; _7 i, J- ?5 @
        proxy_pass http://online;, x. i5 j! Q4 r5 z/ }. o
        proxy_redirect         off;1 g! y' _2 x" u# k: |- D" ^
        proxy_set_header Host $host;
2 H' O, c: M1 c6 y$ }  s0 }7 J        proxy_set_header X-Real-IP $remote_addr;
' l' Y* m" ?" X6 v        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  T- _9 n  p' ^+ j7 m/ h    }
" w: H1 C/ _3 \( D! R( B, A  F}3 m& V3 o4 j  T5 M+ L1 Z8 R4 I/ K2 a
六.测试- A( [- o# q! G9 ~6 b! G& E
' I/ k" y. H# s( M8 g1 e  W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* S" S9 L0 O" |  a) }
" ?& j6 {. K- L  L4 D
<?php
( i& U5 `4 c; c- W! }    phpinfo();   
& J) ^4 w8 l7 F1 x* G$ b?>
- J5 d) A# `- b, @1 m$ I& ^在浏览器中访问:" R8 P9 `$ i' x: p

7 C& H, m2 v$ _% Jhttp://www.52os.net/phpinfo.php?id=1 正常显示。! G& O# _) }4 d  B5 x" R2 G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 u5 ^/ i  W2 X* q+ thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。2 a0 M% b8 S5 P( I# I3 U
说明sql注入和xss已经被过滤了( k) m$ U9 m  |$ ?4 W  r: {; q1 Z
, O0 l  C; C" I
七、安装过程中排错
8 k/ ^- i# h; V. U$ G3 N
" }  @. e9 H8 H  o8 I1.缺少APXS会报错
7 u6 X4 W  r( m) q7 O
6 R* t0 _$ U2 \6 d! X: e3 iconfigure: looking for Apache module support via DSO through APXS
- u: d" V" X/ S) ~: Hconfigure: error: couldn't find APXS  ~" I& y  S9 V% R! y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' B6 M$ z$ \3 b1 r% }/ Q
解决方法:. A8 h  H' D, M

) I: ~( W% ~1 Cyum install httpd-devel
5 }( u& w2 r; d  V, s, `( c0 P5 \. I8 }2.没有pcre3 d- \( i( f8 O* k" n

+ J$ M; w3 ~: q7 B/ U7 D8 qconfigure: *** pcre library not found.
) }6 U+ a5 j* Y0 C6 S. `7 Bconfigure: error: pcre library is required- L2 Y* l  O: w  t( z
解决方法:
9 w% c% g* e1 o5 ?' v, z. \* w' P) U1 W8 H, K+ U: I# s9 z' E
yum install pcre pcre-devel/ M, t' C- p; B' ~2 |2 J
3.没有libxml2! h) _! k; V3 r2 `

, I/ R  e, m' S# C% _2 m0 ^$ H( N' d1 y' u
configure: *** xml library not found.
8 o: X% w# h3 s. }$ e- bconfigure: error: libxml2 is required
, y: M; e* J6 x) q* O解决方法:
5 G. ^3 P6 L+ ]7 G* A0 j: p5 }& V1 r* x9 W) V4 l2 M
yum install  libxml2 libxml2-devel! \6 l' d1 j! }# _- Y  R
4.执行 /opt/tengine/sbin/nginx -m 时有警告* o, Q5 D9 r& {8 Q
- Y) {/ `& t' e7 v6 u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 v* T2 U, [9 Z8 A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, M6 N; e* O. E0 T! n
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" |4 R9 R1 t/ M( Y. P& \! I$ O( i
# B  l, m* U7 |' A. D5 L- K, T4 `0 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: Q  H" x" n# t. g0 c' u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: @1 q# [' z, l4 T/ L- T/ n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) n9 M( B; ]! U$ ?. {& ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. A: {% m. m  `4 p" ?' u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 C( j9 D3 o. o2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 F2 _! G) R) t" y# D解决方法,移除低版本的APR (1.3.9)
- s6 h7 w2 Y  c; P3 n" r, o
: k3 _% x# T7 ?3 h, H3 T! \  Iyum remove apr
- O8 z! A+ d4 u* g4 i% s* F5.Error.log中有: Audit log: Failed to lock global mutex
4 g, Z" Q5 M# u2 B- i) I7 h" s2 g" `2 e  \* L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 x1 P9 S) U" @5 Cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 `& L' g) s4 ~4 y# e2 V
解决方法:" s" _$ I5 m. U3 ~% [- l4 [' J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' Q/ W9 ^; L, H: i0 f9 T: v, O

( m. n9 J+ n- [+ @5 s9 A; H/ KSecAuditLogDirMode 0777# o; u7 W0 Z/ Q' D2 q$ p. O! t
SecAuditLogFileMode 0550% y$ I7 D" O) A- A, j  j5 o
SecAuditLogStorageDir /var/log/modsecurity
- B0 k0 z& b, T1 ASecAuditLogType Concurrent
9 l7 Q. S/ t# E0 j7 E( e7 E; H# V% K/ E7 P参考文章:
5 z# V, c+ }' D: Chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! }$ Q( X  T/ u
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-31 03:03 , Processed in 0.062800 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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