找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12353|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。) o1 v6 T; {# z. e
5 V+ O8 s1 p) \
一.准备工作
) l0 T3 R% [9 F' @* U. W1 ?1 _3 x) i1 g9 `$ U) b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* c8 O5 N8 j8 e  W

6 s5 r3 h8 Q* F) `6 M3 Y7 mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ ]: J  R! H4 p0 |. X
$ R  n* w4 ?' B+ K! t' E  zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 d) \5 d/ Q8 U; ~0 t0 T
+ v" v2 s+ X3 ~; A
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 O1 m9 n+ Z/ V+ [7 Y! H3 S$ d% x7 N
依赖关系:
. ~; u8 X, C9 l' Z' `' D2 y5 T7 k: wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: ~  O3 Y+ q5 \* G, o( \

5 ~' k5 _- H* R5 F3 @) Wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) D' U4 Y* Z- E. u0 A( g) @! Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr& U) @' o9 E  [! ?7 b1 M! a3 T; J

% W: S: q+ z# j) y( myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel; c' G/ T, D4 n8 B
二.启用standalone模块并编译& w% x; R$ A$ W/ |; x

) V5 b7 C; I, Y. s/ @下载modsecurity for nginx 解压,进入解压后目录执行:: v7 _5 a* Q7 r9 z7 m& a! E/ P% t  B2 G

% D- p8 r+ t6 D/ E% f( b./autogen.sh
; \2 B2 M, _" [9 ~./configure --enable-standalone-module --disable-mlogc' Z  y1 G8 `: \  j8 q- y
make 1 v- W" H5 }$ G/ M& H; ^
三.nginx添加modsecurity模块
$ J# w" _0 D: a: b9 ~% S# }: c! K' F
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 K& a- W& O' u0 V, ^" G& |9 l) H# h' n7 y$ k
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 u6 x+ T# I' O  \+ g) `' \
make && make install
4 Q* K- r" e% T+ f8 c( Z四.添加规则
3 a7 H8 t# a7 G% m8 q
* J9 c# f$ L9 ~  ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 T7 t% L2 @8 t& k* x3 J) S; @

8 n6 y1 z; ^/ |/ n: ]8 y1.下载OWASP规则:  {* x' d7 e$ }) [
6 C2 f6 O6 z8 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 D, {$ R5 z! L  B) l8 Z" R) ?& E' Q7 b* R) r7 v) Z. ~7 t6 P1 W9 g' a
mv owasp-modsecurity-crs /opt/tengine/conf/
" ]  x$ E. u0 f  z+ o
3 W% S) c) @: j; m: v. Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 G2 E0 e4 j( u& M1 H2.启用OWASP规则:3 ]1 A9 h" L: a4 F; c
+ F0 V- {# A+ g& U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ @8 y/ ~7 b6 x4 R- Z0 m8 s; `. |! K# A, {) f$ V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! o% C5 h; ^; k& c9 a
" O) W4 ]. z) R, l7 A/ |7 j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 u6 Z8 p  ?- Y( ^& |& I1 b

7 E- [$ Z" j0 n# H) v& E& ZInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) e' K0 d6 K1 N1 m+ gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* H$ }! G# U$ N2 h6 `* ]" k* ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" l7 X0 R2 j8 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 {6 x$ y0 b4 X/ O8 W2 C7 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 s: K/ M3 k% ^4 ~3 R  P: O5 h
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( b& m  h2 O# d* T2 FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 C9 b! z  O4 d, d9 O
五.配置nginx1 Q/ D7 F/ @- P1 i! h9 d: X

, J1 N6 h: g1 k在需要启用modsecurity的主机的location下面加入下面两行即可:4 z1 E3 h0 |% [9 H, @2 Q, f8 o9 k8 H5 Z

5 q* j0 K8 Q0 S- mModSecurityEnabled on;  
+ @7 u# v9 J0 `9 p+ m5 ~ModSecurityConfig modsecurity.conf;! C% _. L2 x5 Y& @% }0 `
下面是两个示例配置,php虚拟主机:
/ Y1 l5 t4 k( a/ c) ]6 b+ L* l/ g+ j- W. }
server {
& C7 i7 U/ b, u0 z! a5 O- {      listen      80;
7 K) q) D9 R; j/ }0 L# m' p- A9 }      server_name 52os.net www.52os.net;! z+ o( J. t& U; Z
     
/ }- {* j( T6 Q4 v" G, X      location ~ \.php$ {
4 g0 Q3 {5 C$ h- C/ g( y      ModSecurityEnabled on;  . F; Y6 X' w# t" W0 r' N
      ModSecurityConfig modsecurity.conf;
' s5 d0 |: L9 l9 y$ a; t- E. _' @4 n9 y. g' N4 S
      root /web/wordpress;* e0 x  Y8 \+ l5 Y' D
      index index.php index.html index.htm;
9 v1 n/ S4 ?8 }- [; D( ?  
3 X( Q* P* B" e! ^# C* T      fastcgi_pass   127.0.0.1:9000;8 K* m$ b7 \$ m# _7 D& a% o- c/ o
      fastcgi_index  index.php;3 M+ A$ e" K: i% i6 K
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ u  Y+ x. J" Q; t' N3 p      include        fastcgi_params;
4 _4 ]4 }# S) D! c5 s( c      }
+ [8 s1 D5 b6 r) U; T7 a  }* k$ @$ I8 K5 w+ Y
upstream负载均衡:# d& k. o6 P) y$ L1 S8 ~

) X& P2 h. d# zupstream 52os.net {& D; y: Q4 D6 z2 H- `% O$ |. l5 ^6 S
    server 192.168.1.100:8080;2 q7 j" t( _2 _8 R5 R; k2 U7 J& Q/ F
    server 192.168.1.101:8080 backup;# _( {1 ?! q* u9 y+ P9 ?. _* D
}
$ ?3 b( l5 {; s1 V, s
  D( e# ]* O1 Tserver {+ L& P# U/ p& e
listen 80;
# c* J" j5 F" E2 Yserver_name 52os.net www.52os.net;
. L( F0 l# U2 a, v/ V+ c$ b$ ]3 k# W+ a: x; G8 H; {
location / {
) j/ ~9 H/ z1 V0 r) d$ x    ModSecurityEnabled on;  3 l$ {) p9 e* J4 f! H- A  |/ k
    ModSecurityConfig modsecurity.conf;  & m% k1 O. N2 o* l! w

/ Y* S; O. ^1 q( f* N2 \        proxy_pass http://online;% O! z5 B/ r/ {, ?
        proxy_redirect         off;
9 ]$ j# ~  R, N9 U5 w        proxy_set_header Host $host;
0 ]/ X' o7 i9 e' n) L& X- t' R        proxy_set_header X-Real-IP $remote_addr;8 w; a( t! A! J; X. d" _
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
8 V# m2 [2 ]) ^* Y    }
7 ~$ j# ^& t% b, O& v5 b}; U7 y, K/ b+ @( X; Q
六.测试
- x& l, J! y. ]) c. d0 i" d, O/ k) E2 e- s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: W9 c$ L* a# ~! {$ S7 Z7 X

1 ]7 ~" D5 O, \+ b/ z9 i<?php/ r6 x8 z; u. @# a5 X' S2 Y
    phpinfo();   
4 ?9 Z+ L  [# ^& W" P?>" p4 d. h, w8 o1 b6 N
在浏览器中访问:
# d( x* B( [; s/ ^% S7 e1 J0 T; S
. ~! N6 s8 ~1 s4 uhttp://www.52os.net/phpinfo.php?id=1 正常显示。
; ~8 c& W# ~/ F* hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。/ @. N3 Q. G1 {) ^  l
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 e  w+ y. B7 h. O
说明sql注入和xss已经被过滤了- y. `# t- X6 K2 E. L0 h4 d

* x, |6 {( S- r七、安装过程中排错
4 U" X; f' ?$ o3 _: Y0 ]# O& w+ P/ H$ s% U) S# o; Q2 {! d. G( Q
1.缺少APXS会报错
6 Z) p7 y: W( Y4 T5 ?9 x9 e& q# o  N5 F0 [% v
configure: looking for Apache module support via DSO through APXS
' G9 D6 u( ^0 X1 dconfigure: error: couldn't find APXS
2 k+ \' p6 f5 Capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. }4 U$ d+ ?% M解决方法:
# g# A9 N: t) t& B2 C5 J
: t3 ?! {5 \) t* k! L) C; Zyum install httpd-devel9 U% s0 u* O7 a
2.没有pcre5 }; g& G; J! M& f1 [& B4 X3 S
" J. T1 l( X9 v/ m& }2 V
configure: *** pcre library not found.( m( F+ h9 c9 c) q( `. b
configure: error: pcre library is required+ X( p/ d4 x; A# S9 F
解决方法:
3 {  F- [  `8 J/ Z& [6 T
9 {% H$ `7 I" ~3 f1 d. F; [yum install pcre pcre-devel
; x2 K2 Z/ b5 V, e" u! A3.没有libxml2
, P( o# k/ ]$ U$ ^; w) E& k3 C. g" b, u7 [) n; L0 R

9 ^* V9 e- ^! _; `9 U; X% Q# nconfigure: *** xml library not found.
% q4 R& F6 [+ Z( t1 V4 C9 mconfigure: error: libxml2 is required
7 ?- T1 C6 t/ s# i9 v5 Q' Q解决方法:( Y0 }: F/ l1 V$ q) U
  v- H% c3 ~+ T- v
yum install  libxml2 libxml2-devel2 @1 c6 B, k) S8 A+ h+ G$ ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告) i1 P- B) k0 o5 I
2 T8 z7 Y  _% |; Z$ d4 P$ S, C, q0 g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' b2 n* x# V" z9 R7 {
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) |- c' t! \; j+ j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 o' p( z# d/ B$ e: e! ^6 D' g# U
  G1 U) F2 r2 U( R; j5 O* f+ h+ |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( d$ ?$ B9 s, v) _9 y& @0 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, O) u3 z; m5 Q1 w. ?7 m2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 X# Q: H) I+ j! n0 T9 J9 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' _4 r. j6 ^2 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 S+ b$ ]& |& j% f2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  J# R0 @2 h6 b. k& V0 u5 }0 u1 `8 y解决方法,移除低版本的APR (1.3.9)
' Z5 W5 r, |6 T9 E
" U" c& o% L2 T7 F% b( Q6 Ayum remove apr  K) ^$ S6 Z6 }0 l
5.Error.log中有: Audit log: Failed to lock global mutex
! j8 g1 _: W5 u# v
1 ~! [/ c4 {# _9 o2 b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 K% U# i! T# \5 Sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. T  e  X0 x; v! o
解决方法:* }* i4 L9 x+ g0 n: G3 ]+ ^/ x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- y3 h8 k( P6 Q& @6 C" L
# A, J/ k0 W3 @( L) D
SecAuditLogDirMode 0777* E# f, r% j% \" M  Z
SecAuditLogFileMode 0550
3 M+ S9 v  p0 ]; V* S. WSecAuditLogStorageDir /var/log/modsecurity
9 E8 k; v# h) H2 CSecAuditLogType Concurrent
* y: j1 S6 }9 J" X% P. Z3 q参考文章:
4 v5 F9 _* e+ z' f7 hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 a% v; G$ t% i6 d& `
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-29 22:14 , Processed in 0.072298 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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