Hackworth
Hackworth

Reputation: 1109

I have to remove fiddler generated certs on every start of my tool for HTTPS traffic

I have this test tool to try out Fiddler Core:

    static void Main(string[] args)
    {
        #region AttachEventListeners
        //
        // It is important to understand that FiddlerCore calls event handlers on the
        // session-handling thread.  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.
        //

        // 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;
        };

        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"
            //oS.utilDecodeResponse(); oS.utilReplaceInResponse("Microsoft", "Bayden");
        };

        Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)
        {
            Console.WriteLine("Finished session:\t" + oS.fullUrl);
        };

        // Tell the system console to handle CTRL+C by calling our method that
        // gracefully shuts down the FiddlerCore.
        Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
        #endregion AttachEventListeners

        Console.WriteLine("Starting FiddlerCore...");

        // For the purposes of this demo, we'll forbid connections to HTTPS 
        // sites that use invalid certificates
        Fiddler.CONFIG.IgnoreServerCertErrors = true;
        Fiddler.CONFIG.bMITM_HTTPS = true;

        Fiddler.CertMaker.removeFiddlerGeneratedCerts();
        if (!Fiddler.CertMaker.rootCertExists())
        {
            if (!Fiddler.CertMaker.createRootCert())
            {
                throw new Exception("Unable to create cert for FiddlerCore.");
            }
        }

        if (!Fiddler.CertMaker.rootCertIsTrusted())
        {
            if (!Fiddler.CertMaker.trustRootCert())
            {
                throw new Exception("Unable to install FiddlerCore's cert.");
            }
        }

        // Because we've chosen to decrypt HTTPS traffic, makecert.exe must
        // be present in the Application folder.
        Fiddler.FiddlerApplication.Startup(8877, true, true);
        Console.WriteLine("Hit CTRL+C to end session.");

        // Wait Forever for the user to hit CTRL+C.  
        // BUG BUG: Doesn't properly handle shutdown of Windows, etc.
        Object forever = new Object();
        lock (forever)
        {
            System.Threading.Monitor.Wait(forever);
        }
    }

    /// <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>
    static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
        Console.WriteLine("Shutting down...");
        Fiddler.FiddlerApplication.Shutdown();
        System.Threading.Thread.Sleep(750);

    }

This test tool works, I can capture HTTPS traffic, which I need for my actual tool. However, the user has to re-install and re-trust the certificate every time the tool starts. If I do not call

        Fiddler.CertMaker.removeFiddlerGeneratedCerts();

every time, then the tool will not capture HTTPS traffic, and the app I'm monitoring stops working because apparently, the request seems to get intercepted by Fiddler but not routed to the app.

How do I have to set this up so I don't have to remove the fiddler cert every time?

Upvotes: 0

Views: 1418

Answers (1)

EricLaw
EricLaw

Reputation: 57075

You have the CertMaker.dll in your application's folder, which means that you're regenerating a new root and new EE certificates every time the application starts.

To prevent this, you need to cache the values of the preferences fiddler.certmaker.bc.key and fiddler.certmaker.bc.cert

Or remove CertMaker.dll and allow the default makecert.exe certificate logic to apply.

Upvotes: 1

Related Questions