FiddlerCore Blocking Problem

4 posts, 0 answers
  1. Mahbub
    Mahbub avatar
    3 posts
    Member since:
    Nov 2015

    Posted 04 Nov 2015 Link to this post

    Hello, this is my first time using FiddlerCore and am trying to grasp the concepts behind it. I am developing a simple project that should block websites that are specified in a database, and block all sites if the user is browsing at a time that is set by the administrator. It is for a school project. For now, I have hard-coded the site I want blocked: "facebook.com". Below is the code:

     

    void FiddlerApplication_BeforeRequest(Session oSession)
            {
                if (oSession.url.Contains("facebook.com"))
                {
                    oSession.utilCreateResponseAndBypassServer();
                    oSession.oResponse.headers.SetStatus(307, "Redirect");
                    oSession.oResponse["Cache-Control"] = "nocache";
                    oSession.oResponse["Location"] = "http://microsoft.com";
                    oSession.utilSetResponseBody(GenerateErrorMessage("Internet Time", "The request was set at a time interval that is blocked by the " +
                        "administrator(s) of this computer", oSession.fullUrl, "--"));
                    return;
                }
            }

     However, I am getting an ERR_TUNNEL_CONNECTION_FAILED message in google chrome and I am getting a page cannot be displayed in internet explorer. Basically what I would like is if a site is block, display the html in the browser specified in oSession.utilSetResponseBody(GenerateErrorMessage("Internet Time", "The request was set at a time interval that is blocked by the " + "administrator(s) of this computer", oSession.fullUrl, "--"));

     

    This works in all sites that do not have "https:". For ex: I am getting my desired result that I want if I try to block "microsoft.com".

    Any help is appreciated. 

  2. Mahbub
    Mahbub avatar
    3 posts
    Member since:
    Nov 2015

    Posted 04 Nov 2015 Link to this post

    Small edit,

     It should NOT be: oSession.oResponse.headers.SetStatus(307, "Redirect");

     It should be: oSession.oResponse.headers.SetStatus(451, "Blocked");

    I accidentally copy pasted an old code

  3. Eric Lawrence
    Admin
    Eric Lawrence avatar
    833 posts

    Posted 04 Nov 2015 Link to this post

    Yes, you need to use a status other than 307 and you should NOT include a Location header.

    So the code should look something like:

    void FiddlerApplication_BeforeRequest(Session oSession)
            {
                if (!oSession.HTTPMethodIs("CONNECT"))
               {
                if oSession.url.Contains("facebook.com"))

                {
                    oSession.utilCreateResponseAndBypassServer();
                    oSession.oResponse.headers.SetStatus(451, "Blocked");
                    oSession.oResponse["Cache-Control"] = "no-cache";
                    oSession.utilSetResponseBody(GenerateErrorMessage("Internet Time", "The request was set at a time interval that is blocked by the " +
                        "administrator(s) of this computer", Utilities.HtmlEncode(oSession.fullUrl), "--"));
                    return;
                }
             }
            else
            {
                if (oSession.HostnameIs("facebook.com")) oSession["x-replywithtunnel"] = "will block later...";
            }
            }

    If the HTTP Method is CONNECT then you need to do things differently to avoid corrupting the HTTPS channel. You can either not touch the request at all, or you can examine the target hostname and if it's something you care about, then you just set the session flag: oSession["x-replywithTunnel"] = "My interceptor"; and Fiddler will generate an appropriate tunnel response without actually talking to the target server.

    Regards,
    Eric Lawrence
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Mahbub
    Mahbub avatar
    3 posts
    Member since:
    Nov 2015

    Posted 04 Nov 2015 Link to this post

    Thank you, that solved my problem.
Back to Top