找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12043|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。& f* e2 j$ H* g9 |+ B
$ y! i: ^8 ^6 @8 M! ~
一.准备工作
2 l$ c* A1 s" x8 n, v) T! N
- C+ t- I: `! v/ x0 e4 \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 h" S5 R2 q% i# d

# @/ |: @4 x; _. }% w% Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: z  S* m9 M0 B" c  C
  B; f0 j) [2 J! Y& ~  R& g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: x3 d  g% Q. K: P" F6 Y! c" M

: N3 E& r$ y4 w# tOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) h+ _) t# A# z; [& e
2 o3 g& W5 Q6 C
依赖关系:* d9 p& a9 P! v. U# ]7 J- G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! m& u2 Q% b% Q* _8 ~0 ]: G; G, {' c1 L0 J2 T
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel# a! y8 I+ H, x. _7 Q4 i+ [+ M
modsecurty依赖的包:pcre httpd-devel libxml2 apr- b) K; w1 n9 ]9 _

' ^4 u% H7 e% l+ p5 D8 d+ ?# vyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& I% @* g% R% Y, @+ [, _4 ~8 H* B9 G二.启用standalone模块并编译* N- J6 [1 ]3 j( d

0 q, h6 u5 u, z* D# s下载modsecurity for nginx 解压,进入解压后目录执行:" d9 e1 N( N3 T  n7 U7 T

3 u, m7 @. n) O* W' D+ s) S) H. \./autogen.sh3 f/ R; f! C. p
./configure --enable-standalone-module --disable-mlogc
4 y# a+ B/ _8 wmake
6 r6 z, n! Z# x  H1 F三.nginx添加modsecurity模块, r; m0 m: o7 l* y8 R/ v! C
' W) \% \9 n/ V5 ]- e" A
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 |0 n9 B( `9 k; X$ x1 a+ f9 M0 J& y0 Y" F& K  n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) `; A+ V! H& q5 t% b
make && make install
5 y- p/ N1 }- E" R3 c6 n8 q: S# i四.添加规则
1 A5 {1 _- }6 X# {
. _; V4 G" K; z9 y, amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ q: i2 T" E% N

3 j% X7 E% |: z+ E5 k, ?8 S4 R* V1.下载OWASP规则:
6 u: x! R% k; Z! U3 `& u/ U, e0 i/ S, X! d" y- K1 p: u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ A, h* R# l3 L& S- Y/ v9 \6 G1 U0 @# G: L2 q+ x' v" m4 g8 U
mv owasp-modsecurity-crs /opt/tengine/conf/0 _7 w6 F. ?; E+ \! B( g) G5 ?

$ |7 O, V( G3 n" C# Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 l: ?& R( @5 b3 N& I' ?2.启用OWASP规则:
# J3 l) Y' m6 i0 ]- g6 `' x# p. [& P  N" q$ {  Y/ f9 O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) m1 `, H1 ~2 B/ |$ v

) G- S9 j- Q: ?) a2 P: {* F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 Q. }& b& [' r

5 \" A0 S! Q8 R. Z7 Y' Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 X. G& V. r" X) c; i+ |3 x
3 }5 F( A/ K9 ^3 T7 N% |
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! O, a# d9 i* e8 uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 u; {! {# O: k  T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' l1 j% l  d3 P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' y: d+ j6 f- \" UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( C" H) l- r! }& v( P4 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 x5 ]+ w1 x8 k/ H2 ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% j+ q( y  B, ~9 n* s
五.配置nginx
* o0 y" c( a. ?5 n( ]; M$ s2 h1 O, H) m7 i9 U! q
在需要启用modsecurity的主机的location下面加入下面两行即可:* o  M/ T% ?- h# g& W/ z

# O" p* `, N& T; R3 D0 g( w. LModSecurityEnabled on;  ) Z# J$ J/ z9 C
ModSecurityConfig modsecurity.conf;. K1 N7 t, B' r1 L# Y; X2 a' Z3 S
下面是两个示例配置,php虚拟主机:
) f- y1 G: y# I0 h# `& B3 f  g) @8 U/ a) o5 z% M2 G) r9 c
server {) p5 j. t; y3 `, p1 b. _
      listen      80;
7 }4 _0 |' r- `; Z5 C2 i( ]      server_name 52os.net www.52os.net;& K" e2 G1 D4 c( Z  O; ~( h- r
     
% o1 S4 G$ R: V3 `& \- m- D8 H  \+ Y      location ~ \.php$ {
3 a/ I6 ]9 P9 A3 c6 @      ModSecurityEnabled on;  0 @6 J0 z( l. F; W! u+ r
      ModSecurityConfig modsecurity.conf;3 |7 q4 T1 C1 |" K- r& |
& c8 s) U8 d9 g( g0 \* _
      root /web/wordpress;
7 k. E" s/ D8 b, }/ E$ d% Y      index index.php index.html index.htm;$ O: e& B/ f1 H
  7 e3 I1 E5 b7 H
      fastcgi_pass   127.0.0.1:9000;
- `- N2 S: K, Q9 K8 |* W      fastcgi_index  index.php;
6 z9 x: X5 i! j+ w& a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
5 W3 T4 ^2 v: z5 ~4 z5 M; K5 S      include        fastcgi_params;! z' _8 @* p* i$ [$ X1 _( r0 {# M
      }$ J% Z( V* p# s" `* y6 O
  }( ~1 f: _6 R2 F
upstream负载均衡:
8 C7 I) O: e3 o& [3 {5 ]3 n* w5 s/ J; j0 l
upstream 52os.net {
8 l; X$ @; [8 M# G: J5 P8 [. L  U3 i    server 192.168.1.100:8080;
& |' |" H$ H- F5 ~( ?# j. o    server 192.168.1.101:8080 backup;) B. P6 x) }' R& U# T" T
}( U+ b: `1 X+ v1 W
! A8 Y3 w* k/ ?
server {  }6 R$ H0 V, j) O' O
listen 80;0 r' k+ u, R* s
server_name 52os.net www.52os.net;# b( o$ p' h9 e% W, r7 ]# V  X

' _0 a6 t  D4 zlocation / {' A  x6 t( ^/ e+ L) |
    ModSecurityEnabled on;  4 I9 I) _) E7 p: K8 G+ Q9 T2 ~3 K
    ModSecurityConfig modsecurity.conf;  
7 i$ S$ C( e+ W0 N! F# r6 E( O* Z, J, s* |/ z7 r
        proxy_pass http://online;- `4 A& h/ Y. \# O
        proxy_redirect         off;
% |! |: ]0 m, y2 E, P        proxy_set_header Host $host;- C& }. ]1 l) r+ ^& M
        proxy_set_header X-Real-IP $remote_addr;5 P) u) y9 }9 U5 k4 q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
