コード例 #1
0
        public static void Main(string[] args)
        {
            if (args.Length < 1 || (args[0] != "client" && args[0] != "keygen"))
            {
                System.Console.WriteLine("Usage: proxy <server|client|keygen> [options...]");
                return;
            }

            switch (args[0])
            {
            case "client":
                if (args.Length < 4)
                {
                    Console.WriteLine("Usage: proxy client <keyPath> <port> <remoteHost> <remotePort>");
                    return;
                }
                break;
            }

            if (args[0] == "keygen")
            {
                Random r          = new Random();
                byte[] randomNums = new byte[16];
                r.NextBytes(randomNums);

                System.Console.WriteLine(BitConverter.ToString(randomNums).Replace("-", String.Empty));
                return;
            }


            var exitEvent = new ManualResetEvent(false);

            Console.CancelKeyPress += (sender, eventArgs) => {
                eventArgs.Cancel = true;
                exitEvent.Set();
            };

            StreamReader reader = new StreamReader(args[1]);

            byte[] key = Encoding.UTF8.GetBytes(reader.ReadToEnd());

            var Client = new TLSHandshakeClient(Int16.Parse(args[4]), args[3], Int16.Parse(args[2]), key);

            System.Console.WriteLine("Listening on port " + args[2]);
            Client.start();
        }
コード例 #2
0
        async void AsyncTcpProcess(object o)
        {
            TcpClient     tc     = (TcpClient)o;
            NetworkStream stream = tc.GetStream();

            byte[] outBuf       = new byte[1024];
            int    numBytesRead = 0;
            string ipAddr;
            int    port;

            byte[] buff;

            System.Console.WriteLine("Socket connection established");
            int fd = (int)tc.Client.Handle;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                addr_type peerAddr = get_peer_name(fd);
                if (peerAddr.port == -1)
                {
                    throw new Exception();
                }
                string ip           = TLSHandshakeClient.ParseIP(peerAddr.ip);
                var    originalAddr = TLSHandshakeClient.GetOriginalAddrMach(ip, peerAddr.port);
                ipAddr = originalAddr.ip;
                port   = originalAddr.port;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                addr_type originalAddr = get_original_addr(fd);

                if (originalAddr.port == -1)
                {
                    throw new Exception();
                }
                ipAddr = TLSHandshakeClient.ParseIP(originalAddr.ip);
                port   = originalAddr.port;
            }
            else
            {
                System.Console.WriteLine("OS Not supported!");
                return;
            }

            int nBytes = 0;

            using (var ms = new MemoryStream()) {
                while (stream.DataAvailable && (numBytesRead = stream.Read(outBuf, 0, outBuf.Length)) > 0)
                {
                    ms.Write(outBuf, 0, outBuf.Length);
                    nBytes += numBytesRead;
                    System.Console.WriteLine(numBytesRead);
                }
                buff = ms.ToArray();
            }
            System.Console.WriteLine(BitConverter.ToString(buff).Replace("-", " "));

            if (nBytes <= 0)
            {
                return;
            }

            if (buff[0] == 0x16) // TLS Handshake packet
            {
                System.Console.WriteLine("Handshake");
                var    rand = new Random();
                byte[] iv   = new byte[16];
                rand.NextBytes(iv);
                byte[] encryptedData = SecurityModule.AESDecrypt256(buff, this.key, iv);
                string bodyString    = $"{ipAddr}|{port}|{Convert.ToBase64String(encryptedData)}";
                byte[] encryptedInput, decryptedInput;
                encryptedInput = await PostData($"http://{remoteIpAddr}:{remotePort}/tls", bodyString);

                System.Console.WriteLine($"Received {encryptedInput.Length} bytes");

                decryptedInput = SecurityModule.AESDecrypt256(encryptedInput, key, iv);
                stream.Write(decryptedInput, 0, decryptedInput.Length);
            }
            else // IDK just bypass it
            {
                System.Console.WriteLine($"Normal Packet: Initiating connection to {ipAddr}:{port}");
                TcpClient     bypass       = new TcpClient(ipAddr, port);
                NetworkStream bypassStream = bypass.GetStream();

                System.Console.WriteLine($"Writing {nBytes} bytes to stream");
                bypassStream.Write(buff, 0, nBytes);

                while (bypassStream.DataAvailable && (numBytesRead = bypassStream.Read(outBuf, 0, outBuf.Length)) > 0)
                {
                    stream.Write(outBuf, 0, numBytesRead);
                    System.Console.WriteLine(bypassStream.DataAvailable);
                }
                System.Console.WriteLine("Normal Stream Closed");
            }
            stream.Flush();
            stream.Close();
            tc.Close();
        }