[实践OK]PHP mail 模拟socket下的smtp mail类调试成功发邮件信socket 方式,其就是相当于命令行...

jackxiang 2008-5-29 16:09 | |
1.由于50上的qmail不需要验证就可以发信,有了验证反而出现错误,最后在133那台机器就可以了,auth login afeng133@afeng133.sina.net 123qwe(Base64),然后给126(xxx108@126.com)发了信可以收到,不错不错,以下文件存为utf8 格式:

如果有获取whoami之类的,修改配置文件 php.ini 把exec的执行权限放开!

smtp.class.php:

<?php
set_time_limit(120);
class smtp_mail
{
    var $host="";          //主机
    var $port="25";          //端口 一般为25
    var $user="";          //SMTP认证的帐号
    var $pass="";          //认证密码
    var $debug = false;   //是否显示和服务器会话信息?
    var $conn;
    var $socket="";
    var $result_str;      //结果
    var $in;          //客户机发送的命令
    var $from_r;          //真实的源信箱,一般与smtp服务器的用户名一样,否则可能由于smtp服务器的设置而发送不成功
    var $mailformat=0; //邮件格式 0=普通文本 1=html邮件

    function smtp_mail($host,$port,$user,$pass,$debug=false)
    {
        $this->host  = $host;
        $this->port   = $port;
        $this->user   = base64_encode($user);
        $this->pass   = base64_encode($pass);
        $this->debug  = $debug;
        $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);  //具体用法请参考手册
        if($this->socket)
        {
              //$this->result_str  =  "创建SOCKET:".socket_strerror(socket_last_error());
              $this->result_str  =  "创建SOCKET:".iconv("GBK","UTF-8//TRANSLIT//IGNORE", socket_strerror(socket_last_error()));
              $this->debug_show($this->result_str);
        }
        else
        {
            exit("初始化失败,请检查您的网络连接和参数");
        }
        $this->conn = socket_connect($this->socket,$this->host,$this->port);
        if($this->conn)
        {
            $this->result_str  =  "创建SOCKET连接:".iconv("GBK","UTF-8//TRANSLIT//IGNORE",socket_strerror(socket_last_error()));
            $this->debug_show($this->result_str);
        }
        else
        {
            exit("初始化失败,请检查您的网络连接和参数");
        }
        $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
        $this->debug_show($this->result_str);
    }

    function debug_show($str)
    {
        if($this->debug)
        {
            echo $str."<p>\r\n";
        }
    }

    function send($from,$to,$subject,$body)
    {
        if($from == "" || $to == "")
        {
            exit("请输入信箱地址");
        }
        if($subject == "") $sebject = "无标题";
        if($body    == "") $body    = "无内容";

        $All          = "From:".$from."\r\n";
        $All          .= "To:".$to."\r\n";
        $All          .= "Subject:".$subject."\r\n";
        if($this->mailformat==1) $All.= "Content-Type: text/html;\r\n";
        else $All .= "Content-Type: text/plain;\r\n";
        $All          .= "charset=gb2312\r\n\r\n";
        $All          .= $body;
        /*
          如果把$All的内容再加处理,就可以实现发送MIME邮件了
          不过还需要加很多程序
        */

        //以下是和服务器会话
        $this->in       =  "EHLO HELO\r\n";
        $this->docommand();

        $this->in       =  "AUTH LOGIN\r\n";
        $this->docommand();
        $this->in       =  $this->user."\r\n";

        $this->docommand();

        $this->in       =  $this->pass."\r\n";
        $this->docommand();

        if(!eregi("235",$this->result_str)){
           $this->result_str = "smtp 认证失败";
           $this->debug_show($this->result_str);
           return 0;
        }

        $this->in       =  "MAIL FROM:<".$from.">\r\n";
        $this->docommand();

        $this->in       =  "RCPT TO:<".$to.">\r\n";
        $this->docommand();

        $this->in       =  "DATA\r\n";
        $this->docommand();

        $this->in       =  $All."\r\n.\r\n";
        $this->docommand();

        if(!eregi("250",$this->result_str)){
           $this->result_str = "邮件发送失败";
           $this->debug_show($this->result_str);
           return 0;
        }

        $this->in       =  "QUIT\r\n";
        $this->docommand();

        //结束,关闭连接
        return 1;
    }

    function docommand()
    {
        socket_write ($this->socket, $this->in, strlen ($this->in));
        //$this->debug_show("客户机命令:".iconv("GBK","UTF-8", $this->in));
        $this->debug_show("客户机命令:".iconv("GBK","utf-8//TRANSLIT//IGNORE", $this->in)); //防止报错:Notice: iconv(): Unknown error (84)
        $this->result_str = "服务器应答:<font color=#cc0000>".iconv("GBK","UTF-8//TRANSLIT//IGNORE", socket_read ($this->socket, 1024))."</font>";
        $this->debug_show($this->result_str);
    }

} //end class
?>

