找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9227|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, @8 V. w/ F$ l: B( s, q1 G. V
) _9 q( a0 h9 C% X" }' g
一.准备工作
- F6 `8 C+ [# J* p. w, x5 Y! _5 X0 c) h2 v" [. J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; N+ ~* M* @  b# `
+ M, ], Y  T5 y" g8 }9 f+ ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! @9 R0 b8 F  N
7 A: u/ @, W0 a6 Q3 r
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" b, ^! J5 q* U
! [) V: D5 ^, \4 y  m0 JOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" j: D4 K4 ~- I" V/ U  r5 O

% G( X( M! w3 w* K依赖关系:2 y" @5 S: T# V) A1 R" K6 u
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ U' t, m( ^: q- ]4 l/ z) U
+ ^( W+ D8 c" \6 j7 hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; Q5 c* f$ k, A1 o1 }# Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr3 s+ j0 s% Z9 y
3 e! t$ F% {' g1 K# |4 b! I
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
+ g3 Q; L+ c3 [- P6 t/ e8 S二.启用standalone模块并编译
! o+ F4 y0 s7 |8 R
5 T& U! ]! ^; S, \2 h下载modsecurity for nginx 解压,进入解压后目录执行:5 m. e3 `0 c% G: q# s) X' s! R1 D
' L) K' f2 X" ~1 b8 o9 W' N/ J
./autogen.sh
1 N0 s, s8 J5 s3 W./configure --enable-standalone-module --disable-mlogc' t: c# S+ w4 h$ S; @8 \
make 5 v, n. ^% `1 K) Z* H; r( \
三.nginx添加modsecurity模块- W" p# M% l: ^
" b5 M7 C0 w. W" ^. t
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 _) K% c0 u" C( n' {; @1 t6 Z- B; S$ R$ `$ F6 g* k0 K
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ \- E1 A# R0 L( Y3 {- }% l# E2 xmake && make install
- L) {9 ?4 h3 d$ {, n: Q四.添加规则
3 p1 ^) y  s" p6 F6 M% V# ~% @
7 f4 U# {/ P" [% amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ b8 ~$ M3 d9 p& o$ Z" E$ U% i- [; N, l2 B2 A" B
1.下载OWASP规则:
; }$ E! j$ \$ t( u$ k+ {+ u
6 J5 c' p# M; Q6 Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# q4 Y* }2 a% m* ?8 b. d7 Z- ]* S  t: Q7 ~7 d
mv owasp-modsecurity-crs /opt/tengine/conf/1 a! u3 b8 `3 @" }% T) O

$ Q0 s! n/ J1 [) s- Y) Z2 q/ ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 J; I( i- N$ b9 b2 k2.启用OWASP规则:, g* [, `9 x! m2 v. K5 I* t/ L' Z
% Y5 s0 q, Q: z5 [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ `; L' V& F# t, q% J9 n, _

4 }; {4 H4 k1 b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on  h# F2 d/ v. ^3 _/ N/ H2 {
$ Y7 r/ T0 ?1 Q0 A$ Q1 n
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 ?' }& X7 b' s. X: ]# ?) o/ S8 m7 D! q, t
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 f4 U% `4 V& `8 P3 T  W  bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% i0 Q) `2 X5 W8 B, @9 M# V6 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% f% J! t, \% B% q/ R4 M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& J5 D% q: f  D+ }' S7 UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 W; Z$ T) \: O: {& s! T4 \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- g3 w& q, A9 s( j' ~
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 p! w! ]5 X- [, H" @五.配置nginx  |7 u: m/ @4 `  b6 ~* |
$ p) V  v: |! `) z
在需要启用modsecurity的主机的location下面加入下面两行即可:4 V1 l/ E4 J3 _0 j/ z9 Q
9 Q) p! C9 |% ^  q9 ^
ModSecurityEnabled on;  1 u, ^/ u1 w" D, _* _$ T* l7 C/ V
ModSecurityConfig modsecurity.conf;
3 r: |# h4 U0 D1 s9 r' a1 H8 ^下面是两个示例配置,php虚拟主机:
) Q& U7 r3 V' N6 h# ]. ]9 f
/ A& o/ O0 N$ I7 [: K; n) Rserver {5 E& \0 `" y+ y2 g! K8 D3 L
      listen      80;7 k0 {8 z, q. K% W3 r
      server_name 52os.net www.52os.net;
: [$ v& S" F, }5 j0 M3 v     & @: H- `  @' g. j' j$ q7 ?
      location ~ \.php$ {
* n9 Z# X; p$ l* P/ g3 k" L      ModSecurityEnabled on;  
0 ?- m5 X5 |1 f) L0 Y2 a2 {      ModSecurityConfig modsecurity.conf;6 O  Q2 \4 v, _( o9 Q. x* N5 @2 ?
+ H% }. V0 {9 S; x$ C# T/ |9 c
      root /web/wordpress;
) Q/ i- x2 j) E      index index.php index.html index.htm;
$ a* [9 F3 r' g- X  0 [4 w3 V% T% k+ j  V
      fastcgi_pass   127.0.0.1:9000;
+ |+ i& {: I* f8 G* Z. O      fastcgi_index  index.php;
1 R$ u/ T- q" U5 S; E1 [      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ f4 _8 ]6 S8 {+ C# b$ c' d      include        fastcgi_params;
. j4 b& b, N% O9 E      }
( d$ W5 M! d# t( b: G  }9 b+ D: l+ d. t& [$ \
upstream负载均衡:& z5 [8 |6 h9 d0 L
$ ^" u6 |) U" e& e$ t5 w9 F& f% I/ v
upstream 52os.net {
# \' ]. A6 s: F; r% b. H    server 192.168.1.100:8080;
; H- \) ~& J3 m/ o" w    server 192.168.1.101:8080 backup;
- D$ z1 @: n- G. I& {4 u) L}8 a2 V6 p, Y5 f. J7 K6 u1 F

- Z' p* x2 c( N& z, F& t' U7 p7 pserver {
' E& l1 y9 S2 k. N' s3 o9 Zlisten 80;
4 E# f# o, O) ?server_name 52os.net www.52os.net;
* K% i9 m& j: V4 Z% q
$ G$ X. O# U9 j9 \5 Ilocation / {9 k* d% y2 ]0 X+ r8 |- Z
    ModSecurityEnabled on;  : Q1 m0 j1 Z: k( `* a5 T0 K* M+ s
    ModSecurityConfig modsecurity.conf;  
( [, n+ S$ }  l! ?: O+ h" F! Z1 O. B1 l5 @
        proxy_pass http://online;6 P- N) C$ W$ f% v9 U
        proxy_redirect         off;
5 x# ?$ s& J  Y6 ?/ I1 @        proxy_set_header Host $host;5 ^9 {' m1 d/ l7 S6 e
        proxy_set_header X-Real-IP $remote_addr;
# l2 R8 j' X2 G* k3 a        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
! k) V. I0 v" b$ y8 ?/ D( `    }
5 r/ W* b% `/ L! Q( @+ T4 g}
$ Z  S7 [6 G9 H8 }; S' M" \4 X. A六.测试5 @: A% G  m5 ?! N

% J& ~# Y" Y2 S9 Y9 f我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: _6 ^7 @5 ~$ [7 ^

, b8 e" d% R" c- J9 Q( T<?php4 R; B% F5 I' Z7 k# T/ C+ y
    phpinfo();    & S  o  l, B' C2 K: M2 ^1 F3 f  V
?>+ }, P  b) b( \2 W0 @/ I
在浏览器中访问:
! S" L0 _* ^- D7 o' W4 f& |3 U* M) H: J  z6 [
http://www.52os.net/phpinfo.php?id=1 正常显示。
" j/ v" Z% {& r/ D; dhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* a' ?1 Y+ t9 A3 T8 J2 s3 N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- J4 J: L1 A9 h+ T说明sql注入和xss已经被过滤了
& k6 i% m9 E2 @
" @9 A( }/ T7 r七、安装过程中排错: O, m0 n! Q2 M6 Z5 E

: q/ e2 ?1 F" N/ R, W$ U! o2 S# |* p1.缺少APXS会报错6 R2 N( \+ D  P9 V
! Y0 B6 {* r7 ?6 q  `
configure: looking for Apache module support via DSO through APXS2 {, `6 w& v' e  C% a
configure: error: couldn't find APXS) ]4 U4 A2 |* f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 x. ?# e/ f! L+ k: g$ N解决方法:% C9 {, X; Y" u8 x7 \  t3 D. N

3 x  V$ F+ l; y% P3 f% Q+ Iyum install httpd-devel! Z% b( B" t1 I7 V" Q' R
2.没有pcre
+ ~+ i8 L+ E% r; g2 }
4 q  z) _: V/ ?2 }) l8 S, wconfigure: *** pcre library not found.
& e6 K8 A" d- a# Bconfigure: error: pcre library is required1 R- V/ g8 [" m: E
解决方法:
$ @) V+ F& x6 f, J( t8 ?& F
7 V9 |4 s; B7 }3 _yum install pcre pcre-devel
% H, k( p. g2 F0 j. {- z/ T4 E3.没有libxml2
3 Y( d! ~' J: J% }( ^- Z
0 l) O) V2 G; n
1 v9 g. q2 G  `# y* w, X$ e/ Zconfigure: *** xml library not found.
' G1 G4 k0 b7 ^" _  r8 f# _configure: error: libxml2 is required
' |. m& \7 R+ W: p  o解决方法:5 r" [# A+ d. Y% R* I# j* h$ G

& ]0 l* n! c9 b. j1 T8 o9 N# qyum install  libxml2 libxml2-devel  |: _9 H( p0 k1 E1 j( q- r
4.执行 /opt/tengine/sbin/nginx -m 时有警告# o& F5 R& I; k7 A( k- {0 M: K
+ f) M2 u3 Q! W) P
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% m: I" ^/ T  Q1 Q/ n8 H: anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' O/ N# `0 r8 S+ @7 `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) ^2 U( s$ E0 g$ L
* b, x/ g: h1 p# H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! B1 [' O" ]- G. j- i  I3 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"8 \; S% j" s6 R0 q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ R) P, p, q: n" f" [" k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) A8 z" V: s3 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ q' U+ U0 q) a" z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: u$ `% ^, c9 \  J& \* X
解决方法,移除低版本的APR (1.3.9)
, ^, \- J* y0 Q0 e$ ~
6 M/ j* ~: u' q+ kyum remove apr+ e" T, w, _" Y3 f( y3 P$ N
5.Error.log中有: Audit log: Failed to lock global mutex
; u3 q/ A, d' {9 \1 }* B6 d1 r; Z5 o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ G# N3 J8 R% z' m* l. Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 _9 v/ h' |) T# O3 Y解决方法:
# `" Y, ]; I, u2 {* @编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 E' A+ V: u# z' A% j0 C! t8 v9 k- ~, b) t5 V& [  W  `) v. @
SecAuditLogDirMode 0777% F8 t) E0 R) x2 q$ P  e
SecAuditLogFileMode 0550" d9 t' X, W* j
SecAuditLogStorageDir /var/log/modsecurity
" O7 W/ @; A+ ~# }SecAuditLogType Concurrent
+ i, G. e; f2 B4 i. C0 `参考文章:
$ |  `2 o3 @1 E* Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' I0 o$ f0 \9 G/ ^3 k# f3 Q1 k
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-28 18:24 , Processed in 0.067269 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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