找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11030|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 ~% A. m/ j% u: @5 L& x. P9 x* e+ X* ~9 H2 T% [! ]9 R
一.准备工作- g% b( A1 F, S' ~1 \
/ ]1 m+ q, R$ R& }) Q6 ^& ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; W( E$ F5 Y. {" f7 M% U
- ]3 J: r/ l; p* P6 Ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, X7 [6 C* I& q- i  n9 \/ I* Q
8 A) Y4 E3 u0 r3 o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' \( y# l9 w3 W9 O; [

- q% I$ w" |* [+ z) j0 v* G& W, ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& Y9 `7 {5 l3 P( ~5 v8 n- M8 z! N

( Z1 _2 e4 c9 m. D依赖关系:6 j8 O1 F* P3 @$ X# s- K1 i
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 l$ S3 m% V  r& c6 C) q6 {' R, I: E, b# N$ Y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" `4 U1 J7 F& A, u
modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 M2 I  }" ?7 a* F% V
- v! \* {  a5 V, }yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel; p) [- @) S8 E8 O  d
二.启用standalone模块并编译- ?; k% `- W$ W+ y

4 {$ l0 y% }7 Z& H) X7 u. o下载modsecurity for nginx 解压,进入解压后目录执行:
1 P! B8 J' G  W: W% b+ e6 z
8 ^+ \  T  B4 x' A& F./autogen.sh9 k6 x$ I% X2 q8 Y
./configure --enable-standalone-module --disable-mlogc4 U+ m3 u3 ^1 O9 U
make , ?1 u! T& v2 S7 D) Q& g# x+ L
三.nginx添加modsecurity模块: K! R9 c% B2 ], u# O' Y9 S. K  D

% x/ u* h3 b9 K: B- T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& a" L# f' b+ e, R# U7 \
- G4 n& n' D  r0 V% s* g/ Q2 v
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 @/ I2 Y) l$ K7 i
make && make install/ @* S, I0 k0 B& T' q0 ?( m
四.添加规则5 B# p5 h0 b3 `. M- R  z) c- U
0 F0 E! z6 H8 s+ I$ L1 A6 z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* t: b) r3 p3 C8 s! _
5 H5 ?' _7 E2 D3 d! t$ |1.下载OWASP规则:2 C) d$ b2 A3 `' H9 O

