// ///////////////////////////// public void SaveSessionData(Session oS) { this.session = oS; RequestHeaders = this.session.RequestHeaders; ResponseHeaders = this.session.ResponseHeaders; }
public void UtilAssignResponse(string header, string body) { var requestHeaders = new HTTPResponseHeaders(); requestHeaders.AssignFromString(header); this.InnerSession.utilAssignResponse(requestHeaders, this.InnerSession.GetResponseBodyEncoding().GetBytes(body)); }
internal void ParseResponse() { _responseHeaders.Clear(); HTTPResponseHeaders headers = _session.oResponse.headers; foreach (HTTPHeaderItem item in headers) { _responseHeaders.Add(item.Name, item.Value); } string contentType = string.Empty; if (_responseHeaders.TryGetValue("content-type", out contentType) && !string.IsNullOrEmpty(contentType)) { _responseDataType = GetType(contentType); } }
protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) { var task = result as Task <HttpResponseMessage>; if (task.IsFaulted) { throw task.Exception.InnerException; } if (task.IsCanceled || context.IsCancellationRequested) { context.MarkCanceled(); return; } try { var response = task.Result; var requestSuccesful = response.StatusCode == HttpStatusCode.OK; var output = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); HTTPResponseStatus.Set(context, response.StatusCode); HTTPResponseBody.Set(context, output); HTTPResponseHeaders.Set(context, response.Headers); RequestSuccesful.Set(context, requestSuccesful); try { APIResponse deserializedAPIResponse = JsonConvert.DeserializeObject <APIResponse>(output); APIResponse.Set(context, deserializedAPIResponse); } catch (Exception) { } } catch (OperationCanceledException) { context.MarkCanceled(); } finally { if (task.Result != null) { task.Result.Dispose(); } } }
private bool isJson(HTTPResponseHeaders header) { if (header == null) { return(false); } if (header.Exists("Content-Type")) { var type = header["Content-Type"]; if (type.ToLower().Contains("json")) { return(true); } } return(false); }
/// <summary> /// CreateResponseHeaders returns a set of common response headers. /// </summary> /// <param name="tableUri">The table URI to include in the Location header. Null of empty if no location.</param> /// <returns></returns> private static HTTPResponseHeaders CreateResponseHeaders(string tableUri) { var headers = new HTTPResponseHeaders(); headers["Transfer-Encoding"] = "chunked"; headers["Date"] = DateTime.UtcNow.ToString("R"); headers["Cache-Control"] = "no-cache"; if (!string.IsNullOrEmpty(tableUri)) { headers["Location"] = tableUri; } headers["Content-Type"] = "application/atom+xml;charset=utf-8"; headers["x-ms-version"] = TableConstants.XMsVersion; headers["x-ms-request-id"] = Guid.Empty.ToString(); return(headers); }
/// <summary> /// The set response fiddler headers. /// </summary> /// <param name="response"> /// The response. /// </param> /// <param name="headers"> /// The headers. /// </param> public static void SetResponseFiddlerHeaders( this HttpWebResponseHolder response, HTTPResponseHeaders headers) { foreach (HTTPHeaderItem httpResponseHeader in headers) { try { // remove control char from header value httpResponseHeader.Value = Regex.Replace( httpResponseHeader.Value.Trim(), @"[\u0000-\u001F]", string.Empty); response.Headers[httpResponseHeader.Name] = httpResponseHeader.Value; } catch (ArgumentException ex) { Library.Logger.Logger.WriteWarning( ex, "control char hasn't been removed , header value {0}", httpResponseHeader.Value); } } }
public HttpHeaders(HTTPResponseHeaders hdrs, Session sess) { Headers = new Dictionary <string, string>(); foreach (var hdr in hdrs) { Headers[hdr.Name.ToLowerInvariant()] = hdr.Value; } using (var buffer = new MemoryStream()) { sess.WriteRequestToStream(true, true, buffer); buffer.Flush(); HeadersInOrder = Encoding.ASCII.GetString(buffer.ToArray()); } CacheControl = hdrs["Cache-Control"]; Connection = hdrs["Connection"]?.Split(';').Select(x => x.Trim()).ToArray(); ContentEncoding = hdrs["Content-Encoding"]; ContentLength = string.IsNullOrWhiteSpace(hdrs["Content-Length"]) ? (uint?)null : uint.Parse(hdrs["Content-Length"]); Expires = hdrs["Expires"]; Host = hdrs["Host"]; Pragma = hdrs["Pragma"]; ProxyConnection = hdrs["Proxy-Connection"]?.Split(';').Select(x => x.Trim()).ToArray(); Referer = hdrs["Referer"]; TransferEncoding = hdrs["Transfer-Encoding"]?.Split(';').Select(x => x.Trim()).ToArray(); }
public byte[] Decode(byte[] body, bool encoded, HTTPResponseHeaders headers) { if (encoded) { // decode body byte[] numArray = Utilities.Dupe(body); Utilities.utilDecodeHTTPBody(headers, ref numArray); body = numArray; } // get body as string string raw = Encoding.Default.GetString(body); // convert to json dynamic json = JsonConvert.DeserializeObject(raw); // decode and decompress the json DecodeInternal(json); // serialize json back to bytes body = Encoding.Default.GetBytes(JsonConvert.SerializeObject(json)); return(encoded ? Util.Compress(body) : body); }
private void AddSessionsToUI(Session[] oSessions) { int iCount = 0; foreach (Session oSession in oSessions) { try { if (!oSession.bHasResponse || oSession.HTTPMethodIs("CONNECT") || (oSession.responseBodyBytes.Length < 1)) { continue; } Asset thisAsset = new Asset(); thisAsset.oS = oSession; iCount++; HTTPResponseHeaders rhHeaders = oSession.ResponseHeaders; thisAsset.arrRaw = Utilities.Dupe(oSession.responseBodyBytes); if (oSession.ResponseHeaders.ExistsAndContains("Transfer-Encoding", "chunked")) { rhHeaders = (HTTPResponseHeaders)rhHeaders.Clone(); rhHeaders.Remove("Transfer-Encoding"); thisAsset.arrRaw = Utilities.doUnchunk(thisAsset.arrRaw); } thisAsset.cbServed = (uint)thisAsset.arrRaw.Length; Utilities.utilDecodeHTTPBody(rhHeaders, ref thisAsset.arrRaw); thisAsset.cbRaw = (uint)thisAsset.arrRaw.Length; ListViewItem oLVI = new ListViewItem(new[] { oSession.fullUrl, Utilities.TrimBefore(oSession.oResponse.MIMEType, '/'), thisAsset.cbRaw.ToString("N0"), thisAsset.cbServed.ToString("N0"), "-", "-", "-", "-" } ); if (thisAsset.cbServed < 1) { oLVI.ForeColor = Color.Gray; oLVI.Font = new Font(oLVI.Font, FontStyle.Strikeout); } thisAsset.sMIME = oSession.oResponse.MIMEType.ToLower(); if (thisAsset.sMIME.OICStartsWithAny("image/", "video/", "font/")) { oLVI.ForeColor = Color.Gray; } thisAsset.oLVI = oLVI; oLVI.Tag = thisAsset; lvQueue.Items.Add(oLVI); } catch (Exception eX) { MessageBox.Show(eX.Message + "\nSession #" + oSession.id.ToString() + ": " + oSession.fullUrl, "Failed Analysis"); } } if (iCount > 0) { lnkRecompute.Visible = true; } }
public JResponseHeaders(JSession session) { this.InnerHeaders = session.InnerSession.ResponseHeaders; }
private static List <Session> GetSessionsFromStreams(TCPStream tcpsClient, TCPStream tcpsServer) { StringBuilder stringBuilder = null; if (PacketCaptureImport.bVerboseDebug) { stringBuilder = new StringBuilder(); } if (stringBuilder != null) { stringBuilder.AppendFormat("Connection {0}\nREQUEST\n", tcpsClient.sStreamID); } MemoryStream payloadStream = tcpsClient.GetPayloadStream(stringBuilder, 0); if (stringBuilder != null) { stringBuilder.AppendFormat("\nRESPONSE\n", new object[0]); } MemoryStream memoryStream; if (tcpsServer != null) { memoryStream = tcpsServer.GetPayloadStream(stringBuilder, 0); } else { memoryStream = new MemoryStream(); } payloadStream.Position = 0L; memoryStream.Position = 0L; List <Session> list = new List <Session>(); int num = 0; bool flag; do { string text = null; SessionTimers sessionTimers = new SessionTimers(); sessionTimers.ClientConnected = tcpsClient.dtConnectStart; if (tcpsServer != null) { sessionTimers.ServerConnected = tcpsServer.dtConnectStart; } sessionTimers.ClientBeginRequest = tcpsClient.GetTimestampAtByte((int)payloadStream.Position); HTTPRequestHeaders hTTPRequestHeaders; byte[] array; flag = Parser.TakeRequest(payloadStream, out hTTPRequestHeaders, out array); sessionTimers.ClientDoneRequest = tcpsClient.GetTimestampAtByte((int)payloadStream.Position - 1); if (flag) { num++; HTTPResponseHeaders hTTPResponseHeaders = null; byte[] emptyByteArray = Utilities.emptyByteArray; bool flag2 = false; if (tcpsServer != null) { sessionTimers.ServerBeginResponse = tcpsServer.GetTimestampAtByte((int)memoryStream.Position); flag2 = Parser.TakeResponse(memoryStream, hTTPRequestHeaders.HTTPMethod, out hTTPResponseHeaders, out emptyByteArray); } if (flag2) { while (flag2 && hTTPResponseHeaders.HTTPResponseCode > 99 && hTTPResponseHeaders.HTTPResponseCode < 200) { text = text + "Eating a HTTP/" + hTTPResponseHeaders.HTTPResponseCode.ToString() + " message from the stream."; flag2 = Parser.TakeResponse(memoryStream, hTTPRequestHeaders.HTTPMethod, out hTTPResponseHeaders, out emptyByteArray); } sessionTimers.ServerDoneResponse = tcpsServer.GetTimestampAtByte((int)memoryStream.Position - 1); } else { hTTPResponseHeaders = Parser.ParseResponse("HTTP/1.1 0 FIDDLER GENERATED - RESPONSE DATA WAS MISSING\r\n\r\n"); } Session session = new Session(hTTPRequestHeaders, array); session.oResponse.headers = hTTPResponseHeaders; session.responseBodyBytes = emptyByteArray; session.oFlags["X-EgressPort"] = (session.oFlags["X-ClientPort"] = tcpsClient.tcpEndpoints.iSrcPort.ToString()); session.oFlags["X-ClientIP"] = tcpsClient.tcpEndpoints.addrSrc.ToString(); session.oFlags["X-HostIP"] = tcpsClient.tcpEndpoints.addrDst.ToString(); session.oFlags["X-HostPort"] = tcpsClient.tcpEndpoints.iDstPort.ToString(); if (!string.IsNullOrEmpty(text)) { session.oFlags["x-fiddler-streaming"] = text; } session.Timers = sessionTimers; if (num > 1) { session.UNSTABLE_SetBitFlag((SessionFlags)24, true); } string sProcessInfo = tcpsClient.sProcessInfo; if (!string.IsNullOrEmpty(sProcessInfo)) { session.oFlags["X-ProcessInfo"] = sProcessInfo; } if (stringBuilder != null) { session.oFlags["X-ConnectionDebugInfo"] = stringBuilder.ToString(); } list.Add(session); } }while (flag); return(list); }
public static Session[] GetHTTPSHandshakeFromStreams(TCPStream tcpsClient, TCPStream tcpsServer) { Session[] result; try { if (tcpsClient == null) { result = new Session[0]; } else { MemoryStream payloadStream = tcpsClient.GetPayloadStream(null, 1024); payloadStream.Position = 0L; MemoryStream memoryStream = null; if (tcpsServer != null) { memoryStream = tcpsServer.GetPayloadStream(null, 1024); memoryStream.Position = 0L; } string text = Utilities.UNSTABLE_DescribeClientHello(payloadStream); string text2 = "No server response was found"; if (memoryStream != null && memoryStream.Length > 0L) { text2 = Utilities.UNSTABLE_DescribeServerHello(memoryStream); } if (string.IsNullOrEmpty(text) && (text2 == null || text2.Length < 48)) { result = new Session[0]; } else { HTTPRequestHeaders hTTPRequestHeaders = new HTTPRequestHeaders(); hTTPRequestHeaders.HTTPMethod = "CONNECT"; hTTPRequestHeaders.RequestPath = string.Format("{0}:{1}", tcpsClient.tcpEndpoints.addrDst, tcpsClient.tcpEndpoints.iDstPort); hTTPRequestHeaders.Add("Host", hTTPRequestHeaders.RequestPath); hTTPRequestHeaders.Add("Fiddler-Import", "Packet capture contained HTTPS traffic. Parsing HTTPS Handshake to show this mock Session."); HTTPResponseHeaders hTTPResponseHeaders = new HTTPResponseHeaders(); hTTPResponseHeaders.SetStatus(200, "Emulated CONNECT Tunnel"); Session session = new Session(hTTPRequestHeaders, Encoding.UTF8.GetBytes(text)); session.oResponse.headers = hTTPResponseHeaders; session.responseBodyBytes = Encoding.UTF8.GetBytes(text2); session.oFlags["X-EgressPort"] = (session.oFlags["X-ClientPort"] = tcpsClient.tcpEndpoints.iSrcPort.ToString()); session.oFlags["X-ClientIP"] = tcpsClient.tcpEndpoints.addrSrc.ToString(); session.oFlags["X-HostIP"] = tcpsClient.tcpEndpoints.addrDst.ToString(); session.oFlags["X-HostPort"] = tcpsClient.tcpEndpoints.iDstPort.ToString(); session.Timers.ClientConnected = tcpsClient.dtConnectStart; session.Timers.ClientBeginRequest = tcpsClient.dtFirstPayload; session.Timers.FiddlerGotRequestHeaders = (session.Timers.FiddlerGotResponseHeaders = new DateTime(0L)); session.Timers.ServerConnected = tcpsServer.dtConnectStart; session.Timers.ServerBeginResponse = tcpsServer.dtFirstPayload; string sProcessInfo = tcpsClient.sProcessInfo; if (!string.IsNullOrEmpty(sProcessInfo)) { session.oFlags["X-ProcessInfo"] = sProcessInfo; } session.UNSTABLE_SetBitFlag((SessionFlags)1, true); result = new Session[] { session }; } } } catch (Exception) { result = new Session[0]; } return(result); }
private void FiddlerApplication_AfterSessionComplete(Session session) { // Ignore HTTPS connect requests if (session.RequestMethod == "CONNECT") { return; } if (session == null || session.oRequest == null || session.oRequest.headers == null) { return; } var full_url = session.fullUrl; Console.WriteLine("URL: " + full_url); HTTPRequestHeaders request_headers = session.RequestHeaders; HTTPResponseHeaders response_headers = session.ResponseHeaders; int http_response_code = response_headers.HTTPResponseCode; Console.WriteLine("HTTP Response: " + http_response_code.ToString()); string referer = null; Dictionary <String, HTTPHeaderItem> request_headers_dictionary = request_headers.ToDictionary(p => p.Name); if (request_headers_dictionary.ContainsKey("Referer")) { referer = request_headers_dictionary["Referer"].Value; } //foreach (HTTPHeaderItem header_item in response_headers) //{ // Console.Error.WriteLine(header_item.Name + " " + header_item.Value); //} //foreach (HTTPHeaderItem header_item in request_headers) //{ // Console.Error.WriteLine(header_item.Name + " " + header_item.Value); //} Console.Error.WriteLine("Referer: " + referer); var timers = session.Timers; var duration = (TimeSpan)(timers.ClientDoneResponse - timers.ClientBeginRequest); Console.Error.WriteLine(String.Format("Duration: {0:F10}", duration.Milliseconds)); var dic = new Dictionary <string, object>() { { "url", full_url }, { "status", http_response_code }, { "duration", duration.Milliseconds }, { "referer", referer } }; insert(dic); // https://groups.google.com/forum/#!msg/httpfiddler/RuFf5VzKCg0/wcgq-WeUnCoJ // the following code does not work as intended: request body is always blank //string request_body = session.GetRequestBodyAsString(); //if (!string.IsNullOrEmpty(request_body)) //{ // // TODO: UrlDecode // Console.Error.WriteLine(string.Join(Environment.NewLine, request_body.Split(new char[] { '&' }))); //} }
public bool LoadRules(string sFilename, bool bIsDefaultRuleFile) { this._orules.Clear(); this._orules2.Clear(); this.lstDuplicate.Clear(); if (bIsDefaultRuleFile) { FiddlerApplication.oAutoResponder.ClearRules(); } try { if (!File.Exists(sFilename) || (new FileInfo(sFilename).Length < 0x8fL)) { return(false); } FileStream input = new FileStream(sFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Dictionary <string, string> dictionary = new Dictionary <string, string>(); XmlTextReader reader = new XmlTextReader(input); while (reader.Read()) { string str; if (((reader.NodeType == XmlNodeType.Element) && ((str = reader.Name) != null)) && ((str != "State") && (str == "ResponseRule"))) { try { string attribute = reader.GetAttribute("Match"); string sAction = reader.GetAttribute("Action"); int iLatencyMS = 0; string s = reader.GetAttribute("Latency"); if (s != null) { iLatencyMS = XmlConvert.ToInt32(s); } bool bIsEnabled = "false" != reader.GetAttribute("Enabled"); string str5 = reader.GetAttribute("Headers"); if (string.IsNullOrEmpty(str5)) { FiddlerApplication.oAutoResponder.IsRuleListDirty = false; ResponderRule item = FiddlerApplication.oAutoResponder.AddRule(attribute, sAction, bIsEnabled); this._orules.Add(item); ResponderRuleExt ext = new ResponderRuleExt(); ext.arrResponseBytes = null; ext.strMatch = attribute; ext.strDescription = sAction; ext.bEnabled = bIsEnabled; ext.iLatencyMS = iLatencyMS; ext.oResponseHeaders = null; this._orules2.Add(ext); } else { byte[] buffer; HTTPResponseHeaders oRH = new HTTPResponseHeaders(); str5 = Encoding.UTF8.GetString(Convert.FromBase64String(str5)); oRH.AssignFromString(str5); string str6 = reader.GetAttribute("DeflatedBody"); if (!string.IsNullOrEmpty(str6)) { buffer = Utilities.DeflaterExpand(Convert.FromBase64String(str6)); } else { str6 = reader.GetAttribute("Body"); if (!string.IsNullOrEmpty(str6)) { buffer = Convert.FromBase64String(str6); } else { buffer = new byte[0]; } } FiddlerApplication.oAutoResponder.IsRuleListDirty = false; ResponderRule rule2 = FiddlerApplication.oAutoResponder.AddRule(attribute, oRH, buffer, sAction, iLatencyMS, bIsEnabled); this._orules.Add(rule2); ResponderRuleExt ext2 = new ResponderRuleExt(); ext2.arrResponseBytes = buffer; ext2.strMatch = attribute; ext2.strDescription = sAction; ext2.bEnabled = bIsEnabled; ext2.iLatencyMS = iLatencyMS; ext2.oResponseHeaders = oRH; this._orules2.Add(ext2); try { dictionary.Add(attribute, Guid.NewGuid().ToString()); } catch (Exception) { this.lstDuplicate.Add(attribute); } } continue; } catch { continue; } } } reader.Close(); return(true); } catch (Exception exception) { FiddlerApplication.ReportException(exception, "Failed to load AutoResponder settings from " + sFilename); return(false); } }