我正在考虑为Google App Engine开发一款应用程序,该应用程序不应该获得太多流量。我真的不愿意超过免费配额。但是,通过重载应用程序并超过配额,似乎很容易导致拒绝服务攻击。有没有什么方法可以防止或更难超过免费配额?我知道,例如,我可以限制来自IP的请求数量(使其更难超过CPU配额),但是有没有办法让它更难超出请求或带宽配额?
答案 0 :(得分:16)
没有内置工具来阻止DoS。如果您使用java编写Google Apps,则可以使用service.FloodFilter
过滤器。以下代码将在任何Servlet执行之前执行。
package service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
*
* This filter can protect web server from simple DoS attacks
* via request flooding.
*
* It can limit a number of simultaneously processing requests
* from one ip and requests to one page.
*
* To use filter add this lines to your web.xml file in a <web-app> section.
*
<filter>
<filter-name>FloodFilter</filter-name>
<filter-class>service.FloodFilter</filter-class>
<init-param>
<param-name>maxPageRequests</param-name>
<param-value>50</param-value>
</init-param>
<init-param>
<param-name>maxClientRequests</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<param-name>busyPage</param-name>
<param-value>/busy.html</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>JSP flood filter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
*
* PARAMETERS
*
* maxPageRequests: limits simultaneous requests to every page
* maxClientRequests: limits simultaneous requests from one client (ip)
* busyPage: busy page to send to client if the limit is exceeded
* this page MUST NOT be intercepted by this filter
*
*/
public class FloodFilter implements Filter
{
private Map <String, Integer> pageRequests;
private Map <String, Integer> clientRequests;
private ServletContext context;
private int maxPageRequests = 50;
private int maxClientRequests = 10;
private String busyPage = "/busy.html";
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
{
String page = null;
String ip = null;
try {
if ( request instanceof HttpServletRequest ) {
// obtaining client ip and page URI without parameters & jsessionid
HttpServletRequest req = (HttpServletRequest) request;
page = req.getRequestURI();
if ( page.indexOf( ';' ) >= 0 )
page = page.substring( 0, page.indexOf( ';' ) );
ip = req.getRemoteAddr();
// trying & registering request
if ( !tryRequest( page, ip ) ) {
// too many requests in process (from one client or for this page)
context.log( "Flood denied from "+ip+" on page "+page );
page = null;
// forwarding to busy page
context.getRequestDispatcher( busyPage ).forward( request, response );
return;
}
}
// requesting next filter or servlet
chain.doFilter( request, response );
} finally {
if ( page != null )
// unregistering the request
releaseRequest( page, ip );
}
}
private synchronized boolean tryRequest( String page, String ip )
{
// checking page requests
Integer pNum = pageRequests.get( page );
if ( pNum == null )
pNum = 1;
else {
if ( pNum > maxPageRequests )
return false;
pNum = pNum + 1;
}
// checking client requests
Integer cNum = clientRequests.get( ip );
if ( cNum == null )
cNum = 1;
else {
if ( cNum > maxClientRequests )
return false;
cNum = cNum + 1;
}
pageRequests.put( page, pNum );
clientRequests.put( ip, cNum );
return true;
}
private synchronized void releaseRequest( String page, String ip )
{
// removing page request
Integer pNum = pageRequests.get( page );
if ( pNum == null ) return;
if ( pNum <= 1 )
pageRequests.remove( page );
else
pageRequests.put( page, pNum-1 );
// removing client request
Integer cNum = clientRequests.get( ip );
if ( cNum == null ) return;
if ( cNum <= 1 )
clientRequests.remove( ip );
else
clientRequests.put( ip, cNum-1 );
}
public synchronized void init( FilterConfig config ) throws ServletException
{
// configuring filter
this.context = config.getServletContext();
pageRequests = new HashMap <String,Integer> ();
clientRequests = new HashMap <String,Integer> ();
String s = config.getInitParameter( "maxPageRequests" );
if ( s != null )
maxPageRequests = Integer.parseInt( s );
s = config.getInitParameter( "maxClientRequests" );
if ( s != null )
maxClientRequests = Integer.parseInt( s );
s = config.getInitParameter( "busyPage" );
if ( s != null )
busyPage = s;
}
public synchronized void destroy()
{
pageRequests.clear();
clientRequests.clear();
}
}
如果您使用的是python,则可能需要滚动自己的过滤器。
答案 1 :(得分:7)
我不确定是否可能,但是App Engine FAQs表示如果你能证明它是DOS攻击,那么他们将退还与攻击相关的任何费用。
答案 2 :(得分:2)
看起来他们现在有一个基于IP地址的过滤器可用于Python和Java(我知道这是一个旧线程,但它在Google搜索中仍然很高)。
https://developers.google.com/appengine/docs/python/config/dos
答案 3 :(得分:1)
始终可以使用在App Engine应用程序前提供拒绝服务保护功能的服务。例如,Cloudflare提供了备受推崇的服务https://www.cloudflare.com/waf/,还有其他服务。我的理解(免责声明:我还没有亲自使用过该服务)这些功能可以在免费计划中找到。
在您的应用程序本身中构建基于memcache的速率限制实现也相当容易。这是我从Google搜索此方法获得的第一个搜索:http://blog.simonwillison.net/post/57956846132/ratelimitcache。这种机制是合理的,并且可以节省成本,因为共享内存缓存使用可能就足够并且是免费的。此外,走这条路线可以控制旋钮。缺点是应用程序本身必须处理HTTP请求并决定允许或拒绝它,因此可能需要花费成本(或[免费]配额耗尽)。
完全披露:我在Google App上工作,与Cloudflare或Simon Willison没有关联。
答案 4 :(得分:1)
GAE firewall最近发布,旨在取代之前相当有限的DoS Protection Service。
它通过(REST)Admin API支持防火墙规则的编程更新:apps.firewall.ingressRules可以与其他答案中描述的DoS检测的应用内逻辑相结合。不同之处在于,一旦规则被部署,违规请求将不再产生费用,因为它们不再到达应用程序,因此不需要应用程序内过滤。