Created
August 12, 2022 11:46
-
-
Save AlpaGit/8ab028f80a2ba471d25b595fa07a9a07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Diagnostics; | |
| using System.Net; | |
| using System.Runtime.InteropServices; | |
| namespace CheckTcpConnection; | |
| public class Main | |
| { | |
| [DllImport("iphlpapi")] | |
| public static extern unsafe uint GetTcpTable2(MIB_TCPTABLE2* TcpTable, ref uint SizePointer, bool Order); | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct MIB_TCPTABLE2 { | |
| public uint dwNumEntries; | |
| public MIB_TCPROW2 table; | |
| } | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct MIB_TCPROW2 { | |
| public const uint SIZE = 0x1C; | |
| public MibTcpState dwState; | |
| public uint dwLocalAddr; | |
| public uint dwLocalPort; | |
| public uint dwRemoteAddr; | |
| public uint dwRemotePort; | |
| public uint dwOwningPid; | |
| public TCP_CONNECTION_OFFLOAD_STATE dwOffloadState; | |
| } | |
| public enum TCP_CONNECTION_OFFLOAD_STATE { | |
| TcpConnectionOffloadStateInHost = 0, | |
| TcpConnectionOffloadStateOffloading = 1, | |
| TcpConnectionOffloadStateOffloaded = 2, | |
| TcpConnectionOffloadStateUploading = 3, | |
| TcpConnectionOffloadStateMax = 4, | |
| } | |
| public enum MibTcpState : uint | |
| { | |
| CLOSED = 1, | |
| LISTENING = 2, | |
| SYN_SENT = 3, | |
| SYN_RCVD = 4, | |
| ESTABLISHED = 5, | |
| FIN_WAIT1 = 6, | |
| FIN_WAIT2 = 7, | |
| CLOSE_WAIT = 8, | |
| CLOSING = 9, | |
| LAST_ACK = 10, | |
| TIME_WAIT = 11, | |
| DELETE_TCB = 12, | |
| NONE = 0 | |
| } | |
| public static unsafe MIB_TCPROW2[] GetTcpRows() { | |
| uint sizePointer = 0; | |
| const bool order = false; | |
| GetTcpTable2(null, ref sizePointer, order); | |
| var rawTableBytes = new byte[sizePointer]; | |
| fixed (byte* ptbl = rawTableBytes) { | |
| var error = GetTcpTable2((MIB_TCPTABLE2*)ptbl, ref sizePointer, order); | |
| Debug.Assert(error == 0); | |
| if (error != 0) | |
| return Array.Empty<MIB_TCPROW2>(); | |
| var elems = (int)((MIB_TCPTABLE2*)ptbl)->dwNumEntries; | |
| var res = new MIB_TCPROW2[elems]; | |
| var pelem = (MIB_TCPROW2*)(ptbl + 4); | |
| var p = ptbl + 4; | |
| for (var i = 0; i < res.Length; i++, p += MIB_TCPROW2.SIZE) | |
| res[i] = *(MIB_TCPROW2*)p; | |
| return res; | |
| } | |
| } | |
| public void Run() | |
| { | |
| var tcpRows = GetTcpRows(); | |
| var dofusProcesses = Process.GetProcessesByName("Dofus Retro"); | |
| foreach (var process in dofusProcesses) | |
| { | |
| var tcpConnections = tcpRows.Where(x => x.dwOwningPid == process.Id); | |
| foreach (var connection in tcpConnections) | |
| { | |
| Console.WriteLine( | |
| $"Le processus {process.Id} à une connexion TCP ouverte sur {new IPAddress(connection.dwRemoteAddr)}:{IPAddress.HostToNetworkOrder((short)(connection.dwRemotePort & 0xFFFF))}"); | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment