找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12047|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 y+ t  K' m4 d. R" y9 [! }- m; E  ]* a& O
一.准备工作
5 E& R( A! c) a
8 V$ |  i. R, ~- v9 F系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& B" t" `2 {7 [* G1 K

) I( e: a; b  B+ v6 T5 \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* O3 H: x. ^, M* w# a* ~
  Y/ W! V( }1 c) @7 Jmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! P7 e1 Z  T$ V( U% |+ H
& _# x! X0 g4 ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ B& X3 B5 Z# _% ]
8 {# f9 ~( V" B4 X, g2 Y依赖关系:9 x! X1 ~: u+ i0 C) m
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 R9 l. }* b# H
" L# N4 `2 @9 h7 P. Hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
# C4 H  g) o0 b8 F, [( U# _modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 d/ I& d+ H/ W5 t8 |) U1 R# R, ?' {% P0 v5 z; ]; y7 N; z+ S
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel3 o) x* q* Y) N& m& p3 `
二.启用standalone模块并编译
" }5 t+ L& j! x9 g! u9 u! h- H( R) x! Z3 G  q6 x, J2 s  G/ v
下载modsecurity for nginx 解压,进入解压后目录执行:1 A5 O$ L+ [, A1 B2 ^" g3 s

) e: W2 e, c  P" O! U0 z./autogen.sh/ e3 x1 i: x/ i5 p
./configure --enable-standalone-module --disable-mlogc
5 r$ A/ V  n$ w9 v6 V6 imake
- B$ }( [% i7 T. W三.nginx添加modsecurity模块
! D7 x; a  \  G9 r- y: n' E
& L! R4 v1 S  s, R; K' Q" J5 Z. m: L# v在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" l6 D* ?# f7 X8 o' P5 x( W4 k$ ~% O0 M; T. o8 g( l- `  e3 N1 P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 V; U4 I6 G# P" S. m1 `8 T
make && make install
% l% ]' m/ F6 y. Y9 w0 L0 D四.添加规则
: {* e: R% x& @1 z2 s5 p/ A+ U% i9 `, O" b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' [- d9 A. r2 h+ d% C# @' O$ S

( B3 }3 [2 e! y/ v& `2 a1.下载OWASP规则:
  _2 p5 w  I* I( a& b
3 f7 |" x8 b1 s. Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
  u* u9 g; `; }# C4 `/ C' ~$ s$ K/ U
6 Y4 h; M; L, Nmv owasp-modsecurity-crs /opt/tengine/conf/
7 j7 ^( r. N' E" ?0 c3 }: [- r/ M4 K4 J4 Q+ _4 T* M' A
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. D5 I) S: y7 X$ h1 D% P2.启用OWASP规则:
/ v2 `- `5 W+ Z. b2 j# ^1 o, |: ?$ R6 r# a
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 T9 S* D6 _5 H- {! x

) V, E# F1 c9 J6 t" y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' n5 Y& a9 {& @8 L. O9 ~; @( `
, y+ j- ^2 |7 w( _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) [9 n2 D/ V4 d, b9 C2 f1 j

6 F- c5 E5 K" ?. o6 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 E) `6 S  @& x4 q& L( B' R! QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" f9 ?# z9 O3 TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" o+ T# j9 }7 \; U2 q+ m6 F. {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 l, B# {; Y$ F7 Z0 ?1 _' n- V9 ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! b8 m" V" ?4 X4 RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ W/ H% l* l' B9 ^: v% f3 rInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 Z; [, Y8 m( ]' w% h五.配置nginx
5 N6 v& E, z  ~5 u7 [; j% `2 Q6 E9 P7 N/ V3 F' Y+ f- ~
在需要启用modsecurity的主机的location下面加入下面两行即可:
" j8 f7 ?! _/ s5 z- ]$ @. t' C7 U* {# A' N+ O) Z8 o  O
ModSecurityEnabled on;  
5 L) M: y/ ~4 w( `1 x% P+ e7 nModSecurityConfig modsecurity.conf;* k9 c) F) z9 _
下面是两个示例配置,php虚拟主机:
0 b+ z* ]1 J0 x# A! J$ J+ |
6 `9 @$ P5 p* R- ^/ L, B- wserver {4 q9 W! i* ~, r4 _7 Q% ^
      listen      80;
: Z) ]3 b  t1 {( y2 Y      server_name 52os.net www.52os.net;8 a6 l( t" Z- a
     " J8 x; |! k- G4 |1 ]
      location ~ \.php$ {
$ Z- a# I. n" Z: H$ F      ModSecurityEnabled on;  1 B. X: ^' h$ f: y- V0 w
      ModSecurityConfig modsecurity.conf;
  n0 E  }: X. Q% x; r7 f
$ Y" c0 O  b0 g4 m4 S      root /web/wordpress;
( L: l6 J+ G' k2 s6 k' g      index index.php index.html index.htm;
; u( u& @  X8 ?& Z6 a  
& _) s: W7 d+ k3 R4 _! n4 I      fastcgi_pass   127.0.0.1:9000;8 c$ Y3 R& A& i0 g" ~
      fastcgi_index  index.php;- L1 e; f: F% c- c8 |
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 V2 \# ]6 F% C$ L+ ]0 h1 F
      include        fastcgi_params;' ]- ]" e4 i# e  _/ c, g
      }
( g4 `5 Q: r0 X. Z( k6 X! z  }
) S9 E" e% D9 n$ N; Y5 nupstream负载均衡:
0 P9 y3 L' |" b9 A8 w" t- d2 f5 z3 N6 R" |
upstream 52os.net {) }7 l+ y' _6 l. L% Q/ b; C
    server 192.168.1.100:8080;
9 o$ ]* f& V+ }3 Q/ J    server 192.168.1.101:8080 backup;. A* H5 S8 T) b6 E1 {8 h
}  k+ ^% R  s$ A  B& x$ M

/ }8 l7 Y; a) M' T7 aserver {4 C4 `, Q: L$ H0 @& z0 o
listen 80;5 G, c( g1 o! d7 g1 M* E- i7 Q
server_name 52os.net www.52os.net;
% G/ l! D( g- a% D3 c# v$ D% z+ _! P
location / {& a6 X6 k! f  a# l5 T
    ModSecurityEnabled on;  " C( L+ h3 _. a5 k( |
    ModSecurityConfig modsecurity.conf;  6 ^: ^1 p4 C) |

: w& O  J' @: {6 ?8 P4 f; \        proxy_pass http://online;: m; @2 `, N" w8 R
        proxy_redirect         off;
* ^5 E" j$ Y" a' x1 h1 j) Y( j        proxy_set_header Host $host;3 v2 l& N( B% Z1 F4 ]: h
        proxy_set_header X-Real-IP $remote_addr;( P! H- W& ?2 M+ I
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ @7 ~# W' T$ x, N0 ?( l    }
% Z3 |1 j2 j* |+ ]( U9 a}1 O* _. |" N2 s& k' b
六.测试
- ^) G$ G0 C2 x+ `$ K, N0 \0 e( Q; q4 z+ C! Z8 y8 d+ B! F4 n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; L) |- Z' n4 e2 [
# ~* {- A0 o7 S& a
<?php
8 [4 {6 \1 D+ s0 M/ z    phpinfo();    2 D$ O+ ]# q. g( X& M& ^, ]
?>
9 Q. [- [/ L: l在浏览器中访问:" g! |* n, d! ?6 e. O

  f7 Q  f1 b4 m8 S$ whttp://www.52os.net/phpinfo.php?id=1 正常显示。
  H2 |  P0 V2 A/ }5 j* J( Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. Z# D7 @7 C1 Y7 @' y+ z& Xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 V4 P( n) l5 @5 ~1 M说明sql注入和xss已经被过滤了( X7 L- t& T' |+ [
# S1 @- Q: E8 ^# Z/ g- I" A
七、安装过程中排错; j5 {, p: J, s) x2 ~+ _/ G& e
' y& O. H/ d/ F" g6 }
1.缺少APXS会报错
" x0 v- ]; u# X  R
# g8 V4 W" q) N4 A3 a1 v! K; ?1 A; kconfigure: looking for Apache module support via DSO through APXS6 R; u: q& y6 X* S' X! g
configure: error: couldn't find APXS" q2 ^2 u0 ~# w% o. ?  I4 p- G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 k0 s3 j4 G+ @& u  z4 d& P0 Q
解决方法:% U4 X) [2 l9 v: L2 F$ R. K) x4 c) v

3 V6 r1 B# x# ~* J+ c1 Fyum install httpd-devel3 G7 a( D4 {' \9 j, N
2.没有pcre
% \2 ?- Y  T4 ]1 f. g  V) n! |4 D8 q' o( J2 J
configure: *** pcre library not found.0 y# k( [. M. b2 U
configure: error: pcre library is required
, e; g) q# l  l5 U& i解决方法:  y# Q  }7 W: r, U3 r1 T4 x

' t$ \, e6 z: zyum install pcre pcre-devel
6 E9 B$ S; k1 p" |9 P$ y3.没有libxml2
8 X! E3 d/ o* X: C. [8 {) a
4 C9 O; v! }; _- f  `2 u5 `; L6 O
configure: *** xml library not found.
1 e  [; r7 U& @, wconfigure: error: libxml2 is required6 p. e4 r1 h- }$ x+ S* Y1 E- C' l5 T
解决方法:
% x' L# r$ F. v0 m9 I5 @# i
+ |. N" Y8 J1 u  K- eyum install  libxml2 libxml2-devel
1 `1 l7 n3 z" v" z2 J( W# V4.执行 /opt/tengine/sbin/nginx -m 时有警告
* p7 i: T1 ]6 A: p6 N
/ |, v, a7 x8 S! UTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 J2 V* |+ w/ Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!' ?1 }) [8 ]& Y$ W5 ?; |: i
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 \& T6 P" T- k$ N, j0 x; ^
4 z; i8 m( F4 j1 C1 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 d6 z- m/ Z* e% \+ D; X, _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"9 x+ C" a+ F+ k) }/ I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 r- p, q9 G$ R5 w$ Z& k# H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( g3 R1 w; g2 y7 Q9 E! K# A  g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": g8 C5 A( V8 A$ Z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) R; m  n! n4 c: N9 ]" A6 I- d解决方法,移除低版本的APR (1.3.9)
8 \0 E# j% `; r; |. d- U. n! ^" Q+ V! T& g1 H$ N6 g8 H' m4 A. j
yum remove apr
. \, L% s7 ?# |1 d) H, K5.Error.log中有: Audit log: Failed to lock global mutex' L2 s/ n5 m4 v9 A9 v7 _( }- `
1 O: {7 J4 ]7 q0 Y3 h( |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( H! I! f  i: G. _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* L( h3 ^7 V" o  T1 C, \
解决方法:/ u" E; b6 ?  H1 v! M, V
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ f& g& V/ Q/ o: b2 |' |" O

8 h4 S* v, m& @5 RSecAuditLogDirMode 0777
9 p! i2 l  W- m) {5 W* p/ xSecAuditLogFileMode 0550
, T9 g6 b/ V! U( E% USecAuditLogStorageDir /var/log/modsecurity3 t/ ~% {* A9 J, s
SecAuditLogType Concurrent
* v& x: [; b# Y% X$ O参考文章:
2 Y9 q" Q# A4 P" b& S5 ^1 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 a; v& M. K: {0 |
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-6 02:25 , Processed in 0.079700 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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