找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12475|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  Y. k" w* b, e( u6 E2 |! f" i! ~
5 k$ b1 W' y! C3 i3 u3 \7 P一.准备工作
1 Z' e, }2 _8 ]4 {/ z$ Z9 a4 h
3 ^, ~' e+ T* j" ^$ w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 Y( E5 h9 }6 Q9 C: Q4 ]0 J8 h+ h7 I+ Q0 o9 n+ ]0 a/ ^+ v4 h3 K1 L% I" f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ N+ D& L$ {4 W- ]( X& d" a+ L/ [3 S9 }! P
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 M1 ]4 _" W% e) N" t" Z, `6 }% ^" [0 z9 }8 t- y- M
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- v, [# M6 k8 S3 d/ _- ?
% k4 l" V. s0 I9 L1 ^5 O  S
依赖关系:" z& A8 ]* ]; G# r0 t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; A* G4 X: E; e; W$ R9 Y/ R0 P6 r$ }( D) ]' [) z. U" a* ~5 {, O
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
8 c0 I9 @3 Q. z5 c0 r& }modsecurty依赖的包:pcre httpd-devel libxml2 apr
! h8 h; {% S) U: d4 ~0 j
0 U) }. J  v# x6 o% u: z2 m% zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel; h( i* `. M- G5 {! ]6 k
二.启用standalone模块并编译
9 G  S8 X) Y% I! `4 D
, x% s4 }: }3 ?3 H. T7 t下载modsecurity for nginx 解压,进入解压后目录执行:
  |! u' \( ~% |7 }8 C9 r/ w+ f% j6 k- T7 x6 b/ T
./autogen.sh
+ x- s. i  F5 I- U. g$ `/ J! E4 S./configure --enable-standalone-module --disable-mlogc" M5 f! V* ~* ^3 m" N6 r
make
; `8 @9 S' i: e三.nginx添加modsecurity模块7 u# k4 s  ^& n) G* C6 n( H% t$ k

0 z! g) X) t% p5 z! h& W1 {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 K, t# Y8 x/ f* x3 s3 i, ?0 }; k: k; y* O0 C# Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 g% N/ v1 U3 V1 W6 J& ^
make && make install
; R3 J( R& e% ?. m" `1 |4 L4 J; K四.添加规则
9 @  N+ a, @5 D7 p) r
6 \8 v; N( [, ?  Z( ~4 Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: o, P) \% Y5 X! _9 H' c$ I

' g+ U  u" O% r8 Q1.下载OWASP规则:) J& K% s9 S, O! k4 B

' @0 z) ?: o% sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 U. B4 I# v) e* k- z' C. _; P1 p7 R% X1 T
mv owasp-modsecurity-crs /opt/tengine/conf/' N' `: o5 Y- w% z% G; V2 u

8 I$ Y. L4 L  c% \, F6 B8 g* Dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, n- e+ u. f& Z% x2 X* |
2.启用OWASP规则:
9 d/ {. Y# k1 Y3 h* ~" @9 z8 D# f  W% j% {( h! T4 Z# [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( A6 ~9 }6 O0 |: G+ H" c
1 m, ^& j5 q) ~# z! P: W4 q0 O9 N编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% K& a# `4 c3 ~8 k# {6 N# m8 |4 o8 y  @
4 ?' ]$ `8 G( {
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- m( L2 ]$ W  p& p" g

* c7 b8 M* F; V; P* qInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  `. B) y) e# r' Z: C3 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 ?$ y) |) p. p5 K, s( P. iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) J6 R6 |/ a" w; uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% ]" M' `; x0 ?: E/ qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ j+ x! N  V+ v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% W! W+ c% N6 _
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ d9 L; A( a3 r. w0 Z) A五.配置nginx' q! X5 a* F* I# d" c5 e

0 {3 L9 p1 |" N' J) m! [  ^在需要启用modsecurity的主机的location下面加入下面两行即可:
$ z5 U( z! y- x% k3 I0 ~% m  Y
' U& q! V/ l2 tModSecurityEnabled on;  
1 K3 g+ h# q* y# t  w+ B' l6 H2 yModSecurityConfig modsecurity.conf;5 L9 T# i7 o, l4 Z
下面是两个示例配置,php虚拟主机:8 h% Y; l7 U, U

$ |9 q9 x  f7 ^# `3 `server {1 S1 w% l# j1 o; f5 @
      listen      80;" N6 k: l) K; \- N, G
      server_name 52os.net www.52os.net;3 |+ I6 H9 c  I' p9 M
     
6 z  P, ^+ z) Y2 W. `      location ~ \.php$ {
) ^, {9 I% K$ _      ModSecurityEnabled on;  
' t; v! O. J5 X      ModSecurityConfig modsecurity.conf;% R5 q# Q' `" f( M( c) h6 u- x/ u

3 o4 c( j2 B  w8 I3 H* N7 q% Y      root /web/wordpress;; t. g: T& d" j$ s$ @: o! L4 J
      index index.php index.html index.htm;
7 o! B& [4 |' D  
: _$ ]) _% \, G$ X: _& F      fastcgi_pass   127.0.0.1:9000;
' S" E; W3 i  ~6 s1 p1 b      fastcgi_index  index.php;0 H4 K- y6 y  ^5 j& o6 K/ S
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! t/ f+ m  c6 T- {7 W/ L. ?      include        fastcgi_params;
. h0 i+ O6 J9 I$ _+ [: y. H/ Q  \! i      }9 `# T  t1 ~- E' E, ?: C: G' F
  }
" o* \1 p3 U0 u% y" u/ G0 yupstream负载均衡:9 _  @' T7 K; U4 l& p/ ?

  _  U: Z* w4 O6 j' p- |4 P/ b: tupstream 52os.net {3 F- d" v9 E. _: H) |/ I: J
    server 192.168.1.100:8080;
5 r' M% Y" z3 V6 F& U% x) ^, T7 Q    server 192.168.1.101:8080 backup;! @$ y6 B, n' V* N* {$ f4 Z
}
+ t" r" s5 [. s
5 S- @) v+ h0 t* H% h2 eserver {5 s. c: D7 o" l' \% {" p2 `
listen 80;; B: I' K4 a  b9 d
server_name 52os.net www.52os.net;- u" H6 C2 n; C, m; R: f5 p% R2 e
* x  a7 C* I; ^; Q0 W/ a
location / {' X8 W' I" n0 O
    ModSecurityEnabled on;  1 Q& D  C( U; ]8 ^
    ModSecurityConfig modsecurity.conf;  3 d" r4 O) C6 s! H% A1 j

, o9 o9 g0 r# h/ a        proxy_pass http://online;
% ^* V" j# b4 _# j4 e# N2 r% N3 b# B        proxy_redirect         off;
0 N$ p% m2 Z$ K) ?- Y) e        proxy_set_header Host $host;
2 X! m- {8 d( E& v* C# B        proxy_set_header X-Real-IP $remote_addr;
' @& h% J! H3 _3 @# n+ L        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
9 y7 ^+ f) t( P1 l7 R- g: _4 P3 V4 ?    }
! H1 r' l) v* c; X2 n/ m}
2 I, o) F7 V  i( ^- ?, M六.测试
; {0 w3 v0 G/ i& I; _+ W9 r3 y- R* E. I
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& {! e, t* s. U
- v0 f1 l' X+ I4 v4 e' k
<?php& H3 R- {8 X( u" h0 I7 E
    phpinfo();   
: B  N1 @! Y! i?>
4 G1 ^" n; [; `- r$ ~. ?在浏览器中访问:; k  U0 Y/ ^. b5 U$ k' a1 N

7 B. F3 B/ a6 Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" O' K; `7 `; L3 ?0 Z5 p; B! khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 }2 {+ E; [" N' _; b" o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ I% D$ o4 s9 D说明sql注入和xss已经被过滤了
5 \+ Z: V8 T2 m6 b+ V1 g. I7 M5 i2 Z5 `; `6 t/ @- A
七、安装过程中排错
3 e2 Y) |: _- W, N0 N. ^
' B3 I+ _  ^9 l. A) q7 ~2 ~! M: e1.缺少APXS会报错
* Z" x* C$ E. q! W8 {1 `* H; s1 v  K$ e
, C2 L+ a8 j# D' oconfigure: looking for Apache module support via DSO through APXS, _& j+ F5 t- O
configure: error: couldn't find APXS
$ c9 ]5 H) @3 M7 s, E; sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 X7 n2 [0 ~: j" W9 j% T/ P解决方法:/ p, @! B) m' k3 t$ x, ~

) D- y$ W* ?4 K$ `8 H7 {yum install httpd-devel: g, H& T& I$ b, o
2.没有pcre
# }6 E( ]# ?# R9 a) n: q0 w& K1 n5 ^4 A# l) i: [
configure: *** pcre library not found.
# j! ]4 L4 }/ p" Lconfigure: error: pcre library is required1 a: h8 y# J" H$ y0 u
解决方法:! l1 @4 o, M$ _

* w+ P* Q2 Q$ u8 Cyum install pcre pcre-devel
8 ~: F6 B* Y- r9 J3 n2 @# v" o3.没有libxml20 J+ Y2 a) [8 l% Y

9 a$ j  N- ~, \( Q; |) ~7 B5 X6 a0 u1 `/ M- o1 y
configure: *** xml library not found.) s6 }! f& D% D) q& d4 T: }4 X
configure: error: libxml2 is required* e9 F4 ?5 D4 F: }# b) j" z
解决方法:
4 \, V+ k4 {+ _8 K8 p" m* Q/ b, {5 L9 h: \3 `* d) k$ m5 J% f
yum install  libxml2 libxml2-devel
9 A+ C! K* j/ ^( Q4.执行 /opt/tengine/sbin/nginx -m 时有警告
, I$ m% T3 w5 H. o" ]. q$ j( I! g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 f2 \. L& m- E+ ?8 `) G
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) x8 R3 @! ~9 n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( n+ e& F9 n# A) q
; h' d/ R# ^8 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  D# l0 y! ~9 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
* Q# O+ g" u* K/ \) z. j2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 {" O; X, Z2 [9 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ @- A5 ^# k3 Q0 P# y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& U) R  W* a. b% s6 j  a6 [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- {! p  m& {4 l: v( c$ T. G解决方法,移除低版本的APR (1.3.9), `: m/ t. K& m8 v

* D, t2 M. b+ y7 f& N" I/ M( K- D: Gyum remove apr
( n$ x. M# e/ F/ u/ A5.Error.log中有: Audit log: Failed to lock global mutex
& }7 l3 ?2 y$ S* c, i% b! r, d+ n# o7 Q! n2 |- D* d
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 h6 }$ l7 n! m/ d( @global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 b* {' z, i& K5 e+ a% o
解决方法:
3 _% l+ y4 V, t0 i' Z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' x3 S2 l& N0 j4 z; p! R% r; ~4 A3 n+ I2 Q; }
SecAuditLogDirMode 0777
) }! D- z7 E) K+ p, d4 r  hSecAuditLogFileMode 0550
4 n9 r0 Q+ Z, y0 \SecAuditLogStorageDir /var/log/modsecurity4 _( n5 T; q8 W. J
SecAuditLogType Concurrent
% o4 Y$ F" V; _+ H" B9 U- w$ J6 {参考文章:
% a0 O) W$ N) G& x. yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. t8 A9 h) r" ~; L
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-9 02:15 , Processed in 0.070898 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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