找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11919|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 b# v) j! s- H: B1 G" N4 u, K- y; S: ~- {' R; J0 i* X6 E
一.准备工作' k# h4 ^: w) T" R6 c6 k6 A; b2 E

6 `. O4 r% I0 j; P; Y7 l  H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. ~, S) m) R; f+ e  O

+ [/ M5 H7 ~+ g) v/ [8 _  xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- F1 r, D( t  r8 i7 _
8 [6 v" W) q2 r* h, z* Lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ ?& G+ F* ^$ f4 ~) q& G  L* T4 }
5 @8 ?5 [5 J( T6 b( G9 BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& ~  P! M9 M- F" C6 C8 P3 H5 s+ O' P9 z1 |  G
依赖关系:6 z/ t9 N) |0 K0 K. f5 d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 I) |- c4 r8 p
2 q2 d; H4 ^- L' O" t, h2 E3 K: b
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel$ {: f8 o5 O4 t. Z5 G  H
modsecurty依赖的包:pcre httpd-devel libxml2 apr
$ \, Y' _( L6 I3 i$ m/ n+ z/ V) s
& H+ t& \2 o4 Y) {yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  e) g+ ^& s/ c9 g3 l; U, h
二.启用standalone模块并编译
: J3 M9 C# h2 _0 O2 r3 a4 b/ ?4 Q1 k3 u
下载modsecurity for nginx 解压,进入解压后目录执行:1 R( w# Q  i  q$ q

3 p# S) ~. F$ q) M./autogen.sh
4 B; W- n9 n6 V: N1 E" ?* s./configure --enable-standalone-module --disable-mlogc+ O$ d% V0 k9 G$ Y8 s1 Q: F. i: v* e
make # Y& z, g* }  r* V& e7 s
三.nginx添加modsecurity模块  C: D8 K7 K9 C$ c. P
- N- L" s4 }4 R+ v4 G( l* m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 ?! g0 r  W! A1 z' B$ w; Z; L

% x- I5 _% d! E; R9 ], E./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine; a1 K+ I. n& a: g3 u
make && make install
) N! N% N& H1 G5 E, c6 y# A& D四.添加规则9 T  z8 ]: B* o9 p% a: c- Q
/ n' e4 `6 J# ]* ~; X
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
  N% C2 m; `! Z6 h* N$ d/ p. }# ]. ?7 S  h- I
1.下载OWASP规则:
3 L3 W( r: t% U, ~
7 \9 J0 x/ E; @9 Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 R. ~" I' e6 b* Y5 o
, [3 O3 r0 I6 |: O- X& S
mv owasp-modsecurity-crs /opt/tengine/conf/3 m$ K% P: G/ w' w/ j- O, Q
. f7 s8 i+ h" f7 f$ Z' z6 D% n
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# g8 J; _! ~6 S6 ?- l3 S" {
2.启用OWASP规则:2 p2 S; ?  G" w

1 p2 d2 W4 w0 e6 I- c; _' \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 L" t: d) i5 U6 G* F8 L
: K& |$ A7 @& N. B- _% \
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 R% b2 k( K2 z1 g3 q$ C5 C) \
' Y* B- T  b- Y, ?; S
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, X1 q* E, k, ?! r" ~2 S5 Y) B: [/ n7 y" B% J
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& V0 }! e# p' F3 ?) q/ {, R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 U. q6 J, F  B4 P" A( ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 G& p5 A# u) j/ B% m: l, f5 W" l! IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; e) c% _( F& j- j0 f4 e3 O; dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. x- K9 V  N9 ]8 b4 h! E' @9 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( R/ Q4 T7 g+ Y6 ^* q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: W5 Q& r% W. w2 x) L9 ~2 y
五.配置nginx
, M: z% Q1 e9 m$ m6 Z  K' Q8 X" R( D# p$ D4 Q+ d" l7 p
在需要启用modsecurity的主机的location下面加入下面两行即可:! A$ S) f, F! F: G4 m5 h. V! H

) N4 d4 G+ z9 J/ x3 `9 @3 {ModSecurityEnabled on;  & ]; k/ v/ V( f# P
ModSecurityConfig modsecurity.conf;0 ^, s$ N: {0 b: ?* @6 d5 F' D/ s
下面是两个示例配置,php虚拟主机:
( S) g; _7 L4 a1 u# L. s" `; {1 r3 ^- {1 p+ y0 e3 Q0 z
server {
0 H4 j0 v6 v& T0 z      listen      80;. s$ }' M) ?* K+ T3 W" |
      server_name 52os.net www.52os.net;
# M" Q8 S0 q3 A& ]+ m7 o     
( I2 ?  X/ y# ~/ T      location ~ \.php$ {
$ D- Z0 O& c1 _, X+ A      ModSecurityEnabled on;  ' c4 ^+ R9 {* y% k& p, b
      ModSecurityConfig modsecurity.conf;4 T! j5 s" e* X) R4 B  [. `" _3 Q$ g
. G; M9 B! P' m: o2 C+ W! |0 S$ i
      root /web/wordpress;+ y) b( r% ~2 l$ B' V9 n1 N
      index index.php index.html index.htm;
5 ~1 e7 `! T8 p) }3 Z6 a* ^/ y  
6 l7 ~6 x/ V, n- n4 g8 K# p      fastcgi_pass   127.0.0.1:9000;
& u- F2 \* |+ y3 ^0 x% }1 Y      fastcgi_index  index.php;7 }9 M* [; F+ k4 k
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;- d- t- X# Q+ Y2 B  L
      include        fastcgi_params;
: {% e& w* A$ W! O      }, |" T& B0 E! ]$ e, D* P
  }
" H! a( D0 n/ N9 jupstream负载均衡:3 D3 E1 Z  }( z# E! ]
- `# X, ?0 Q) B5 {
upstream 52os.net {
  I8 H* Q" G9 T; \3 O6 s    server 192.168.1.100:8080;; ~# F9 w- X% ?! j
    server 192.168.1.101:8080 backup;/ X9 E7 J; {! \3 l* G0 H4 y
}2 l- q1 ^. h( \) E
/ N/ i  G$ u% y& f# Z$ d, }2 ?
server {
9 Q! c# }$ P% M% nlisten 80;% s9 L. n' S7 }! O" K
server_name 52os.net www.52os.net;9 w) |! h% j+ _, H
# i0 F& @8 U& g# ^  x+ d
location / {7 h! \4 v9 ?" @" {
    ModSecurityEnabled on;  6 a3 g% Z# \- q: U! `- L
    ModSecurityConfig modsecurity.conf;  
1 @7 Z( b% H' K6 @7 F; P! |$ q4 w& X$ \( U: r1 F
        proxy_pass http://online;/ M2 G, N9 `1 [/ d) T
        proxy_redirect         off;& s9 F! H4 r" V& i
        proxy_set_header Host $host;* F' Z" L% `# w: N
        proxy_set_header X-Real-IP $remote_addr;
" D' k3 P6 m4 y& J* E+ l$ Y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ s. @' s6 a% j7 w. T8 K: s    }" y& o/ `8 |1 E5 ^4 [6 s! `4 q
}! t" _8 F9 |, |: d
六.测试+ l" ?! K9 H$ W

) w8 m- b8 |; \, |. l5 J+ U! U我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 h5 B: {7 z4 c, c9 Q
6 x2 l% S0 T/ B, V
<?php# |0 N6 T* I  l; \" t% S5 y
    phpinfo();   
3 h# Q. t3 |3 _; J5 O( @?>/ @- C7 @( B+ |/ g: @; _+ ^
在浏览器中访问:
8 y# j5 M3 ?3 T2 H- @" y0 T' s. W4 {$ p  B0 U' c
http://www.52os.net/phpinfo.php?id=1 正常显示。; q. e1 r' A  p
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 \5 k% _. u& d9 t% e) z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。* Q" |' N" q/ Z, ]) D
说明sql注入和xss已经被过滤了9 B/ C1 \+ S7 W$ M$ G

  u9 {3 A1 W- Z9 X; @7 c  e七、安装过程中排错4 t% t3 a* G* G) a: r
4 S$ f2 c4 E2 ?) ?% s! o+ b+ V
1.缺少APXS会报错
$ p( _7 E/ Q3 l8 C
* H) L# t' T$ M% C  J* `& l0 A/ Vconfigure: looking for Apache module support via DSO through APXS
; K9 n! c& F- mconfigure: error: couldn't find APXS) u8 u! m% r4 v; f0 p
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 T5 P6 X! z9 B/ ~9 o  l
解决方法:
( ~3 i7 j! t6 n9 s
, X! n9 G, d8 l; J. j$ x' Z9 `yum install httpd-devel
8 ~( ~$ B. f9 F* j" C4 Y2.没有pcre
1 u6 b' l' m+ }; L9 ^& g+ D. ^& P0 i
configure: *** pcre library not found.* }3 M$ D0 J' r4 q- v
configure: error: pcre library is required
2 ^* U  A7 o. _; y( L; d& ]解决方法:" P& e8 b3 P7 a+ ^" s( F+ ^
2 l$ `* r9 P. ^/ ~7 E3 o+ K. W
yum install pcre pcre-devel
% v+ ~+ O3 T% N( D3.没有libxml2
  z; a: P; ^9 Z% h4 p7 k2 u! ^: a0 M$ V
! Z% ~5 Q2 L+ z: [8 E; m3 r+ K3 J
configure: *** xml library not found.0 c5 a" [. O! \* j: [4 v
configure: error: libxml2 is required* Q% w; G( k3 G$ k
解决方法:+ _+ P' I3 L5 S2 @1 F

* [* Z# a+ J# M$ D5 y6 v8 g- Wyum install  libxml2 libxml2-devel
2 a7 x+ g  h, _1 U& L  m$ N6 U( U4.执行 /opt/tengine/sbin/nginx -m 时有警告: q4 |% M0 p* o/ T
( {) X; I$ R" T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' l! w$ V; N7 @0 {1 s
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, _$ _" _) Z: e7 {# T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 a- `7 X+ Y) H6 u- G2 ?. i
& a5 Q! @/ C$ M/ x1 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' v, f4 O7 ?- i: I/ i8 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 X$ y, C  s8 D- B+ _3 t
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" M" X3 {4 s' V% V  R! T7 s& g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 w6 V* U+ ^5 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% Y/ U" T2 t4 d# U* O2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ Q6 n! Z1 `  b; U' B) l- @
解决方法,移除低版本的APR (1.3.9)
) B' Q- i: @8 ^4 R, P+ ]1 q, G; ^6 E4 q, l  W
yum remove apr& z9 g9 I4 s1 e) Y7 ?
5.Error.log中有: Audit log: Failed to lock global mutex
7 ]2 W( g" ~& H/ _, ]. C8 [: m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* b# q9 F2 e6 C0 I( K+ D, A9 Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. J% W1 K- E7 t/ P; v
解决方法:0 r  e$ `' J1 c* n4 O6 Z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. T$ D; L" m1 C, c6 |- A$ p; I  {/ a. ^, N1 O% ?" o' x  G
SecAuditLogDirMode 07777 g3 r, l  g/ o2 r
SecAuditLogFileMode 0550; G" ^+ g0 {0 ]( N' W3 m/ _/ T5 e1 u/ {
SecAuditLogStorageDir /var/log/modsecurity3 H! U! ?9 S0 t
SecAuditLogType Concurrent# B$ ^) K3 v3 c; b- B% j: a5 E
参考文章:+ B/ r" I9 A9 q7 @! h
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" G" |5 T$ g2 P+ Mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-25 06:22 , Processed in 0.056472 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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