找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11770|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! c5 `7 d# ^" O; q) b. v; Q7 J$ z$ A5 N1 t( n) }
一.准备工作. @+ b0 o4 m( [- P5 z
. @; E3 i/ x$ N/ t9 H7 L9 C
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, N; Q$ d* M- u; n) Z0 u# M4 \$ `5 z" t# H9 Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 m" o0 O. j! Q+ z

3 T' `4 W3 |  Z2 y% Wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. x$ b$ {. w2 c6 S
. V4 y( q6 u% e
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 I' b) u+ A9 t- N0 Z

' I& O, |- Y0 q( F依赖关系:
  v% r+ M: z* w* ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 O+ l: @  r2 N+ p0 V- l2 |6 z. J
. D0 p; W+ a6 v  _2 h7 x2 H. j+ lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 X9 n) ~; @# x) @1 r9 e' t# ?7 k6 Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr( H5 L* R6 o! |! V! U

7 U4 G8 P) P) r+ e6 q3 e: Wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, }; o6 K8 U# L( M& x& g9 _二.启用standalone模块并编译
5 |1 W* J3 p# T# @6 {7 ^) ?
- U7 ?- j# A% {6 r9 K; T6 T下载modsecurity for nginx 解压,进入解压后目录执行:
! z6 T& ?, Q, b3 d" F' i
: `. w  }6 n2 M0 o./autogen.sh  O1 Y0 W1 h% c. S4 P1 T
./configure --enable-standalone-module --disable-mlogc
, i* l+ D) H5 v9 Qmake 7 v3 F$ g/ G8 z3 K6 C, n
三.nginx添加modsecurity模块$ ^+ ^3 C. [7 F( c' C) [
/ I+ r/ X$ v6 W2 {7 R4 {
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ `) u0 o- d- }- {
+ @1 g) O: c' i
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 w6 |- g* C& M( Y# M4 ymake && make install' M) L5 Z2 p( X% [5 U) ~7 v
四.添加规则3 N, {( ]2 |! S

4 i0 b8 N5 }% U8 I2 s3 M9 w/ Z# Imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 Z3 g1 D: W! t0 Y) A  K: v8 u& q! e* n* ?0 h, Q  w& t, k
1.下载OWASP规则:
% C6 }: X% Y! ^; a
& s2 ~* X  A+ [2 T$ I! Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ Z+ n0 d3 J5 W8 P3 T
/ P8 Q. {+ \) [: ]+ {* W. a0 Xmv owasp-modsecurity-crs /opt/tengine/conf/
! E, i- \8 k6 E0 U/ z) r) ]
0 @7 V& b8 F! a, P& X. Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" ^4 r* ?5 h9 d% l% I5 \
2.启用OWASP规则:" k( `5 Y# R- j& K' z. x

  s7 X* B. f0 J) U" B  X复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- k4 a6 V3 p- B( O2 _6 L
7 v+ S- ?' A$ |5 {9 E
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ g7 j; f# M3 K6 e

/ X: F2 R- H5 j1 X9 `1 p" {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! B( E/ W7 K5 u; n' @2 W

5 l5 l, O6 w4 G  H6 EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: p$ \3 M$ u2 f; ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; X& {+ I0 T- i3 L6 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( b* [: V" M, s) TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' y3 Y6 p  U% y6 ^6 y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" K3 f+ `2 c7 c: c, R& }) L! w6 _  BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 I, O6 ^. ]0 s/ ~3 _8 E9 w
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 f1 Y% W1 ]4 z3 g& N- v- C五.配置nginx  _7 Z8 s7 Y* ?8 V" D4 e

2 l% U6 w, k1 A$ b% {在需要启用modsecurity的主机的location下面加入下面两行即可:! v( s: M' \+ v) Q4 E3 s
0 T) ~% u& b3 H
ModSecurityEnabled on;  0 G3 r" O! q- E, j5 h- D
ModSecurityConfig modsecurity.conf;
: j- ^+ K' y$ c( t: B, a* n0 F下面是两个示例配置,php虚拟主机:
4 r: U$ H! j5 U. k- Y3 J; o$ f$ I; F3 B6 m% f% B
server {
3 L! t0 P$ q- h0 E1 f% A; @      listen      80;, G% W( L, v+ _9 C' e- h
      server_name 52os.net www.52os.net;
: v: ~' E* k+ y# V! X' W     
5 b, o/ K0 [: z, I5 ^; V      location ~ \.php$ {5 v) h- I" |7 j% }  Z7 c
      ModSecurityEnabled on;  * O2 p* E0 t. ?( Q/ T
      ModSecurityConfig modsecurity.conf;8 W0 q; {; e3 N
. C: ]1 m) x- Q# x: b, a% a
      root /web/wordpress;8 M! W4 u  x% t7 ~' M
      index index.php index.html index.htm;8 ~" r- G* Y8 b5 P
  1 {+ }" V9 b/ I4 H7 o
      fastcgi_pass   127.0.0.1:9000;
6 J/ }1 U  a7 I' Y! o      fastcgi_index  index.php;" r" \8 c' _+ y& x# w) C% f5 G) V
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
; Q2 ], T+ d4 F" Y$ j      include        fastcgi_params;9 _' _( h9 }, Z/ Z9 K% \* d
      }
+ ^8 F; `. [& }- ~% O# K  }9 ~$ C& e2 O6 `1 r* G6 J3 r) V/ h
upstream负载均衡:
. r1 r: @* Z2 ]9 `7 `9 l* D' a% P9 [0 k  t
upstream 52os.net {7 @; C/ v0 g4 d! E
    server 192.168.1.100:8080;2 r& {5 u: r; V# d2 {4 j  @& {! c
    server 192.168.1.101:8080 backup;* }; o# e7 P1 A
}
! M8 Q$ ]1 ~/ j9 ~; h4 P
+ o( Q/ w5 ?) u! O& r; qserver {: m1 w* ]3 T& S$ v2 d4 _- w' E
listen 80;
; [  K7 Y+ @* F' B5 A; Z% F8 X$ c3 l1 Mserver_name 52os.net www.52os.net;
% L1 K+ H) f( F$ s/ c# P( M
! |) l/ u1 n" i* o5 c* Ylocation / {- I! _3 G& T5 q% ^7 t" K  o" `8 q
    ModSecurityEnabled on;  ! @* ?" G+ A# J/ q* R$ U9 _# W3 o
    ModSecurityConfig modsecurity.conf;  1 h& P5 E0 h8 \9 y/ |! J
6 Y6 b! m3 W% j1 V, b$ F  T1 x  T0 I( O
        proxy_pass http://online;% L& z- N% t; f4 e
        proxy_redirect         off;
/ ]3 h( B' S8 k' P1 B        proxy_set_header Host $host;" Z; w( b- m. b
        proxy_set_header X-Real-IP $remote_addr;* y4 G$ N; `' `5 R* f: H% S: [6 y% e
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  `( Q$ g9 K! c0 _' i% C+ R2 n$ j    }
" Q( j" r- Z6 L}
4 M' Q  O* H. a  ~  I  n六.测试4 y) p" r9 E6 p. b0 S* ~
* W) }8 f0 x: S1 U5 ^" r& @
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* k8 V' h+ @2 I: |$ n& a% a& B  i: S5 G, I: p
<?php( Q+ {# F- f: P- W- H4 r2 |
    phpinfo();    0 N9 a$ _3 n4 S" y9 |
?>
7 v' u; y3 H3 c7 l+ k" ^: w5 B在浏览器中访问:
3 o) y* L- |2 c0 x$ }9 L9 f. a( p0 e* a8 Y& K: y5 |
http://www.52os.net/phpinfo.php?id=1 正常显示。! O5 W  N# A* U  i0 \* B$ i2 P% @
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: j4 M  I7 r  [# x+ j7 e0 thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。. j7 W: z# Q8 I$ k, H; b2 v4 A
说明sql注入和xss已经被过滤了
5 I9 x. ]5 W$ G1 m" g% d4 r
2 `* |  s# e; H9 m七、安装过程中排错
1 W* u; M& |; T* q" l' h
) ?/ l2 f- |* Y& `6 I! T- p1.缺少APXS会报错! f3 p4 r+ B8 Q* g- W

- B* D+ v, g# K' Z: e' lconfigure: looking for Apache module support via DSO through APXS
- m' k3 s  C7 }! Z( Econfigure: error: couldn't find APXS8 C( L  I* K* g" Q2 v7 S
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 g2 x4 y( Q& i* P
解决方法:0 Q4 @& m: j9 p  a3 B8 s

; |3 p/ k0 a* z4 L+ e# k" [yum install httpd-devel5 U) V% H$ b2 ?. z) e. S* _- j
2.没有pcre
8 e6 }9 M6 q$ D
/ \- `9 [; E7 v, W; vconfigure: *** pcre library not found.
: ?; n0 \; S* H0 \configure: error: pcre library is required/ Z9 @  O4 g: Z  s
解决方法:' z; F/ F+ J! x, A" m  q
3 z0 r0 T1 ~. V; g7 J+ V# i3 a
yum install pcre pcre-devel% n4 a& ^5 Y' ^7 H) ^8 l
3.没有libxml2
0 Y. p. f" o! H" _% l
6 n! p/ d& u  N1 ~9 i& X
2 U! G8 h2 Z& S! Y7 Y" \configure: *** xml library not found.2 N+ `! c7 A9 N, [5 E6 L% z9 t
configure: error: libxml2 is required& o& y1 T$ t) X8 R0 B) h+ G  u
解决方法:- f9 K4 W2 ^" U+ p
1 d; |3 R0 W2 B% F
yum install  libxml2 libxml2-devel6 [$ E& \3 j" ]( K9 ~: [
4.执行 /opt/tengine/sbin/nginx -m 时有警告- R; D; L1 o" f# s2 p; s

* I) t% V/ U4 O+ _Tengine version: Tengine/2.1.0 (nginx/1.6.2)' X3 s- v# g9 d5 O" h' A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
3 F4 k* k& r, Y$ D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! k/ r+ B( S& j" Q0 `
- p# V2 G2 h/ U3 z5 }( h6 Y  m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' G3 e& {! b) x& J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"  @$ c2 v9 Z' u9 I9 T. ]0 }6 [7 T
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* P0 w5 V& h; c! o$ B- l) s& i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ h/ B5 m. E: z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# p, l2 [! G1 J6 I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* M5 Z) e4 J: i6 y  k
解决方法,移除低版本的APR (1.3.9)
% B, P" z* R) i5 N/ `" B! O1 p4 z) D- Z5 {! M
yum remove apr
( o4 r- T. y  M0 C5.Error.log中有: Audit log: Failed to lock global mutex+ P- v! A- `) w1 p5 `0 h$ w* K

; W* n* t( i  [7 b. L7 `; K$ k% R8 h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* o+ F6 J5 X' ?) vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 V/ Z& V8 _, |+ |/ Y
解决方法:" R  |  P4 J6 k" ?! U
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' H5 Q$ v0 {, n% V4 v. Y5 W

5 ~- Z. L$ q' w# F5 Y: bSecAuditLogDirMode 0777, b$ H; w& d! w7 ^0 |, o
SecAuditLogFileMode 0550' ~$ @' v: b2 {
SecAuditLogStorageDir /var/log/modsecurity, x. r. L; h: k" u+ a: L
SecAuditLogType Concurrent
: o0 G  ]* }/ B- S5 F2 a# C参考文章:% p* N9 T  U* Z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# i& |# o& q+ ^; y  l
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-11 16:50 , Processed in 0.077525 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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