5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fiddler Classicでレスポンス書き換え(カスタムスクリプト使用)

Last updated at Posted at 2020-02-16

レスポンスヘッダーを書き換える必要がありました。
Fiddler Classicのカスタムスクリプトを用いればリクエストやレスポンスの内容が改竄可能です。
※デバッグ機能もあり、レスポンスを都度止めて内容を書き換えることも可能です(参考)。

編集方法

  1. Fiddler Classicにて,メニューバー「Rules」→「Customize Rules..」を選択
  2. Fiddler ScriptEditorが開かれる
  3. スクリプト内のOnBeforeResponseメソッドを探す

カスタムスクリプト修正

指定したパスのレスポンスヘッダーを変更

画像ファイルに対してCache-Controlに値no-storeを設定。

static function OnBeforeResponse(oSession: Session) {
    if (oSession.PathAndQuery == "/img/something.png") {
        oSession.oResponse["Cache-Control"] = "no-store";
    }
}

指定したドメインからの特定ヘッダーを削除

www.fizzbuzz.com:8080 からのレスポンス全てに対してCache-Controlヘッダーを削除。

static function OnBeforeResponse(oSession: Session) {
    if (oSession.host == "www.fizzbuzz.com:8080") {
        oSession.oResponse.headers.Remove("Cache-Control");
    }
}

指定したURLのレスポンスボディーを変更

外部のWebAPIのレスポンス内容を{"mean": "意味はない"}に変更。

static function OnBeforeResponse(oSession: Session) {
    if (oSession.url== "www.hoge.com/rest/api/get/means") {
        oSession.oResponse["Content-Type"] = "application/json";
        oSession.utilSetResponseBody('{\"mean\": \"意味はない\"}');
    }
}

指定した拡張子のファイルのレスポンスコードを変更する

CSSファイルの全レスポンスを404に変更する。

static function OnBeforeResponse(oSession: Session) {
    if (oSession.uriContains(".css")){
        oSession.oResponse.headers.HTTPResponseCode = 404;
        oSession.oResponse.headers.HTTPResponseStatus = "404 Not Found";
    }
}

JSONオブジェクトをスクリプトで扱う

ファイル先頭に以下を追加

import System;
import System.Windows.Forms;
import Fiddler;
+ import Newtonsoft.Json.Linq;

Newtonsoftは.NETのライブラリ、Fiddlerのインストールディレクトリにdllが存在しているため、別途インストール作業は不要

以下スクリプトを追加

static function OnBeforeResponse(oSession: Session) {
    if (oSession.host == "target.com:8080") {
        var oBody = oSession.GetResponseBodyAsString();
        var json = JObject.Parse(oBody);
        // NOTE: 配列の場合はJArray.Parseを使う
        json["title"] = "change";
        oSession.utilSetResponseBody(json.ToString());
    }
}

参考

  1. 2025年頃のFiddler Classicアップデートにより、以前までは使えたJSON文字列デシリアライズを行う「Fiddler.WebFormats.JSON.JsonDecode」が使えなくなったようです

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?