Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This demo program shows how to use the FiddlerCore library.
- *
- * Before compiling, ensure that the project's REFERENCES list points to the
- * copy of FiddlerCore4.dll included in this package.
- *
- * SESSION ARCHIVE (SAZ) SUPPORT
- * ===========
- * By default, the project is compiled without support for the SAZ File format.
- * If you want to add SAZ support, define the token SAZ_SUPPORT in the list of
- * Conditional Compilation symbols on the project's BUILD tab. You will also
- * need to add Ionic.Zip.Reduced.dll to your project's references, add the included
- * SAZ-DotNetZip.cs file to your code, and set
- *
- * FiddlerApplication.oSAZProvider = new DNZSAZProvider();
- *
- * in your startup code, as shown below.
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Threading;
- using Fiddler;
- using System.Threading.Tasks;
- namespace Demo
- {
- public class Program
- {
- public static bool filewtittebtodisk;
- public static bool insidetheprogram = true;
- public static int count = 0;
- public static Proxy oSecureEndpoint;
- public static string sSecureEndpointHostname = "localhost";
- public static int iSecureEndpointPort = 7777;
- public static List<Fiddler.Session> oAllSessions;
- public static void WriteCommandResponse(string s)
- {
- //ConsoleColor oldColor = Console.ForegroundColor;
- //Console.ForegroundColor = ConsoleColor.Yellow;
- //Console.WriteLine(s);
- //Console.ForegroundColor = oldColor;
- }
- public static string logpath;
- public static void DoQuit()
- {
- if (null != oSecureEndpoint) oSecureEndpoint.Dispose();
- Fiddler.FiddlerApplication.Shutdown();
- }
- public static string Ellipsize(string s, int iLen)
- {
- if (s.Length <= iLen) return s;
- return s.Substring(0, iLen - 3) + "...";
- }
- public Program()
- {
- logpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\PerformanceMonitorUtility";
- }
- //private void T_Tick(object sender, EventArgs e)
- //{
- // //if (count == 1)
- // //{
- // // string[] s = { "l" };
- // // Main(s);
- // // count = count + 1;
- // //}
- //}
- #if SAZ_SUPPORT
- public static void ReadSessions(List<Fiddler.Session> oAllSessions)
- {
- Session[] oLoaded = Utilities.ReadSessionArchive(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
- + Path.DirectorySeparatorChar + "ToLoad.saz", false);
- if ((oLoaded != null) && (oLoaded.Length > 0))
- {
- oAllSessions.AddRange(oLoaded);
- WriteCommandResponse("Loaded: " + oLoaded.Length + " sessions.");
- }
- }
- public static void SaveSessionsToDesktop(List<Fiddler.Session> oAllSessions)
- {
- bool bSuccess = false;
- if (!Directory.Exists(logpath + "\\Log\\NetworkLog"))
- {
- Directory.CreateDirectory(logpath + "\\Log\\NetworkLog");
- }
- string sFilename = logpath + "\\Log\\NetworkLog\\" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".saz";
- try
- {
- try
- {
- Monitor.Enter(oAllSessions);
- string sPassword = "ECWTECH";
- bSuccess = Utilities.WriteSessionArchive(sFilename, oAllSessions.ToArray(), sPassword, false);
- }
- finally
- {
- Monitor.Exit(oAllSessions);
- }
- //WriteCommandResponse(bSuccess ? ("Wrote: " + sFilename) : ("Failed to save: " + sFilename));
- }
- #pragma warning disable
- catch (Exception eX)
- {
- //Console.WriteLine("Save failed: " + eX.Message);
- }
- }
- #endif
- public static void WriteSessionList(List<Fiddler.Session> oAllSessions)
- {
- //ConsoleColor oldColor = Console.ForegroundColor;
- //Console.ForegroundColor = ConsoleColor.White;
- //Console.WriteLine("Session list contains...");
- try
- {
- Monitor.Enter(oAllSessions);
- //foreach (Session oS in oAllSessions)
- //{
- // //Console.Write(String.Format("{0} {1} {2}\n{3} {4}\n\n", oS.id, oS.oRequest.headers.HTTPMethod, Ellipsize(oS.fullUrl, 60), oS.responseCode, oS.oResponse.MIMEType));
- // File.AppendAllText(drv[0] + "//PerformanceUtility//flog.txt", oS.id + " " + oS.oRequest.headers.HTTPMethod + " " + Ellipsize(oS.fullUrl, 60) + " " + oS.responseCode + " " + oS.oResponse.MIMEType);
- //}
- }
- finally
- {
- Monitor.Exit(oAllSessions);
- //todo : max memory usage check and fix ---- added to check if memory can be reclaimed jaymin mod to check
- //oAllSessions.Clear();
- }
- //Console.WriteLine();
- //Console.ForegroundColor = oldColor;
- return;
- }
- public static void Main(string[] args)
- {
- oAllSessions = new List<Fiddler.Session>();
- // <-- Personalize for your Application, 64 chars or fewer
- //Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreDemoApp");
- #region AttachEventListeners
- //
- // It is important to understand that FiddlerCore calls event handlers on session-handling
- // background threads. If you need to properly synchronize to the UI-thread (say, because
- // you're adding the sessions to a list view) you must call .Invoke on a delegate on the
- // window handle.
- //
- // If you are writing to a non-threadsafe data structure (e.g. List<t>) you must
- // use a Monitor or other mechanism to ensure safety.
- //
- // Simply echo notifications to the console. Because Fiddler.CONFIG.QuietMode=true
- // by default, we must handle notifying the user ourselves.
- Fiddler.FiddlerApplication.OnNotification += delegate (object sender, NotificationEventArgs oNEA) { Console.WriteLine("** NotifyUser: " + oNEA.NotifyString); };
- Fiddler.FiddlerApplication.Log.OnLogString += delegate (object sender, LogEventArgs oLEA) { Console.WriteLine("** LogString: " + oLEA.LogString); };
- Fiddler.FiddlerApplication.BeforeRequest += delegate (Fiddler.Session oS)
- {
- // Console.WriteLine("Before request for:\t" + oS.fullUrl);
- // In order to enable response tampering, buffering mode MUST
- // be enabled; this allows FiddlerCore to permit modification of
- // the response in the BeforeResponse handler rather than streaming
- // the response to the client as the response comes in.
- oS.bBufferResponse = false;
- Monitor.Enter(oAllSessions);
- oAllSessions.Add(oS);
- Monitor.Exit(oAllSessions);
- // Set this property if you want FiddlerCore to automatically authenticate by
- // answering Digest/Negotiate/NTLM/Kerberos challenges itself
- // oS["X-AutoAuth"] = "(default)";
- /* If the request is going to our secure endpoint, we'll echo back the response.
- Note: This BeforeRequest is getting called for both our main proxy tunnel AND our secure endpoint,
- so we have to look at which Fiddler port the client connected to (pipeClient.LocalPort) to determine whether this request
- was sent to secure endpoint, or was merely sent to the main proxy tunnel (e.g. a CONNECT) in order to *reach* the secure endpoint.
- As a result of this, if you run the demo and visit https://localhost:7777 in your browser, you'll see
- Session list contains...
- 1 CONNECT http://localhost:7777
- 200 <-- CONNECT tunnel sent to the main proxy tunnel, port 8877
- 2 GET https://localhost:7777/
- 200 text/html <-- GET request decrypted on the main proxy tunnel, port 8877
- 3 GET https://localhost:7777/
- 200 text/html <-- GET request received by the secure endpoint, port 7777
- */
- if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (oS.hostname == sSecureEndpointHostname))
- {
- oS.utilCreateResponseAndBypassServer();
- oS.oResponse.headers.SetStatus(200, "Ok");
- oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
- oS.oResponse["Cache-Control"] = "private, max-age=0";
- oS.utilSetResponseBody("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());
- }
- };
- /*
- // The following event allows you to examine every response buffer read by Fiddler. Note that this isn't useful for the vast majority of
- // applications because the raw buffer is nearly useless; it's not decompressed, it includes both headers and body bytes, etc.
- //
- // This event is only useful for a handful of applications which need access to a raw, unprocessed byte-stream
- Fiddler.FiddlerApplication.OnReadResponseBuffer += new EventHandler<RawReadEventArgs>(FiddlerApplication_OnReadResponseBuffer);
- */
- /*
- Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS) {
- // Console.WriteLine("{0}:HTTP {1} for {2}", oS.id, oS.responseCode, oS.fullUrl);
- // Uncomment the following two statements to decompress/unchunk the
- // HTTP response and subsequently modify any HTTP responses to replace
- // instances of the word "Microsoft" with "Bayden". You MUST also
- // set bBufferResponse = true inside the beforeREQUEST method above.
- //
- //oS.utilDecodeResponse(); oS.utilReplaceInResponse("Microsoft", "Bayden");
- };*/
- Fiddler.FiddlerApplication.AfterSessionComplete += delegate (Fiddler.Session oS)
- {
- //Console.WriteLine("Finished session:\t" + oS.fullUrl);
- //Console.Title = ("Session list contains: " + oAllSessions.Count.ToString() + " sessions");
- };
- // Tell the system console to handle CTRL+C by calling our method that
- // gracefully shuts down the FiddlerCore.
- //
- // Note, this doesn't handle the case where the user closes the window with the close button.
- // See http://geekswithblogs.net/mrnat/archive/2004/09/23/11594.aspx for info on that...
- //
- Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
- #endregion AttachEventListeners
- string sSAZInfo = "NoSAZ";
- #if SAZ_SUPPORT
- sSAZInfo = Assembly.GetAssembly(typeof(Ionic.Zip.ZipFile)).FullName;
- // You can load Transcoders from any different assembly if you'd like, using the ImportTranscoders(string AssemblyPath)
- // overload.
- //
- //if (!FiddlerApplication.oTranscoders.ImportTranscoders(Assembly.GetExecutingAssembly()))
- //{
- // Console.WriteLine("This assembly was not compiled with a SAZ-exporter");
- //}
- DNZSAZProvider.fnObtainPwd = () =>
- {
- //Console.WriteLine("Enter the password (or just hit Enter to cancel):");
- string sResult = Console.ReadLine();
- //Console.WriteLine();
- return sResult;
- };
- FiddlerApplication.oSAZProvider = new DNZSAZProvider();
- //DNZSAZWriter dz = new DNZSAZWriter("abc.saz");
- //StreamWriter s = new StreamWriter("abc.saz");
- //SAZWriterDelegate sw =null;
- //sw += filetowrite;
- //dz.AddFile("abc.saz", sw);
- //dz.CompleteArchive();
- #endif
- //Console.WriteLine(String.Format("Starting {0} ({1})...", Fiddler.FiddlerApplication.GetVersionString(), sSAZInfo));
- // For the purposes of this demo, we'll forbid connections to HTTPS
- // sites that use invalid certificates. Change this from the default only
- // if you know EXACTLY what that implies.
- Fiddler.CONFIG.IgnoreServerCertErrors = false;
- // ... but you can allow a specific (even invalid) certificate by implementing and assigning a callback...
- // FiddlerApplication.OnValidateServerCertificate += new System.EventHandler<ValidateServerCertificateEventArgs>(CheckCert);
- FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);
- // NOTE: In the next line, you can pass 0 for the port (instead of 8877) to have FiddlerCore auto-select an available port
- ushort iPort = 8877;
- FiddlerCoreStartupSettings startupSettings =
- new FiddlerCoreStartupSettingsBuilder()
- .ListenOnPort(iPort)
- .RegisterAsSystemProxy()
- .DecryptSSL()
- //.AllowRemoteClients()
- //.ChainToUpstreamGateway()
- .MonitorAllConnections()
- //.HookUsingPACFile()
- //.CaptureLocalhostTraffic()
- //.CaptureFTP()
- .OptimizeThreadPool()
- //.SetUpstreamGatewayTo("http=CorpProxy:80;https=SecureProxy:443;ftp=ftpGW:20")
- .Build();
- //FiddlerApplication.oSAZProvider = new DNZSAZProvider();
- // *******************************
- // Important HTTPS Decryption Info
- // *******************************
- // When FiddlerCoreStartupSettingsBuilder.DecryptSSL() is called, you must include either
- //
- // MakeCert.exe
- //
- // *or*
- //
- // CertMaker.dll
- // BCMakeCert.dll
- //
- // ... in the folder where your executable and FiddlerCore.dll live. These files
- // are needed to generate the self-signed certificates used to man-in-the-middle
- // secure traffic. MakeCert.exe uses Windows APIs to generate certificates which
- // are stored in the user's \Personal\ Certificates store. These certificates are
- // NOT compatible with iOS devices which require specific fields in the certificate
- // which are not set by MakeCert.exe.
- //
- // In contrast, CertMaker.dll uses the BouncyCastle C# library (BCMakeCert.dll) to
- // generate new certificates from scratch. These certificates are stored in memory
- // only, and are compatible with iOS devices.
- Fiddler.FiddlerApplication.Startup(startupSettings);
- FiddlerApplication.Log.LogFormat("Created endpoint listening on port {0}", iPort);
- FiddlerApplication.Log.LogFormat("Gateway: {0}", CONFIG.UpstreamGateway.ToString());
- //Console.WriteLine("Hit CTRL+C to end session.");
- // We'll also create a HTTPS listener, useful for when FiddlerCore is masquerading as a HTTPS server
- // instead of acting as a normal CERN-style proxy server.
- oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);
- if (null != oSecureEndpoint)
- {
- FiddlerApplication.Log.LogFormat("Created secure endpoint listening on port {0}, using a HTTPS certificate for '{1}'", iSecureEndpointPort, sSecureEndpointHostname);
- }
- // bool bDone = false;
- // do
- // {
- // Console.WriteLine("\nEnter a command [C=Clear; L=List; G=Collect Garbage; W=write SAZ; R=read SAZ;\n\tS=Toggle Forgetful Streaming; T=Trust Root Certificate; Q=Quit]:");
- // Console.Write(">");
- // //ConsoleKeyInfo cki = Console.ReadKey();
- // //Console.WriteLine();
- // switch (Char.ToLower(Convert.ToChar(args[0])))
- // {
- // case 'c':
- // Monitor.Enter(oAllSessions);
- // oAllSessions.Clear();
- // Monitor.Exit(oAllSessions);
- // WriteCommandResponse("Clear...");
- // FiddlerApplication.Log.LogString("Cleared session list.");
- // break;
- // case 'd':
- // FiddlerApplication.Log.LogString("FiddlerApplication::Shutdown.");
- // FiddlerApplication.Shutdown();
- // break;
- // case 'l':
- // WriteSessionList(oAllSessions);
- // bDone = true;
- // break;
- // case 'g':
- // Console.WriteLine("Working Set:\t" + Environment.WorkingSet.ToString("n0"));
- // Console.WriteLine("Begin GC...");
- // GC.Collect();
- // Console.WriteLine("GC Done.\nWorking Set:\t" + Environment.WorkingSet.ToString("n0"));
- // break;
- // case 'q':
- // bDone = true;
- // DoQuit();
- // break;
- // case 'r':
- //#if SAZ_SUPPORT
- // ReadSessions(oAllSessions);
- //#else
- // WriteCommandResponse("This demo was compiled without SAZ_SUPPORT defined");
- //#endif
- // break;
- // case 'w':
- //#if SAZ_SUPPORT
- // if (oAllSessions.Count > 0)
- // {
- // SaveSessionsToDesktop(oAllSessions);
- // }
- // else
- // {
- // WriteCommandResponse("No sessions have been captured");
- // }
- //#else
- // WriteCommandResponse("This demo was compiled without SAZ_SUPPORT defined");
- //#endif
- // break;
- // case 't':
- // try
- // {
- // WriteCommandResponse("Result: " + Fiddler.CertMaker.trustRootCert().ToString());
- // }
- // catch (Exception eX)
- // {
- // WriteCommandResponse("Failed: " + eX.ToString());
- // }
- // break;
- // // Forgetful streaming
- // case 's':
- // bool bForgetful = !FiddlerApplication.Prefs.GetBoolPref("fiddler.network.streaming.ForgetStreamedData", false);
- // FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.ForgetStreamedData", bForgetful);
- // Console.WriteLine(bForgetful ? "FiddlerCore will immediately dump streaming response data." : "FiddlerCore will keep a copy of streamed response data.");
- // break;
- // }
- // } while (!bDone);
- }
- public void GetInput(char c)
- {
- bool bDone = false;
- do
- {
- //Console.WriteLine("\nEnter a command [C=Clear; L=List; G=Collect Garbage; W=write SAZ; R=read SAZ;\n\tS=Toggle Forgetful Streaming; T=Trust Root Certificate; Q=Quit]:");
- //Console.Write(">");
- //ConsoleKeyInfo cki = Console.ReadKey();
- //Console.WriteLine();
- switch (c)
- {
- case 'c':
- //Monitor.Enter(oAllSessions);
- //oAllSessions.Clear();
- //Monitor.Exit(oAllSessions);
- //WriteCommandResponse("Clear...");
- //FiddlerApplication.Log.LogString("Cleared session list.");
- break;
- case 'd':
- //FiddlerApplication.Log.LogString("FiddlerApplication::Shutdown.");
- //FiddlerApplication.Shutdown();
- break;
- case 'l':
- try
- {
- var gettrustcertval = Fiddler.CertMaker.trustRootCert();
- if (gettrustcertval == false)
- {
- Fiddler.CertMaker.trustRootCert();
- }
- }
- catch (Exception ex)
- {
- //MessageBox.Show(ex.ToString());
- }
- WriteSessionList(oAllSessions);
- return;
- case 'g':
- //Console.WriteLine("Working Set:\t" + Environment.WorkingSet.ToString("n0"));
- //Console.WriteLine("Begin GC...");
- //GC.Collect();
- //Console.WriteLine("GC Done.\nWorking Set:\t" + Environment.WorkingSet.ToString("n0"));
- break;
- case 'q':
- bDone = true;
- DoQuit();
- break;
- case 'r':
- #if SAZ_SUPPORT
- ReadSessions(oAllSessions);
- #else
- #endif
- break;
- case 'w':
- #if SAZ_SUPPORT
- if (oAllSessions.Count > 0)
- {
- //SaveSessionsToDesktop(oAllSessions);
- //Task t = new Task(() => SaveSessionsToDesktop(oAllSessions));
- //Task t = new Task(() => SaveSessionsToDesktop(oAllSessions));
- Task.Factory.StartNew(() => SaveSessionsToDesktop(oAllSessions)).ContinueWith(tsk => filewtittebtodisk = true);
- //ContinueWith(u=> insidetheprogram=false).ContinueWith(x => DoQuit());
- //t.Start();
- }
- else
- {
- //todo while updating the app check once if you can update the app with 0 status of count
- //filewtittebtodisk = true
- WriteCommandResponse("No sessions have been captured");
- }
- #else
- WriteCommandResponse("This demo was compiled without SAZ_SUPPORT defined");
- #endif
- return;
- case 't':
- try
- {
- WriteCommandResponse("Result: " + Fiddler.CertMaker.trustRootCert().ToString());
- }
- catch (Exception eX)
- {
- WriteCommandResponse("Failed: " + eX.ToString());
- }
- break;
- // Forgetful streaming
- case 's':
- bool bForgetful = !FiddlerApplication.Prefs.GetBoolPref("fiddler.network.streaming.ForgetStreamedData", false);
- FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.ForgetStreamedData", bForgetful);
- Console.WriteLine(bForgetful ? "FiddlerCore will immediately dump streaming response data." : "FiddlerCore will keep a copy of streamed response data.");
- break;
- }
- } while (!bDone);
- }
- /*
- /// <summary>
- /// This callback allows your code to evaluate the certificate for a site and optionally override default validation behavior for that certificate.
- /// You should not implement this method unless you understand why it is a security risk.
- /// </summary>
- static void CheckCert(object sender, ValidateServerCertificateEventArgs e)
- {
- if (null != e.ServerCertificate)
- {
- Console.WriteLine("Certificate for " + e.ExpectedCN + " was for site " + e.ServerCertificate.Subject + " and errors were " + e.CertificatePolicyErrors.ToString());
- if (e.ServerCertificate.Subject.Contains("fiddler2.com"))
- {
- Console.WriteLine("Got a certificate for fiddler2.com. We'll say this is also good for any other site, like https://fiddlertool.com.");
- e.ValidityState = CertificateValidity.ForceValid;
- }
- }
- }
- */
- //public static void filetowrite(Stream s)
- //{
- // byte[] b = null;
- // s.Write(b, 0, 0);
- //}
- /*
- // This event handler is called on every socket read for the HTTP Response. You almost certainly don't want
- // to add a handler for this event, but the code below shows how you can use it to mess up your HTTP traffic.
- static void FiddlerApplication_OnReadResponseBuffer(object sender, RawReadEventArgs e)
- {
- // NOTE: arrDataBuffer is a fixed-size array. Only bytes 0 to iCountOfBytes should be read/manipulated.
- //
- // Just for kicks, lowercase every byte. Note that this will obviously break any binary content.
- for (int i = 0; i < e.iCountOfBytes; i++)
- {
- if ((e.arrDataBuffer[i] > 0x40) && (e.arrDataBuffer[i] < 0x5b))
- {
- e.arrDataBuffer[i] = (byte)(e.arrDataBuffer[i] + (byte)0x20);
- }
- }
- Console.WriteLine(String.Format("Read {0} response bytes for session {1}", e.iCountOfBytes, e.sessionOwner.id));
- }
- */
- /// <summary>
- /// When the user hits CTRL+C, this event fires. We use this to shut down and unregister our FiddlerCore.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
- {
- DoQuit();
- }
- }
- }
Add Comment
Please, Sign In to add comment