⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netconnection.pas

📁 VC++编写的类似Sniffer的网络数据包监听程序
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit NetConnection;

interface


uses
  Windows, SysUtils,Classes, WinSock,winsvc,Dialogs;
 type
   _SERVER_INFO_100=record
          sv101_platform_id:DWORD;
          sv101_name:LPWSTR;
   end;
  SERVER_INFO_100=_SERVER_INFO_100;
  PSERVER_INFO_100=^_SERVER_INFO_100;

   _SERVER_INFO_101=record
          sv101_platform_id:DWORD;
          sv101_name:LPWSTR;
          sv101_version_major:DWORD;
          sv101_version_minor:DWORD;
          sv101_type:DWORD;
          sv101_comment:LPWSTR;
   end;
  SERVER_INFO_101=_SERVER_INFO_101;
  PSERVER_INFO_101=^_SERVER_INFO_101;
//  LPSERVER_INFO_101=^_SERVER_INFO_101;

 type
    _NET_DISPLAY_USER =record
             name     :LPWSTR;
             comment  :LPWSTR;
             flags    :DWORD;
             full_name:LPWSTR;
             user_id  :DWORD;
             next_index:DWORD;
 end;
  NET_DISPLAY_USER=_NET_DISPLAY_USER;
  PNET_DISPLAY_USER=^_NET_DISPLAY_USER;

type _NET_DISPLAY_MACHINE=record
             name   :LPWSTR;
             comment:LPWSTR;
             flags  :DWORD;
             user_id:DWORD;
             next_index:DWORD;
end;
  NET_DISPLAY_MACHINE=_NET_DISPLAY_MACHINE;
  PNET_DISPLAY_MACHINE=^_NET_DISPLAY_MACHINE;

type _NET_DISPLAY_GROUP=record
             name      :LPWSTR;
             comment   :LPWSTR;
             user_id  :DWORD;
             flags:DWORD;
             next_index:DWORD;
end;
  NET_DISPLAY_GROUP=_NET_DISPLAY_GROUP;
  PNET_DISPLAY_GROUP=^_NET_DISPLAY_GROUP;


 type
   _SHARE_INFO1_NT=record
    netname    :LPWSTR;
    net_type   :DWORD;
    remark     :LPWSTR;
   end;

  SHARE_INFO1_NT = _SHARE_INFO1_NT;
  PSHARE_INFO1_NT = ^SHARE_INFO1_NT;
  LPBYTE = Pointer;
  NET_API_STATUS = DWORD;
  LPVOID = Pointer;

  type
    share_info1_9x=record
     shi1_netname     :array [0..21] of char;
     shi1_pad1        :char;
     shi1_type        :short;
     shi1_remark      :pchar;
  end;

  Tmy_WNetAddConnection2    = function(lpNetResource:PNetResourceA;
                                     lpPassword:     LPCTSTR;
                                     lpUsername:     LPCTSTR;
                                     dwFlags:        DWORD
                                     ): DWord; stdcall;

  Tmy_WNetCancelConnection2 = function(lpName:  LPCTSTR;
                                       dwFlags: DWORD;
                                       fForce:  BOOL
                                     ): DWord; stdcall;
  Tmy_NetQueryDisplayInformation = function(ServerName:  LPCWSTR;
                                                 Level:  DWORD;
                                                 Index:  DWORD;
                                      EntriesRequested:  DWORD;
                                PreferredMaximumLength:  DWORD;
                                var ReturnedEntryCount:  longint;
                                       SortedBuffer:  Pointer
                                     ): DWord; stdcall;

  TNetServerGetInfo      =function  (servername:LPWSTR;level:DWORD;
                                      VAR buf:PSERVER_INFO_101): DWord; stdcall;

{   TNetShareEnum_9x      =function( pszServer:pchar;
                                    sLevel:short;
                                    pbBuffer:pchar;
                                    cbBuffer:short;
                                    pcEntriesRead:short;
                                    pcTotalAvail:short
                                   ): DWord; stdcall;

 } TNetShareEnum_NT=function(pszServer:LPWSTR; sLevel:longint;
    var p:PSHARE_INFO1_NT;var cbBuffer: longint; var pcEntriesRead: longint; var pcTotalAvail:longint;var hEnum:HWND): NET_API_STATUS; stdcall;

  TIConnectionException = class(Exception);
 type
  TIConnectionReply  = procedure(Sender: TObject; Error : Integer) of object;
  TIConnection = class(TObject)
  private
    hmprdll :        HModule;                    // Handle for mpr.dll
    ShareEnum_dll :  HModule;                    // Handle for mpr.dll
    my_WNetAddConnection2    : Tmy_WNetAddConnection2;
    my_WNetCancelConnection2 : Tmy_WNetCancelConnection2;
    my_NetQueryDisplayInformation:Tmy_NetQueryDisplayInformation;
    NetShareEnum_NT : TNetShareEnum_NT;
    FAddress,fsharename:        String;                     // Address given
    Fusername,Fpassword :    String;
    FosVersion          :    integer;
    FOnEchoClose :  TIConnectionReply;
    FOnEchoOpen :   TIConnectionReply;
    FOnEchoShareEnum :  TIConnectionReply;
    FOnOpenSucceed : TNotifyEvent;
    FNetServerGetInfo      : TNetServerGetInfo;//用于取服务器信息
  public
    list:tstringlist;
    list_info:tstringlist;
    AutoDo:boolean;
    FGetInfo:boolean;
    constructor Create; virtual;
    destructor  Destroy; override;
    procedure   SetAddress(Value : String);
    function    getserverinfo:Integer;
    function    open : Integer;
    function    close : Integer;
    function    ShareEnum : Integer;
    function    NetQueryDisplayInformation(Level:integer;var var_list:Tstringlist):integer;
    function    get_user(Level:integer;var var_list:Tstringlist):integer;
    function    my_EnumServices(var var_list:Tstringlist):integer;

    property Address       : String         read  FAddress   write SetAddress;
    property ShareName       : String       read  Fsharename   write fsharename;
    property UserName       : String         read  Fusername   write Fusername;
    property PassWord       : String         read  Fpassword   write Fpassword;

    property OnEchoClose : TIConnectionReply   read  FOnEchoClose  write FOnEchoClose;
    property OnEchoOpen  : TIConnectionReply   read  FOnEchoOpen   write FOnEchoOpen;
    property OnShareEnum : TIConnectionReply   read  FOnEchoShareEnum   write FOnEchoShareEnum;
    property OnOpenSucceed : TNotifyEvent   read  FOnOpenSucceed   write FOnOpenSucceed;

  end;

//  function NetShareEnum_NT;external 'netapi32.dll' name 'NetShareEnum';

implementation
   const
       SV_TYPE_WORKSTATION     =   $00000001;
       SV_TYPE_SERVER          =   $00000002;
       SV_TYPE_SQLSERVER       =   $00000004;
       SV_TYPE_DOMAIN_CTRL     =   $00000008;
       SV_TYPE_DOMAIN_BAKCTRL  =   $00000010;
       SV_TYPE_TIME_SOURCE     =   $00000020;
       SV_TYPE_AFP             =   $00000040;
       SV_TYPE_NOVELL          =   $00000080;
       SV_TYPE_DOMAIN_MEMBER   =   $00000100;
       SV_TYPE_PRINTQ_SERVER   =   $00000200;
       SV_TYPE_DIALIN_SERVER   =   $00000400;
       SV_TYPE_XENIX_SERVER    =   $00000800;
       SV_TYPE_SERVER_UNIX     =   SV_TYPE_XENIX_SERVER;
       SV_TYPE_NT              =   $00001000;
       SV_TYPE_WFW             =   $00002000;
       SV_TYPE_SERVER_MFPN     =   $00004000;
       SV_TYPE_SERVER_NT       =   $00008000;
       SV_TYPE_POTENTIAL_BROWSER  =   $00010000;
       SV_TYPE_BACKUP_BROWSER  =   $00020000;
       SV_TYPE_MASTER_BROWSER  =   $00040000;
       SV_TYPE_DOMAIN_MASTER   =   $00080000;
       SV_TYPE_SERVER_OSF      =   $00100000;
       SV_TYPE_SERVER_VMS      =   $00200000;
       SV_TYPE_WINDOWS         =   $00400000;  ///* Windows95 and above */
       SV_TYPE_DFS             =   $00800000;  ///* Root of a DFS tree */
       SV_TYPE_CLUSTER_NT      =   $01000000;  ///* NT Cluster */
       SV_TYPE_TERMINALSERVER  =   $02000000;  ///* Terminal Server(Hydra) */
       SV_TYPE_DCE             =   $10000000;  ///* IBM DSS (Directory and Security Services) or equivalent */
       SV_TYPE_ALTERNATE_XPORT =   $20000000;  ///* return list for alternate transport */
       SV_TYPE_LOCAL_LIST_ONLY =   $40000000;  ///* Return local list only */
       SV_TYPE_DOMAIN_ENUM     =   $80000000;

constructor TIConnection.Create;
var
//    WSAData: TWSAData;
   OSVer:TOSVersionInfo;//版本信息
begin
    fpassword:='';
    fusername:='';
    fsharename:='ipc$';
    AutoDo:=true;
    FGetInfo:=false;
    // initialise winsock
//    if WSAStartup($101, WSAData) <> 0 then
//        raise TIConnectionException.Create('Error initialising Winsock');

