找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12897|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# n! S" ^$ D8 G* r- {
+ ]! j+ b- z% E) v
一.准备工作
4 f( l; Q% X+ s+ _
# B5 l. ]. j9 X8 L) s/ d" E8 Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 X; v! f+ V* y) ~( L& C' |

! c/ [# H( A& |+ A% qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" t% T! q" b1 r* }" D

5 u( Y3 e$ i" \1 V: I' jmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 [' C5 s* M+ P6 P0 c2 P' _" j- f# {  g& v  v+ D7 t' q: x% ~7 u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; H$ l. V' s  q& P, [7 c0 w7 R  R$ b; W  b& |' S) I+ p) J5 Q. u
依赖关系:4 i* ~8 V1 q  E2 a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* o* Z! ^. e* q8 p6 p: ^3 a7 \
, s3 ~  N( m8 L2 l# Lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 l0 x6 _1 ~) S5 Y; x+ s. C& @* xmodsecurty依赖的包:pcre httpd-devel libxml2 apr6 j9 N9 U4 H! Y# Z, Q) }( I$ |

' L* v% J$ r* pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 V' x7 R6 g7 a# v8 b9 c) K二.启用standalone模块并编译9 }, c+ N& l; G& d6 g" A  N; _$ n
8 y" c4 h0 H  K9 a. }" @6 x
下载modsecurity for nginx 解压,进入解压后目录执行:0 F1 N1 t$ \1 ?3 m

4 o  E) I1 w  x3 w7 r./autogen.sh' d- m; y/ }7 C) I0 Q8 u1 [2 q
./configure --enable-standalone-module --disable-mlogc
7 b! C: o- E% T& B( |1 imake " h$ t3 i* n+ i+ M* g
三.nginx添加modsecurity模块8 W3 h; Z0 u- c, r. i8 I" Q

5 L4 L% y0 N# Y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; ~5 A) m3 O4 W% |& q; G0 ?
2 A# n- E& w. B$ T6 t./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
- {" v( L: k. U: A4 V* W! O2 Wmake && make install% v* C% y2 x8 M4 E3 }; @
四.添加规则
: W$ T  R7 ]5 a% i
1 u3 R. O/ `2 ~, omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ l6 k5 s- r; T7 X2 c/ v
/ k/ K/ y9 E& u$ m. J9 W; h: w
1.下载OWASP规则:+ C1 s3 ^2 h7 Z! `7 [; g# W
$ y: t' N; \3 M1 b. g2 M3 Z" G
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' L# l  P1 w7 ]# A" P2 g0 e+ a  O

4 ]; h, g. J* a+ N+ I! Omv owasp-modsecurity-crs /opt/tengine/conf/
0 ]/ ]6 _6 m7 u% L9 }( B% B! {% u8 O( |
! c; N) d0 m3 h( Y2 @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ N# ]) [3 Q6 e, M3 Q
2.启用OWASP规则:
+ C2 P6 H0 a0 X( R, S: k, y. x% `$ _2 U+ a2 O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) {, F0 L" e9 T3 q9 Y# i2 O! {6 \  |' i: Z6 N2 J: o
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ @, \" T! u5 p" e8 n7 Z
# g: ^% r' ]! P. i. D1 b
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* K% ~; e: D6 r, {/ r

- u: H5 |/ j/ c$ ?7 ]* u5 J* WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 b: T3 f  f- V9 {% G7 xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- [/ f3 f+ [- \, J9 A. D0 d/ r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) D9 K3 r7 k3 p1 D1 eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& A( z( h( `/ {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! B+ I7 v2 [( W$ g) |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# i4 T% E! H' f8 f* c! ^' o
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- R3 E& T, g% h% S
五.配置nginx* L/ {/ c4 \( ?$ d
- K# ~/ T- P) G& A5 D
在需要启用modsecurity的主机的location下面加入下面两行即可:
& l& M0 v. Y5 O7 j1 j$ T. b' O( X' o6 C7 J& K0 p' G# J6 _/ o9 @' t4 V) E
ModSecurityEnabled on;  0 ^) P# V7 X' ^# A/ R( M
ModSecurityConfig modsecurity.conf;) C7 f4 t8 d8 W) G1 H# \
下面是两个示例配置,php虚拟主机:7 e* _& z$ P3 t7 x

( ]% D  v9 l* P, T, l' yserver {3 T0 Y4 ?( M' f0 A4 s, s- p4 W
      listen      80;
) d! x6 t9 B2 O7 q9 d" P8 U1 A      server_name 52os.net www.52os.net;
& C% b( f. O1 J; ^9 v     
8 |' r8 `# W. p8 j      location ~ \.php$ {
* N6 P/ P/ ^3 \4 h2 ~6 D      ModSecurityEnabled on;  ' |, ]% T# d- Z0 q
      ModSecurityConfig modsecurity.conf;9 p8 s8 h5 R( r! a: s* S. X3 l

- }9 x/ O( @. e1 M# O2 p: H8 `7 `      root /web/wordpress;1 Q+ B. g6 p, C% M1 F$ q- `
      index index.php index.html index.htm;
7 X9 ^  [  S, a- x/ x- |' y* A4 z  ( j% L1 a$ ^$ \0 A0 J+ `
      fastcgi_pass   127.0.0.1:9000;
1 n3 r& t$ A. z' R% y- ?; F; F      fastcgi_index  index.php;8 p( O9 ^, f: }6 H; u6 y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" H' u' D0 C6 Z  P0 |1 h      include        fastcgi_params;7 Y# T* _. Y. B. K, y
      }4 q3 O0 i1 ^# d% P7 \. L0 ]. s
  }5 k3 ?+ V  v) S) r- |8 t
upstream负载均衡:
* ?0 n3 R0 t% y  T0 m
& g+ u- X: v, Z+ d& Q/ kupstream 52os.net {
1 Y; w2 w9 O) T2 [, Z    server 192.168.1.100:8080;
( M! n) N; K6 u: h    server 192.168.1.101:8080 backup;
! z9 K1 f6 t% B. w( c}, ?' ^; w4 C! n2 i( o- W/ j
9 r0 w$ U9 U5 j; X) q9 U
server {( G/ Q; {' c: C7 S
listen 80;& Q! f: u3 K2 q4 k) K
server_name 52os.net www.52os.net;
& @; [' i# l; ^& Y( A7 W2 R% m3 ]$ G5 e
location / {
) v. D7 I1 N" E5 F3 v    ModSecurityEnabled on;  
' E! |) ]7 s6 @! D2 K  m2 J    ModSecurityConfig modsecurity.conf;  $ t" q( I! r; Z/ H8 d
6 I# X  e7 O; V# e0 E0 @
        proxy_pass http://online;, n$ Q# q6 X1 ?7 c, z4 \: u
        proxy_redirect         off;
% i  `! N% B: B. p; f  V; o        proxy_set_header Host $host;+ h8 P1 K9 a# }; R6 V$ X. \. ]
        proxy_set_header X-Real-IP $remote_addr;. E7 t; P6 p! f  f" A8 l
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 E, \, Z+ d: e# D" H
    }7 Y& `! @3 ^7 ?& E
}2 F4 f+ Z# d3 C
六.测试
' P3 f' y* _0 W1 f) Q  J. v
8 O& G3 r  o' K* M8 |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 o; q. g5 T) w( @- ~* f
, P# P  x4 ^; r( s4 p7 C6 S8 `5 E0 b<?php/ T4 ]8 \: L( D1 R; g& d, Y
    phpinfo();    " o1 n6 W$ q; c4 S( n
?>
6 P+ N. n9 ?" t* ~; Q在浏览器中访问:# M9 r( z0 G% q& ?4 i
8 X& G2 d, J- e, z& \' {* a1 i
http://www.52os.net/phpinfo.php?id=1 正常显示。: S4 e6 X* H3 \" E. m
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 y* q* }4 M5 |$ p9 N: o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。: F- _' e9 N' a7 e0 @1 q5 V
说明sql注入和xss已经被过滤了1 u% A8 w# Y6 u+ C
: R" m1 D& p1 k8 u4 d
七、安装过程中排错% i7 `9 [& l; J5 f# U
7 x! B4 q4 v% P8 n; a
1.缺少APXS会报错
6 ]5 n  m% g# G6 j
* q4 n" @& z% Z* R$ y& L3 sconfigure: looking for Apache module support via DSO through APXS$ z: e/ [2 _2 B: ]% r+ i* C. n
configure: error: couldn't find APXS
7 }( }* {: T1 ~( B; w# ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 r8 _0 d2 m. v; g( [; K- C; i解决方法:4 }. `) T9 X" r
2 L+ \9 \, L- z6 X2 x) x4 e
yum install httpd-devel
0 v/ n# Z2 i- W6 p+ e2.没有pcre
: g' f- K4 i. c
- [1 ^8 o( J& ]5 ]' ]( o, dconfigure: *** pcre library not found.
0 B$ S" p- }9 v0 K* ^configure: error: pcre library is required
3 o5 y" @, ?5 m" M6 ?' n! d/ W解决方法:- c1 b$ e# n. A  ~$ [3 I! z8 d

* H" B8 V2 G8 a" I' I/ Dyum install pcre pcre-devel
$ h' I) Q; w3 b; r/ E3.没有libxml2  L4 l* G' I8 G' c/ V  j
2 D# z# Z: I% I4 g
& h7 C* v. T4 W. S. W
configure: *** xml library not found.
1 X0 k5 {' K, E  X  \$ i+ Q( U( lconfigure: error: libxml2 is required
. W% u0 C) y/ M4 \解决方法:
( z0 u6 F1 Z0 h9 O# d9 D. n# y+ h1 f  R
yum install  libxml2 libxml2-devel
+ Y- T5 S- }1 c/ h3 K0 [, A4.执行 /opt/tengine/sbin/nginx -m 时有警告
; E& D3 n" `/ Q2 Z
$ A  @6 M6 d" `' S+ _Tengine version: Tengine/2.1.0 (nginx/1.6.2)# [" [9 R4 H6 J6 j; l/ ~: M/ W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 U1 m( c0 Y6 R+ ?# U4 F原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 x2 n( z  @; Y3 l* S; m0 X

; K/ G: t6 A% T3 I) v- t8 s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' d& t: |9 l' [7 E0 Y0 m9 E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, E% o, p$ Y, x* ?& @5 P2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 L% O3 e/ d& j- `5 ~, @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ w" i$ R* X8 R; ?$ g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# S1 e- A+ X3 F0 Q6 U4 z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 ?. K- I& T. _5 S! }解决方法,移除低版本的APR (1.3.9)! p$ `9 N# k& Q% L/ H7 ~

$ t4 A- K. F7 u) q  byum remove apr
( h. C' b' F: G& p5.Error.log中有: Audit log: Failed to lock global mutex' F; S" c$ P/ J0 Q( ^3 F1 J

* W; F5 {! E) x1 n& R2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 B# W; M/ k0 ^' F& w
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( ~1 b; o: E% N& z1 ?) [解决方法:& q1 Q7 Q0 G9 |+ E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- K0 e) h/ q( V# l. k  Y8 }# e
7 J; h3 B1 L: Z: J! ]  R- A! w: JSecAuditLogDirMode 0777' t8 C5 k0 V9 B7 O6 G4 I8 X( n' ~# U0 j
SecAuditLogFileMode 0550  ]9 T  U( S6 M: w
SecAuditLogStorageDir /var/log/modsecurity
" n2 z1 L4 W1 jSecAuditLogType Concurrent  K& R% O  d& J, s' u& r" J5 Y. S+ W
参考文章:$ H$ s$ H) \: }0 R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, A  `: [' b- [$ U, k& p8 i8 Ohttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-24 03:48 , Processed in 0.078331 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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