This article describes the implementation process of the Trojan file transfer method under Delphi in the form of examples. The specific steps are as follows:
Server side code:
unit ServerFrm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ExtCtrls,WinSock;type TfrmMain = class(TForm) Panel1: TPanel; Label1: TLabel; edtPort: TEdit; Panel2 : TPanel; stabar: TStatusBar; SaveDialog: TSaveDialog; btnListen: TButton; btnReceive: TButton; btnStop: TButton; btnExit: TButton; procedure FormCreate(Sender: TObject); procedure btnExitClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure btnListenClick( Sender: TObject); procedure btnReceiveClick(Sender: TObject); procedure btnStopClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans:Boolean; //Whether to stop the transmission switchInTrans:Boolean; //Indicates that the file is being received Server:TSocket; / /Define the socket handle on the server side//Customized process to receive files procedure RecvFile(FileName:String); end;var frmMain: TfrmMain;const BlockLen=1024*4;implementation{$R *.dfm}procedure tfrmmain.RecvFile(FileName:String);var Ftrans:file of Byte; Recelen:Integer; Blockbuf:array[0..BlockLen-1] of Byte; RecvSocket:TSocket; ra:Sockaddr_in; ra_len:integer;begin ra_len:=sizeof(ra); Recvsocket:=accept(server,@ra,@ra_len); assignFile(Ftrans,filename); rewrite(ftrans); stoptrans:=false; intrans:=true; recelen :=recv(recvsocket,Blockbuf,BlockLen,0); while (recelen>0) and (not StopTrans) do begin BlockWrite(Ftrans,Blockbuf[0],BlockLen); application.ProcessMessages; recelen:=recv(recvsocket,Blockbuf,Blocklen,0); if stoptrans then begin CloseFile(Ftrans); CloseSocket(RecvSocket); InTrans: =False; MessageBox(Handle,'Stop transmission!','Prompt',MB_OK); EXIT; END; END; //Close the file, received SOCKET CloseFile(Ftrans); Closesocket(recvsocket); InTrans:=False; if (Recelen=SOCKET_ERROR) then messagebox(handle,'Transmission terminated abnormally!','Prompt' ,MB_OK) ELSE MESSAGEBOX(HANDLE,'The client has closed connection 1, the file may have been transferred!','Prompt',MB_OK);end; procedure TfrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101 ,aWSAData)<>0 then raise Exception.Create('Cannot start WinSock dynamic link library'); messageBox(Handle,aWSAdata.szDescription,'WinSock dynamic link library version',mb_ok);end;procedure TfrmMain.btnExitClick(Sender: TObject);begin Close;end;procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction) ;begin if InTrans then if MessageBox(handle,'Receiving files, stop? ','Prompt',MB_YESNO)=IDNO then abort; IF SERVER<>INVALID_SOCKET THEN CLOSESOCKET(SERVER); //Release the resources created by the winsock dynamic link library if WSACleanup<>0 then messagebox(handle,'Clear the Winsock dynamic link Library error! ','prompt',MB_OK) ELSE messagebox(handle,'Clear Winsock dynamic link library successfully!','Prompt',MB_OK);end;procedure TfrmMain.btnListenClick(Sender: TObject);var ca:SOCKADDR_IN;begin //Create server-side SOCKET Server:=Socket( PF_INET,SOCK_STREAM,IPPROTO_IP); IF server=invalid_socket then begin stabar.SimpleText :='Create and receive SOCKET error 1'; exit; end; //Bind server-side SOCKET ca.sin_family :=PF_INET; CA.sin_port :=htons(strtoint(trim(edtPort.Text ))); ca.sin_addr. S_addr :=INADDR_ANY; if bind(server,ca,sizeof(ca))=socket_error then begin stabar.SimpleText :='Binding socket error, please change the receiving port'; closeSocket(server); exit; end else stabar.SimpleText :='Binding the receiving socket successfully!'; //Start listening listen(server,5); btnlisten .Enabled :=False; btnstop.Enabled :=true;end;procedure TfrmMain.btnReceiveClick(Sender: TObject);begin if (server=INVALID_SOCKET) THEN BEGIN MESSAGEBOX(HANDLE,'Not listening yet, please listen first!','Prompt',MB_OK); EXIT; END; IF SaveDialog.Execute THEN RECVFILE(SaveDialog.FileName); end;procedure TfrmMain.btnStopClick(Sender: TObject);begin STOPTRANS:=TRUE; IF SERVER<>INVALID_SOCKET THEN cLOSESOCKET(SERVER); //Server:=INVALID_SOCKET needs to be stated here; bTNSTOP.Enabled :=fALSE; BTNlISTEN.Enabled :=TRUE;end;end.
Client code:
unit ClientFrm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,WinSock;type TfrmMain = class(TForm) opendfile: TOpenDialog; Label1: TLabel; edtIP: TEdit; Label2: TLabel ; edtPort: TEdit; StatusBar: TStatusBar; btnConnect: TButton; btnSend: TButton; btnStop: TButton; btnExit: TButton; ProgressBar: TProgressBar; procedure FormCreate(Sender: TObject); procedure btnExitClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure btnConnectClick(Sender: TObject); procedure btnSendClick(Sender: TObject); procedure btnStopClick(Sender: TObject); private { Private declarations } Client:TSocket; public { Public declarations } StopTrans:Boolean; //Whether to stop sending developmentInTrans:Boolean; //Indicates whether the transmission is in progress File procedure TransFile(FileName:String); //The process of transferring files end; const BlockLen=1024*4; //The maximum amount of data sent each time var frmMain: TfrmMain;implementation{$R *.dfm}procedure TfrmMain.TransFile(FileName:String); //The process of transferring files var Ftrans:file of Byte; Flen:integer; BlockNum,RemainLen:integer; BlockBuf:array[0 ..BlockLen-1] of Byte; i:integer; SendLen:Integer;begin assignFile(Ftrans,filename); reset(Ftrans); Flen:=FileSize(Ftrans); BlockNum:=Flen div BlockLen; progressBar.Max :=1+BlockNum; RemainLen:=Flen mod BlockLen; StopTrans:=False; InTrans: =True; SendLen:=1; for i:=0 to BlockNum-1 do begin if (StopTrans) or (SendLen<=0) then Break; BlockRead(Ftrans,Blockbuf[0],BlockLen); SendLen:=Send(Client,Blockbuf,BlockLen,0); ProgressBar.Position :=i; Application.processMessages; end; if StopTrans then begin CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; MessageBox(Handle,'Stop transmission!','Prompt',mb_ok); progressbar.Position :=0; exit; end; if (SendLen<=0) then begin CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'Outgoing abnormal termination!','Prompt',MB_OK); progressBar.Position :=0; exit; end; if remainLen>0 then begin BlockRead(Ftrans,BlockBuf[0],RemainLen); SendLen:=send(client,BlockBuf,Remainlen,0); if (sendLen<=0) then begin closeFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'Transmission terminated abnormally!','Prompt',mb_ok); progressBar.Position :=0; exit; end; end; progressBar.Position :=ProgressBar.Max ; CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'Transfer completed!','Prompt',mb_ok); progressbar. Position :=0;end;procedure TfrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then raise Exception.Create('Cannot start WinSock dynamic link library'); messageBox(Handle,aWSAdata.szDescription,'WinSock dynamic link library version',mb_ok);end;procedure TfrmMain.btnExitClick (Sender: TObject);begin Close;end;procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);begin if InTrans then if MessageBox(handle,'Transmitting files, stop? ','Prompt',MB_YESNO)=IDNO then abort; //Release the resources created by winsock dynamic link library if WSACleanup<>0 then messagebox(handle,'Clear Winsock dynamic link library error!','Prompt',MB_OK) ELSE messagebox(handle,'Clear Winsock dynamic link library successfully!','Prompt',MB_OK); CloseSocket(Client);end;procedure TfrmMain.btnConnectClick(Sender: TObject);var ca:SOCKADDR_IN; hostaddr:u_long;begin Client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP); IF CLIENT=INVALID_SOCKET THEN BEGIN StatusBar.SimpleText :='Create COSKET for connecting to the remote server Error!'; exit; end; ca.sin_family :=PF_INET; CA.sin_port :=HTONS(STRTOINT(TRIM(EDTpORT.Text ))); HOSTADDR:=INET_ADDR(PCHAR(TRIM(EDTIP.Text ))); //Judge whether the IP is legal if (hostaddr= -1 ) then begin StatusBar.SimpleText :='Host IP address:'+trim(edtip.Text)+'Error'; exit; end else ca.sin_addr.S_addr :=hostaddr; //Connect to the server if connect(Client,ca,sizeof(ca))<>0 then begin StatusBar.SimpleText :='SOCKET error connecting to the server!'; exit; end else StatusBar. SimpleText :='Connect to remote SOCKET successfully!';end;procedure TfrmMain.btnSendClick(Sender: TObject);begin if (opendfile.Execute ) and (FileExists(opendfile.FileName )) then transFile(opendfile.FileName );end;procedure TfrmMain.btnStopClick(Sender: TObject);begin Stoptrans:=True;end;end.