找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12484|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ d! e6 P7 _9 J8 E5 P
, n1 ~' t& {- Z0 ?7 E- o一.准备工作* Q2 F/ P5 j# _. S

* s& K+ F9 m3 D& o  E+ D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! `2 N( Y/ G1 L/ s3 `0 Q# j1 T8 F0 n0 z9 J* T2 E+ J# z9 I
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" F! X2 D) r5 v+ K* k
+ B: Y# h4 h+ s* W  q8 k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( K+ h6 v* A. n& F4 e9 f. a) {: r8 r1 i9 s6 y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* c( e, F. k# W  m% Z, _
) P" s* v8 i, A0 P) Q; \. j5 x依赖关系:& A8 v7 N$ Q' V# I7 r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; z7 H, \. M$ t; e6 J6 Q3 ~
# g6 E+ H* s9 E2 r& qyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ H) z  D* F( ^6 x6 G3 h- X
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 h2 S' X4 N4 Q1 m# d$ c

3 i4 o4 k8 E8 ~* {( lyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
! ?7 C$ t8 k. P1 ]二.启用standalone模块并编译* @' x+ x, |" V* ~% r# S4 t4 Y

$ l; x- ]' |2 k3 k& V, b! H' p下载modsecurity for nginx 解压,进入解压后目录执行:2 `% T; A+ @9 w; k  C$ j/ c' k

) F/ w" e2 ?9 |0 p./autogen.sh$ S$ J% z+ b" X5 y* F
./configure --enable-standalone-module --disable-mlogc
5 S8 P6 Y+ J! k) ?' C/ I+ tmake
9 S# |  b& Q  W8 i! c2 P. v三.nginx添加modsecurity模块0 [7 |" d6 g* _* P! n; l
3 t# i7 j5 x; x# H- x0 t7 ^3 D
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% \& }" l1 {4 `, Y  d) T$ D/ j

8 v: C- C( O5 L! }- @$ L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 T, n4 e& G& E6 a2 ~4 p, N
make && make install
& u( m! ~9 @0 ^' s, ~+ N# K9 [4 S四.添加规则5 D, C7 c3 O1 }, U

- Y' o7 o" y& {7 wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 S2 u- B# X/ ~& v
- Q3 G1 c" E% @+ w5 H
1.下载OWASP规则:/ d5 P" S3 o2 W
3 ]! R; T) l0 m9 X
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 m6 y* b& \7 a$ w2 W! D
" S  y$ n! Y1 \* c) Jmv owasp-modsecurity-crs /opt/tengine/conf// ?* S1 H) Z6 }4 o; x

