Advertisement
Guest User

Untitled

a guest
Nov 28th, 2011
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using DWORD = System.UInt32;
  7.  
  8. namespace ChatBlocker
  9. {
  10.     class Program
  11.     {
  12.         public enum State{
  13.                 All=0,
  14.                 Closed=1,
  15.                 Listen=2,
  16.                 Syn_Sent=3,
  17.                 Syn_Rcvd=4,
  18.                 Established=5,
  19.                 Fin_Wait1=6,
  20.                 Fin_Wait2=7,
  21.                 Close_Wait=8,
  22.                 Closing=9,
  23.                 Last_Ack=10,
  24.                 Time_Wait=11,
  25.                 Delete_TCB=12
  26.         }
  27.         private struct MIB_TCPROW{
  28.                 public int dwState;
  29.                 public int dwLocalAddr;
  30.                 public int dwLocalPort;
  31.                 public int dwRemoteAddr;
  32.                 public int dwRemotePort;
  33.         }
  34.         [DllImport("iphlpapi.dll")]
  35.         private static extern int GetTcpTable(IntPtr pTcpTable,ref int pdwSize,bool bOrder);
  36.         [DllImport("iphlpapi.dll")]
  37.         private static extern int SetTcpEntry(IntPtr pTcprow);
  38.         [DllImport("wsock32.dll")]
  39.         private static extern int ntohs(int netshort);
  40.         [DllImport("wsock32.dll")]
  41.         private static extern int htons(int netshort);
  42.         public static void CloseRemotePort(int port)
  43.         {
  44.             MIB_TCPROW[] rows = getTcpTable();
  45.             for (int i = 0; i < rows.Length; i++)
  46.             {
  47.                 if (port == ntohs(rows[i].dwRemotePort))
  48.                 {
  49.                     rows[i].dwState = (int)State.Delete_TCB;
  50.                     IntPtr ptr = GetPtrToNewObject(rows[i]);
  51.                     int ret = SetTcpEntry(ptr);
  52.                 }
  53.             }
  54.         }
  55.         private static IntPtr GetPtrToNewObject(object obj)
  56.         {
  57.             IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(obj));
  58.             Marshal.StructureToPtr(obj, ptr, false);
  59.             return ptr;
  60.         }
  61.         private static MIB_TCPROW[] getTcpTable()
  62.         {
  63.             IntPtr buffer = IntPtr.Zero; bool allocated = false;
  64.             try
  65.             {
  66.                 int iBytes = 0;
  67.                 GetTcpTable(IntPtr.Zero, ref iBytes, false); //Getting size of return data
  68.                 buffer = Marshal.AllocCoTaskMem(iBytes); //allocating the datasize
  69.  
  70.                 allocated = true;
  71.                 GetTcpTable(buffer, ref iBytes, false); //Run it again to fill the memory with the data
  72.                 int structCount = Marshal.ReadInt32(buffer); // Get the number of structures
  73.                 IntPtr buffSubPointer = buffer; //Making a pointer that will point into the buffer
  74.                 buffSubPointer = (IntPtr)((int)buffer + 4); //Move to the first data (ignoring dwNumEntries from the original MIB_TCPTABLE struct)
  75.                 MIB_TCPROW[] tcpRows = new MIB_TCPROW[structCount]; //Declaring the array
  76.                 //Get the struct size
  77.                 MIB_TCPROW tmp = new MIB_TCPROW();
  78.                 int sizeOfTCPROW = Marshal.SizeOf(tmp);
  79.                 //Fill the array 1 by 1
  80.                 for (int i = 0; i < structCount; i++)
  81.                 {
  82.                     tcpRows[i] = (MIB_TCPROW)Marshal.PtrToStructure(buffSubPointer, typeof(MIB_TCPROW)); //copy struct data
  83.                     buffSubPointer = (IntPtr)((int)buffSubPointer + sizeOfTCPROW); //move to next structdata
  84.                 }
  85.  
  86.                 return tcpRows;
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 throw new Exception("getTcpTable failed! [" + ex.GetType().ToString() + "," + ex.Message + "]");
  91.             }
  92.             finally
  93.             {
  94.                 if (allocated) Marshal.FreeCoTaskMem(buffer); //Free the allocated memory
  95.             }
  96.         }
  97.         static void Main(string[] args)
  98.         {
  99.             Menu();
  100.         }
  101.         static void Menu()
  102.         {
  103.             int id = 0;
  104.             string[] lines = { "127.0.0.1 chat.na1.lol.riotgames.com", "127.0.0.1 chat.eu.lol.riotgames.com", "127.0.0.1 chat.eun1.lol.riotgames.com" };
  105.             while (true)
  106.             {
  107.                 switch (id)
  108.                 {
  109.                     case 1:
  110.                         {
  111.                             Console.Clear();
  112.                             try
  113.                             {
  114.  
  115.                                 FileStream file = new FileStream(System.Environment.SystemDirectory+ @"\drivers\etc\hosts", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  116.                                 foreach (string line in lines)
  117.                                 {
  118.                                     byte[] message = Encoding.ASCII.GetBytes("\n"+line+"\n");
  119.                                     file.Write(message, 0, message.Length);
  120.                                 }
  121.                                 file.Close();
  122.                                 CloseRemotePort(5223);
  123.                             }
  124.                             catch (Exception e)
  125.                             {
  126.                                 Console.WriteLine(e.Message);
  127.                                 Console.WriteLine("Unable to open file, is it open by any other program?");
  128.                                 Console.WriteLine("Press any button to go back to the menu");
  129.                                 Console.Read();
  130.                                 id = 0;
  131.                                 break;
  132.                             }
  133.                             Console.WriteLine("Succesfully wrote to hosts file!");
  134.                             Console.WriteLine("Press any button to go back to the menu");
  135.                             Console.Read();
  136.                             id = 0;
  137.                            
  138.                             break;
  139.                         }
  140.                     case 2:
  141.                         {
  142.                             Console.Clear();
  143.                             try
  144.                             {
  145.                                 string[] alllines = File.ReadAllLines(System.Environment.SystemDirectory + @"\drivers\etc\hosts");
  146.                                 List<string> list = new List<string>(alllines);
  147.                                 foreach (string line in lines)
  148.                                 {
  149.                                     list.Remove(line);
  150.                                 }
  151.                                 list.RemoveAll(delegate(string line) { return line == ""; });
  152.                                 FileStream file = new FileStream(System.Environment.SystemDirectory + @"\drivers\etc\hosts", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  153.                                 foreach (string str in list)
  154.                                 {
  155.                                     byte[] message = Encoding.ASCII.GetBytes(str+"\n");
  156.                                     file.Write(message, 0, message.Length);
  157.                                 }
  158.                                 file.Close();
  159.                             }
  160.                             catch (Exception)
  161.                             {
  162.                                 Console.WriteLine("Unable to open file, is it open by any other program?");
  163.                                 Console.WriteLine("Press any button to go back to the menu");
  164.                                 Console.Read();
  165.                                 id = 0;
  166.                                 break;
  167.                             }
  168.                             Console.WriteLine("Succesfully wrote to hosts file!");
  169.                             Console.WriteLine("Press any button to go back to the menu");
  170.                             Console.Read();
  171.                             id = 0;
  172.                             break;
  173.                         }
  174.                     case 3:
  175.                         {
  176.                             return;
  177.                         }
  178.                     default:
  179.                         {
  180.                             Console.Write("\n\n");
  181.                             Console.WriteLine("Invisible Mode for League of Legends!");
  182.                             Console.Write("\n\n");
  183.                             Console.WriteLine("1. Enable Invisible Mode");
  184.                             Console.WriteLine("2. Disable Invisible Mode");
  185.                             Console.WriteLine("3. Exit\n");
  186.                             try
  187.                             {
  188.                                 id = Int32.Parse(Console.ReadLine());
  189.                             }
  190.                             catch (Exception)
  191.                             {
  192.                                 Console.WriteLine("\n Please write a valid number!");
  193.                             }
  194.                             break;
  195.                         }
  196.                 }
  197.             }
  198.         }
  199.     }
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement