找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13320|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ y' X, `4 L& _( Z' X% \) |2 `
) Y+ h% G5 u6 `$ i. c7 |4 t
一.准备工作
! o5 B6 T: A' J8 U2 `- V- F9 Y' ?- q( a3 w
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 ?1 D$ a3 C1 S& L- K
+ O) a4 M9 D, N. |tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 ?- P- Z. ^  I! \. P9 m3 U' A
) C% o6 q* S1 q' r7 T7 |" f
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ w$ E7 k( q1 e- l5 B: `
' H1 m, d- k+ |$ r& N3 ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 B4 P2 H5 F# Y+ E1 Q% C. q  R
7 x% ]& T. ~: x$ _. V
依赖关系:
' m4 J( [, L$ S! I/ Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: q' Z; D% G; w- t4 u

0 r) V* g( i# q2 tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ z6 ]5 ~+ r  p3 ?$ q# q) t9 X
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ r' y2 C9 F7 s& Y* J! h6 |

7 [+ @2 ]  J+ V; i2 c* V; }) Syum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
3 {. H) W2 X6 U: ~- X4 u二.启用standalone模块并编译
: T4 T% I! I3 y
7 B6 L( g& y$ w) y/ S下载modsecurity for nginx 解压,进入解压后目录执行:
" i2 F& r  A/ h$ H& D
: t. i2 W1 z! K6 E3 h; ]1 V  h- o./autogen.sh
% J0 P4 Z4 n( q0 O3 w./configure --enable-standalone-module --disable-mlogc
1 I7 E" Z( ~7 i% imake
: M4 M7 ?4 M* Q- [三.nginx添加modsecurity模块
0 m; q, R5 U5 q7 S
" W" k& r4 i( i9 H. j$ m8 N在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, i2 l3 _7 @& ^+ @9 H0 q* G) V
4 A4 z- n  p: E6 _9 V( o& f  `
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine4 A9 ^( |# v' k1 B' \% b0 `! Y
make && make install
/ u3 A& }- W1 J. o' F6 K5 Z. Y四.添加规则. w, M8 N! B; _+ a5 o( o  N
6 E2 ^! I/ c4 ^& W$ `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 u  z+ S- `, t4 Q1 g! Z, g

6 M- e% ^" _$ s6 K1.下载OWASP规则:1 A0 M5 x* L# b/ b1 G

( b( _9 D7 Y' M- q1 Y: h6 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: h6 [+ b1 O# ~4 |4 h+ G$ t$ L$ ]
  Z$ \  O8 r2 n" q; q! q, ^
mv owasp-modsecurity-crs /opt/tengine/conf/
. A4 U# {( e. f$ u& t; G, l- @- v& z2 C3 |, F7 G1 h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ }  u" l2 @' k9 B2.启用OWASP规则:
) Z% w6 F4 I3 F1 {( m4 w1 ]
6 h. V+ {4 @2 u* B$ w' U复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- \0 o6 K8 M) g& U/ z; b# c
8 u& \- n* P5 l" R. p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ K  {, j+ F0 Y8 [3 n+ h9 o
( |6 _5 N) x) @' I
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 ?8 T, r. {  A+ j: s, I( ]7 ~( S5 y. A4 Y& C+ z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, H' v$ z* m. x7 G+ M! h) w) t) l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 `" |1 j+ ~" ~4 \# x" M9 x; nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ i! o4 \* e/ P- h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 c' b: q5 L0 \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) ]7 D, N5 U% ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 E: ^7 i. y5 {/ b8 |Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" S+ I- o5 H1 x7 U3 h) I五.配置nginx
+ X* S) c/ u) x' X) E. {6 ?: C5 j0 @+ C. v
在需要启用modsecurity的主机的location下面加入下面两行即可:
- @& ~0 I& C* e; ~' r- s( X4 P0 w; p0 Q9 n" I% x
ModSecurityEnabled on;  ; H6 w4 w3 r$ z, g1 l  e7 r
ModSecurityConfig modsecurity.conf;# u) @; Q* I, ]( E' u' S  P
下面是两个示例配置,php虚拟主机:
4 I+ _. H1 k4 P
$ d% q2 n; |+ A% g3 j  N  `server {; C5 z- _* e) b9 g# _! y5 d
      listen      80;: @% F# C1 b6 K( A9 x
      server_name 52os.net www.52os.net;
, E( k# g8 J: o     1 v# z: k5 ^, y0 t, [
      location ~ \.php$ {9 h, v; |- p& F
      ModSecurityEnabled on;  
$ a" ^" ?) n& C0 Q$ H      ModSecurityConfig modsecurity.conf;& A2 v: ?$ u  k

$ V( H* k5 Q; a5 l1 r      root /web/wordpress;/ g( l% Z& a6 g6 ?. o" K. K
      index index.php index.html index.htm;$ s- P+ l6 i5 T" M4 l+ a
  
0 k' p" Q3 r  _2 d" d8 B1 l      fastcgi_pass   127.0.0.1:9000;! J" ~$ F- T: u) E+ a
      fastcgi_index  index.php;( t, N9 R* ^+ C% F. y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;3 E; @5 @: Z" C2 a1 i, C4 w
      include        fastcgi_params;5 `' q$ O# l) D1 q" L
      }& Y7 I3 B0 y5 a+ [/ S6 c
  }  v5 P# `" ^5 d; ^2 Y: y* b
upstream负载均衡:& v# G! |% z# n. T

# G& O5 {9 o" K! u/ Y/ @upstream 52os.net {2 ]# D) R, a: R5 D& H
    server 192.168.1.100:8080;7 z0 [* ~+ N5 Y+ C% ]7 A3 j
    server 192.168.1.101:8080 backup;
" h7 V  x7 B0 I0 I1 k}- H" f2 {4 [- Y, F& K, E: B
7 o- z" {& `& F( ~1 K
server {1 \6 h( n$ d# i. q& c! B; {; f
listen 80;- M0 M5 F' I' N+ Z) C2 \- E( P
server_name 52os.net www.52os.net;* Y2 \! G. q: U

- s$ y6 w; K, R( `" e" ]8 Ylocation / {
( g3 M9 _3 }" ^    ModSecurityEnabled on;  
  P8 s( n7 O1 |    ModSecurityConfig modsecurity.conf;  9 O1 v) E, b0 x7 F

0 h& Q5 p7 A& }  V3 _0 M+ H  L        proxy_pass http://online;
. d9 y, W3 d# ?' R- a( e- m" n        proxy_redirect         off;
& a! S. Z0 p" Z+ L* X/ P9 ?7 C9 `        proxy_set_header Host $host;! }! l, p7 h: s
        proxy_set_header X-Real-IP $remote_addr;
' @& G0 \( G0 L        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
1 G6 A& Z/ `. z+ J    }6 ^3 H% E) o& d5 [9 o( o. C
}
8 M+ ], s8 E8 l0 p六.测试
5 X  I# h& C( _+ s0 a
- B# R/ ^. U* b$ _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ ]) }8 E, \$ Q* q5 _- W. `' ~8 F2 p+ N% [8 F
<?php
6 Y6 ^6 w6 f& S, m% Q    phpinfo();    # ~8 {$ D' ~8 l5 J" `  c
?>; x& z0 C2 y0 R% ], y
在浏览器中访问:
: B8 `/ }- l) j  K; J1 b
+ Z/ b) Q, n) G2 J& D/ Whttp://www.52os.net/phpinfo.php?id=1 正常显示。* H/ _- Y' d4 g' j; c8 u( c2 B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
7 E# r% T' w5 j% f, uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。& R& B; j: T1 T" a% [8 t4 ?  N
说明sql注入和xss已经被过滤了- I4 G' H7 r" V; ]

0 s5 u, o  d/ }7 t七、安装过程中排错
3 e& Z! [+ O% M8 m' v2 f: |; C% d% E% X9 U
1.缺少APXS会报错1 l# Q  C* t' B' R8 p

% d- g, u0 k  ~7 N" |* B$ P% Zconfigure: looking for Apache module support via DSO through APXS  G7 ?, f! N; S
configure: error: couldn't find APXS
4 D: m; D4 f) y# J6 V: @& k- Hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, \) J5 p) D) c0 b2 s解决方法:
* J) s5 B  X0 _% Q. D1 Z8 t7 x- h# G8 P
yum install httpd-devel8 P  Z. a8 L5 J) n8 X$ \7 R
2.没有pcre% A0 y! S- i  M  t3 x
5 B& a9 g4 g" C, y% i* }0 y
configure: *** pcre library not found.
$ h# f, m$ T) P  U7 U1 `configure: error: pcre library is required  e3 Q" ?( h6 W3 z, z9 s  T
解决方法:' t# Y& s( D; _3 B" V8 D0 f
% g  T! Q4 A; S" I
yum install pcre pcre-devel6 F0 }- C4 p) C9 V$ G* ?
3.没有libxml2
# N" I% r% w( @3 R- {
# S4 v3 @" I% W6 t5 e
& n( G) W2 T3 }% d. T: [configure: *** xml library not found.1 X" _6 O2 ~# l, k
configure: error: libxml2 is required
. f3 p' U% [' A  j解决方法:) y# ?/ J) M! x7 _! |

$ e0 `2 [4 a7 G. k  ]  Eyum install  libxml2 libxml2-devel
; |  E' u: `( q5 X4.执行 /opt/tengine/sbin/nginx -m 时有警告
, q* E) h3 m1 ~4 d5 }( i& b9 O0 M
, A' Z# E: u* ITengine version: Tengine/2.1.0 (nginx/1.6.2)
/ o  @0 e" [+ V$ B/ ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 d  h; O3 W% B6 k  z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ l* C: X# e# P' E5 s

; T( {  ]3 J9 r5 H) a, A0 @6 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# q9 M  u, b' U' n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"2 J/ B* i. j* _6 z' ~3 M4 \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' M9 F- P1 v) u) S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 u' ]4 E% y+ [: y/ ]# T. a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 I: d: x" Y" w: O) `2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: K# |7 u8 q3 @: f
解决方法,移除低版本的APR (1.3.9)" [7 j5 r: |: c3 Z

% T  |- ?4 K% ?* nyum remove apr
" X% h% m- F+ S/ d1 n5.Error.log中有: Audit log: Failed to lock global mutex
: L3 W+ Z, o9 f3 D
$ k, \4 P  o* L) g4 o2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & ^* F* b. d+ y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- t2 o/ ~- z9 f/ P: l  W' N! X
解决方法:
! e  Z; ~, F/ x1 R$ ~# l编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 G3 u: @# E- J% B! `, L+ U; P% _' i
3 R, |# R/ u* P6 L) j; G
SecAuditLogDirMode 0777
* w( ~: x( }) n2 P/ v* j( l. t3 x" lSecAuditLogFileMode 0550
2 P2 K. H$ i) K( Q& v' z& MSecAuditLogStorageDir /var/log/modsecurity
! F! W, Z. s# m6 H1 a; aSecAuditLogType Concurrent
) @( ^+ g. I- S5 f. Z2 @) b参考文章:
/ h, j6 v" z# \4 F+ a% shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ s* w# p1 l% i: k1 u, vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-4 01:25 , Processed in 0.070656 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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