找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12875|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ w' k9 g9 Z5 z/ N# A
6 A# K5 ?  f. I: X2 K7 G# L6 C& g一.准备工作! }; V+ M& I! p5 h, x' ^; `

" J- B, n! K# `- J% g8 q& h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) v7 o' y* O2 P* m
( U# W- r- ?  X. wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* `2 \- \) Y6 B: D+ ?* U! `7 @$ T

; X; c9 L" J$ umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: c7 t# u8 W" ?2 h+ M2 ~2 J3 _8 u7 Z+ q8 F4 s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: S1 P5 s% L7 l% K5 d& Z$ _8 V6 N2 m; p/ n
依赖关系:0 q9 r$ V% F, B5 d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' s4 N- J6 H- ~: e
) t3 R9 g+ w7 a: Z1 ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
0 R/ q% w8 u1 H& c2 G9 umodsecurty依赖的包:pcre httpd-devel libxml2 apr3 {" g) _8 @- g& L$ p0 _

* Y  t1 x6 H! f2 b2 j$ |( Eyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* {; R8 ?$ C& z+ U! ^4 P
二.启用standalone模块并编译* b5 x: ~' M& D
$ G- K4 G' N1 b3 Q" l
下载modsecurity for nginx 解压,进入解压后目录执行:
% z9 k) h3 B2 [8 N( {
* {# T( B: ?9 ?# Q8 x9 L./autogen.sh1 b) R0 J: z% T5 e6 y0 A6 L3 E
./configure --enable-standalone-module --disable-mlogc& M* ]4 @8 O" T$ T# p! N
make . E( R% h/ O" i$ t! d$ m
三.nginx添加modsecurity模块& G+ e) F8 O4 ]% o
3 X2 i. p) Z* U1 |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! n* r5 _* o6 W& p3 S4 d0 y& n' `& ]
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! j6 _% j  P/ t- W. |
make && make install
( G( ?7 I0 s0 ~+ @% m6 H四.添加规则( H( B- A3 K, ?7 j

' ?9 I7 x0 j3 Z8 s* A0 Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" ^: g1 N2 D$ d/ M4 f

8 P4 F* B  U5 W1.下载OWASP规则:
1 P& w4 |# U+ ~, T7 g( p
5 d$ U/ \4 ^5 X' T5 Xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ V. Y  j: {: ~
) Q) e( p# k: t  [! f% U0 p8 `mv owasp-modsecurity-crs /opt/tengine/conf/: c+ X* x4 X& ^. c) u0 a/ A
, H. r6 @2 L6 ?0 p
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' z7 B4 o: j7 E4 X3 p2.启用OWASP规则:; a, N# X: K% _- u" `
6 H8 C9 t8 G7 a3 ]4 J" }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 l) d9 T$ s/ P; w- x' P/ z

0 m% Q3 r% z$ I0 k) L) G编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( v- L( F( e; F& A0 N9 g7 h- E# P: _* u; [
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 {7 a! u1 [+ Y( k6 @

+ ^+ S  N: D% d+ P8 lInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, [' o: Y8 O% p$ CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! `/ }1 a, S2 ^( DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, Z% q  W0 O% M0 L7 c; ?/ \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' L0 _' ?9 F  y4 @/ P  @0 T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 ~1 v" t8 {4 m+ G( p  gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 |4 I! I+ O. F) ~: X, o  U: N) \Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' `  b0 ]; e( [0 r* X
五.配置nginx
+ y5 C& p$ R  y
9 C1 w0 Q6 D- J! D% H$ n6 ?在需要启用modsecurity的主机的location下面加入下面两行即可:
  R# v( |3 v  F8 Q! ?; o( W; e' T" |* v! x( g
ModSecurityEnabled on;  
8 _$ J4 {; r4 U0 w& ?ModSecurityConfig modsecurity.conf;
! X$ o6 T7 t" l+ ?; W6 u下面是两个示例配置,php虚拟主机:
6 T! E" ~" E3 l' |. |+ p! e+ e0 d1 v% D
server {8 [+ I3 L1 @( t; r( T! a
      listen      80;& b9 [7 s& P; R
      server_name 52os.net www.52os.net;
& y( H& u8 k" X: X" h9 f6 {5 n     9 g: [4 L. v7 R- ^
      location ~ \.php$ {
) W7 |. b) ~, h% w6 M- c8 a      ModSecurityEnabled on;  
0 {$ b/ U- B+ o0 a0 N) J, h& O      ModSecurityConfig modsecurity.conf;
2 }. D% H" W1 b5 F' |' l+ |/ ?( w+ ?4 \# s: o$ Y) T
      root /web/wordpress;
8 V  W5 g. [& a9 Q* S6 q      index index.php index.html index.htm;
/ S7 K) K2 ^2 I4 W8 a* m/ l  
$ b+ P& U* h) k. \      fastcgi_pass   127.0.0.1:9000;# v' |  ~! O/ o$ i- i* @
      fastcgi_index  index.php;7 V6 v" h8 S) _! P7 ?8 C
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 L: i7 e9 j) ]/ v( q- J
      include        fastcgi_params;
* r# f' X9 p3 D$ B8 e2 g      }
0 F, z$ E1 O. H, N  l& d8 x* R% \  }7 p+ A, U6 e# q8 ?
upstream负载均衡:( b/ D. K) j- K; O& t! Z1 E
6 W! |/ i+ F  \5 _8 c$ x+ [. q
upstream 52os.net {. b& B$ f, q+ D" x3 M
    server 192.168.1.100:8080;. k8 @8 r$ ^+ e4 o, T0 `
    server 192.168.1.101:8080 backup;9 m8 l, s' N' a6 h7 _8 ?
}
- J0 p9 n- ?2 @, S, U
. s0 Z( L% v: X3 Xserver {
' l* C! Q' \4 e, r+ a& ~6 J" zlisten 80;
8 f0 p4 W* b5 }- U  aserver_name 52os.net www.52os.net;, y9 j: C( |* x8 A+ Q
, Y# x' v$ r4 }$ [
location / {$ [5 b$ u% o2 x3 S* n
    ModSecurityEnabled on;  : d) {7 Z5 u, L% m
    ModSecurityConfig modsecurity.conf;  
$ [+ m) _3 x3 K: B  ~' w' z1 `
4 M- v4 u7 t# o" y4 V9 Y        proxy_pass http://online;
; w$ P4 X3 _! z        proxy_redirect         off;2 p1 i' S2 A; a& h4 W; {
        proxy_set_header Host $host;
# B2 n- \1 Z+ I# Q2 j; J        proxy_set_header X-Real-IP $remote_addr;4 n; g" ], V* t# I: p6 o9 D- R
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;6 `* U8 K% e0 R' ^; A( }3 `
    }9 f2 H5 H3 V( b, g5 E8 N. w
}
6 M& @# p) i6 D0 @六.测试
1 \) J. b* U  [  ~
4 q4 m2 Y1 J& |) q& _% a. Q- Z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. _; J: h1 |, p0 P. W6 `+ v! l
( t  S% S  y: T<?php+ N- Y  r" T3 C( z# y  o
    phpinfo();    8 m; a& E, q4 H
?>
9 ?* {8 T  h9 f. v( G在浏览器中访问:
5 B! K! `3 ?# x' P: l, n! }4 d1 Y0 h7 m
3 {8 T! w1 l, P" j6 Q1 E% xhttp://www.52os.net/phpinfo.php?id=1 正常显示。- v  \+ l' _( {- l& P, ~6 d. K7 ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。% B' E& X+ G4 A' G
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 @1 h/ _9 R/ F4 \* g" O
说明sql注入和xss已经被过滤了
2 n- K9 r. T/ X) a7 j+ s7 F  Q; p9 z9 f2 [
七、安装过程中排错
0 U' ^7 D# C* l: s8 [2 E9 _
6 \& D. N. J2 ?# |7 k1.缺少APXS会报错
: u5 R; m; k9 J% e
7 e9 |: f6 q5 A: u' @7 a- p8 [configure: looking for Apache module support via DSO through APXS8 w# o; Z1 e7 ]
configure: error: couldn't find APXS8 B# x( F: \% ~$ J) ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 k+ U! `8 z4 k# I/ `' j( m% {" N
解决方法:
7 }5 X9 N2 J' O7 {. }  s3 `1 n  w; E9 N4 X9 B5 g( a6 @% T* k) p2 @
yum install httpd-devel
* E' K4 d4 O4 d9 ]+ e2.没有pcre
) B3 U% N- U, `- u0 `3 [; Q4 u
( V0 Q3 `1 W, C5 S7 k% uconfigure: *** pcre library not found.
5 U% |/ x1 k6 G" P6 `( D- ^7 cconfigure: error: pcre library is required
# r. z( P# w8 s* Q4 C% |5 `. u解决方法:
: Z* w7 A! }; J8 M. _8 _$ L& v6 A( J( _
yum install pcre pcre-devel7 s/ o  e  ~3 e* A/ k& c
3.没有libxml2& u6 r; l2 G2 Y. O0 r+ Q

1 o( m6 n) n6 ^3 q$ }6 I+ w5 Z
) j& T( n# `6 _& Tconfigure: *** xml library not found.. g7 f7 j; I% [, D( P+ ~3 A5 B
configure: error: libxml2 is required
; T& U# G1 d* s$ i7 [解决方法:& Z6 _0 e( e2 A4 \2 O
  L' q* K- ]7 W' S) {6 N6 ?
yum install  libxml2 libxml2-devel4 a4 J! U% c* i8 P, K' p  e
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ g' _+ L' K( q1 ?3 {6 H1 Z
, x9 D0 x; B. Q0 `% F/ r1 HTengine version: Tengine/2.1.0 (nginx/1.6.2)
- J5 f1 {: J' mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 w1 R: e7 a' |) j; @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- ~8 s' R& H2 N& ^/ `* E# R  `& R% h/ f
3 F. j7 A# f1 _; B: f  ?7 x' l7 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! z$ R3 s( N- J- S8 J; U. ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  A0 ?' }3 I; I+ r: |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% r9 A4 {$ k# p0 ]. f6 z2 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ t2 M* ^3 d3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ C( l; w2 c! @1 o# k* b2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% x9 H0 n. O# v7 I
解决方法,移除低版本的APR (1.3.9)% f7 N- F  W4 L

0 [/ w6 _0 ^2 Y# R6 w8 {yum remove apr! d' |) _+ X0 E2 _  O3 h8 m
5.Error.log中有: Audit log: Failed to lock global mutex* p) p# m% L- O* ~* ?: a" c3 f' B
9 }; c. S5 `& @4 M# }7 m) ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 z( X3 l9 ~) l2 R
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 }9 a7 N$ n6 G% d% t5 b) {
解决方法:1 V- f. Y6 T9 q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( [$ W7 y# n9 V
; n/ i4 M+ z2 d: ~+ o. w5 E& B3 R, FSecAuditLogDirMode 0777
' f4 w& }2 H: c+ r( H- v3 LSecAuditLogFileMode 0550
  F( m# l1 d5 G; vSecAuditLogStorageDir /var/log/modsecurity! g# w( m8 R* P  S/ J2 V
SecAuditLogType Concurrent
. A7 B+ a: T4 F) E$ r: I" X7 y; o参考文章:
, ], b1 B" ?. M5 ?( ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 a8 W- a6 B) P: ?9 D: K7 k
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-20 22:35 , Processed in 0.064312 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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