Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (6)


Remote port scannerFormat this article printer-friendly!Bookmark function is only available for registered users!
using sockets
Product:
Delphi all versions
Category:
Security
Skill Level:
Scoring:
Last Update:
04/02/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet port scan scanner socket hack port-scan port-scanner
Times Scored:
4
Visits:
10793
Uploader: Eber Irigoyen
Company: BTXSys
Reference: N/A
 
Question/Problem/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:

------- beggining of unit -----
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.
--------- 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





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
having some problems with this
    Mike (May 20 2005 5:38PM)

I have tried this program and it works fine if I use a single IP address and certain ports to check, but if I have a list of ip addresses to check it doesn't like it and I get exception errors. I have tried using sleep(value) and application.processmessage, but this hasnot worked. If I put a showmessage in the middle of loop this works to a degree if i don't click on 'ok' quickly.

does anyone have any idea on how to sort this.
Respond

Where is the source code?
    Johannes Lindthaler (Oct 7 2004 2:51PM)

It would be helpful if the source code could be downloaded instead of spending time reconstructing the form
Respond

DFM Code please
    Stewart Moss (Sep 28 2004 12:57PM)

Please can you post the DFM code :)

Thanks
Respond

Nice Code, Pas2HTML Broken
    Ken Wilcox (Apr 12 2002 5:44PM)

Your Source has a lot of &n b s p but the right code should be &n b s p ; so it shows up properly formated.

I had to add the spaces between the command since this lets me use html codes in the comments
Respond

RE: Nice Code, Pas2HTML Broken
Eber Irigoyen (May 8 2002 11:04PM)

I fixed the source code of that article to include " " instead of " ", thanks for the advice

salu2

EberSys
Respond

RE: RE: Nice Code, Pas2HTML Broken
Eber Irigoyen (May 8 2002 11:06PM)

peace of... ok... I mean
include   instead of &nbsp
[& n b s p ;] instead of [& n b s p]
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Wischnewski
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)