找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11883|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- H# o9 s" o0 }( }6 {  }! w5 C7 d4 B6 n0 z9 s
一.准备工作0 Z: v( G4 K1 ^4 C8 m5 T) p

) L' |1 t/ n6 f# T/ B0 B系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( Z5 b. @5 d5 s7 j, c& V6 g

' Z9 U* L- J3 E+ v, ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 `! q0 s, G7 ~- U) p% N7 T
! d* E. T' x! W- hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ J3 x: {3 t8 j5 c. [- H+ v
7 }' p+ F; z( V1 ^( nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' e  C; U# l& a6 z; }3 g1 C. G6 x1 h9 s
依赖关系:1 s8 H, z& a( Q  d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" w6 a2 ]  x. G) O
9 v) a* ?( K' V+ A4 [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. i( P- L" `1 ]; x
modsecurty依赖的包:pcre httpd-devel libxml2 apr" W! X# o1 f# Z* w% M

% {0 R4 K4 z0 o& yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, e% P1 A1 r6 x) Q! [8 L; |二.启用standalone模块并编译
' e( J* r) @% y: N& W% m+ e. E8 `0 o5 y/ ~3 Q
下载modsecurity for nginx 解压,进入解压后目录执行:9 P/ y0 L" T) d0 ~  ~

# {5 O% ?, |: j( L& v; U. m, H, g./autogen.sh" F; y4 G5 J. M0 C+ a% H
./configure --enable-standalone-module --disable-mlogc
. F5 t6 k) g+ s! P3 a; Hmake
+ \$ `2 C- x2 @7 b; Q3 [三.nginx添加modsecurity模块
- m1 r; f. b) N. e" M! z+ r2 \) x7 \$ |& o8 n& q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) J% G; M( w* h6 ], O3 g/ [& H
9 v6 d. L$ I* e+ ]: Q./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  Z$ g9 d' z( ~* [make && make install
' ^4 M# E/ A8 t3 o四.添加规则. D# ^9 N  y" S  Z0 {, z
1 z5 e# P5 X% e5 l  }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) S1 h8 D6 z, ]4 H, O* s5 j5 w# x3 P

0 J4 Y% _. Y8 `/ M$ [; l1.下载OWASP规则:
0 Q9 e9 ~5 X" |7 Y! w+ M& G; p+ K, S1 v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 a* K5 J+ s5 y1 Q( M

0 H: I2 {9 W$ l+ n2 O% omv owasp-modsecurity-crs /opt/tengine/conf/
" s8 L% C* h' M
3 H* x0 d' n7 ~# ]$ ~* E  gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) y) O( b6 P# |6 q3 x3 s6 T
2.启用OWASP规则:
- b9 M7 A; L5 X- V1 W& e  w4 H; L- Q
7 J# v8 g% F1 Z0 w复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 J5 I) j. E2 \* L9 g" [

: ?' [4 v/ m4 P7 {1 _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& F. K: Y0 {* b9 m' r
( k) c" J6 n, k2 S) a; Z# B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- F- V. ^+ ~& i6 }" ?
6 f6 [$ r. S+ D$ O7 i/ d  bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ }) K! _' M, G+ j4 J& gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 k: H; g' Z+ R) k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( i# M5 n6 ?( |+ m4 B9 N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 o" u, s$ q) \9 M* S, ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 _7 S: T5 O' X# A& N0 fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" E+ p6 Q* c: N# W, y, Q7 F
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& T, h' L+ M7 g3 O( \3 R* v
五.配置nginx
2 J) c  e( i4 s1 N, Y+ ^
: c! g& W! Y0 V& J在需要启用modsecurity的主机的location下面加入下面两行即可:
3 S) r5 [+ F  i6 Z, Y# O3 G1 ^, |. {4 Y! Y4 {6 Y# ?" b
ModSecurityEnabled on;  
3 R6 F, E3 F" p$ gModSecurityConfig modsecurity.conf;& Q2 [7 k- X0 }5 e3 h5 ?' B1 |- |
下面是两个示例配置,php虚拟主机:
. W5 O" F6 l5 w$ V% n
0 D! }) l3 T9 V3 M; jserver {
# M; U/ X  }/ b9 N4 f5 I      listen      80;% l* p" x1 J+ q2 a9 X
      server_name 52os.net www.52os.net;
, E7 E. i5 g! n' E4 p& L     0 k  R- d* k$ o! p4 e. d
      location ~ \.php$ {8 r5 C% w7 b; `' |6 m4 I, L) _
      ModSecurityEnabled on;  
) P; E( I6 ~' ^3 q% c% a# W* |  G      ModSecurityConfig modsecurity.conf;  Z& W- Q6 L- a; g. y" F

2 t6 Y4 Y% l7 D( r: ?* x      root /web/wordpress;
2 m1 S0 z, j# _0 F4 S. o3 v! l      index index.php index.html index.htm;' _1 K$ N( l. C) y, |1 U6 v% g
  + H/ Q* I: W& s2 ^
      fastcgi_pass   127.0.0.1:9000;5 Q9 _8 k0 w5 z8 p/ t! J' C
      fastcgi_index  index.php;
* [; j8 g3 {4 r% X2 B7 \      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 d7 N& v6 W( ~+ C
      include        fastcgi_params;  S6 t4 e5 D, [! Z1 G9 h7 C) J
      }9 A( @6 ~  M7 n3 \! @
  }  _* ?* [( l3 a3 M6 \
upstream负载均衡:8 x& m4 z2 g6 x; Y& n
# R+ z* t2 r1 u- c# g$ S: G
upstream 52os.net {+ ]" e* Z' x5 q- X. k
    server 192.168.1.100:8080;
. M  t; T' k+ _: j- `    server 192.168.1.101:8080 backup;
1 S/ A7 G& W0 ?/ q}
0 X! Q# i* x% W* h' G/ q0 \, t$ b# ]+ |& u. q$ _% q8 w' H; i
server {
5 n6 ~1 g. u; A( X( I3 clisten 80;, u  E4 ~: u8 l" |0 m
server_name 52os.net www.52os.net;
' ]* ~: \1 Z0 v4 N1 L8 F" A. ]' I4 g* d
location / {
7 s2 J' |% C7 B0 j    ModSecurityEnabled on;  
* H. [2 c$ V  T6 I7 T    ModSecurityConfig modsecurity.conf;  
. y* R  c3 r8 |2 K4 ]+ Z- x) {+ a
        proxy_pass http://online;
. X5 A$ r; w5 l& i; U: L        proxy_redirect         off;
+ ?7 [1 ^  X4 T        proxy_set_header Host $host;
6 X. t' K& I0 _; q        proxy_set_header X-Real-IP $remote_addr;! t$ O! {) M1 U  D
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" d2 Q1 _) |; v* S2 d    }
% C6 e% i- P" D( [}, T0 e# h5 @' q1 h3 d& o
六.测试
6 j  C: F! F1 l6 k- O+ J0 k( S4 p. z( q( t7 U! o: j+ q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  @) Y- O/ Y: V& B: m4 n2 T

8 o! [8 I! E( l; \<?php
1 s6 `0 }- y9 d    phpinfo();   
4 [; D. x0 ]9 w9 n4 U8 a?>, F8 E, j9 P3 g0 x! c0 c* [
在浏览器中访问:* @6 D  l- W) g* m) s& A) V' O
+ J7 [8 H) {$ q( D- [, @* O) |
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 _" c' |8 \' |5 C& U  Y6 H# Fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. q1 V8 o7 l: U3 F6 T, [3 y! o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' @) l3 X1 y: a( R3 o& y# |8 a7 Z
说明sql注入和xss已经被过滤了0 A+ Z( C& L0 j" Q1 \1 D0 Y

+ P+ F/ H$ z1 s, F七、安装过程中排错
1 E1 d* R1 v' o# \6 U8 p, }1 I1 L" ^
1.缺少APXS会报错5 o2 y) x0 B3 o) A1 v5 n

! |' t1 }' |' }9 ?6 ?# o/ t' Fconfigure: looking for Apache module support via DSO through APXS
' D3 D2 `/ [5 sconfigure: error: couldn't find APXS
* A$ m! N. S, _apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* U% l; r+ w2 `) A* }8 O解决方法:  C& |- S4 V2 a

2 a7 r  ]/ K& e% i& dyum install httpd-devel
$ `* [3 e( E% W! T2.没有pcre9 d; c  k& c+ w% j7 u

! F" S4 ~7 i: R; v# r. tconfigure: *** pcre library not found.
( x; H2 H. L8 ~: t- Oconfigure: error: pcre library is required
- O9 F; y6 F* t* a; A! F6 R解决方法:
- i0 `! P( E' H0 c# d* w0 y* b/ n8 l1 V( @& @/ w
yum install pcre pcre-devel$ V- i5 d1 e1 ^( S
3.没有libxml2: E' i- \( |7 h- Z7 H8 C

7 q; Q' d0 H/ ^' f* v
$ x/ W5 V4 R3 jconfigure: *** xml library not found.
% A- G+ B1 _% G7 C) rconfigure: error: libxml2 is required& f  p7 k6 Y3 j
解决方法:
/ T; ~) @* z0 h& `( x
: P; e( P0 I; f! O& g' |$ Zyum install  libxml2 libxml2-devel7 \/ a8 l4 c% V
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ e/ e5 E% h! P( [- Z

  b0 {4 Z* \/ _1 X7 f2 PTengine version: Tengine/2.1.0 (nginx/1.6.2)
* M0 [$ P$ h& E& Z: @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( Y* l+ x4 n2 \" ]/ O
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: u5 y+ _2 w) u: G/ G8 ?0 [- v$ U- R: K  N" o& d1 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& ?0 u/ s9 T2 T4 ^7 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, @5 ]) c7 o7 _" J- W& S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ U8 i3 n) [- o& P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& ]+ }! X+ R! O& U( X" e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"( J/ m9 Q; W' u; o2 d# L& B) c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 W/ u+ d4 d; T8 t5 c) \
解决方法,移除低版本的APR (1.3.9)& k4 l7 B! r6 a4 D' i4 t$ y# k

+ n7 W( b- y. q7 J) i+ gyum remove apr+ I  R5 k1 \5 P1 A" n4 Q4 x* z- j
5.Error.log中有: Audit log: Failed to lock global mutex
+ a6 Y3 l- F, U& A; ~( K3 m0 q. D1 t3 X# ?9 O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 X! W8 K' j& i5 D+ j+ @, p  ?/ K: |# H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 }, L( R8 n6 W5 `  M8 M解决方法:' Y- K! S8 Z$ z- ?; q5 M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: x  E2 l; L: D2 a
7 Q2 G; X1 K( X# j: b
SecAuditLogDirMode 0777/ q& K; ~0 H( k5 L; _0 p
SecAuditLogFileMode 0550
* b% |7 K( _1 [3 |: k- s3 MSecAuditLogStorageDir /var/log/modsecurity1 y0 N* b  U( A! W! r) H
SecAuditLogType Concurrent( O' }/ c. W# g7 l, w9 f
参考文章:
& E) P5 x8 y9 q) a: a' [" Thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; N) ?: C- q/ ~http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-22 14:51 , Processed in 0.069466 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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