Skip to content

Commit fcdd959

Browse files
committed
Fix mitm script not recognizing output file path properly
1 parent 01ed545 commit fcdd959

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

Framework/Built_In_Automation/Sequential_Actions/common_functions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6776,9 +6776,17 @@ def proxy_server(data_set):
67766776
with open(r'{}'.format(output_file_path), 'a') as output_file:
67776777
# Start the subprocess
67786778
process = subprocess.Popen(
6779-
['mitmdump', '-s', r'{}'.format(str(mitm_proxy_path)), '-w', r'{}'.format(str(captured_network_file_path)), '-p', str(port)],
6779+
[
6780+
"mitmdump",
6781+
"-s",
6782+
f"{mitm_proxy_path}",
6783+
"-p",
6784+
str(port),
6785+
"--set",
6786+
f"output_file_path={captured_network_file_path}",
6787+
],
67806788
stdout=output_file, # Redirect stdout to the file
6781-
stderr=output_file # Redirect stderr to the file
6789+
stderr=output_file, # Redirect stderr to the file
67826790
)
67836791

67846792
pid = process.pid

Framework/Built_In_Automation/Sequential_Actions/mitm_proxy.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
from mitmproxy import http
1+
from mitmproxy import http, ctx
22
import time
3-
import sys
43
import csv
54
import os
65

76
# Initialize a list to track request details
87
requests_data = []
98

10-
# Get the output file path from command-line arguments
11-
output_file_path = sys.argv[3] if len(sys.argv) > 3 else 'default_output.csv'
12-
print(f"OUTPUT: {output_file_path}")
139

14-
# Create a CSV file and write headers if the file does not exist
15-
if not os.path.exists(output_file_path):
16-
with open(output_file_path, 'w', newline='') as csvfile:
17-
writer = csv.writer(csvfile)
18-
writer.writerow(["url", "status_code", "duration in seconds", "content_length in bytes", "timestamp"]) # Write CSV header
10+
def load(l):
11+
# Define the custom option `output_file_path`
12+
ctx.options.add_option("output_file_path", str, "", "Path to output CSV file")
13+
1914

2015
def request(flow: http.HTTPFlow) -> None:
2116
# Capture request data when it's made
@@ -30,6 +25,12 @@ def request(flow: http.HTTPFlow) -> None:
3025
})
3126

3227
def response(flow: http.HTTPFlow) -> None:
28+
output_file_path = ctx.options.output_file_path
29+
create_file_if_not_exists(output_file_path)
30+
31+
# print("Flow", flow)
32+
# print("Response", flow.response)
33+
3334
res = flow.response
3435
end_time = time.time()
3536

@@ -58,3 +59,23 @@ def response(flow: http.HTTPFlow) -> None:
5859

5960
# Optionally print captured details for console output
6061
print(f"Captured: {captured_details}")
62+
63+
def create_file_if_not_exists(filepath):
64+
"""
65+
Check if the output CSV file exists.
66+
If it does not exist, create the file and add csv headers.
67+
"""
68+
69+
if not os.path.exists(filepath):
70+
with open(filepath, "w", newline="") as csvfile:
71+
writer = csv.writer(csvfile)
72+
writer.writerow(
73+
[
74+
"url",
75+
"status_code",
76+
"duration_in_seconds",
77+
"content_length_in_bytes",
78+
"timestamp",
79+
]
80+
)
81+
print(f"Created output file: {filepath}")

0 commit comments

Comments
 (0)