找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10957|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; k+ k% x0 \$ d. Y  ^
# n  z5 X# I' e" a# U' w  v
一.准备工作
# d: _6 O- t/ ]0 G5 O, R) p5 J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 o* `7 {0 d* J2 M  S; s% V- u" W9 K  S& z' N  ]/ c; Q3 U0 }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 q3 R" E4 t5 C& [6 ~# T
7 N9 B! p" @1 y: t7 p+ Nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; t/ R5 L* d% t( u+ m6 G; R& v3 J+ b+ ^5 n/ J4 u& F6 b! h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& b+ j, X/ c5 P( i0 a
  _% J" `8 X# C: n. @, M依赖关系:
4 K- x6 {" I7 t* ?* @, Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ b  Q; m8 F  x" F
4 m7 K0 X. `2 |6 A2 Dyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 t- v! l$ S$ z: m) fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. I, ?" H* Y& c' X2 |( A0 p9 }+ a+ N" ]' T, A
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
0 t2 U( Q" U. s! m: @9 N& p" m二.启用standalone模块并编译' [3 d! `& G/ a' J: \3 s9 B

( v9 W0 [* O5 Z3 Z! q5 s下载modsecurity for nginx 解压,进入解压后目录执行:7 k+ P" @! Z  h6 F4 A

/ Q/ ]. i. V7 S: p& t5 O./autogen.sh
' O6 }) h, y  e; V* i+ [4 Q./configure --enable-standalone-module --disable-mlogc0 S* W% u; W* a6 Q; D/ d/ u  Q
make 6 ~9 q' S; W. d% L) C$ n
三.nginx添加modsecurity模块
6 Y, c7 c! X) h9 s; W& Z9 |% f9 F) y6 I$ `% k' X: N8 _/ K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 O) s& [7 I( ^& b
4 m% H8 }8 K) O% B5 n/ t0 \* j; X./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
: o9 E+ W/ H" gmake && make install5 X! _" @& r0 r# G$ Y
四.添加规则
; F6 ]0 c5 @- ?% ~) t
; |6 F5 g. ?; Z/ G9 B4 t9 P- ^7 Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 [& F8 B, X- H, j# }* n$ z/ {; \
1.下载OWASP规则:
& ^4 t: y4 L" d. V6 a8 F! k3 W9 N  W' w' n6 [6 o" L' Q( I2 O/ J; N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 w3 m' b8 F' X
' d! g  C8 [- v+ H1 ]
mv owasp-modsecurity-crs /opt/tengine/conf/
8 }& i$ S! {9 S+ K: Y7 A1 @* ]3 N! {4 ]7 m: e( }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ o8 a" @1 y5 H+ I2.启用OWASP规则:& [' [) J. M3 E2 {* C) C! v/ m: Y2 Q" o

& p7 |, _" `# T2 B1 P$ }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% x. q) g; K0 s' U  Q( W+ m

