📜 ⬆️ ⬇️

Modifying HTTP traffic using FiddlerScript and .NET plug-ins to Fiddler

On Habré, they have repeatedly told about such a powerful and convenient HTTP traffic monitoring tool as Fiddler . All available articles, however, tell about the built-in features of the program, without focusing on the possibilities of its expansion, of which there are as many as two: using the built-in language FiddlerScript and using writing .NET plug-ins. In this article we will look at both of them, and to make it more interesting, we use them to solve a quite practical task, which I wrote about in my last article (replacing broken links to pictures in Habré articles with workers).

So let's remember, for a start, what the past article ended with: we received a list of non-working links to pictures and their corresponding working links on the web archive. Now you need to give them to the browser and for this we will write extensions to Fiddler (one in FiddlerScript and one in .NET). Pay attention to the convenience of the solution: yes, we will need to run Fiddler, but then the broken links will be replaced by working regardless of the article domain (Habr, Hyktimes or Megamozg), regardless of the browser used (if only it had proxy support) and even mobile devices can be configured to use the Fiddler installed on the computer as a proxy.

FeeddlerScript


FiddlerScript is a JScript.NET based programming language built into Fiddler that allows you to analyze and modify incoming and outgoing traffic, extend the functionality of Fiddler itself, modify its interface.
A couple of related links:


You need to write the code in the file that opens by clicking on the menu on Rules-> Customize Rules. If you suddenly shut it down - just delete it, Fiddler will re-create it from the backup.
')
The simplest substitution of one link to another in FiddlerScript will look like adding this code to the OnBeforeRequest handler:

static function OnBeforeRequest(oSession: Session) { ... if (oSession.url=="www.example.com/bad_url.jpg") { oSession.url = "www.example.com/good_url.jpg"; } } 


We need to replace 13863 links. To write such a forest "Ipha" is unproductive. Recall that FiddlerScript is based on JScript.NET, JScript.NET is .NET, and in .NET there are very productive data structures for storing a set of strings and a quick search through it, which is based on hash tables. Yes, yes, I'm about StringDictionary.

Let's take our initial data and a couple of simple autochange operations in any text editor with support of regular expressions; we will bring it to the form:

 myDict.Add("img224.imageshack.us/img224/410/yandexmoney7mg.gif", "web.archive.org/web/20060723135036/http://img224.imageshack.us:80/img224/410/yandexmoney7mg.gif"); myDict.Add("blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif", "web.archive.org/web/20070703010741/http://blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif"); myDict.Add("blaugh.com/cartoons/060712_google_life.gif", "web.archive.org/web/20120112151300/http://blaugh.com/cartoons/060712_google_life.gif"); ... 


It is necessary to initialize the dictionary only once (for example, in the OnBoot handler, and we can use it in every OnBeforeRequest call when searching :

 static var myDict: StringDictionary = null; static function OnBoot() { myDict = new StringDictionary(); myDict.Add("img224.imageshack.us/img224/410/yandexmoney7mg.gif", "web.archive.org/web/20060723135036/http://img224.imageshack.us:80/img224/410/yandexmoney7mg.gif"); myDict.Add("blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif", "http://web.archive.org/web/20070703010741/http://blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif"); myDict.Add("blaugh.com/cartoons/060712_google_life.gif", "http://web.archive.org/web/20120112151300/http://blaugh.com/cartoons/060712_google_life.gif"); .... } ... static function OnBeforeRequest(oSession: Session) { ... if (null != myDict && myDict.ContainsKey(oSession.url)) { oSession.url = myDict[oSession.url]; } } 


We save, check by opening the article with a broken link to the picture , we see a picture that was not available without this substitution - hurray, everything works!

Full code

Writing a .NET plugin to Fiddler


If you didn’t like to write the code in a strange language JScript.NET in a strange editor and launch it in a strange way to copy-paste into the config file, waiting for 30 seconds while the script written by us is parsed and you have all the power of C #,. NET, Visual Studio and other pleasures of life!

Briefly about creating a project:
1. We start the Visual Studio, we create the new .NET-project like "Class library".
2. If you are writing under Fiddler 4 - select in the project properties .NET version 4, if under Fiddler 2 - .NET version 3.5
3. Add, depending on Fiddler itself (Fiddler.exe, located in your Program Files folder).
4. In the code, you need to specify which minimum version of Fiddler you support:
 using Fiddler; // Extension requires Fiddler 2.2.8.6+ because it uses types introduced in v2.2.8... [assembly: Fiddler.RequiredVersion("2.2.8.6")] 

5. You need to write a class that implements the IAutoTamper interface. The events he has, in general, are the same as they were in our code in FiddlerScript, so here it is schematically:
 using System; using Fiddler; using System.Collections.Specialized; [assembly: Fiddler.RequiredVersion("2.3.5.0")] public class HabraFixer : IAutoTamper // Ensure class is public, or Fiddler won't see it! { public HabraFixer() { } public void OnLoad() { myDict = new StringDictionary(); myDict.Add("img224.imageshack.us/img224/410/yandexmoney7mg.gif", "web.archive.org/web/20060723135036/http://img224.imageshack.us:80/img224/410/yandexmoney7mg.gif"); myDict.Add("blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif", "web.archive.org/web/20070703010741/http://blaugh.com/wp-content/themes/blaugh/images/cartoon-gd_01.gif"); myDict.Add("blaugh.com/cartoons/060712_google_life.gif", "web.archive.org/web/20120112151300/http://blaugh.com/cartoons/060712_google_life.gif"); ... } public void OnBeforeUnload() { } public void AutoTamperRequestBefore(Session oSession) { if (null != myDict && myDict.ContainsKey(oSession.url)) { oSession.url = myDict[oSession.url]; } } public void AutoTamperRequestAfter(Session oSession) { } public void AutoTamperResponseBefore(Session oSession) { } public void AutoTamperResponseAfter(Session oSession) { } public void OnBeforeReturningError(Session oSession) { } private StringDictionary myDict; } 


Full code

Bildim, we put the assembly in% Program Files% \ Fiddler2 \ Scripts (for all users) or in% USERPROFILE% \ My Documents \ Fiddler2 \ Scripts (only for the current one), run Fiddler, check - everything works!

This is how we learned to write extensions for Fiddler.

Source: https://habr.com/ru/post/251363/


All Articles