找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12815|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% w1 [! P7 u/ C% T! ^
7 k) f* T) G( {8 }- c一.准备工作
' P9 T( U2 j, P) Z# o
' x; ]1 `1 u! Z& {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# i! Y* `% b: w) X; H

3 x, V+ K0 j* \" @: \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ J, `/ n9 i5 w0 Y! |1 {

2 l5 I7 W, ^# smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 N# T7 t! w( j$ @3 r5 ~2 {

( l) A+ H1 {/ w4 r# W# GOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 k: m5 w( |6 Z1 J# c
* [. }5 d2 P$ `) [# o7 m/ O
依赖关系:* w* |6 P4 V6 M7 l; L3 P! X" ?9 `
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 K6 e- p3 n# k( }* a! v2 E. {* _& t  q0 |4 w0 I
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
% A! d, B0 l2 c+ w$ Smodsecurty依赖的包:pcre httpd-devel libxml2 apr
2 ]% z% v7 B" X  W3 T: \8 [9 Q) C8 O: M, C9 }
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 ^3 w9 y( _$ g% m# Q二.启用standalone模块并编译5 y& O1 }5 D# p; I, \
6 g! T2 R1 z* q9 P; x: T
下载modsecurity for nginx 解压,进入解压后目录执行:
. M! I2 E' ]" w1 v' S( V
! n9 ?1 n$ u  V% g./autogen.sh
. |! l; _8 Q2 l' F- \' m./configure --enable-standalone-module --disable-mlogc
9 C' }& M# D9 c; F! O# @! [" lmake - [6 f2 H4 s# c" n5 w
三.nginx添加modsecurity模块4 R: x& ~% d6 d0 V1 U

; b! A' l1 U; o# p" `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ r/ H  @1 r+ e) X2 Y

* R: ]% x* I: J) m" h# u) z* R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 [7 |- G/ S8 bmake && make install
% g( R% W% |$ o/ m四.添加规则/ n, Z9 t% l% `( u
5 V- t0 o% o- {8 i  a: X
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# D; k3 F9 j6 |8 n2 M: B& ]- D3 G. D) s' l: q, h
1.下载OWASP规则:
) {& b( }4 @; g' r9 p" o
0 ]# n$ }" G( V5 t- {git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 J  s# w% M. ?1 Q  c8 ?1 ?

2 c; t( J$ P- }* T( s8 U' v7 K! Fmv owasp-modsecurity-crs /opt/tengine/conf/
" o3 L- ]" U+ W: _% W' d7 _# j( o' \3 s
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! A' R9 ^+ B0 N2.启用OWASP规则:! H2 ]2 h1 H) A& F2 T" }4 ?

9 H( q; P. ?9 G( H3 y/ E. P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& @' y/ g( i7 r8 Z

$ k! H' L# U! X, A! j" B: j/ _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" I/ Z  I  F" R  g
% O. t$ I9 U# @( y% l" P" _- X: F+ ?
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  G. ]5 X6 N, J- c. c
1 u0 v* G& M, ?" N' t# q8 ]. A9 o$ A' sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ H$ y( g: f! A9 a: rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( w% k/ I' n! X$ n% z* C+ q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: Y: d2 c7 h8 IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" _( W6 \3 u1 nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ J" @) k8 P- Q3 m- R7 t" u& p- m* VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 W5 |' N* H  u7 p  Y' Y3 L3 z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 D0 X; }3 {$ R8 r+ F五.配置nginx0 ~9 @' q- H, S* V4 t, s: I

+ Q5 ~, ?3 E: F+ x8 R; N6 D, i在需要启用modsecurity的主机的location下面加入下面两行即可:: K6 X3 n: y  P: j
8 \4 _: j" B; ~1 b" e, Q. o/ [
ModSecurityEnabled on;  
2 T0 Q5 f/ y# L$ ~" S1 gModSecurityConfig modsecurity.conf;
: H, o: w0 Q/ u# ]) C7 e2 |下面是两个示例配置,php虚拟主机:( A- I0 o+ s2 I, s
* j0 i" u. p6 W( x
server {
# B" c9 A% v6 `- C% T      listen      80;
% U$ B9 ]: y; k: m$ S      server_name 52os.net www.52os.net;, |# X" z. N1 X8 L9 G- Q3 K" q
     
5 X& v- f0 i; b      location ~ \.php$ {* K& s3 z; ?$ |& l+ x4 ?/ l/ e9 ]" ~
      ModSecurityEnabled on;  
* h( B( ~4 X, b/ T1 D0 v      ModSecurityConfig modsecurity.conf;8 C8 l  x) u7 A+ K7 n. d6 m
0 D0 d& @) w  M; a* }
      root /web/wordpress;
. Q) _! |% P2 Q( j      index index.php index.html index.htm;2 o- m9 f; `# F( h9 b- c) A5 A7 X" W
  , _8 D- ?! d, L" t
      fastcgi_pass   127.0.0.1:9000;
# Q0 C+ u' K  _( r* B# N/ T! k& x      fastcgi_index  index.php;
0 }) B) b/ e/ b      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
) H  B% Q# u2 e7 F      include        fastcgi_params;
- e! r5 e( v& W      }( ^7 m: k+ s* L/ V' {) H4 n6 G4 |
  }6 b# N$ Z% H. ~2 i7 `; E% S0 `1 z
upstream负载均衡:
4 J0 E  b( V5 b6 ~/ X! n  l% w+ \, r5 L9 W# ]" U
upstream 52os.net {) H5 t% Y6 f2 g% n5 V2 |
    server 192.168.1.100:8080;/ X  F" Z, K2 [5 [5 S/ }' \
    server 192.168.1.101:8080 backup;
( \- `" F8 w* y3 Y8 C* y  v, M1 q1 @}0 q4 H0 H; c0 q* m9 h

- F9 Z( R+ P  M; Eserver {& P2 b/ e/ |, m+ [3 c7 p8 b: |, N
listen 80;. F1 F" B' w4 @: {
server_name 52os.net www.52os.net;' E3 V1 j2 H7 o6 ~" g& H4 ?- S

/ l% `, b. o- |8 j# p* klocation / {' J* a4 y7 e3 V
    ModSecurityEnabled on;  $ U, B7 F5 [6 l5 S  `
    ModSecurityConfig modsecurity.conf;  
# U* \0 y2 d8 J  o% N% N) a  z
$ ~3 p; F7 V0 r# I" `* L        proxy_pass http://online;; @% o+ s4 W3 L5 k' y" P6 H5 P
        proxy_redirect         off;) ~+ P4 X( s& c! _  O  j5 Z
        proxy_set_header Host $host;7 p* l* p! y9 ^" S- `5 f
        proxy_set_header X-Real-IP $remote_addr;$ S4 t% c! d9 ~/ f8 w  B: H
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
9 L7 a: T  T  C! t    }) S! z! J, y( @! z9 r# d) p
}
6 A# m+ A5 |/ X: Q$ O( F$ }六.测试
* ?1 }5 M& {1 ^0 N0 h0 a
: Y  B' Z* Q& {# D4 O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* @! Q1 f! E5 n/ |: M
8 E3 [8 ^5 e( i. e- ?+ h0 N. w8 p
<?php
) T5 M5 S  ?" T  O$ g  }    phpinfo();   
$ i1 P" v+ j1 {9 m+ r+ y6 N) w8 W5 E?>
5 g/ }* s6 a  s$ s" T' V# o. x在浏览器中访问:. a) y: H9 Q  r  p( P& G0 E  u
- n4 `# O3 X) _  E. H& {+ i: C
http://www.52os.net/phpinfo.php?id=1 正常显示。# S$ ]1 _/ c. M0 m
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 s; f+ ]5 Q: ]" ]
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- j( b& q5 ]5 F3 A" F8 U  z7 E0 @说明sql注入和xss已经被过滤了8 c& p3 H& B2 @, i
$ `4 ^; }( g2 P0 n' F3 r
七、安装过程中排错6 d! p  P" Z7 [4 f' n) v

5 b6 G1 `4 l3 M' a  R/ u1.缺少APXS会报错
/ h$ R, N/ m  Y3 L' i! l* X, E8 t' k; V0 C0 m' X. e' F. o
configure: looking for Apache module support via DSO through APXS
- d. \: k# o9 E' M$ U& Vconfigure: error: couldn't find APXS2 m5 k) Q: {* G/ ?( l
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ Q" J& K, j$ u& R3 r
解决方法:
7 |, K3 L# @/ b! k* N  F: d; `8 {6 }& L) ~
yum install httpd-devel8 e  Q0 f: G( k: q" J0 M& y9 t
2.没有pcre+ Q5 N7 B8 Q4 ]+ ]# p0 n: L
( s( V# s1 n* O# j# C( c+ {& r) M, p( w
configure: *** pcre library not found.
* T! x. a; r* A. N" ~8 Jconfigure: error: pcre library is required/ {! Z6 y5 @! l* v( P. Q
解决方法:. Q. z4 B- W4 n4 s  j
4 D  s, u/ k$ `" n& k
yum install pcre pcre-devel' A( E5 q' Y/ u  Y1 \6 X0 W
3.没有libxml2
5 l& s( d( Y6 E( L0 K7 Z$ X2 ~8 C( @+ k. l  j# \
( }; z; k* C9 b- N# l7 p. q
configure: *** xml library not found.) \2 E" m% O: \5 r5 m/ j+ u
configure: error: libxml2 is required
1 s* H- s7 F+ E! z1 x6 Z解决方法:
  _* Q, ^: u! Z* O. j$ Q4 `: v( ?# @8 \( q, g/ N; W
yum install  libxml2 libxml2-devel) g- M8 T; X7 G) R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ V8 O3 ?) S( M" p1 }. O9 ~/ }, k; ^  w8 U* \
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 J% i% \3 y; m
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% V$ E" s" K& l) U7 T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! w5 F9 @+ ], z# t# X
8 S6 S1 ~7 D! X& `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) `8 G  K; a1 U4 e' D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: \" Q! r( J, G) C! t. z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ s! P8 K/ ~) |# Q- v7 B4 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ I4 r! E3 t5 ~2 A5 x' n& d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! n9 @( k: V: c' V! M5 m2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- m# n0 D8 o0 t
解决方法,移除低版本的APR (1.3.9). ]2 k4 v1 D+ X  P0 r: I7 t& G

: w, X/ }' h$ s: kyum remove apr8 a( b+ b- K: b3 I
5.Error.log中有: Audit log: Failed to lock global mutex
$ r" D0 E- Z/ T: A6 ~' h. s" C
5 y0 c9 I9 T4 s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     5 B( ?: x6 T" i1 ?6 m& _
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% p9 c' o3 Z1 T7 O! Y. a
解决方法:8 c# G  y6 y  m3 r4 s4 Q% P" l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) \: Z4 `3 v) P

, Z+ k( B9 \, n8 w1 _) I1 ]6 J5 d7 OSecAuditLogDirMode 07774 L0 C8 [" I* ~# H9 C7 d
SecAuditLogFileMode 0550- a4 X. m& I6 O' Q$ Y
SecAuditLogStorageDir /var/log/modsecurity
  V* B3 h3 D. z5 [3 i0 j7 M4 b; ^; nSecAuditLogType Concurrent) i; C/ m# z0 E0 b5 y/ a
参考文章:
( e' D* x) P4 X! n! ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
0 Q) g- u0 t# Y7 l7 S6 h, q3 T9 Zhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-14 19:35 , Processed in 0.068008 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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