public
Last active

POSH: Get network statistics (powershell implementation of netstat command)

  • Download Gist
get-netstat.ps1
PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
Add-Type -TypeDefinition @"
using System;
using System.Net;
using System.Runtime.InteropServices;
 
public class NetworkUtil
{
[DllImport("iphlpapi.dll", SetLastError = true)]
static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, int reserved);
[DllImport("iphlpapi.dll", SetLastError = true)]
static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, int reserved);
[StructLayout(LayoutKind.Sequential)]
public struct MIB_TCPROW_OWNER_PID
{
public uint dwState;
public uint dwLocalAddr;
public uint dwLocalPort;
public uint dwRemoteAddr;
public uint dwRemotePort;
public uint dwOwningPid;
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_UDPROW_OWNER_PID
{
public uint dwLocalAddr;
public uint dwLocalPort;
public uint dwOwningPid;
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_TCPTABLE_OWNER_PID
{
public uint dwNumEntries;
MIB_TCPROW_OWNER_PID table;
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_UDPTABLE_OWNER_PID
{
public uint dwNumEntries;
MIB_UDPROW_OWNER_PID table;
}
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_OWNER_MODULE
}
 
public static Connection[] GetTCP()
{
 
MIB_TCPROW_OWNER_PID[] tTable;
int AF_INET = 2;
int buffSize = 0;
 
uint ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
IntPtr buffTable = Marshal.AllocHGlobal(buffSize);
 
try
{
ret = GetExtendedTcpTable(buffTable, ref buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
if (ret != 0)
{
Connection[] con = new Connection[0];
return con;
}
 
MIB_TCPTABLE_OWNER_PID tab = (MIB_TCPTABLE_OWNER_PID)Marshal.PtrToStructure(buffTable, typeof(MIB_TCPTABLE_OWNER_PID));
IntPtr rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
tTable = new MIB_TCPROW_OWNER_PID[tab.dwNumEntries];
 
for (int i = 0; i < tab.dwNumEntries; i++)
{
MIB_TCPROW_OWNER_PID tcpRow = (MIB_TCPROW_OWNER_PID)Marshal.PtrToStructure(rowPtr, typeof(MIB_TCPROW_OWNER_PID));
tTable[i] = tcpRow;
rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow)); // next entry
}
}
finally
{ Marshal.FreeHGlobal(buffTable);}
Connection[] cons = new Connection[tTable.Length];
 
for(int i=0; i < tTable.Length; i++)
{
IPAddress localip = new IPAddress(BitConverter.GetBytes(tTable[i].dwLocalAddr));
IPAddress remoteip = new IPAddress(BitConverter.GetBytes(tTable[i].dwRemoteAddr));
byte[] barray = BitConverter.GetBytes(tTable[i].dwLocalPort);
int localport = (barray[0] * 256) + barray[1];
barray = BitConverter.GetBytes(tTable[i].dwRemotePort);
int remoteport = (barray[0] * 256) + barray[1];
string state;
switch (tTable[i].dwState)
{
case 1:
state = "Closed";
break;
case 2:
state = "LISTENING";
break;
case 3:
state = "SYN SENT";
break;
case 4:
state = "SYN RECEIVED";
break;
case 5:
state = "ESTABLISHED";
break;
case 6:
state = "FINSIHED 1";
break;
case 7:
state = "FINISHED 2";
break;
case 8:
state = "CLOSE WAIT";
break;
case 9:
state = "CLOSING";
break;
case 10:
state = "LAST ACKNOWLEDGE";
break;
case 11:
state = "TIME WAIT";
break;
case 12:
state = "DELETE TCB";
break;
default:
state = "UNKNOWN";
break;
}
Connection tmp = new Connection(localip, localport, remoteip, remoteport, (int)tTable[i].dwOwningPid, state);
cons[i] = (tmp);
}
return cons;
}
public static Connection[] GetUDP()
{
MIB_UDPROW_OWNER_PID[] tTable;
int AF_INET = 2; // IP_v4
int buffSize = 0;
 
uint ret = GetExtendedUdpTable(IntPtr.Zero, ref buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0);
IntPtr buffTable = Marshal.AllocHGlobal(buffSize);
 
try
{
ret = GetExtendedUdpTable(buffTable, ref buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0);
if (ret != 0)
{//none found
Connection[] con = new Connection[0];
return con;
}
MIB_UDPTABLE_OWNER_PID tab = (MIB_UDPTABLE_OWNER_PID)Marshal.PtrToStructure(buffTable, typeof(MIB_UDPTABLE_OWNER_PID));
IntPtr rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
tTable = new MIB_UDPROW_OWNER_PID[tab.dwNumEntries];
for (int i = 0; i < tab.dwNumEntries; i++)
{
MIB_UDPROW_OWNER_PID udprow = (MIB_UDPROW_OWNER_PID)Marshal.PtrToStructure(rowPtr, typeof(MIB_UDPROW_OWNER_PID));
tTable[i] = udprow;
rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(udprow));
}
}
finally
{ Marshal.FreeHGlobal(buffTable);}
Connection[] cons = new Connection[tTable.Length];
 
for (int i = 0; i < tTable.Length; i++)
{
IPAddress localip = new IPAddress(BitConverter.GetBytes(tTable[i].dwLocalAddr));
byte[] barray = BitConverter.GetBytes(tTable[i].dwLocalPort);
int localport = (barray[0] * 256) + barray[1];
Connection tmp = new Connection(localip, localport, (int)tTable[i].dwOwningPid);
cons[i] = tmp;
}
return cons;
}
}
public class Connection
{
private IPAddress _localip, _remoteip;
private int _localport, _remoteport, _pid;
private string _state, _remotehost, _proto;
public Connection(IPAddress Local, int LocalPort, IPAddress Remote, int RemotePort, int PID, string State)
{
_proto = "TCP";
_localip = Local;
_remoteip = Remote;
_localport = LocalPort;
_remoteport = RemotePort;
_pid = PID;
_state = State;
}
public Connection(IPAddress Local, int LocalPort, int PID)
{
_proto = "UDP";
_localip = Local;
_localport = LocalPort;
_pid = PID;
}
public IPAddress LocalIP { get{ return _localip;}}
public IPAddress RemoteIP{ get{return _remoteip;}}
public int LocalPort{ get{return _localport;}}
public int RemotePort{ get { return _remoteport; }}
public int PID{ get { return _pid; }}
public string State{ get { return _state; }}
public string Protocol{get { return _proto; }}
public string RemoteHostName
{
get {
if (_remotehost == null)
_remotehost = Dns.GetHostEntry(_remoteip).HostName;
return _remotehost;
}
}
public string PIDName{ get { return (System.Diagnostics.Process.GetProcessById(_pid)).ProcessName; } }
}
"@
 
function Get-NetStat
{
PARAM([switch]$TCPonly, [switch]$UDPonly)
if(!$UDPonly)
{$tcp = [NetworkUtil]::GetTCP()}
if(!$tcponly)
{$udp = [NetworkUtil]::GetUDP()}
$results = $tcp + $udp
return $results
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.