Modifying HTTP traffic with FiddlerScript and .NET Fiddler plugins
- Tutorial
Habré has repeatedly talked about such a powerful and convenient tool for monitoring HTTP traffic as Fiddler . All the available articles, however, talk about the built-in features of the program, without focusing on the possibilities of its expansion, which exist as many as two: using the built-in FiddlerScript language and writing .NET plugins. In this article we will consider one and the other, and to make it more interesting, we use them to solve a quite practical problem that I wrote about in my last article (replacing broken links to pictures in the articles on Habré with working ones).
So, let's start by recalling how the last article ended: we got a list of broken links to images and their corresponding working links on the web archive. Now we need to give them to the browser and for this we will write extensions to Fiddler (one on FiddlerScript and one on .NET). Pay attention to the convenience of the obtained solution: yes, we will need to run Fiddler, but broken links will be replaced by working ones, regardless of the domain of the article (Habr, Giktayms or megamind), regardless of the browser used (if only had proxy support) and even mobile devices can be configured to use the installed Fiddler as a proxy.
FiddlerScript is a programming language built into Fiddler based on JScript.NET that allows you to analyze and modify incoming and outgoing traffic, expand the functionality of Fiddler itself, and modify its interface.
A couple of related links:
You need to write the code in a file that opens by clicking on the menu on Rules-> Customize Rules. If you suddenly lock it up - just delete it, Fiddler will recreate it from the backup.
The simplest substitution of one link for another with FiddlerScript will look like adding the following code to the OnBeforeRequest handler:
We need to replace 13863 links. To write such a forest of "ifas" is unproductive. Recall that FiddlerScript is based on JScript.NET, JScript.NET is .NET, and .NET has very efficient data structures for storing a set of strings and quickly searching for it, which is based on hash tables. Yes, yes, I'm talking about StringDictionary.
Let's take our initial data and a couple of simple auto-replace operations in any text editor with support for regular expressions, we bring it to the form:
Initialize the dictionary need only once (for example, in the handler ONBOOT , and use when searching for it we can each call OnBeforeRequest :
We save, check by opening an article with a broken link to the picture , we see a picture that was not available without this substitution - hurray, everything works!
Full code
If you didn’t really like writing code in the strange JScript.NET language in a strange editor and launching it in a strange way, copy-paste to the config file, waiting for 30 seconds while it parses and compiles the script we wrote - all the power of C # is at your service. NET, Visual Studio and other pleasures of life!
Briefly about creating a project:
1. Run Visual Studio, create a new .NET project of the type “Class Library”.
2. If you are writing under Fiddler 4, select .NET version 4 in the project properties; if under Fiddler 2, select .NET version 3.5
3. Add Fiddler itself (Fiddler.exe executable, which is in your folder in Program Files), depending on the version .
4. In the code you need to specify which minimum version of Fiddler you support:
5. You need to write a class that implements the IAutoTamper interface. His events are basically the same as in our FiddlerScript code, so here it is schematically:
Full code
Build, 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!
That's how we learned how to write extensions for Fiddler.
So, let's start by recalling how the last article ended: we got a list of broken links to images and their corresponding working links on the web archive. Now we need to give them to the browser and for this we will write extensions to Fiddler (one on FiddlerScript and one on .NET). Pay attention to the convenience of the obtained solution: yes, we will need to run Fiddler, but broken links will be replaced by working ones, regardless of the domain of the article (Habr, Giktayms or megamind), regardless of the browser used (if only had proxy support) and even mobile devices can be configured to use the installed Fiddler as a proxy.
FiddlerScript
FiddlerScript is a programming language built into Fiddler based on JScript.NET that allows you to analyze and modify incoming and outgoing traffic, expand the functionality of Fiddler itself, and modify its interface.
A couple of related links:
- Good Introductory Article About FiddlerScript
- FiddlerScript Editor - FiddlerScript code editor with syntax highlighting, auto-completion, class browser
You need to write the code in a file that opens by clicking on the menu on Rules-> Customize Rules. If you suddenly lock it up - just delete it, Fiddler will recreate it from the backup.
The simplest substitution of one link for another with FiddlerScript will look like adding the following 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 of "ifas" is unproductive. Recall that FiddlerScript is based on JScript.NET, JScript.NET is .NET, and .NET has very efficient data structures for storing a set of strings and quickly searching for it, which is based on hash tables. Yes, yes, I'm talking about StringDictionary.
Let's take our initial data and a couple of simple auto-replace operations in any text editor with support for regular expressions, we 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");
...
Initialize the dictionary need only once (for example, in the handler ONBOOT , and use when searching for it we can each call OnBeforeRequest :
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 an 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 for Fiddler
If you didn’t really like writing code in the strange JScript.NET language in a strange editor and launching it in a strange way, copy-paste to the config file, waiting for 30 seconds while it parses and compiles the script we wrote - all the power of C # is at your service. NET, Visual Studio and other pleasures of life!
Briefly about creating a project:
1. Run Visual Studio, create a new .NET project of the type “Class Library”.
2. If you are writing under Fiddler 4, select .NET version 4 in the project properties; if under Fiddler 2, select .NET version 3.5
3. Add Fiddler itself (Fiddler.exe executable, which is in your folder in Program Files), depending on the version .
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. His events are basically the same as in our FiddlerScript code, 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
Build, 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!
That's how we learned how to write extensions for Fiddler.