找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11802|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 i3 n. \( e4 @1 o2 p0 g3 L! R& G0 v. u1 n' X& s
一.准备工作
8 k( }: T' B! C7 ?5 p$ W0 ~
/ x5 ^' O- K5 `7 J( c; F8 m0 l系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" |* w% ~$ X" S4 V# Z+ R
1 y; X# V+ p' }& N
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 `) ~6 Z* H% a' x
) w6 ?4 V+ l) |# d
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( H! _7 e8 g' F7 @3 L5 d

% E+ D: n8 A$ d  ]! \+ ~  Z$ hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' T2 F, U. Y  J) H# M
4 h7 C" P; q+ ^
依赖关系:
4 b5 _% b  C/ V3 o1 vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' J. K( o/ x/ F7 u8 N& D

; V1 c$ J3 m) Jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' T) E, q/ V& Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr; g6 K& x! E* j

/ e; q: J5 }9 ?3 Lyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 b8 l( a" |1 s# U) |二.启用standalone模块并编译9 m& f; {& v2 {$ H/ {+ q  m5 j) ^

2 ?) g/ u4 V, B下载modsecurity for nginx 解压,进入解压后目录执行:8 v" P& N$ I+ s; h& Y8 a

7 v' w; I6 d4 I2 i& B, r4 r4 Y./autogen.sh5 B  F1 [7 `6 P8 `
./configure --enable-standalone-module --disable-mlogc' l. b  B; w  m* K8 a  a
make
/ j5 Y+ [, z) s0 ^9 n三.nginx添加modsecurity模块
! e+ d' e" \. {; M9 f
7 T9 ?4 g$ f6 v' ^2 u; @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 V/ m% o5 V0 [5 R, {: Z
$ c4 x: p3 r# t  _. l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! p& O) J( \. L  Y+ X% U7 i; F* S
make && make install
- M, S$ |$ H- V* x5 k8 L四.添加规则
; Q' ?2 d! N9 G7 {& |, v
- M; n; f' A! r# X5 c9 omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 i0 n/ s+ g4 h! w$ w
# \, J8 C! Y) Q; o( ?# r1.下载OWASP规则:
3 k' x+ q% c) ^* B9 N# P5 J  v4 J* B+ Q2 b2 D/ R6 W" N; {% {
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& E% d' X1 d1 T/ R! S( ?/ c9 Y  j, Y9 d9 F# B( ]/ I
mv owasp-modsecurity-crs /opt/tengine/conf/
3 R5 Z7 F! j& R' z1 @6 x7 J7 |/ E( f" k7 s* k3 V, l; ^
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* F& B( |6 P; o$ J
2.启用OWASP规则:- k0 @5 A5 F) L! p; [
8 u0 E+ u& j1 ^; C: V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; L: f0 h& ^0 p2 y$ m9 B' ]; _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( V# K1 t3 N7 t4 M
* R$ P- o% M' [( T' H% xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! a, s0 [2 k& _
6 L- b* `8 x! G, F7 L( ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( P. R/ N4 r+ K( ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ n5 \& Y  B8 D  G. [# {% E: jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 W* Q4 |* Y& u1 q. }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) x/ C  ]) p: B( z" ]( Z5 Z- t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  s# N8 d0 a% y( V! J! f; I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. ?2 F) j+ M7 t4 nInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ L4 }! E2 e" ]" W  n3 K$ K+ r五.配置nginx6 b' {; y* p& s
) u9 J9 i: K/ {+ O4 v4 a' m3 T
在需要启用modsecurity的主机的location下面加入下面两行即可:
# B6 R5 y& f3 ?% |: E1 w! C5 h# k! K/ J  t/ Y) s
ModSecurityEnabled on;  9 X& H& {/ u, C7 z
ModSecurityConfig modsecurity.conf;
# K+ w5 l. e9 l9 a6 _* ?下面是两个示例配置,php虚拟主机:$ q7 Y; I0 {; y5 ~* A8 K

6 @, z1 e# T- s. ]# hserver {  O# D# M* n, r, s/ Q
      listen      80;
7 F) C' |" o- |9 [& S      server_name 52os.net www.52os.net;' ~5 M2 E2 Z1 I$ g
     # V& f! l! Y" Z
      location ~ \.php$ {6 n2 w4 U: P) C3 N3 z  C/ ^7 P
      ModSecurityEnabled on;  
! P% u& e; ^7 \6 C      ModSecurityConfig modsecurity.conf;, r& r1 W6 s3 s, E# l, I! n

% ~: \7 M- s- H5 W      root /web/wordpress;" F+ Y2 [4 r6 w" X+ ]
      index index.php index.html index.htm;% I# ^! Y# R6 g: {) L
  3 j. ]. T* E2 K+ Q  U
      fastcgi_pass   127.0.0.1:9000;
. H& T4 B) I! Z" Z  R; q! ?* E      fastcgi_index  index.php;, W- l4 x5 T" M2 F) P& q. K! Q& N
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' g) |( C! P0 K: {
      include        fastcgi_params;! d  X+ x! d5 v7 W( _
      }
/ z* q; n# P% @, T  }8 r% h0 v  K; M
upstream负载均衡:& R/ p+ r$ R; V2 |( b# b- d/ B+ z

/ P$ e2 U4 ]. {& l7 e' Vupstream 52os.net {
4 ^& f0 R4 {/ e1 C    server 192.168.1.100:8080;3 o# ^! o  y( X
    server 192.168.1.101:8080 backup;
# b5 d# {: E! i2 y. u# d% z}
: g; \% g$ [& s8 a# D) U9 V+ Q$ q
server {
8 f. u. c  O2 k! B. \7 Y) klisten 80;
8 g; H# y/ c( D( aserver_name 52os.net www.52os.net;
, z7 I1 n& ^! U: @, V8 L
" h5 @# |4 l3 ilocation / {
: M; y7 b7 ]2 X, B( {+ t    ModSecurityEnabled on;  9 r0 `1 g, r/ ]9 D( H! Q/ K' b
    ModSecurityConfig modsecurity.conf;  5 u6 R4 U8 u+ d5 G

7 K8 v0 T9 l* s- m        proxy_pass http://online;
& C. Z' V0 ^/ m3 s( t9 N        proxy_redirect         off;( X% Z' P' j$ I0 _3 p2 T
        proxy_set_header Host $host;8 t3 u+ q/ Z- b
        proxy_set_header X-Real-IP $remote_addr;
" N* [/ T5 |6 c5 E/ U. Q& @        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;. h4 c$ K6 {# r2 a0 a. D7 V, M% d0 Y
    }& [" G) K6 t. X6 L0 g2 ?% t
}
) L/ `+ j4 F, e0 Q六.测试
4 ~+ ?2 ~5 O- [# |3 y  P! V" y" T9 D6 F' e# o, D- |6 G9 l
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ p( P+ u8 r0 X( I, Z0 @: w' U$ f4 \5 w" @- p" |
<?php/ B1 L% A: F. J8 I1 m% m5 I; J
    phpinfo();    # \4 y7 E% n; \. P+ b: J
?>4 a$ u6 O: _  z. T. W/ S4 @
在浏览器中访问:7 @  m; }) G( x9 Y
+ L/ d1 i% C# \
http://www.52os.net/phpinfo.php?id=1 正常显示。/ j" [8 n, |$ R" v2 l5 A
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 U$ \2 z0 ?8 u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。) s" K# h6 o" t2 G  g
说明sql注入和xss已经被过滤了
2 k: ?3 `# X, w  o7 V# Q
3 D. |  k5 |4 s+ g, r  K七、安装过程中排错
! Q9 J6 K/ X& X
( ^; A" r% ^6 G5 x1.缺少APXS会报错
6 F& `' D- r- c' M" x: B
9 J1 s: _# G+ g8 Wconfigure: looking for Apache module support via DSO through APXS
& P4 A& p9 F& a  ~& B; Jconfigure: error: couldn't find APXS+ K4 N0 h2 H4 N) x+ u1 K' }
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 R: X$ H/ v! O4 U解决方法:
4 K. |) u5 C' {6 O) V: e, ?' V" r" o- i0 P4 m
yum install httpd-devel
1 V7 k" M% u" k7 R4 V/ N2.没有pcre$ x5 [, n- I6 q  v* h! L
2 C. R1 H* _0 _, y8 e. d" D
configure: *** pcre library not found.
9 e3 R1 ], n# W" {6 {9 `3 r1 Xconfigure: error: pcre library is required
) \  _# S( G) [: r  i3 @. Z解决方法:1 n' ~" Y$ |0 J0 c2 U) d) n6 k
7 d# F6 k& V9 q. X: z
yum install pcre pcre-devel" K1 g% {' N' y) o
3.没有libxml2
" F; G4 z! Q, K' x4 ]+ S1 E  N( ]+ ?, W) C; g; v2 ?
$ D' l4 f- {1 X6 H- a+ C# c
configure: *** xml library not found.
" w$ D- X! x. W# k+ J% @configure: error: libxml2 is required
* _) ~; }  L2 p: n, z5 E解决方法:
4 S( q1 {+ `  h5 r" o# i; D" y
yum install  libxml2 libxml2-devel
- t: i/ t: R! h- G# r  d! X. ]4.执行 /opt/tengine/sbin/nginx -m 时有警告
, p; \3 G9 O$ X" S, M0 \. P1 H7 p9 ]# ]& T  K  k
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ b) s! b+ f4 B1 _7 g. o3 b' C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- ]4 Q, J, L' J  A' I0 X# w. j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 P6 ]( _: l: V! l' [
6 B1 Q2 u4 r% s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ a+ e/ n* B3 r+ q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"$ W) y) Y% \( M+ \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* G8 S; ^! [3 W8 R+ s+ M, {% D5 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 K; s$ |# U/ W2 {# ~% c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# M/ V3 l) N8 C; ]7 I7 R4 s0 _, o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* h) Q0 G! \9 T2 ~' P* v9 A# h) E解决方法,移除低版本的APR (1.3.9)
  D6 R/ N  {7 P$ F) A6 C3 a9 ~
: i6 W5 C, k/ Y% Z9 \8 G% Yyum remove apr, A5 I/ y/ ]7 j' h
5.Error.log中有: Audit log: Failed to lock global mutex
: K5 y  |; W1 ?( K3 n* a, ~- l
* H0 N. z- Y9 y, D% P2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ |! i, q; N2 {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 B* ~( _# s5 U  ?9 z解决方法:
  Q# i0 x/ ]& ^  u4 G* F. W/ J0 Q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 H( h! m, A% q% K# x3 F$ M5 f
6 }2 e: B! f- c0 M! g- \5 m+ YSecAuditLogDirMode 07771 O. ]6 h; d; }  e- F9 W
SecAuditLogFileMode 0550
4 Y( l  o- e/ p* S0 {9 d. p$ hSecAuditLogStorageDir /var/log/modsecurity
! R  P9 N' u" H6 VSecAuditLogType Concurrent
. N: [% Q2 h' U参考文章:6 g! B! s0 Q% T$ M3 x7 M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  m" J+ d$ h9 U, e) Z& Thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-14 21:38 , Processed in 0.240865 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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