sendmail.php:

<?php
  header("Content-type: text/html; charset=utf-8");
?>
<?php
  include("smtp.class.php");
  //$mails=new smtp_mail("220.181.15.111","25","xxx108@126.com","********");
  $mails=new smtp_mail("220.181.15.111","25","xxx108@126.com","********",true);
  $date = date("Y-m-d H:i:s");
  if($mails->send("xxx108@126.com","xxx108@126.com","有人于".$date."登录服务器","请注意是否自己在该时间登录。","登录时间。"))
  {
    echo "发送成功!";
  }else{
      echo "邮件服务器忙,请稍候再试试";
  }
?>


如果有获取谁的exec函数加上(修改配置文件 php.ini 把exec的执行权限放开!):

这个程序的实质是模仿如下终端操作:

telnet smtp.sina.net 25
Trying 202.108.37.33...
Connected to smtp.sina.net.
Escape character is '^]'.
220 sina3-197.sina.net ESMTP - qmail-1.04
helo sina.net
250 sina3-197.sina.net
AUTH LOGIN
334 VXNlcm5hbWU6
ZWFzdHN1bkBlYXN0c3VuLnNpbmEubmV0
334 UGFzc3dvcmQ6
MTIzcXdl
235 验证通过- authentication successfully
data
503 请先用 RCPT - RCPT first (#5.5.1)
mail from:<xxx108@126.com>
250 Mail OK
rcpt to:<xxx108@126.com>  
rcpt to:<372647693@qq.com>      //第二个抄送人
250 Mail OK
data
354 End data with <CR><LF>.<CR><LF>
From: Mail test <372647693@qq.com>
Sender: jackxiang <xxx108@126.com>
To: xxx108@126.com
Cc: 372647693@qq.com
Subject: mail testing mail title

This is mail content...

.
250 Mail OK queued as smtp1,C8mowEDpO0uNr0dUwfhiAA--.1433S3 1413984769

(最后这个小数点一定要加上)

此时,372647693@qq.com也会收到,这个cc:就是抄送人,在邮件标题里体现,真正发还是在rcpt to:<372647693@qq.com>      //第二个抄送人。

邮件标题:
mail testing mail title
发件人:Mail test<372647693@qq.com> (由 xxx108@126.com 代发)
收件人:我<xxx108@126.com>
抄送人:372647693<372647693@qq.com>
This is mail content...

QQ邮箱:
发件人:我自己的邮箱 <372647693@qq.com>(由 xxx108@126.com 代发)
时   间:2014年10月22日(星期三) 晚上9:46  
收件人:xdy108 <xxx108@126.com>
抄   送:回忆未来-向东-Jàck <372647693@qq.com>
This is mail content...

————————————————————————————————————————————————————————
以下如果打开$mails=new smtp_mail("smtp.sina.net","25","eastsun@eastsun.sina.net","123qwe","false");  //false
可以看见其到底是如何进行会话的,如下:
创建SOCKET:您的主机中的软件放弃了一个已建立的连接。
创建SOCKET连接:您的主机中的软件放弃了一个已建立的连接。

服务器应答:220 smtp-5-32.sina.net ESMTP - qmail-1.04

客户机命令:EHLO HELO

服务器应答:250-smtp-5-32.sina.net 250-AUTH=LOGIN 250-AUTH LOGIN 250-PIPELININGJH 250 8BITMIME

客户机命令:AUTH LOGIN

服务器应答:334 VXNlcm5hbWU6

客户机命令:ZWFzdHN1bkBlYXN0c3VuLnNpbmEubmV0

服务器应答:334 UGFzc3dvcmQ6

客户机命令:MTIzcXdl

服务器应答:235 验证通过 - authentication successfully

客户机命令:MAIL FROM:xiaoqian@staff.sina.com.cn

服务器应答:250 eastsun@eastsun.sina.netok

客户机命令:RCPT TO:xiangdong2@staff.sina.com.cn

服务器应答:250 ok

客户机命令:DATA

服务器应答:354 请继续 - go ahead

客户机命令:From:xiaoqian@staff.sina.com.cn To:xiangdong2@staff.sina.com.cn Subject:我来个中文测试一下,怎么就不正常了呢,哈哈。。向东 Content-Type: text/plain; charset=gb2312 我来个中文测试一下,这里是信的内容!!! .

服务器应答:250 ok 1212053255 qp 74366

客户机命令:QUIT

服务器应答:221 smtp-5-32.sina.net

发送成功!


对用户名和密码的Base64编码方法:

<?php
$user="xiangdong2@staff.sina.com.cn";          
$pass="xiangdong";
$user   = base64_encode($user);
$pass   = base64_encode($pass);
echo $user."\n";
echo $pass."\n";
?>




[root@vm0000055 ~]# telnet staff.sina.com.cn 110
Trying 10.210.98.10...
Connected to staff.sina.com.cn (10.210.98.10).
Escape character is '^]'.
+OK staff-jes1.sina.com.cn POP3 service (Sun Java(tm) System Messaging Server 6.2-3.04 (built Jul 15 2005))
xiangdong2
-ERR Unrecognized command
xiangdong2@staff.sina.com.cn
-ERR Unrecognized command
USER xiangdong2
+OK Name is a valid mailbox
pass xiangdong  
+OK Maildrop ready
AUTH
-ERR Unrecognized command
LIST
+OK scan listing follows
1 1675
2 8490
3 3004
4 625
5 650
6 1877
7 1928
8 1939
9 2131
.
RETR 1
+OK 1675 octets
Return-path: <jinsong@staff.sina.com.cn>
Received: from sina123123123 ([10.218.26.58]) by staff-jes1.sina.com.cn
(Sun Java System Messaging Server 6.2-3.04 (built Jul 15 2005))
id <0KLS00I01MFE7V00@staff-jes1.sina.com.cn>
(original mail from jinsong@staff.sina.com.cn); Thu,
25 Jun 2009 20:01:15 +0800 (CST)
Received: from sina123123123 ([10.218.26.58]) by staff-jes1.sina.com.cn
(Sun Java System Messaging Server 6.2-3.04 (built Jul 15 2005))
with ESMTPA id <0KLS00OCGMQ2Q1B0@staff-jes1.sina.com.cn>; Thu,
25 Jun 2009 20:01:14 +0800 (CST)
Date: Thu, 25 Jun 2009 20:01:34 +0800
From: =?GB2312?B?wbq+osvJ?= <jinsong@staff.sina.com.cn>
Subject:
=?gb2312?B?vfHM7M/Czuc2o7owMLnHuMnN+LnK1c+jrNbQtefQxbPGueO2q7P2yqHCt9PJxvez9rnK1c8g1OyzybulwarN+NO1yPs=?=
To: cpyw <cpyw@staff.sina.com.cn>, sinamail <sinamail@staff.sina.com.cn>,
platform <platform@staff.sina.com.cn>
Message-id: <200906252001339840407@staff.sina.com.cn>
MIME-version: 1.0
X-Mailer: Foxmail 6, 9, 201, 16 [cn]
Content-type: text/plain; charset=gb2312
Content-transfer-encoding: base64

vfHM7M/Czuc2o7owMLnHuMnN+LnK1c+jrNbQtefQxbPGueO2q7P2yqHCt9PJxvez9rnK1c8g1Oyz
ybulwarN+NO1yPsNCmh0dHA6Ly90ZWNoLnNpbmEuY29tLmNuL3QvMjAwOS0wNi0yNS8xOTMyMzIx
MjIyNS5zaHRtbA0KaHR0cDovL3RlY2guc2luYS5jb20uY24vdC8yMDA5LTA2LTI1LzE5MTYzMjEy
MjEzLnNodG1sDQpodHRwOi8vdGVjaC5zaW5hLmNvbS5jbi9pLzIwMDktMDYtMjUvMTkyNTMyMTIy
MTguc2h0bWwNCiAJCQkJDQotLS0tLS0tLS0tLS0tLQ0Kwbq+osvJDQoNCnNpbmG7pbavyefH+NTL
06rWp7PW1+kNCg0Ktee7sKO6MDEwo602MjY3NjgxMQ0KDQrK1rv6o7oxMzcxNzkwMzMxOQ0KDQrT
ys/ko7pqaW5zb25nQHN0YWZmLnNpbmEuY29tLmNuIA0KDQpNU046cm9zZWxpYW5nanNAaG90bWFp
bC5jb20NCg0KUVE6NjQyNzkyMjI0DQoyMDA5LTA2LTI1DQo=








PHP通过Socket来发送网页Post请求和Get请求Ok代码:

如果没有Host在里面[fwrite($sock, "Host: upload.com\r\n");],则会出现:
51 [Mon Jun 20 18:12:19 2011] [error] [client 172.25.38.70] client sent HTTP/1.1 request without hostname (see RFC2616
        section 14.23): /upload/upload.cgi


判断refer啥的,这块涉及到php的curl传refer,请参考:
伪造refer:  https://jackxiang.com/post/4022/
伪造Host绑定来源:https://jackxiang.com/post/4423/

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1057/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2022-7-31 01:06
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]