找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9309|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 L2 K- g  u, _& z' y$ A
+ E" s3 N! ?- ?! b' H一.准备工作
: P& a3 m' H( I: o
* ?2 z) J1 b5 B* R5 t3 S! ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: g- E# J( l- F$ F

/ T& J$ Z! k8 P. j% `6 x7 K  U0 Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  c" I" C, e) n# q

; y1 \7 ]9 l% c, s6 ]modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; J; Y# L8 ^: t9 @% }! R
% h5 l# d9 K, S$ j7 P- u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 i- V) O5 j% ]8 C- f- C

+ Y/ K; n! X1 g' C! H4 J* w依赖关系:
9 ?6 D, K% O4 f; v) c$ I: ]  d7 N6 ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) M0 r* W6 S6 ^6 M
9 \9 a# ?0 Z+ g. H" ^3 C- Iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 T2 l/ u2 ~" j3 q6 Smodsecurty依赖的包:pcre httpd-devel libxml2 apr
. j: Z. E4 r* w1 Q( t) S3 y9 ]+ L  o7 r( O& L. y/ u
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 [' ^' @2 C; [! M" z  h, @二.启用standalone模块并编译
% }3 T2 V# c% u( W( Q1 S+ N+ j
) `1 U- k2 o$ c' t, o2 Q' @下载modsecurity for nginx 解压,进入解压后目录执行:7 T( ]5 z4 D1 n3 E3 _. i$ u1 d
8 }- J2 o( O/ z: Z1 N+ ?
./autogen.sh2 Z7 T7 L1 b. ^0 U# L$ D3 h3 w
./configure --enable-standalone-module --disable-mlogc3 L9 v+ q5 z5 j
make
/ ^* W- g$ o+ u三.nginx添加modsecurity模块- i" i/ {/ u- }& K) l1 ?, i
% L; D# k# W* W+ n9 N! R/ r/ z- a
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ q0 E7 x! i$ H. E; L  \/ R  Q  c* K5 x+ e7 {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine- Z% `4 }' @3 W0 Z) h; d
make && make install
. ?0 k( R% A7 J, _四.添加规则
( e- [5 F. m# l* A5 Y$ o4 a; {; }# {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' ]9 g  g/ d$ z7 P) t. u3 {
6 G8 X" U; W: C3 I/ q3 |/ Z
1.下载OWASP规则:
- a5 _' @/ K) c$ [4 l- s3 s' P  Y
4 v* v( ^9 O/ q' P4 o$ x+ W, Fgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 L* M2 v; B+ k
8 B2 b/ W9 O  W' Y; w
mv owasp-modsecurity-crs /opt/tengine/conf/
$ z& u* h( r( q
$ P# B4 v( ?7 scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* u# D& y7 O$ D2 Q1 f; o$ O2.启用OWASP规则:. S. S* f9 f3 |. M* _# ^# p2 w% e9 G

0 d; z' c, t2 ?+ w, `5 X0 \- ^复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) I7 K" h' ?0 e. E: V) G  R  @% a0 @: Y# e9 P
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ K+ l$ u5 N7 n( [2 m" |- d1 l

5 ?) Y" I; d& |; F% F3 ]# ?* R- F8 a+ B8 aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& s8 F: Y/ t/ @, J/ J# h

$ x6 z  ~, o6 w2 F' I% ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 g- k  E3 C  D) {9 d* DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* L  i( d0 }! Q& ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" G9 S; i8 D$ pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- P% k5 {( C  C. O# V/ Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 g/ W' i1 ]0 v( L! K! f5 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" n4 F3 J0 Q# u6 Y6 d  NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) u  B4 l6 p6 [* G& D1 h) M1 M
五.配置nginx" y4 S9 ]" C2 j! C) A
6 I# B, T) o5 l+ r9 j* A' H2 C
在需要启用modsecurity的主机的location下面加入下面两行即可:2 Q7 o: w$ C3 H3 ^
  j! O8 w: g; ]5 k3 [
ModSecurityEnabled on;  
/ C. E6 J4 Y) q. l% ~ModSecurityConfig modsecurity.conf;8 n* ^5 L3 ]0 |! O0 x9 |
下面是两个示例配置,php虚拟主机:
/ U2 B" H. T' j/ U  i! _
( G# D. e' v- v: j3 A0 v; |8 jserver {- f; a0 F6 a+ X* l9 p, C' v# l: {0 _
      listen      80;
! T/ }  D4 X3 f9 q8 p( d      server_name 52os.net www.52os.net;
1 f6 c1 B* u: ^     
+ p2 j# {% V3 n$ q      location ~ \.php$ {- E/ d' r8 S3 P# L9 y7 g3 X+ |
      ModSecurityEnabled on;  5 i* k3 y$ p- e) p( J' _/ W
      ModSecurityConfig modsecurity.conf;( j: I6 q* [# n) X

+ T; f% E3 W6 T1 K      root /web/wordpress;" J( h% ?, o4 e
      index index.php index.html index.htm;. v+ I# I6 P+ M
  0 i% e" k, z5 U
      fastcgi_pass   127.0.0.1:9000;( l7 A# y: M; }- N
      fastcgi_index  index.php;; E3 w. X$ l2 V" _9 |% f/ f( f# `
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;0 h6 r" r- F4 e
      include        fastcgi_params;8 w* \3 H: o6 C! ~  ~
      }0 D/ Y6 S- a  j& w/ T7 g# j# X7 g
  }9 d: T$ ^/ D) w" X' Y5 `. @
upstream负载均衡:% X3 x+ L# F& D$ O  G9 n" N( V3 X
* @* ~: Y) z% T; q4 p, N
upstream 52os.net {; _: V5 j- v* ^1 [# R
    server 192.168.1.100:8080;
7 s/ ^1 a6 J" X) D9 T    server 192.168.1.101:8080 backup;
7 v* [; a$ z1 s$ `: L# o}5 G  S  n' I5 U" b# }7 m" T
9 V, Y& X+ ?. m$ `  j
server {
- H1 D! s: O1 \listen 80;
# `" E, p+ k" i: J& }- {% T8 Dserver_name 52os.net www.52os.net;+ o% n& C  e" ?9 ]& o8 j5 n/ D# N

$ _0 a1 B5 V5 a& @  O" x/ llocation / {, T. D, \- ~( D
    ModSecurityEnabled on;  
8 {2 J1 V! ^' k/ Q1 q& M( v    ModSecurityConfig modsecurity.conf;  ! o* a( e7 b+ `# b, `- O; V
& p( I0 j) }9 \
        proxy_pass http://online;# T4 y0 g5 T1 ^  N, N
        proxy_redirect         off;, c" q, b& p3 {& x+ {  K: n
        proxy_set_header Host $host;
. @! m7 V2 O% @( I        proxy_set_header X-Real-IP $remote_addr;
; @9 }& ?! l* w4 W3 y7 a        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;5 _' ]. N1 E, G9 Y2 _
    }
( S: i3 o' v2 M* w1 R}
/ ~9 D' M  E$ @; I" M, n; u: Z六.测试
9 Z: }4 m! z! e7 h9 D, n
# e8 T& G; [# G9 |" a' ]0 @) m: d4 c3 \: \我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 x3 s1 q7 Q  P7 E" J# D2 _! t" \/ s, |
<?php
: G2 X4 ]4 `* A' }: Z% n7 E    phpinfo();    3 f/ [# ?: |# y7 i, R# }: d
?>* T' m0 F7 }) n) g+ ?, I" V
在浏览器中访问:0 g4 W' ^! @) d0 |0 L0 A  H

* ]: Z2 @4 n( `4 o& g" Qhttp://www.52os.net/phpinfo.php?id=1 正常显示。, P! d0 j+ S3 R9 i/ p2 V, A2 [/ S
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
0 E1 N4 W/ h. Z" Vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 C8 L6 K' ?$ s9 }* l9 u! q& C说明sql注入和xss已经被过滤了% Z8 S( a; A8 @; x
9 U% v5 T2 F1 z1 j  S7 C
七、安装过程中排错- ~, l4 A0 t, D5 l+ r3 b" r3 p

& D( S: v+ C8 W6 E! d1.缺少APXS会报错# U/ a/ y* D4 _. v+ w0 S1 \1 T

% \7 Y0 |& q9 s5 j8 i, ?7 `5 Iconfigure: looking for Apache module support via DSO through APXS! d+ W, `& W; C% q8 q
configure: error: couldn't find APXS) V+ x  {: k$ |& f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) j4 @1 T* T3 L解决方法:
4 ]5 |' s* [9 P7 j" k. P; a3 G. s. v# M; {: f$ O6 g' L8 a
yum install httpd-devel
6 m' C9 h; P9 \7 m4 F  p. z2.没有pcre
' ~4 T$ H( D* e4 ~  b6 F: z6 T; T6 k, B3 Y
configure: *** pcre library not found.
4 j& S7 {# ~9 M% |# o! Kconfigure: error: pcre library is required
& ^& ^6 d6 b* |解决方法:
) s( e# b9 Y+ q- I! k9 {0 L$ F( ~* |, _  b
yum install pcre pcre-devel# ~# Z+ i; E0 L6 ?8 G( A7 R
3.没有libxml2$ A$ @; b- k# Y1 T( @
- J. x  ]: m& @% C, S+ p
6 |+ G7 F- w8 n# j# a. }6 y
configure: *** xml library not found.
4 J( E- f" v1 ?/ U9 F3 dconfigure: error: libxml2 is required) d5 |% d! I) C! Z" z( ~2 x
解决方法:
( S- P' L5 c* S) O7 u5 k0 U
; G* y0 T+ D; e, b9 O, r) Q; ^, u! qyum install  libxml2 libxml2-devel6 C6 p/ w+ ?7 p
4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 x0 J: t! C& [& Z6 y+ r& J
0 o* _8 z/ Y3 Y& Q, E9 a" ETengine version: Tengine/2.1.0 (nginx/1.6.2)
' _! v6 ]$ Z. @' o% c) Dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!- k. A) Z1 t. l
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 c/ y5 @. X! x6 U2 O. @

2 P% y$ [+ f  s( ?+ L: j& O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ V+ Q- f; W- o2 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: E& x' U' ?& g3 T1 O- s  |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ b6 ]$ O7 C0 [# p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 K1 J# i3 M0 A/ H9 p1 L+ k; q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ \4 G4 D( I7 g  R3 G
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ u& g4 T( @" p4 b0 n8 H* s0 R, P
解决方法,移除低版本的APR (1.3.9)4 Z( I' a/ [: Z- Q+ l+ z9 ^
; O+ `3 \1 w5 [3 |
yum remove apr
" e+ L9 |" a+ K5.Error.log中有: Audit log: Failed to lock global mutex
- ~% L  N1 O2 S+ D
& @* m; {1 f5 c9 A8 ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
3 g! Q& h& B7 B9 g' ^3 J, O2 a( Sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) {1 H; x/ x8 q# e* d  g/ |解决方法:0 |/ m( k- O8 N5 F+ R0 u8 h( b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ ^1 I9 z5 x8 Z" `, X$ d7 Y- }" F& G/ T* r5 ?2 X' D) o
SecAuditLogDirMode 0777
1 R6 c" l5 |9 qSecAuditLogFileMode 0550
9 j3 Y/ _; S4 o8 @, H# ]3 xSecAuditLogStorageDir /var/log/modsecurity9 m' H& W( S: F/ n$ S- g# N1 n7 w
SecAuditLogType Concurrent
# Q3 b0 i6 i! z, k, W' W& t参考文章:
# U! e9 D" }; O1 E% k  Y6 }https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( D6 N4 c. C( |1 zhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-4 08:35 , Processed in 0.068475 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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