找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12834|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 @: ]) I9 t8 u. G: h# K
+ \( @3 n3 H$ u, }一.准备工作
4 _5 [. P' I7 X# q) ?
9 t$ U! A9 m' x! o- K: V! A" n系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& B/ t+ Q; {% s( m7 p1 n0 [
' j5 \4 P: G8 ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* k/ M. L8 \" h9 k5 I% U  `1 Q2 o( |% j0 n2 F8 L* p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 O! D; `  q' d. q# }
# r  M% |9 F9 }6 P6 E* @' B: HOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 {* T6 A$ L+ Z% Y0 S+ ?) |) ~( k6 E7 i# |$ l
依赖关系:+ f5 ]! |7 K2 ~
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 I; [8 G1 ~8 {+ V! s( c5 C$ R2 k+ @) w! ~! a& b
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. k, z3 K+ H- e, Imodsecurty依赖的包:pcre httpd-devel libxml2 apr, ~' g8 V2 B7 |

, q& Z' G3 Q) d' {yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* k& g5 ?2 @' P. b0 [, E) j3 _4 N' f* c
二.启用standalone模块并编译
+ N, v: }, G) R2 f, G. `$ R
& j/ r* F2 q) z0 T( d  e- b下载modsecurity for nginx 解压,进入解压后目录执行:
* ~% x. }# L1 Q0 b& U, g
: L* I' c, \+ v./autogen.sh2 v, F" Q0 V" L# G6 c6 t( D
./configure --enable-standalone-module --disable-mlogc
$ Y1 w0 k. V. |; h' Q- _make ( w6 W' D3 ?; E( T
三.nginx添加modsecurity模块
0 s) k* h0 M3 o4 T: j# B; \3 ]" D( V0 `6 q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; F2 N" U% u0 Q1 w4 x: O) _; p& [7 O" o. g1 w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 ^' [$ m) C' @9 y% p% x5 Umake && make install
/ m( [9 \  y; m6 O5 X, U, T$ F5 }5 Y四.添加规则
1 v! }& D8 D' U/ n# B! h3 E% a" P& Z7 B5 C- s# S2 K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) K7 E6 [% w! _6 s0 H; B
. ~1 H5 c) R* ^7 _/ q
1.下载OWASP规则:- b9 m0 M# c% Z) u
3 r8 }6 Y$ @  e. \2 S* ~* R
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& m; |( _1 ~( `4 d. Z4 A# L% i7 v
5 J% D0 c; K$ g$ p7 @* M& |mv owasp-modsecurity-crs /opt/tengine/conf/
$ L; p6 \8 Y8 r
0 l1 q5 f& K7 ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; l' E. T5 W! d- m& s. F
2.启用OWASP规则:
4 n8 j. n: J0 A! ]0 G8 S5 Q  ~: B+ B! ]7 q, X1 i
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 C% R* R* b0 _: r7 i% i% v: g6 s& N! b6 \. y7 X
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ k$ g, y/ N$ p5 D8 v& ?5 n% W" S0 ^1 c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 i& B& r- m6 w

# h, h/ l1 ?- k5 l3 `4 w" F* FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) l* q* |/ B2 H" @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 P" i  n4 U+ s7 p& V9 O, LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: Z" S) x$ |. |# g0 M% D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, Z; V- d. E* j4 v% D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; Z1 q  u2 S) n' F3 v% F$ W2 ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# L2 O0 |! }; R" {1 c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 X! U' s; B: Q: A! H4 C' Z% P" P' ]五.配置nginx
- Q: H: r! Z: N% w$ x  I" X
6 z- m$ N- [* }在需要启用modsecurity的主机的location下面加入下面两行即可:
) Z2 p$ N" s$ a& k7 i7 N; t
, p' o$ X- o; Q  @% cModSecurityEnabled on;  * U5 a( w. r5 }0 t
ModSecurityConfig modsecurity.conf;( l8 N3 X: ^4 T" h
下面是两个示例配置,php虚拟主机:
* q$ ]& @& `, o: N! E% n
8 F: c2 x5 R0 y" Q. m0 \server {& T$ [- G8 M; H; b
      listen      80;
8 y( ~0 k: m& {9 i6 \) l      server_name 52os.net www.52os.net;
6 b$ ]0 G' p: c0 S9 }4 @     * _* H" ~4 h4 B; Z
      location ~ \.php$ {
9 h) L; z' {6 s      ModSecurityEnabled on;  6 l$ X5 [8 b" d
      ModSecurityConfig modsecurity.conf;: f3 R/ o9 p. g8 H0 x5 P( `, T

) E% o$ @) r: B- z9 l0 K8 I      root /web/wordpress;
" _8 a3 o8 i3 D      index index.php index.html index.htm;( N/ [1 c0 _  r
  0 h, }4 {# t# X+ M
      fastcgi_pass   127.0.0.1:9000;
: j- l7 ?! \: q9 l      fastcgi_index  index.php;8 i. X) L- w& [6 ~$ k
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 h7 [, T! ^7 R; e
      include        fastcgi_params;" c* U# [- w5 Q' V: X& }1 Z
      }
7 Q8 X: s0 t% Q9 p6 R# ^  }* {. e  X! E) B0 }$ A) a' d% y$ u( C
upstream负载均衡:
7 J# Z' e9 ]9 O/ Z/ V7 X6 @& j9 e9 P
. A1 N- u( [/ l% ]  q) Fupstream 52os.net {
- t+ |) j- a5 `+ o    server 192.168.1.100:8080;
; n4 q( f; G3 A# b6 R3 t    server 192.168.1.101:8080 backup;
, Y1 f& ^( g* T4 k, j}
, m# n, h8 w- [7 Z3 ~
& E. T' v; a' B/ @4 ~9 |4 G* Lserver {+ k8 \# k2 y( w" T" ~2 `
listen 80;
( q5 \! P5 |1 F$ s/ nserver_name 52os.net www.52os.net;: |1 v/ c! A# J9 _+ |) Z

' d$ L$ e7 z8 x  |) A( ~) Flocation / {5 V9 ^1 w' {2 g' A% Y
    ModSecurityEnabled on;  
. b' d2 W9 A3 K: ]- M5 R+ L    ModSecurityConfig modsecurity.conf;  # Z9 D: u- C$ Z( A/ r1 H8 h

& F& {# R( S" u) h/ n        proxy_pass http://online;: Y, t6 p' {5 m7 Z1 N
        proxy_redirect         off;( ]' m$ P, n2 Z8 B/ p2 r
        proxy_set_header Host $host;
8 l, l& C4 q6 x, X$ x. K, H        proxy_set_header X-Real-IP $remote_addr;
% i- l5 f% G" o" }" }# A3 h        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ V* S) r  E7 R& y* q, k  G    }# Q# [2 ]4 J$ O# A
}  J$ S; i! o8 T4 ]0 l$ U# ^
六.测试; f/ N6 [' ^  v2 o' ~! }- {
# I- o# b# t2 }# v/ k( C4 P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 A. k6 ?  g5 U3 f4 n; j- W1 z; B/ U1 K! z+ B2 k0 Z
<?php' u& j+ {4 v1 E, I+ W. H1 S
    phpinfo();    # C% p7 _) a  `3 L* B3 _1 l7 |
?>
+ ?( W: E6 ~" p2 y7 B9 O4 @在浏览器中访问:
: k$ X8 u1 _+ d8 u2 M+ |9 L- a, [/ A* }
http://www.52os.net/phpinfo.php?id=1 正常显示。
0 b4 W' ^$ d  Q# _$ D2 m4 F0 vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 h8 u" i1 w, K
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
% h0 f5 c, ~& @; u; t& Q说明sql注入和xss已经被过滤了5 Q5 B+ V! F6 C( d+ Z( a" J6 @- A

5 p% Z" b2 b- b3 G七、安装过程中排错
- N( u4 {# Z7 D' _! H  u- k' U0 r8 P% W2 Q8 T
1.缺少APXS会报错
4 G  Q# O4 ], F, J0 Y: M( |  S; O7 l& n1 p- {+ Y" I
configure: looking for Apache module support via DSO through APXS8 g1 x7 |  `8 b" S/ C# w! s
configure: error: couldn't find APXS
4 {  p! P: v1 S, E9 n( l6 V9 k# n, Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& M1 Z8 l: ^; c  l解决方法:  W+ k% X  F* t
: [  T: r. J! Q/ r* j. ?+ [
yum install httpd-devel
: V: `4 }. t$ R2 X6 I2 P2.没有pcre
& f% @! I5 h9 G9 _1 n# B6 \- Z' L% F* E; B5 e, U
configure: *** pcre library not found.2 U) Y1 @5 Q  u0 p1 j" F
configure: error: pcre library is required+ S9 \& g1 K/ @- d* z  t4 i- E
解决方法:
& N. `1 u: B% `- G; h, @$ h% g
6 U: K' F* ]0 Ayum install pcre pcre-devel
# P4 y/ N+ e1 J. t7 A. a; `! I3.没有libxml2" ^# K: T4 O' f( F3 E7 v8 ~/ C
0 G+ \7 t$ f  t% r  a; t

4 u$ [9 h" s. [# N5 E) P5 _configure: *** xml library not found.' X6 v/ {' n. l, @! y) }( [; G  M
configure: error: libxml2 is required
$ m' O* K' b, x) }解决方法:: V. `6 v3 q' T6 }1 Z9 j+ d
7 `! v. v0 M; L' T6 m) d3 t; j  d+ {  o
yum install  libxml2 libxml2-devel
# j  A- x. K% p* l7 @2 y4.执行 /opt/tengine/sbin/nginx -m 时有警告
  C: k/ V2 L, J1 ]  m, B5 f* ?4 O9 K: O1 R! o" f9 c8 c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 `% K; g* ^! g+ O; B) ~7 S1 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 ^1 H# l0 {& D0 a7 a3 K0 ]
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% s0 V+ o6 u' p5 d: g
7 i/ X% ]" _$ f4 @" H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" Y; n) b4 s% f* f. |3 R8 J4 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"  ^: E8 b. K% |! @/ }$ C
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 V. C4 a- X$ l8 [$ `$ Q# l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' u8 }7 v- ~  R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 P  m% }1 k6 h' P8 B! u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: w& |, J/ [/ C; Z解决方法,移除低版本的APR (1.3.9)
1 H: a" \$ z1 R9 M* o  d2 i0 s( U- }) r0 w1 e2 b) v
yum remove apr5 F+ G" N1 @1 Z+ ^; q3 X1 d
5.Error.log中有: Audit log: Failed to lock global mutex6 W' ?" l7 v" |: T
) B, \/ I1 h5 p& X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; {  c( y5 H4 M" P2 z0 nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ B7 Z! @& V" l5 S. d
解决方法:1 h6 P9 v  @$ L- T: w% _& q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* t7 `$ k6 j4 p. y% ^* L
2 u% K, M3 G) n1 E# a
SecAuditLogDirMode 0777
0 y1 d5 M2 W% {% m$ D' E& [3 DSecAuditLogFileMode 0550
7 m7 U4 p  Q; d: U# ]SecAuditLogStorageDir /var/log/modsecurity
# `0 ?9 w$ Y6 A+ n3 u1 X7 q: tSecAuditLogType Concurrent8 v5 D' F3 L) o8 \6 ]4 C
参考文章:3 q, A/ x6 y" d6 q1 U4 g8 l2 a
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 @9 M$ {7 \0 s3 `- A5 Qhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-16 20:52 , Processed in 0.076684 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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