Guest User

PoE Logout Console App

a guest
Oct 23rd, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Linq;
  4.  
  5.  
  6. namespace ConsoleApp1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var command = args[0];
  13.             var processId = args[1];
  14.  
  15.             if (command == "/process" && processId != null)
  16.             {
  17.                 var result = uint.TryParse(processId, out uint uintProcessId);
  18.                 if (result)
  19.                 {
  20.                     CommandHandler.KillTCPConnectionForProcess(uintProcessId);
  21.                 }
  22.             }
  23.  
  24.         }
  25.     }
  26.  
  27.     public static partial class CommandHandler
  28.     {
  29.         public static void KillTCPConnectionForProcess(uint ProcessId)
  30.         {
  31.             MibTcprowOwnerPid[] table;
  32.             var afInet = 2;
  33.             var buffSize = 0;
  34.             var ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll);
  35.             var buffTable = Marshal.AllocHGlobal(buffSize);
  36.             try
  37.             {
  38.                 ret = GetExtendedTcpTable(buffTable, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll);
  39.                 if (ret != 0)
  40.                     return;
  41.                 var tab = (MibTcptableOwnerPid)Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid));
  42.                 var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
  43.                 table = new MibTcprowOwnerPid[tab.dwNumEntries];
  44.                 for (var i = 0; i < tab.dwNumEntries; i++)
  45.                 {
  46.                     var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid));
  47.                     table[i] = tcpRow;
  48.                     rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow));
  49.  
  50.                 }
  51.             }
  52.             finally
  53.             {
  54.                 Marshal.FreeHGlobal(buffTable);
  55.             }
  56.  
  57.             //Kill Path Connection
  58.             var PathConnection = table.FirstOrDefault(t => t.owningPid == ProcessId);
  59.             PathConnection.state = 12;
  60.             var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PathConnection));
  61.             Marshal.StructureToPtr(PathConnection, ptr, false);
  62.             SetTcpEntry(ptr);
  63.  
  64.  
  65.         }
  66.  
  67.         [DllImport("iphlpapi.dll", SetLastError = true)]
  68.         private static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TcpTableClass tblClass, uint reserved = 0);
  69.  
  70.         [DllImport("iphlpapi.dll")]
  71.         private static extern int SetTcpEntry(IntPtr pTcprow);
  72.  
  73.         [StructLayout(LayoutKind.Sequential)]
  74.         public struct MibTcprowOwnerPid
  75.         {
  76.             public uint state;
  77.             public uint localAddr;
  78.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] localPort;
  79.             public uint remoteAddr;
  80.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] remotePort;
  81.             public uint owningPid;
  82.  
  83.         }
  84.  
  85.         [StructLayout(LayoutKind.Sequential)]
  86.         public struct MibTcptableOwnerPid
  87.         {
  88.             public uint dwNumEntries;
  89.             private readonly MibTcprowOwnerPid table;
  90.         }
  91.  
  92.         private enum TcpTableClass
  93.         {
  94.             TcpTableBasicListener,
  95.             TcpTableBasicConnections,
  96.             TcpTableBasicAll,
  97.             TcpTableOwnerPidListener,
  98.             TcpTableOwnerPidConnections,
  99.             TcpTableOwnerPidAll,
  100.             TcpTableOwnerModuleListener,
  101.             TcpTableOwnerModuleConnections,
  102.             TcpTableOwnerModuleAll
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment