Mega Code Archive

 
Categories / Delphi / Examples
 

Remote port scanner

Title: Remote port scanner Question: ever needed to test open ports on your machine? Answer: you can write a small utility for this purpose in Delphi, using sockects... here's my approach use this code under you own risk, I present this article for educational purposes only, I take no responsability for the use of it I'll put a link to the whole demo, here's the unit, I'm sure you can recreate the form and run this: ------- beggining of unit ----- Unit PortScanU; Interface Uses &nbsp&nbspWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, &nbsp&nbspDialogs, StdCtrls, ScktComp; Type &nbsp&nbspTMainForm = Class(TForm) &nbsp&nbsp&nbsp&nbspLblIPAddress: TLabel; &nbsp&nbsp&nbsp&nbspIPAddressE: TEdit; &nbsp&nbsp&nbsp&nbsplblScanRange: TLabel; &nbsp&nbsp&nbsp&nbspMinPortE: TEdit; &nbsp&nbsp&nbsp&nbsplblPorttoport: TLabel; &nbsp&nbsp&nbsp&nbspMaxPortE: TEdit; &nbsp&nbsp&nbsp&nbspStatusL: TLabel; &nbsp&nbsp&nbsp&nbspActivityLB: TListBox; &nbsp&nbsp&nbsp&nbspStartBtn: TButton; &nbsp&nbsp&nbsp&nbspWSsocket: TClientSocket; &nbsp&nbsp&nbsp&nbspStopBtn: TButton; &nbsp&nbsp&nbsp&nbspOpenOnlyCB: TCheckBox; &nbsp&nbsp&nbsp&nbspProcedure StartBtnClick(Sender: TObject); &nbsp&nbsp&nbsp&nbspProcedure WSsocketConnect(Sender: TObject; Socket: TCustomWinSocket); &nbsp&nbsp&nbsp&nbspProcedure WSsocketError(Sender: TObject; Socket: TCustomWinSocket; &nbsp&nbsp&nbsp&nbsp&nbsp&nbspErrorEvent: TErrorEvent; Var ErrorCode: Integer); &nbsp&nbsp&nbsp&nbspProcedure StopBtnClick(Sender: TObject); &nbsp&nbsp&nbsp&nbspProcedure FormCreate(Sender: TObject); &nbsp&nbspPrivate &nbsp&nbsp&nbsp&nbsp{ Private declarations } &nbsp&nbsp&nbsp&nbspPortX, MaxPort:Integer; &nbsp&nbsp&nbsp&nbspIsRunning:Boolean; &nbsp&nbsp&nbsp&nbspProcedure SetStuffOnOff(Const St:Boolean); &nbsp&nbspPublic &nbsp&nbsp&nbsp&nbsp{ Public declarations } &nbsp&nbspEnd; Var &nbsp&nbspMainForm: TMainForm; Implementation {$R *.dfm} Procedure TMainForm.SetStuffOnOff(Const St:Boolean); Begin &nbsp&nbspIsRunning:=St; &nbsp&nbspStopBtn.Enabled:=St; &nbsp&nbspStartBtn.Enabled:=Not St; &nbsp&nbspIf Not (St) Then &nbsp&nbspBegin &nbsp&nbsp&nbsp&nbspActivityLB.Items.Add('Done Scanning ' + IPAddressE.text); &nbsp&nbsp&nbsp&nbspStatusL.Caption:='Status:' &nbsp&nbspEnd End; Procedure TMainForm.StartBtnClick(Sender: TObject); Begin &nbsp&nbspActivityLB.Items.Clear; &nbsp&nbspPortX := StrToInt(MinPortE.text); &nbsp&nbspMaxPort := StrToInt(MaxPortE.text); &nbsp&nbspwsSocket.Address := IPAddressE.text; &nbsp&nbspwsSocket.Port := PortX; &nbsp&nbspwsSocket.Active := True; &nbsp&nbspSetStuffOnOff(True); &nbsp&nbspActivityLB.Items.Add('Beginning scan: ' + IPAddressE.text) End; Procedure TMainForm.WSsocketConnect(Sender: TObject; &nbsp&nbspSocket: TCustomWinSocket); Begin &nbsp&nbsp//socket connection made &nbsp&nbsp//port must be open! &nbsp&nbspActivityLB.Items.Add('PORT: ' + inttostr(PortX) + '; OPEN!'); &nbsp&nbsp//try next port... &nbsp&nbspwsSocket.Active := False; &nbsp&nbspPortX := PortX + 1; &nbsp&nbspwsSocket.Port := PortX; &nbsp&nbspStatusL.Caption:='Scanning port:['+IntToStr(PortX)+']'; &nbsp&nbspIf (IsRunning) Then &nbsp&nbsp&nbsp&nbspIf (PortX MaxPort) Then &nbsp&nbsp&nbsp&nbsp&nbsp&nbspSetStuffOnOff(False) &nbsp&nbsp&nbsp&nbspElse &nbsp&nbsp&nbsp&nbsp&nbsp&nbspwsSocket.Active := True //test the new port End; Procedure TMainForm.WSsocketError(Sender: TObject; Socket: TCustomWinSocket; &nbsp&nbspErrorEvent: TErrorEvent; Var ErrorCode: Integer); Begin &nbsp&nbsp//connection failed.... &nbsp&nbspErrorCode:=0; //handle the error &nbsp&nbspIf Not (OpenOnlyCB.Checked) Then &nbsp&nbsp&nbsp&nbspActivityLB.Items.Add('Port: ' + inttostr(PortX) + '; Closed.'); &nbsp&nbsp//try next port &nbsp&nbspwsSocket.Active := False; //close it &nbsp&nbspPortX := PortX + 1; //new port to check &nbsp&nbspwsSocket.Port := PortX; //put the port in the socket &nbsp&nbspStatusL.Caption:='Scanning port:['+IntToStr(PortX)+']'; &nbsp&nbspIf (IsRunning) Then &nbsp&nbsp&nbsp&nbspIf (PortX MaxPort) Then &nbsp&nbsp&nbsp&nbsp&nbsp&nbspSetStuffOnOff(False) &nbsp&nbsp&nbsp&nbspElse &nbsp&nbsp&nbsp&nbsp&nbsp&nbspwsSocket.Active := True //test the new port End; Procedure TMainForm.StopBtnClick(Sender: TObject); Begin &nbsp&nbspSetStuffOnOff(False); &nbsp&nbspwssocket.Active := False; &nbsp&nbspActivityLB.Items.Add('Stoped scan; port ' + inttostr(PortX) + '!') End; Procedure TMainForm.FormCreate(Sender: TObject); Begin &nbsp&nbspIsRunning := False End; End. --------- end of unit --------- as you can see, the idea is pretty easy... - set the address - set the port - try to activate the socket - and check if it went good - next port, repeat steps note:to test ports on your local machine you need to set IPAddressE.Text:='localhost' let me know of any bugs oh, by the way, the source code of the article was formatted using my: PAS 2 HTML converter ...keep up coding EberSys