• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Scan for MSSQL servers

Hi,
     How can one scan for MSSQL servers running on the current network? And return this list (preferably hostname's and version) to a stringlist?

Thanks
0
Narusegawa
Asked:
Narusegawa
1 Solution
 
DragonSlayerCommented:
You can actually try doing a telnet to every IP in the subnet at the default MSSQL port.

However, 2 considerations
1. Some administrators set their MSSQL to not use the default port
2. Your system might be mistaken as a virus/exploit
0
 
soapsiamCommented:
There is a program called SQL ping on Internet. The following is the program ported to Delphi by me. It is not a good design program just an example.

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, NMUDP, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient;

type
  TForm1 = class(TForm)
    NMUDP1: TNMUDP;
    Button1: TButton;
    Memo1: TMemo;
    IdUDPClient1: TIdUDPClient;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
      FromIP: String; Port: Integer);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Buff : array[0..0] of char;
begin

    Buff[0] := #2;
    NMUdp1.SendBuffer(Buff,1);
end;

procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
  NumberBytes: Integer; FromIP: String; Port: Integer);
var
    Buff : array[0..65535] of char;
    i, Len, cnt : integer;
    s : string;
begin
    s := IntToStr(NumberBytes)+' bytes received from IP '+FromIP;
    NMUdp1.ReadBuffer (Buff,Len);
    s := s + #13#10; cnt := 0;
    for i := 3 to Len-1 do
    begin
        if ((Buff[i] =';') and (Buff[i+1]<> ';')) then
        begin
            if cnt mod 2 = 0 then
            begin
                s := s + ':';
                cnt := cnt + 1;
            end
            else
            begin
                s := s+#13#10;
                cnt := cnt + 1;
            end
        end
        else
        begin
            if Buff[i] <> ';' then
                s := s + char(Buff[i])
            else
                s := s + #13#10;
        end;
        //showmessage(Buff[i]+' , '+ S);
    end;
    Memo1.Lines.Add (s);
end;

procedure TForm1.Button2Click(Sender: TObject);
var Buff : array[0..0] of char;
    RxBuff : array [0..117] of char;
    ReceivedString,s : String;
    i, cnt : integer;
begin
    Buff[0] := #2;
    IdUDPClient1.SendBuffer (Buff,1);
    //ReceivedString := idUDPClient1.ReceiveString();
    idUDPClient1.ReceiveBuffer (RxBuff,117);
    s := s + #13#10; cnt := 0;
    for i := 3 to 116 do
    begin
        if ((RxBuff[i] =';') and (RxBuff[i+1]<> ';')) then
        begin
            if cnt mod 2 = 0 then
            begin
                s := s + ':';
                cnt := cnt + 1;
            end
            else
            begin
                s := s+#13#10;
                cnt := cnt + 1;
            end
        end
        else
        begin
            if RxBuff[i] <> ';' then
                s := s + char(RxBuff[i])
            else
                s := s + #13#10;
        end;
        //showmessage(Buff[i]+' , '+ S);
    end;
    Memo1.Lines.Add (s);
end;

end.

object Form1: TForm1
  Left = 341
  Top = 191
  Width = 544
  Height = 376
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 176
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 96
    Top = 88
    Width = 337
    Height = 177
    TabOrder = 1
  end
  object Button2: TButton
    Left = 280
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
  object NMUDP1: TNMUDP
    RemoteHost = 'jtsnet1'
    RemotePort = 1434
    LocalPort = 0
    ReportLevel = 1
    OnDataReceived = NMUDP1DataReceived
    Left = 72
    Top = 32
  end
  object IdUDPClient1: TIdUDPClient
    Active = True
    Host = '10.20.0.22'
    Port = 1434
    ReceiveTimeout = 250
    Left = 40
    Top = 104
  end
end

You can use this technique by first check on default port. If not found then check on another port.


SoapSiam

 
0
 
DoronlCommented:
Use NetServerEnum with SV_TYPE_SQLSERVER.

Example:

unit Unit1;

interface

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

type
  PServerInfo101 = ^TServerInfo101;
  TServerInfo101 = packed record
    sv101_Platform_ID   : DWord;
    sv101_Name          : PWideChar;
    sv101_Version_Major : DWord;
    sv101_Version_Minor : DWord;
    sv101_Type          : DWord;
    sv101_Comment       : PWideChar;
  end;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

const
  MAX_PREFERRED_LENGTH = DWord(-1);
  SV_TYPE_SQLSERVER           = $00000004;

  function NetServerEnum(ServerName: PWideChar;
                         level: DWord;
                         var Buffer: Pointer;
                         PrefMaxLen: DWord;
                         var EntriesRead,
                             TotalEntries: DWord;
                         ServerType: DWord;
                         Domain: PWideChar;
                         var resume_handle: DWord): LongWord; stdcall;
external 'netapi32.dll';

  function NetApiBufferFree(Buffer: Pointer): LongWord; stdcall;
external 'netapi32.dll';


implementation

{$R *.DFM}


procedure ListServers(aStringGrid: TStringGrid; aServer, aDomain:
string);
var
  EntriesRead  : DWord;
  I            : Integer;
  ResumeHandle : DWord;
  Rslt         : LongWord;
  SessionInfo,
  P            : PServerInfo101;
  TotalEntries : DWord;
  TotalSoFar   : LongWord;
  CName,
  CDomain      : PWideChar;
  aString      : string;
  intIPAdres   : integer;
  strIPAdres   : string;
begin
  ResumeHandle := 0;
  TotalSoFar   := 0;

  aString := aServer;
  CName   := StringToOleStr(aString);
  aString := aDomain;
  CDomain := StringToOleStr(aString);
  repeat
    Rslt := NetServerEnum(CName,
                          101,
                          Pointer(SessionInfo),
                          MAX_PREFERRED_LENGTH,
                          EntriesRead,
                          TotalEntries,
                          SV_TYPE_SQLSERVER,
                          NIL, //CDomain,
                          ResumeHandle);
    if Rslt <> 0 then
      begin
        showmessage('Fout : ' + IntToStr(Rslt) + ' meaning "' +
SysErrorMessage(Rslt) + '"');
      end;

    P := SessionInfo;
    for I := 0 to EntriesRead - 1 do
      begin
        if WideCharToString(P.sv101_Name) <> '' then
          begin
            aStringGrid.Cells[0, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Platform_ID);
            aStringGrid.Cells[1, aStringGrid.RowCount - 1] :=
WideCharToString(P.sv101_Name);
            aStringGrid.Cells[2, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Version_Major);
            aStringGrid.Cells[3, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Version_Minor);
            aStringGrid.Cells[4, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Type);
            aStringGrid.Cells[5, aStringGrid.RowCount - 1] :=
WideCharToString(P.sv101_Comment);

            aStringGrid.RowCount := aStringGrid.RowCount + 1;
          end;
        Inc(LongWord(P), SizeOf(TServerInfo101))
      end;
    Inc(TotalSoFar, EntriesRead);
  until TotalSoFar >= TotalEntries;

  Rslt := NetApiBufferFree(SessionInfo);
  if Rslt <> 0 then
    showmessage('Fout : ' + IntToStr(Rslt) + ' meaning "' +
SysErrorMessage(Rslt) + '"');
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ListServers(StringGrid1, '\\192.10.25.254', '');
end;

end.
0
 
NarusegawaAuthor Commented:
ListServers(StringGrid1, '\\192.10.25.254', '');

How would one use that function for the current domain? Not knowing the current domain name, as this could be used on different domains and ranges of ip. Thanks
0
 
NarusegawaAuthor Commented:
Nevermind, leaving the 2nd 2 parameters blank works.

Thanks ever so much for this. I've modified it slightly and added it to the onDropDown event of a ComboBox. Works a treat.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now