官方版

Build Cookie Scanning Extension

Below is the code for the Fiddler Privacy Scanner add-on.

  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Collections.Generic;
  5. using System.Windows.Forms;
  6. using System.Text;
  7. using Fiddler;
  8. using System.IO;
  9. using System.Diagnostics;
  10. using Microsoft.Win32;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. [assembly: Fiddler.RequiredVersion("2.3.9.0")]
  14. [assembly: AssemblyVersion("1.0.1.0")]
  15. [assembly: AssemblyTitle("PrivacyScanner")]
  16. [assembly: AssemblyDescription("Scans for Cookies and P3P")]
  17. [assembly: AssemblyCompany("Eric Lawrence")]
  18. [assembly: AssemblyProduct("PrivacyScanner")]
  19. public class TagCookies : IAutoTamper2
  20. {
  21. private bool bEnabled = false;
  22. private bool bEnforceP3PValidity = false;
  23. private bool bCreatedColumn = false;
  24. private System.Windows.Forms.MenuItem miEnabled;
  25. private System.Windows.Forms.MenuItem miEnforceP3PValidity;
  26. private System.Windows.Forms.MenuItem mnuCookieTag;
  27. public void OnLoad()
  28. {
  29. /*
  30. * NB: You might not get called here until ~after~ one of the AutoTamper methods was called.
  31. * This is okay for us, because we created our mnuContentBlock in the constructor and its simply not
  32. * visible anywhere until this method is called and we merge it onto the Fiddler Main menu.
  33. */
  34. FiddlerApplication.UI.mnuMain.MenuItems.Add(mnuCookieTag);
  35. }
  36. public void OnBeforeUnload() { /*noop*/ }
  37. private void InitializeMenu()
  38. {
  39. this.miEnabled = new System.Windows.Forms.MenuItem("&Enabled");
  40. this.miEnforceP3PValidity = new System.Windows.Forms.MenuItem("&Rename P3P header if invalid");
  41. this.miEnabled.Index = 0;
  42. this.miEnforceP3PValidity.Index = 1;
  43. this.mnuCookieTag = new System.Windows.Forms.MenuItem();
  44. this.mnuCookieTag.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.miEnabled, this.miEnforceP3PValidity });
  45. this.mnuCookieTag.Text = "Privacy";
  46. this.miEnabled.Click += new System.EventHandler(this.miEnabled_Click);
  47. this.miEnabled.Checked = bEnabled;
  48. this.miEnforceP3PValidity.Click += new System.EventHandler(this.miEnforceP3PValidity_Click);
  49. this.miEnforceP3PValidity.Checked = bEnforceP3PValidity;
  50. }
  51. public void miEnabled_Click(object sender, EventArgs e)
  52. {
  53. miEnabled.Checked = !miEnabled.Checked;
  54. bEnabled = miEnabled.Checked;
  55. this.miEnforceP3PValidity.Enabled = bEnabled;
  56. if (bEnabled) { EnsureColumn(); }
  57. FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.enabled", bEnabled);
  58. }
  59. public void miEnforceP3PValidity_Click(object sender, EventArgs e)
  60. {
  61. miEnforceP3PValidity.Checked = !miEnforceP3PValidity.Checked;
  62. bEnforceP3PValidity = miEnforceP3PValidity.Checked;
  63. FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.EnforceP3PValidity", bEnforceP3PValidity);
  64. }
  65. private void EnsureColumn()
  66. {
  67. if (bCreatedColumn) return;
  68. FiddlerApplication.UI.lvSessions.AddBoundColumn("Privacy Info", 1, 120, "X-Privacy");
  69. bCreatedColumn = true;
  70. }
  71. public TagCookies()
  72. {
  73. this.bEnabled = FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.enabled", false);
  74. this.bEnforceP3PValidity = FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.EnforceP3PValidity", true);
  75. InitializeMenu();
  76. if (bEnabled) { EnsureColumn(); } else { this.miEnforceP3PValidity.Enabled = false; }
  77. }
  78. private void SetP3PStateFromHeader(string sValue, ref P3PState oP3PState)
  79. {
  80. if (string.IsNullOrEmpty(sValue))
  81. {
  82. return;
  83. }
  84. string sUnsatCat = String.Empty;
  85. string sUnsatPurpose = String.Empty;
  86. sValue = sValue.Replace('\'', '"');
  87. string sCP = null;
  88. Regex r = new Regex("CP\\s?=\\s?[\"]?(?<TokenValue>[^\";]*)");
  89. Match m = r.Match(sValue);
  90. if (m.Success && (null != m.Groups["TokenValue"]))
  91. {
  92. sCP = m.Groups["TokenValue"].Value;
  93. }
  94. if (String.IsNullOrEmpty(sCP))
  95. {
  96. return;
  97. }
  98. // Okay, we've got a compact policy token.
  99. oP3PState = P3PState.P3POk;
  100. string[] sTokens = sCP.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  101. foreach (string sToken in sTokens)
  102. {
  103. // Reject clearly invalid tokens...
  104. if ((sToken.Length < 3) || (sToken.Length > 4))
  105. {
  106. oP3PState = P3PState.P3PMalformed;
  107. return;
  108. }
  109. if (",PHY,ONL,GOV,FIN,".IndexOf("," + sToken + ",", StringComparison.OrdinalIgnoreCase) > -1)
  110. {
  111. sUnsatCat += (sToken + " ");
  112. continue;
  113. }
  114. if (",SAM,OTR,UNR,PUB,IVA,IVD,CON,TEL,OTP,".IndexOf("," + sToken + ",", StringComparison.OrdinalIgnoreCase) > -1)
  115. {
  116. sUnsatPurpose += (sToken + " ");
  117. continue;
  118. }
  119. // TODO: Look up the token in the complete collection and check validity
  120. }
  121. // If a cookie contains an unsatisfactory purpose and an unsatisfactory category, mark it
  122. // https://msdn.microsoft.com/en-us/library/ie/ms537343(v=vs.85).aspx#unsatisfactory_cookies
  123. if ((sUnsatCat.Length > 0) && (sUnsatPurpose.Length > 0))
  124. {
  125. if (oP3PState == P3PState.P3POk)
  126. {
  127. oP3PState = P3PState.P3PUnsatisfactory;
  128. }
  129. }
  130. }
  131. private enum P3PState
  132. {
  133. NoCookies,
  134. NoP3PAndSetsCookies,
  135. P3POk,
  136. P3PUnsatisfactory,
  137. P3PMalformed
  138. }
  139. public void OnPeekAtResponseHeaders(Session oSession)
  140. {
  141. if (!bEnabled) return;
  142. P3PState oP3PState = P3PState.NoCookies;
  143. if (!oSession.oResponse.headers.Exists("Set-Cookie"))
  144. {
  145. return;
  146. }
  147. oP3PState = P3PState.NoP3PAndSetsCookies;
  148. if (oSession.oResponse.headers.Exists("P3P"))
  149. {
  150. SetP3PStateFromHeader(oSession.oResponse.headers["P3P"], ref oP3PState);
  151. }
  152. switch (oP3PState)
  153. {
  154. case P3PState.P3POk:
  155. oSession["ui-backcolor"] = "#ACDC85";
  156. oSession["X-Privacy"] = "Sets cookies & P3P";
  157. break;
  158. case P3PState.NoP3PAndSetsCookies:
  159. oSession["ui-backcolor"] = "#FAFDA4";
  160. oSession["X-Privacy"] = "Sets cookies without P3P";
  161. break;
  162. case P3PState.P3PUnsatisfactory:
  163. oSession["ui-backcolor"] = "#EC921A";
  164. oSession["X-Privacy"] = "Sets cookies; P3P unsatisfactory for 3rd-party use";
  165. break;
  166. case P3PState.P3PMalformed:
  167. oSession["ui-backcolor"] = "#E90A05";
  168. if (bEnforceP3PValidity)
  169. {
  170. oSession.oResponse.headers["MALFORMED-P3P"] = oSession.oResponse.headers["P3P"];
  171. oSession["X-Privacy"] = "MALFORMED P3P: " + oSession.oResponse.headers["P3P"];
  172. oSession.oResponse.headers.Remove("P3P");
  173. }
  174. break;
  175. }
  176. }
  177. public void AutoTamperRequestBefore(Session oSession) { }
  178. public void AutoTamperRequestAfter(Session oSession){ /*noop*/ }
  179. public void AutoTamperResponseAfter(Session oSession) {/*noop*/}
  180. public void AutoTamperResponseBefore(Session oSession) { /*noop*/ }
  181. public void OnBeforeReturningError(Session oSession) {/*noop*/}
  182. }

谷歌翻译版

建立Cookie扫描扩展

以下是Fiddler Privacy Scanner加载项的代码。

  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Collections.Generic;
  5. using System.Windows.Forms;
  6. using System.Text;
  7. using Fiddler;
  8. using System.IO;
  9. using System.Diagnostics;
  10. using Microsoft.Win32;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. [assembly: Fiddler.RequiredVersion("2.3.9.0")]
  14. [assembly: AssemblyVersion("1.0.1.0")]
  15. [assembly: AssemblyTitle("PrivacyScanner")]
  16. [assembly: AssemblyDescription("Scans for Cookies and P3P")]
  17. [assembly: AssemblyCompany("Eric Lawrence")]
  18. [assembly: AssemblyProduct("PrivacyScanner")]
  19. public class TagCookies : IAutoTamper2
  20. {
  21. private bool bEnabled = false;
  22. private bool bEnforceP3PValidity = false;
  23. private bool bCreatedColumn = false;
  24. private System.Windows.Forms.MenuItem miEnabled;
  25. private System.Windows.Forms.MenuItem miEnforceP3PValidity;
  26. private System.Windows.Forms.MenuItem mnuCookieTag;
  27. public void OnLoad()
  28. {
  29. /*
  30. * NB: You might not get called here until ~after~ one of the AutoTamper methods was called.
  31. * This is okay for us, because we created our mnuContentBlock in the constructor and its simply not
  32. * visible anywhere until this method is called and we merge it onto the Fiddler Main menu.
  33. */
  34. FiddlerApplication.UI.mnuMain.MenuItems.Add(mnuCookieTag);
  35. }
  36. public void OnBeforeUnload() { /*noop*/ }
  37. private void InitializeMenu()
  38. {
  39. this.miEnabled = new System.Windows.Forms.MenuItem("&Enabled");
  40. this.miEnforceP3PValidity = new System.Windows.Forms.MenuItem("&Rename P3P header if invalid");
  41. this.miEnabled.Index = 0;
  42. this.miEnforceP3PValidity.Index = 1;
  43. this.mnuCookieTag = new System.Windows.Forms.MenuItem();
  44. this.mnuCookieTag.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.miEnabled, this.miEnforceP3PValidity });
  45. this.mnuCookieTag.Text = "Privacy";
  46. this.miEnabled.Click += new System.EventHandler(this.miEnabled_Click);
  47. this.miEnabled.Checked = bEnabled;
  48. this.miEnforceP3PValidity.Click += new System.EventHandler(this.miEnforceP3PValidity_Click);
  49. this.miEnforceP3PValidity.Checked = bEnforceP3PValidity;
  50. }
  51. public void miEnabled_Click(object sender, EventArgs e)
  52. {
  53. miEnabled.Checked = !miEnabled.Checked;
  54. bEnabled = miEnabled.Checked;
  55. this.miEnforceP3PValidity.Enabled = bEnabled;
  56. if (bEnabled) { EnsureColumn(); }
  57. FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.enabled", bEnabled);
  58. }
  59. public void miEnforceP3PValidity_Click(object sender, EventArgs e)
  60. {
  61. miEnforceP3PValidity.Checked = !miEnforceP3PValidity.Checked;
  62. bEnforceP3PValidity = miEnforceP3PValidity.Checked;
  63. FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.EnforceP3PValidity", bEnforceP3PValidity);
  64. }
  65. private void EnsureColumn()
  66. {
  67. if (bCreatedColumn) return;
  68. FiddlerApplication.UI.lvSessions.AddBoundColumn("Privacy Info", 1, 120, "X-Privacy");
  69. bCreatedColumn = true;
  70. }
  71. public TagCookies()
  72. {
  73. this.bEnabled = FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.enabled", false);
  74. this.bEnforceP3PValidity = FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.EnforceP3PValidity", true);
  75. InitializeMenu();
  76. if (bEnabled) { EnsureColumn(); } else { this.miEnforceP3PValidity.Enabled = false; }
  77. }
  78. private void SetP3PStateFromHeader(string sValue, ref P3PState oP3PState)
  79. {
  80. if (string.IsNullOrEmpty(sValue))
  81. {
  82. return;
  83. }
  84. string sUnsatCat = String.Empty;
  85. string sUnsatPurpose = String.Empty;
  86. sValue = sValue.Replace('\'', '"');
  87. string sCP = null;
  88. Regex r = new Regex("CP\\s?=\\s?[\"]?(?<TokenValue>[^\";]*)");
  89. Match m = r.Match(sValue);
  90. if (m.Success && (null != m.Groups["TokenValue"]))
  91. {
  92. sCP = m.Groups["TokenValue"].Value;
  93. }
  94. if (String.IsNullOrEmpty(sCP))
  95. {
  96. return;
  97. }
  98. // Okay, we've got a compact policy token.
  99. oP3PState = P3PState.P3POk;
  100. string[] sTokens = sCP.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  101. foreach (string sToken in sTokens)
  102. {
  103. // Reject clearly invalid tokens...
  104. if ((sToken.Length < 3) || (sToken.Length > 4))
  105. {
  106. oP3PState = P3PState.P3PMalformed;
  107. return;
  108. }
  109. if (",PHY,ONL,GOV,FIN,".IndexOf("," + sToken + ",", StringComparison.OrdinalIgnoreCase) > -1)
  110. {
  111. sUnsatCat += (sToken + " ");
  112. continue;
  113. }
  114. if (",SAM,OTR,UNR,PUB,IVA,IVD,CON,TEL,OTP,".IndexOf("," + sToken + ",", StringComparison.OrdinalIgnoreCase) > -1)
  115. {
  116. sUnsatPurpose += (sToken + " ");
  117. continue;
  118. }
  119. // TODO: Look up the token in the complete collection and check validity
  120. }
  121. // If a cookie contains an unsatisfactory purpose and an unsatisfactory category, mark it
  122. // https://msdn.microsoft.com/en-us/library/ie/ms537343(v=vs.85).aspx#unsatisfactory_cookies
  123. if ((sUnsatCat.Length > 0) && (sUnsatPurpose.Length > 0))
  124. {
  125. if (oP3PState == P3PState.P3POk)
  126. {
  127. oP3PState = P3PState.P3PUnsatisfactory;
  128. }
  129. }
  130. }
  131. private enum P3PState
  132. {
  133. NoCookies,
  134. NoP3PAndSetsCookies,
  135. P3POk,
  136. P3PUnsatisfactory,
  137. P3PMalformed
  138. }
  139. public void OnPeekAtResponseHeaders(Session oSession)
  140. {
  141. if (!bEnabled) return;
  142. P3PState oP3PState = P3PState.NoCookies;
  143. if (!oSession.oResponse.headers.Exists("Set-Cookie"))
  144. {
  145. return;
  146. }
  147. oP3PState = P3PState.NoP3PAndSetsCookies;
  148. if (oSession.oResponse.headers.Exists("P3P"))
  149. {
  150. SetP3PStateFromHeader(oSession.oResponse.headers["P3P"], ref oP3PState);
  151. }
  152. switch (oP3PState)
  153. {
  154. case P3PState.P3POk:
  155. oSession["ui-backcolor"] = "#ACDC85";
  156. oSession["X-Privacy"] = "Sets cookies & P3P";
  157. break;
  158. case P3PState.NoP3PAndSetsCookies:
  159. oSession["ui-backcolor"] = "#FAFDA4";
  160. oSession["X-Privacy"] = "Sets cookies without P3P";
  161. break;
  162. case P3PState.P3PUnsatisfactory:
  163. oSession["ui-backcolor"] = "#EC921A";
  164. oSession["X-Privacy"] = "Sets cookies; P3P unsatisfactory for 3rd-party use";
  165. break;
  166. case P3PState.P3PMalformed:
  167. oSession["ui-backcolor"] = "#E90A05";
  168. if (bEnforceP3PValidity)
  169. {
  170. oSession.oResponse.headers["MALFORMED-P3P"] = oSession.oResponse.headers["P3P"];
  171. oSession["X-Privacy"] = "MALFORMED P3P: " + oSession.oResponse.headers["P3P"];
  172. oSession.oResponse.headers.Remove("P3P");
  173. }
  174. break;
  175. }
  176. }
  177. public void AutoTamperRequestBefore(Session oSession) { }
  178. public void AutoTamperRequestAfter(Session oSession){ /*noop*/ }
  179. public void AutoTamperResponseAfter(Session oSession) {/*noop*/}
  180. public void AutoTamperResponseBefore(Session oSession) { /*noop*/ }
  181. public void OnBeforeReturningError(Session oSession) {/*noop*/}
  182. }