' b& {0 H: G4 @$ G) E. i5 C* Z    }
- D1 t0 W' Y) n}
, z5 S( x7 ^8 L: c8 Q0 }+ w六.测试
: C* w4 h4 P6 k8 n, l4 F. s# J3 E$ [9 p/ S  c5 L) O+ J
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: c& ^! P( X4 h9 b/ Y1 u

& L4 W) @, Q$ c' ~( q; y<?php
& A  T' F7 l1 S# V# K2 k    phpinfo();    : d8 o8 O# `0 Z, t( x
?>
9 ~+ X- a& I* d& |0 n! K在浏览器中访问:3 k! j; R: M! r; _! T

( }8 ~! g2 E* K( T4 e0 vhttp://www.52os.net/phpinfo.php?id=1 正常显示。/ {, G; c* M- ?9 P: v
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
0 v3 @0 Q0 j2 \4 s. o2 ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 _6 G' Z2 z/ ]6 m' Q, X; u0 W说明sql注入和xss已经被过滤了
$ {1 g  M% s4 p
& i4 U, M: Z9 q七、安装过程中排错, p1 r: w& [5 N7 H  u
/ J) I: g; m+ Z: ]8 `$ h
1.缺少APXS会报错
9 ]/ y- O1 R2 v) V2 Z6 w6 t  c! z5 @( \0 C5 q  j$ @% L- X
configure: looking for Apache module support via DSO through APXS7 {  a: p* @! s* d+ Z
configure: error: couldn't find APXS
1 d, v' t! j! Qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。9 m; d. p. T, J" W$ Z5 F
解决方法:2 s# r6 ^2 b7 ]0 G# A
% H' f8 x, k3 h* p( Q( _
yum install httpd-devel1 M9 X+ o* q4 w, j6 {5 N5 q' w* w
2.没有pcre
5 O# R1 h% G* h* i+ D" }: S, s2 y7 c
( p2 K2 E5 @, p$ |configure: *** pcre library not found.2 j' O% Q# o/ P/ I6 I9 n
configure: error: pcre library is required& a. }( {) T$ b2 M5 J1 O% ?
解决方法:% t8 G3 g" r9 J% t5 H# Z9 [% c7 ?
% _! [' b' \7 L3 {. z: Y+ I" W
yum install pcre pcre-devel( f1 w, u) I+ X
3.没有libxml2  j  p' F. E( P
/ B; Y. M" q7 R  z! e7 R
7 F: s* i  o# `# W  `( j
configure: *** xml library not found.0 Y! d0 M" t7 k
configure: error: libxml2 is required
7 M' H6 c1 G& B; e解决方法:
6 d% V1 F' h+ y; u* ?
! O! ]7 ?' v# o$ a. M2 R0 syum install  libxml2 libxml2-devel- i5 }- v, S. y1 P
4.执行 /opt/tengine/sbin/nginx -m 时有警告# \, ~, r9 ]& C( Y

4 }! w3 c6 v. M- s0 QTengine version: Tengine/2.1.0 (nginx/1.6.2)& V3 q' [7 L. I- B; g
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 m  r, z1 `# d. `/ }9 z7 c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 z! r# s) d' `' c- v8 B, F7 C- T( q4 Q& r# I! ~4 t7 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 W+ ?. f" O' |+ Q/ L. n/ t' Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! X) k$ G' k; R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& y; o! X1 B$ k3 e/ K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( Q# ?- H3 ~, c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 a. j, l: F1 ?& f6 x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- U& x6 b4 m7 F解决方法,移除低版本的APR (1.3.9)
/ |" q, i0 X2 V* }* R/ y5 G9 Y8 s% ?- }( c; K6 J& e$ V
yum remove apr
2 `  \) x7 e2 x/ L5.Error.log中有: Audit log: Failed to lock global mutex9 k' O) q- n  g, S/ Q2 h4 N( V

2 ?: t. T: e% D& b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 a, S* E, U6 o2 j$ y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# p7 V5 Y# Q. c5 c$ X- K- @
解决方法:4 a$ O% n2 Q6 s2 X) M/ r
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" @) q2 v" C% a* ]; m
5 T( h/ J6 S+ F
SecAuditLogDirMode 0777
5 _" I# U2 g2 `! QSecAuditLogFileMode 0550, `! k6 a2 Z4 r2 ~% C
SecAuditLogStorageDir /var/log/modsecurity% y2 H. H, G- b5 x0 a
SecAuditLogType Concurrent
9 S, R( h* T3 ^. N/ D3 K参考文章:# W, @" Y! M# u# s/ y( Z. Z/ L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 z+ \  g3 R* p5 |
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-5 19:45 , Processed in 0.068212 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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