public Response(int _statusCode, string _httpMessage, string _version, VDictionary _headers, string _body, byte[] fullBytes, VConsole con, VMitm mitmHttp) { statusCode = _statusCode; httpMessage = _httpMessage; version = _version; bodyText = _body; body = fullBytes; console = con; mitm = mitmHttp; headers = _headers; }
public void Serialize(bool fromSslStream = false) { if (full == "") { bogus = true; return; } if (!full.EndsWith("\r\n\r\n") && fromSslStream) { notEnded = true; //setting only when requests are marked to allow normal (not MITM) https packets even if they are not ending with \r\n\r\n } try { string infoLine = full.Split('\n')[0].Replace("\r", String.Empty); string[] iParts = infoLine.Split(' '); method = iParts[0]; target = iParts[1]; version = iParts[2]; headers = new VDictionary(); string[] data = full.Split('\n'); bool isBody = false; string nl = Environment.NewLine; for (int i = 1; i < data.Length; i++) { string line = data[i].Replace("\r", String.Empty); if (line == "") { isBody = true; continue; } if (!isBody) { //Add headers string hName = line.Substring(0, line.IndexOf(':')); string hValue = line.Substring(line.IndexOf(':') + 2, line.Length - line.IndexOf(':') - 2); headers.Add(hName, hValue); } else { if ((i + 1) < data.Length) { htmlBody += line + nl; } else if ((i + 1) == data.Length) { htmlBody += line; } } } //Add ssl packet filter if (!version.Contains("HTTP")) { bogus = true; } } catch (Exception) { bogus = true; } }
private static void BISend(Request r, NetworkStream ns, VSslHandler vSsl, Mode Protocol, Form1 ctx) { Task getPage = new Task(new Action(() => { if (ctx.mitmHttp.started) { ctx.mitmHttp.DumpRequest(r); } string hostString = r.headers["Host"]; string target = r.target.Replace(hostString, string.Empty); if (Protocol == Tunnel.Mode.HTTPs) { hostString = "https://" + hostString + target; } else { hostString = "http://" + hostString + target; } HttpClientHandler handler = new HttpClientHandler() { UseProxy = false, Proxy = null }; HttpClient client = new HttpClient(handler); HttpRequestMessage hrm = new HttpRequestMessage { Method = new HttpMethod(r.method), RequestUri = new Uri(hostString) }; foreach (KeyValuePair <string, string> kvp in r.headers.Items) { hrm.Headers.Add(kvp.Key, kvp.Value); } if (r.htmlBody != null) { hrm.Content = new StringContent(r.htmlBody); } client.SendAsync(hrm).ContinueWith(responseTask => { try { HttpResponseMessage resp = responseTask.Result; byte[] content = new byte[0]; string strContent = ""; int statusCode = 0; string statusDescription = ""; string version = ""; VDictionary headers = new VDictionary(); Task getContent = new Task(() => { content = resp.Content.ReadAsByteArrayAsync().Result; foreach (KeyValuePair <string, IEnumerable <string> > x in resp.Content.Headers) { string name = x.Key; if (name == "Content-Length") { ctx.ConMod.Debug("Got content length"); } string value = ""; foreach (string val in x.Value) { value += val + ";"; } value = value.Substring(0, value.Length - 1); headers.Add(name, value); } ctx.ConMod.Debug("Headers in content" + resp.Content.Headers.Count()); strContent = Encoding.ASCII.GetString(content); }); Task getHeaders = new Task(() => { foreach (KeyValuePair <string, IEnumerable <string> > x in resp.Headers) { string name = x.Key; string value = ""; foreach (string val in x.Value) { value += val + ";"; } value = value.Substring(0, value.Length - 1); headers.Add(name, value); } }); Task getRest = new Task(() => { statusCode = (int)resp.StatusCode; statusDescription = resp.ReasonPhrase; version = "HTTP/" + resp.Version.ToString(); }); getContent.Start(); getHeaders.Start(); getRest.Start(); Task.WaitAll(getContent, getHeaders, getRest); Response _r = new Response(statusCode, statusDescription, version, headers, strContent, content, ctx.ConMod, ctx.mitmHttp); _r.SetManager(ctx.vf); _r.BindFilter("resp_mime", "mime_white_list"); _r.BindFilter("resp_mime_block", "mime_skip_list"); _r.CheckMimeAndSetBody(); if (ctx.mitmHttp.started) { string _target = r.target; if (_target.Contains("?")) { _target = _target.Substring(0, _target.IndexOf("?")); } ctx.mitmHttp.DumpResponse(_r, _target); } //ConMod.Debug("Before sending to client"); if (Protocol == Tunnel.Mode.HTTPs) { _r.Deserialize(null, r, vSsl); } else { _r.Deserialize(ns, r); } } catch (Exception) { //ctx.ConMod.Debug("Error: " + ex.ToString() + "\r\nStackTrace:\r\n" + ex.StackTrace); //ctx.ConMod.Debug($"On resource: {r.target}"); } }); })); getPage.Start(); }