; z8 F; P, A$ T$ S/ m编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& Q% }4 J/ _. x2 [4 Y, m8 {" n2 p  D

, e; M5 w9 X* C! {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. E' _7 d2 f! K( _/ d. Q/ n: }
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 v+ N" u/ x4 B- \0 ^. W# yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 G; a) q) U# f) Q1 ]5 a7 e2 c8 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* J4 g; ~$ [# h- @2 JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 Q* G7 P3 p- a+ f" j  cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. [& b) f  h  e6 ]% A+ n- @& o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' i* n3 `  D! P! `
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& c- h5 p/ D/ Z* u五.配置nginx! d; l% N! f# o6 f  V4 f$ f3 ^. s
. @3 c6 x0 Q4 g) d2 p# O$ n  ^& d
在需要启用modsecurity的主机的location下面加入下面两行即可:# {; e! S0 ^! T$ P, ^7 Y  W2 G

  X7 A4 X% W4 Q4 ?ModSecurityEnabled on;  
5 F/ C" X% m( H8 b0 g% zModSecurityConfig modsecurity.conf;
: D9 Z! j& K  [下面是两个示例配置,php虚拟主机:
' M: s- B  i6 j# L0 t
/ ^" V6 X8 h: X# ^& ?$ O+ r; a5 Dserver {
- W, G) u+ k2 ]      listen      80;
1 j4 D5 s$ S6 S      server_name 52os.net www.52os.net;9 B. k) ?4 q8 Q
     9 B1 H, V- g: e! t/ }
      location ~ \.php$ {
+ F+ f# P: ]3 g' R% w, v8 Y      ModSecurityEnabled on;  
7 U+ i, ]& o+ R2 g9 E3 E      ModSecurityConfig modsecurity.conf;6 |; c" [/ t- I2 x
5 r& Q0 w/ y+ p
      root /web/wordpress;& q" r5 T* z6 B. [- F& t
      index index.php index.html index.htm;) W4 [. b& n( O( m
  
6 \+ o0 S: H( S      fastcgi_pass   127.0.0.1:9000;
  @0 ~3 U& d. g0 ^& k+ o+ }      fastcgi_index  index.php;2 d! K) `6 M; |3 T. u% c( v( M- f
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 g! l8 A; Q4 I; Y3 {0 \8 ~: _
      include        fastcgi_params;
  Y0 s' Y" E# n- u& i9 b7 V) \      }
( G% u) X. A, s8 U3 S  }5 Q1 A8 a; m) {( e+ K
upstream负载均衡:
1 ]3 Q$ h2 ~+ y# ?; D+ _  F; ~8 U$ E* {" L  j, H5 y
upstream 52os.net {
" }& D4 {, D: W- c  G) u    server 192.168.1.100:8080;2 L: Z0 V; f7 k
    server 192.168.1.101:8080 backup;7 C6 p3 x0 R( t) a# o
}
8 _* W* X% |2 v5 |
0 x' Z+ k* u: H1 Jserver {. h. |- i6 f( K3 c4 _: i
listen 80;
3 X* q; K  W9 v6 p4 |# B; Xserver_name 52os.net www.52os.net;
$ S  ]  Q/ p! [/ Y
' O3 }2 s/ s& U8 }4 f/ zlocation / {
7 S7 [% Q0 w% Q* x& g& {, p    ModSecurityEnabled on;  9 l, g" Y' Q' k6 n+ \, v; k
    ModSecurityConfig modsecurity.conf;  
1 ~) T% c  G9 \# S. N' [  @8 c
        proxy_pass http://online;
' x" J4 r( Z6 W7 X; R1 s8 c$ u( |        proxy_redirect         off;" k1 n3 b5 ?% C8 s8 J$ v
        proxy_set_header Host $host;, J% I, Y& s2 _3 U
        proxy_set_header X-Real-IP $remote_addr;/ w3 }# U, E  ]# V/ S. W
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) T+ E; S; a: a) ?- g! B    }& R9 o* `6 k$ ^% {
}/ k7 r0 _3 [  Z
六.测试
' q; `7 n  f8 }; x+ Z. n0 D& j/ `2 L1 Z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 T  ~/ V  j+ c. x" C( c+ f' m) E4 y) v5 m
<?php5 w+ P6 ?8 a5 t
    phpinfo();    5 O, t3 {$ P2 e$ @0 Z
?>
  C, f, }7 F, K& {* {在浏览器中访问:
, V( C) x  p* \: G3 P( E/ c* t  R! w6 n4 C7 _. [4 c
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 {+ }$ m( G5 ^( vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
9 m% @1 y! T* V3 c; B. q6 uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! x: }* u, A' Y4 Q
说明sql注入和xss已经被过滤了
( j2 Y$ y9 a- A1 D
, Q: }3 H$ g: i2 O, u七、安装过程中排错6 E; }; B) Q$ j1 `

! |* P8 R; o( X# D' m8 @8 o! J1.缺少APXS会报错
* r" c- c+ z' s& e" q6 E: x
! U6 f2 U+ `# F  j/ R3 Qconfigure: looking for Apache module support via DSO through APXS4 I+ ]/ j. j9 i4 n) B6 j
configure: error: couldn't find APXS
! f! }8 \% D  e6 X( ~) Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% Q- c7 [) p& _- a0 Q2 @! @解决方法:  [( D. g. O4 D/ r$ s* k
) {0 k( q' I5 Y1 ]3 x  y
yum install httpd-devel+ M' i$ q# ~* l5 R6 b
2.没有pcre
* g& T* N. g/ I9 y
5 f' X! F2 R8 e% k" L: oconfigure: *** pcre library not found.
7 ?% c+ W7 }2 p) F# g0 F; Iconfigure: error: pcre library is required
( m9 W9 F- z  b8 u解决方法:% }( r6 y" S7 P* W7 ]9 O& U1 Y6 }9 Y2 D

7 z) y/ Z6 H; d: m* U* vyum install pcre pcre-devel* T& g% ?7 d+ v) z
3.没有libxml2
. V- V5 t- o' `% `7 X: F
* Z& Q8 x& l9 {7 M& D( q. r/ u- q( k) m( A6 \) L5 \1 b
configure: *** xml library not found.7 _+ N# _; H0 W- d
configure: error: libxml2 is required
* t- L; t; V! g# d# [, ~( i解决方法:- F: C; k3 a3 U3 I" p

! ?* _/ B" `2 \yum install  libxml2 libxml2-devel- S" ?- O0 w' h8 E+ `. U
4.执行 /opt/tengine/sbin/nginx -m 时有警告' B0 {' P) {3 a% c

( k/ O- g1 I% b. I8 yTengine version: Tengine/2.1.0 (nginx/1.6.2)
) ]/ [! p% ?3 G1 {nginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 Y. j6 K% F) F6 ]6 u0 ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log  [$ }% ]5 J4 L/ X  d

- y& ^4 \2 V  H  _0 p" L: m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 ?8 }+ i5 W( d) H' R& f' J5 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 D! m: V# t2 ^: t* ~/ a6 l
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' z" g) v& Y# Z6 X, V) i4 v" [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' B$ O7 g& [2 \. @3 v+ |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# A4 W! A2 s2 u& I* k- ?; ?2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! ?" c3 u1 a8 O- ~% [( w- X5 A! G) J
解决方法,移除低版本的APR (1.3.9)  w* I* I9 k' ?3 N% t; `1 S

2 A6 n7 C- D1 h& j8 r- \8 C9 Oyum remove apr
' q9 T3 H( G/ m9 P2 }7 v5.Error.log中有: Audit log: Failed to lock global mutex0 z# I: Z$ p; n- e, B

* e( x* ^0 r0 X9 x( N! r2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ) F9 A* d# X* @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( d/ p0 w& V$ |. V9 S) ?. \
解决方法:/ t1 L0 l2 q/ Y# M' Z1 B1 F8 S0 x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# j) b: E; [& A4 K. u) w
2 N7 m3 K9 J0 l: I  l
SecAuditLogDirMode 07770 ~8 n5 Z% x, b" [. z- l
SecAuditLogFileMode 05505 q/ s0 a) I% U9 J& L0 N. ?
SecAuditLogStorageDir /var/log/modsecurity& e8 y  [& u; s- ?
SecAuditLogType Concurrent
" q) i; U! [+ _8 B# I参考文章:* n1 u' J( h. P2 w: O; R; O
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! r. Z/ N% `7 ^: D
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-8 18:20 , Processed in 0.070458 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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