9 r7 K. x, z, r7 p+ acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ x" w: q7 A0 C* F1 A: F" K  _
2.启用OWASP规则:
$ g. n/ [- g0 }" [4 h# x# U
& X4 a8 y; x* Y( _( j9 q$ Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- V0 B5 X: b8 M9 [& z+ m# V
% W0 V' w" ]  [( G7 t2 d; M% \0 G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( y* P* h& H" n! Y
/ q% d# i; r. s  w7 x7 {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% ~3 c( A1 _- r9 K1 F
! |. p  [$ W7 [. u* d* ~+ G! jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: z8 Z& P1 H7 z" f3 UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 i) z) Y9 U# _! b; q3 A% m! `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: x1 R: f7 j$ j% ~7 N: [1 L  c& RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! h: D" X/ @8 M  F* r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 a2 K1 L/ a% b) `0 l( t9 S. |2 dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; Y, x0 ^5 a7 c1 H6 h2 V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; `, U2 y% |, {
五.配置nginx
9 ]7 Y$ ^; }/ c+ B& l+ ~  m+ t/ y* y. K! a) G2 J
在需要启用modsecurity的主机的location下面加入下面两行即可:9 L5 W' w( s( b5 d% e$ |- r; u

9 G7 l' v7 s* {* W. x2 @ModSecurityEnabled on;  
2 i3 M! j6 k7 x$ j" YModSecurityConfig modsecurity.conf;" \+ a$ Q1 G1 i5 l( m7 G* t8 ]
下面是两个示例配置,php虚拟主机:
. }6 r/ C. Q- o( U8 ], n% s. y+ z; J/ K( P! s% S+ Z/ U
server {
1 p1 x) s/ x2 c1 i6 ?      listen      80;+ Q0 R( X9 c& X( i7 J& I
      server_name 52os.net www.52os.net;6 q0 i- t8 Q5 t9 q" k' J
     % J; w1 u0 H$ _8 f0 A
      location ~ \.php$ {6 @" X. O/ u& E5 T
      ModSecurityEnabled on;  ) S/ m- X3 u  ]( Y- j
      ModSecurityConfig modsecurity.conf;  y( j+ z' Z; c, S

  @  Z$ \6 o* D; k- F7 D" H; f' B      root /web/wordpress;) G2 \9 C: d4 W9 W8 \+ k1 e$ g
      index index.php index.html index.htm;
* r) q5 j  K. n0 |  
' p0 ?7 q5 n/ O! v& h      fastcgi_pass   127.0.0.1:9000;! ^) q( u) \6 h& T0 d
      fastcgi_index  index.php;
* y' Z7 ~" q5 y8 R# s      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 I! W# u) }' B; F# t      include        fastcgi_params;5 M; z0 x0 F; G" |* z
      }  A0 r: T, b8 R1 Q4 t' b! {9 X, s2 a
  }
7 J& R7 c4 S* z+ @9 V/ S% Mupstream负载均衡:* `6 H- g; l' S) R" X

3 J% w& K( a& I' n7 qupstream 52os.net {
/ c8 v' @0 N% r, V# c" b    server 192.168.1.100:8080;
+ j; Q. X6 C5 v8 b  U    server 192.168.1.101:8080 backup;, ~, E: h4 {+ h+ b# L; e
}) l' E  e5 {: C- i+ s" j2 |; z

' |' T' x6 b. h- S2 U& {server {
2 j" }- {1 b# ]listen 80;1 O8 b6 R. @* Z
server_name 52os.net www.52os.net;
  d% |, @' n& c3 M/ s) C9 D% \4 q& E" \
location / {2 f* U/ F8 M6 ?' V
    ModSecurityEnabled on;  & R2 L2 Y* b' e  g: t2 t% }
    ModSecurityConfig modsecurity.conf;  
1 m7 I& ], ^8 G* d& S
+ K3 \9 h5 ]+ A+ h        proxy_pass http://online;
; N* u' X* k( i. q        proxy_redirect         off;
9 P/ b# L! t7 V+ b        proxy_set_header Host $host;* \* c: I0 l4 s5 W# D; f' z, l
        proxy_set_header X-Real-IP $remote_addr;/ Y: O1 n& n& q  K0 A6 n( J! F
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: Z" r3 p$ |! A, m" o4 m5 ]! G
    }( C2 g& H- Y4 P+ c6 ]; N/ U
}
! G5 {  |5 G# _: n& |5 r8 A六.测试
* ]* @9 K2 G" G% D: b/ d. I6 t) z* @6 w0 ?5 O' B9 G0 ~
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ M9 w" K( y6 M! f
* k7 e8 Q) m  D& K<?php9 l# R5 ]( d  M. u7 K
    phpinfo();    9 Z1 x# k1 u( A0 o
?>
+ k% B  |% A4 m; m在浏览器中访问:
2 E5 T" V4 ~2 u! C0 r* m
, R; `  K$ h7 X7 g8 {3 ^( W9 nhttp://www.52os.net/phpinfo.php?id=1 正常显示。
: T. \/ S* J0 D! |  khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 J5 B+ L) W+ ~+ ~
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 V% j# @6 y% h# p1 U, `  m说明sql注入和xss已经被过滤了
+ F- @5 B* w" Z( O" k' L2 q4 w0 l0 k2 o
七、安装过程中排错
) `  L+ g( B( c! C+ Z7 |# F+ H8 I4 x! R6 W, c( |
1.缺少APXS会报错/ i/ d/ h5 f! @% g$ K: Y7 X9 Q
& J- D+ l* p. K0 o4 }# i; y$ N7 K
configure: looking for Apache module support via DSO through APXS0 {- y! h" Z" }* Z0 A1 g* \/ t
configure: error: couldn't find APXS
# H4 j4 r! ?; O* l% w* vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* u$ R" y9 j9 O4 [* G4 M! s& w4 z' @解决方法:
( W/ ?9 z2 Q( `1 U- T4 A/ C0 y& G) V- T" f5 W
yum install httpd-devel' {5 B' j0 N4 a- r6 J! m
2.没有pcre+ A3 w: c* ^' c  p( I' l5 i6 w
0 Y4 `/ H6 k+ Q; S
configure: *** pcre library not found.0 [, Y; R  `4 Y3 W/ _1 u7 b
configure: error: pcre library is required& J' W* @: @/ O
解决方法:
: e0 X8 q9 z  c+ A" K, _5 _# r7 b/ c, r# r' i+ a* P
yum install pcre pcre-devel
6 U* x9 @' W2 H# G- V3.没有libxml25 P3 l4 k0 V, t' Q: {
* M) S9 ^5 I: g  ?' K" q

* Q: Z, C! X2 m! G, H/ w9 tconfigure: *** xml library not found.
  @. a) J. I1 Xconfigure: error: libxml2 is required8 D' d% \5 N6 k9 Z  O1 \1 b; B
