Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.IO;
  5.  
  6. namespace MySockTest
  7. {
  8.     class Program
  9.     {
  10.         // Variables
  11.         public static string pServer = "87.106.48.157";
  12.         public static int pPort = 6543;
  13.  
  14.         public static string dServer = "92.61.32.19";
  15.         public static int dPort = 6667;
  16.  
  17.         public static string userName = "anonymous";
  18.         public static string userPass = "anonymous";
  19.  
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.             // Variable
  24.             IPEndPoint pIpPort = new IPEndPoint(IPAddress.Parse(pServer), pPort);
  25.  
  26.             ushort nIndex;
  27.             byte[] pRequest = new byte[257];
  28.             byte[] pResponse = new byte[257];
  29.  
  30.             // Connection
  31.             Console.WriteLine("Trying to connect...");
  32.             Socket pClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  33.             pClient.Connect(pIpPort);
  34.             Console.WriteLine(" - Info set up:\n");
  35.  
  36.             nIndex = 0;
  37.             pRequest[nIndex++] = 0x05; // Version 5.
  38.             Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[0]);
  39.             pRequest[nIndex++] = 0x02; // 2 Authentication methods are in packet...
  40.             Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[1]);
  41.             pRequest[nIndex++] = 0x00; // NO AUTHENTICATION REQUIRED
  42.             Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[2]);
  43.             pRequest[nIndex++] = 0x02; // USERNAME/PASSWOR
  44.             Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[3]);
  45.  
  46.             pClient.Send(pRequest, nIndex, SocketFlags.None);
  47.             Console.WriteLine("\n[S] Index: {0}, Request[1]: {1}, Response[0]: {2}", nIndex, pRequest[1], pResponse[0]);
  48.  
  49.             int nGot = pClient.Receive(pResponse, 2, SocketFlags.None);
  50.             Console.WriteLine("[R] Got: {0} <-- If 2 it's ok! ----| Response[1]: {1}\n", nGot, pResponse[1]);
  51.  
  52.             byte[] rawBytes;
  53.  
  54.             if (/*pResponse[1] == 0x02*/true)
  55.             {
  56.                 Console.WriteLine(" - We need username/password! 0x02!");
  57.                 nIndex = 0;
  58.                 pRequest[nIndex++] = 0x05; // Version 5.
  59.  
  60.                 // Add user name...
  61.                 pRequest[nIndex++] = (byte)userName.Length;
  62.                 rawBytes = System.Text.Encoding.Default.GetBytes(userName);
  63.                 rawBytes.CopyTo(pRequest, nIndex);
  64.                 nIndex += (ushort)rawBytes.Length;
  65.  
  66.                 // Add user password...
  67.                 pRequest[nIndex++] = (byte)userPass.Length;
  68.                 rawBytes = System.Text.Encoding.Default.GetBytes(userPass);
  69.                 rawBytes.CopyTo(pRequest, nIndex);
  70.                 nIndex += (ushort)rawBytes.Length;
  71.  
  72.                 // Send the Username/Password request
  73.                 pClient.Send(pRequest, nIndex, SocketFlags.None);
  74.  
  75.                 // Receive 2 byte response...
  76.                 nGot = pClient.Receive(pResponse, 2, SocketFlags.None);
  77.                 if (nGot != 2)
  78.                     Console.WriteLine("Bad response received from proxy server.");
  79.                 if (pResponse[1] != 0x00)
  80.                     Console.WriteLine("Bad Usernaem/Password. {0} <-- not 0x00, Response[0]: {1}, Response[2]: {2}", pResponse[0], pResponse[1], pResponse[2]);
  81.             }
  82.  
  83.             // Connection request...
  84.             Console.WriteLine("\n - Sending connection request...");
  85.             nIndex = 0;
  86.             pRequest[nIndex++] = 0x05;    // version 5.
  87.             pRequest[nIndex++] = 0x01;    // command = connect.
  88.             pRequest[nIndex++] = 0x00;    // Reserve = must be 0x00
  89.  
  90.             IPEndPoint dIpPort = new IPEndPoint(IPAddress.Parse(dServer), dPort);
  91.             IPAddress destIP = IPAddress.Parse(dServer);
  92.  
  93.  
  94.             // Address is IPV4 format
  95.             pRequest[nIndex++] = 0x01;
  96.             rawBytes = destIP.GetAddressBytes();
  97.             rawBytes.CopyTo(pRequest, nIndex);
  98.             Console.WriteLine("Index: {2}, IP Converted[First two numbers]: {0} Ip/Port: {1}", pRequest[nIndex], dIpPort, nIndex);
  99.             nIndex += (ushort)rawBytes.Length;
  100.             Console.WriteLine("Index: {0}", nIndex);
  101.  
  102.             // using big-edian byte order
  103.             byte[] portBytes = BitConverter.GetBytes(dPort);
  104.             for (int i = portBytes.Length - 1; i >= 0; i--)
  105.                 pRequest[nIndex++] = portBytes[i];
  106.  
  107.             // send connect request.
  108.             pClient.Send(pRequest, nIndex, SocketFlags.None);
  109.             pClient.Receive(pResponse);    // Get variable length response...
  110.             Console.WriteLine(" - Finishing...{0} {1}", pResponse[0], pResponse.Length);
  111.  
  112.             if (pResponse[1] != 0x00)
  113.             {
  114.                 Console.WriteLine("\nError: {0}", pResponse[1]);
  115.             }
  116.             else
  117.             {
  118.                 Console.WriteLine("\n * Connected. [{0}]", pResponse[1]);
  119.             }
  120.  
  121.             /* Success Connected...
  122.             Console.WriteLine("-!-   * Giving information to server..");
  123.             pClient.WriteLine("USER C-Sharp 8 * :I'm C# Experiment!");
  124.             pClient.WriteLine("NICK Lovely[M]");
  125.             pClient.WriteLine("JOIN #C-Sharp");
  126.             Console.WriteLine("-!-   * Completed.\n"); */
  127.  
  128.             //return pClient;
  129.  
  130.                 Console.WriteLine("\nRead line begins... {0}", nGot);
  131.                 Console.ReadLine();
  132.  
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement