找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13115|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! X' R- D8 g) B+ {) c  D5 @* s6 R
. @  k" }1 K5 h( D9 {! d- X1 p  q' L一.准备工作
( e+ c3 ^, n9 v1 V
. G; [) K8 R6 P4 L  h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- M9 W* T- W8 h) s  f+ v- `

; ?5 [$ [9 Z2 y- |( e9 `5 {, mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 B- P2 `6 f4 x0 ~
) M. B) e1 A, Y3 Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 J0 M% v8 a* ^  z/ m. Y; T2 Q0 m( {" |  T# E
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" D% d5 d8 P; q& a, l" y2 ]
0 |8 J: E: K3 P8 G$ {# P依赖关系:' m4 P: q6 k( L- q* }9 a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 Y" `# a! `! T, O6 S( l' X
, E+ E, F3 V  C2 J. a3 s3 F
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
/ G$ `# b( N3 h3 c$ Bmodsecurty依赖的包:pcre httpd-devel libxml2 apr0 [/ X. I3 B# m" Y+ {5 d0 ]
  U% j0 m% a; G
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* ]  Y7 `- _# B. D3 B" K
二.启用standalone模块并编译: j2 Q" r- b2 T& t+ k( b' R. q' }
8 A! T$ `- w/ q+ `6 C
下载modsecurity for nginx 解压,进入解压后目录执行:
. H3 H* `2 ~) c
4 L! l) d$ @9 e3 }./autogen.sh- n+ c1 o* g4 M- k7 z
./configure --enable-standalone-module --disable-mlogc0 A. c3 F, c7 n% G& v2 P
make / p6 U% l, g! U
三.nginx添加modsecurity模块6 T0 u+ n  L# P' Y0 f

, A) Q  a7 D, j* r, U$ E7 T% T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% U+ P( D* \; s" ~3 q1 K

. ]  D* m" @# V3 O! m; a  y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
, t5 C) k* V3 q" umake && make install
. Z0 t' h$ _! Y/ H5 ~) x% G四.添加规则
  p* }8 @1 d) O
4 \  r" d$ H+ a* pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- l* X$ q& ]4 C4 O& L4 l
- n4 b; f7 \  [% W5 J
1.下载OWASP规则:
" P# b+ m, C- I; W9 N: n2 q# @
- L2 s9 t. r, r- A! x1 Y$ _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( n0 [. H( W! D5 ~" Z# y) q3 @6 B
mv owasp-modsecurity-crs /opt/tengine/conf/( {  q1 c. B  }/ |% M6 B% m

; z# M& S7 k1 x: k  v; e7 lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. J! n9 H7 `+ P3 k4 O; A) `1 u2.启用OWASP规则:8 @) u6 Y. X7 O. M$ V1 |
1 F5 `2 v# F! o, w6 a
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 G( r" f) W; C8 R
) z" `  A, k) r编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- a5 S# `5 V" p3 ?( ]3 U
; Q5 G$ c+ G2 h1 ~6 s" a! _; v: z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# D5 b5 g% m3 F7 G2 a6 D6 D

. b- M% A% j1 q% y8 H5 mInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 \$ r/ [. |9 G% d6 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 l2 D0 Z+ d* f% a1 i. h0 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 w- p1 O5 d7 c# R: h& nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ t1 e; l0 }1 g6 ~0 s" d' y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
, O! {4 I- h- a- F: \# zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- s6 Z$ Z; K# E0 N3 Y1 XInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf  t: |6 Y# y, N" K" F6 B; U
五.配置nginx+ y5 N# h5 e2 J0 F: m) P6 H

( a0 H- r6 ]; j4 T1 M在需要启用modsecurity的主机的location下面加入下面两行即可:
& }& q2 T- I4 ?( i# b4 M9 P8 H( Z8 i% a0 x7 Z, V# T- @
ModSecurityEnabled on;  
; v" C  h6 ]% i; O; cModSecurityConfig modsecurity.conf;
' M, e# k' n* ~& t下面是两个示例配置,php虚拟主机:" R1 c( Y2 `3 J4 Q' l! y, ~/ x
) {, Y1 q* D. w
server {$ |8 k# b0 K8 }
      listen      80;! Q+ _$ e/ w; Z6 e6 A3 o$ H& \: p
      server_name 52os.net www.52os.net;$ A0 ]! K8 C' |4 \+ V2 D
     
; l" V& A* d. N# }' E      location ~ \.php$ {
/ Z) ]4 q; Y" E* a+ J) o, U/ ~      ModSecurityEnabled on;  
) ]2 n6 [9 ^0 @8 g, L      ModSecurityConfig modsecurity.conf;) Q) {$ [& @/ ]8 |2 J# U
3 b* O, T6 L6 X
      root /web/wordpress;
7 k( `) y& }8 f' F      index index.php index.html index.htm;
7 X7 Y4 O6 {" r7 t. I0 x  
! V9 v6 X) n6 {- I0 r, l" C      fastcgi_pass   127.0.0.1:9000;( v+ ?4 z3 h4 S
      fastcgi_index  index.php;  k3 H% m/ Z/ B0 }; X! x7 ~. [
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 n$ s# \8 D# S$ ?5 R  K- ]      include        fastcgi_params;0 `" h. l5 Z4 K7 r9 u
      }5 R' v0 Y) A1 \# ~( b8 ]. A
  }4 T' Y' g- Y$ z' d8 N% {; V
upstream负载均衡:
7 a5 }: s6 i. N, b
+ d" S; @! w* O; }8 gupstream 52os.net {
" s2 J* ~6 U7 B; H    server 192.168.1.100:8080;* ?" z7 D/ N1 o  n
    server 192.168.1.101:8080 backup;
; Q$ Y; i# a+ I}
; E/ r) G8 {4 \, ]8 h1 j
/ L' Y* v+ Z% @server {
* b- N  W- D7 q2 J3 g  blisten 80;
5 k; F, U8 E8 }( ~" rserver_name 52os.net www.52os.net;
$ A8 W' ]# x3 l. c. p% C4 z5 o) S/ e5 A
location / {& u# c2 G; s3 s! A  X5 ^4 }
    ModSecurityEnabled on;  2 I9 \( G2 u! T5 t3 }
    ModSecurityConfig modsecurity.conf;  
3 n# d- s+ A' D0 e" }; d2 {2 U1 N3 {
        proxy_pass http://online;
5 y/ f; \* p4 C, |+ Z4 A        proxy_redirect         off;
) u: m* u4 `, ^" Z" N4 @% j        proxy_set_header Host $host;
; ]$ }5 |* a/ _$ @0 O        proxy_set_header X-Real-IP $remote_addr;4 X) U1 |1 Q# T5 F
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' R( O/ V5 B; N+ a5 z# O; X" A) e. X& Y
    }& s0 ?9 `% W( r. j
}0 s% \' `3 X  w7 O7 c
六.测试( H7 [2 o: l/ T4 k9 r$ g4 n

. f% r3 ^% r8 S% g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 o: N6 I+ v5 n+ w- {$ E" ?: n, l' s- G
<?php4 E; R; d" i0 V" z) [7 q
    phpinfo();    * a3 ~6 V# D. C- A
?>
) w! P3 q- @0 e, d( o在浏览器中访问:+ j/ Q& L8 ~. f! Z# B. F2 o# H& Y
0 M) M) u1 N# A5 h' y' d2 q( z
http://www.52os.net/phpinfo.php?id=1 正常显示。1 i: G1 s) `# k
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ ]3 \4 `; p2 P$ O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
' H/ Q$ C' G  ]6 [+ @说明sql注入和xss已经被过滤了$ D: g# M4 g$ V% e9 l  r

: ]4 A/ N8 ?' k0 D. j3 u9 V七、安装过程中排错1 n. }/ X2 }+ \, l- c7 _
1 G1 O: {! }; m" i
1.缺少APXS会报错- W6 G9 Z- t2 u$ w$ X: Y7 X

5 h8 h% K  P4 B; h* \: uconfigure: looking for Apache module support via DSO through APXS9 w/ {, P4 ]2 |9 X* x* w# Z
configure: error: couldn't find APXS
4 b* ?3 |8 c+ k0 Z9 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 j9 Q+ I/ A' t; G: ^6 N" _
解决方法:
: f/ {* V4 d9 y' O) ]4 B* @
0 U' Z6 c4 h4 |5 x$ e# V/ I: }yum install httpd-devel/ t) V- z& U0 R1 r# |5 x! f9 R
2.没有pcre  s! @& X$ {* U3 e- j! f3 H8 h* i
0 g5 c- D9 Y. D7 ]; Z& L
configure: *** pcre library not found.0 r! J% j5 h1 M
configure: error: pcre library is required
4 u! c8 v' I, U/ b6 o解决方法:
2 W; Y) ~2 t) o5 n2 Q. D: J3 {6 G8 z, S- K: L9 m5 D# \# |
yum install pcre pcre-devel  a7 q3 b( [9 S  l8 }
3.没有libxml22 x5 G  s( ?: s$ J$ z1 Z' {

6 b6 l& t. k6 Z% H) U: S
  J9 n* M& n* h9 xconfigure: *** xml library not found.# C7 P; R" o) U4 |
configure: error: libxml2 is required
7 O. l; G0 `! f2 M4 l解决方法:
) _" }4 G' m1 P+ n! o# b9 {! |
$ u/ o. r9 l& X; ?7 P+ e: e: y: ayum install  libxml2 libxml2-devel6 u0 W" a9 C* F$ N
4.执行 /opt/tengine/sbin/nginx -m 时有警告" s3 m" g* J+ h" K

/ g8 q3 z. t( {5 @( [6 e5 S. p+ ?; eTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ x6 @5 p& u6 [, ^3 e9 Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# {8 _  V$ o6 L! T. v4 J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! u9 ?: B! P, d7 ^! J9 N# i# @8 e" y# x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" l1 m7 k4 W' O/ U, P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- M  \- T3 \* @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ P; }* a4 p9 ~3 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 W; M) A4 B, p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ v6 ?! V( p! S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! r# \+ [/ K9 I解决方法,移除低版本的APR (1.3.9)* q  P- E. }& `- q; k
: \5 E, n8 x6 J2 y& s
yum remove apr
* F" x' Q( X$ o( u  U! ]: C& h2 F5.Error.log中有: Audit log: Failed to lock global mutex
6 R- B# k+ f1 e& l  G2 i) N- ^: F6 M" i0 k6 F3 V4 S0 K
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) U$ n& |4 F# V0 }% Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 m) ]/ B8 ?/ K  i! q  K解决方法:1 e9 x( v* `3 j* F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 \, H; R5 N* y& b& K7 M" }
7 B5 \* u8 |+ _: D* ~! J' H4 LSecAuditLogDirMode 0777
9 `7 M% a6 \$ RSecAuditLogFileMode 0550
  c' |! k( ]8 cSecAuditLogStorageDir /var/log/modsecurity
& a' d  e, }6 J5 c) ASecAuditLogType Concurrent+ D) A* \4 ~* a
参考文章:3 k5 w) n+ N- g7 U; |; T
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" {1 ?$ c0 J0 k5 T( y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-16 11:11 , Processed in 0.046841 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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