找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12885|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! V' d( M6 f/ ~6 P+ y; \
! w9 E& L9 p# H
一.准备工作
. _' X5 {( A3 q5 r0 V4 u8 x* o  Z8 s, j6 o1 O  G, u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) q$ B: O: \& T# j0 W9 p+ F! V9 c" W0 J! H
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 h: ]; H& F' v9 e3 V
& \/ C! j0 K/ W: T" j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 y) f" I" J$ X

8 q3 y6 o: M, R8 jOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  |0 o6 ?, t: X
  D7 M7 o: G  ~; F3 {
依赖关系:
& t3 `. J/ r6 _( n7 O% d& J3 Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. _) {2 L1 t2 F; J- Z
5 B5 i: O6 O5 M. L
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel: C4 c0 @8 ?$ J( D/ P) l
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! M, i( }/ J7 e! P$ q* A+ V& Y, H/ @4 f& }( g) o7 x' O6 o
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# g3 [: M7 C3 K& [
二.启用standalone模块并编译
' `5 K2 E9 _, e2 @* a
$ O, Q: M0 n; W( d1 t7 U" `下载modsecurity for nginx 解压,进入解压后目录执行:, _) ^& l& q8 Q  a/ R" v0 g
! k- e9 p) \8 O- P1 a
./autogen.sh, z0 Q9 _- T: V8 R
./configure --enable-standalone-module --disable-mlogc: T0 \) _" F- G# j  D7 w. t( Y
make , `0 p) w( B& x3 @# _& m+ F8 A5 E8 `
三.nginx添加modsecurity模块
2 K- b1 r7 s, c4 f. Y: g' v4 F. L) H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- D& V$ ?- U/ c- Q- x9 J( D  z4 Z# h4 ]+ _% `! w# }
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
( u9 v" C. e, D) _5 ymake && make install
- ~4 ]. }4 R3 m四.添加规则2 p4 N7 T  C- w) c+ f

6 P9 V; X4 R2 Y, D- s8 Q! ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 R+ j4 J. _# Z7 j6 x4 K" }- K1 P9 w& a5 j3 q
1.下载OWASP规则:/ A: a' f3 D! r8 s' Z
( [: e; g. s3 W8 v9 i0 g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" i8 T8 ?; }, Q0 e2 t1 P# t) k/ z" G2 q( v" n- F
mv owasp-modsecurity-crs /opt/tengine/conf/) Z8 Y/ {# F2 ^" m+ n2 J
1 q5 r7 V, t% V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( M  t7 {/ h+ i0 x7 p2.启用OWASP规则:; `4 V: Q. b" q3 {7 x; }7 v- I

' ^4 h, I' Z8 O/ Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 [! l; H0 b6 P

4 j, L' }0 H# ]编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 d! [* _& y' L; V3 x8 p% s; `' r! C
, G2 @: P( S0 O  `& W/ j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; M2 w2 M. N1 r- t+ _. m  `/ }# ~: S# W! Q; C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, _+ |; |# u! t0 Z% G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; F0 P( Y# x6 B# d/ C0 I+ Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' k9 Y3 j+ C$ l" z9 gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! i& f) N. O! u6 d/ A  w  G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- Y8 T( D6 z; y5 |: u4 U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 C0 ^+ W9 s! [" j- _7 N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 B5 M7 P7 M  r2 u8 s4 O& c3 |3 S五.配置nginx7 B& |. F% I9 }) i  W# q

  A: u: x* s- O) G1 h6 b- [在需要启用modsecurity的主机的location下面加入下面两行即可:: M; c. @0 g/ Z! Y1 l+ H8 j

+ P8 C3 `/ u( W/ H2 r* b6 R# p! g, tModSecurityEnabled on;  
& N! s( Q0 h- V& kModSecurityConfig modsecurity.conf;
$ c9 N9 L) \7 q7 x8 s1 b下面是两个示例配置,php虚拟主机:4 F. z/ f( U' L+ I
. ^) ~9 b3 E3 m, O* u2 I
server {
& a  y7 t! |  @! A& i      listen      80;. Y/ V/ Z% X" m% D- u+ R- ]
      server_name 52os.net www.52os.net;: h2 T( ~6 S& a% Y7 B# ]/ Z: ^
     : a9 O; i0 M! S
      location ~ \.php$ {
7 W, D2 H: e/ T5 r9 M7 ^) G      ModSecurityEnabled on;  # I( ?3 q- k. l
      ModSecurityConfig modsecurity.conf;
: _7 r* q0 Q/ e5 `: Y& q, h2 |& R6 w: s
      root /web/wordpress;9 `% b- U% Q: H5 y3 d' n
      index index.php index.html index.htm;
0 |- b3 e) _' i5 a" |  
( |3 X/ c% J* O. C      fastcgi_pass   127.0.0.1:9000;
4 ~5 I2 W/ |# \  y" ^/ R5 J      fastcgi_index  index.php;. m" T& w6 s7 K- G5 o
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 F/ q8 Q6 ~. F; E. @
      include        fastcgi_params;2 M9 B: D$ v& Z9 z' l  n1 v
      }$ s: L7 [! N$ h; ]
  }
: H: T3 l* j! t* U/ @1 C% S7 `2 V, Uupstream负载均衡:3 h+ ], {% P4 [+ c

6 X7 Z2 p1 R) Cupstream 52os.net {
( Z# b4 y1 g4 @: l    server 192.168.1.100:8080;
6 |' x; s, |: X0 E* `/ l% T    server 192.168.1.101:8080 backup;
; u5 Z/ O! I' [5 X}
! n1 r5 C( Y, l: `/ P+ C" q; W8 v. ]; Q% b# k) `2 m
server {
6 o0 ~& `( u5 O1 @- `listen 80;4 e8 ^& ^, z+ f+ N  ?. Y: `+ J
server_name 52os.net www.52os.net;
8 y3 |5 [: o  J# K) U+ z* T6 P
" S4 L. ?* ^! ?- D3 {# Z- _5 i1 Clocation / {
; }# X' n% ?+ M) m% Q6 j    ModSecurityEnabled on;  
7 j# f1 D5 [: h: I4 x/ q. j    ModSecurityConfig modsecurity.conf;  
4 o6 ^% Q0 X* |5 Y4 j8 e5 D
3 S* B- L5 u  o2 i" H        proxy_pass http://online;
' b- S# D1 s2 k3 m" C        proxy_redirect         off;
3 L) ?# p. y& V+ |        proxy_set_header Host $host;- B  T' L  G8 X' z6 j+ w
        proxy_set_header X-Real-IP $remote_addr;
8 I3 O$ L$ C) Y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: G0 @1 p/ `, I9 Q
    }! E  Y2 H7 _6 p" i! l' l" ]4 c
}
( K7 }6 }! q6 L: e" v六.测试
! V: I' ]+ n# ?" E: Q
# r! }! r4 X' x. }7 I/ g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ K8 A/ n' R; ~' B) d
' O" P* I! W% Q# ~<?php( Y, [: A* ?  ?5 I
    phpinfo();   
( A4 W# o' e  w( b6 T( C?>
; _. j% D, \; [在浏览器中访问:  f# V- N# X# _1 r4 ?+ ~
& M, ]4 J. j1 l2 `% G/ U( b
http://www.52os.net/phpinfo.php?id=1 正常显示。
: n* h0 T- v: i* dhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。: X$ Q, F7 I; |
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 F; R* t- [- m' @: X& A
说明sql注入和xss已经被过滤了: s& B* j+ j0 Q. r  ^: r
) V4 j) G8 U$ D4 Y5 ~
七、安装过程中排错
8 x( K5 D1 E. \$ }9 P) M2 ^
% j* v# A2 I9 W1.缺少APXS会报错3 X0 K9 g4 y* Z
5 ?6 P% l6 c: ~: j0 k  ~- a
configure: looking for Apache module support via DSO through APXS
7 t5 A! R# e) K# r  Cconfigure: error: couldn't find APXS
. K! K: }6 J3 k) Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# L( J8 l8 Q# t; y) G  K解决方法:- Q: H! h) y6 y; j) Z$ m

1 c# O$ W( M7 p. jyum install httpd-devel5 t1 j7 Q7 K7 x. ^" v2 Y9 I! S
2.没有pcre/ X1 K: ]6 X" j3 G. d2 E2 M

% V: O, I) T7 Y1 yconfigure: *** pcre library not found." G) v5 z, L( [4 u8 O
configure: error: pcre library is required' q1 W' G  c$ v' ^% z
解决方法:& P8 y- U/ U9 }. {  g

9 I5 p+ X! U* V: \3 n1 zyum install pcre pcre-devel
$ G0 K* X/ t) [" i3.没有libxml2: v! B3 S  `+ ^6 z: ?$ A/ z4 E$ {
  s8 s( B7 \3 X6 ?" \6 i/ ~0 J

3 y4 k& b4 _1 h, h; tconfigure: *** xml library not found.
4 s: S, C; r0 A% _" S; uconfigure: error: libxml2 is required% V+ A) f6 }8 O: S2 \
解决方法:9 p+ v! O/ _' F" b
/ s% U: i/ [0 N/ c  N+ b
yum install  libxml2 libxml2-devel
' c2 Z+ K8 d0 z: A' y# i4.执行 /opt/tengine/sbin/nginx -m 时有警告8 f, u% n1 S6 v0 |- u
/ s' }2 S2 L4 o3 F
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 R$ n$ X% Z/ e4 I$ c
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ e7 f! h, O5 |+ S( S' D; j. a
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 d% g4 E( U: B! x' B) i0 Z, f! n% L
; y; ]& J. R6 x( L; y+ g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- w9 z. N& r+ r) `; [0 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"  ~1 {+ I( \4 J% _5 U
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 F3 h1 m0 j  M* t3 w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 e8 w3 Z( R$ f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) O6 |* X' W! }5 S& A2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% e' V& G+ ]4 d9 ?7 X解决方法,移除低版本的APR (1.3.9)
' s2 ], ?9 ^# X
% c6 |" G  o5 C: K6 vyum remove apr
) T, _9 a  B9 K) g% z- W5 ~5.Error.log中有: Audit log: Failed to lock global mutex
5 W5 u3 x5 u( X/ F4 X  a1 _' ^/ {" U- W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 E  d( e( g: `; _, ~3 w$ P3 p6 J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 a- `5 A$ C1 B/ n$ q  x5 K
解决方法:
% e' g7 k& j) y( y* z7 Q) H编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 T- B8 {* g# F# R& O# R8 v' h
6 a8 A3 r6 \$ ?- P6 w1 LSecAuditLogDirMode 0777% Z- q8 H- }9 r' y. `% a5 P
SecAuditLogFileMode 05502 k4 y# F* D' q% v9 K, i! ~
SecAuditLogStorageDir /var/log/modsecurity
% X! `, R1 \$ d+ f+ G1 q; k; V. ]SecAuditLogType Concurrent
+ v4 `, ^3 c( U$ i参考文章:
$ V) V) m+ g5 ]& ^% Yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' f5 X. a# j' f4 F# e+ c/ L& {! jhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-22 00:03 , Processed in 0.076453 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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