Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Netstat Programme zu Ports ermitteln (https://www.delphipraxis.net/204979-netstat-programme-zu-ports-ermitteln.html)

zeras 20. Jul 2020 12:59

Netstat Programme zu Ports ermitteln
 
Ich habe vorhin in der DP gelesen, dass man mittels netstat die Ports ermitteln kann, die angesprochen werden.
Dann habe ich das Programm mittels netstat -an gestartet. Da bin ich fast vom Hocker gefallen. Ca. 120 Einträge mit "Abhören", "Hergestellt", "Wartend" und "Schliessen_Warten" bei TCP, dann noch ca. 40 Einträge mit UDP.
Ist das nicht ein wenig zuviel?
Wie kann ich die Ports Programmen zuordnen?
Ich möchte hier ein wenig "aufräumen", wenn es geht.

TiGü 20. Jul 2020 13:20

AW: Netstat Programme zu Ports ermitteln
 
Runterladen, starten, staunen:
https://docs.microsoft.com/en-us/sys...nloads/tcpview

Dalai 20. Jul 2020 14:04

AW: Netstat Programme zu Ports ermitteln
 
Es gibt auch NirSoft CurrPorts.

Grüße
Dalai

Michael II 20. Jul 2020 15:03

AW: Netstat Programme zu Ports ermitteln
 
oder wenn du's einfach magst:
netstat -b
zeigt dir das jeweils beteiligte Programm an.

netstat ? zeigt dir alle Optionen.

Der Windows Ressoucenmonitor (> Netzwerk) genügt auch oft ;-).

himitsu 20. Jul 2020 15:21

AW: Netstat Programme zu Ports ermitteln
 
Viele programme nutzen auch lokal TCP/IP-Verbindungen und die sind dann doppelt in der Liste,
also einmal der Server (der den Port geöffnet hat) und der Client (welcher sich verbunden hat)

Anhand dem Wort Abhören/Verbunden/... kann man im netstat erkennen ob das Links ein Server oder Client ist.

Und nee, sooo viel ist das nicht.

Teils mehrere Verbindungen von einem Programm,
dann die Doppelten der lokalen Verbindungen (z.B. TrayIcon, GUI und der Dienst oder z.B. ein Admin-Prozess mit höheren Rechten)
dann Windows-Update, Zeitabfrage, Browser, Backup (bei mit die Verbindung zum NAS) usw.
und für durtzende Programme jeweils ein eigenes schwachsinniges Update-Tool, welches nutzlos dauerhaft aktiv ist.

Rollo62 20. Jul 2020 16:11

AW: Netstat Programme zu Ports ermitteln
 
Nur mal Interessehalber:
TcpView ist von 2011, NirSoft von 2020.

Wird SysInternals eigentlich noch gepflegt ?

himitsu 20. Jul 2020 16:21

AW: Netstat Programme zu Ports ermitteln
 
Ja, es kommen ab und an Updates, aber es sind wohl nur noch Bugfixe/Funktionsanpassungen aber keine Neuerungen dabei ... kommt mir jedenfalls so vor.
Aber zumindestens laufen alle aktuellen Programme noch problemlos.

zeras 20. Jul 2020 17:58

AW: Netstat Programme zu Ports ermitteln
 
Danke für eure Infos, speziell auch die Info, dass es wahrscheinlich nicht zu viele Verbindungen sind.
Ich werde in den nächsten Tagen nochmals prüfen.

QuickAndDirty 21. Jul 2020 08:11

AW: Netstat Programme zu Ports ermitteln
 
Ich lege die netstat ausgabe immer in einer Datei ab
Code:
netstat -b > c:\Meinprogramm\LogsOrdner\Netstat.txt
oder
Code:
netstat -ano > c:\Meinprogramm\LogsOrdner\Netstat.txt
Datein kann man mit Notepad und Strg+f durchsuchen.

Und ein Copy-Pasta um das ganze per programm zu ermitteln, falls du nach einem Freien Port suchst.
Delphi-Quellcode:
{$WARN UNSAFE_TYPE off}
{$WARN UNSAFE_CAST off}
{$WARN UNSAFE_CODE off}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_LIBRARY OFF}
{$WARN SYMBOL_DEPRECATED OFF}

// must turn off range checking or various records declared array [0..0] die !!!!!
{$R-}
{$Q-}
interface
uses Windows, Messages, SysUtils, Classes, Dialogs, controls, Psapi,
     Winsock, TypInfo;

type
TConnInfo = record
  State: Integer;
  LocalAddr: String;
  LocalPort: Integer;
  RemoteAddr: String;
  RemotePort: Integer;
  ProcessID: DWORD;
  LocalHost: string;
  RemoteHost: string;
  DispRow: integer;
  ProcName: WideString;
  CreateDT: TDateTime;
end;

TConnRows = array of TConnInfo;

implementation

const
  ANY_SIZE = 1;
  TCPIP_OWNING_MODULE_SIZE = 16;
  TCPConnState:array[0..12] of string =
    ('', 'closed', 'listening', 'syn_sent',
    'syn_rcvd', 'established', 'fin_wait1',
    'fin_wait2', 'close_wait', 'closing',
    'last_ack', 'time_wait', 'delete_tcb'
    );
   
type
PTMibTCPRow = ^TMibTCPRow;
TMibTCPRow = packed record
  dwState: DWORD;
  dwLocalAddr: DWORD;
  dwLocalPort: DWORD;
  dwRemoteAddr: DWORD;
  dwRemotePort: DWORD;
end;

PTMibTCPTable = ^TMibTCPTable;
TMibTCPTable = packed record
  dwNumEntries: DWORD;
  Table: array[0..0] of TMibTCPRow;
end;


PTMibTCPRowEx = ^TMibTCPRowEx;
TMibTCPRowEx = packed record
  dwState: DWord;
  dwLocalAddr: DWord;
  dwLocalPort: DWord;
  dwRemoteAddr: DWord;
  dwRemotePort: DWord;
  dwProcessID: DWord;
end;

PTMibTCPTableEx = ^TMibTCPTableEx;
TMibTCPTableEx = packed record
  dwNumEntries: Integer;
  Table: array [0..0] of TMibTCPRowEx;
end;

_MIB_TCPROW_OWNER_MODULE = record
  dwState: DWORD;
  dwLocalAddr: DWORD;
  dwLocalPort: DWORD;
  dwRemoteAddr: DWORD;
  dwRemotePort: DWORD;
  dwOwningPid: DWORD;
  liCreateTimestamp: TFileTime; {LARGE_INTEGER}
  OwningModuleInfo: Array[0..TCPIP_OWNING_MODULE_SIZE-1] of int64;
end;
TMibTcpRowOwnerModule = _MIB_TCPROW_OWNER_MODULE;
PTMibTcpRowOwnerModule = ^_MIB_TCPROW_OWNER_MODULE;

_MIB_TCPTABLE_OWNER_MODULE = record
  dwNumEntries: DWORD;
  table: Array[0..ANY_SIZE-1] of TMibTcpRowOwnerModule;
end;
TMibTcpTableOwnerModule = _MIB_TCPTABLE_OWNER_MODULE;
PTMibTcpTableOwnerModule = ^_MIB_TCPTABLE_OWNER_MODULE;

_TCPIP_OWNER_MODULE_BASIC_INFO = record
  pModuleName: PWCHAR;
  pModulePath: PWCHAR;
end;
TTcpIpOwnerModuleBasicInfo = _TCPIP_OWNER_MODULE_BASIC_INFO;
PTcpIpOwnerModuleBasicInfo = ^_TCPIP_OWNER_MODULE_BASIC_INFO;

TTcpIpOwnerModuleBasicInfoEx = record
  TcpIpOwnerModuleBasicInfo: TTcpIpOwnerModuleBasicInfo ;
  Buffer: Array[0..1024] of byte;
end;

TTcpTableClass = (
TCP_TABLE_BASIC_LISTENER,
TCP_TABLE_BASIC_CONNECTIONS,
TCP_TABLE_BASIC_ALL,
TCP_TABLE_OWNER_PID_LISTENER,
TCP_TABLE_OWNER_PID_CONNECTIONS,
TCP_TABLE_OWNER_PID_ALL,
TCP_TABLE_OWNER_MODULE_LISTENER,
TCP_TABLE_OWNER_MODULE_CONNECTIONS,
TCP_TABLE_OWNER_MODULE_ALL) ;

TTcpIpOwnerModuleInfoClass = (
  TcpIpOwnerModuleInfoClassBasic );

