找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13366|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ s( S7 |+ G( w3 \$ P
# r8 O, i; y* F  [一.准备工作
. \/ r8 w* F# |. q# `: T) R% ]* z9 f. \, |. p
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* P5 ~# J7 [, Y% h2 J# D; ~

: Z  K/ V3 b- m8 ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 r+ p" T- Z' V* c
1 I' s; i( R* J5 |$ j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ x" ^% W# p1 ]/ @* a4 W% M) R5 R+ c1 F5 w, ?; m, X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' \) m2 Q+ B1 C# M5 Z+ a5 g5 E5 ^* ^
依赖关系:
$ f8 v4 S6 C2 \& ]+ D& k3 ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ r: z9 y- l4 V3 M' C3 l
3 O- _/ f; F7 Y4 k7 H+ K& P# C. i1 jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel1 I5 O2 Q# p# r! R0 w
modsecurty依赖的包:pcre httpd-devel libxml2 apr- h- Z% f/ m& C+ i* Y4 J& y

3 t! E4 w5 R9 iyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
0 D% m) g+ w: R! o& L, r& S, o' p二.启用standalone模块并编译
- j% v' z* P2 `2 A; `! n& a% D4 t% N- P$ i; ~( v
下载modsecurity for nginx 解压,进入解压后目录执行:6 f4 \& K" W' Q1 C  Y4 \; m" s. w

( p( q7 C6 Y* H( e- G7 O  e' B./autogen.sh# Y# }# R9 H( R
./configure --enable-standalone-module --disable-mlogc
6 n4 o, r& Q4 Y$ ~3 O) |make
$ x+ I  h5 }3 {- T4 B5 |5 _: p三.nginx添加modsecurity模块8 s2 |+ U# c$ i: u: J! Q% v9 d4 G

, g# T* y3 \! p5 e  ?在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 ~% w  H0 g# B. K- X, X% H# n4 U
# q8 |( U- p; t1 p" ]- I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 d+ A5 o6 x, z0 F; Y' x+ Y5 S9 {# Dmake && make install
4 x. u* G/ {3 }' x, I7 _& n( T四.添加规则$ o: j$ w) W3 t& i" ]* [1 m

. W/ P: F2 D; b; |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 B- y. y# K' f! T0 s  w% u2 K' f4 Z9 S9 j& j
1.下载OWASP规则:
( s2 y8 U9 T3 {1 f3 `# I! [: t5 I5 H4 y1 p
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" y& |, F/ _9 C$ }6 [% \
5 G$ N/ i  ^' W+ n
mv owasp-modsecurity-crs /opt/tengine/conf/. E1 U" ^% s( j! T

3 Q7 n- H. i7 ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ }( X8 c; O& c! c2.启用OWASP规则:+ {/ l' Q9 f! A* `2 O
  ~" x( K3 x9 S" q/ w1 z$ R
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" z: y8 [% C/ f% E3 k! t9 Q2 a9 H3 y% A9 L! d& f) i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 ?+ B! Y$ H' t* g$ S- ?" N
" q8 {5 K. g- Y5 k& `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 n2 K! H4 G7 H/ F) K$ n5 J$ z3 T% w' [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ z" w- C+ f% Y  p0 q0 s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% T5 q5 p' @/ o  zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# D' H# b- t5 x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 t* W. {2 k  q) V% PInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( E* q5 Y# S- X' @0 A' b  I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- O" ^. G6 o6 KInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 s$ G. c) M/ T五.配置nginx
0 X3 G/ a' W+ [8 t8 K$ }
. p9 f6 F# S! M: z* U& }在需要启用modsecurity的主机的location下面加入下面两行即可:
/ O5 g3 U- w; ~( p3 I0 V0 d) e9 M) u6 O' K' b
ModSecurityEnabled on;  3 b" l1 g% ^6 y( ~2 t* e5 \7 u- o
ModSecurityConfig modsecurity.conf;
5 K% P6 @- D  q5 x8 }; x# _) m7 U! g# A9 A下面是两个示例配置,php虚拟主机:
  _8 x9 p. x5 C8 {5 N# w6 S! p4 [$ s& R  K/ n- `, D5 l  `1 Q
server {
9 N  @: Y, Y) ]      listen      80;7 {# \3 o+ x% F' K* j
      server_name 52os.net www.52os.net;
) Q% g1 L2 t3 U) r     
4 O0 e0 `# T) ?$ z' r# h$ }      location ~ \.php$ {( G. a( @# B4 Q" U; Z
      ModSecurityEnabled on;  7 g' F& i+ C& {8 G
      ModSecurityConfig modsecurity.conf;
7 |/ O$ `8 u, b, G+ b
- w5 o; X4 M- Y" \$ Z      root /web/wordpress;& ~3 a0 D" L8 o" ^* _6 q. @
      index index.php index.html index.htm;1 b/ G7 B# y3 g- M9 O
  
+ M8 W' R8 ?: c4 G      fastcgi_pass   127.0.0.1:9000;
- J" P$ j# z0 w2 s& n$ I. P' [      fastcgi_index  index.php;& p$ b* D1 A# m! y3 y2 ~- o
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 I. t) r+ i& [) t! g# o
      include        fastcgi_params;8 [: o# ^( S) U- a
      }7 h- j! a6 Q5 Y5 u% a% q
  }
4 W+ F* t  ~" B7 kupstream负载均衡:
8 q; w' @. G$ W' W7 |; K
' ~' F- u8 J* }' K! f4 }upstream 52os.net {
2 T6 B! B4 S: e$ ?# i7 g    server 192.168.1.100:8080;6 ~' G1 o, [6 D2 H# E$ @
    server 192.168.1.101:8080 backup;
9 D1 ?6 k/ @' X6 @}
- M8 C; K$ y3 E/ y: A: X
: T) r  ?1 a5 F! }9 s5 b# Rserver {
) @$ }: K1 i5 X0 n/ Q2 plisten 80;; f- Y( W% K  ^5 U
server_name 52os.net www.52os.net;) @; Q. L5 \. I

! G; y; F7 T" r% P3 c% _7 zlocation / {
8 _# P; c. f* @# A1 R  D, @    ModSecurityEnabled on;  . N' _5 D3 [, @0 P3 m6 i
    ModSecurityConfig modsecurity.conf;  
0 Z& R  L+ E4 J/ m$ u0 t# Y! p. h  Y5 s3 k0 D! ^; R* U4 ~
        proxy_pass http://online;: h1 o: H% O" i+ l. ?: c* _
        proxy_redirect         off;6 G4 m# I5 Y/ x8 o" i9 m* \
        proxy_set_header Host $host;" N0 ^* N  S1 @  V6 t
        proxy_set_header X-Real-IP $remote_addr;7 p8 x7 {2 T3 P' M) G+ ]3 R9 U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  r3 n7 l- M- p0 c
    }
) U- e: q% w5 k8 H1 Q6 O}
1 {1 a1 f4 B& `6 Z六.测试6 n' E' u1 {! F0 i; p; i% s% X" D
- n( p; X1 Z7 v! q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% Z; c1 Z: H* b0 Q
0 L3 ^) ^8 L3 W
<?php8 P: c3 A' L* K9 L' I  W
    phpinfo();   
3 |' H* |" P! v' e! ^?>2 H, g0 C  t7 l% `6 P/ o
在浏览器中访问:
8 a$ h2 M$ G( }- R
, F6 P9 ^: s. C! y, K# shttp://www.52os.net/phpinfo.php?id=1 正常显示。
- x- ?; n* n$ ?, T4 E1 }2 q' fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% F* P! Q4 x$ Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 ]1 H) y1 z8 z" I* e# O! [5 T说明sql注入和xss已经被过滤了
8 T! L/ k9 I; S0 I% ]$ X6 H! y) H$ f2 _
七、安装过程中排错
/ D* ?, t2 Z) F5 e! v( W& c
8 a0 z  n5 n( A  R% `# J9 o# ]) k  B8 E1.缺少APXS会报错5 Y0 \: e. J& o) |

0 J# C3 M3 l# I' e2 S! Cconfigure: looking for Apache module support via DSO through APXS
0 R/ ~5 H! k9 c% ^% E0 U4 Econfigure: error: couldn't find APXS
: F7 h% j/ K! a: K, d7 `& I, Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 k9 B6 T* Q2 A7 Z* W  V" h解决方法:5 h; D+ \& w/ B6 d  {  {

0 s% Q8 |1 y  j/ ?9 |2 gyum install httpd-devel
/ U+ D, ?$ n. k1 I: U  ^, F3 \" t2.没有pcre
2 k0 |6 ~+ T- O2 {# O; L4 U7 i8 \, u" H7 I* r3 R* A
configure: *** pcre library not found.3 w* O. ]7 e( J1 m
configure: error: pcre library is required3 [9 P2 a( P# X* _( i+ P: J
解决方法:
% b$ o1 z' ^8 s) V; ^: @) j) }4 A) O& L/ K& n4 O1 L" Z' X
yum install pcre pcre-devel
' r6 _7 E. f& e3.没有libxml21 ?8 S& R2 g8 r" H3 Z

8 Z0 I+ m7 {- ^- r1 C# p# L5 D9 W- u
configure: *** xml library not found.* U' r" \$ h5 q- P0 p- L! t
configure: error: libxml2 is required2 w0 s' z6 w8 O* M
解决方法:# h4 F) l" B& I% y1 B; R* m
6 \* }! R# o1 n  s6 v, I
yum install  libxml2 libxml2-devel
& o5 P6 \. @, c9 T. w6 p4.执行 /opt/tengine/sbin/nginx -m 时有警告$ d4 f: h3 O: g3 r6 b& ~
, P0 m7 x; r  ?; [2 [  E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; `' T, U4 c6 d/ C& H9 Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ ^& G* u) U  c/ [- K5 k
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 |1 G0 v0 I: |5 L5 z
$ X$ E; n- ?6 l  a2 W) Q4 ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% ]9 r2 ^% g' n$ j2 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( w. Y5 P& B; m# K2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 `/ }% i+ T1 x7 }$ V. ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 ^- |1 Z4 l( h* T% H4 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 f  ?6 ]) W" X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( g! F4 K4 e# M% i) @+ P解决方法,移除低版本的APR (1.3.9)/ F2 s" B5 c% r5 q* h% \

8 e, f! G' z5 @5 k% r$ Oyum remove apr3 q% F" R+ k$ T. u
5.Error.log中有: Audit log: Failed to lock global mutex  i8 b' w5 d/ k" D) O
- c2 N- ~7 D9 V4 j6 Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 `2 B+ R( p1 aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], m3 f5 O5 p$ @) m/ y8 p8 `
解决方法:/ K" ^% _* s; B
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& @/ I: p1 q8 H/ e# O
3 H. A% n6 E. I2 J
SecAuditLogDirMode 0777
  G, c& A+ G! }$ ~" c' DSecAuditLogFileMode 0550  D* M% B: C3 F6 _
SecAuditLogStorageDir /var/log/modsecurity
; v! j* H3 ?; O" g6 c8 R  p- N* ]SecAuditLogType Concurrent
( R6 X1 X. u; ^参考文章:. s8 u; _$ B* q3 B' F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 ~+ e, O: g* p) V7 D6 whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-7 01:48 , Processed in 0.056979 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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