解决方法:
# u- u! P  l6 k, I( X/ L7 R7 L7 H6 I8 o$ X6 n
yum install  libxml2 libxml2-devel
7 b' N& G  V+ s1 r4.执行 /opt/tengine/sbin/nginx -m 时有警告, N3 `# y0 _; L* r( l
+ u2 _! L* m* m2 K% p, x0 s2 E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 ]9 {) r& Q, ~' T$ `# }* Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 N) m2 `. X$ `( w  z* T0 R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ x) m6 ~1 e$ h5 o

  S( ~( d# K, m' p* o/ W9 j* d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 w2 ~0 @, c! ^: P5 q0 W2 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
+ `9 Y" C  j. w4 ]8 Z4 A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% N0 q' p& n" X, I  V0 _8 @% E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 Q( E. G# O7 y# N' g6 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- g2 h' L- x# u) B$ R1 j7 h( C" J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 ]7 N; s) [# \* q2 z解决方法,移除低版本的APR (1.3.9)
- G6 d1 H; g1 M0 G/ J
/ Y. x4 f4 j* |' syum remove apr
! \; j) [$ d" Q5.Error.log中有: Audit log: Failed to lock global mutex" S3 ~4 ]2 Q2 e; v) \) ?8 a! G' F. H

8 O& E- j2 C7 O/ V2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 I4 L- x" [/ K. J: J, Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 D7 J; d% k) Q0 l5 y0 h0 H
解决方法:
  s/ z/ y' V( ~4 B编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 O) M% K* c# [
& {& I+ }9 B4 p4 o2 rSecAuditLogDirMode 0777
! e: z1 u4 ~! V  q# V6 e- PSecAuditLogFileMode 0550, X9 B+ C9 m. C( g
SecAuditLogStorageDir /var/log/modsecurity
' W* r/ h8 V8 b1 ]SecAuditLogType Concurrent/ A: R2 R! [+ C0 L+ i
参考文章:7 X# u, J& C+ C$ j( s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 _; i0 I# v0 g: [* C! F6 o+ Q! shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-9 17:41 , Processed in 0.069570 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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