找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13092|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 K, u. a, I# @4 }3 w. i/ G9 Q# K8 L
一.准备工作
+ B$ c1 e7 Q3 p0 u1 D2 l0 X2 \" o( `# c# n0 H5 @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, Q5 U6 B& o# C7 W
; |0 A: _# u4 j) Y. Y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 U/ N5 o# n/ G" K7 R( E. t0 [
1 ^: Z: I2 q* m) A' _8 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& B6 f/ ~5 Z; E2 u3 F% G7 N
2 Z  b  g) m% o( p$ C/ S, yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 Z  e; `$ A% g" n
& ~9 i9 h% q  N( ~1 ^- q, F* w
依赖关系:
* u( W) q/ m% P+ Btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 r) Y0 [5 \" ?( O7 R0 i4 a' y& `" P7 \/ D6 _5 t
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
8 t- Z% A0 \* I9 Mmodsecurty依赖的包:pcre httpd-devel libxml2 apr$ Q9 X0 C1 m: \5 O" a# @

5 K! q3 x/ Q/ x5 ?* e9 fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, {0 v5 |% t* _) {/ l
二.启用standalone模块并编译& H7 G2 \1 z9 H* r- t( D
8 Q- [4 z8 Q7 B% t# U
下载modsecurity for nginx 解压,进入解压后目录执行:, c" j4 g9 B7 S- X5 ]) J) F/ L8 t8 y8 z

' [7 s) C0 ]3 d9 h9 s8 E! F./autogen.sh/ h$ b* _; h  j- ~- O" @% `
./configure --enable-standalone-module --disable-mlogc) v/ \- Y+ O. \: H! j
make
7 G, u! ^% X5 c4 |* ]三.nginx添加modsecurity模块1 C% E2 N/ M0 ~; y7 c9 q& |, d$ l) A

: K  j( D+ l9 f% X6 V. r' m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" P6 ]2 a* ^+ V

2 m/ z$ U3 p: }+ R- G4 @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 a. E! u7 D) }& [" l  H  T
make && make install
/ t/ t* O- l! H1 K; F8 w/ n四.添加规则
6 t- p3 i. S& |5 o& _# W7 v9 }1 N% r3 o  L1 N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; z8 [/ n' }" p7 C, |

! p' q* N: d% Z3 i1.下载OWASP规则:2 {% S+ E* N, G/ _

5 t3 A9 `3 R5 }/ ]+ Q$ b; Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 Y3 t* \9 Y, ]3 z
4 r* H% h* h/ K, n1 V* E$ zmv owasp-modsecurity-crs /opt/tengine/conf/
2 B% O5 f$ ^/ i8 T7 U5 q# p
% G% c/ a- Z/ b' t; x3 V. x; Ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 O7 a- [* Y6 {) x( G; H2.启用OWASP规则:
! u  _1 ]  d. V! \, h4 M0 q
  M, U' V6 Q$ c" K! H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) \# X. [; P$ r' O- v. E- T. f
; S4 J5 G8 \& @7 Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- w: q! X, j0 C

" M' Y: `$ _  J4 X! Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 V& b. ]; I8 H& j5 k
9 R- y0 n  R9 \8 c1 x0 V2 x$ f4 z* a3 C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  Z& M- w. h& b4 A5 ?' r' g. KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- _5 T) J1 ]8 W/ O4 a+ p) ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# Q& C$ `! i1 [8 UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* V3 J1 t. @9 E! H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; P+ O1 k. f. v: t& Y+ K/ bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 c6 Z0 _3 N  L6 n  ]# Z+ O0 g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. X7 z6 m% ]( w  [( V4 m4 P五.配置nginx7 b1 ?: }' q6 p3 w% y2 g7 t  h
' S+ I" U* C2 w1 M! S
在需要启用modsecurity的主机的location下面加入下面两行即可:, l# \* [5 S+ p- m5 p

/ {0 b; h; }5 J' x, v0 P# z' AModSecurityEnabled on;  
, p# u1 I) q! \4 G% R  M7 ]ModSecurityConfig modsecurity.conf;
( S3 L- t+ W- ^# x# A下面是两个示例配置,php虚拟主机:6 l7 M( C  ]" Q, Z/ n) R* L" I& h7 T

& ?8 O) P- e) c) \# j8 q3 S! Userver {
9 t. B* g5 Z4 u; y' U      listen      80;
# A. F- h0 i2 `9 |" x) t      server_name 52os.net www.52os.net;2 G- S0 M& k5 K! S5 q5 w% e  p% ]
     
3 |, [0 `) v% C/ R# p. u      location ~ \.php$ {0 m6 m4 I" i5 J) L
      ModSecurityEnabled on;  ( x- {. |) w$ L3 I3 n
      ModSecurityConfig modsecurity.conf;
& l$ V0 w. J9 o. o( _1 j) A- f
( F1 x( _$ k% X7 E' V: h" t/ ^      root /web/wordpress;
$ p# s- b' q& q: L% ~      index index.php index.html index.htm;5 h. D( C1 p2 W$ Y; H
  ( ]0 D7 b( M% C, S$ G% f
      fastcgi_pass   127.0.0.1:9000;1 s# {5 G+ N  X- ]
      fastcgi_index  index.php;
: @4 _2 {6 t# q: b7 I4 a1 W: o      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
6 O" e# S  q/ f. v9 k! {* v      include        fastcgi_params;
1 j0 ]' d0 ~* ?4 a& Q      }/ D* Z+ O! M6 y; h6 }
  }) o, m+ u; h+ X; m
upstream负载均衡:  K# @! o0 j, T
2 R% T2 K  T1 d9 Y5 F7 Q
upstream 52os.net {$ z7 y# I% D' I. }
    server 192.168.1.100:8080;
2 u3 m: L8 V* s) T- J1 X    server 192.168.1.101:8080 backup;
% K$ ~9 L$ m7 p; ]+ {1 ^. u3 X}
; T( J3 x% d! A2 n, O  I: c9 T' c0 Q2 T0 |6 K7 w& L
server {+ q' x  v" a7 B: e
listen 80;
8 r/ Y6 v; }; H5 q, k4 G/ rserver_name 52os.net www.52os.net;# w5 i) f6 u& f7 \7 y% X
& \1 e: V6 z5 ~" s! j. `
location / {
; u$ P4 X9 ?& I- R, W    ModSecurityEnabled on;  
' e+ p6 `) {0 j/ i# Y" u# Z    ModSecurityConfig modsecurity.conf;  
' U. J7 I0 g. B
; p% `4 g9 W* ~/ }" d- w( Y  O7 O        proxy_pass http://online;
: _2 r  ^; p- [% C2 Z        proxy_redirect         off;
. d% u$ T1 |, [; U3 e  h        proxy_set_header Host $host;. v1 z9 u" {, q8 L  A
        proxy_set_header X-Real-IP $remote_addr;. @* c6 [) n( ?( C; h0 i, `
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) `0 u+ O* q% p  a9 \
    }1 V0 L6 l) U5 w3 p" x* I9 q& g
}1 C, f) o/ f% L3 g' e
六.测试5 L6 S! u5 z# A; s4 ?  c" E$ I7 I% Z
2 h* T, `- {6 m8 D& |+ @& j# p
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ F0 s! g% k6 M( D1 l
' E5 w; ?/ U9 W9 C/ x5 n# Q, k  d<?php
; M! u2 u" a5 V    phpinfo();    - e% V4 ~0 P: G2 ^$ x
?>
+ w7 v( _+ b8 ]- j$ s在浏览器中访问:
& U- @7 n% A) ?* @4 D( R9 A* l& J/ n5 i5 ?$ n2 V
http://www.52os.net/phpinfo.php?id=1 正常显示。0 }# J2 b! R4 y! n+ k
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
2 A. Y+ f. b7 xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! _  R2 H$ ~  ~$ _说明sql注入和xss已经被过滤了
. a. X& O! g# v( L2 G2 a- q. V! ~( X9 L% u. f+ R. h1 e, ]7 U3 {
七、安装过程中排错1 \; p6 t/ n9 \

1 _  Z! X# r8 c+ b" Y7 E1.缺少APXS会报错
" m5 a6 T, s) ?% W: {# m
, r3 F# n6 ~! i0 {configure: looking for Apache module support via DSO through APXS
  G$ c2 A$ A/ [  u8 C5 P/ cconfigure: error: couldn't find APXS5 H' n5 t* w; W- s' z* i, S' j
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 ~9 \* @1 Q/ N( d9 a8 H$ H+ Y解决方法:
: @, n- V5 p+ p5 P
; O8 q2 W- A, I7 }6 B, X" }4 ayum install httpd-devel
5 z! D3 |6 ^4 p3 v' c* q0 R6 G2.没有pcre
# l6 j, J, p/ y; H/ C& k& U, t
  e- k6 A. s, ^: ~6 Kconfigure: *** pcre library not found.
4 R6 r7 }8 W5 b" v2 Rconfigure: error: pcre library is required# |$ m! u$ {1 L
解决方法:3 T" A, t/ a% K- T5 m! ~5 c
6 u0 r7 j5 U" O5 r
yum install pcre pcre-devel
5 q# t# _9 k" n3.没有libxml26 z8 D  u& }9 E' X& G1 `8 ^6 A

" T6 Q. @% A0 C  e: M& {2 f, c
2 B- ~& l- L- `configure: *** xml library not found.  R: a8 f# P1 k. V  d0 E- }/ t
configure: error: libxml2 is required
6 o- [7 F% r( c9 m解决方法:, k! v5 |0 J6 l8 V  C+ C

7 u/ w; @7 N1 s( u8 [yum install  libxml2 libxml2-devel
9 G3 _9 ^6 _6 t6 }4.执行 /opt/tengine/sbin/nginx -m 时有警告8 W7 F$ d# A/ X6 F
8 I. a; }# g" Q- Z. J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 `) Z3 r' i/ v. J! q3 c; anginx: [warn] ModSecurity: Loaded APR do not match with compiled!* ]# X4 U) b( ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ f) b$ L4 Z  ?; |+ J1 Q- j

& V& n! U% W/ A  S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( b9 U! h( Z4 b" h2 P7 L$ A, F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
& e4 e3 T5 w. w  k2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; ?$ j0 d2 X) F1 B* x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 W- l8 u. n% P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# {/ ?9 j4 h4 d% l: H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 c/ Z' c, j: _7 E; f, E解决方法,移除低版本的APR (1.3.9)
' |* W+ P& X7 G- P. u. t
( b+ b1 ]# z" \/ k- r1 Pyum remove apr' L% l' z2 ^7 g! o
5.Error.log中有: Audit log: Failed to lock global mutex- H) f# a7 p, v$ T% V: Q+ @' u- D. k& E
9 F0 U# p- a  t, _% z3 _
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     3 x0 R  q' o& Z& [
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 n. z% P; a1 P' ]. Y' h9 H; N8 p# u解决方法:. F) V8 R3 \  Y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 I( U% f0 }+ n, A! |& y$ S7 P( S; U
4 `0 G0 Z7 `3 x! D! NSecAuditLogDirMode 0777
: E# f) W* p" T- f4 WSecAuditLogFileMode 0550
0 x9 i0 ]' a- f8 s4 M7 kSecAuditLogStorageDir /var/log/modsecurity
0 l5 e+ Q1 R0 E( R: H$ |4 _# s1 ^( ZSecAuditLogType Concurrent: G; u" x! \6 B* {
参考文章:( _, I7 T1 p! @. ~5 \1 M0 ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 c; q; B! n, E" J! p' J' dhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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