找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11308|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ o& X: P5 k& r% x" H) J* W
3 s; v# h. H( Y6 w  `; h/ @7 }' R3 k
一.准备工作7 Q$ P9 O+ F6 j

9 @6 C2 R( k! Y" d/ e/ B系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' x% J- q- m  [1 j8 w( Z
1 k* o1 S+ f5 [7 g
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 k$ S9 u! y9 f4 {, x
) j8 Y+ n1 k7 ^3 Y3 t
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 `: ]- P/ ~/ r1 X5 r# S

  @* J8 g0 l3 d  `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' F$ X/ s! X0 U* p- Z
; O5 G3 m# Q/ o9 E0 s* i# o, E依赖关系:7 P# o; c% B# z& W, {) Y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: I5 u- J8 A' t  q% _
/ q; I% `* V" ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
  ?& g6 Z# s5 t$ t7 P! Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr( {: ]% ]9 p! E" L  @0 @  n

0 [3 t  ~' c  G) ]4 e! S7 ^yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ F3 w6 j& i, n- h2 Q  F* l二.启用standalone模块并编译
; v- X% p. `5 B6 L' a4 T  u1 E% n: w7 C& Y8 b
下载modsecurity for nginx 解压,进入解压后目录执行:
: C% K9 D: p* ^. |# }8 \# B8 K% x  [7 _- R8 T0 u
./autogen.sh
$ I; i: V) F! H" k' T( Y./configure --enable-standalone-module --disable-mlogc; s- a9 v$ o; ]1 P: F3 v+ P$ R" h3 S
make 7 [) H+ x1 C. Z6 }' g
三.nginx添加modsecurity模块
7 d) I; e% K/ {& A0 E
/ G6 L- b6 K5 V; {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& C! m6 _- F: h3 M" y- Z6 M, A1 c# e! y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* M& K- K$ Y8 U  T9 A5 u
make && make install
) c1 {! a! T. |四.添加规则
6 l# A2 z7 U/ H" ^8 D  e
, E. ?0 t4 \, `- smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 f! j/ H/ e& P' H, M5 r2 o
1 x) B: T, W! ^' |* F8 T
1.下载OWASP规则:" J& _5 _. e  D! `  E8 G- v  Z% m
2 Q7 h! H. i) ^/ W! g. J% {& p
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* T: P' M9 Y) _* u4 L% J4 M5 Z0 z) k3 v0 O, h8 s7 N
mv owasp-modsecurity-crs /opt/tengine/conf/
- b7 l& V1 H4 h3 l' J# ?+ H7 W  i! w; {6 G0 b& u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, X  i$ t# e, |" J$ L5 K# N) _2.启用OWASP规则:' w$ a3 e- x, ]9 S/ s! O$ J

' m" ~) @/ K+ n. H+ C0 Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ E" Z' E9 y9 @1 s" [. G

/ j6 C' K# q# N% Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on  X  ]& R0 ^+ ]8 i/ D
0 Q" H3 x9 I  w; c7 @8 Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' p& V" p* I3 }4 D6 i
! t1 r( a( z. X- j" w9 g6 U4 jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  S6 _, M. {4 @  KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; Q) V8 t# ~( P: {7 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 c+ T* ~6 c6 i0 Q* Q) l5 IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ }, x9 g5 m, m4 P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 o( M8 u' N& NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  x1 b  M. t6 @% v) Q0 a! A  mInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( r6 R' J- {( i9 G; Q/ X五.配置nginx
, J$ Z5 N6 I: U6 R
* S2 S( p1 A0 x: T# B在需要启用modsecurity的主机的location下面加入下面两行即可:6 D* }$ a: n, `+ C
- ~! _& o* D7 F& `) d5 x- n: a9 T
ModSecurityEnabled on;  ' {* L8 E! m0 J5 M7 X: F
ModSecurityConfig modsecurity.conf;5 F7 P" i8 Q/ v" _
下面是两个示例配置,php虚拟主机:
. o1 B7 x& k: \4 g  Z: A8 B. P$ F0 l1 _  G
server {
6 y! C# G( O4 W+ n9 C7 C' R: Y$ o/ P      listen      80;
, p# \: l( y  L6 S5 q      server_name 52os.net www.52os.net;
; s8 g6 Y9 M0 }* a- |* C       b) ^$ v, X% d" ^' u/ T; m8 _: o' P
      location ~ \.php$ {1 ?$ m& i0 M& }/ X
      ModSecurityEnabled on;  . n0 Q1 _% P: H5 ~& [8 P& X7 [
      ModSecurityConfig modsecurity.conf;8 G9 O4 T# h0 h" g# g9 h2 w

" Q+ @8 l, i2 N( m      root /web/wordpress;
. k# g3 ]0 H+ L      index index.php index.html index.htm;
1 S3 d" E( E. J; y  5 {8 a. \  _# ?
      fastcgi_pass   127.0.0.1:9000;) g+ p4 V4 a) O( D; o8 ~
      fastcgi_index  index.php;# K4 w: \" S0 P2 x7 p- G
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
1 D+ \% P1 }. v$ u) S: D/ b7 o- I      include        fastcgi_params;- S/ G% \4 |8 P' N3 ]6 I0 H+ o
      }
8 Y4 K4 M# ~; \  }
# e/ b" x* C; Xupstream负载均衡:
7 J7 l9 _- ^5 m: O* i, H! d: I' p3 U$ q; }
upstream 52os.net {' ^! C, Y* X: C
    server 192.168.1.100:8080;
. }+ j! v8 e: X/ g    server 192.168.1.101:8080 backup;
0 u6 V  }. ~1 Q* |2 r; P2 [}4 Q3 k: n+ r7 |
& P/ W! S' J7 Y! u) P
server {
3 X: |  f. N8 {7 H3 alisten 80;8 c2 _3 l! k; H: j  L: t3 m
server_name 52os.net www.52os.net;
8 \; X+ Y/ B8 D, i) B+ S0 e3 T) [/ [' `3 @8 R+ Q( |
location / {9 ~+ B7 ^/ j/ L% T  c5 R- G4 Y  Y
    ModSecurityEnabled on;  
- a' L5 ]0 E( W6 H, V" r    ModSecurityConfig modsecurity.conf;  5 k$ ]+ T4 `! O# N: G

3 _/ C; J0 }4 A6 j( |        proxy_pass http://online;
: r1 @: n) e) Z; ?4 p, E! Y& A% m. k        proxy_redirect         off;! M% b9 T& N7 V2 P
        proxy_set_header Host $host;
' j9 W/ q6 _5 [% b6 [# A        proxy_set_header X-Real-IP $remote_addr;, s7 ^0 [% q$ Y  J2 Z% f
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& v: _; p: X7 n  l- @
    }" l0 ~9 v. {- r6 F0 e  V
}
; P! N" C/ b" ~. m  m, L, F6 Z六.测试( b* |& d0 H3 N" i# n/ x& w
& }  k1 [: T* R" I9 e. J, @0 d1 C
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& z  q7 `4 t( U7 y& g' s- C
4 E/ O6 Z4 O( d+ b
<?php/ n: q2 ~: ]5 \* Y( s
    phpinfo();   
2 F8 w8 `& n" T9 f1 E, K+ E& F+ e8 ^?>
- `& w3 s/ h' {7 E- y2 M在浏览器中访问:. R. h# ~# t- T1 A. r1 f- Y9 G
8 f* _- c; W5 F$ P9 u
http://www.52os.net/phpinfo.php?id=1 正常显示。
; O7 q5 ]' u0 T* z6 ^http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 z  m) T8 Q# U* e# d: Z4 {
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。% B+ {  ?2 m  B& j4 r2 H! q
说明sql注入和xss已经被过滤了
% L: o  l& v/ O1 r& b* `7 X& k' U3 m* o
七、安装过程中排错
( l1 D$ E3 J' U' x2 ]9 y5 G0 G# ^- E; f% \
1.缺少APXS会报错
; \# w/ Y; i3 a! o- H' D* z$ ^
3 r0 J) U% Z, g  s$ kconfigure: looking for Apache module support via DSO through APXS& A* p5 M: j8 j# Y5 {' l
configure: error: couldn't find APXS
4 _  V( w) |2 M5 P" u. c1 U7 Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 v5 c7 W( g) _
解决方法:0 q# m* z1 f: P! s7 c4 [! f9 U8 G8 [
, o$ B/ C, }9 T+ l  ~
yum install httpd-devel
% H; `0 p& Q9 B$ ~2.没有pcre
& T  e( i4 \3 w2 s0 b
0 U3 {/ V. T7 Lconfigure: *** pcre library not found.
8 {" [- _: _1 H' I* {5 hconfigure: error: pcre library is required' @9 m* d+ C! l8 ^' C
解决方法:, O+ U! g( c3 c+ F- g$ x

6 I$ d6 D4 i" H8 X( E" ]. Z9 [1 ryum install pcre pcre-devel% @3 U9 x: Q  ?7 b" @4 O
3.没有libxml2
; J' b- q. R. U+ o/ p8 K8 V
4 G8 l7 @1 A" {6 u7 g$ S2 Z
& O3 B- \( y0 m. o% g4 ~configure: *** xml library not found.
% h8 K* k! D. w% ^4 u+ C# |- sconfigure: error: libxml2 is required
; j/ g5 i. Y& z3 _) X解决方法:) ?5 q  N6 \: O3 z

% \9 \! l) x" X2 l& M3 eyum install  libxml2 libxml2-devel
& C3 c& X6 ^5 K0 M1 K3 ]4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 B# m& M# s8 q* i! U6 h2 n  k% B% @  `
# L9 I% Y$ Q/ YTengine version: Tengine/2.1.0 (nginx/1.6.2)
: R" h% W2 r; H9 w% @! o: Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  T  f5 N: ?. ~& O) b7 Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, I. }( J) p- H
! \) Y- o2 j9 r. P9 R# g0 e" |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 C1 }% _! X* O! G# s* }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"" O, W) Q# [* l4 w3 s6 P
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 }, t, m$ p9 [: Q+ J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' u2 {' b( n# t: l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 ~) Y2 l9 Q, p5 C4 X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ j$ @, I2 i% d+ H' W. n
解决方法,移除低版本的APR (1.3.9)
- A/ E" D4 I) ^8 x, {+ b- A, g7 k! q8 ^% [- D
yum remove apr5 m$ U0 R5 l4 T: u) F* S
5.Error.log中有: Audit log: Failed to lock global mutex
  l! n. S- }0 x; p% T% S$ Q9 c( |# `/ |2 z2 x: Y% l9 i" ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ' T! X7 D) J% c/ k9 T
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- o/ K7 b. q5 {解决方法:$ ~8 x1 c7 S3 ~  T# G! |( y& S, k
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! ~7 M2 `- P7 o/ F! Q$ ?, C
7 m( |  W( g+ y7 H& k. K/ fSecAuditLogDirMode 0777+ n. c6 `, N/ ^+ S# g
SecAuditLogFileMode 0550" l! c: Z6 l& O1 _' L
SecAuditLogStorageDir /var/log/modsecurity
7 \) Z3 Q8 X0 I- A1 d& m# u( ^SecAuditLogType Concurrent5 `2 ]6 m. C! Q! U
参考文章:
5 g/ u% g* M$ V! |3 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  P! i% ~8 J9 z0 e- Z8 \" ]
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-8 04:53 , Processed in 0.069266 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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