操作步骤:
具体实现:
Fiddler 右键菜单效果
代码:
public static ContextAction("Remove Session from disk")
function DoRemoveSession(oSessions: Session[]) {
if (oSessions.Length > 1) {
MessageBox.Show("Only select a session.");
return;
}
//保存session信息用于与api匹配
//filePathForRemoveSession是文件地址
var fso;
var file;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.OpenTextFile(filePathForRemoveSession,2 ,true);
file.writeLine("Request url: " + oSessions[0].url);
file.writeLine("Request header:" + "\n" + oSessions[0].oRequest.headers);
file.writeLine("Request body: " + oSessions[0].GetRequestBodyAsString());
file.writeLine("Response code: " + oSessions[0].responseCode);
file.writeLine("Response body: " + oSessions[0].GetResponseBodyAsString());
file.writeLine("\n");
file.close();
//js 调用cmd执行python脚本进行删除
var s = new ActiveXObject("WScript.Shell");
// python文件地址自己加
s.Run("python RemoveSession.py")
}
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# FileName RemoveSession.py
# Author: HeyNiu
# Created Time: 20160726
"""
1.source_path >> 需要移除的session,从fiddler右键Remove Session form disk中得来
2.target_path >> fiddler自动保存的sessions信息
3.通过source_path得到要删除的url和url的请求时间(唯一)
4.读取target_path的文件,拿url的请求时间与之匹配
5.匹配到的session执行删除
6.重新覆盖target_path的文件
"""
import re
import os
class RemoveSession(object):
def __init__(self, source_path, target_path):
self.source_path = source_path
self.target_path = target_path
self.t = ""
self.url = ""
self.target_file_path = ""
def __get_session(self):
"""
要知道删除那个session,唯一的标识就是时间,时间是一致的,可以从这里入手
:return: 返回一个session的时间格式和session的url
"""
# Digest t="2016-07-26 11:10:02"
reg_t = re.compile(r'Digest t="(.+?)"')
reg_url = re.compile(r'Request url(.+?)\n')
with open(self.source_path, encoding='gbk') as f:
data = f.read()
self.t = re.findall(reg_t, data)[0]
self.url = re.findall(reg_url, data)[0].split("/")[-1]
def __get_file_list(self):
"""
获取目标地址目录下的文件列表
:return: 返回标地址目录下的文件列表
"""
for root, dirs, files in os.walk(self.target_path):
return (f for f in files)
def __match_file(self):
"""
url与文件列表匹配
:return:返回匹配的文件
"""
self.__get_session()
return [i for i in self.__get_file_list() if i.startswith(self.url)]
def __read_target_file(self):
"""
读取目标文件
:return:
"""
file_name = self.__match_file()
single_session = []
total_session = []
if file_name:
self.target_file_path = self.target_path + "\\" + file_name[0]
l = open(self.target_file_path, encoding='gbk').readlines()
for i in l:
single_session.append(i)
if i.startswith("Session end"):
total_session.append(single_session)
single_session = []
return total_session
def __remove_session(self):
"""
移除对应的session,通过前面匹配到的时间来对应
:return:
"""
l = self.__read_target_file()
# 待移除的session
r = []
for i in l:
for j in i:
if j.find(self.t) != -1:
r.append(i)
for i in r:
l.remove(i)
return l
def override_session_file(self):
"""
把移除了session的目标文件,重新写入文件
:return:
"""
l = self.__remove_session()
with open(self.target_file_path, 'w', encoding='gbk') as f:
for i in l:
for j in i:
f.write(j)
if __name__ == "__main__":
session = RemoveSession("D:\\Fiddler Sessions\\RemoveSession.txt", "D:\\Fiddler Sessions\\Api")
session.override_session_file()