找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12797|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 d7 i3 r$ ~1 C3 Y* T: D; S4 g) Q; g6 e7 F3 |4 {* n
一.准备工作
8 q: w' ]) O: A) d) a$ L7 e: i' Q% Y, F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 Q* x5 h9 M) i0 `
# f6 C3 \3 W! H# k5 B% utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# n/ X5 f1 W7 a3 W% _5 w1 b
7 Z' z8 @' k- P; W# J  Zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: u: E1 {; b2 a6 u; L. U' K) `

* W) Z$ t( q+ A" e4 hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, H/ ?6 [, G' k: o. \
! i" P& b/ D1 i* c; f8 v. Z依赖关系:
/ K  u6 W' T" K! e6 u% Wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  L9 O( ]+ Y6 Y3 C+ ^8 H, H% h6 O5 C' I! i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel6 M6 k. F6 s6 ^1 o" y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( M2 J# C$ ]: L8 g  K9 R& ^2 I' i8 A7 P, p9 o- P+ A
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( ]+ @+ L! M+ k2 U二.启用standalone模块并编译' @$ \% F$ k2 K3 k. T# Q  \
$ e+ r4 B8 B9 b1 h
下载modsecurity for nginx 解压,进入解压后目录执行:
* G' ?" s* j5 }, G; i4 h* s$ z% d! ?; v) Q: ^3 Z2 X0 k) m
./autogen.sh
+ I& n% V% }% H$ H# o4 s./configure --enable-standalone-module --disable-mlogc
& W" d; N' s( J( Q5 S1 jmake ( @8 P: C1 {2 B# [, a1 H- M
三.nginx添加modsecurity模块! a, A6 Q3 f; W" b3 u. E

7 Q& ^" p) Y  D2 ?在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 [. q" K. o# o; \
# y# q; @5 P& B; Y7 p) Y% ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 z  E) ^* w+ L4 }
make && make install
1 B  X' T% b$ l/ Q" L' N0 }四.添加规则& W4 n9 }( m: j

( u1 K2 a6 W) {; h3 U1 Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ v' L' r# P3 [  H% L% l, Z0 O; s$ |5 s" A
1.下载OWASP规则:
# P2 c, C- }+ j6 z0 O4 a
3 y# H) ]# u8 d1 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs" Y" t( F1 x1 ]" g3 r

7 O+ h4 T7 |) {9 @, smv owasp-modsecurity-crs /opt/tengine/conf/& X1 B& S2 [4 x/ R% {, |) e
5 z2 a3 O7 K$ }9 l, c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 p- f/ M$ }& E$ |
2.启用OWASP规则:
  ?& k4 H( P5 |4 O7 E" h  G$ C$ M0 e6 Z6 ?' k1 U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% j8 W$ c" n- D; f) `5 H  h' s5 I3 T* [2 P
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& ^+ t/ j2 E2 l. u" F+ y; j9 a; F! T5 x3 i& j7 t" I
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 A8 w1 j# u) W* a
* Z8 Q4 |! q" y1 d( |) D
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 G8 X4 L9 L2 W: J5 y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 z' L7 ?2 {3 [" c; W2 G  ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 c8 }) M) g9 p) MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( i0 A1 L9 E, p2 V8 O# ^* n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. r/ B/ m# s, B/ J7 w. `' V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 Z6 P, O5 H6 F. h# v. sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, b+ i" r* Q0 l0 U* v* B; y
五.配置nginx0 Y& T' g+ e6 g# c; e
! Q; D6 J! o- t2 {1 U5 I& K1 `
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 k$ L. V6 S- a' W( D8 Y+ Z. C, M$ [; ^5 }( s1 o& v/ M3 u  B
ModSecurityEnabled on;  
6 K& j/ `8 X9 y* e# q3 f. LModSecurityConfig modsecurity.conf;
. p, k3 G  r0 v* F4 ?下面是两个示例配置,php虚拟主机:5 w3 F- Y1 z% ^; z
: W- l/ P- t  C8 v
server {$ i" T+ }8 r' }  W* L4 ]
      listen      80;
$ n* _- g: z& G3 V& S      server_name 52os.net www.52os.net;1 u) ^# j/ k6 i: Z
     5 D$ U1 c' S1 v# o; F& e
      location ~ \.php$ {7 {% L; Y( M. u& F0 r2 h
      ModSecurityEnabled on;  
& L5 V2 t( ]6 L4 D% A- _+ o      ModSecurityConfig modsecurity.conf;
- d$ F* S  `+ ?6 a) a0 W; s5 X" a; J) N( w
      root /web/wordpress;- _% G# o+ Z4 A5 ]! K5 [
      index index.php index.html index.htm;
) o6 s" x1 X; D; W$ p: T' i  4 m4 c1 L6 ]; |3 ^# _; R$ V/ H9 N
      fastcgi_pass   127.0.0.1:9000;8 E* h8 d% Z1 c6 t3 S  Y
      fastcgi_index  index.php;
# c' |  S. u2 {  |$ t) h6 I      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
* [4 ?  R. O  @( k7 l      include        fastcgi_params;
: o$ W8 L6 T1 K4 g1 t8 I9 n      }% o+ I2 E3 W$ `. Z
  }
4 ^; h! v+ e& l8 M# k1 B2 r9 Nupstream负载均衡:2 t) i1 V3 y) S

! W7 j7 z$ g# P! `, R' a( \upstream 52os.net {
! Z: W0 O4 f3 W, d" X. ?    server 192.168.1.100:8080;
" _# ~4 F- w% U8 P: b. t    server 192.168.1.101:8080 backup;
1 m, D6 q0 s3 k: O' `3 m; n}$ X) \% X0 m2 H

3 z* L( A# o' L: m; v' Kserver {
7 {+ a7 Y7 W0 w, V4 v0 dlisten 80;0 b# X0 Q& @! Y5 L0 S3 M; r
server_name 52os.net www.52os.net;
+ {; F6 [# c; |  l9 `) W% a* g$ x# [& o0 k
location / {
: S4 `" i3 d; ~0 d, i    ModSecurityEnabled on;  3 w" j2 ~: k& ~6 \/ M
    ModSecurityConfig modsecurity.conf;  0 b7 P+ v. s) ?- |. O
( l* `' \  K8 H
        proxy_pass http://online;
" }  q6 @8 ]# G- f* ~        proxy_redirect         off;
5 j* s# [: S5 s3 f# Q6 K0 _        proxy_set_header Host $host;# t- g9 g( P; x0 X
        proxy_set_header X-Real-IP $remote_addr;# I, i0 t( j; a( A, T, F
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: n% n" J6 c; g) K    }
( m0 d) z8 z0 ~}# W! j7 r0 q; ]+ y& Q% D
六.测试% O; q4 i% a) O+ z& @6 H& d
& K! h* C3 \4 M+ K) U2 D
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 T" Z: O6 }; v; i
: x5 m" S; T5 b( D9 W; p& E2 f+ t
<?php- b6 f' d+ P5 ?" l  x
    phpinfo();   
' O! R  ]" \& A5 \  ]: F?>2 C+ ^! G6 c! G7 h) T' e- `. K
在浏览器中访问:
# j$ ?# E% c2 Y
! J0 r5 @8 e6 L, e5 Ahttp://www.52os.net/phpinfo.php?id=1 正常显示。, \: _5 n. U" B& R
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 ~  t2 E- ^, h/ U- B- X  ^
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 e; F( Q+ i2 r8 z/ _说明sql注入和xss已经被过滤了
1 u+ Y* C  x2 m# u
3 S/ S7 ?) |: f+ L七、安装过程中排错
/ k5 J% P1 m  q0 d' @" j7 K! ^! z( m$ _0 c9 }
1.缺少APXS会报错
, r' [& v9 T7 @: q* ]& ^7 `/ a5 I
0 h2 A3 m0 h& j9 i# U; gconfigure: looking for Apache module support via DSO through APXS
+ |* G% ]( n, i+ S) a+ Xconfigure: error: couldn't find APXS
( J8 \# E9 z& Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, s* k; P% s0 q. K! t8 g解决方法:8 Z( }4 }5 \; T6 y

; q4 U& H4 F1 B! F) d8 Myum install httpd-devel
0 M8 Y, B- X# R$ P+ [( G2.没有pcre9 H, i: B+ B. o6 e: k/ J2 `3 i4 _

: J8 e9 o% b/ M. S6 K% L! B% d& Aconfigure: *** pcre library not found.( ~6 l' J: i) d9 q3 F" W0 U
configure: error: pcre library is required
) j8 M/ x3 C+ f" n  C, j解决方法:
( h. n' X/ h* i( }1 s& o) Y
. g7 q* h9 y1 f9 ?% u4 Y! byum install pcre pcre-devel$ B/ |8 `  C$ L# _8 l
3.没有libxml2$ c: [5 s. V0 D2 a* `

% m6 l1 f9 p' _0 `! [( R8 m0 z  ]% O
configure: *** xml library not found.: Y4 @7 N  N$ q: A4 Z) D
configure: error: libxml2 is required
9 Z) c5 _, _7 Z2 a  l( Y% ^/ J: K解决方法:
: l3 ~5 x) c% ~! r/ R5 r7 F& W! v: p' h
yum install  libxml2 libxml2-devel
- ]4 p( K) t& y! j, _# ?- @" {4.执行 /opt/tengine/sbin/nginx -m 时有警告. R# @1 q3 k9 f& [
* I6 `; p, q. P4 O# n
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, _" c; @5 a* f+ fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!: q4 c3 ], e$ N8 a( k( V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 r1 g: P$ C9 d" g4 I" \' }
. \1 I! F, Y) N, s" G" g" {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 K/ `% c& |: L+ }& H5 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 S) b: ^& J3 o3 L* o2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! n/ a# F2 \$ O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 F4 ?3 s: N1 f0 Z  _4 X  H* y+ t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) n' G4 G* K& E. [  U0 u/ V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- A; {4 h& Z9 i解决方法,移除低版本的APR (1.3.9)+ |4 C& k0 c4 U( @

8 v8 g" P& Q% a, l2 Pyum remove apr) U) `* O' i) D, V3 U' H' F: ]* \+ q9 @
5.Error.log中有: Audit log: Failed to lock global mutex* c% k+ X5 X8 z4 {/ ]
8 g+ w  _$ c# t! B
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
: v& |' A& `. J; c2 u3 K- ?global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& L# z0 D% |" m+ I  h解决方法:) s1 [$ y9 Z/ q, t
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# K4 ~1 w6 x# v" @0 H5 v; P  }+ V
SecAuditLogDirMode 0777
9 S0 v  d1 t* V$ v  iSecAuditLogFileMode 0550
+ O& J( b0 P9 ?; \6 }8 P! hSecAuditLogStorageDir /var/log/modsecurity
* h' Y3 ]" T8 j5 cSecAuditLogType Concurrent: j- s8 b% X0 p: p
参考文章:; _! U9 F1 D1 w2 I; i
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! z! z4 [! S% `' S' [# e0 c
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-12 14:26 , Processed in 0.070169 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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