找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13273|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ j0 o) d2 e3 G; y# s9 Q

7 y' |- _% g* _8 c$ K一.准备工作
* t/ ]; k4 L  ]1 `; I3 O6 ?" O/ K5 q& X" D0 v9 m4 K+ ^- S( i* n$ V
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; e1 R8 r$ ?* ~- [, r, A7 G/ n; v9 Y

$ L" K" B* |7 y- \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 X2 l/ |) u1 p3 n& r, b  d
/ [" E, Q: U' t( p7 M, t) r' @
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 R/ i& T$ i2 ]3 q, T2 K
5 j* J4 {2 W) l; I; P* d' ]
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: o8 ~* |" N7 N
; ~& I% n& w/ b6 R- M
依赖关系:
& a! b$ R2 K4 J) m; `# J0 c4 N. otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 O% t. F, W, K+ Y
$ e3 C2 j' d" X2 u( Q" z' o! n! Zyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
% ~1 r' J; u4 z0 u! H; x1 Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
! {' S6 ]- ?8 F  o* l2 {) `& {5 m5 S" u$ \# u( i* y- S
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& A: b. u. h0 Y( d7 x; B: B
二.启用standalone模块并编译: q, Z6 ?/ o5 S( f6 w) t8 [; U) y

1 U' j* Y6 ^/ W9 ]" ]下载modsecurity for nginx 解压,进入解压后目录执行:/ {3 F" e3 [" E/ G7 x1 B
& F- ]2 j3 e3 X" t! W) }; q
./autogen.sh
, F7 c+ S6 @9 e' @; w2 @; h* D./configure --enable-standalone-module --disable-mlogc" f; D$ A  z' ^: n" h2 v2 C7 r) e
make / Z+ P& O, T7 Z/ c$ |( Q
三.nginx添加modsecurity模块; T6 r1 T0 H* A1 `6 v. X: m8 h

* N! W) y1 U% d. A6 C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ }( G( \  G+ ^3 e2 A3 o! J  G
6 u) z1 F  T- \& R3 _+ N8 v
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine6 I9 N( ?- O9 [' L; k
make && make install/ t2 |! y, m* h0 i, N
四.添加规则& I% p; v# w$ _* I% P% W! T

" C$ V" G8 `: _1 b  v0 [9 Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( c) w- c1 ?# T* _; d
8 I2 O: h6 Q% d4 F1.下载OWASP规则:, S* t8 `0 I4 V; U, V' H

* y- d3 w/ z5 k, Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 U* h9 ^' A* C8 g5 ~, i) Z# Q

# {* ~4 P! H3 q$ m# H1 qmv owasp-modsecurity-crs /opt/tengine/conf/
! n- B+ p- e7 S5 o/ O! y- N" o- C+ X7 {5 L4 E5 N
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% e3 X2 U& Y; y; O8 v& O6 [% x
2.启用OWASP规则:
" }/ Y) P) X( K) T/ A( t+ m5 {' ^( X0 ]4 Y8 Z6 F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 v# R1 |& J5 u$ o
; H5 s* D: G: ^: U: V  ^: E5 d! V$ `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 g# p- r% j7 F# z1 W

5 G6 Y5 u% }/ k4 k) n% t# k' dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. Z- d# ]6 E8 \, e
0 F/ `% b: L4 f) d2 V0 [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" b9 e1 x& Y1 V$ S$ d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 z) I/ Z: q7 L$ C  ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. L+ {9 E& j* N! ]$ UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# {/ \; E6 Z( u; u+ g2 i7 a, c% TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 H# J: T* Z) A& a" b3 y7 c4 Q! JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 r% E3 D8 X& q% oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 I$ E' _- a# w' J/ V2 W五.配置nginx0 C8 ]/ s( z" w6 ^) M$ B6 b

! Q5 `# i; G: s0 {在需要启用modsecurity的主机的location下面加入下面两行即可:5 H3 ~) p9 `7 A6 a$ j, j
, M& ~: T" K8 T, S  |% B% t5 k
ModSecurityEnabled on;  
$ U, d! {* l; Y8 Z0 r2 mModSecurityConfig modsecurity.conf;3 k% s( Q* N8 t
下面是两个示例配置,php虚拟主机:: a7 @$ y6 W! J' J4 N: T  k
2 H% ^( J8 @! J2 q$ m
server {6 P  R$ d7 Y7 F! B
      listen      80;
+ b: W' |1 S/ f9 l      server_name 52os.net www.52os.net;
3 Q6 k4 J% q2 L     
$ U7 K3 ], ~- m9 F5 ], q5 t      location ~ \.php$ {0 h5 B. ^+ F1 i  a0 w" p% g
      ModSecurityEnabled on;  6 J' j/ h+ Y  q5 _
      ModSecurityConfig modsecurity.conf;# @: Z! p# |  ?
* f/ q- o$ u4 ?1 n% _3 `
      root /web/wordpress;" h+ ?2 X3 e6 N. H  o8 y% K
      index index.php index.html index.htm;' C1 S/ @( d' i  r5 g; j  s$ J
  0 O# x1 G1 U1 y4 ?+ H
      fastcgi_pass   127.0.0.1:9000;8 x4 w1 ]* ]4 ^- R. Y  w4 f# n7 b7 Y
      fastcgi_index  index.php;# C8 R1 d& i) B, \
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& y8 o% e9 O7 p, L2 f, M      include        fastcgi_params;
& a/ d; y3 f! Z      }
& ?: T' |( B' ^: k8 @6 B- P* k# k  }1 `% y7 p1 K, J1 o( b
upstream负载均衡:
3 k, X" s5 R; t/ n& ?) U) S& O( x; w* v* s) b/ @" S
upstream 52os.net {
0 L! }- W8 y  x* d) t) J. c+ f5 i    server 192.168.1.100:8080;
8 z' ^' D- @. t5 ]/ l0 r- \    server 192.168.1.101:8080 backup;
, a: B/ c9 l0 t- }0 ]3 H3 {}0 V+ o! N0 {2 j. N

3 G* J( {/ F4 k5 P+ T& T; N, n7 B7 qserver {
8 z9 U. ?# E+ o  flisten 80;
- u. R0 k8 U( h! jserver_name 52os.net www.52os.net;
, K& R' E" V( j3 [) o! n5 c
' e+ M3 p; X+ _9 f, y9 @( l9 ylocation / {
0 I5 x) a3 ?7 U% y    ModSecurityEnabled on;  
  \0 A/ {/ E3 [5 x+ Y4 T7 T    ModSecurityConfig modsecurity.conf;  
/ @  p1 a+ O; K' G1 V
- C: N. v( K  k7 H" J1 w2 I        proxy_pass http://online;9 R, D6 F7 V7 [3 m2 H; n
        proxy_redirect         off;
1 F. f) V! |0 K) S% b0 B        proxy_set_header Host $host;, g9 V* A/ [8 l3 K9 t3 K
        proxy_set_header X-Real-IP $remote_addr;
5 Y: T1 D# |# c$ g4 j4 p        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;7 c5 Q! |% G/ q# v0 e
    }7 G8 ^% h9 {" y  O' M$ ~
}  _9 i. A9 J% A% E0 S
六.测试
1 C! c8 ^- j: M1 H: F0 A7 O* l" ^9 _, g$ V
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) x& _9 d7 U2 |+ m. q3 E* p& |0 J
$ _; ~' X' x5 u4 L6 t+ y4 i" h0 J
<?php- s6 T  A7 u& h# f. ^$ Q, o. ^* _6 l# n
    phpinfo();   
* X6 t/ ^' U; [- j4 h) w?>3 l" k2 L# Y7 f. K& N. ~
在浏览器中访问:" G6 P3 h: f2 `5 @+ G# P1 ~  r

: w$ F5 F# l7 Z) m' Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。- Y$ I. B; a& n& f- F
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
9 P$ D8 P4 i, k' C: qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ ~6 \# w3 H8 g说明sql注入和xss已经被过滤了0 s/ b! U) q, q3 _7 _# D

4 [- ?0 ~8 Y4 ^/ M1 F) Y- R  N七、安装过程中排错
$ e! m" k8 E# C) G( R- T. H4 F* b: H
1.缺少APXS会报错3 e0 ^8 ^0 m6 ?

* Y% ^" r2 J- Tconfigure: looking for Apache module support via DSO through APXS
* ]8 I/ {2 t' ?6 ?& _5 Uconfigure: error: couldn't find APXS: @& c1 L2 H0 h: K
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" f6 [' O/ H/ T2 S9 G, h
解决方法:
  t% O9 S2 E4 {1 r0 X* P- E+ A7 ?( e. d; j% c- K
yum install httpd-devel
5 z4 H6 m( q$ o1 N6 u5 c2.没有pcre+ I4 N0 q/ T( _4 v& |& l& u

7 U: [/ A4 S* u; X9 v# l3 D$ bconfigure: *** pcre library not found.$ e+ z) E/ o5 [- K. _! r8 N
configure: error: pcre library is required. ^" `5 G& K& [4 Q$ T( M" N/ p+ }
解决方法:; _$ V8 |, m8 _9 g. v& o7 C
- O; @/ E. i/ B& T4 u
yum install pcre pcre-devel6 D5 k! b8 F  U; G7 q
3.没有libxml2
( }) {' v2 @" w6 b4 t; ]
1 I. n# f) E- h- A8 @: [6 c
' F% G3 G3 j$ V' R- y. ]configure: *** xml library not found.
: I9 w8 t' W6 A8 D4 H+ c4 ^8 rconfigure: error: libxml2 is required; j/ a* A' c8 S" G
解决方法:2 p. c0 T  Y( M( \

  q7 z& d6 q+ L. V, Myum install  libxml2 libxml2-devel) n# P- [  D2 O6 i' B
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ ~$ t7 A: q- ?! u' I( s8 v/ y7 c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( P: t7 s, F( z! rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ R- M, S) j! B/ L$ a1 H/ f8 x原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" c; o: C- G8 m( @9 y) Y. `, j$ G% C' Y* f  C2 V' s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' P; `- F* M) S) x, ]! d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
7 U8 O1 t* ~; p* ]; \2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( j# j4 i( T( E, _; n% c# y$ E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 o- S) x# h/ [5 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 @8 D& e" g+ p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ D$ P# {' [& i+ G$ z  w解决方法,移除低版本的APR (1.3.9)
' W) ], U* Y, {9 L" k4 t, T/ `) C, a4 Q
yum remove apr
/ D9 y7 ~) [, Q% Y9 \; E7 T' Z  Q' Z5.Error.log中有: Audit log: Failed to lock global mutex
) t6 e5 }$ J  L" x7 k) }8 t- |# j# ]/ W2 ^4 y; u* q! J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. N  X; h& N2 g* J$ ?& n! @global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- E  @  o* Q% `8 D0 k% F  K: n解决方法:
$ H( R, b* q+ a: \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ ~: o* l; r+ _7 A6 n# Y% k0 h( p0 S
$ D3 n4 z5 y5 Q; R8 o; PSecAuditLogDirMode 0777
+ x2 M# x. W. ~9 }: @2 O9 {SecAuditLogFileMode 0550
5 s* d; j" _& ?) u6 i  L! R6 TSecAuditLogStorageDir /var/log/modsecurity
3 |8 H9 E. @7 J) f2 WSecAuditLogType Concurrent
9 p5 c7 q4 h0 [% U; d1 S参考文章:1 Y. [. `2 v& B5 h
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 @% U" ]3 L2 X9 i+ j; E) N) lhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-1 04:41 , Processed in 0.069483 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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