找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12312|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' M/ |  f5 u% Z; V! {$ C  }3 [8 V, @& f+ E( h; b
一.准备工作! A" v, ?' y5 Y3 V& E, e2 i3 W

1 T5 Y1 R( c  `; n系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- }( ]5 G/ }1 P) w4 W$ }2 }/ b+ {" a2 c6 h) O8 _
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  c- B2 Q* H- q, _% i  s& ?( U
" h) k# q8 t: z( Z9 H# U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; C$ l# _( }6 S7 |9 T+ {
+ S) @+ V/ X) q: b/ q' \9 @$ E
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% t8 m) B- D. G2 j
% E) V) N" e4 V. N/ w
依赖关系:" s$ b( f; t4 O/ F) j* t1 I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 x0 a- Q: j- {/ D1 i! D6 `! J0 E+ s2 V" d
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 W+ \: o2 g7 {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 i, ]% l% b% L! s2 T
5 X$ A) e$ ^/ e* I2 j+ E  C% [yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel% Z7 P+ ]5 P7 p3 i3 H. Z
二.启用standalone模块并编译5 w9 L4 l4 J. ~) }9 Z
: t: d* A$ V5 Y" W
下载modsecurity for nginx 解压,进入解压后目录执行:3 Y* ^  U. _: f8 c* X6 d

. x* `4 _7 y1 W0 z2 F./autogen.sh
; ?% H" q" B3 t! I) y; [./configure --enable-standalone-module --disable-mlogc) i. y7 M! g. x: l% R5 z2 J: w
make 1 ?. V; D  \+ ]  A! Z
三.nginx添加modsecurity模块1 v7 B& g7 S8 Q, C- z% u. i( c+ D
0 g' N. o- w1 x; _' y) [; o2 W8 @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. j' k3 w' @$ z% F6 @% c
- E2 L7 f5 j* k3 x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, Q" a4 l5 V% \+ T* D+ H0 k
make && make install
/ z( k8 N8 B& y四.添加规则
/ ~( c# n" v7 @; o: D9 f
& s; ]+ E" X$ l: M1 W- `8 tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# x" J: @/ @8 Z8 ~9 y0 H' ?7 E7 ~$ L

& O7 M8 s. A6 V1.下载OWASP规则:
! G7 i5 W+ i. }; I# B* A( x  y
: G9 V$ x/ ]! pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. [; l" d/ y8 N- i! Y  @7 ?, T! C# x( \+ ~7 S. H
mv owasp-modsecurity-crs /opt/tengine/conf/& P, K; |, X+ d1 Q: `
& g/ ^9 F) `( a' |# N2 ?& c; T
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: A5 ~7 j( g* o  H  A3 p0 w2.启用OWASP规则:
0 q3 R6 U  P9 @& F
6 V6 N7 J9 {$ ?, ~  j# W* ~复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 V: U4 x! j. L+ C
! A' K0 P: e$ K/ B
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ I+ h3 U9 H; |

  R3 {5 \: g* @! uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; G2 ]9 p7 n, P2 S# O

* c# H! @9 ^3 q6 a; vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ ?% U1 q! u  [) v( k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; q% |: K$ g$ SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ p/ m6 v& U( ?6 O" oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 \* R4 g: s- ]* q0 u6 T% mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  |: H0 X' o( |/ y, b% s3 hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ H/ ]9 G1 r4 i/ ~8 n9 T+ ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# V, g1 g. E  G6 t( _五.配置nginx5 _3 C2 m4 M5 Z& u2 c" ?$ C0 p

4 [$ G  t8 A4 J& h! d$ ]  `在需要启用modsecurity的主机的location下面加入下面两行即可:( \  i% K6 F9 T2 P$ y, x7 `" u

7 \0 H' P! I6 O" |6 Z9 R  PModSecurityEnabled on;  
8 q2 Z) X2 e! K- ]8 X! c: J, E$ xModSecurityConfig modsecurity.conf;
3 J( h; ~7 G' K7 F: A5 }7 @下面是两个示例配置,php虚拟主机:
, u" }* m  Q6 E; S% @  I" t) p* g8 `0 O
server {5 m# g( F/ Y  X! F4 k/ w0 d
      listen      80;
0 u4 V8 [2 t4 Z  ~0 p) A8 ^      server_name 52os.net www.52os.net;
+ p7 z/ @2 x& x, l. u3 J' d     ( _, p& P) j* H* A' [) ~( h
      location ~ \.php$ {* ]6 G7 k; \+ w( x7 R
      ModSecurityEnabled on;    f. M/ z# `- E  c+ c
      ModSecurityConfig modsecurity.conf;
/ Q# T6 w  Z8 ?  X" n: T5 e+ a5 S6 y, B3 t0 L6 i7 R
      root /web/wordpress;6 t6 R1 V9 W: v
      index index.php index.html index.htm;/ K$ D* S$ M  V% m4 `: c/ D1 w
  ; m  K3 `$ f5 g5 k% T. U; F
      fastcgi_pass   127.0.0.1:9000;
; |% v$ O: t2 S  f, `1 I      fastcgi_index  index.php;
6 m" Z- Y( ]$ [6 |      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 i  B9 H) K  A4 k' F# E      include        fastcgi_params;
/ q4 F& S- e) d7 B) o4 M& L3 h      }# }3 k! q9 Y; L
  }6 p& E- n1 K/ r; l! y5 e# }
upstream负载均衡:
/ K- Z; u- g. ~2 {* T5 b
0 p- j' W& h. p& f( Mupstream 52os.net {
' V: C0 b5 e5 C6 o( w9 E    server 192.168.1.100:8080;
7 a) L- M0 U7 D$ S& s    server 192.168.1.101:8080 backup;) o* p2 u3 m& @$ N
}
# U; E4 A9 n7 w" u4 x' |/ w9 h- f
server {( `9 N& Q$ [; p7 S0 Z& C
listen 80;
  B" n: |+ U) Q( m) _: K5 J1 w/ Q) V; dserver_name 52os.net www.52os.net;
% N+ d. Y, G/ c( z& O- D  n% f; O9 z' X$ A1 A
location / {% n' @9 i  Y# x6 J
    ModSecurityEnabled on;  8 E. B# ?8 u9 I6 ], s
    ModSecurityConfig modsecurity.conf;  1 w/ Z; W# f9 @
, Q5 d. s5 p' n" ?1 u
        proxy_pass http://online;* G* k% b3 u! {5 N* ?- r# z" a. J
        proxy_redirect         off;0 ~* C% u5 F- E; C: G7 p
        proxy_set_header Host $host;
7 ~+ V4 y! }  @) d        proxy_set_header X-Real-IP $remote_addr;* s' ]9 ]! |1 W2 V: q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  v0 `4 A$ D8 P$ [7 Q    }) z3 s4 a4 t" }1 k
}
% x  W) a! H- K- ~六.测试7 k& A7 h/ Q# L$ ^

6 N' d/ u7 r) a" O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" e! p2 _# |0 L+ t8 Z- i5 _

4 g* t5 v' g3 z+ Q6 Y$ @! l" J<?php$ k% ^+ s% y* i; m1 @7 Z8 a
    phpinfo();    ' j& N# A  ~5 D" P- K
?>
8 y3 K2 D2 J0 T3 D. {2 ~. n1 Y在浏览器中访问:, h7 r1 B1 p5 h: R' s( A" v
) \+ a8 e. H6 ?# d- ]9 Q
http://www.52os.net/phpinfo.php?id=1 正常显示。( @8 e3 N9 L) i5 u/ `
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。, W! T3 A6 x  @$ T2 c3 @6 X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。# G# w/ a' J/ a. U+ M& G
说明sql注入和xss已经被过滤了6 l: a2 d- R. b$ k

  j* W& [$ D5 O1 v+ ^) T七、安装过程中排错
! D1 Q- {8 E& J& ~9 i" a* F. M( r5 Y- m
1.缺少APXS会报错
$ j$ _$ |: E0 V* T4 q4 B" U
5 |$ Q! a% G3 T9 Cconfigure: looking for Apache module support via DSO through APXS
$ l- O, h1 L8 e% L2 v# uconfigure: error: couldn't find APXS' ?+ P1 N4 l! C- s% m
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! W) y$ ^; o: f; A, l解决方法:  ^+ O! V- I4 ^# l# }5 C5 u* m

8 _* A. h2 p4 S! _: ~yum install httpd-devel
! x. J0 x' @' \4 z8 h2 D2.没有pcre! z% S( N% G" `
0 F0 L8 b8 y3 [% g" d1 [' W
configure: *** pcre library not found.
8 ]) K$ D& o. b2 T' _* ?configure: error: pcre library is required
/ e4 l) c2 j: l8 r# U解决方法:& p4 g9 ^% h! K- ^4 n  A" \, i- \

3 q, a* m9 f/ Nyum install pcre pcre-devel- I) ]% n5 V3 x2 r5 x
3.没有libxml2, a* o) P2 C+ H0 t: z4 l& O

' H& U1 J: G" H' q2 Y
2 S+ a3 I% `, k: n+ ]configure: *** xml library not found.
3 R9 D  T8 x/ Aconfigure: error: libxml2 is required
0 f- n- z6 I4 h5 [  P$ d' s% {解决方法:
# y+ M( C2 C1 }  _. J- G( L
, ~: R0 t3 f: U" f+ qyum install  libxml2 libxml2-devel
. s4 Z9 G! c' {7 W( P4.执行 /opt/tengine/sbin/nginx -m 时有警告/ k: C4 h( [* e# `
6 q' h' `3 Y2 y5 ?  \
Tengine version: Tengine/2.1.0 (nginx/1.6.2)) z+ a+ C8 i6 y3 {. }
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# i  m/ d$ q6 K" \) N, H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! U. V. U, a' D( M& w. T6 m2 L4 ?8 c8 a4 n9 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& r4 ]- ]: m. c6 F7 ?6 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, J1 B% m: n& ?& Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 \- X6 {" U! j: N1 I+ A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" p+ |& X2 u5 i" y4 m" O' D9 f/ l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", l( W2 ?( @* i
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* F) R& M: c9 n. _解决方法,移除低版本的APR (1.3.9)6 c5 V; V7 I2 c0 @  K

# H; L# M7 Q: G$ M, Gyum remove apr
: b7 ~8 g: k) s$ G+ G& e8 W5.Error.log中有: Audit log: Failed to lock global mutex5 s( k6 S) @- g+ y8 m- v
; g& @1 L) }0 r3 i8 p2 m% m  U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( B  N9 O1 o* e$ b, s- Aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: ?9 k  m. r4 P
解决方法:
3 U1 f8 S- c' E# J4 ~5 i+ }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* j- k  x& @1 e7 B! o. n) k* P. O& u8 ]
SecAuditLogDirMode 0777
8 W1 ?7 b" W0 [, X6 |SecAuditLogFileMode 0550, s, O" j" O1 r7 w. p- h" `
SecAuditLogStorageDir /var/log/modsecurity! T# Z1 x$ R/ s1 f
SecAuditLogType Concurrent
2 Y$ _" [; I" X- _, z( Z2 I参考文章:
2 {; f+ b7 {4 o5 lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: p) P, J: p* ?! f+ T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-25 15:01 , Processed in 0.201024 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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