PHP email injection attack technology (1)

Source: Internet
Author: User
Tags php email

1. Introduction

Today, the use of the Internet has risen sharply, but the vast majority of Internet users have no security knowledge background. Most people use the Internet to communicate with others by Email. For this reason, most websites allow their users to contact them, provide suggestions to the website, report a problem, or request feedback. The user will send an email to the website administrator.

Fortunately, most web developers do not have enough knowledge about secure Code-Security, some of which use existing libraries or frameworks that are vulnerable to many known vulnerabilities. These vulnerabilities have been published, and the vendor has fixed them, and the corresponding attack source code poc can be downloaded on the Internet, but most developers are too reluctant to upgrade to the latest version.

Today we will talk about email injection. Attackers can use your email server to send spam.

2. Mail Injection

From Wikipedia:

Email injection is a security vulnerability widely used in Internet email sending and receiving applications. This is an email injection, similar to an HTTP header injection. Similar to SQL injection attacks, this vulnerability is a common vulnerability that occurs when one programming language is embedded into another, for example, MYSQL is embedded into PHP.

When a form that can submit data to a Web application is added to a Web page, malicious users may use the MIME format to add additional information to the message to be sent (POST/GET), such as a new recipient list or a completely different message body. Because the MIME format uses carriage return to separate the information in the data packet (each line in the HTTP data packet has a line break, and there are two line breaks between POST and http header ), by adding carriage return to submit form data (some plug-ins of FB can be easily used), a simple message board can be used to send thousands of messages. Similarly, a spam sender can use this tactic to maliciously send a large number of anonymous messages.

Email injection is an attack type targeting PHP's built-in email function. It allows malicious attackers to inject any header fields, such as BCC, CC, and subject. It allows hackers to send spam mails from the victim's email server through injection. For this reason, this attack is called email injection or spam. This vulnerability is not limited to PHP. It may affect any application that receives messages from the user UI and sends email messages. The main cause of this attack is the improper user input verification or the application does not have a verification and filtering mechanism at all.

3. Principles of email injection attacks

The old Chinese saying goes well: only by knowing the truth can we know the truth.

To explain how mail injection works, we must first understand the working principles of the PHP Email function. The following describes how to find an API in PHP Manual.

 
 
  1. mail(): 
  2.  
  3. http://www.php.net/manual/en/function.mail.php 
  4.  
  5. bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) 

You can note that this requires three required parameters ("destination, topic, and message") and other optional parameters and functions to return a Boolean value.

 
Let's look at a code with a vulnerability to demonstrate this vulnerability:

 
 
  1. <?php 
  2.  $to="littlehann@foxmail.com"; 
  3.  if (!isset($_POST["send"])) 
  4.  { 
  5. ?> 
  6.    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
  7.    From: <input type="text" name="sender"> 
  8.    Subject : <input type="text" name="subject"> 
  9.    Message : 
  10.    <textarea name="message" rows="10" cols="60" lines="20"></textarea> 
  11.    <input type="submit" name="send" value="Send"> 
  12.    </form> 
  13. <?php 
  14.  } 
  15.  else 
  16.  { 
  17.    // the form has been submitted 
  18.    $from=$_POST['sender']; 
  19.    // send mail : 
  20.    if (mail($to,$_POST['subject'],$_POST['message'],"From: $fromn")) 
  21.    { 
  22.      echo "Your mail has been sent successfully"; 
  23.    } 
  24.    else 
  25.    { 
  26.      echo "An error has been occured !"; 
  27.    } 
  28.  } 
  29.  ?> 

The preceding Code demonstrates the purpose and explains the attack principles. The preceding code is divided into three parts:

Part 1

 
 
  1. <?php 
  2.  $to="littlehann@foxmail.com"; 
  3.  if (!isset($_POST["send"])){ 
  4. ?> 

This code will check whether the form is submitted or not. The user clicks the submit button and the normal response to the script accessing this page is different. if this code returns True (the final result in the if statement is true), this means that the form is not submitted. The form appears, waiting for user input. On the other hand, if it returns "False", it means the form has been submitted, so the email will be sent.

Part 2

 
 
  1. <form method="POST" action="<?echo $_SERVER['PHP_SELF'];?>"> 
  2.  From: <input type="text" name="sender"> 
  3.  Subject : <input type="text" name="subject"> 
  4.  Message : 
  5.  <textarea name="message" rows="10" cols="60" lines="20"></textarea> 
  6.  <input type="submit" name="send" value="Send"> 
  7.  </form> 

The second part is an HTML form tag, which requires user input.

Part 3

 
 
  1. <?php 
  2. else 
  3.    // the form has been submitted 
  4.    $from=$_POST['sender']; 
  5.    // send mail : 
  6.    if (mail($to,$_POST['subject'],$_POST['message'],"From: $fromn")) 
  7.    { 
  8.      echo "Your mail has been sent successfully"; 
  9.    } 
  10.    else 
  11.    { 
  12.      echo "An error has been occured !"; 
  13.    } 
  14. } ?> 

In the previous code, we can pay special attention to this line.Mail ($ to, $ _ POST ['subobject'], $ _ POST ['message'], "From: $ fromn ")The mail () function of PHP requires parameters such as subject, message, and from. If the function is successfully executed, after the PHP engine sends an email, the message "Your mail has been sent successfully" is displayed ". If An error occurs, the message "An error has been occurred" is displayed.

But a friend asked, where is the problem? The main issue does not require necessary verification and filtering of user input. As mentioned in white hat web security, any security issue can be attributed to a trusted question, the problem here is that the program code has unlimited trust in user input. As you can see in the third part of the code, the sending function code receives input (including the subject, message, and source) from the user, and the parameters are not filtered and verified. Therefore, malicious attackers can control the value of these parameters and send inject attacks.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.