Var
GetExtendedTcpTable : function ( pTCPTable: Pointer; pDWSize: PDWORD;
    bOrder: BOOL; ulAf: LongWord; TableClass: TTcpTableClass; Reserved: LongWord): DWORD; stdcall;
AllocateAndGetTcpExTableFromStack: procedure (var pTCPTableEx: PTMibTCPTableEx;
        bOrder: Bool; Heap: THandle; Zero, Flags: DWORD); stdcall;
GetOwnerModuleFromTcpEntry: function( pTcpEntry: PTMibTcpRowOwnerModule;
  InfoClass: TTcpIpOwnerModuleInfoClass; pBuffer: Pointer; pdwSize: PDWORD): LongInt stdcall ;
GetTcpTable: function ( pTCPTable: PTMibTCPTable; pDWSize: PDWORD;
  bOrder: BOOL ): DWORD; stdcall;

const
    IpHlpDLL = 'IPHLPAPI.DLL';
var
    IpHlpModule: THandle;

function LoadIpHlp:Boolean;
begin
  Result := True;
  if IpHlpModule <> 0 then Exit;

  IpHlpModule := LoadLibrary (IpHlpDLL);
  if IpHlpModule = 0 then
  begin
    Result := false;
    exit;
  end ;
  GetTcpTable := GetProcAddress (IpHlpModule, 'GetTcpTable') ;
  AllocateAndGetTcpExTableFromStack := GetProcAddress (IpHlpModule,'AllocateAndGetTcpExTableFromStack') ;
  GetExtendedTcpTable := GetProcAddress (IpHlpModule, 'GetExtendedTcpTable') ;
  GetOwnerModuleFromTcpEntry := GetProcAddress (IpHlpModule, 'GetOwnerModuleFromTcpEntry') ;
end;

function IpAddr2Str( IPAddr: DWORD ): string;
var i:integer;
begin
  Result := '';
  for i := 1 to 4 do
  begin
    Result := Result + Format( '%3d.', [IPAddr and $FF] );
    IPAddr := IPAddr shr 8;
  end;
  Delete( Result, Length( Result ), 1 );
end;

function Port2Wrd( nwoPort: DWORD ): DWORD;
begin
  Result := Swap( WORD( nwoPort ) );
end;

function FileTimeToInt64 (const FileTime: TFileTime): Int64 ;
begin
  Move (FileTime, result, SizeOf (result)) ;
end;

const
  FileTimeBase = -109205.0;  // days between years 1601 and 1900
  FileTimeStep: Extended = 24.0 * 60.0 * 60.0 * 1000.0 * 1000.0 * 10.0; // 100 nsec per Day
function FileTimeToDateTime(const FileTime: TFileTime): TDateTime;
begin
    Result := FileTimeToInt64 (FileTime) / FileTimeStep ;
    Result := Result + FileTimeBase ;
end;


procedure IpTCPTable(var ConnRows: TConnRows);
var
  i, ExBufSize, NumEntries : integer;
  TableSize, ModSize : DWORD;
  ErrorCode, ErrorCode2 : DWORD;
  pTCPTable    : PTMibTCPTable ;
  pTCPTableEx  : PTMibTCPTableEx;
  pTCPTableEx2  : PTMibTCPTableOwnerModule;
  ExFlag, ExFlag2 : boolean ;
  TcpIpOwnerModuleBasicInfoEx: TTcpIpOwnerModuleBasicInfoEx ;
  LocalFileTime: TFileTime ;
