• Knowledge Database

    Knowledge Database

    Didn't find what you we looking for?
    Why not ask one of our online staff or drop us a message!

    Webhosting | Internet Access | Security | 

    Tutorials > Advanced Web Hosting, PHP > Email Injection from website - how to stop it



    Email Injection from website - how to stop it

    We have seen in big increase in email injection attacks on websites and people/computers trying to use the website's contact form to send out spam email.

    A typical symptom is you receive many junk or blank emails from your website's contact form. This problem is caused by the hacker putting additional specifically and carefully crafted information into the contact form.

    Why is this happening?

    There are a lot of ways to send anonymous emails, some use it to mass mail, some use it to spoof identity, and some (a few) use it to send email anonymously. Usually a web mailform using the mail() function generates emails containing headers with the originating IP of the server it's running on. This is mainly used to send out spam or anonymous email.

    How to resolve this and protect your webhosting

    Your developer must make a few changes to your contact form. This will build in some additional checking to prevent the email injection problem.

    Use the following code to make sure a "Mime" header is not passed through. A mime header is typical of this exploit and is easily detectable.


    <?php
    // First, make sure the form was posted from a browser.
    // For basic web-forms, we don't care about anything
    // other than requests from a browser:    
    if(!isset($_SERVER['HTTP_USER_AGENT'])){
       die(
    "Forbidden - You are not authorized to view this page");
       exit;
    }

    // Make sure the form was indeed POST'ed:
    //  (requires your html form to use: action="post") 
    if(!$_SERVER['REQUEST_METHOD'] == "POST"){
       die(
    "Forbidden - You are not authorized to view this page");
       exit;    
    }

    // Host names from where the form is authorized
    // to be posted from: 
    $authHosts = array("domain.com""domain2.com""domain3.com");

    // Where have we been posted from?
    $fromArray parse_url(strtolower($_SERVER['HTTP_REFERER']));

    // Test to see if the $fromArray used www to get here.
    $wwwUsed strpos($fromArray['host'], "www.");

    // Make sure the form was posted from an approved host name.
    if(!in_array(($wwwUsed === false $fromArray['host'] : substr(stristr($fromArray['host'], '.'), 1)), $authHosts)){    
       
    logBadRequest();
       
    header("HTTP/1.0 403 Forbidden");
           exit;    
    }

    // Attempt to defend against header injections:
    $badStrings = array("Content-Type:",
                         
    "MIME-Version:",
                         
    "Content-Transfer-Encoding:",
                         
    "bcc:",
                         
    "cc:");

    // Loop through each POST'ed value and test if it contains
    // one of the $badStrings:
    foreach($_POST as $k => $v){
       foreach(
    $badStrings as $v2){
           if(
    strpos($v$v2) !== false){
               
    logBadRequest();
               
    header("HTTP/1.0 403 Forbidden");
                   exit;
           }
       }
    }    

    // Made it past spammer test, free up some memory
    // and continue rest of script:    
    unset($k$v$v2$badStrings$authHosts$fromArray$wwwUsed);
    ?> 


    Similar Information from this category