Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- using System.Linq;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var command = args[0];
- var processId = args[1];
- if (command == "/process" && processId != null)
- {
- var result = uint.TryParse(processId, out uint uintProcessId);
- if (result)
- {
- CommandHandler.KillTCPConnectionForProcess(uintProcessId);
- }
- }
- }
- }
- public static partial class CommandHandler
- {
- public static void KillTCPConnectionForProcess(uint ProcessId)
- {
- MibTcprowOwnerPid[] table;
- var afInet = 2;
- var buffSize = 0;
- var ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll);
- var buffTable = Marshal.AllocHGlobal(buffSize);
- try
- {
- ret = GetExtendedTcpTable(buffTable, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll);
- if (ret != 0)
- return;
- var tab = (MibTcptableOwnerPid)Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid));
- var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
- table = new MibTcprowOwnerPid[tab.dwNumEntries];
- for (var i = 0; i < tab.dwNumEntries; i++)
- {
- var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid));
- table[i] = tcpRow;
- rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow));
- }
- }
- finally
- {
- Marshal.FreeHGlobal(buffTable);
- }
- //Kill Path Connection
- var PathConnection = table.FirstOrDefault(t => t.owningPid == ProcessId);
- PathConnection.state = 12;
- var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PathConnection));
- Marshal.StructureToPtr(PathConnection, ptr, false);
- SetTcpEntry(ptr);
- }
- [DllImport("iphlpapi.dll", SetLastError = true)]
- private static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TcpTableClass tblClass, uint reserved = 0);
- [DllImport("iphlpapi.dll")]
- private static extern int SetTcpEntry(IntPtr pTcprow);
- [StructLayout(LayoutKind.Sequential)]
- public struct MibTcprowOwnerPid
- {
- public uint state;
- public uint localAddr;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] localPort;
- public uint remoteAddr;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] remotePort;
- public uint owningPid;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct MibTcptableOwnerPid
- {
- public uint dwNumEntries;
- private readonly MibTcprowOwnerPid table;
- }
- private enum TcpTableClass
- {
- TcpTableBasicListener,
- TcpTableBasicConnections,
- TcpTableBasicAll,
- TcpTableOwnerPidListener,
- TcpTableOwnerPidConnections,
- TcpTableOwnerPidAll,
- TcpTableOwnerModuleListener,
- TcpTableOwnerModuleConnections,
- TcpTableOwnerModuleAll
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment