2004. augusztus 30., hétfő

Remote port scanner


Problem/Question/Abstract:

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:

unit PortScanU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ScktComp;

type
  TMainForm = class(TForm)
    LblIPAddress: TLabel;
    IPAddressE: TEdit;
    lblScanRange: TLabel;
    MinPortE: TEdit;
    lblPorttoport: TLabel;
    MaxPortE: TEdit;
    StatusL: TLabel;
    ActivityLB: TListBox;
    StartBtn: TButton;
    WSsocket: TClientSocket;
    StopBtn: TButton;
    OpenOnlyCB: TCheckBox;
    procedure StartBtnClick(Sender: TObject);
    procedure WSsocketConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure WSsocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure StopBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    PortX, MaxPort: Integer;
    IsRunning: Boolean;
    procedure SetStuffOnOff(const St: Boolean);
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
implementation

{$R *.dfm}

procedure TMainForm.SetStuffOnOff(const St: Boolean);
begin
  IsRunning := St;
  StopBtn.Enabled := St;
  StartBtn.Enabled := not St;
  if not (St) then
  begin
    ActivityLB.Items.Add('Done Scanning ' + IPAddressE.text);
    StatusL.Caption := 'Status:'
  end
end;

procedure TMainForm.StartBtnClick(Sender: TObject);
begin
  ActivityLB.Items.Clear;
  PortX := StrToInt(MinPortE.text);
  MaxPort := StrToInt(MaxPortE.text);

  wsSocket.Address := IPAddressE.text;
  wsSocket.Port := PortX;
  wsSocket.Active := True;

  SetStuffOnOff(True);
  ActivityLB.Items.Add('Beginning scan: ' + IPAddressE.text)
end;

procedure TMainForm.WSsocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  //socket connection made
  //port must be open!
  ActivityLB.Items.Add('PORT: ' + inttostr(PortX) + '; OPEN!');

  //try next port...
  wsSocket.Active := False;
  PortX := PortX + 1;
  wsSocket.Port := PortX;
  StatusL.Caption := 'Scanning port:[' + IntToStr(PortX) + ']';

  if (IsRunning) then
    if (PortX > MaxPort) then
      SetStuffOnOff(False)
    else
      wsSocket.Active := True //test the new port
end;

procedure TMainForm.WSsocketError(Sender: TObject; Socket: TCustomWinSocket;
  ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  //connection failed....
  ErrorCode := 0; //handle the error
  if not (OpenOnlyCB.Checked) then
    ActivityLB.Items.Add('Port: ' + inttostr(PortX) + '; Closed.');

  //try next port
  wsSocket.Active := False; //close it
  PortX := PortX + 1; //new port to check
  wsSocket.Port := PortX; //put the port in the socket
  StatusL.Caption := 'Scanning port:[' + IntToStr(PortX) + ']';

  if (IsRunning) then
    if (PortX > MaxPort) then
      SetStuffOnOff(False)
    else
      wsSocket.Active := True //test the new port
end;

procedure TMainForm.StopBtnClick(Sender: TObject);
begin
  SetStuffOnOff(False);
  wssocket.Active := False;
  ActivityLB.Items.Add('Stoped scan; port ' + inttostr(PortX) + '!')
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  IsRunning := False
end;

end.

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'

Nincsenek megjegyzések:

Megjegyzés küldése