Fakedo0r

EnumTCPConnections [Advanced]

Aug 24th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.56 KB | None | 0 0
  1. //******************************************************************************
  2. //* UNIT:         UNT_EnumTCPConnections
  3. //* AUTOR:        Fakedo0r .:PD-TEAM:.
  4. //* FECHA:        10.08.2012
  5. //* CORREO:       [email protected]
  6. //* BLOG:         Sub-Soul.blogspot.com / Sub-Soul.com
  7. //******************************************************************************
  8. Unit UNT_EnumTCPConnections;
  9. //******************************************************************************
  10. //DECLARACION DE LIBRERIAS / CLASES
  11. //******************************************************************************
  12. Interface
  13.  
  14. Uses
  15.   Winapi.Windows, Winapi.IpHlpApi, Winapi.IpRtrMib, Winapi.Messages,
  16.   System.SysUtils, System.Variants, PsAPI, TLHelp32,
  17.   System.Classes, Registry, ActiveX, WinSvc, Vcl.ComCtrls, Winsock,
  18.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  19. //******************************************************************************
  20. //DECLARACION DE CONSTANTES
  21. //******************************************************************************
  22. Const
  23.   TCP_TABLE_OWNER_PID_ALL = 5;
  24. //******************************************************************************
  25. //DECLARACION DE ESTRUCTURAS
  26. //******************************************************************************
  27. Type
  28.   PMIB_TCPROW_OWNER_PID = ^MIB_TCPROW_OWNER_PID;
  29.  
  30.   MIB_TCPROW_OWNER_PID = Packed Record
  31.     dwState:      DWORD;
  32.     dwLocalAddr:  DWORD;
  33.     dwLocalPort:  DWORD;
  34.     dwRemoteAddr: DWORD;
  35.     dwRemotePort: DWORD;
  36.     dwOwningPid:  DWORD;
  37.   End;
  38.  
  39.   PMIB_TCPTABLE_OWNER_PID = ^MIB_TCPTABLE_OWNER_PID;
  40.  
  41.   MIB_TCPTABLE_OWNER_PID = Packed Record
  42.     dwNumEntries: DWORD;
  43.     table: Array [0 .. 0] Of MIB_TCPROW_OWNER_PID;
  44.   End;
  45.  
  46.   TCP_TABLE_CLASS = Integer;
  47. //******************************************************************************
  48. //DECLARACION DE LIBRERIAS / CLASES EXTERNAS
  49. //******************************************************************************
  50. Function GetExtendedTcpTable(pTcpTable: Pointer; dwSize: PDWORD; bOrder: BOOL;
  51.   lAf: ULONG; TableClass: TCP_TABLE_CLASS; Reserved: ULONG): DWORD; Stdcall;
  52.   External 'iphlpapi.dll';
  53. //******************************************************************************
  54. //DECLARACION DE FUNCIONES / PROCEDIMIENTOS
  55. //******************************************************************************
  56. Function EnumTCPConnections(sDel: String): TStringList;
  57. Function IPToStr(iValue: Integer): String;
  58. Function StateToStr(dwState: DWORD): String;
  59. Function GetProcName(iPID: Integer): String;
  60. Function CloseConnection(dwID: DWORD): BOOL;
  61. Function KillProcess(dwPID: DWORD): BOOL;
  62. //******************************************************************************
  63. Implementation
  64. //******************************************************************************
  65. //<--- ENUMERA LAS CONEXIONES --->
  66. //******************************************************************************
  67. Function EnumTCPConnections(sDel: String): TStringList;
  68. Var
  69.   dwSize:   DWORD;
  70.   dwIndex:  DWORD;
  71.   tArrTemp: TStringList;
  72.   tTCPTOP:  PMIB_TCPTABLE_OWNER_PID;
  73. Begin
  74.   Result := TStringList.Create;
  75.   tArrTemp := TStringList.Create;
  76.   dwSize  := 0;
  77.  
  78.   If GetExtendedTcpTable(Nil, @dwSize , False, AF_INET,
  79.     TCP_TABLE_OWNER_PID_ALL, 0) <> ERROR_INSUFFICIENT_BUFFER Then
  80.     Exit;
  81.  
  82.   GetMem(tTCPTOP, dwSize);
  83.  
  84.   If GetExtendedTcpTable(tTCPTOP, @dwSize , True, AF_INET,
  85.     TCP_TABLE_OWNER_PID_ALL, 0) = NO_ERROR Then
  86.     For dwIndex := 0 To tTCPTOP.dwNumEntries - 1 Do
  87.       tArrTemp.Add(GetProcName(tTCPTOP.table[dwIndex].dwOwningPid) + sDel +
  88.                   IntToStr(tTCPTOP.table[dwIndex].dwOwningPid) + sDel +
  89.                   IpToStr(tTCPTOP.table[dwIndex].dwLocalAddr) + sDel +
  90.                   IntToStr(htons(tTCPTOP.table[dwIndex].dwLocalPort)) + sDel +
  91.                   IPToStr(tTCPTOP.table[dwIndex].dwRemoteAddr) + sDel +
  92.                   IntToStr(htons(tTCPTOP.table[dwIndex].dwRemotePort)) + sDel +
  93.                   StateToStr(tTCPTOP.table[dwIndex].dwState));
  94.   FreeMem(tTCPTOP);
  95.   Result := tArrTemp;
  96. End;
  97. //******************************************************************************
  98. //<--- CIERRA X CONEXION --->
  99. //******************************************************************************
  100. Function CloseConnection(dwID: DWORD): BOOL;
  101. Var
  102.   dwSize:     DWORD;
  103.   dwIndex:    DWORD;
  104.   tTCPTABLE:  PMIB_TCPTABLE;
  105. Begin
  106.   dwSize := 0;
  107.   GetMem(tTCPTABLE, SizeOf(MIB_TCPTABLE));
  108.  
  109.   If GetTcpTable(tTCPTABLE, dwSize, True) <> ERROR_INSUFFICIENT_BUFFER Then
  110.     Exit;
  111.   GetMem(tTCPTABLE, dwSize);
  112.  
  113.   If GetTcpTable(tTCPTABLE, dwSize, True) = NO_ERROR Then
  114.     If tTCPTABLE.table[dwID].dwState <> MIB_TCP_STATE_LISTEN Then
  115.     Begin
  116.       tTCPTABLE.table[dwID].dwState := MIB_TCP_STATE_DELETE_TCB;
  117.  
  118.       If SetTcpEntry(tTCPTABLE.table[dwID]) = NO_ERROR Then
  119.         Result := True
  120.       Else
  121.         Result := False;
  122.     End;
  123.   FreeMem(tTCPTABLE);
  124. End;
  125. //******************************************************************************
  126. //<--- CIERRA X PROCESOS APARTIR DE PID --->
  127. //******************************************************************************
  128. Function KillProcess(dwPID: DWORD): BOOL;
  129. var
  130.   tProc: THandle;
  131. begin
  132.   Try
  133.     tProc := OpenProcess(PROCESS_ALL_ACCESS, True, dwPID);
  134.  
  135.     If TerminateProcess(tProc, 0) Then
  136.       Result := True
  137.   Except
  138.     Result := False;
  139.   End;
  140. end;
  141. //******************************************************************************
  142. //<--- MUESTRA EL ESTADO DE X CONEXION --->
  143. //******************************************************************************
  144. Function StateToStr(dwState: DWORD): String;
  145. Var
  146.   sTemp: String;
  147. Begin
  148.   Case dwState Of
  149.     MIB_TCP_STATE_CLOSED: sTemp := 'CLOSED';
  150.     MIB_TCP_STATE_LISTEN: sTemp := 'LISTENING';
  151.     MIB_TCP_STATE_SYN_SENT: sTemp := 'SYN_SENT';
  152.     MIB_TCP_STATE_SYN_RCVD: sTemp := 'SYN_RCVD';
  153.     MIB_TCP_STATE_ESTAB: sTemp := 'ESTABLISHED';
  154.     MIB_TCP_STATE_FIN_WAIT1: sTemp := 'FIN_WAIT1';
  155.     MIB_TCP_STATE_FIN_WAIT2: sTemp := 'FIN_WAIT2';
  156.     MIB_TCP_STATE_CLOSE_WAIT: sTemp := 'CLOSE_WAIT';
  157.     MIB_TCP_STATE_CLOSING: sTemp := 'CLOSING';
  158.     MIB_TCP_STATE_LAST_ACK: sTemp := 'LAST_ACK';
  159.     MIB_TCP_STATE_TIME_WAIT: sTemp := 'TIME_WAIT';
  160.     MIB_TCP_STATE_DELETE_TCB: sTemp := 'DELETE_TCB';
  161.   End;
  162.  
  163.   Result := sTemp;
  164. End;
  165. //******************************************************************************
  166. //<--- CONVIERTE IP A CADENA --->
  167. //******************************************************************************
  168. Function IPToStr(iValue: Integer): String;
  169. Var
  170.   y1: Byte;
  171.   y2: Byte;
  172.   x1: WORD;
  173.   x2: WORD;
  174. Begin
  175.   Result := '';
  176.  
  177.   x1 := iValue Shr 16;
  178.   x2 := iValue And $FFFF;
  179.  
  180.   y1 := x2 Div $100;
  181.   y2 := x2 Mod $100;
  182.  
  183.   Result := IntToStr(y2) + '.' + IntToStr(y1) + '.';
  184.  
  185.   y1 := x1 Div $100;
  186.   y2 := x1 Mod $100;
  187.  
  188.   Result := Result + IntToStr(y2) + '.' + IntToStr(y1);
  189. End;
  190. //******************************************************************************
  191. //<--- OBTIENE EL NOMBRE DE PROCESO APARTIR DE PID --->
  192. //******************************************************************************
  193. Function GetProcName(iPID: Integer): String;
  194. Var
  195.   tSnapShot:  THandle;
  196.   tProcEntry: TProcessEntry32;
  197. Begin
  198.   tSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  199.   tProcEntry.dwSize := Sizeof(tProcEntry);
  200.  
  201.   If Process32First(tSnapShot, tProcEntry) Then
  202.   Begin
  203.     Repeat
  204.       If tProcEntry.th32ProcessID = iPID Then
  205.       Begin
  206.         Result := tProcEntry.szExeFile;
  207.         Break;
  208.       End;
  209.     Until Not Process32Next(tSnapShot, tProcEntry);
  210.   End;
  211.  
  212.   CloseHandle(tSnapShot);
  213. End;
  214.  
  215. End.
Advertisement
Add Comment
Please, Sign In to add comment