找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13022|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 Q6 ~/ J  F6 O! ~9 L0 w
; |/ a; L  \' h# m, q一.准备工作$ i3 q& P$ \3 K3 P" O3 y

* n* ^* |: q& ^2 n' I5 L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 t+ c7 V' ~6 |. F1 d9 E  B* g0 b* Z- r. W/ @8 \' r% M$ C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 _* G) Q4 y0 D" l; W
  e5 \% y/ J% B; l/ M" R
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" @( G. T7 @' H3 L" B
8 U5 d" I1 Y; mOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ _/ \3 V( o$ k) d; [; i7 A, p5 G+ A7 `

! S" U& H3 x/ Y# ]( s4 V依赖关系:
% i, _- L; |1 a/ r3 m+ ~5 G! Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, v; H0 K* _; g* M0 r
5 }. [' k$ n  k% [! S- I( A) {7 p
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 q" {! @5 ~' l6 e: B7 k' u0 ~# A' P
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ M3 _7 ^4 P* H7 R: q) a3 I1 V

' K5 G1 _! n  }0 X7 |- P" Pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 u5 F9 p7 ], d% w二.启用standalone模块并编译
+ }( r0 u& }* m) }% G6 |% y0 c2 i, M5 X( x; B: c/ J  ^+ a9 }
下载modsecurity for nginx 解压,进入解压后目录执行:+ J. _1 [, u' r* i( b
, b$ b" }- o* z: n
./autogen.sh# }# S, B2 H# v  b6 a% j. Z
./configure --enable-standalone-module --disable-mlogc2 e& W8 D  Y, J/ ^6 m
make $ C" n. v5 F  t2 }' n5 J
三.nginx添加modsecurity模块
- N; L6 i' |4 M' K% F7 O% p8 q" }/ P! G0 k+ O1 }
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 i) [# a, o: _3 e5 Q1 o

& h) r' N; Y: S$ u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
$ h( r! {) {. w7 I: |make && make install
. X. Q, G. `, _5 c- ?四.添加规则
  C, g4 {# t) [4 }/ A& C
, i) J( ]9 _! ^* W0 ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* \8 P$ ?& G$ z# u4 Z- q* A# N! Z4 l6 {5 B5 \! \; m
1.下载OWASP规则:  k# ~6 m( N/ P, p+ A6 y3 h

% Y" `) O6 ~0 q3 ?  jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! U- R  K& @" c: y! \
4 ~; \. f* h3 K
mv owasp-modsecurity-crs /opt/tengine/conf/' {, G& v2 U, ]
' U% ~- ]9 |: E* Y8 M/ O3 c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# C5 T& ~$ u6 i+ G2.启用OWASP规则:8 G% e3 f8 P- p6 T! h: Z' O

* K2 w3 R* h2 I1 H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 _# ?. h# W; k: n, A8 q% R1 M

0 i: k! b0 H( o( `# o1 ]编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- F- X- _( [. Y4 O# ^+ o

4 ?! x( l3 M, n& f' H7 ^9 Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% B: Z" A7 m9 h7 m( D) o
; [( H6 G/ }+ B: S" p- @, D$ FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& W2 U3 o5 x( C, D3 E( \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 m" K$ P! W; j, R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 {, _2 u1 L1 X3 `7 ~2 R; k3 kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 O8 K" P( T- u0 D6 t, QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: `2 B" Z) x1 F* D. kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ S6 V2 M( J* ~* b6 W- Y  P1 w2 ^
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ @! ]9 Q! z7 r
五.配置nginx
" g* I, h  h) a% c+ Z
  V% o' _. w0 s6 Z在需要启用modsecurity的主机的location下面加入下面两行即可:
# @; v  ?3 Y+ N
8 i" b/ t& a( OModSecurityEnabled on;  4 i( i* w. a* j0 v
ModSecurityConfig modsecurity.conf;
) b- a, g. W5 q2 k5 K下面是两个示例配置,php虚拟主机:0 X1 g, I9 c5 ?4 g( B% |

' H  Y# f& }# X( U. R" b& p! i" sserver {
3 {0 P' Y3 C3 x; u6 ?2 O6 O  Q      listen      80;  N1 Y' C. U' J
      server_name 52os.net www.52os.net;0 z+ M; {" \: J
     . A; R$ R$ t, I9 z, D" _
      location ~ \.php$ {9 X8 Q! S: ?/ H1 s
      ModSecurityEnabled on;  * y1 i5 v6 H/ ~. S) _6 _6 Y
      ModSecurityConfig modsecurity.conf;: R9 K7 I' G' t" ?

2 j% k& x2 r/ O) T1 S  |! k. [+ M      root /web/wordpress;
2 E1 G9 j+ E8 k8 r9 o! i/ E- K      index index.php index.html index.htm;8 V7 b7 V, L: O/ b. ]8 e
  2 S* z' }8 R0 ^/ m
      fastcgi_pass   127.0.0.1:9000;" a2 Z$ L$ b- d$ I( j# @, i, d
      fastcgi_index  index.php;
* |; W  D% a! Y# g2 E      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 x; \( }5 X9 W& Y" f      include        fastcgi_params;6 E2 M! q- I1 C
      }5 @" L# p8 U5 u# A, D- x
  }
3 a) Y, L+ u3 a/ ]' }' ]* N$ Fupstream负载均衡:% X9 y7 A) |9 k$ u

2 J( m$ d2 q* `0 L, Vupstream 52os.net {- \' w4 Q3 o5 H8 F
    server 192.168.1.100:8080;* {) M" T- I3 a7 H
    server 192.168.1.101:8080 backup;
) j" B" ?  ]4 o/ o, L% W}
& S% ]! H4 x4 z' k, W9 Z' K1 ~2 i$ m# [3 B# B
server {
! c  Y5 ]- L* {; I* h/ i4 p* [5 ^4 z. Rlisten 80;
' m+ \( u  P1 ]+ @4 Aserver_name 52os.net www.52os.net;: n2 @  r) D6 [  S+ X

1 `! g& q/ X! c% B+ H) V) v+ }location / {: W6 R( k- q9 S% [
    ModSecurityEnabled on;  . P* o; |7 ?- o$ }, i" y  N
    ModSecurityConfig modsecurity.conf;  4 U' v1 ~" z8 K7 J4 Y" B6 f% U4 @# H
; X/ A! w8 P; c' n5 O3 |# L
        proxy_pass http://online;) L8 a, ^& _$ j6 E. M
        proxy_redirect         off;! t8 U3 R! u4 o; R) l
        proxy_set_header Host $host;
2 m/ ~+ c2 `3 ^; x7 l        proxy_set_header X-Real-IP $remote_addr;
1 R3 u4 S, v& i5 f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;+ ^2 M$ `( ^1 ^9 [
    }
; T7 L  Z, @' d( I' a}; \$ {7 S& e  u" r4 Z6 n, B5 k
六.测试
. t; M5 C; F4 Q7 @
! B5 B+ @: h2 O2 V" A我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 o1 }' R8 _+ C' w/ H
8 r# }5 y8 J6 J6 [( S6 u6 g7 {7 ~
<?php: \. C$ a  Z" o! L" h
    phpinfo();   
) t2 r# G  E" V, o# U, n?>' w# h* u- l" `) Z6 }0 Q: v* J/ t
在浏览器中访问:
; u% x. |" f# g. P' `
) B4 d0 w0 I% A) H& `http://www.52os.net/phpinfo.php?id=1 正常显示。
" z, ?9 d7 R5 m- e2 I8 khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ R: z  @4 e2 x1 ?, ?http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& M( o; z* |  d说明sql注入和xss已经被过滤了
' k' f) C( m) [* v9 P+ A3 D0 B, R( G! N3 W
七、安装过程中排错
6 I' N/ g4 {( X+ }! j" B% ^7 v
. F6 I& e6 R$ q  W) g1.缺少APXS会报错5 R( Y! U# B: r. u
+ k! L$ X8 r! I6 G
configure: looking for Apache module support via DSO through APXS
. X/ l& w" v" e2 W+ ^2 e/ [configure: error: couldn't find APXS& N4 f0 D  P+ C# A# R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- |% v# r3 L8 n! t1 X: V) h$ e% D, D
解决方法:5 @2 O+ u, ]" @7 l' u; m, i

0 a1 n5 O- m, S' [; X5 T, ?yum install httpd-devel
' k' O4 Z4 H( Q1 @# i$ Z1 K2.没有pcre
* F! B4 k2 Q% f0 k: x6 q6 f1 g% Y; L0 Q, A, D: D' t2 M
configure: *** pcre library not found.2 _# n: U& D/ }
configure: error: pcre library is required) x- ^6 C5 Y- Z1 j& o+ K( T
解决方法:
) q9 |7 ~5 B/ H& r; ]
# Q; I% I, C$ v, i6 A3 ^yum install pcre pcre-devel
  a; K4 \5 N* @! S7 H# j& k8 n0 q8 Q3.没有libxml2
( S8 `0 D+ Z8 B* q' c7 A( ?- H
2 b. d# B5 Z6 e) Q4 T5 j: m. k8 ?6 ~! |  q8 d
configure: *** xml library not found.
: Z. e& E2 e  |- T4 ^0 Kconfigure: error: libxml2 is required: `- O8 L) D% v  q& W
解决方法:) V3 _7 D* ^! t( e$ j3 ]
* z  m4 j; t; T- I
yum install  libxml2 libxml2-devel1 V1 g+ @1 D6 I- \( h, v
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! T. U, O. Q+ s. ^% J: c9 N+ E' Z3 M+ w( r8 [
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
6 _- u7 v! t5 b; t: Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 p1 n& m0 E5 `6 \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) U: ~/ ^& O) v7 `$ A& ~; v; D6 v9 J6 m4 c/ m, ?2 {2 J% w  K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( j. F4 }# W$ K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! a$ s$ O8 }( n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ |2 c( ?; M4 T2 \: b) b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! N- `" X4 c  Z+ W3 E1 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  ]: O; o  W6 B0 \7 E8 @6 x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; u2 B( J2 J, a7 P/ }解决方法,移除低版本的APR (1.3.9), P) \# R$ z6 p+ @

/ `* f0 ?+ K7 X1 B4 ^+ Dyum remove apr
4 L7 [$ p& R3 {( m$ G3 T5.Error.log中有: Audit log: Failed to lock global mutex
9 n) C* E' z' n# k- Q8 j% F  \
% L# ?/ q* Z+ S2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
  L5 ~4 _2 D$ r: k; sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 p) d( j0 u% h/ `% s3 {# f
解决方法:4 T: v4 p& J2 P  O( J0 B1 B" |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! B. O2 D' u5 p+ G. U! [3 M5 t2 t: Y' r4 g- Z, e
SecAuditLogDirMode 0777% K5 s/ u. P3 r8 k
SecAuditLogFileMode 0550
8 j, ~. d+ M. w. d3 s, K# uSecAuditLogStorageDir /var/log/modsecurity/ P% A; j5 @& K" Z  W: R  s( J+ r
SecAuditLogType Concurrent
9 z+ |/ W# V  W, r  S0 H* e' ?( h参考文章:
7 ?+ p. ^& f3 K7 {0 Z+ a' @https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! e; |! k/ ]5 ?; s
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-4 18:25 , Processed in 0.058780 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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