Skip to content

Instantly share code, notes, and snippets.

@jasonmfehr
Last active March 31, 2023 12:13
Show Gist options
  • Save jasonmfehr/6ec6419f6229f7549f82b5b87ee2f658 to your computer and use it in GitHub Desktop.
Save jasonmfehr/6ec6419f6229f7549f82b5b87ee2f658 to your computer and use it in GitHub Desktop.
Writes mitmproxy data in CSV format to a file
import mitmproxy
from datetime import datetime
import math
class RequestsToCSV:
def load(self, loader):
#
# note: update this path to change the data file name and/or location
#
self.file_handle = open("requests-" + datetime.now().isoformat().split(".")[0] + ".csv", "w")
self.file_handle.write("Request,Start Time,EndTime,Request Method,Path,Response Size Raw (bytes),Response Size Uncompressed (bytes),Duration (ms)\n")
self.file_handle.flush()
self.request_count = 0
def done(self):
self.file_handle.close()
def response(self, flow: mitmproxy.http.HTTPFlow):
start_time = datetime.fromtimestamp(flow.request.timestamp_start).isoformat()
end_time = datetime.fromtimestamp(flow.response.timestamp_end).isoformat()
duration = str((flow.response.timestamp_end - flow.request.timestamp_start) * 1000).split(".")[0]
self.request_count += 1
self.file_handle.write(str(self.request_count) + "," + start_time + "," + end_time + "," + flow.request.method + "," + flow.request.path + "," + str(len(flow.response.raw_content)) + "," + str(len(flow.response.content)) + "," + str(duration) + "\n")
self.file_handle.flush()
addons = [
RequestsToCSV()
]
@jasonmfehr
Copy link
Author

This script works with mitmproxy to output http request details to a csv file as the requests flow through the proxy.

To use it, pass the script name to one of the mitmproxy commands using the -s flag. For example:

mitmdump --mode reverse:http://127.0.0.1:28000 --listen-port 28007 -s mitmproxy_requests_to_csv.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment