找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13179|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- G4 y' O9 d) G5 M) Z3 h
' t; f# Q/ f- Q$ I% v
一.准备工作* H4 X1 L" \& t; Z5 z

$ Y0 P) J% [' _. d& r系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 e+ }, W1 l. m9 t' L9 l
) o4 M+ d; ~( r3 l7 A+ Z, f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 W/ D. G- r2 j# _# f+ P5 K% D9 @3 I, q* q: x2 }* W
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# k& @) D: \. O" P& c  K1 K5 z& `/ v$ p" \& ?& x3 i
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 }& W- c# t. p! l8 u2 G
: N! u0 ?, O5 j, Z8 n依赖关系:! w. G  u  Q$ e) d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ J  N1 N* {3 A+ e$ v- L. c7 n  ?
1 K; n% b( ]( Nyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 [) a+ z! Y  i1 _2 W" C3 R1 |modsecurty依赖的包:pcre httpd-devel libxml2 apr& H( u9 p' A. E! t, {

* l; @: g3 ?) t) X% {) P/ _yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel: u+ @; r6 ?. z1 m% `4 @$ ?3 L/ j
二.启用standalone模块并编译
- o$ l. J& c; F5 r
& @- Y9 R  _0 f& U下载modsecurity for nginx 解压,进入解压后目录执行:
9 B# ?. T7 S9 P" q' c9 k5 b, G$ [( j* B
./autogen.sh$ X8 U  ^: s* `
./configure --enable-standalone-module --disable-mlogc
6 W# f6 T& C5 w0 K' g% umake
4 A6 X4 o; R! e0 o三.nginx添加modsecurity模块
8 e: i5 K$ a6 a3 y9 h8 ?5 H9 E! E
7 Y: \$ K- ~  r" a! E! C5 E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( G4 [* N6 H* k6 S' {/ W, S
% p  O1 z# G8 }2 ~! _! Y& {7 a0 ]! |./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ e% z/ K  I1 ~( R- ~0 Q9 e; y
make && make install% Q9 {. b" E7 j& [4 C
四.添加规则
0 n- F, q6 y8 [- D
0 f/ `, ~5 W$ Z6 X" o) K1 w1 g3 A6 Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
  d* S) w/ K: Z
; A. _) O; q( ^. x" j; u. }: k1.下载OWASP规则:* M/ T& `3 n& I( {/ I' X5 v" C+ U
+ h$ C8 G8 g9 L
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 p6 [3 B' S  C3 x+ U* e) F' l5 j6 S% V
mv owasp-modsecurity-crs /opt/tengine/conf/
  x6 F% c5 C- e9 Z* j( c' V' I2 e3 o( F% f1 V: U# ~+ t. Z, Q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. F, q6 j: d; h8 u
2.启用OWASP规则:- e$ X/ @- b8 h$ v

+ Y: C. c! q1 }$ c* O/ ?) S复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' I2 j0 o  i, I2 u$ I

! b0 {, V; n6 a编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% h( s/ R" h* V0 O2 y0 o
/ a3 K0 q1 o( A& Y$ K. A7 n
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' {9 y1 ~: A" s% q# j! y3 C+ ^/ G8 |+ x0 B7 T. }0 G, G, T
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: a) t- M- {% A7 L# m8 m: mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 _# s4 g4 e' a1 h' _8 G& ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! F7 I2 m& w$ {# b5 [( f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 r$ n6 a  Y6 |% S2 m. yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 \1 D& |% @3 V& V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' l8 i# I2 q( j$ ZInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 {+ t" A1 q' T' G" Q5 b% O五.配置nginx4 J; D1 `5 u$ K& y8 f1 W' o

$ F0 l2 n4 `+ f: L' w在需要启用modsecurity的主机的location下面加入下面两行即可:/ q1 I0 t3 |/ V) v  Q

. ~% a4 \4 F0 K$ S  VModSecurityEnabled on;  + k4 W1 o9 J9 i5 F) S- U: r
ModSecurityConfig modsecurity.conf;
) r5 a" |$ c& W9 p下面是两个示例配置,php虚拟主机:
- @$ X; J0 V4 \1 D! n# b4 H* f& a( x: a; \: M& a( r
server {% E4 N, B5 c) V% D) ~0 w6 X
      listen      80;" K8 h& Y. t2 R  a! Q, v4 R
      server_name 52os.net www.52os.net;% V6 `3 U7 K2 d+ V3 Q4 m
       W4 a" g7 F! n0 \
      location ~ \.php$ {% G' s- }! w5 P9 R: L
      ModSecurityEnabled on;  
( o. L' U5 n1 G' O8 Z0 u: H0 F# j      ModSecurityConfig modsecurity.conf;' [+ k9 ~+ r  b. R" Q$ D0 a* T: W

3 P+ m, z5 U$ r$ E      root /web/wordpress;$ D" g! q6 d) O2 d  V
      index index.php index.html index.htm;8 l; y. n& ?( |3 u  p
  4 z* J) K% t" ~9 p) R+ x/ l+ K( K
      fastcgi_pass   127.0.0.1:9000;
$ F  }6 z" N# g$ T( o      fastcgi_index  index.php;8 ]' U8 ?/ l6 F* _4 w& R% d& G. y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. T; W4 }! U/ s' O) E0 n      include        fastcgi_params;- J; s+ Z2 o# r( [
      }1 k' }2 [$ n8 ]3 |. ~  R2 F: ^
  }% `2 ]8 X6 T/ j0 w" r
upstream负载均衡:
+ N, q! ?# u5 T" R* i; G
. Y0 d& X- y* V+ p. x+ Gupstream 52os.net {
& z+ n, N7 @! Q5 v1 u' _0 T    server 192.168.1.100:8080;1 p: O* G, ^4 Z2 J3 Y1 k) W/ w8 ~: J/ k
    server 192.168.1.101:8080 backup;/ g, i, H6 A" A9 ?
}& k+ M6 h8 \2 Z5 B
; A' o8 g/ _: P7 h& \# ^
server {$ _$ h/ p! r. f1 z5 c' R# w; w
listen 80;
: e- o( ]3 h6 G3 Y  _server_name 52os.net www.52os.net;
& h( b) t0 h4 A& h. \) Y# u
5 Q7 y  l" s# D& @- Mlocation / {
0 e3 Q9 F: s" |* R    ModSecurityEnabled on;  , b$ X  Y4 z5 R/ O/ ^5 l3 V8 H
    ModSecurityConfig modsecurity.conf;  
% x: i  X/ Y; |# x& L) x0 F; w$ ^( P( p  C8 |; ?
        proxy_pass http://online;
& x. s5 O9 Q0 d        proxy_redirect         off;  ], ?4 Q4 ?8 \  z
        proxy_set_header Host $host;
. E8 X  _. q$ A% z        proxy_set_header X-Real-IP $remote_addr;
( d" s) K- q) t/ Y+ t8 M1 i/ ~' J        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
0 o1 b; Q; R$ i2 C" j* u' _, m    }
# r7 y$ w2 x; E1 G- t* Q}" x& v) s: G! T- u& P& C4 m1 p5 |$ b
六.测试
. V6 Y9 N& g* R7 C0 S, I9 _8 N
5 n9 {' p; j+ K$ `* i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! F9 l6 i% F2 j3 z) ^

1 Y8 v# c3 Z& y( ?2 q<?php1 S1 F. Q0 k9 @% y$ v
    phpinfo();   
8 @0 {+ v$ Q2 L?>
+ ^  `" X- k) h5 O" B& O) K在浏览器中访问:7 @8 E0 i5 J8 O& ~' h3 M* \

* R' [5 n) d9 ~) Q- Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。- d' a. [& n$ y* L3 r
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。( V! z* z% ~, p9 }: t- I# w
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ T9 d, _# ]  E说明sql注入和xss已经被过滤了# t. d* }5 q& s1 [; v( v

  E" U1 J, L) X2 f+ u& U七、安装过程中排错
  V  E( ~/ U2 N6 Q& P/ d! u8 U) w% }$ p+ Q1 ]
1.缺少APXS会报错
2 a) ^/ E* Q) v+ V  s0 f
  w/ D4 ]0 X4 t* n, o( m) k/ _configure: looking for Apache module support via DSO through APXS
' j' o- z+ e5 H. |& X) cconfigure: error: couldn't find APXS
: ?/ Z3 `9 [. u. d) |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 G0 h$ g7 _9 b' `3 p" ^
解决方法:# d) s7 {6 E# q! c% P( Y
! `* ]8 E. I( Z8 N' {" T
yum install httpd-devel# k" S, x3 o" I  d4 M) Y
2.没有pcre3 |: D! [" }8 Z2 m  z. L+ C

$ j& ~0 V& L4 y" r( D! k5 Yconfigure: *** pcre library not found.% \/ V/ z5 a1 y) J% c" ^+ b
configure: error: pcre library is required
  Z* v" n0 Z& a+ }" V解决方法:
7 {% r0 i% b# L' U
: K1 s# {9 U2 P. a( Z" Pyum install pcre pcre-devel
: @7 e. ]3 k0 I1 t$ `4 t, _! U3.没有libxml20 [. Q5 O/ i* k
# w1 R( t+ ^; Y, b
- D1 z2 W$ k. }. w& x* H
configure: *** xml library not found.
% [: z# |% y! ^configure: error: libxml2 is required
9 `# p( {- Z7 B2 ?2 K* `解决方法:+ v# y- W! F  V0 F3 Q
2 U  K( K) ?" U$ l) x
yum install  libxml2 libxml2-devel. }. w% D% i& s2 h; G( v
4.执行 /opt/tengine/sbin/nginx -m 时有警告; _1 A( b  M; k' i5 E
0 l& `  M  W, G( I" ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
8 Z9 Q% w* j7 a! ]nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 d( j. r0 \8 X- h' Q' d原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 C5 m4 m0 p8 J. ]5 _8 D0 j* O/ ?9 O# Z: q3 c5 N9 i$ z* q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- m9 m  d& Y( x) O- T  Y* j- h( S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& H8 _6 k/ o$ n1 ~* O) A
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) z& h; P# p% p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 Y! Z& ^3 a* E5 L5 A! e* i3 l  I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. T, b8 _3 x/ [* q. g# u2 |- j2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' g. k# D' ~' i3 _解决方法,移除低版本的APR (1.3.9)
* w0 Z: I4 Y3 D1 ?
' \4 s- }1 K, }" Ryum remove apr5 u, _% u; d! A* L0 G
5.Error.log中有: Audit log: Failed to lock global mutex
  t; r: @, r) o- P) j( `/ Q- ]
+ l  u, h: Z  L( _9 N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 o9 `- S/ w3 p3 p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 H8 s4 f" o0 G
解决方法:
, h& d- j' H6 I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 `  x* D& |% t! R- p& j1 Y# e! B! D8 w! J9 t  y: d- L& A; n
SecAuditLogDirMode 0777
( f. d( m8 N+ j% p! X* ^0 Y+ uSecAuditLogFileMode 0550
, K9 [, g, _4 W# x9 hSecAuditLogStorageDir /var/log/modsecurity
- x; b! c, a7 qSecAuditLogType Concurrent
7 q& o5 Y. \  Q0 \4 i参考文章:
4 z( u8 J. e3 {+ z! y2 _1 shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% Y: h- @' C1 z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-21 18:35 , Processed in 0.066862 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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