Skip to content

Add sampling to flow heuristic. #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions mitmproxy2swagger/mitmproxy_capture_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
from mitmproxy.exceptions import FlowReadException


def has_non_printable_sampled(file_path: str, sample_size=2048, samples=3) -> bool:
file_size = os.path.getsize(file_path)
chunk_offsets = [int(file_size * i / samples) for i in range(samples)]

with open(file_path, "rb") as f:
for offset in chunk_offsets:
f.seek(offset)
data = f.read(sample_size)
text = data.decode("utf-8", "ignore").replace("\r", "").replace("\n", "")
if not text.isprintable():
return True
return False


def mitmproxy_dump_file_huristic(file_path: str) -> int:
val = 0
if "flow" in file_path:
Expand All @@ -19,13 +33,7 @@ def mitmproxy_dump_file_huristic(file_path: str) -> int:
with open(file_path, "rb") as f:
data = f.read(2048)
# if file contains non-ascii characters after remove EOL characters
if (
data.decode("utf-8", "ignore")
.replace("\r", "")
.replace("\n", "")
.isprintable()
is False
):
if has_non_printable_sampled(file_path):
val += 50
# if first character of the byte array is a digit
if data[0:1].decode("utf-8", "ignore").isdigit() is True:
Expand Down