How to enable DDoS protection?

前端 未结 10 1182
天命终不由人
天命终不由人 2020-11-29 15:04

DDoS (Distributed Denial of Service Attacks) are generally blocked on a server level right?

Is there a way to block it on a PHP level, or at least reduce it?

相关标签:
10条回答
  • 2020-11-29 15:28

    Anti DDOS steps:

    • The very first important thing is to identify the ddos attack first. Identifying the ddos attack more early means more better for your server .
    • Getting better bandwidth available for your server. Always keep more than enough bandwidth which is required to for your server. This won’t prevent DDOS attack but it will take longer time. By which you will get some extra time to act.
    • If you own your own web server then you can defend at network parameter by rate limit your router, add filters to drop packets to different sources of attacks, time out half opened connections more aggressively. Also set lower SYN, ICMP and UDP flood drop thresholds.
    • If you don’t have much idea about these things, then go and contact your hosting providers quickly. They can try their best prevent the DDOS attacks.
    • There are also Special DDOS mitigation service provided by Cloudflare and many other companies. By which they can help you to prevent the DDOS attacks. Also many companies offer cheap ddos protection and dos protection.
    0 讨论(0)
  • 2020-11-29 15:31

    DDOS are generally blocked on a server level, Please enable DDOS protection in your Server Level. Please check the below notes for DDOS protections.

    Apache HTTP Server configuration settings that can help prevent DDOS problems:

    The RequestReadTimeout directive allows to limit the time a client may take to send the request.

    Allow 10 seconds to receive the request including the headers and 30 seconds for receiving the request body:

    RequestReadTimeout header=10 body=30
    

    Allow at least 10 seconds to receive the request body. If the client sends data, increase the timeout by 1 second for every 1000 bytes received, with no upper limit for the timeout (except for the limit given indirectly by LimitRequestBody):

    RequestReadTimeout body=10,MinRate=1000
    
    RequestReadTimeout header=10-30,MinRate=500
    RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
    

    The KeepAliveTimeout directive may be also lowered on sites that are subject to DoS attacks. Some sites even turn off the keepalives completely via KeepAlive, which has of course other drawbacks on performance. The values of various timeout-related directives provided by other modules should be checked.

    The directives LimitRequestBody, LimitRequestFields, LimitRequestFieldSize, LimitRequestLine, and LimitXMLRequestBody should be carefully configured to limit resource consumption triggered by client input. Tune the MaxRequestWorkers directive to allow the server to handle the maximum number of simultaneous connections without running out of resources.

    0 讨论(0)
  • 2020-11-29 15:34

    The php level is too late in the request chain.

    Putting your apache server behind an open source appliance may be a good option for you.

    http://tengine.taobao.org/ has some documentation and source code more modules aimed at DDOS prevention. It is a expansion of nginx, so you can easily set it up as a reverse proxy for your apache instance.

    See: http://blog.zhuzhaoyuan.com/2012/01/a-mechanism-to-help-write-web-application-firewalls-for-nginx/ for how to fight collision has DoS attacks.

    Totally forgot too, http://www.cloudflare.com is one the top free web application firewall, they have free and paid plans and will save your ass from DDOS we use it for alot of our high traffic sites just for its caching capabilities. It is awsome!

    0 讨论(0)
  • 2020-11-29 15:35

    How about something like this on PHP side:

    //if user does not change IP, then ban the IP when more than 10 requests per second are detected in 1 second
    $limitps = 10;
    if (!isset($_SESSION['first_request'])){
        $_SESSION['requests'] = 0;
        $_SESSION['first_request'] = $_SERVER['REQUEST_TIME'];
    }
    $_SESSION['requests']++;
    if ($_SESSION['requests']>=10 && strtotime($_SERVER['REQUEST_TIME'])-strtotime($_SESSION['first_request'])<=1){
        //write the IP to a banned_ips.log file and configure your server to retrieve the banned ips from there - now you will be handling this IP outside of PHP
        $_SESSION['banip']==1;
    }elseif(strtotime($_SERVER['REQUEST_TIME'])-strtotime($_SESSION['first_request']) > 2){
        $_SESSION['requests'] = 0;
        $_SESSION['first_request'] = $_SERVER['REQUEST_TIME'];
    }
    
    if ($_SESSION['banip']==1) {
        header('HTTP/1.1 503 Service Unavailable');
        die;
    }
    
    0 讨论(0)
  • 2020-11-29 15:36

    DDoS is best handled by very expensive, purpose-built network appliances. Hosts are generally not good at doing DDoS protection because they are subject to relatively low performance, state exhaustion, limited bandwidth, etc. Use of iptables, apache mods, and similar services can help in some situations if you have no access to DDoS mitigation hardware or a DDoS mitigation service, but it is far from ideal and still leaves you at risk of attack.

    0 讨论(0)
  • 2020-11-29 15:37

    Do NOT use PHP-based protection, it's horrible and will hardly have an impact at all! Configure your webserver to rate-limit requests, for example in Nginx using the limit_req module (http://nginx.org/en/docs/http/ngx_http_limit_req_module.html)

    Although, I would recommend using CloudFlare to combat layer-4 - however not layer-7 based attacks unless you're willing to pay.

    0 讨论(0)
提交回复
热议问题