Skip to content

Instantly share code, notes, and snippets.

@CarsonSlovoka
Created April 16, 2026 09:32
Show Gist options
  • Select an option

  • Save CarsonSlovoka/4b42c4743a45a5eddcbaa60459fa5339 to your computer and use it in GitHub Desktop.

Select an option

Save CarsonSlovoka/4b42c4743a45a5eddcbaa60459fa5339 to your computer and use it in GitHub Desktop.
mitmproxy plugin: hemp
"""
:hcmp.reset
:hcmp.first
:hcmp.second
:hcmp.cmp
當使用hcmp.first時, 會記錄當前的flow, 並將它的response的header保存在1號中
當使用hcmp.second時, 會記錄當前的flow, 並將它的response的header保存在2號中
當使用hcmp.cmp時, 如果1, 2號都有內容,則會比較1, 2號的header的內容.
當使用hcmp.reset時,會清除1, 2號的內容
"""
import logging
from mitmproxy import command, ctx, flow
class HeaderComparator:
def __init__(self):
self.header_1 = None
self.header_2 = None
@command.command("hcmp.reset")
def reset(self) -> None:
"""清除保存的 Header 內容"""
self.header_1 = None
self.header_2 = None
ctx.log.info("hcmp: Headers have been reset.")
@command.command("hcmp.first")
def set_first(self) -> None:
# 手動從 UI 視圖獲取當前焦點
f = ctx.master.view.focus.flow
if f and f.response:
self.header_1 = dict(f.response.headers.items())
ctx.log.info("hcmp: Saved #1")
@command.command("hcmp.second")
def set_second(self) -> None:
# 手動從 UI 視圖獲取當前焦點
f = ctx.master.view.focus.flow
if f and f.response:
self.header_2 = dict(f.response.headers.items())
ctx.log.info("hcmp: Saved #2")
@command.command("hcmp.cmp")
def compare(self) -> None:
"""比較 1 號與 2 號的 Header 內容"""
if self.header_1 is None or self.header_2 is None:
ctx.log.error("hcmp: Both #1 and #2 must be set before comparing.")
return
ctx.log.info("--- Header Comparison Result ---")
all_keys = set(self.header_1.keys()) | set(self.header_2.keys())
has_diff = False
for key in sorted(all_keys):
val1 = self.header_1.get(key)
val2 = self.header_2.get(key)
if val1 == val2:
continue
has_diff = True
if val1 is None:
ctx.log.info(f"[+] {key}: {val2} (Only in #2)")
elif val2 is None:
ctx.log.info(f"[-] {key}: {val1} (Only in #1)")
else:
ctx.log.info(f"[*] {key}:")
ctx.log.info(f" #1: {val1}")
ctx.log.info(f" #2: {val2}")
if not has_diff:
ctx.log.info("No differences found.")
ctx.log.info("-------------------------------")
addons = [HeaderComparator()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment