找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11891|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。& V! ]' `. @8 `

' |1 p8 Z% }: K1 ~# V- ^: n8 l% I一.准备工作
9 w& W+ b3 ^7 ~1 z8 g3 M1 a* J4 q8 c0 ^( n. x3 [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" }8 f& \  `4 F6 b7 y
* N5 r' f+ Q0 H& z& B/ t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
, k" G; X" o7 C5 V: [& h5 G% g3 [. u1 I% s/ g% P0 V1 F
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 l  h( A1 c) F8 v: Y9 X
/ P7 O; G! I; w9 G1 I: T
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 s% K/ c1 T) s9 C+ J/ `" x" z+ h
( P- M; \# c0 H8 \依赖关系:/ d8 i  L; ^8 {& y: b; B5 E, n
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 T7 M% i6 A% c2 ~
7 }! f7 C- n1 i0 e5 X* c  I
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel- X+ Y9 J5 d! Z2 {
modsecurty依赖的包:pcre httpd-devel libxml2 apr7 k9 P' }1 v: c$ \7 v6 z

# u. i) l4 h2 ?yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ L- i$ m- N& O
二.启用standalone模块并编译
8 O; ^  u  ]$ P
# \, z) q' U  _$ k! H下载modsecurity for nginx 解压,进入解压后目录执行:
. h+ w2 `: a8 b( `; }% i8 y
6 J+ D$ A7 w5 W3 i' t+ |; W./autogen.sh: `9 U3 _1 Q2 P$ Q/ y
./configure --enable-standalone-module --disable-mlogc0 W! Y- M  Q3 A. M% u4 P
make : T) ^9 [: o( f: e: h1 [
三.nginx添加modsecurity模块
: w  [- r! p7 W
5 C7 M) }, [6 S4 `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 J1 T- I6 r! B: ^  u
! L- i1 _  E9 Z- Z" o./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
) G! R# V& L" S3 ?& V0 K! ]make && make install
3 K  U( J* Z0 i四.添加规则+ k0 i; A+ W' S
5 a, t7 `: C: o5 C
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 [1 p6 D: T4 z5 J! m2 o& K" z2 T7 T1 k3 j: S3 @" T  {
1.下载OWASP规则:
6 \& b% S. k- O+ q- D, q# M! y, l$ U  m: d7 o/ r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 }! Y' O" N( ^. t
! q8 f+ P: e+ p( e. ?6 M6 x1 Cmv owasp-modsecurity-crs /opt/tengine/conf/4 t& Y1 K* L  t

" y3 A9 \, [$ a$ @; gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 U* l; U1 P. }; z2.启用OWASP规则:1 I- g8 r  i! g4 L6 E

" y. _0 M3 _) M4 c- v& ^6 o4 y; {( `复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% F+ S  {2 p1 D. j& B

; o6 x' [' |+ E8 c" S: C2 n. ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# Y0 B; B/ x- E% O" s4 ^5 E. J9 J1 R

3 r1 w0 C0 A7 S* q; f6 h$ Q$ K- oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ `4 |1 t6 |9 V7 C- U: t7 c& x
4 J' H' w& j: h. S" x! c  F; ?8 p
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 Z: o- s' r& s' V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- G, V  J4 ?) I% d% K, Q0 H4 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# T0 a% A; |. X5 X* ]3 k' z/ Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) G0 B# ^: |+ F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' N2 ~! y( K) S& SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( h" [7 ?/ x: i# x& L. q, \+ `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 l% _1 U3 @8 q  X% j五.配置nginx
+ f) S2 M# \9 f( @- c( t( ~% @- H& [# P; i8 ^- d0 S6 M
在需要启用modsecurity的主机的location下面加入下面两行即可:
# @( d8 @+ r1 U1 M) t! @) U1 s
" z0 g1 g8 a  {1 F  K+ fModSecurityEnabled on;  " L7 O. [1 p# P2 Z2 v+ Q
ModSecurityConfig modsecurity.conf;
6 V0 G$ @4 A4 F8 Y' h6 R) [7 U下面是两个示例配置,php虚拟主机:& H; E2 \2 O1 h' E3 m* |$ r

2 e1 A- j. ^; O' xserver {
% Y- f  h8 {- M2 {' l7 z      listen      80;
+ g: S% l  W& P: s3 Y      server_name 52os.net www.52os.net;
2 C7 a/ Y# e8 k5 [7 y     7 p! S2 h9 }9 p$ ?
      location ~ \.php$ {
2 ]- b3 i. N! ~; i* f* B7 V      ModSecurityEnabled on;  6 k+ q2 m+ |8 D4 b) A, b
      ModSecurityConfig modsecurity.conf;
+ l5 M" l0 @+ N! B
; a  i1 W3 y* x; n      root /web/wordpress;; E+ B9 s. X$ S. l6 W: i4 F
      index index.php index.html index.htm;+ m. x* a% `- E7 Q% z
  
2 J( ?* B' T; I5 R1 x      fastcgi_pass   127.0.0.1:9000;3 J" x& |( X6 t8 C* b: o) f
      fastcgi_index  index.php;
* L- E* [3 d# \2 V4 J4 \* x+ F      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ h$ S$ p' ?1 P      include        fastcgi_params;
, e# E, ~" Z/ U3 R      }9 r" U( h7 Y4 j$ g7 ]
  }
1 o8 P: |: t; [* R9 Eupstream负载均衡:: T* G% y) H: U- W

% V2 K, T: A& @4 `5 gupstream 52os.net {
9 m- H: B- K! }/ a% M/ r    server 192.168.1.100:8080;
& U& J3 k- n) ~, `+ S    server 192.168.1.101:8080 backup;
! M3 I+ W; T( c8 I" Z& H* S' V: s7 l}
# f! _9 N! l" R3 @! _( j
3 E' K* e- L" W3 ^server {! |, v% l# d  Q- B4 F
listen 80;
" `1 m( a" m1 c/ K: D. D, Cserver_name 52os.net www.52os.net;
- j' _5 W) h8 W3 |! e8 Q
  N, Z+ ?0 m: N! Elocation / {
  W) Z' b6 p+ [* _    ModSecurityEnabled on;  
2 K9 ]% [) _: A8 ~3 t    ModSecurityConfig modsecurity.conf;  " d+ C, V8 R) {& h; J- e1 Q

" W% B/ v6 |8 ^' Q* x& {, D% a        proxy_pass http://online;1 d1 t6 b! f% z, y
        proxy_redirect         off;
- x; j1 Y0 s- U$ Z8 j3 m$ K        proxy_set_header Host $host;' R% U! q% G; V
        proxy_set_header X-Real-IP $remote_addr;, ?; ?9 J% M0 [* [4 t0 _+ |
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) R3 t+ T; Q" u& x0 K
    }
$ A! _6 G! M, _, u2 A/ ~}
0 P6 A+ ]$ s0 A9 l1 l. k7 ~: ~六.测试
* M; Y; L. M! ]. |$ s1 w2 `9 R2 {. U! p3 n+ t
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 c7 @+ \# T+ _# P  C) q* {, k% j( X
<?php6 e& \- Z% ?8 ~' h- Z  p
    phpinfo();    ' \  `1 u2 R* \2 A# r2 T/ p) z
?>& _' D9 T( H8 m9 p6 g
在浏览器中访问:, R8 I7 I! f3 j) {( {
5 C# d3 o" c6 Q" j: m- e7 {5 X+ r7 |
http://www.52os.net/phpinfo.php?id=1 正常显示。
; u. i" S- C7 Hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 E% P( x$ E: d
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 n1 }0 A$ P4 u, b& G: i2 D
说明sql注入和xss已经被过滤了( J: _$ m6 T. q& F$ j& m

: B( F+ h8 u1 W. S# D7 D5 O$ X七、安装过程中排错
) z# @: ~5 [$ o2 M' ?3 l0 ?9 Q
" g! v) y8 U2 Z. s6 y1 k1.缺少APXS会报错4 j" W, m4 [% K! y+ [
% m7 Q) Q4 e# H
configure: looking for Apache module support via DSO through APXS
2 a$ X+ Y; o3 b, x9 @+ `% x: ]configure: error: couldn't find APXS
* t8 A3 S6 c, j' Y( U% I9 Kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) c1 V7 Q% F1 W解决方法:
& W7 [" Y3 E( ~, B3 c
  B: F$ X+ y8 D/ ^1 _yum install httpd-devel
8 O% `$ t! Y6 S0 K( p& h2.没有pcre- i  J/ m- ?) `. e$ N
6 @( \) m+ U! r2 q
configure: *** pcre library not found.3 c4 P. _6 ]% h7 ~- M' k& H9 j
configure: error: pcre library is required
+ f- {4 F* @6 K/ V解决方法:8 \& L% O, p( L7 R9 \

7 V$ M4 }% ~2 X4 {yum install pcre pcre-devel7 y6 Y" U# ?# P  G
3.没有libxml2* S* ]% E2 p& Q1 z$ `

2 R1 n! v+ T7 v" C5 E5 ?- d5 Z' J8 K  L
configure: *** xml library not found.& }3 V8 x' d2 \$ W: [  M
configure: error: libxml2 is required
5 }2 z/ Y( d6 y' p' U解决方法:
2 c  G- P6 s* }" p- a& |, Y& m$ z3 r$ {6 s6 u
yum install  libxml2 libxml2-devel' T) D1 r7 V+ s- A% N
4.执行 /opt/tengine/sbin/nginx -m 时有警告! u1 g9 Y! k3 e& i

5 b0 g: ~1 G' W/ b* {Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' w4 Z( H" x2 s2 v+ W/ [2 z( N" Anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, |. Q/ h. E' d% M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, a3 m* B4 T' [# n" @

# A" w0 K4 k# m! R& j' ]5 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 y" x4 ~0 r. B4 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". z  K" v; F4 _1 u
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 C0 t0 Z, |! A$ f5 f. w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  K0 a# X1 R$ M) `9 P" ~: i6 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 s5 D' b7 r8 ]: u) O' ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 _4 ?; ]9 a  B9 D8 Z/ M- B7 ~解决方法,移除低版本的APR (1.3.9)
# I5 s2 d) E( I4 E
7 P, p5 P+ w9 x# Syum remove apr
# J7 E- \7 F) d. o. ~5.Error.log中有: Audit log: Failed to lock global mutex
, h! |- F  I+ X4 Q- i. ^1 O6 d- }7 e/ }8 G2 z) |, T" D1 d) }4 f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % Q0 K( p/ n' c$ }* L8 ^
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( E& J+ a+ K0 K6 n/ Y9 u解决方法:
- d- q; H: o2 m8 P. `5 N, K' z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 e, g  t3 h  @; L1 a
, Y' X" P$ |. m7 r
SecAuditLogDirMode 0777/ d: F5 B6 D( a! O4 g
SecAuditLogFileMode 0550% J; D8 E2 W, G- D( L
SecAuditLogStorageDir /var/log/modsecurity: w4 M: B5 z* T5 f2 F% @
SecAuditLogType Concurrent
" `9 v" y1 i1 e# S& u6 X0 ^参考文章:* C0 o8 d' n8 {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" [) x( b) O3 `# H5 u9 Z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-23 06:28 , Processed in 0.070755 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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