找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8759|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ t5 R4 S7 H: e' H( h) |7 m3 ?
3 U& f/ I3 K/ D; {一.准备工作
, Q  H. ]) H- J7 r' G4 o* V6 o* S. l$ L' c, ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 s* N+ z- {2 a# q3 G7 _9 a

( \  g0 n- i4 R3 S) M7 ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! V0 p  i0 L* Z+ e- t  Y5 j2 [- @7 ^4 O  J1 p( ], c
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 B( p! i# a5 e! }5 G

1 m, L5 h7 J9 S! I8 ^( OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) Y% i7 M+ A; H. l% c

! `/ M4 t( m6 v% {* t/ p依赖关系:8 G& a2 P0 m" L! l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% R4 \) e8 Q3 s
  T4 Y9 G( N% z9 Y( k7 R4 T) Wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel& j: A& A) i/ h9 I
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ \) R( ~: z% |
% r% X: K% Y1 Y) c) V* _& S
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& B0 i/ ]0 ]: I1 o' ^4 i二.启用standalone模块并编译
4 y# C/ F5 [7 k, J
: y- F1 H+ v1 @4 R' P  [下载modsecurity for nginx 解压,进入解压后目录执行:
/ j: V' d( q- G* R6 R+ j+ C8 ^. @0 P7 i" p; c# v
./autogen.sh# f5 R  l. p2 K: H- [6 k$ z& b* u
./configure --enable-standalone-module --disable-mlogc
  [, Q) A" ]& }! o  Amake ! g2 }" s! `1 v& @  m; a& f( c5 W
三.nginx添加modsecurity模块' P, B  f! n9 L6 B9 l- f

$ N; t: N6 b# G2 @, Q  [8 y3 X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' d. M3 T8 ~8 p# [  l' _& e0 o+ r5 Q7 S
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
1 V% }' z2 z* _# emake && make install8 Q; B3 V8 T) H9 t4 P
四.添加规则# e' C' D2 {9 d) o" l

. o5 l2 R" \3 N* |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# a( o9 [7 }8 t9 h& }
# l1 }' ^2 F' c( Z5 Z$ P6 I1.下载OWASP规则:$ I% ~$ m; d7 e$ ?& t

* H* S1 T& E8 R+ tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- F2 k3 Z+ M+ V& @( p' p2 a( h% v, a
4 M5 f9 n$ ]# _. b  b: t9 g- o" Q8 m
mv owasp-modsecurity-crs /opt/tengine/conf/
9 M4 A9 W1 N* q  d; E* A' H
6 E# X8 J  b5 _4 T. l) }! hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 x3 `/ @( H! T& Y' k7 n2.启用OWASP规则:
! R! f5 u& a. l! S& b+ I! W  k4 s( |! x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  E- N) {9 S. }5 o9 a. `3 v0 z. @# U. d+ U  i9 \
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) G# C" K: B) {- o3 t- p. M
! w5 F/ h/ J) @! [
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* H  w1 w  N& D. F6 R$ a0 t, F% T. `$ x: j$ P; E
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 L+ t; Z* v2 S- I) B. V" B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 G+ t+ y& B) N+ WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 G' B( n! s' A3 R+ X% A7 `$ bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 q9 f/ C- C- T$ T- S. EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 B0 d5 h! J, c# J/ J, \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; ?5 b4 r4 i4 W! J
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* ^/ w* o% j+ D五.配置nginx" o8 Z# P: r3 V) R

+ T8 L# J/ I1 x: n2 q8 G在需要启用modsecurity的主机的location下面加入下面两行即可:
' o, l: e8 L- L2 }! U, P  z# ~8 A# v: G
ModSecurityEnabled on;  
5 {$ ~% Y; A: {# p* y; rModSecurityConfig modsecurity.conf;% L+ P& V2 _9 w4 a) D) }( d9 d2 H7 J
下面是两个示例配置,php虚拟主机:$ `0 m4 F/ Q& r' F" x3 x$ w
" Y; G/ ^5 }4 d5 U, G  }
server {
: ?" [$ O8 |' W/ o5 M  W& ~      listen      80;5 {- D" h+ j0 M8 p) y
      server_name 52os.net www.52os.net;0 n: u+ }. v. M
     6 M! H! n6 I: _$ a7 s* c# S& f
      location ~ \.php$ {* T6 D; u2 s1 z- m( H
      ModSecurityEnabled on;  + j4 R9 H( a  p( S+ [( E; i
      ModSecurityConfig modsecurity.conf;
# D  ]7 r/ M0 A, r  U: X1 r) ]- y) e1 g/ k5 L0 k& N# |+ A
      root /web/wordpress;
) E9 Q& w/ R! x- o" b  m  E& W      index index.php index.html index.htm;' j/ U4 q- y1 i# D7 W4 |' Z- f
  / m- N2 D$ {. K
      fastcgi_pass   127.0.0.1:9000;
, V# i; V$ P3 q3 j/ g1 J5 {, z) P      fastcgi_index  index.php;
! b  G9 R8 {9 c8 t& b" [      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;/ ]7 }* z5 e: [! D
      include        fastcgi_params;
# o+ J$ W" N3 y" k, k$ ]5 y      }
3 X) A) q: k, W0 r9 p  }
1 F, I" V1 ^% ]/ t2 aupstream负载均衡:
4 r$ T$ Q( e: J' `$ t8 u( g- C% N& d4 E( e$ ^# f/ d- F6 [
upstream 52os.net {
! h$ ~0 G! q( f6 d% N4 H$ R2 Q4 Y* ]    server 192.168.1.100:8080;
" i1 N# p  V. H9 C9 i    server 192.168.1.101:8080 backup;* U8 N8 t! a: y2 g7 C; r7 u/ q5 X
}% r9 N+ V" q1 J' D" b5 Z" A8 W

& E( e- f& l% o) J5 K$ eserver {* \/ ]4 R6 G( p3 _0 v
listen 80;4 H. q* N" K3 B6 u2 Q  n
server_name 52os.net www.52os.net;
6 H; b; g* k7 y- [$ j6 L' ]& ?6 D' [5 u  ^: g
location / {
3 Y. O" w2 J; T9 q- G    ModSecurityEnabled on;  
/ |7 j0 \4 u5 e1 e. |' x0 T    ModSecurityConfig modsecurity.conf;  
8 b8 k& l7 Z) i7 R, Z! _# X
, C  I% W0 K, S- I        proxy_pass http://online;" W- u( T. d5 {
        proxy_redirect         off;0 n& e, H% t) L3 M) _5 o, w4 c# h' J
        proxy_set_header Host $host;& K& b7 A4 B9 H% o
        proxy_set_header X-Real-IP $remote_addr;
6 q0 z  P6 B, Z# u' b        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; }; _9 {& h  s' `
    }
* G/ Z5 |. V, t# v8 }, L}- S3 j  B5 d( }
六.测试
  V4 {& b% ?! T4 D  b% U% D
* V9 D; w0 ?3 P* e" W+ y) z. N$ K+ x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 _: p! L6 T- ^% l( Z* L( |9 B6 m' P# E# U5 Z5 H9 Y# ]
<?php! z$ n) k0 H0 |) Y8 f/ N" @
    phpinfo();    : Y6 e0 {' K! ?3 W4 v
?>2 i4 m; W! [, X& B
在浏览器中访问:/ a' |2 _# A! N+ k8 `
+ j) t* q. n9 z! r9 H
http://www.52os.net/phpinfo.php?id=1 正常显示。
5 e6 D( E/ M% Y' _% |9 D+ z4 J- whttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 Y/ A) }  Y7 m: Z0 A2 [
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
$ r8 w! C. ?3 \( V说明sql注入和xss已经被过滤了
8 h( Z; T/ g4 b+ p4 O# E, ^: m3 H5 U7 V* ^0 Y
七、安装过程中排错8 F/ Q+ v3 d0 a
) ~  a- H) T8 ], F
1.缺少APXS会报错
0 ]0 b" P! b( r/ B+ R+ x  x2 _) G5 O" _& y3 I2 b
configure: looking for Apache module support via DSO through APXS" V0 H9 v7 y; Q- R/ z
configure: error: couldn't find APXS
2 O$ [- y: ]2 s5 E3 R! h& Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, }0 c0 H$ y7 G' m9 W- H, f7 U
解决方法:
8 S7 W4 w8 J1 f% |' Q% Q& J; c. |  I) }* b% C' M) s
yum install httpd-devel2 N- N8 X/ _5 I- ~. h/ x
2.没有pcre( |% f, L& P, s0 ^$ R  Q% J, J
) g5 l7 r4 n4 C' ~
configure: *** pcre library not found.0 v" r  a" ~. y- H3 X' _
configure: error: pcre library is required
! B6 t8 s! Y! j1 s: q( ~解决方法:5 Q/ J4 f4 _, Y8 j

, c6 J" i1 s( H% _yum install pcre pcre-devel3 G/ p% M- r  F+ p
3.没有libxml2
  U8 _' H; s# N9 Z( r0 @
  [8 ]$ Y4 L( Y
: w( A/ ~. R, @7 ?- e0 v0 F' \9 fconfigure: *** xml library not found.
/ b$ @/ F' n+ u, C$ u$ ^; A: sconfigure: error: libxml2 is required3 \- R6 Q6 V( x8 v) h* k0 I
解决方法:
) M) w* h2 K+ g# R4 `1 {1 q
, A- U/ Q* f- B: g$ X- M5 B7 t$ |yum install  libxml2 libxml2-devel+ J, o! t4 h9 ?9 {
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 H+ Q9 ~& O' K& J5 [
& L% O' k8 {7 e0 ?: ]- p" r; B+ Y6 Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)# A* l. c. L( _% U9 E8 l" f
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 f- _  |- z* k& t; j/ ?3 X/ @; [原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 R, e/ P. L+ w' K/ E, M+ V# {1 p. |' Y) x* f6 x2 |/ B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) I7 Y" I: F0 X1 S& X! r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"$ u$ c! `* u; c! ^8 t- B: A
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, y2 U5 U# w( o* J/ ?2 P: v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". D$ ~0 G4 W9 k3 }) T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 u9 R0 R- f( X/ ^" ], T  i, D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." @3 M9 P8 b! N6 P
解决方法,移除低版本的APR (1.3.9)$ h9 {' h/ Y1 }. L9 C

; a/ p  E4 J' n) Q* O+ \9 f( _yum remove apr
- R9 Y- }( `' M/ R' I% {5.Error.log中有: Audit log: Failed to lock global mutex/ n1 I' P" y) M: i) e

  a& a6 u- \( R2 D3 O2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 f. @6 j& r) m9 _( D! xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 M8 x. Q' p$ n# i) S解决方法:
0 _/ E& u; ]5 B9 E+ ~- H* W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! D2 C9 Y9 l6 S5 [, A
* C  N7 w/ v# j+ k6 A" ~( wSecAuditLogDirMode 0777) V0 \2 p" t! ]2 i4 x
SecAuditLogFileMode 0550
/ {. _4 y# n# n$ t* ~; E8 g5 HSecAuditLogStorageDir /var/log/modsecurity3 ~4 F8 r0 c9 L) Q4 c
SecAuditLogType Concurrent0 s3 t7 C$ u1 r! l" t" @
参考文章:& \/ m* f$ o1 k8 a. R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) i' E9 U2 D$ @/ A; \% S0 b
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-9-19 10:00 , Processed in 0.075397 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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