//先获取系统版本,再决定装载函数
    ZeroMemory(@OSVer, sizeof(TOSVersionInfoA));
    OSVer.dwOSVersionInfoSize:= sizeof(TOSVersionInfoA);
    GetVersionEx(OSVer); FosVersion:=integer(OSVer.dwPlatformId);
    list:=tstringlist.Create;//建立字符串列表
    list_info:=tstringlist.Create;//建立字符串列表

    if FosVersion=VER_PLATFORM_WIN32_NT then
      begin
        ShareEnum_dll:=LoadLibrary('netapi32.dll');
        if ShareEnum_dll = 0 then
           raise TIConnectionException.Create('Unable to register netapi32.dll');
      end else
      begin
        ShareEnum_dll:=LoadLibrary('svrapi.dll');
        if ShareEnum_dll = 0 then
           raise TIConnectionException.Create('Unable to register svrapi.dll');
      end;
    hmprdll := LoadLibrary('mpr.dll');
    if hmprdll = 0 then
        raise TIConnectionException.Create('Unable to register mpr.dll');


    @my_WNetAddConnection2  := GetProcAddress(hmprdll, 'WNetAddConnection2A');
    @my_WNetCancelConnection2 := GetProcAddress(hmprdll, 'WNetCancelConnection2A');
//  if FosVersion=VER_PLATFORM_WIN32_NT then//win9x不支持
    @my_NetQueryDisplayInformation:=GetProcAddress(ShareEnum_dll, 'NetQueryDisplayInformation');
    @NetShareEnum_NT := GetProcAddress(ShareEnum_dll, 'NetShareEnum');
    @FNetServerGetInfo := GetProcAddress(ShareEnum_dll, 'NetServerGetInfo');

    if (@my_WNetAddConnection2 =nil) or (@FNetServerGetInfo =nil) or (@my_NetQueryDisplayInformation =nil) or
       (@my_WNetCancelConnection2 =nil) or (@NetShareEnum_NT =nil) then
          raise TIConnectionException.Create('加载dll函数错误!');
//  if (FosVersion=VER_PLATFORM_WIN32_NT) and (@my_NetQueryDisplayInformation =nil) then
//          raise TIConnectionException.Create('加载NetQueryDisplayInformation函数错误!');

end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
destructor TIConnection.Destroy;
begin
    if hmprdll <> 0 then
        FreeLibrary(hmprdll);
    if ShareEnum_dll <> 0 then
        FreeLibrary(ShareEnum_dll);
    list.Free;list_info.Free;
    inherited Destroy;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TIConnection.SetAddress(Value : String);
begin
    // Only change if needed (could take a long time)
    if FAddress = Value then Exit;
    FAddress:= Value;
end;

function TIConnection.getserverinfo:Integer;
var
    pBuf:PSERVER_INFO_101;
    pBuf_100:PSERVER_INFO_100;
    str:string;
    res:Dword;
    ServerName:pwchar;
begin
  result:=0;
  list_info.Clear;
  list_info.add(FAddress);

    if pos('\\',FAddress)=1 then str:=FAddress
    else str:='\\'+FAddress;

    ServerName:=StringToOleStr(str);

    res:=FNetServerGetInfo(ServerName,101,pBuf);

    if (res =0) then
      begin
       if (pBuf.sv101_type and SV_TYPE_DOMAIN_CTRL) =SV_TYPE_DOMAIN_CTRL then str:='PDC'
	  else
       if (pBuf.sv101_type and SV_TYPE_DOMAIN_BAKCTRL)=SV_TYPE_DOMAIN_BAKCTRL then str:='BDC'
	  else
       if (pBuf.sv101_type and SV_TYPE_SERVER_NT)=SV_TYPE_SERVER_NT then str:='NT4.0 / WINDOWS2000'
	  else
       if (pBuf.sv101_type and SV_TYPE_NT)=SV_TYPE_NT then str:='NT WORKSTATION'
	  else
       if (pBuf.sv101_type and SV_TYPE_SQLSERVER)=SV_TYPE_SQLSERVER then str:='SQL_server'
	  else
       if (pBuf.sv101_type and SV_TYPE_NOVELL)=SV_TYPE_NOVELL then str:='Novell Netware Server'
	  else
       if (pBuf.sv101_type and SV_TYPE_WFW)=SV_TYPE_WFW then str:='WINDOWS ME/Windows for Workgroups'
	  else
       if (pBuf.sv101_type and SV_TYPE_WINDOWS)=SV_TYPE_WINDOWS then str:='Windows 9x'
	  else
            str:=inttostr(pBuf.sv101_type);

       list_info.Add('名称:'+pBuf.sv101_name);
       list_info.Add('系统:'+str);
       list_info.add('platform:'+inttostr(pBuf.sv101_platform_id));
       list_info.add('版本:'+format('%d - %d',[ pBuf.sv101_version_major, pBuf.sv101_version_minor]));       list_info.add('类型:'+format('%d',[pBuf.sv101_type]));       list_info.add('备注:'+pBuf.sv101_comment);    end else    begin     res:=FNetServerGetInfo(ServerName,100,PSERVER_INFO_101(pBuf_100));
     if (res =0) then
      begin
       list_info.Add('名称:'+pBuf_100.sv101_name);
       list_info.add('platform:'+inttostr(pBuf_100.sv101_platform_id));
      end else result:=-1;
    end;end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -