找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9217|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 s8 i0 r+ S$ n! k, s6 ~  @

) N% b7 h. ]) B  I8 [. g一.准备工作; }2 a1 T5 B" u% ]) B0 R

6 r: H: H) w1 c# s: X; g! u( i系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 g! y% P0 q( u" E# B

+ z4 a2 Z+ {) \( L# Q5 \; `5 j/ |tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( ?' S8 a0 L! Z; N1 I2 x
/ B) U" K) {# ?: O* k' vmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 O* E( b8 p% t. ?% }7 V: a; y& ]3 r- ]3 K
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. G  c3 r0 C5 L9 h6 W, [7 B% l' g; h& P/ C# Q: e) b. F* D
依赖关系:
  z+ o: g5 F3 a5 K& ~; Q& btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% n2 O; x4 S; W8 l7 B+ n9 G6 k8 G

6 |1 |$ r8 g# C8 H7 C, y. k( Gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- F2 O. ]8 P' M6 s2 Amodsecurty依赖的包:pcre httpd-devel libxml2 apr. Y/ W/ t0 U# M3 r" F( c% g. q5 q

# }& g8 A. w. Gyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 z$ g3 f( `4 ?二.启用standalone模块并编译
4 R. N5 `$ e! y# z* {0 Y% n- i" Y( z% ?9 @" D. y1 [
下载modsecurity for nginx 解压,进入解压后目录执行:! p3 q5 g6 }8 c* j& |* N
; h# A1 Y* Q  \  ]
./autogen.sh' W; s: A  h8 e, A% G
./configure --enable-standalone-module --disable-mlogc
: F* O+ w3 d% a8 o; w+ k; u2 e2 Jmake
8 [( }- J* a9 m4 c! \7 |8 M三.nginx添加modsecurity模块
  a- c( G! W. ]( l& h6 v6 W! c+ V/ v, Z/ _: G( h9 K9 s% P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! L- d$ l3 \3 S1 n
1 x! Z3 ?# |/ P3 b4 A./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! U' o9 ?& A) V4 [make && make install
; o3 i/ I  |2 w. F四.添加规则6 l8 @- p, D% c6 g( r2 M

+ o: b0 q8 |( p+ b0 x1 L$ c0 m. Mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 U; a* w! b! d% M- S
( P' L7 j; x& y9 f. x) s( L' Y1.下载OWASP规则:
7 p) W. p6 H" d
( N+ v# s3 V6 \- N) cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, L$ Q7 e1 u1 \% r/ X

5 [: V$ N3 }: a+ I9 X; L# hmv owasp-modsecurity-crs /opt/tengine/conf/4 q+ l, F" i" B' ~( B  `2 y2 {; ~9 C

( m" s3 T9 ?8 \/ _: Qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 W& l- E: ?) R5 J' _+ b
2.启用OWASP规则:
" a7 {. q! d% v: n9 R8 y) n% O( Y5 Q. K3 T6 y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 ^3 P, X. i/ N& @, E9 x. M7 e, o7 c4 r
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ W: J, I* Z; Z9 h+ `& u6 E- r' E

: h7 O" M& W$ S9 x2 C9 |owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# o7 B9 s9 j6 M+ |, y& P" R0 x
5 @, I9 _+ M  x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) o- Q4 @* L1 o: e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
  w2 f- R. d$ \* V; S7 B# c" ?6 qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- |) B7 H, F8 s4 h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' d: g4 Q; o. S1 b( u. y5 W
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; z5 I3 V, Z6 f( HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% [8 t5 B8 i7 H8 m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 n" R+ x) t; _9 ]) ~! E
五.配置nginx
. V2 a  D# H+ D
+ `2 X; S' ^* a, _& N" H: n$ L在需要启用modsecurity的主机的location下面加入下面两行即可:, q0 i( a* [- L3 x
% v: M; O4 @+ a% R/ R, A" W& e
ModSecurityEnabled on;  
8 v- Q$ x5 e' M) L9 [ModSecurityConfig modsecurity.conf;: C: X( N* ~- s& d3 F
下面是两个示例配置,php虚拟主机:* J$ E$ G3 g2 H3 `
5 t' z& M: ~. v0 O1 \+ Z* R9 W( L
server {
7 G7 \. ?; P& U, N5 |9 B6 P4 B$ g2 Y# O      listen      80;
" q0 f3 X, ~- x) i      server_name 52os.net www.52os.net;9 k) t8 X' L$ A; b
     
" d; u1 v8 Z) _4 L, p6 S      location ~ \.php$ {  C) d3 D* e& w5 w) R9 ^
      ModSecurityEnabled on;  
: _' c; m) u4 J2 p- V' v5 @8 a      ModSecurityConfig modsecurity.conf;! f% r; Z# d5 O9 M

% x; @0 A8 X' m' P/ ]8 B; I      root /web/wordpress;8 F2 P1 I0 w! Z. t$ C
      index index.php index.html index.htm;% R4 k$ s  U7 h6 ~
  7 Q! b  l2 }6 h4 J& u
      fastcgi_pass   127.0.0.1:9000;5 e0 I/ R0 u# ?- N% J; g: U2 y
      fastcgi_index  index.php;
+ y/ w# [6 z/ W      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 X# Y$ E5 z1 r! K9 r
      include        fastcgi_params;
6 L/ E3 Z5 ~7 }+ g1 `6 S      }; ^* s& Y. I" S) y. ]) s0 h
  }
4 ^0 `# k1 l% E: v! Z% K5 r6 _0 Dupstream负载均衡:
1 B! P1 ^. ]. x2 C- L& q+ F; s
8 z* ?! \3 W; kupstream 52os.net {& ~$ z8 T: P! U4 y- J6 D# H4 L3 q
    server 192.168.1.100:8080;
& E! U& t- \8 ~0 {) I    server 192.168.1.101:8080 backup;9 ~. ~2 ?- L: P* i3 J/ S
}, L  Q, u% g! H- t7 F) j% n

0 _$ Q( i4 k0 z( B3 A$ T9 v# oserver {: q6 y" W; K' U+ Z/ m5 E/ P
listen 80;
  f$ O; o2 _0 L* e9 d0 ]9 u' @; r& Kserver_name 52os.net www.52os.net;9 C0 @( n' q0 T, J

0 [0 B- W" W8 q: c" j5 `+ plocation / {
6 _* S1 H: @* H' s  i/ m- I    ModSecurityEnabled on;  
# V, q" Y/ }! q$ S    ModSecurityConfig modsecurity.conf;  # s" l7 t; |! p. p

/ _6 @# \8 e. A  l7 C, [8 V        proxy_pass http://online;! @4 s' W; w. }$ C3 ?
        proxy_redirect         off;& j4 K% h8 f- C, w
        proxy_set_header Host $host;+ L7 @/ {" Y. P: {" e
        proxy_set_header X-Real-IP $remote_addr;" k3 n* q5 {& u6 y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;$ u7 c1 N+ |: A5 w7 Y9 }1 k
    }
$ K  x5 s9 M7 x6 N  P}
. }( Q6 {0 [3 H. D  P7 O1 n5 y六.测试
/ o- M% y  x, z0 e. o& x
6 S! q# X! \8 G1 W# C" V- c9 m: [我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: J0 I% @" {" c* T
  R( ?1 y' ?# g2 I
<?php
7 H5 C' B! }, }8 X    phpinfo();    ; W: A% [, F3 J" O: v) Q6 S
?>
" _& L+ ~' p6 r/ I0 u% A: F2 |: j. C在浏览器中访问:7 G) o6 r5 o2 M
+ u2 H$ n% |! Q; @
http://www.52os.net/phpinfo.php?id=1 正常显示。2 }: d! |) V1 y' Y) G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。" U* X. p5 d7 I( @  S6 A0 n/ s
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! K$ u2 h1 }  Z0 Y说明sql注入和xss已经被过滤了
' \) I1 C3 A4 e$ K# W
: X7 ^8 @9 F8 \5 f* g/ ^" U七、安装过程中排错
5 ]7 W9 h% S3 m8 a
8 l" b3 c$ S' `9 A  D* H1.缺少APXS会报错
- M/ Q3 p( n! z6 k8 t9 M. R
  ^( c- x% J3 \. _: r1 d4 r. {configure: looking for Apache module support via DSO through APXS
1 R4 C/ ~( |' lconfigure: error: couldn't find APXS" [% q5 Y, [+ v& ^9 w  p  z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& ^: T8 N8 e$ j5 }解决方法:; o# j& v( d  D: F" \
" Z& [' U" R5 [- q3 o4 R
yum install httpd-devel
, ^, x3 Y* C6 h9 ?5 c* k! o: F* j2.没有pcre
# v2 t+ L3 I. {8 j1 B$ `& K, Y  w! ?+ n4 Y
configure: *** pcre library not found.
4 g1 J' I6 H$ w( l2 N& U. ?! |configure: error: pcre library is required
8 p1 E* h! {/ J: K. G解决方法:
1 o5 k8 P8 d3 P+ Q8 v/ s' W) R# W+ T5 r1 D  H
yum install pcre pcre-devel
" K, c( X8 V! R8 k5 X3.没有libxml2) `, Z. c5 u$ I. d- E3 G8 v! @

7 G$ l* Q' P; K! k9 G) x- Y4 E; @8 M5 e# O6 W2 T
configure: *** xml library not found." @; e0 x+ Y. z
configure: error: libxml2 is required
: U6 x" Z3 W+ T- y7 X6 T" s解决方法:8 X  a2 i% x8 e5 w6 g( M* ]
  V8 o0 F' @  C
yum install  libxml2 libxml2-devel0 B" F+ _9 @$ @$ M& l1 F& Q
4.执行 /opt/tengine/sbin/nginx -m 时有警告8 O0 R- \- m' P6 k" a( T8 K- A
5 m2 m( N" {, z. L
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  g# C) A0 B! w; C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ V& d4 G. n3 g3 j9 }4 ~原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 j0 n% Y5 i4 D1 x

3 Q, A+ f$ J0 G5 ]2 h3 J6 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( _- J2 _) R8 u, K- ?$ T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( W$ O, d5 C* t* |) I) P' u" X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  r2 |- ]* n: c8 x/ s& E- C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". i; I6 n% [3 n; R3 c6 V" v) d& W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 @( e! @$ W2 [4 q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 S  N& r  w3 _1 |. F
解决方法,移除低版本的APR (1.3.9)
6 j, @6 |5 O3 m4 P( S8 O3 c3 M" L. [# z5 ~1 @- b0 }* u
yum remove apr
1 A+ T2 ^# ]: a5.Error.log中有: Audit log: Failed to lock global mutex; U6 x! n+ R/ ^  W  S* e& B

1 Q) o& u$ c; ~+ x( @" G8 x3 {$ w5 Y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock       K' C0 s- Q7 k& ^4 y3 [, f! v. B
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 J1 ?# z! Z: c0 C解决方法:; S, o1 A: P% e/ o9 l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" W& p3 D) j  K: i9 ~6 Z6 m

* v0 o% V2 @" n2 cSecAuditLogDirMode 0777
7 |7 t) [" h+ |; l/ MSecAuditLogFileMode 0550
7 E+ f& c% Q% C8 q  ?+ C; |& P* r, kSecAuditLogStorageDir /var/log/modsecurity8 G, h6 T3 z  T1 E+ d
SecAuditLogType Concurrent
, r5 W5 D' D3 y+ c2 ^! N- e, h参考文章:& U6 n" x( E3 z; c! `/ E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 A' ?7 o. o/ v
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-27 11:59 , Processed in 0.061306 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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