找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12046|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" I3 a1 i2 i$ t0 \6 y  T3 P

# S$ L4 W* ^1 O7 s一.准备工作0 F& J8 H; j+ O% p! n

) P% P" i7 Y* x系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 Y3 `; x. d: {4 S! l2 _4 ^
" U$ L7 O1 p" O7 a5 M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& E2 ^  ^8 U) H, Q0 P" C/ o! I

+ g, C$ c1 t. q" E. Hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- ^7 s. N5 G. d) Y4 O3 {
, r  u8 _3 p) aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- _( n& v  b, K% G
2 O6 q. @) z2 H! D
依赖关系:
) F) a' U5 b$ W7 ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' z! W7 L. [6 y* R. j
+ w* f; a( a" X
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" ]8 P) W" n3 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr# {. n0 u4 [. k* x- I

0 ]) [- O4 c# X  cyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- G8 R9 A2 o. R0 x5 Y  f
二.启用standalone模块并编译
# d5 c# u4 `) `! k; v
6 a$ N& L: g* l7 A% |- y3 N下载modsecurity for nginx 解压,进入解压后目录执行:
# O7 y, h" W7 e* {/ Y* Z9 A1 ~) ^' p; ]
./autogen.sh1 _" k5 n8 b- S4 p7 i# g
./configure --enable-standalone-module --disable-mlogc
# E5 @) y) }4 ~2 ^- r- X. dmake
: u0 L; \: I) m/ J) P: M& f7 O& y三.nginx添加modsecurity模块3 S6 o+ S+ X$ V9 K( d5 J* l

- x7 q7 {* R2 I! j' T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- F' V  U- w% T  d2 J: ^  {* k( Y! @2 J' i0 P% }6 N2 _6 e7 P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine- g* j2 t4 y6 U0 J) W  G
make && make install
/ S# g0 T2 `$ b  R# ]0 m* L四.添加规则8 I. b, }& \( t

, `- v! g' n; U6 B; L2 t6 jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 @7 N# t; I7 U) j  ?/ |0 e1 z& a
- q5 y' g" h/ Q2 J. B7 h! y
1.下载OWASP规则:$ P+ A8 A. w! y$ t, e9 @
% E, K* B, M% B
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 \1 C+ f2 a+ C9 q. [! [+ L; M5 ]

7 }& H! @: R* G4 p, m8 qmv owasp-modsecurity-crs /opt/tengine/conf// ]% t- L: ^, _8 p( Y' u

% a# I+ {  z2 W) Ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- W/ ]/ ]/ j. e) ~2.启用OWASP规则:
9 L! k) K- U4 P; I/ M4 B% s# ~" b( ~0 P: W
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ ~8 y5 q8 N8 t! n. O, l' [

, V. [# T2 g. R7 O! Q6 R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 _' ]6 |) a3 K0 A0 @! p1 {; [2 D9 r2 u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; P2 c0 k+ J! ]8 P# u% `% E1 {
$ e1 W/ L; |% ?
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  T4 p1 z6 m0 h+ ~. B5 y7 ?, Q# RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 I5 E$ I# }9 m6 L" J5 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 U& x1 m/ R1 r: a9 i8 ~; _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; f, f/ K% R5 D: g$ Z# ]( r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 K$ x5 _- ^" a: _" w3 M8 k' \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 m6 V3 H) i: g! K# y; h8 F
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 z: q8 I& i# L& U$ P" L# ?+ R五.配置nginx
! o& B9 ?" B# j0 ^# E+ Z5 w. d7 _
$ k1 H) j3 P! s% `& A在需要启用modsecurity的主机的location下面加入下面两行即可:
; i6 \, a4 |3 c+ f. l. g) P
; W$ D* h- e0 @% z0 M( H+ w6 g; e) M/ xModSecurityEnabled on;  
. [( E% G. g' n* A  Q+ w2 v$ h9 uModSecurityConfig modsecurity.conf;7 H6 c# W; j; F( K5 A1 B
下面是两个示例配置,php虚拟主机:* K+ ~& d9 l( Z' Y- U
7 x" ?: V# V, c1 |  v9 A
server {
. q! k2 l4 }* e) m! i9 [' e      listen      80;4 L) q3 L: z8 b4 K( b
      server_name 52os.net www.52os.net;4 P/ @8 h6 t1 D; j0 b9 b9 w: v
     " y' E6 m2 f. D0 O/ }% q) n
      location ~ \.php$ {
" A' u' \  b/ B      ModSecurityEnabled on;  
7 x2 I0 X6 q6 w9 H; `      ModSecurityConfig modsecurity.conf;
) p/ _5 V4 R: W- I+ G6 q% T9 G1 E5 I  D
      root /web/wordpress;) `; _" i7 O+ W6 R
      index index.php index.html index.htm;# R1 W/ F' W" b$ P; q7 }
  
. T- r9 A' l! ?. K. G3 s" w" J      fastcgi_pass   127.0.0.1:9000;* ?6 _' d9 e" k( V2 f- z2 @
      fastcgi_index  index.php;( P2 [$ l7 z* K
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
; n  h$ n) a( t  D; V      include        fastcgi_params;4 H& {+ l0 _. U" R% H
      }$ r, T6 F" ~, F  P8 r
  }
# I0 \. [" o+ ^+ bupstream负载均衡:
% V' ]$ K4 ?1 f# F) J  g, ]$ f& ~
upstream 52os.net {/ j8 v$ H$ a/ z# k: F
    server 192.168.1.100:8080;
9 k7 f/ `; @. ?$ }  A2 c; N    server 192.168.1.101:8080 backup;
6 F5 j) D, L. h( b% g1 o}. R, G4 M9 c1 X) y, F7 z% @

# H% I/ P( U! k' bserver {$ |) B# L/ N0 V; ^: x
listen 80;: Q4 J& \& ~8 _/ u7 M; ?: V
server_name 52os.net www.52os.net;3 x1 z/ s7 f6 C  w" m
3 f3 F0 Z1 Y$ j- c6 p
location / {
% x6 Q/ e% Q2 Q4 y/ S    ModSecurityEnabled on;  * Q4 x. \  J4 m5 u
    ModSecurityConfig modsecurity.conf;  : c5 G  v* D/ f9 j' L
: ?/ b' j& t% `6 L" p- w
        proxy_pass http://online;& c" N! G/ u& i. S+ [. n
        proxy_redirect         off;
$ S" a5 N$ L: ~/ K( }        proxy_set_header Host $host;
. T3 t# \+ p' x5 _0 O* G8 R9 m        proxy_set_header X-Real-IP $remote_addr;- a; M# b! p+ |$ ~
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: Y- h+ [. Q$ @$ R8 a    }9 Q+ R1 m7 ^8 \) [
}+ |" i' d! i& i# s2 _
六.测试5 X8 C0 t7 E% B- p& a

) r  Y' b1 p( D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  O/ R: p- `: E* z# q, Q7 m) u
! \* W! c- W- A( ]) _
<?php8 K6 ~3 C& s8 h+ t0 T) U
    phpinfo();    5 h, |/ U5 k, ^" j
?>
  ?. A9 m9 B% q3 C5 X5 t0 A; k在浏览器中访问:, W; e' u. j2 `, A6 y9 _. p

/ A1 u% n5 s7 H0 @, mhttp://www.52os.net/phpinfo.php?id=1 正常显示。+ e  L1 |& ~* `0 o7 H5 U
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, o% G7 L" c3 k, y. Lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
9 d  o7 f3 Z5 R说明sql注入和xss已经被过滤了6 l& y; B. |# w
1 |. t- V2 X4 Z' Q( v
七、安装过程中排错1 k( L* f4 G9 \6 s1 |
7 ?5 D' {  i+ c; [% _9 a- m
1.缺少APXS会报错- X: D9 N1 Q8 G4 X1 y& W

( q( f4 B( `: n6 Jconfigure: looking for Apache module support via DSO through APXS
/ Y# d/ ~+ I* r0 l( _! ?3 n) f/ _" Iconfigure: error: couldn't find APXS
" n" E3 I4 d6 T/ i4 T" A9 napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  h8 C) c1 `2 M' z  B- [! n解决方法:
" p6 e4 o$ U. |
$ B$ W) U  n/ U( [" B4 l6 ?yum install httpd-devel0 R* u( U" O2 Q/ H$ P& S
2.没有pcre, X/ N5 u" o! W  `& q$ l; l, C5 P7 E

6 ]6 N3 F8 P, [8 Z3 s  X; fconfigure: *** pcre library not found.% o; Q5 K: ?1 p' Q! A* g9 Q2 H6 @
configure: error: pcre library is required5 g9 Z3 L! V& i; P% H5 B
解决方法:
, [6 D5 X9 v/ Q# s! |
3 x( b1 M9 X" B' s0 Gyum install pcre pcre-devel% _6 i! g; m  X- J4 ^* I8 F/ o
3.没有libxml25 [5 k3 k' ]+ b, _
3 N! u6 E: C# h8 p

, Q+ r, S& a; ]" h2 w, j: aconfigure: *** xml library not found.! Z6 d  b, ]1 [! b: A; O& Y% L
configure: error: libxml2 is required0 H; N) q7 `/ _
解决方法:
% }, V3 I4 e4 M1 ]$ F- |9 ?2 H& x6 [/ h2 r! R
yum install  libxml2 libxml2-devel
8 U! S4 F& T( a5 @4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 t3 J  f0 J! {$ h: s
; k: e2 k8 u0 P( _, T7 t7 E2 YTengine version: Tengine/2.1.0 (nginx/1.6.2)  @5 z! v/ p: J2 X  }9 f: a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ O* x( N+ K. A& A5 J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% y  W% n5 e( F, Y: ~- m* ]. v- \; _# I6 E3 ]6 A  N2 u- I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: u8 `# ?! Q: g- p. W$ Q! K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ i7 O' J2 X5 w  X8 t- m! O  H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* _) Z4 e# n. ]/ \) K- {4 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* v5 c4 ]6 A5 z/ i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 f2 u, e7 ^# }
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 D9 k; ?$ R9 x+ @! i  ^$ Y$ n解决方法,移除低版本的APR (1.3.9)
/ B& R# s# i. u* L. |& ~$ j" a) z
yum remove apr
4 t- k7 [: ]& e5.Error.log中有: Audit log: Failed to lock global mutex( ^0 x* q* X4 x7 |" R! N4 L7 Z: f
" @- Q- y+ _) N+ ~' `  K# n
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & B# n8 ?' M! M& H* u: A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; h6 V/ Q( Z& ?
解决方法:6 ]+ J5 c2 t8 T( E7 [9 w9 f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
: X' s; [: p7 R( d$ W. k5 ^, e, d% I2 _
SecAuditLogDirMode 07779 K. I8 [7 d7 q% A
SecAuditLogFileMode 0550. v  ]- n1 E& D9 x' J
SecAuditLogStorageDir /var/log/modsecurity
  [) d7 _4 n- E9 c. }" zSecAuditLogType Concurrent
4 ?" |9 E6 q* `5 X* ^参考文章:% f& n6 c5 H, r3 J, m. q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 l2 R: d8 g+ {http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-6 01:34 , Processed in 0.074998 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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