private void ReadPackets(IAsyncResult ar)
        {
            ReadObj obj    = (ReadObj)ar.AsyncState;
            Socket  client = obj.s;

            byte[] buffer = obj.buffer;
            int    read   = -1;

            try
            {
                read = client.EndReceive(ar);
            }
            catch (Exception)
            {
                KillSocket(client, !stopping);
                ctx.LogMod.Log("[DISCONNECT] Client Disconnected from server", VLogger.LogLevel.information);
                return;
            }
            if (read == 0)
            {
                try { if (client.Connected)
                      {
                          client.BeginReceive(obj.buffer, 0, obj.buffer.Length, SocketFlags.None, new AsyncCallback(ReadPackets), obj);
                      }
                }
                catch (Exception e)
                {
                    KillSocket(client, !stopping);
                    Console.WriteLine("Client aborted session!" + Environment.NewLine + e.Message);
                }
                return;
            }

            string  text = Encoding.ASCII.GetString(buffer, 0, read);
            Request r;
            bool    sslHandlerStarted = false;

            if (obj.request != null)
            {
                if (obj.request.notEnded)
                {
                    string des = obj.request.full;
                    des += text;
                    r    = new Request(des);
                }
                else
                {
                    r = new Request(text);
                }
            }
            else
            {
                r = new Request(text);
            }

            if (!r.notEnded && !r.bogus)
            {
                ctx.LogMod.Log("<target> [HTTP]", VLogger.LogLevel.request, r);
                Tunnel t = new Tunnel(Tunnel.Mode.HTTP, httpMode, httpsMode, ctx, client, console);
                t.CreateMinimalTunnel(r);
                if (t.sslRead && httpMode == Mode.MITM) //Handle MITM SSL Connections
                {
                    string            host     = t.GetHost();
                    NetworkStream     clientNS = new NetworkStream(client);
                    VSslHandler       vsh      = new VSslHandler(ctx, console);
                    VSslHandler.Error errCode  = vsh.InitSslStream(clientNS, host);
                    if (errCode != VSslHandler.Error.Success)
                    {
                        ctx.LogMod.Log("Init SSL Stream failed\r\nError Code: " + errCode.ToString(), VLogger.LogLevel.error);
                    }
                    else
                    {
                        sslHandlerStarted = true;
                        vsh.InitAsyncRead();
                        console.Debug("SSL Tunnel MITM Started");
                        return;
                    }
                }
                else if (t.sslRead && httpsMode == Mode.forward) //Handle HTTPS normal
                {
                    t.InitHTTPS(client);
                    return;
                }

                if (httpMode == Mode.MITM) //Handle HTTP MITM
                {
                    Request httpSend = new Request(t.FormatRequest(r));
                    Tunnel.Send("", Tunnel.Mode.HTTP, ctx, httpSend, new NetworkStream(client));
                }
                else if (httpMode == Mode.forward) //Handle HTTP normal
                {
                    t.SendHTTP(r, client);
                    return;
                }
            }
            else if (r.notEnded)
            {
                obj.request = r;
            }
            Array.Clear(buffer, 0, buffer.Length);
            try { if (client.Connected && !sslHandlerStarted)
                  {
                      client.BeginReceive(obj.buffer, 0, obj.buffer.Length, SocketFlags.None, new AsyncCallback(ReadPackets), obj);
                  }
            }
            catch (Exception e)
            {
                KillSocket(client, !stopping);
                Console.WriteLine("Client aborted session!" + Environment.NewLine + e.Message);
            }
        }
Exemple #2
0
        private void ReadFromStream(IAsyncResult ar)
        {
            ReadObj ro        = (ReadObj)ar.AsyncState;
            Request r         = ro.r;
            int     bytesRead = 0;

            try { bytesRead = _ssl.EndRead(ar); }
            catch (Exception) { return; }
            byte[] read = new byte[bytesRead];
            Array.Copy(buffer, read, bytesRead);
            string text = Encoding.ASCII.GetString(read);

            if (bytesRead > 0)
            {
                if (r == null)
                {
                    r = new Request(text, true);
                }

                if (r.notEnded)
                {
                    if (ro.full == "")
                    {
                        ro.full = text;
                    }
                    else
                    {
                        ro.full += text;
                        r        = new Request(ro.full, true);
                    }
                }

                if (!r.notEnded && !r.bogus)
                {
                    if (ctx.mitmHttp.started)
                    {
                        ctx.mitmHttp.DumpRequest(r);
                    }

                    string requestString = r.Deserialize();

                    Tunnel.Send(requestString, Tunnel.Mode.HTTPs, ctx, r, null, this);
                    ro.full           = "";
                    ro.requestHandled = true;
                }
            }

            Array.Clear(buffer, 0, buffer.Length);
            if (!ro.requestHandled)
            {
                ro.r = r;
            }
            else
            {
                ro.r = null;
                ro.requestHandled = false;
            }
            try { _ssl.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadFromStream), ro); }
            catch (Exception ex)
            {
                //ctx.LogMod.Log("Ssl stream error MITM\r\n" + ex.Message, VLogger.LogLevel.error);
                Console.WriteLine("St: " + ex.StackTrace);
            }
        }