找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11882|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  U1 f( g! U6 k7 w; c/ E: \' j7 w8 }- W" R" S* Z! X  V3 ?, ?
一.准备工作
9 y& p6 z6 U6 A% T& s- t: i
9 O$ d( p* D+ N5 T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ U- _  U" }" ?$ t4 ^
0 s% _" d& }$ ~7 h) s
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 P" D, H( j6 P0 w6 X8 z: N/ D6 z& Y+ {; W+ t
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ Q, c0 w6 ^: s" C* c( T: U& b. d- w! u4 N4 f0 [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: a8 u: L: t% p! H& s1 K5 r

+ P6 O7 _7 t2 a' m依赖关系:# R6 P) n1 [" Z7 s! x' c9 i' q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 }$ f( o& p( M8 J; w( e6 E3 v) C' F6 f) U/ l# y3 l9 ]" D- ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 Q+ n, }6 |7 y, I
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 a2 M8 D/ n8 D3 x4 n5 H+ }, b7 g" y$ r8 r1 M; P' m( W( G  t
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 I- w8 o, o/ \/ w  h7 c
二.启用standalone模块并编译$ q& H& x+ k' ], W! |9 y

, \- R: {: C/ P' J4 _* b下载modsecurity for nginx 解压,进入解压后目录执行:2 c  l3 L5 l' T4 f

9 X; L% K# l/ B4 |7 ~. d3 i./autogen.sh) B8 t; f3 b" l6 x9 ~
./configure --enable-standalone-module --disable-mlogc
! f3 G6 q3 @+ w& kmake " ?9 X" h! n1 n+ J
三.nginx添加modsecurity模块" a9 h: [+ U; t
- h8 P  T- L1 K' R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 L6 B8 U4 n2 d8 q# E

1 B% F8 [/ Q4 r5 y$ {6 c./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" `$ b; _8 e( n& n$ Z) S6 amake && make install
  p1 D# l0 {2 m+ Q0 `/ K2 X6 D- e四.添加规则
. y0 P7 X7 [7 D+ S3 B5 V( n
! F4 K, F7 e7 b$ w4 Qmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. \, [3 P9 v( e" [7 |8 F. b

( O; M1 x$ k1 b  z4 d" d4 r1 g9 d1.下载OWASP规则:
* ~* r7 u: Z! _' l; S! S6 i" R* h& F+ ^6 a/ i, Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( [/ x' p: a5 M( `; `6 r- v" |1 s6 h  X: w4 e/ _3 h! c4 @' L3 T# s
mv owasp-modsecurity-crs /opt/tengine/conf/
& J) Y' S# K9 y1 W, Q0 C% |: S# W; `5 q" a
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 N9 w5 \9 ^3 x# y0 g2.启用OWASP规则:  v: \1 D7 q1 P9 G. W. q& k& ~

% [# o3 c3 }5 }* H& d- M5 m/ t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: a2 f, K/ X# }2 Y5 T( a; R* }# e( W+ }  g7 [! @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 s5 t2 c# T" C5 ?# e/ n: p
+ ]8 C! I( J& d5 k' d0 B8 Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% x, C) |. T, O* Z0 @) a
- W- D5 m- O; I/ \# I, sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 B  `3 q! v1 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 J; t$ Q/ ?' k, [! b/ l( r$ m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* i1 x9 B7 Y3 ~; B7 S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# \: I$ ~) \% `! \, z, e0 R( cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 o& c) q) a! j* [3 C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 O$ R* y& D$ ^$ ?7 v( i7 q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 F  ?/ n4 a* Z% k# K, s7 s五.配置nginx
. G8 S/ K8 V- V2 t- Y! J, s" C0 |+ ~5 x
在需要启用modsecurity的主机的location下面加入下面两行即可:* i' T' {- ?  P4 ^9 F1 E8 n( G

, r2 Z  `' ^4 a& f2 [) OModSecurityEnabled on;  . x& Z$ \+ q  k6 z4 e: {
ModSecurityConfig modsecurity.conf;% t( X, Z7 K+ Y$ P4 f* G6 Z) x- s
下面是两个示例配置,php虚拟主机:
  q( E+ N3 \& _5 c4 [6 H7 s4 r1 J5 |% {( \. l4 B( l
server {4 p' I, p# _$ F6 Z4 W6 ~( a
      listen      80;
% M- U7 N/ ~( h/ o# Z9 A- s      server_name 52os.net www.52os.net;
  ~" {0 G9 j0 m+ {) @5 O     
* \3 d7 D. `  ^/ {      location ~ \.php$ {
: ?0 r2 @8 T( z1 j      ModSecurityEnabled on;  
& `8 h0 \7 |/ A# x/ }& Q0 ^      ModSecurityConfig modsecurity.conf;
  K* `% |$ A7 E5 f$ v
  U  e- X. R* g7 f6 r/ t* i4 B      root /web/wordpress;
2 [/ j$ t# z( T      index index.php index.html index.htm;
4 t7 a8 q! r* m" J# ?! v* A  
& e% q% S8 F, ]/ T! D      fastcgi_pass   127.0.0.1:9000;/ m6 {+ \0 L; [  ^
      fastcgi_index  index.php;
) I+ B/ H% J0 Z' U, E      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;& ^: d2 N# w# n6 k# o1 j( K6 _
      include        fastcgi_params;$ r# b( h2 d( a. t( t. W
      }8 _4 L8 M! K* [: `: a+ {3 t+ F( x" H
  }  W1 F" C  |* N) P
upstream负载均衡:
! O: j) [# s9 `! h( U: Z. O) v* x
, t1 N* c, |" {+ s4 C. q/ Oupstream 52os.net {; J3 e, J. G1 `& \; }
    server 192.168.1.100:8080;
1 {8 ^. \4 w( U$ X' R5 [7 Q" ^$ \  ?6 \    server 192.168.1.101:8080 backup;
3 I! ^% O0 Z2 K}' P9 t8 t$ n2 Y* v. {4 j* _
# x' c. m) Q4 h6 z8 Z! j$ C$ k- t$ }) J
server {/ A( }# ?& P6 k4 Q
listen 80;
" e8 r/ M- y0 b& o/ J. i! mserver_name 52os.net www.52os.net;0 \7 U4 R( M& G$ s/ E! c, a9 A
) J; k+ k. X, X8 F$ u1 U' ?  r5 P
location / {
1 l8 y) I- v: y7 }: P. K, b7 ~    ModSecurityEnabled on;  
" `7 ?$ w7 G$ i( S  c/ h* M    ModSecurityConfig modsecurity.conf;  
' `  _% M* L8 h; d2 d
+ T- A' W6 j% ~) @        proxy_pass http://online;
" M; {, J# W. m+ u+ A9 S  J        proxy_redirect         off;& N& a! V  H0 Z2 l
        proxy_set_header Host $host;
* W! ]' ~. z1 O$ e5 d        proxy_set_header X-Real-IP $remote_addr;
. z' y  L# b( Z; u; X6 a1 ^        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# n$ u, B9 H) T# k' P1 V# A8 V% u
    }" h1 E' }, \6 O9 r; p1 e
}1 _! Y: K0 j9 @9 P6 @
六.测试  Z' `0 i! I8 t3 m. h+ H) s
6 h) ]& V. Z, W7 W8 Y0 s' G; ]) ^3 v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& M6 X8 L9 T: X: T/ Y& h  m2 t* T9 \7 l$ K
<?php
/ S6 j0 p8 w8 X, W% \7 Q    phpinfo();      y7 r4 c4 e! t! B$ C! @
?>
: E/ F0 ?: W$ }- f7 G0 J$ Z在浏览器中访问:
( V4 i! |* j& _: e& T: j2 [( `. k
9 T7 ?( x( t! q% R3 a7 A/ Qhttp://www.52os.net/phpinfo.php?id=1 正常显示。0 q% y: U! e, c4 |
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
' b4 G: r9 i% c! A6 F( a8 khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; Z; S( i3 e. n2 C3 A& m" I3 j
说明sql注入和xss已经被过滤了/ Z, a2 H! Y2 i( p6 [2 ?

9 D& l+ y- }# j) [( `9 ^七、安装过程中排错0 m" K. O* e# f, I1 U- |
6 T& u7 Z- O, M1 Z8 m
1.缺少APXS会报错
: ~  p& C( t7 N( Z( k  b! H
8 S3 V1 b* c* M8 E& x2 dconfigure: looking for Apache module support via DSO through APXS
9 K7 J4 \) m/ F; T. b# B: c* Pconfigure: error: couldn't find APXS
; \. G. [  W$ capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 g( Y1 g/ Z* L1 o# t. j解决方法:( T0 P; P1 C- R, Z
" P$ G! S3 p4 F) ?. V2 ~  G
yum install httpd-devel( Z9 b1 p0 f1 e4 [
2.没有pcre6 d0 k( I9 ~0 s9 g

0 N7 f$ n/ }1 w/ o0 a0 econfigure: *** pcre library not found.* H( Y6 ]' U7 q9 f/ D
configure: error: pcre library is required, b0 @4 D/ j  c4 |% N- _( ~' S) l
解决方法:
. l/ [% u, H( h0 ~+ w( Y7 s
+ V* |# {* B6 y2 {7 X9 myum install pcre pcre-devel
$ [3 f5 i$ b* U1 |1 t' o3.没有libxml2
2 D+ ?# _; G" j! `* g$ d+ ^+ p' M( _2 h9 ~( e3 e2 K! \) Y
- g9 \  l) H( O" E  n$ n; f
configure: *** xml library not found.
+ c  w4 _) ]6 w1 }( p4 ?- u6 Qconfigure: error: libxml2 is required
+ @8 q2 i4 o, x! @; l8 Z解决方法:" ~: P1 Q' G; Z7 Y+ b6 l1 k- e6 m$ @
/ B+ p5 A) D3 |- P9 I3 ^* @3 O3 [
yum install  libxml2 libxml2-devel9 G# `" w/ |8 Q% p8 u% a
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' d! j" W9 [- ~6 F
  o. }1 m# X0 @. T3 Z4 STengine version: Tengine/2.1.0 (nginx/1.6.2)- l' E, X1 W  a9 @/ s4 m  |* [" \
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!) P, A7 G2 W* o
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: ]! F7 s2 y: R: |3 U) |" L
* M, \: L7 n5 f- Q9 V" P  u1 ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; F; V# _7 y" E2 w$ N7 o. M0 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 k+ k% k2 ]- Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, k: o! H  ?& l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 i0 K7 e6 }2 p+ L% t8 U) S9 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# l. V2 Q4 a5 j7 k: y; A4 g; A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." Q( H- o: J* `! i
解决方法,移除低版本的APR (1.3.9)
9 M. b5 Z7 I' H4 u  p' H" F- G2 a  Q% h( I/ Y
yum remove apr
/ ^4 g4 v+ w4 x0 y* M8 q5.Error.log中有: Audit log: Failed to lock global mutex
7 F% M7 U/ c, f0 p
9 y8 b  Z. [7 F$ W7 _3 t$ H& b0 s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ P/ j) m( f# g6 s! N5 y/ ]. T5 Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 p$ F; Z  r4 }; r
解决方法:1 ]0 O: g# y. ^! C: z4 X5 y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" e# _, P1 E7 m% I& Y6 K

" m" [$ P9 H9 B' W1 O: o& pSecAuditLogDirMode 0777' n- V. j9 q6 B0 h# W7 N( }
SecAuditLogFileMode 0550$ w1 b; I7 \, q3 U5 ^9 G
SecAuditLogStorageDir /var/log/modsecurity
/ P" X- w& ?; W, |6 i$ zSecAuditLogType Concurrent1 c$ _. }3 i7 i- o' \- {1 m
参考文章:3 U5 A  S8 R0 \7 N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" c. v; ]$ B) M' i) C; b% r$ O+ T/ y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-22 07:53 , Processed in 0.071491 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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