2014-05-21 60 views
0

我有這樣的測試工具來嘗試提琴手核心我的工具每次啓動證書:我不得不刪除小提琴手產生對HTTPS流量

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); 

    } 

這個測試工具的工作,我可以捕獲HTTPS流量,這是我需要我的實際工具。 但是,每次工具啓動時,用戶都必須重新安裝並重新信任該證書。如果我不叫

 Fiddler.CertMaker.removeFiddlerGeneratedCerts(); 
每次

,則該工具將無法捕獲HTTPS流量,而我的工作監控停止應用程序,因爲很顯然,請求似乎得到由小提琴手攔截而不是路由到應用程序。

我該如何設置這個,所以我不必每次都刪除小提琴手證書?

+0

您是否在使用Fiddler'CertMaker.dll'加載項?如果是的話,你會在控制檯中看到一個參考。 – EricLaw

+0

是的,我有。就像我寫的,原則上一切正常,當我第一次啓動該工具時,我可以看到HTTPS流量很好。這只是我必須在每次啓動工具時刪除提琴手證書,否則它將停止工作。 http://i.imgur.com/qv3sif3.png – Hackworth

+0

FWIW,我遇到了同樣的問題,解決辦法是從項目中刪除certmaker.dll,並將makecert.exe放入應用程序的輸出文件夾中。這讓FiddlerCore可以創建一個可在應用程序啓動時使用的粘性證書,並允許您僅註冊一次證書(因此可以輕鬆添加安裝/卸載SSL選項)。 –

回答

1

您的應用程序文件夾中有CertMaker.dll,這意味着每次應用程序啓動時都會重新生成新的根證書和新的EE證書。

爲了防止這種情況,你需要緩存的喜好fiddler.certmaker.bc.keyfiddler.certmaker.bc.cert

或者刪除CertMaker.dll的值,並允許您使用默認makecert.exe證書的邏輯應用。

+0

你能解釋「你需要緩存首選項的值'fiddler.certmaker.bc.key'等?」?你在這裏談論什麼樣的偏好和關鍵? –

+0

命名值是首選項,存儲在Fiddler首選項系統中(這在您擁有的書中涵蓋:-)。這裏的問題是,基於FiddlerCore的應用程序不會自動將偏好保存到註冊表中(如Fiddler所做的那樣),因此您需要在CertMaker之後顯式地存儲指定首選項的值(在註冊表中,磁盤等上)生成根目錄,然後在應用程序下次啓動時將這些值重新加載到首選項系統中。 – EricLaw

+0

嗯......好吧,但是如果您使用MakeCert.exe而不是發貨組件,那麼您還可以獲得持久行爲,而無需手動管理密鑰。至少這就是我所看到的,這似乎是最簡單的解決方案,除非在Mono上運行或需要不支持MakeCert證書的特殊情況。 –

相關問題