begin
  if NOT LoadIpHlp then exit ;
  TableSize := 0 ;
  ExBufSize := 0 ;
  SetLength (ConnRows, 0) ;
  ExFlag := false ;
  ExFlag2 := Assigned (GetExtendedTcpTable) ;
  if NOT ExFlag2 then ExFlag := Assigned (AllocateAndGetTcpExTableFromStack) ;
  pTCPTable := Nil ;
  pTCPTableEx2 := Nil ;

  try
    // use latest API XP SP2, W2K3 SP1, Vista and later, first call : get size of table
    if ExFlag2 then
    begin
      ErrorCode := GetExtendedTCPTable(Nil, @TableSize, false, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0);
      if Errorcode <> ERROR_INSUFFICIENT_BUFFER then
        EXIT;

      // get required size of memory, call again
      GetMem(pTCPTableEx2, TableSize);
      // get table
      ErrorCode := GetExtendedTCPTable(pTCPTableEx2, @TableSize, true, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0);
      if ErrorCode <> NO_ERROR then
        exit ;
      NumEntries := pTCPTableEx2^.dwNumEntries;
      if NumEntries = 0 then
        exit;
      SetLength(ConnRows, NumEntries);
      for I := 0 to Pred (NumEntries) do
      begin
        with ConnRows [I], pTCPTableEx2^.Table [I] do
        begin
          ProcName := '' ;
          State := dwState ;
          LocalAddr := IpAddr2Str (dwLocalAddr) ;
          LocalPort := Port2Wrd (dwLocalPort) ;
          RemoteAddr := IPAddr2Str (dwRemoteAddr) ;
          RemotePort := Port2Wrd (dwRemotePort) ;
          if dwRemoteAddr = 0 then
            RemotePort := 0;
          FileTimeToLocalFileTime (liCreateTimestamp, LocalFileTime) ;
          CreateDT := FileTimeToDateTime (LocalFileTime) ;
          ProcessID := dwOwningPid ;
          if ProcessID > 0 then
          begin
            ModSize := SizeOf (TcpIpOwnerModuleBasicInfoEx) ;
            ErrorCode2 := GetOwnerModuleFromTcpEntry ( @pTCPTableEx2^.Table [I],
              TcpIpOwnerModuleInfoClassBasic, @TcpIpOwnerModuleBasicInfoEx, @ModSize);
            if ErrorCode2 = NO_ERROR then
              ProcName := TcpIpOwnerModuleBasicInfoEx.TcpIpOwnerModuleBasicInfo.pModulePath ;
          end;
        end;
      end;
    end
  // use originally undocumented API, XP only, not Vista
    else if ExFlag then
    begin
      AllocateAndGetTcpExTableFromStack (pTCPTableEx, true, GetProcessHeap, 2, 2);
      ExBufSize := HeapSize (GetProcessHeap, 0, pTCPTableEx);
      if ExBufSize = 0 then
        exit;
      NumEntries := pTCPTableEx^.dwNumEntries ;
      if NumEntries = 0 then
        exit;
      SetLength (ConnRows, NumEntries);
      for I := 0 to Pred (NumEntries) do
      begin
        with ConnRows [I], pTCPTableEx^.Table [I] do
        begin
          ProcName := '';
          CreateDT := 0;
          State := dwState;
          LocalAddr := IpAddr2Str (dwLocalAddr);
          LocalPort := Port2Wrd (dwLocalPort);
          RemoteAddr := IPAddr2Str (dwRemoteAddr);
          RemotePort := Port2Wrd (dwRemotePort);
          if dwRemoteAddr = 0 then
            RemotePort := 0;
          ProcessID := dwProcessID;
        end;
      end;
    end
    else
    begin
      // use older documented API, first call : get size of table
      ErrorCode := GetTCPTable (Nil, @TableSize, false ); // Angus
      if Errorcode <> ERROR_INSUFFICIENT_BUFFER then
        EXIT;

      // get required size of memory, call again
      GetMem (pTCPTable, TableSize);
      // get table
      ErrorCode := GetTCPTable (pTCPTable, @TableSize, true);
      if ErrorCode <> NO_ERROR then
        exit;
      NumEntries := pTCPTable^.dwNumEntries;
      if NumEntries = 0 then
        exit;
      SetLength (ConnRows, NumEntries) ;
      for I := 0 to Pred (NumEntries) do
      begin
        with ConnRows [I], pTCPTable^.Table [I] do
        begin
          ProcName := '';
          CreateDT := 0;
          State := dwState;
          LocalAddr := IpAddr2Str(dwLocalAddr);
          LocalPort := Port2Wrd(dwLocalPort);
          RemoteAddr := IPAddr2Str(dwRemoteAddr);
          RemotePort := Port2Wrd(dwRemotePort);
          if dwRemoteAddr = 0 then
            RemotePort := 0;
          ProcessID := 0 ;
        end;
      end;
    end;
  finally
    if ExFlag2 then
    begin
      if pTCPTableEx2 <> Nil then
        FreeMem (pTCPTableEx2);
    end
    else if ExFlag then
    begin
        if ExBufSize <> 0 then
          HeapFree (GetProcessHeap, 0, pTCPTableEx);
    end
    else if pTCPTable <> Nil then
       FreeMem (pTCPTable);
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:49 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz