Home > php教程 > php手册 > PHP邮件注入攻击技术(1)

PHP邮件注入攻击技术(1)

WBOY
Release: 2016-06-13 10:19:34
Original
1063 people have browsed it

1. 简介

如今,互联网的使用急剧上升,但绝大多数互联网用户没有安全知识背景。大多数的人都会使用互联网通过邮件Email的方式和他人进行通信。出于这个原因,大 多数网站允许他们的用户联系他们,向网站提供建议,报告一个问题,或者要求反馈,用户将会发送反馈给网站管理员的电子邮件。

不 幸的是,大多数web开发人员对安全编码Code-Security没有足够的认识,其中的一些程序猿使用现成的库或框架,这些库受到许多已知的漏洞。这 些漏洞是已经公布,厂商并已经对其进行了修补,并且相应的攻击源代码poc都在互联网上可下载的,但大多数开发人员都懒得升级到最新版本。

今天我们要谈论电子邮件注射,攻击者可以使用你的邮件服务器来发送垃圾邮件。

2. 邮件注入

From Wikipedia:

电子邮件注入是一个安全漏洞,这种漏洞广泛存在于在互联网电子邮件收发应用中。这是电子邮件注射和HTTP头注射类似。和SQL注入攻击类似,这种漏洞是一类常见的的漏洞,发生在当一个编程语言是嵌入到另一个,例如MYSQL嵌入到PHP中。

当一个可以提交数据到一个Web应用程序表单被添加到一个Web页面,恶意用户可能会利用MIME格式添加额外的信息到要发送的消息中 (POST/GET),比如一个新的收件人列表或一个完全不同的消息体。因为MIME格式使用回车分隔在数据包中信息(HTTP数据包中的每一行之间都有 一个换行符,在POST和HTTP HEADER之间有两个换行符),通过添加回车提交表单数据(使用FB的一些插件可以很容易的做到),可以允许一个简单的留言板是用来发送成千上万的消 息。同样,一个垃圾邮件发送者可以使用这种战术的恶意发送大量的匿名消息。

电子邮件注入是针对PHP内置邮件功能的一种攻击类型。它允许恶意攻击者注入任何邮件头字段,BCC、CC、主题等,它允许黑客通过注入手段从受害者的邮 件服务器发送垃圾邮件。由于这个原因,这种攻击称为电子邮件注入,或者邮件形式滥发。这个漏洞是不限于PHP。它可能会影响任何从用户UI接收消息并发送 电子邮件消息的应用程序。这种攻击的主要原因是不适当的用户输入验证或应用程序根本没有验证和过滤机制。

3. 邮件注入的攻击原理

中国古话说得好: 知其然才能知其所以然。

为了解释邮件注入的工作原理,我们必须先了解PHP Email函数的工作原理。下面是从PHP Manual中找到API解释

<ol class="dp-c">
<li class="alt"><span><span>mail(): </span></span></li>
<li><span> </span></li>
<li class="alt"><span>http:<span class="comment">//www.php.net/manual/en/function.mail.php</span><span> </span></span></li>
<li><span> </span></li>
<li class="alt"><span>bool mail ( string <span class="vars">$to</span><span> , string </span><span class="vars">$subject</span><span> , string </span><span class="vars">$message</span><span> [, string </span><span class="vars">$additional_headers</span><span> [, string </span><span class="vars">$additional_parameters</span><span> ]] ) </span></span></li>
</ol>
Copy after login

你可以注意到,这需要三个必填参数(”目的地、主题和消息”)和其他一些可选参数和函数返回一个布尔值。

 
那么让我们来看看一个带漏洞的代码来演示这个漏洞:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span> <span class="vars">$to</span><span>=</span><span class="string">"littlehann@foxmail.com"</span><span>; </span></span></li>
<li class="alt"><span> <span class="keyword">if</span><span> (!isset(</span><span class="vars">$_POST</span><span>[</span><span class="string">"send"</span><span>])) </span></span></li>
<li><span> { </span></li>
<li class="alt"><span>?> </span></li>
<li>
<span>   <form class="string">"POST"</form></span><span> action=</span><span class="string">"<?php  echo $_SERVER['PHP_SELF'];?>"</span><span>> </span>
</li>
<li class="alt">
<span>   From: <input class="string">"text"</span><span> name=</span><span class="string">"sender"</span><span>> </span>
</li>
<li>
<span>   Subject : <input class="string">"text"</span><span> name=</span><span class="string">"subject"</span><span>> </span>
</li>
<li class="alt"><span>   Message : </span></li>
<li>
<span>   <textarea class="string">"message"</textarea></span><span> rows=</span><span class="string">"10"</span><span> cols=</span><span class="string">"60"</span><span> lines=</span><span class="string">"20"</span><span>> </span>
</li>
<li class="alt">
<span>   <input class="string">"submit"</span><span> name=</span><span class="string">"send"</span><span> value=</span><span class="string">"Send"</span><span>> </span>
</li>
<li><span>    </span></li>
<li class="alt"><span><?php  </span></span></li>
<li><span> } </span></li>
<li class="alt"><span> <span class="keyword">else</span><span> </span></span></li>
<li><span> { </span></li>
<li class="alt"><span>   <span class="comment">// the form has been submitted</span><span> </span></span></li>
<li><span>   <span class="vars">$from</span><span>=</span><span class="vars">$_POST</span><span>[</span><span class="string">'sender'</span><span>]; </span></span></li>
<li class="alt"><span>   <span class="comment">// send mail :</span><span> </span></span></li>
<li><span>   <span class="keyword">if</span><span> (mail(</span><span class="vars">$to</span><span>,</span><span class="vars">$_POST</span><span>[</span><span class="string">'subject'</span><span>],</span><span class="vars">$_POST</span><span>[</span><span class="string">'message'</span><span>],</span><span class="string">"From: $fromn"</span><span>)) </span></span></li>
<li class="alt"><span>   { </span></li>
<li><span>     <span class="func">echo</span><span> </span><span class="string">"Your mail has been sent successfully"</span><span>; </span></span></li>
<li class="alt"><span>   } </span></li>
<li><span>   <span class="keyword">else</span><span> </span></span></li>
<li class="alt"><span>   { </span></li>
<li><span>     <span class="func">echo</span><span> </span><span class="string">"An error has been occured !"</span><span>; </span></span></li>
<li class="alt"><span>   } </span></li>
<li><span> } </span></li>
<li class="alt"><span> ?> </span></li>
</ol>
Copy after login

前面的代码将用于演示目的和解释我们的攻击原理。我们将前面的代码分成三个部分:

第一部分

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li><span> $<span class="attribute">to</span><span>=</span><span class="attribute-value">"littlehann@foxmail.com"</span><span>; </span></span></li>
<li class="alt"><span> if (!isset($_POST["send"])){ </span></li>
<li><span><span class="tag">?></span><span> </span></span></li>
</ol>
Copy after login

这段代码将检查表单提交或不是。用户点击提交按钮和普通访问这个页面脚本的响应将是不同的, 如果这段代码返回True(if语句中的判断最终结果为true)这意味着表单没有提交。表单将出现,等待用户输入。另一方面,如果它返 回"False",这意味着表单已经提交,所以电子邮件将被发送。

第二部分

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">form</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"POST"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"<?echo  $_SERVER['PHP_SELF'];?>"</span><span class="tag">></span><span> </span></span></span></li>
<li><span> From: <span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"sender"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span> Subject : <span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"subject"</span><span class="tag">></span><span> </span></span></span></li>
<li><span> Message : </span></li>
<li class="alt"><span> <span class="tag"><span class="tag-name">textarea</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"message"</span><span> </span><span class="attribute">rows</span><span>=</span><span class="attribute-value">"10"</span><span> </span><span class="attribute">cols</span><span>=</span><span class="attribute-value">"60"</span><span> </span><span class="attribute">lines</span><span>=</span><span class="attribute-value">"20"</span><span class="tag">></span><span class="tag"></span><span class="tag-name">textarea</span><span class="tag">></span><span> </span></span></span></li>
<li><span> <span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"send"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"Send"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span> <span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span> </span></span></li>
</ol>
Copy after login

第二部分是一个HTML表单标记,这要求用户输入。

第三部分

<ol class="dp-c">
<li><span><span><?php  </span></span></span></li>
<li><span>} </span></li>
<li><span><span class="keyword">else</span><span> </span></span></li>
<li><span>{ </span></li>
<li><span>   <span class="comment">// the form has been submitted</span><span> </span></span></li>
<li><span>   <span class="vars">$from</span><span>=</span><span class="vars">$_POST</span><span>[</span><span class="string">'sender'</span><span>]; </span></span></li>
<li><span>   <span class="comment">// send mail :</span><span> </span></span></li>
<li><span>   <span class="keyword">if</span><span> (mail(</span><span class="vars">$to</span><span>,</span><span class="vars">$_POST</span><span>[</span><span class="string">'subject'</span><span>],</span><span class="vars">$_POST</span><span>[</span><span class="string">'message'</span><span>],</span><span class="string">"From: $fromn"</span><span>)) </span></span></li>
<li><span>   { </span></li>
<li><span>     <span class="func">echo</span><span> </span><span class="string">"Your mail has been sent successfully"</span><span>; </span></span></li>
<li><span>   } </span></li>
<li><span>   <span class="keyword">else</span><span> </span></span></li>
<li><span>   { </span></li>
<li><span>     <span class="func">echo</span><span> </span><span class="string">"An error has been occured !"</span><span>; </span></span></li>
<li><span>   } </span></li>
<li>
<span>} </span><span>?> </span>
</li>
</ol>
Copy after login

在前面的代码中我们可以特别注意这一行 mail($to,$_POST['subject'],$_POST['message'],”From: $fromn”), PHP的mail()函数需要subject, message, from 这些参数。如果函数执行成功,由PHP引擎发送邮件后,将打印出成功提示 "Your mail has been sent successfully"。如果出现错误,将提示相应信息 "An error has been occurred

但是有朋友要问了,问题在哪里?主要的问题对用户的输入没有做必要的验证和过滤,正如《白帽子讲web安全》里说到的,任何的安全问题可以归结为信任的问 题,这里存在的问题就是程序代码对用户的输入无限制的信任。正如你所看到的在第三部分代码,发送邮件功能代码从用户接收输入(包括邮件主题、消息和来源 等),参数没有过滤和验证。因此,恶意攻击者可以任意控制这些参数的值,用户发送inject攻击。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template