Skip to content

Instantly share code, notes, and snippets.

@pigeonhands
Last active October 10, 2015 01:25
Show Gist options
  • Save pigeonhands/f98cb3bb1073ee5f25bd to your computer and use it in GitHub Desktop.
Save pigeonhands/f98cb3bb1073ee5f25bd to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
/// <summary>
/// Network connections healper
/// Made by BahNahNah
/// uid=2388291
/// </summary>
public class NetworkHelper
{
/// <summary>
/// Gets a list of all tcp connections
/// </summary>
/// <returns>Connections array</returns>
public static TCPConnection[] GetTCPConnection()
{
TCPConnection[] returnTable = null;
int tSize = 0;
GetExtendedTcpTable(IntPtr.Zero, ref tSize, true, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
IntPtr gAlloc = Marshal.AllocHGlobal(tSize);
try
{
GetExtendedTcpTable(gAlloc, ref tSize, true, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
TABLE_ENTRYS tableEntrys = (TABLE_ENTRYS)Marshal.PtrToStructure(gAlloc, typeof(TABLE_ENTRYS));
IntPtr baseAddr = (IntPtr)(gAlloc + Marshal.SizeOf(tableEntrys.dwNumEntries));
returnTable = new TCPConnection[tableEntrys.dwNumEntries];
for (int i = 0; i < tableEntrys.dwNumEntries; i++)
{
TCPROW_OWNER_PID row = (TCPROW_OWNER_PID)Marshal.PtrToStructure(baseAddr, typeof(TCPROW_OWNER_PID));
returnTable[i] = new TCPConnection(row);
baseAddr = (IntPtr)(baseAddr + Marshal.SizeOf(row));
}
}
finally
{
Marshal.FreeHGlobal(gAlloc);
}
return returnTable;
}
/// <summary>
/// Gets a list of all udp connections
/// </summary>
/// <returns>Connections array</returns>
public static UDPConnection[] GetUDPConnections()
{
UDPConnection[] returnTable = null;
int size = 0;
GetExtendedUdpTable(IntPtr.Zero, ref size, true, 2, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0);
IntPtr gAlloc = Marshal.AllocHGlobal(size);
try
{
GetExtendedUdpTable(gAlloc, ref size, true, 2, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0);
TABLE_ENTRYS entrys = (TABLE_ENTRYS)Marshal.PtrToStructure(gAlloc, typeof(TABLE_ENTRYS));
returnTable = new UDPConnection[entrys.dwNumEntries];
IntPtr baseAddress = (IntPtr)(gAlloc + Marshal.SizeOf(entrys.dwNumEntries));
for(int i = 0; i < entrys.dwNumEntries; i++)
{
UDPROW_OWNER_PID row = (UDPROW_OWNER_PID)Marshal.PtrToStructure(baseAddress, typeof(UDPROW_OWNER_PID));
returnTable[i] = new UDPConnection(row);
baseAddress = (IntPtr)(baseAddress + Marshal.SizeOf(row));
}
}
finally
{
Marshal.FreeHGlobal(gAlloc);
}
return returnTable;
}
#region " Structs "
struct TABLE_ENTRYS
{
public uint dwNumEntries;
}
public struct TCPROW_OWNER_PID
{
public uint dwState;
public uint dwLocalAddr;
public uint dwLocalPort;
public uint dwRemoteAddr;
public uint dwRemotePort;
public uint dwOwningPid;
}
public struct UDPROW_OWNER_PID
{
public uint dwLocalAddr;
public uint dwLocalPort;
public uint dwOwningPid;
}
#endregion
#region " WinApi "
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, UInt32 reserved);
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern uint GetExtendedUdpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, UInt32 reserved);
#endregion
#region " Enums "
enum IPVersion : int
{
IPV4 = 4,
IPV6 = 6
}
enum TCP_TABLE_CLASS
{
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
}
enum UDP_TABLE_CLASS
{
UDP_TABLE_BASIC,
UDP_TABLE_OWNER_PID,
UDP_TABLE_OWNER_MODULE
}
#endregion
}
#region " TCP "
public class TCPConnection
{
NetworkHelper.TCPROW_OWNER_PID entry;
#region " Constructors "
public TCPConnection(NetworkHelper.TCPROW_OWNER_PID _row)
{
entry = _row;
LocalEndpoint = new IPEndPoint(new IPAddress(entry.dwLocalAddr), Convert.ToInt32(entry.dwLocalPort));
RemoteEndpoint = new IPEndPoint(new IPAddress(entry.dwRemoteAddr), Convert.ToInt32(entry.dwRemotePort));
}
#endregion
#region " Functions "
/// <summary>
/// Closes the TCP Connection (Requires Admin)
/// </summary>
/// <returns>Success</returns>
public bool CloseConnection()
{
uint oldState = entry.dwState;
entry.dwState = (uint)TCPConnectionState.Delete_TCB;
bool success = SetTcpEntry(ref entry) == 0;
if (!success) entry.dwState = oldState;
return success;
}
/// <summary>
/// Gets the process that owns the connection
/// </summary>
/// <returns>Returns process if success, Null if failed</returns>
public Process GetOwner()
{
try
{
return Process.GetProcessById(PID);
}
catch
{
return null;
}
}
#endregion
#region " Properties "
public IPEndPoint LocalEndpoint { get; private set; }
public IPEndPoint RemoteEndpoint { get; private set; }
public int PID { get { return Convert.ToInt32(entry.dwOwningPid); } }
public TCPConnectionState State { get { return (TCPConnectionState)entry.dwState; } }
#endregion
#region " WinApi "
[DllImport("Iphlpapi.dll")]
private static extern uint SetTcpEntry(ref NetworkHelper.TCPROW_OWNER_PID row);
#endregion
}
public enum TCPConnectionState : uint
{
Closed = 1,
Listening = 2,
SYN_Sent = 3,
Syn_Recieved = 4,
Established = 5,
Finish_Wait_1 = 6,
Finish_Wait_2 = 7,
Closed_Wait = 8,
Closing = 9,
Last_ACK = 10,
Time_Wait = 11,
Delete_TCB = 12
}
#endregion
#region " UDP "
public class UDPConnection
{
NetworkHelper.UDPROW_OWNER_PID entry;
/// <summary>
/// UDP Connection EndPoint
/// </summary>
public IPEndPoint LocalEndpoint { get; private set; }
/// <summary>
/// Process ID of owner
/// </summary>
public int PID { get; private set; }
public UDPConnection(NetworkHelper.UDPROW_OWNER_PID row)
{
entry = row;
LocalEndpoint = new IPEndPoint(new IPAddress(entry.dwLocalAddr), Convert.ToInt32(entry.dwLocalPort));
PID = Convert.ToInt32(entry.dwOwningPid);
}
/// <summary>
/// Gets the process that owns the connection
/// </summary>
/// <returns>Returns process if success, Null if failed</returns>
public Process GetOwner()
{
try
{
return Process.GetProcessById(PID);
}
catch
{
return null;
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment