阿里云容器服务--配置自定义路由服务应对DDOS攻击
摘要: 容器服务中,除了slb之外,自定义路由服务(基于HAProxy)也可以作为DDOS攻击的一道防线,本文阐述了几种方法来应对普通规模的DDOS攻击
1. TCP洪水攻击(SYN Flood)
ECS系统参数调整,应对TCP洪水攻击,打开文件/etc/sysctl.conf,配置如下参数
# Protection SYN flood
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.tcp_max_syn_backlog = 1024
执行如下命令,使配置文件生效
sysctl -p
2. 慢速连接攻击
一个 Http 请求通常包括头部、url、methods 等,服务器需要接收整个 Http 请求后会做出响应。恶意用户发送缓慢的 Http 请求,比如一个字节一个字节的发送头部,服务器将一直处于 wating 状态,从而耗费服务器的资源。Haproxy 通过配置 timeout http-request 参数,当一个用户的请求时间超过设定值时,Haproxy 断开与该用户的连接。示例compose模板如下:
lb:
image: registry.aliyuncs.com/acs/proxy:0.5
ports:
- '80:80'
restart: always
labels:
# addon 使得proxy镜像有订阅注册中心的能力,动态加载服务的路由
aliyun.custom_addon: "proxy"
# 每台vm 部署一个该镜像的容器
aliyun.global: "true"
# 前端绑定SLB
aliyun.lb.port_80: tcp://proxy_test:80
environment:
# 支持加载路由的后端容器的范围,"*"表示整个集群,默认为应用内的服务
ADDITIONAL_SERVICES: "*"
EXTRA_DEFAULT_SETTINGS: 'timeout http-request 5s'
appone:
ports:
- 80/tcp
- 443/tcp
image: 'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
labels:
# 此处支持http/https/ws/wss 协议
aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
restart: always
生成的HAProxy配置文件为:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
log-send-hostname
maxconn 4096
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.stats level admin
ssl-default-bind-options no-sslv3
ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA
defaults
balance roundrobin
log global
mode http
option redispatch
option httplog
option dontlognull
option forwardfor
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 5s # 该处指令应对慢速连接攻击
listen stats
bind :1936
mode http
stats enable
timeout connect 10s
timeout client 1m
timeout server 1m
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
stats auth stats:stats
frontend port_80
bind :80
reqadd X-Forwarded-Proto:\ http
maxconn 4096
acl is_websocket hdr(Upgrade) -i WebSocket
acl host_rule_1 hdr(host) -i appone.example.com
acl host_rule_1_port hdr(host) -i appone.example.com:80
use_backend SERVICE_test-routing_appone if host_rule_1 or host_rule_1_port
backend SERVICE_test-routing_appone
server test-routing_appone_1 172.19.0.8:443 check inter 2000 rise 2 fall 3
server test-routing_appone_1 172.19.0.8:80 check inter 2000 rise 2 fall 3
通过 telnet 登录验证结果
$ telnet 120.76.43.112 80
Trying 120.76.43.112...
Connected to 120.76.43.112.
Escape character is '^]'.
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>
Connection closed by foreign host.
3. 限制每个用户的并发连接数量
以网站为例,普通用户访问网站,或者从网站下载东西时,浏览器一般会建立 5-7 个 TCP 链接。当一个恶意打开了大量 TCP 链接时,耗费服务器大量资源,影响其它用户的访问,因此我们需要根据实际情况,限制同一个用户的链接数。示例compose模板如下:
lb:
image: registry.aliyuncs.com/acs/proxy:0.5
ports:
- '80:80'
restart: always
labels:
# addon 使得proxy镜像有订阅注册中心的能力,动态加载服务的路由
aliyun.custom_addon: "proxy"
# 每台vm 部署一个该镜像的容器
aliyun.global: "true"
# 前端绑定SLB
aliyun.lb.port_80: tcp://proxy_test:80
environment:
# 支持加载路由的后端容器的范围,"*"表示整个集群,默认为应用内的服务
ADDITIONAL_SERVICES: "*"
EXTRA_DEFAULT_SETTINGS: 'timeout http-request 5s'
EXTRA_FRONTEND_SETTINGS_80: 'stick-table type ip size 100k expire 30s store conn_cur,# Shut the new connection as long as the client has already 10 opened,tcp-request connection reject if { src_conn_cur ge 10 },tcp-request connection track-sc1 src'
appone:
ports:
- 80/tcp
- 443/tcp
image: 'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
labels:
# 此处支持http/https/ws/wss 协议
aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
restart: always
生成的HAProxy配置文件为:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
log-send-hostname
maxconn 4096
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.stats level admin
ssl-default-bind-options no-sslv3
ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA
defaults
balance roundrobin
log global
mode http
option redispatch
option httplog
option dontlognull
option forwardfor
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 5s
listen stats
bind :1936
mode http
stats enable
timeout connect 10s
timeout client 1m
timeout server 1m
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
stats auth stats:stats
frontend port_80
bind :80
reqadd X-Forwarded-Proto:\ http
maxconn 4096
stick-table type ip size 100k expire 30s store conn_cur
# Shut the new connection as long as the client has already 10 opened
tcp-request connection reject if { src_conn_cur ge 10 }
tcp-request connection track-sc1 src
acl is_websocket hdr(Upgrade) -i WebSocket
acl host_rule_1 hdr(host) -i appone.example.com
acl host_rule_1_port hdr(host) -i appone.example.com:80
use_backend SERVICE_test-routing_appone if host_rule_1 or host_rule_1_port
backend SERVICE_test-routing_appone
server test-routing_appone_1 172.19.0.8:443 check inter 2000 rise 2 fall 3
server test-routing_appone_1 172.19.0.8:80 check inter 2000 rise 2 fall 3
利用 apache 测试工具做验证,和服务器一直保持建立 10 个链接。
$ ab -H"host:appone.example.com" -n 5000000 -c 10 http://127.0.0.1:80/
用 telnet 打开第 11 个链接,服务器拒绝该链接。
$ telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
4. 限制每个用户建立连接速度
仅仅限制单个用户的并发链接数并意味着万事大吉,如果用户在短时间内向服务器不断的发送建立和关闭链接请求,也会耗费服务器资源,影响服务器端的性能,因此需要控制单个用户的访问速率。
通常情况下,考虑到用户通过浏览器一般会建立 5-7 条 TCP 链接,我们可以认为普通用户在 3 秒内不应该建立超过 10 条链接。示例compose模板如下:
lb:
image: registry.aliyuncs.com/acs/proxy:0.5
ports:
- '80:80'
restart: always
labels:
# addon 使得proxy镜像有订阅注册中心的能力,动态加载服务的路由
aliyun.custom_addon: "proxy"
# 每台vm 部署一个该镜像的容器
aliyun.global: "true"
# 前端绑定SLB
aliyun.lb.port_80: tcp://proxy_test:80
environment:
# 支持加载路由的后端容器的范围,"*"表示整个集群,默认为应用内的服务
ADDITIONAL_SERVICES: "*"
EXTRA_DEFAULT_SETTINGS: 'timeout http-request 5s'
EXTRA_FRONTEND_SETTINGS_80: '# Table definition,stick-table type ip size 100k expire 30s store conn_rate(3s),# Shut the new connection as long as the client has already 10 opened,tcp-request connection reject if { src_conn_rate ge 10 },tcp-request connection track-sc1 src'
appone:
ports:
- 80/tcp
- 443/tcp
image: 'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
labels:
# 此处支持http/https/ws/wss 协议
aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
restart: always
生成的配置为:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
log-send-hostname
maxconn 4096
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.stats level admin
ssl-default-bind-options no-sslv3
ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA
defaults
balance roundrobin
log global
mode http
option redispatch
option httplog
option dontlognull
option forwardfor
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 5s
listen stats
bind :1936
mode http
stats enable
timeout connect 10s
timeout client 1m
timeout server 1m
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
stats auth stats:stats
frontend port_80
bind :80
reqadd X-Forwarded-Proto:\ http
maxconn 4096
# Table definition
stick-table type ip size 100k expire 30s store conn_rate(3s)
# Shut the new connection as long as the client has already 10 opened
tcp-request connection reject if { src_conn_rate ge 10 }
tcp-request connection track-sc1 src
acl is_websocket hdr(Upgrade) -i WebSocket
acl host_rule_1 hdr(host) -i appone.example.com
acl host_rule_1_port hdr(host) -i appone.example.com:80
use_backend SERVICE_test-routing_appone if host_rule_1 or host_rule_1_port
backend SERVICE_test-routing_appone
server test-routing_appone_1 172.19.0.8:443 check inter 2000 rise 2 fall 3
server test-routing_appone_1 172.19.0.8:80 check inter 2000 rise 2 fall 3
测试,采用 ab 打开 10 个链接。
$ ab -n 10 -c 1 -r http://127.0.0.1:8080/
再用 telnet 打开第 11 个链接,服务器拒绝该请求。
$ telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
阿里云容器服务--配置自定义路由服务应对DDOS攻击的更多相关文章
- 品尝阿里云容器服务:用nginx镜像创建容器,体验基于域名的路由机制
在前一篇博文中我们了解了阿里云容器服务的路由机制: 请求 -> 负载均衡80端口 -> 容器主机9080端口 -> acsrouting路由容器80端口 --基于域名--> W ...
- 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署
部署场景是这样的,我们基于 ASP.NET Core 2.0 Preview 1 开发了一个用于管理缓存的 Web API ,想通过阿里云容器服务基于 Docker 部署为内网服务. 在这篇博文中分享 ...
- 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
摘要: 全球开源区块链领域影响最为广泛的Hyperledger Fabric日前宣布了1.1版本的正式发布,带来了一系列丰富的新功能以及在安全性.性能与扩展性等方面的显著提升.阿里云容器服务区块链解决 ...
- 阿里云容器服务与ASP.NET Core部署:用 docker secrets 保存 appsettings.Production.json
这是我们使用阿里云容器服务基于 docker 容器部署 asp.net core 应用遇到的另一个问题 —— 如果将包含敏感信息的应用配置文件 appsettings.Production.json ...
- 容器监控—阿里云&容器内部服务监控
目前Docker的使用越来越离不开对容器的监控,阿里云最近上线了容器服务,不但提供了核心的容器和宿主机监控能力,而且支持集成 Cloud Insight 监控,下面会介绍如何集成. 首先介绍一下阿里云 ...
- 在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用
本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四 ...
- .Net Core in Docker - 使用阿里云Codepipeline及阿里云容器镜像服务实现持续集成(CI)
前面已经介绍过了 .Net Core 程序发布到 Docker 容器的内容.但是每次通过 SSH 链接到服务器敲命令,运行脚本也是挺麻烦的一件事.程序员是最懒的,能让电脑解决的问题绝不手动解决,如果当 ...
- Knative 应用在阿里云容器服务上的最佳实践
作者|元毅 阿里云智能事业群高级开发工程师 相信通过前面几个章节的内容,大家对 Knative 有了初步的体感,那么在云原生时代如何在云上玩转 Knative?本篇内容就给你带来了 Knative 应 ...
- 利用阿里云容器服务打通TensorFlow持续训练链路
本系列将利用Docker和阿里云容器服务,帮助您上手TensorFlow的机器学习方案 第一篇:打造TensorFlow的实验环境 第二篇:轻松搭建TensorFlow Serving集群 第三篇:打 ...
随机推荐
- VMware Workstation 10安装Centos6.4操作步骤说明
1.在网上下载VMware Workstation 10, 百度软件中心助手安装程序高速下载,下载完成后默认是自动启动安装的,而原来的安装程序文件保存在: C:\Users\用户名\Document ...
- 理解lua 语言中的点、冒号与self
转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...
- Chpater 10: Sorting
Internal Sort: Bubble O(n2) Selection O(n2) Insertion O(n2) Shell O(nlogn) Merge O(nlogn) Heap O(nl ...
- (转)开源爬虫larbin分析
转自风中之炎的博客:http://www.cnblogs.com/FengYan/archive/2012/02/04/2338630.html 1. larbin简介(百度百科) larbin是一种 ...
- POJ 2912 Rochambeau(难,好题,枚举+带权并查集)
下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- span标签里的内容在IE下显示,而在谷歌浏览器下不显示
有如下代码: <span id="spLicenseIncrease" style="color:red;">(51)</span> 在 ...
- 如何防止通过IP地址访问Tomcat管理页面
方法:建议修改webapps下面的原始文件夹的名称,比如加一个后缀: 当需要用管理页面的时候,可以将含有manager的文件夹的后缀去掉即可 manager和host-manager共2个文件夹
- hdu 1796 How many integers can you find
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...
- ASP.NET并发处理
http://blog.csdn.net/hliq5399/article/details/6280288