+ q; q5 G, I2 c, d1 L+ wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
% Z+ F0 Q; t1 g8 D* l0 V0 l% @, v; D; b7 \5 X
mv owasp-modsecurity-crs /opt/tengine/conf/
/ w' {1 @# C) T9 s! }6 b' h) X. i# z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ Y8 s3 U/ m( V1 r3 I0 Z$ h
2.启用OWASP规则:) z8 C+ S( |$ \8 U( S- P& z

3 z( e0 ^' j* J4 e9 y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 C- h  d2 {& Y" W
" z) J* k8 F, m9 ?/ p; E; b
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! t/ V' O7 E. h2 u: d

# G& |* s/ x( w4 b0 R% Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# j1 N" {+ Y! O. \+ M3 h
) o: _& p( E4 X3 t2 {# N, c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ c' c, M- S' s9 p# `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 H  F( `4 U  E3 W% Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 C1 Y% O/ ~$ F9 NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ V4 p. z9 P1 f( Y* {$ N/ nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- b( y- f  D( p6 h% T& zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 I* w+ F6 A0 |7 iInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 n; j9 \! \  z3 t& n* ~
五.配置nginx# A* x3 o6 L; t6 s& S

: o5 d7 [0 q: ]/ h2 }6 I在需要启用modsecurity的主机的location下面加入下面两行即可:
1 T' s, i# _. M
9 Y3 E. l* c) qModSecurityEnabled on;  
. j, V4 r. _$ \4 e& U$ F  F5 PModSecurityConfig modsecurity.conf;
4 X( {2 \9 p) p9 z: c& G# [# l下面是两个示例配置,php虚拟主机:3 L8 o& H% U; E& W

" |3 g$ \; Z: b1 c2 Jserver {; h$ H& E7 x2 B& u' H$ p+ F# X
      listen      80;
& G* |" j6 [' `7 s      server_name 52os.net www.52os.net;- S# \" r3 _' N6 F, b4 y9 h+ J
     
- h* Z8 f8 m, z  [6 X/ Q      location ~ \.php$ {  }# K7 V7 |7 D& t: k
      ModSecurityEnabled on;  
: O' a4 T' V% Y      ModSecurityConfig modsecurity.conf;
+ }2 f9 W% ?4 {! L$ n/ u' J" T5 W& E9 H+ b5 I" Z7 v
      root /web/wordpress;3 ]4 D2 Y- V/ S0 T! |$ R* m
      index index.php index.html index.htm;
4 u, x* `- i. Y- r7 l  4 j7 z0 ^0 L8 S; R
      fastcgi_pass   127.0.0.1:9000;6 F' Y0 h0 Q' [( i# W
      fastcgi_index  index.php;
2 q$ b5 Q0 d1 }# B$ n      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: B; f( k( C( f: G2 m6 i
      include        fastcgi_params;  o( a% ?" M. y
      }% }3 y3 Y$ f3 X/ W. f* V
  }4 d" X, K( r/ U1 a
upstream负载均衡:
* m; [; C# {0 q+ u& E$ y* q
! N% h- W4 u/ j9 {upstream 52os.net {
' {, q& ]4 L1 R* j/ }8 B! z7 k    server 192.168.1.100:8080;3 F& a7 k/ q1 g: H9 d
    server 192.168.1.101:8080 backup;5 I# y; W& o; s& H* x5 n' l, Q
}  Q) b) Y3 Z  ^( s* e

# N7 M2 Y$ y4 K+ }8 r# eserver {
* ~- ?8 P( H# l$ s" qlisten 80;% z/ Z; k* c2 `: N! N
server_name 52os.net www.52os.net;
5 O. @# v! i: a/ l6 I  E0 l2 {. A+ h2 m$ `6 Q! m8 ~
location / {
3 g8 [! c. v% U- u' q2 K    ModSecurityEnabled on;  
* N, E1 Q; m& s( I4 d: l    ModSecurityConfig modsecurity.conf;  
7 E* J- j' [. s4 S- z3 \" P9 _: ]4 e/ C% O  Z7 ~1 g
        proxy_pass http://online;7 R* s* h0 u& Z
        proxy_redirect         off;
9 k* F) }! J# x# N" ^+ ~3 o        proxy_set_header Host $host;
( y, v- W& `# y7 S        proxy_set_header X-Real-IP $remote_addr;/ q" z( g; [% B$ d$ p. i7 Y% z
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( v/ P( [% D. O9 p9 p& k* E
    }
& [% Y6 _& E7 P1 ?}$ G9 P8 H* l( @' _: Z6 a
六.测试; v& S) t/ o4 w- k2 d9 Y
' E9 O& A& \* k$ t/ s5 [
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- [6 V( T- Z! W5 F; N$ e; {! ]
1 v# }0 j- @+ u; i
<?php" e9 m$ I& x$ |6 w& _7 |3 i
    phpinfo();   
# S7 d7 ?* e* [?>3 r/ t$ x/ N- `( \6 Y( D- y
在浏览器中访问:
" o0 n3 L# c0 L9 [
. t0 J6 Y4 _( B* }) dhttp://www.52os.net/phpinfo.php?id=1 正常显示。
3 Z7 N3 J* ^8 }  rhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ e8 d/ N2 G' y1 ^$ X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 B7 j7 }+ z( I; h7 h  m
说明sql注入和xss已经被过滤了
3 [) {+ j5 o, ?1 X7 _
1 r* _6 Z! F  p' I七、安装过程中排错1 V. R1 h/ U9 \5 P

2 ~  F( z6 S- a' d1.缺少APXS会报错3 y, {) F7 i: J" V  g7 k
( N( g) c0 b" t' r. D% B' L, w$ _
configure: looking for Apache module support via DSO through APXS
* Y( h: `& J2 z2 Z/ Mconfigure: error: couldn't find APXS0 t* `1 R+ O4 a- M6 ]4 M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: \9 E! J( T6 Z2 s! f5 w解决方法:
, x5 G  J3 a# A6 [: H3 ]6 J( V" S1 P/ Q5 a7 M
yum install httpd-devel
) `7 @, V, v" s5 h7 A2.没有pcre$ L; X: ~+ V9 Q: B* p

1 V) _- D' `1 y" ?3 B3 w* cconfigure: *** pcre library not found., X6 N. x8 b  X0 P3 h
configure: error: pcre library is required
+ R- M) W. y/ C7 B) ^解决方法:
) P  \" w( @3 Q! l- T9 {0 V. @+ |! b" A5 ^' s* h
yum install pcre pcre-devel; G. K# y  I6 ^  _
3.没有libxml20 K. @; `3 p2 m7 E& I

' @9 u# z. Z& S0 i8 W2 S# G
9 O& W# F2 g, Y1 \9 b' Fconfigure: *** xml library not found.* X' f; w+ n+ l' c  H
configure: error: libxml2 is required
# W( [1 m& d. X. F8 V解决方法:
7 c  w  K2 N/ N( E
! N! |. x+ O4 v) ayum install  libxml2 libxml2-devel
" t7 n9 L1 B* @/ x# j+ A8 q8 T5 i4.执行 /opt/tengine/sbin/nginx -m 时有警告  y( Q) J+ {" \' c; g* i

! C) h4 h9 D8 Z/ v" g7 iTengine version: Tengine/2.1.0 (nginx/1.6.2)
# u3 a3 z' d" unginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! Z* S* r% K: Q0 ]# h原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' u4 M9 y. {4 J1 u9 e$ {- z
0 Y3 B7 ^: I3 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., x9 P! C) i( C: e. x. @; I2 P, D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ q/ Q, M6 b2 n% D; P
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( v1 O: ?& ?9 F) n2 z8 N2 i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 c. n  j  V( E. b. ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' n& D6 J- ~/ M9 M# I3 W2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# B. X  L/ T# F% ]: U0 d解决方法,移除低版本的APR (1.3.9), H  Z3 O  a" M) m4 _

# f  \# F7 w/ s8 @% q- ?yum remove apr
8 |" @9 `! |8 {. S! W1 p5.Error.log中有: Audit log: Failed to lock global mutex
& e8 S8 C. Y+ e( X9 Z8 n" [& f2 ^8 v) A2 ~1 |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     . Z6 _* U4 E) M$ T8 `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- X. _+ k+ t3 R5 T1 g+ ]/ m
解决方法:2 u- g0 L& f. F. O' {, u- f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ t  c) S! T7 w
1 }% m: O6 G+ j- q" LSecAuditLogDirMode 0777  L6 M" A) s- x8 @0 U& N$ Q5 C
SecAuditLogFileMode 0550
0 `9 H5 q  T3 b( z' LSecAuditLogStorageDir /var/log/modsecurity% U+ R$ F% T3 l- e5 F9 q8 U
SecAuditLogType Concurrent
3 ]8 P: m/ w7 d7 I# c* }$ d3 X2 a参考文章:
% p2 S* R7 K+ A7 z8 ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 ~. f: E9 N2 i! @; C( ~( m7 X) A" D0 B
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-14 17:30 , Processed in 0.059927 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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