Skip to content

Python API

friTap ships a stable, SemVer-guaranteed Python API for driving captures, consuming live events, and working with .tap files offline. The public surface is everything exported from the friTap package __all__ (59 symbols); removing or breaking any of them requires a friTap MAJOR bump (see RELEASING.md).

This page leads with the modern API:

The legacy SSL_Logger class is documented last, in a deprecated section.

CI-runnable vs. live-device examples

Examples in this page are labelled either live / device (they attach to or spawn a real process and cannot run in CI) or offline / CI-runnable (they operate on .tap files or in-memory objects and run anywhere friTap imports).


Quick start (FriTap builder)

Live / device example

FriTap(...).start() attaches to or spawns a real target through Frida. This cannot run in CI — it needs a device and the target app/process.

from friTap import FriTap

session = (
    FriTap("com.example.app")
    .mobile()                  # target a USB device
    .keylog("keys.log")        # write an SSLKEYLOGFILE
    .pcap("cap.pcapng")        # write decrypted traffic (pcapng → embedded DSB)
    .start()                   # returns a FriTapSession
)

session.wait()   # block until the target exits; or call session.stop()

With callbacks (also live / device):

from friTap import FriTap

session = (
    FriTap("com.example.app")
    .mobile("device-id")
    .pcap("capture.pcap")
    .on_keylog(lambda e: print(e.key_data))
    .on_data(lambda e: print(f"{e.src_addr}:{e.src_port} -> {e.dst_addr}:{e.dst_port}"))
    .start()
)

build_config() is the offline-safe half of the builder — it returns a FriTapConfig without touching a device, which is useful for tests and for inspecting/serializing configuration:

from friTap import FriTap

config = FriTap("com.example.app").mobile().keylog("keys.log").build_config()
print(config.target, config.output.keylog)   # offline / CI-runnable

FriTapSession

start() returns a FriTapSession handle:

Member Kind Description
event_bus property → EventBus The session's event bus; subscribe for live events.
is_running property → bool Whether the capture is still active.
stop() method Gracefully stop the capture session.
wait() method Block until the session ends (e.g. the process exits).

Builder reference

Every method below returns self, so calls chain. Names are verbatim from friTap.api.FriTap. The builder is hand-documented (chained APIs render poorly under autodoc).

Device & target

Method Description
mobile(device_id=None) Target a mobile device — USB if device_id is omitted, otherwise the given Frida device ID.
host(address) Target a remote Frida device (host:port).
spawn(enable=True) Spawn the target instead of attaching.
spawn_gating(enable=True, all_processes=False) Enable spawn gating for multi-process apps. all_processes=True catches every spawn.
child_gating(enable=True) Enable child-process gating.
follow_exec(enable=True) Force exec-following on/off. Unset = auto: off everywhere (Frida fork-child crash); pass True to opt in.
timeout(seconds) Set a timeout before resuming the suspended target.

Output

Method Description
pcap(path) Write decrypted traffic to a PCAP file. The extension decides (--pcap-format auto): .pcapng writes pcapng (with embedded DSB when keys are extracted), anything else classic libpcap. Resets a prior pcapng() call.
pcapng(path) Write self-decrypting PCAPNG whatever the extension (--pcap-format pcapng): pcapng("out.bin") writes pcapng; a contradicting .pcap name is written as pcapng with a warning.
keylog(path) Write key material in Wireshark-loadable form for the active protocol (NSS SSLKEYLOGFILE for TLS). With --protocol all/auto, the path is split per protocol as <stem>.<proto><ext>.
json_output(path) Write session metadata as JSON. Key material is redacted unless keylog() or json_keys() is also set.
json_keys(enable=True) Include raw key material (key_data) in the JSON output, like --json-keys.
verbose(enable=True) Enable verbose console output.
live(enable=True) Stream to Wireshark via a named pipe.
full_capture(enable=True) Capture the full network stream, not just decrypted payload.

Hooking

Method Description
patterns(path) Use pattern-based (symbol-less) hooking from a JSON file.
offsets(path) Use offset-based hooking from a JSON file.
experimental(enable=True) Enable experimental features.
anti_root(enable=True) Enable anti-root-detection hooks (Android).
payload_modification(enable=True) Enable payload-modification capability.
force_scan(name) Force-scan a module that Cronet-split-topology suppression would otherwise skip. Accepts a literal name, a stem prefix (name*) or a regex (re:...). May be called multiple times.

Protocol & backend

Method Description
protocol(proto) Set the target protocol: tls, ipsec, ssh, auto (and other registered values). Default tls.
backend(backend) Set the instrumentation backend: frida (default), gdb, lldb, ebpf.

Experimental backends

Only frida is fully supported. gdb/lldb/ebpf are experimental/future; FriTapConfig rejects a protocol/backend combination that is not fully supported.

Debug & misc

Method Description
debug(enable=True) Enable debug mode with Chrome Inspector (also turns on debug output).
debug_output(enable=True) Enable debug output only (no inspector).
custom_script(path) Load a custom Frida script before friTap's hooks.
add_script_plugin(plugin) Register a ScriptPlugin to load when the session starts.
environment(path) Provide an environment-variables JSON for spawn.
signal_handler(enable=True) Opt in to friTap handling SIGINT/SIGTERM for the session (default off: the embedding program owns its signals and calls FriTapSession.stop()). When on and start() runs on the main thread, Ctrl+C/SIGTERM stop the session like stop() (outputs finalized, the host process keeps running) and the previous handlers are restored afterwards.
proxy(address) Redirect connections to a host:port proxy and bypass cert pinning (requires fritap-proxy).

Embedded/API mode never calls os._exit: if the target dies, friTap finalizes the outputs and ends the session, leaving the host interpreter running. Only the CLI exits the process.

Callbacks

Each registers a callback fired on the session's event bus.

Method Event delivered
on_keylog(callback) KeylogEvent
on_data(callback) DatalogEvent
on_library_detected(callback) LibraryDetectedEvent
on_session(callback) SessionEvent
on_flow(callback) FlowEvent — wires up a FlowCollector automatically.

Terminal methods

Method Description
build_config() Build a FriTapConfig from the current builder state. Offline-safe — does not touch a device.
start() Build the config, create the capture, wire up the event bus, and start. Returns a FriTapSession. Live / device.

Events & EventBus

friTap dispatches typed events through a thread-safe publish-subscribe EventBus. Output handlers, the TUI, plugins, and your own callbacks all subscribe to event classes.

friTap.events.EventBus

Thread-safe publish-subscribe event bus.

bus.subscribe(KeylogEvent, handler) / bus.emit(KeylogEvent(...)). Higher priority runs first; plugins use PLUGIN_PRIORITY to run before built-in outputs.

Source code in friTap/events.py
class EventBus:
    """Thread-safe publish-subscribe event bus.

    ``bus.subscribe(KeylogEvent, handler)`` / ``bus.emit(KeylogEvent(...))``.
    Higher *priority* runs first; plugins use ``PLUGIN_PRIORITY`` to run before built-in outputs.
    """

    MAX_HANDLER_FAILURES = 10
    PLUGIN_PRIORITY = 100

    def __init__(self, on_handler_error: Optional[Callable] = None) -> None:
        self._subscribers: Dict[Type[FriTapEvent], List[Tuple[int, Callable]]] = {}
        self._lock = threading.RLock()
        self._logger = logging.getLogger("friTap.events")
        self._failure_counts: Dict[str, int] = {}
        self._on_handler_error = on_handler_error

    def subscribe(self, event_type: Type[FriTapEvent], callback: Callable, *, priority: int = 0) -> None:
        """Call *callback* whenever *event_type* is emitted; higher *priority* (default 0) runs first."""
        with self._lock:
            subs = self._subscribers.setdefault(event_type, [])
            subs.append((priority, callback))
            subs.sort(key=lambda t: t[0], reverse=True)

    def unsubscribe(self, event_type: Type[FriTapEvent], callback: Callable) -> None:
        """Remove a previously registered callback."""
        with self._lock:
            subs = self._subscribers.get(event_type, [])
            for i, (_, cb) in enumerate(subs):
                if cb == callback:
                    subs.pop(i)
                    break

    def emit(self, event: FriTapEvent) -> None:
        """Dispatch *event* to its type's subscribers and to ``FriTapEvent`` catch-alls, by descending priority."""
        with self._lock:
            specific = self._subscribers.get(type(event), [])
            if type(event) is not FriTapEvent:
                catch_all = self._subscribers.get(FriTapEvent, [])
                if not catch_all:
                    # fast path
                    handlers = list(specific)
                else:
                    # Merge two pre-sorted (descending priority) lists
                    handlers = []
                    i, j = 0, 0
                    while i < len(specific) and j < len(catch_all):
                        if specific[i][0] >= catch_all[j][0]:
                            handlers.append(specific[i])
                            i += 1
                        else:
                            handlers.append(catch_all[j])
                            j += 1
                    handlers.extend(specific[i:])
                    handlers.extend(catch_all[j:])
            else:
                handlers = list(specific)

        for _prio, cb in handlers:
            try:
                cb(event)
            except Exception as exc:
                handler_name = getattr(cb, "__qualname__", repr(cb))
                self._logger.exception(
                    "Error in event subscriber %s for %s",
                    handler_name,
                    type(event).__name__,
                )

                count = self._failure_counts.get(handler_name, 0) + 1
                self._failure_counts[handler_name] = count

                if self._on_handler_error is not None:
                    try:
                        self._on_handler_error(cb, event, exc)
                    except Exception:
                        pass

                # Guard against recursion on ErrorEvent handlers.
                if not isinstance(event, ErrorEvent):
                    try:
                        self.emit(ErrorEvent(
                            error=f"Handler {handler_name} failed",
                            description=str(exc),
                            severity=ERROR_SEVERITY_ERROR,
                        ))
                    except Exception:
                        pass

                if count >= self.MAX_HANDLER_FAILURES:
                    self._logger.warning(
                        "Auto-unsubscribing handler %s after %d failures",
                        handler_name, count,
                    )
                    self._remove_handler(cb)

    def _remove_handler(self, cb: Callable) -> None:
        """Remove *cb* from every subscriber list."""
        with self._lock:
            for subs in self._subscribers.values():
                for i, (_p, c) in enumerate(subs):
                    if c is cb:
                        subs.pop(i)
                        break

    @property
    def handler_failures(self) -> Dict[str, int]:
        """Copy of the handler failure counts."""
        return dict(self._failure_counts)

    def clear(self) -> None:
        """Remove all subscribers."""
        with self._lock:
            self._subscribers.clear()

subscribe(event_type, callback, *, priority=0)

Call callback whenever event_type is emitted; higher priority (default 0) runs first.

Source code in friTap/events.py
def subscribe(self, event_type: Type[FriTapEvent], callback: Callable, *, priority: int = 0) -> None:
    """Call *callback* whenever *event_type* is emitted; higher *priority* (default 0) runs first."""
    with self._lock:
        subs = self._subscribers.setdefault(event_type, [])
        subs.append((priority, callback))
        subs.sort(key=lambda t: t[0], reverse=True)

unsubscribe(event_type, callback)

Remove a previously registered callback.

Source code in friTap/events.py
def unsubscribe(self, event_type: Type[FriTapEvent], callback: Callable) -> None:
    """Remove a previously registered callback."""
    with self._lock:
        subs = self._subscribers.get(event_type, [])
        for i, (_, cb) in enumerate(subs):
            if cb == callback:
                subs.pop(i)
                break

emit(event)

Dispatch event to its type's subscribers and to FriTapEvent catch-alls, by descending priority.

Source code in friTap/events.py
def emit(self, event: FriTapEvent) -> None:
    """Dispatch *event* to its type's subscribers and to ``FriTapEvent`` catch-alls, by descending priority."""
    with self._lock:
        specific = self._subscribers.get(type(event), [])
        if type(event) is not FriTapEvent:
            catch_all = self._subscribers.get(FriTapEvent, [])
            if not catch_all:
                # fast path
                handlers = list(specific)
            else:
                # Merge two pre-sorted (descending priority) lists
                handlers = []
                i, j = 0, 0
                while i < len(specific) and j < len(catch_all):
                    if specific[i][0] >= catch_all[j][0]:
                        handlers.append(specific[i])
                        i += 1
                    else:
                        handlers.append(catch_all[j])
                        j += 1
                handlers.extend(specific[i:])
                handlers.extend(catch_all[j:])
        else:
            handlers = list(specific)

    for _prio, cb in handlers:
        try:
            cb(event)
        except Exception as exc:
            handler_name = getattr(cb, "__qualname__", repr(cb))
            self._logger.exception(
                "Error in event subscriber %s for %s",
                handler_name,
                type(event).__name__,
            )

            count = self._failure_counts.get(handler_name, 0) + 1
            self._failure_counts[handler_name] = count

            if self._on_handler_error is not None:
                try:
                    self._on_handler_error(cb, event, exc)
                except Exception:
                    pass

            # Guard against recursion on ErrorEvent handlers.
            if not isinstance(event, ErrorEvent):
                try:
                    self.emit(ErrorEvent(
                        error=f"Handler {handler_name} failed",
                        description=str(exc),
                        severity=ERROR_SEVERITY_ERROR,
                    ))
                except Exception:
                    pass

            if count >= self.MAX_HANDLER_FAILURES:
                self._logger.warning(
                    "Auto-unsubscribing handler %s after %d failures",
                    handler_name, count,
                )
                self._remove_handler(cb)

clear()

Remove all subscribers.

Source code in friTap/events.py
def clear(self) -> None:
    """Remove all subscribers."""
    with self._lock:
        self._subscribers.clear()

The bus also exposes the class constant PLUGIN_PRIORITY = 100; plugins should pass priority=EventBus.PLUGIN_PRIORITY to subscribe() so they run before the built-in output handlers.

A standalone bus can be created, subscribed to, and emitted into without any device — useful for testing handlers:

from friTap import EventBus, KeylogEvent

bus = EventBus()
bus.subscribe(KeylogEvent, lambda e: print("key:", e.key_data))
bus.emit(KeylogEvent(key_data="CLIENT_RANDOM abc def"))   # offline / CI-runnable
# -> key: CLIENT_RANDOM abc def

Event base class

All events subclass FriTapEvent, which provides:

Field / member Type Description
timestamp float Creation time (time.time()).
protocol str Protocol context (default "tls").
cancel() method Advisory cancellation (DOM preventDefault style).
cancelled property → bool Whether cancel() was called.

KeylogEvent

Emitted when key material is extracted (on_keylog).

Field Type Description
key_data str Pre-formatted keylog line (e.g. CLIENT_RANDOM <hex> <hex>).
payload dict \| None Structured payload for protocols that need it (e.g. SSH KEX shared secret).

DatalogEvent

Emitted when decrypted application data is captured (on_data).

Field Type Description
data bytes The decrypted payload.
function str The hooked function that produced it.
direction str "read" or "write".
src_addr / src_port str / int Source endpoint.
dst_addr / dst_port str / int Destination endpoint.
ss_family str Socket family (e.g. "AF_INET").
ssl_session_id str TLS session identifier.
client_random str TLS client random.
transport str "tcp" or "udp" (QUIC is udp).
http3_headers list \| None Decoded HTTP/3 headers [[name, value], ...] (app-api QUIC mode).
stream_id int \| None QUIC stream id; stored on the resulting FlowChunk.stream_id.
quic_scid / quic_dcid str QUIC source / destination connection IDs.
quic_stream_type str QUIC stream type.

LibraryDetectedEvent

Emitted when a TLS/SSL library is detected (on_library_detected).

Field Type Description
library str Logical library name (e.g. "openssl").
module str The loaded module/file name.
path str Full module path in the target.

SessionEvent

Emitted on TLS/QUIC session lifecycle changes (on_session).

Field Type Description
session_id str Session identifier.
event_type str One of SESSION_STARTED/SESSION_RESUMED/SESSION_ENDED/SESSION_DESTROYED (the string constants "started", "resumed", "ended", "destroyed").
cipher_suite str Negotiated cipher suite.
cipher property → str Read-only alias for cipher_suite.
protocol_version str TLS version (e.g. "TLS 1.3").
server_name str SNI server name.
alpn str Negotiated ALPN ("h2", "http/1.1", …).
quic_version str QUIC transport version (QUIC only).
client_random, connection_id, src_addr, src_port, dst_addr, dst_port Additional session/endpoint metadata.

FlowEvent

Emitted when a flow is created, updated, or completed (on_flow).

Field Type Description
flow Flow The flow object.
flow_event_type str "created", "updated", or "completed".

Other events

These are part of the public surface and are emitted by the agent/output pipeline; subscribe to them the same way:

  • ConsoleEvent (message, level, origin) — console log lines from the agent; origin is "agent_dev" for the agent's developer diagnostics (debug level).
  • ErrorEvent (error, description, stack, file, line, severity) — agent/hooking errors; severity is "info"/"warning"/"error"/"fatal".
  • SocketTraceEvent (src_addr, src_port, dst_addr, dst_port, ss_family) — traced socket info.
  • DetachEvent (reason) — emitted when the target process detaches.

Configuration dataclasses

FriTap.build_config() returns a FriTapConfig. You can also construct it directly and pass it to the lower-level CoreController / SSL_Logger.

friTap.config.FriTapConfig dataclass

Top-level configuration for a friTap session.

Usage

config = FriTapConfig( target="com.example.app", device=DeviceConfig(mobile=True, spawn=True), output=OutputConfig(pcap="capture.pcap", keylog="keys.log"), )

Source code in friTap/config.py
@dataclass
class FriTapConfig:
    """
    Top-level configuration for a friTap session.

    Usage:
        config = FriTapConfig(
            target="com.example.app",
            device=DeviceConfig(mobile=True, spawn=True),
            output=OutputConfig(pcap="capture.pcap", keylog="keys.log"),
        )
    """
    target: str
    # Original argv tokens for spawn mode (e.g. ["wine", "/p/My Game/app.exe"]).
    # Kept separate from `target` (which stays a single string for attach-by-name
    # / display) so spawn passes the real argv to device.spawn() instead of
    # re-splitting the joined string on spaces, which corrupts paths containing
    # spaces. None for attach mode / programmatic configs.
    target_argv: Optional[List[str]] = None
    device: DeviceConfig = field(default_factory=DeviceConfig)
    output: OutputConfig = field(default_factory=OutputConfig)
    hooking: HookingConfig = field(default_factory=HookingConfig)
    protocol: str = "tls"
    # `protocols` is the canonical selection: an ordered, de-duplicated list
    # (e.g. ["tls", "rc4"]). `protocol` mirrors its first element for callers that
    # read a single string; __post_init__ keeps the two in sync.
    protocols: List[str] = field(default_factory=list)
    backend: str = BackendName.FRIDA
    # True when the user explicitly passed --backend (vs. the default). In "auto"
    # Wine mode an explicit `--backend frida` suppresses the GDB Wine route so the
    # experimental Frida Wine agent stays reachable.
    backend_explicit: bool = False
    debug: bool = False
    debug_output: bool = False
    custom_hook_script: Optional[str] = None
    environment_file: Optional[str] = None
    install_lsass_hook: bool = True
    proxy: Optional[str] = None  # "host:port" or "[ipv6]:port" format

    def __post_init__(self):
        if self.debug:
            self.debug_output = True
        # Whichever of `protocols`/`protocol` the caller supplied, keep both
        # consistent: derive the list from `protocol` when only that was set,
        # otherwise `protocol` becomes the list's first element.
        if not self.protocols:
            self.protocols = [self.protocol]
        else:
            self.protocol = self.protocols[0]
        self._resolve_pcap_container()

    def _resolve_pcap_container(self) -> None:
        """Resolve the -p container once for this config and warn about a conflict.

        A helper session sharing another session's -p file (LSASS,
        ``auxiliary_session``) stays silent: the target session already warned.
        """
        from .pcap_utility import warn_pcap_format_conflict
        self.output.resolve_pcap_container()
        if not self.output.auxiliary_session:
            warn_pcap_format_conflict(self.output.pcap, self.output.output_format)

    def validate_protocol_backend(self, protocol_handler=None, protocol_name=None) -> None:
        """Validate that the selected backend supports a configured protocol.

        Parameters
        ----------
        protocol_handler
            A ProtocolHandler instance. If None, validation is skipped.
        protocol_name
            The name of the protocol being validated (used in the skip check and
            error message). Defaults to the primary ``self.protocol`` so existing
            single-handler callers are unchanged; multi-protocol callers pass one
            name per selected handler so each selection is validated in turn.

        Raises
        ------
        UnsupportedProtocolBackendError
            When the backend support level is STUB or UNSUPPORTED.
        """
        name = protocol_name or self.protocol
        if name in ("auto", "all"):
            return  # auto/all always start with the Frida default
        if protocol_handler is None:
            return
        from .protocols.base import BackendSupport
        level = protocol_handler.get_backend_support_level(self.backend)
        if level != BackendSupport.FULL:
            supported = [
                name for name, lvl in protocol_handler.supported_backends.items()
                if lvl == BackendSupport.FULL
            ]
            raise UnsupportedProtocolBackendError(
                f"Protocol '{name}' does not fully support the "
                f"'{self.backend}' backend (level: {level}). "
                f"Supported backends: {', '.join(supported)}"
            )

    @classmethod
    def from_legacy_params(
        cls,
        app: str,
        pcap_name: Optional[str] = None,
        spawn_argv: Optional[List[str]] = None,
        verbose: bool = False,
        spawn: bool = False,
        keylog: bool | str = False,
        enable_spawn_gating: bool = False,
        spawn_gating_all: bool = False,
        enable_child_gating: bool = False,
        follow_exec: Optional[bool] = None,
        mobile: bool | str = False,
        live: bool = False,
        environment_file: Optional[str] = None,
        debug_mode: bool = False,
        full_capture: bool = False,
        socket_trace: bool | str = False,
        owner_capture: bool = False,
        owner_strict: bool = False,
        owner_no_dns: bool = False,
        owner_nflog_group: int = 30,
        host: bool | str = False,
        offsets: Optional[str] = None,
        debug_output: bool = False,
        experimental: bool = False,
        wine_mode: str = "auto",
        backend_explicit: bool = False,
        anti_root: bool = False,
        payload_modification: bool = False,
        library_scan: bool = False,
        enable_default_fd: bool = False,
        patterns: Optional[str] = None,
        custom_hook_script: Optional[str] = None,
        json_output: Optional[str] = None,
        json_keys: bool = False,
        output_format: str = "auto",
        install_lsass_hook: bool = True,
        timeout: Optional[int] = None,
        script_load_timeout: float = 20.0,
        backend: str = BackendName.FRIDA,
        protocol: str = "tls",
        protocols: Optional[List[str]] = None,
        proxy: Optional[str] = None,
        filter_expression: Optional[str] = None,
        filter_infrastructure: bool = True,
        include_loopback: bool = False,
        auto_relabel: bool = True,
        force_scan_modules: Optional[List[str]] = None,
        quic_capture_mode: str = "stream",
        quic_only: bool = False,
        no_loader_hook: bool = False,
        stealth_loader: bool = False,
        pairip_safe: bool = False,
        probe: bool = False,
        force_anchor_locator: bool = False,
        attribute_sdk: bool = False,
        quic_egress_headers_layer: str = "auto",
        scan_keys_region: Optional[str] = None,
        memory_scan: bool = False,
        memory_scan_patterns: Optional[str] = None,
        memory_scan_interval: float = 2.0,
        memory_scan_emit_unconfirmed: bool = False,
        memory_scan_rc4_known_plaintext: Optional[str] = None,
        memory_scan_rc4_ciphertext: Optional[str] = None,
        scan: Optional[str] = None,
        scan_report: str = "table",
        scan_report_out: Optional[str] = None,
        scan_min_severity: str = "info",
        scan_min_confidence: float = 0.0,
        scan_source: Optional[str] = None,
        scan_category: Optional[str] = None,
        scan_show_pii: bool = False,
        scan_analyzer_path: Optional[List[str]] = None,
    ) -> "FriTapConfig":
        """Build a FriTapConfig from the legacy SSL_Logger constructor parameters."""
        params = dict(locals())
        return cls(
            target=app,
            target_argv=spawn_argv,
            device=_legacy_device_config(params),
            output=_legacy_output_config(params),
            hooking=_legacy_hooking_config(params),
            protocol=protocol,
            protocols=list(protocols) if protocols else [protocol],
            backend=backend,
            backend_explicit=backend_explicit,
            debug=debug_mode,
            debug_output=debug_output,
            custom_hook_script=custom_hook_script,
            environment_file=environment_file,
            install_lsass_hook=install_lsass_hook,
            proxy=proxy,
        )

friTap.config.DeviceConfig dataclass

Configuration for target device connection.

Source code in friTap/config.py
@dataclass
class DeviceConfig:
    """Configuration for target device connection."""
    device_id: Optional[str] = None  # Frida device ID (from TUI enumeration)
    mobile: bool | str = False
    host: Optional[str] = None
    spawn: bool = False
    enable_spawn_gating: bool = False
    spawn_gating_all: bool = False
    enable_child_gating: bool = False
    # Follow every instrumented process across exec() (e.g. Homebrew's python3
    # launcher exec()ing the real binary), in spawn and attach mode. It needs
    # Frida child gating, which costs ~40-50 ms per fork. Tri-state: None =
    # auto (off everywhere), True = --follow-exec, False =
    # --no-follow-exec. Read it through :func:`resolve_follow_exec`.
    follow_exec: Optional[bool] = None
    timeout: Optional[int] = None
    # Upper bound (seconds) for the agent's blocking ``script.load()``. Without
    # it a wedged agent hangs the session silently. ``0`` disables the bound.
    script_load_timeout: float = 20.0
    # Android multi-process capture (--profile / --discover / --also-package):
    # a tlsLibHunter discovery profile names the delegate and sibling processes
    # that carry the target's traffic; they are spawn-gated and instrumented too.
    profile_path: Optional[str] = None
    discover_seconds: Optional[float] = None
    # --discover-baseline: idle seconds sampled first (new sockets afterwards rank first).
    discover_baseline: float = 0.0
    # --discover-probe: tlsLibHunter's binder probe before discovery (finds more delegates).
    discover_probe: bool = False
    also_packages: List[str] = field(default_factory=list)
    # --include-shared-delegates: also attach the profile's shared system processes;
    # their keys go to <keylog>.shared (not attributable to the target).
    include_shared_delegates: bool = False

    @property
    def profile_capture_requested(self) -> bool:
        """Whether --profile, --discover or --also-package asked for a multi-process capture."""
        return bool(self.profile_path or self.discover_seconds or self.also_packages)

friTap.config.OutputConfig dataclass

Configuration for output destinations and formats.

Source code in friTap/config.py
@dataclass
class OutputConfig:
    """Configuration for output destinations and formats."""
    pcap: Optional[str] = None
    keylog: Optional[str] = None
    # -j <file>: JSON (or *.jsonl) output file. ``-j -`` is normalized away at
    # construction: it sets ``json_stream`` and leaves this None, so nothing
    # downstream ever has to recognise the "-" sentinel.
    json_output: Optional[str] = None
    # -j -: stream JSONL events to stdout (see friTap.output.stdout_stream).
    json_stream: bool = False
    # --json-keys: put raw key material into the -j JSON/JSONL output even
    # without -k. Off by default: JSON output is often handed to scripts and AI
    # agents, so without -k/--json-keys keylog records are redacted.
    json_keys: bool = False
    # --tap <file>: write a .tap capture (decrypted flows) from a CLI session.
    tap_output: Optional[str] = None
    # Forces plaintext hooks off even when a selected handler consumes
    # plaintext (the LSASS helper session of a -f full-capture run).
    plaintext_hooks_disabled: bool = False
    # Requested -p container: "auto" (by extension), "pcap" or "pcapng"
    # (--pcap-format). Writers read the resolved ``pcap_container`` instead.
    output_format: str = "auto"
    # The resolved -p container ("pcap" or "pcapng"), set by
    # resolve_pcap_container() when the config is built.
    pcap_container: str = field(default="pcap", init=False)
    live: bool = False
    live_mode: str = ""  # "", "wireshark", "live_pcapng"
    verbose: bool = False
    full_capture: bool = False
    socket_trace: bool | str = False
    # --owner-capture (Android/Linux). Delegate full capture to the AppTap library
    # to acquire an *app-scoped* pcap (kernel UID-scoped: NFLOG where supported,
    # else a socket-table filter) instead of capturing the whole device and
    # post-filtering via the Frida socket trace. owner_strict / owner_no_dns narrow
    # the capture breadth; owner_nflog_group selects the Tier-2 NFLOG group.
    owner_capture: bool = False
    owner_strict: bool = False
    owner_no_dns: bool = False
    owner_nflog_group: int = 30
    filter_expression: Optional[str] = None  # Wireshark-like display filter
    # Drop frida/adb infrastructure traffic (ports 5037/5555/27042/27043) by default
    filter_infrastructure: bool = True
    # Include loopback/localhost traffic (e.g. a client talking to a local server,
    # Firefox NSS IPC). OFF by default; opt-in via --loopback, which makes a full
    # capture (-f) also sniff the loopback adapter and the pipeline keep loopback traffic.
    include_loopback: bool = False
    # Auto-relabel a Windows SChannel/lsass keylog by trial decryption against the
    # full capture at teardown (fixes TLS 1.3 label swaps + ??? client_randoms so the
    # keylog loads directly in Wireshark). ON by default; disable with --no-auto-relabel.
    auto_relabel: bool = True
    # A helper session that contributes to ANOTHER session's outputs: the Windows
    # LSASS worker (``hook_lsass`` in friTap.py) shares the target session's -p,
    # -k and --json paths. Its JSON session_info is recorded under
    # "auxiliary_sessions" so "session_info" stays the target session's.
    auxiliary_session: bool = False
    # Live passive-analysis ("scan") of observed traffic during capture.
    # ``scan`` is an analyzer spec (None disables; "all" / comma-list selects).
    scan: Optional[str] = None
    scan_report: str = "table"
    scan_report_out: Optional[str] = None
    scan_min_severity: str = "info"
    scan_min_confidence: float = 0.0
    scan_source: Optional[str] = None
    scan_category: Optional[str] = None
    scan_show_pii: bool = False
    # External analyzer references ("module" or "module:Class") to load for the
    # live scan, mirroring offline ``analyze --analyzer-path``. Repeatable.
    scan_analyzer_path: Optional[List[str]] = None

    def __post_init__(self) -> None:
        if self.json_output is not None:
            self.set_json_target(self.json_output)
        self.resolve_pcap_container()

    def resolve_pcap_container(self) -> str:
        """Resolve ``pcap_container`` from ``pcap`` + ``output_format`` and return it.

        :class:`FriTapConfig` calls it again when it is built, so builder
        changes made after construction (``FriTap.pcapng()``) are picked up.
        """
        from .pcap_utility import resolve_pcap_format
        self.pcap_container = resolve_pcap_format(self.pcap, self.output_format)
        return self.pcap_container

    def set_json_target(self, path: Optional[str]) -> None:
        """Select the -j target: a file path, or ``-`` for the stdout stream."""
        from .output.stdout_stream import is_stdout_target
        self.json_stream = is_stdout_target(path)
        self.json_output = None if self.json_stream else path

    @property
    def wants_json(self) -> bool:
        """Any -j output (file or the stdout stream) was requested."""
        return bool(self.json_output or self.json_stream)

friTap.config.HookingConfig dataclass

Configuration for hooking strategies.

Source code in friTap/config.py
@dataclass
class HookingConfig:
    """Configuration for hooking strategies."""
    offsets: Optional[str] = None
    patterns: Optional[str] = None
    experimental: bool = False
    # Wine GDB key-extraction route (Linux): "auto" detects Wine targets and uses
    # the GDB extractor, "on" forces it on a local Linux target, "off" never uses
    # it (falls through to the normal Frida flow). See SSL_Logger._should_use_wine_gdb.
    wine_mode: str = "auto"
    enable_default_fd: bool = False
    anti_root: bool = False
    payload_modification: bool = False
    library_scan: bool = False
    # QUIC plaintext capture boundary: "stream" (default lower-boundary
    # stream-level Readv hooks) or "app-api" (Boundary-4 decoded HTTP/3
    # headers; Chrome/Android Google QUICHE only).
    quic_capture_mode: str = "stream"
    # When True, the agent installs ONLY the Google QUICHE hooks and skips every
    # TLS-library hook (BoringSSL, Conscrypt, NSS, …), the Java hooks, OHTTP,
    # and the keylog scan-result hooks. Useful when the user only wants HTTP/3
    # capture: attach is dramatically lighter (no multi-megabyte Memory.scanSync
    # passes, no Java VM safepoint sync), which also helps fritap attach to a
    # target that is already in the middle of active QUIC traffic.
    quic_only: bool = False
    # When True, the agent skips the inline android_dlopen_ext loader hook. This
    # is the hook PairIP / anti-tamper runtimes detect and SIGSEGV on during a
    # spawn-time integrity scan. Only already-loaded /
    # explicitly-selected TLS libraries are then hooked. The agent also auto-
    # skips it in spawn mode when an anti-tamper library is detected.
    no_loader_hook: bool = False
    # EXPERIMENTAL (Android). Watch android_dlopen_ext via a hardware breakpoint
    # (ARM64 debug registers, no linker code patch) instead of the inline
    # trampoline, so late-loaded TLS libs can be hooked on PairIP-protected apps
    # without tripping the anti-tamper scan. Unvalidated on-device; default OFF.
    stealth_loader: bool = False
    # --pairip-safe (Android; attach and spawn). Minimal, scan-free capture mode
    # for PairIP-protected apps: hook only a curated TLS-library allowlist
    # (libssl.so, libhttpengine.so, libjavacrypto.so, libconscrypt*,
    # libcommerce_http_client.so, offset-based libwebviewchromium.so; libunity.so
    # is opt-in via --offsets), resolved WITHOUT any Memory.scan (exports ->
    # symbols -> offsets). Skips the loader hook, WebView/Cronet pattern scan,
    # Java hooks, OHTTP and the library-scan pass: the broad footprint that trips
    # PairIP's periodic integrity check (an in-process SIGSEGV). Keys persist via
    # "blink" (hooks toggled so .text stays pristine between scans). Default OFF.
    pairip_safe: bool = False
    # --probe. Dry-run diagnostic: the agent reports which platform branch it
    # selected and then stops BEFORE installing any hook. Exists because a
    # target that dies during instrumentation leaves the user with no way to
    # learn how far friTap got. Boolean on purpose: staged verbosity
    # levels would duplicate -v/-do.
    probe: bool = False
    # --boringssl-anchor-only (Android arm64; debugging aid). Force friTap's
    # last-resort BoringSSL keylog tier (the "anchor locator") by skipping the
    # byte-pattern tier, so a fully-stripped BoringSSL lib (Chrome libchrome.so,
    # libhttpengine.so) routes straight to onAllKeylogTiersMissed -> tier 4. Lets
    # the anchor locator be exercised on modules a pattern would otherwise win.
    # Still honours --pairip-safe (tier 4 is a memory scan). Default OFF.
    force_anchor_locator: bool = False
    # --attribute-sdk: capture the calling thread's backtrace once per connection
    # at the first TLS plaintext hook and emit it as a `caller_stack` message for
    # SDK / app attribution. Metadata only; never alters captured traffic.
    attribute_sdk: bool = False
    # Override which layer of the HTTP/3 egress-headers fallback chain the
    # agent actually attaches to. "auto" (default) keeps the winner-takes-all
    # logic: quiche-internal QuicSpdyStream::WriteHeaders preferred, then
    # net::QuicChromiumClientStream::WriteHeaders, then
    # quic::QuicSpdySession::WriteHeadersOnHeadersStream as a last-resort gQUIC
    # fallback. Set to "chrome-shim" or "session-level" to FORCE a fallback
    # layer for testing, e.g. to validate the chain on builds where
    # the quiche-internal layer still resolves. Only effective in app-api mode.
    quic_egress_headers_layer: str = "auto"
    # Generic memory-region key-scan target (--scan-keys-region). None disables
    # the scan. Passed through to the agent via config_batch.extensions.scan_region;
    # protocol-agnostic (the public scan engine and any private scan binding both
    # read it). See agent/shared/scan/.
    scan_keys_region: Optional[str] = None
    # Heap secret-scanner (--memory-scan / -ms). When True, friTap loads a
    # separate, independently-injected agent (friTap/fritap_memscan.js) that
    # recovers TLS secrets by scanning process heap memory instead of hooking
    # the TLS library. It is complementary to the normal capture: passed alone,
    # it is the ONLY thing loaded (the main TLS-hooking agent is skipped);
    # combined with -k/-p/-c it runs alongside them. memory_scan_patterns is the
    # optional -ms value: either a path to a user profile file that
    # overrides/extends the shipped friTap/memory_scanning/patterns.json,
    # OR a targeted engine name (boringssl | schannel | rc4) / profile id.
    # The resolver (memory_scanning.loader.select_profiles) disambiguates by
    # resolution order: an existing file wins, then a known engine/id, so this
    # single field carries both meanings. memory_scan_interval is the poll
    # cadence (seconds) at which the host drives the scanner's scanOnce().
    memory_scan: bool = False
    memory_scan_patterns: Optional[str] = None
    memory_scan_interval: float = 2.0
    # Opt-in (--ms-emit-unconfirmed): also write memory-scan key candidates the
    # confirmation oracle could not verify (MTProto auth/E2E) to the keylog. OFF
    # by default; harmless offline since a wrong candidate matches no record.
    memory_scan_emit_unconfirmed: bool = False
    # RC4 memory-scanner oracles (--ms-rc4-known-plaintext / --ms-rc4-ciphertext).
    # There is no SSPI oracle on Android, so these inject a known-plaintext prefix
    # (an exact, zero-false-accept key test) and/or a ciphertext sample (for
    # trial-decrypt scoring) into the rc4 profile's params via
    # memory_scanning.loader.apply_rc4_param_overrides. Each accepts hex or plain
    # text; None (the default) leaves the profile untouched.
    memory_scan_rc4_known_plaintext: Optional[str] = None
    memory_scan_rc4_ciphertext: Optional[str] = None
    # Whether the main hooking agent (interception) runs at all. The TUI's
    # "Key Extraction Method" step sets this False for a memory-scan-only run;
    # see memory_scan_only(). The CLI leaves it True and relies on inference.
    intercept: bool = True
    encapsulated_protocols: Dict[str, bool] = field(
        default_factory=lambda: {"ohttp": True}
    )
    # Module names that should bypass the Cronet-split-topology suppression
    # check, even when friTap would otherwise treat them as covered by a
    # sibling library. Accepts literal names, prefixes (a value ending in '*'
    # is treated as a stem prefix), or regexes prefixed with "re:".
    force_scan_modules: List[str] = field(default_factory=list)

    @property
    def ohttp_enabled(self) -> bool:
        return self.encapsulated_protocols.get("ohttp", True)

    def __post_init__(self) -> None:
        env_value = os.environ.get("FRITAP_FORCE_SCAN")
        if env_value:
            extra = [item.strip() for item in env_value.split(",") if item.strip()]
            for item in extra:
                if item not in self.force_scan_modules:
                    self.force_scan_modules.append(item)

HookingConfig exposes the encapsulated-protocol toggle ohttp_enabled (OHTTP is on by default within --protocol tls) and the QUIC knobs quic_capture_mode, quic_only, and quic_egress_headers_layer.

Migrating from legacy parameters

FriTapConfig.from_legacy_params(...) bridges the old flat SSL_Logger keyword arguments (app, pcap_name, keylog, mobile, patterns, …) into the structured dataclasses, so existing code can move incrementally:

from friTap import FriTapConfig

config = FriTapConfig.from_legacy_params(
    app="com.example.app",
    pcap_name="capture.pcap",
    keylog="keys.log",
    mobile=True,
)
print(config.target, config.device.mobile, config.output.pcap)   # offline / CI-runnable

Working with flows

A Flow is friTap's reconstructed picture of one connection: endpoints, timing, parsed request/response, a protocol layer stack, and any attached findings. Flows are produced live (via on_flow) and read back from .tap files (see Reading .tap).

friTap.flow.models.Flow dataclass

Source code in friTap/flow/models.py
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
@dataclass
class Flow:
    # Identity
    flow_id: str = ""
    connection_id: str = ""
    src_addr: str = ""
    src_port: int = 0
    dst_addr: str = ""
    dst_port: int = 0
    ssl_session_id: str = ""

    # Transport of the connection: "tls" (default) or "quic"; stamped from ``event.protocol``
    # at creation (FlowCollector._stamp_metadata). LayerPipeline reads it to pick TlsLayer vs QuicLayer.
    transport: str = "tls"

    # State
    state: FlowState = FlowState.ACTIVE
    started: float = field(default_factory=time.time)
    ended: float = 0.0

    # Parsed (from parsers module)
    request: Optional[ParseResult] = None
    response: Optional[ParseResult] = None

    # OHTTP inner payloads (decrypted bhttp, from NSS HPKE hooks)
    ohttp_inner_request: Optional[ParseResult] = None
    ohttp_inner_response: Optional[ParseResult] = None

    # Trailing data (unconsumed bytes after valid WebSocket/protocol frames)
    trailing_bytes: Optional[bytes] = None
    trailing_protocol: str = ""
    trailing_parse: Optional[ParseResult] = None
    # Trailing data left in the READ (response) direction; the trailing_* slot above keeps
    # write-direction / direction-less leftovers so direction-less parsers are unchanged.
    response_trailing_bytes: Optional[bytes] = None
    response_trailing_protocol: str = ""
    response_trailing_parse: Optional[ParseResult] = None

    # Raw
    chunks: list[FlowChunk] = field(default_factory=list)

    # Cached byte total (incremented by FlowCollector when appending chunks).
    _total_bytes: int = 0

    # True once the flow exceeded the per-flow retained-payload cap (MAX_FLOW_CHUNK_BYTES):
    # ``_total_bytes`` keeps counting but later chunks are not retained.
    truncated: bool = False

    # Protocol detected by the parser registry even without a request/response yet; display fallback before "unknown".
    detected_protocol: str = ""

    # Schema v2 additive enrichment fields (all optional).

    # Role labels for the endpoints: ``src_*``/``dst_*`` stay the canonical transport endpoints;
    # these name the same ends as local (instrumented process) / remote (peer). Stored fields
    # (set by tap_format.decode_flow and the collector, persisted as their own meta keys),
    # derived from src/dst in ONE place, FlowCollector._enrich_flow; everything else only reads them.
    local_addr: str = ""
    local_port: int = 0
    remote_addr: str = ""
    remote_port: int = 0

    # Process / package identity (from the capture target / agent).
    process_name: str = ""
    package_name: str = ""
    # Origin of the flow's first event when friTap follows several processes
    # (FriTapEvent.process_origin): pid and role ("target", "child", "spawn").
    # Persisted into the .tap meta only when set.
    pid: Optional[int] = None
    process_role: str = ""

    # Resolved SDK attribution (``--attribute-sdk``): the trusted ``{"sdk", "vendor", "category"}``
    # dict from the bundled signature table (friTap/analysis/attribution/sdk.py), or ``None``.
    # Persisted into the .tap meta only when set.
    sdk: Optional[dict] = None

    # Compact caller stack of the flow's connection (``--attribute-sdk``), stored whether or not it
    # matched an SDK: ``{transport_module, thread_name, tid, pid, native_frames[:8], java_frames[:8]}``,
    # strings escaped and capped at 80 chars (friTap/caller_stack_shapes.py). Target-controlled
    # text; ``None`` when no stack was joined. Persisted only when set.
    caller_stack: Optional[dict] = None

    # Transport vs originator attribution (friTap/flow/attribution.py): ``{"transport": {module, kind},
    # "originator": {sdk, vendor, category}, "confidence", "source", "bucket", "mixed": True}``, every
    # key optional. Transport is stamped on every TLS/QUIC flow with a known library; originator only
    # when a classified caller stack joined.
    attribution: Optional[dict] = None

    # True while src/dst (and local/remote) are a synthetic 198.18.0.0/15 pin for an fd-less
    # connection (option 5, friTap/endpoint_pinner.py); cleared when the real tuple is observed.
    # Persisted only when True.
    endpoints_synthetic: bool = False

    # Ordered layer stack (outermost transport -> innermost), e.g. [tls, http2]; per-flow LINEAR
    # (HTTP/2-3 streams are separate flows). ``flow.tls`` / ``flow.quic`` / ``flow.ssh`` resolve
    # by name via __getattr__ (lazily created empty, so ``flow.tls.sni`` is never None). Whole-layer
    # writes go through add_layer/set_layer; field mutation works on the cached instance.
    layers: list[ProtocolLayer] = field(default_factory=list, repr=False)

    # Hook origin: ``hook_function`` mirrors the dominant chunk.function; ``hook_stack`` is reserved.
    hook_function: str = ""
    hook_stack: str = ""

    # Mutable analyst annotations.
    tags: list[str] = field(default_factory=list)
    notes: str = ""
    # Attached findings, typed loosely to avoid importing ``friTap.analysis`` here (it imports models under TYPE_CHECKING).
    findings: list = field(default_factory=list)

    # (chunks list, scanned length, id so far or _MIXED_STREAMS) backing
    # ``stream_id``.
    _stream_id_cache: Optional[tuple] = field(
        default=None, init=False, repr=False, compare=False)

    @property
    def stream_id(self) -> Optional[int]:
        """Real transport stream id when every id-carrying chunk agrees (None if none or mixed).

        Chunks are append-only, so the cache is keyed on the list object (held, so its id
        cannot be reused) and only the new tail is scanned.
        """
        chunks = self.chunks
        # Snapshot the length first: chunks appended concurrently after this
        # point stay unscanned for the next call.
        end = len(chunks)
        cache = self._stream_id_cache
        if cache is not None and cache[0] is chunks and cache[1] <= end:
            scanned, found = cache[1], cache[2]
        else:
            scanned, found = 0, None
        if found is not _MIXED_STREAMS:
            for index in range(scanned, end):
                sid = chunks[index].stream_id
                if sid is None:
                    continue
                if found is None:
                    found = sid
                elif sid != found:
                    found = _MIXED_STREAMS
                    break
        if end != scanned:
            self._stream_id_cache = (chunks, end, found)
        return None if found is _MIXED_STREAMS else found

    # ------------------------------------------------------------------
    # Protocol layer access (flow.<protocol> -> typed ProtocolLayer)
    # ------------------------------------------------------------------

    def __getattr__(self, name: str):
        # Only invoked when normal lookup fails. Resolves a registered protocol name to its
        # layer, lazily creating an empty one (so flow.tls/flow.quic are never None); anything
        # else raises AttributeError so getattr(flow, x, default) (filter engine, copy/pickle
        # probing) keeps working. This attaches by design (``flow.tls.version = ...`` persists):
        # PURE-READ probes (serializers, snapshots) must use the non-mutating :meth:`layer`.
        if name.startswith("_"):
            raise AttributeError(name)
        layers = self.__dict__.get("layers")
        if layers is None:
            # Half-constructed (e.g. during copy/unpickle before fields set).
            raise AttributeError(name)
        desc = get_registry().get(name)
        if desc is None:
            raise AttributeError(name)
        for ly in layers:
            if ly.name == name:
                return ly
        return self._create_layer(name, desc)

    def _create_layer(self, name: str, desc) -> ProtocolLayer:
        layer = desc.layer_cls()
        # Per-instance name so generic layers (AppLayer, one class under several names) report the right ``name``.
        layer._name = name
        layer.depth = len(self.layers)
        layer._flow = self
        if desc.data_source == "chunks":
            layer.data = LayerData(data_source="chunks", _owner=self)
        self.layers.append(layer)
        return layer

    def layer(self, name: str) -> Optional[ProtocolLayer]:
        """Return the layer named *name*, or None if not present."""
        for ly in self.layers:
            if ly.name == name:
                return ly
        return None

    def add_layer(self, layer: ProtocolLayer) -> ProtocolLayer:
        """Append *layer* to the stack, linking parent/child + binding data."""
        layer.depth = len(self.layers)
        if self.layers:
            parent = self.layers[-1]
            parent.child = layer
            layer.parent = parent
        layer._flow = self
        if layer.data.data_source == "chunks" and layer.data._owner is None:
            layer.data._owner = self
        self.layers.append(layer)
        return layer

    def set_layer(self, layer: ProtocolLayer) -> ProtocolLayer:
        """Replace an existing same-name layer in place, else append it."""
        for i, existing in enumerate(self.layers):
            if existing.name == layer.name:
                layer.depth = existing.depth
                layer.parent, layer.child = existing.parent, existing.child
                layer._flow = self
                if layer.data.data_source == "chunks" and layer.data._owner is None:
                    layer.data._owner = self
                self.layers[i] = layer
                return layer
        return self.add_layer(layer)

    def __copy__(self) -> "Flow":
        # Shallow copy that re-lists the mutable containers (chunks, layers) so appends to a live
        # flow do not mutate a snapshot; elements are shared.
        new = Flow.__new__(Flow)
        new.__dict__.update(self.__dict__)
        new.chunks = list(self.chunks)
        new.layers = list(self.layers)
        return new

    @property
    def has_trailing_data(self) -> bool:
        return self.trailing_bytes is not None

    @property
    def has_response_trailing_data(self) -> bool:
        return self.response_trailing_bytes is not None

    @property
    def segments(self) -> list[dict]:
        """Segment descriptors for multi-protocol rendering.

        Each is a dict with keys type ('parsed'|'raw'), protocol, parse_result (or None),
        data (raw bytes for 'raw'), source. The response-direction trailing segment
        (source ``"response_trailing"``) is last. The TUI addresses segments PER PANE:
        segment 1 is that pane's own trailing segment (see ``pane_segments``).
        """
        segs: list[dict] = []
        if self.request is not None:
            segs.append({
                "type": "parsed",
                "protocol": self.display_protocol,
                "parse_result": self.request,
                "source": "primary",
            })
        segs.extend(self._trailing_segments(
            self.trailing_bytes, self.trailing_protocol,
            self.trailing_parse, "trailing"))
        segs.extend(self._trailing_segments(
            self.response_trailing_bytes, self.response_trailing_protocol,
            self.response_trailing_parse, "response_trailing"))
        return segs

    def pane_segments(self, pane: str) -> list[dict]:
        """Segments shown on one detail pane: primary plus that pane's trailing.

        *pane* is ``"request"`` or ``"response"``; index 0 is the primary
        message, index 1 (when present) the pane's trailing segment.
        """
        trailing_source = "response_trailing" if pane == "response" else "trailing"
        return [s for s in self.segments
                if s["source"] in ("primary", trailing_source)]

    @staticmethod
    def _trailing_segments(data: Optional[bytes], protocol: str,
                           parse: Optional[ParseResult], source: str) -> list[dict]:
        """0 or 1 segment descriptor for one trailing slot."""
        if parse is not None:
            return [{"type": "parsed", "protocol": protocol,
                     "parse_result": parse, "source": source}]
        if data:
            return [{"type": "raw", "protocol": protocol or "unknown",
                     "data": data, "source": source}]
        return []

    @property
    def duration(self) -> float:
        if self.ended > 0:
            return self.ended - self.started
        return time.time() - self.started

    @property
    def display_method(self) -> str:
        return _display.display_method_layered(self)

    @property
    def display_host(self) -> str:
        return _display.display_host(self.request, self.dst_addr, self.dst_port)

    @property
    def display_status(self) -> str:
        return _display.display_status(self.response)

    @property
    def display_size(self) -> str:
        total_bytes = self._total_bytes
        if total_bytes == 0 and self.chunks:
            total_bytes = sum(len(c.data) for c in self.chunks)
        return _display.display_size(self.response, total_bytes)

    @property
    def display_protocol(self) -> str:
        return _display.display_protocol(self)

    @property
    def has_request_data(self) -> bool:
        if self.request is not None:
            return True
        return any(c.direction == "write" for c in self.chunks)

    @property
    def has_response_data(self) -> bool:
        if self.response is not None:
            return True
        return any(c.direction == "read" for c in self.chunks)

    def get_direction_bytes(self, direction: str, max_bytes: int = 0) -> bytes:
        """Get concatenated raw bytes for a given direction ('read' or 'write').

        If *max_bytes* > 0, stop accumulating after that many bytes.
        """
        if max_bytes <= 0:
            return b"".join(c.data for c in self.chunks if c.direction == direction)
        parts: list[bytes] = []
        total = 0
        for c in self.chunks:
            if c.direction != direction:
                continue
            parts.append(c.data)
            total += len(c.data)
            if total >= max_bytes:
                break
        return b"".join(parts)

    @property
    def display_source(self) -> str:
        return _display.display_source(self.src_addr, self.src_port)

    @property
    def display_connection(self) -> str:
        """Directional connection string: src -> dst, src <- dst, or src <-> dst."""
        return _display.display_connection(
            self.request if self.has_request_data else None,
            self.response if self.has_response_data else None,
            self.src_addr, self.src_port, self.dst_addr, self.dst_port,
            literal_direction=_display.is_message_transport(self),
        )

    def to_dict(self, include_bodies: bool = False) -> dict:
        """Return a JSON-safe dict view of this flow for API/web consumers.

        Covers identity, transport/timing, parsed request/response, layer names, annotations
        and findings. Chunk bytes are never included; bodies only (hex) when *include_bodies*.
        Uses the non-mutating :meth:`layer` so serializing never grows the layer stack.
        """
        tls = self.layer("tls")
        total_bytes = self._total_bytes or sum(len(c.data) for c in self.chunks)
        out = {
            "flow_id": self.flow_id,
            "connection_id": self.connection_id,
            "src_addr": self.src_addr,
            "src_port": self.src_port,
            "dst_addr": self.dst_addr,
            "dst_port": self.dst_port,
            "ssl_session_id": self.ssl_session_id,
            "transport": self.transport,
            "state": self.state.value if isinstance(self.state, FlowState) else str(self.state),
            "started": self.started,
            "ended": self.ended,
            # Deterministic: avoid the wall-clock ``duration`` for in-progress flows.
            "duration": (self.ended - self.started) if self.ended > 0 else 0.0,
            "protocol": self.display_protocol,
            "detected_protocol": self.detected_protocol,
            "total_bytes": total_bytes,
            "process_name": self.process_name,
            "package_name": self.package_name,
            "tls_sni": tls.sni if tls is not None else "",
            "tls_alpn": tls.alpn if tls is not None else "",
            "layers": [ly.name for ly in self.layers],
            "tags": list(self.tags),
            "notes": self.notes,
            "request": self.request.to_dict(include_body=include_bodies) if self.request else None,
            "response": self.response.to_dict(include_body=include_bodies) if self.response else None,
            "findings": [f.to_dict() for f in self.findings],
        }
        return put_stream_id(out, self.stream_id)


        # Cache for reconstructed bodies: {direction: bytes}.
    _body_cache: dict = field(default_factory=dict, repr=False)

    def reconstruct_body(self, direction: str) -> bytes:
        """Reconstruct the body for *direction* from raw chunks, cached per direction."""
        if direction in self._body_cache:
            return self._body_cache[direction]

        proto = self.display_protocol.lower()
        if "http/2" in proto:
            body = self._extract_h2_body(direction)
        elif "http/3" in proto:
            body = self._extract_h3_body(direction)
        elif "http/1" in proto:
            body = self._extract_h1_body(direction)
        elif "websocket" in proto:
            body = self._extract_ws_body(direction)
        else:
            body = b""

        self._body_cache[direction] = body
        return body

    def invalidate_body_cache(self) -> None:
        """Clear cached body data (call when chunks change)."""
        self._body_cache.clear()

    def _extract_h1_body(self, direction: str) -> bytes:
        """Extract HTTP/1 body by re-parsing raw bytes with h11."""
        try:
            import h11
        except ImportError:
            return b""
        raw = self.get_direction_bytes(direction)
        if not raw:
            return b""
        conn = h11.Connection(
            h11.CLIENT if direction == "read" else h11.SERVER
        )
        conn.receive_data(raw)
        body_parts: list[bytes] = []
        while True:
            try:
                event = conn.next_event()
            except h11.ProtocolError:
                # Malformed (attacker-controlled) framing: keep what parsed.
                break
            if event is h11.NEED_DATA or event is h11.PAUSED:
                break
            if isinstance(event, h11.Data):
                body_parts.append(bytes(event.data))
            elif isinstance(event, h11.EndOfMessage):
                break
        return b"".join(body_parts)

    def _extract_h2_body(self, direction: str) -> bytes:
        """Extract HTTP/2 DATA payloads from raw chunks (stateless scan, no HPACK).

        Without a parsed message the flow's own stream is used; stream id 0 (unknown) keeps every stream's DATA.
        """
        from friTap.parsers.http2_dataframe import http2_data_for_stream

        msg = self.request if direction == "write" else self.response
        if msg is not None:
            stream_id = msg.stream_id
        else:
            stream_id = self.stream_id or 0
        body, _degraded = http2_data_for_stream(
            self.get_direction_bytes(direction), stream_id
        )
        return body

    def _extract_h3_body(self, direction: str) -> bytes:
        """Extract HTTP/3 DATA frame payloads using varint framing."""
        from friTap.parsers.http3 import iter_h3_data_frames

        return b"".join(iter_h3_data_frames(self.get_direction_bytes(direction)))

    def _extract_ws_body(self, direction: str) -> bytes:
        """Extract WebSocket payload — body is already in ParseResult for WS."""
        msg = self.request if direction == "write" else self.response
        if msg is not None and msg.body:
            return msg.body
        return b""


    @staticmethod
    def _get_header(msg: "Optional[ParseResult]", name: str, default: str = "") -> str:
        if msg is None:
            return default
        lower_name = name.lower()
        for key, value in msg.headers.items():
            if key.lower() == lower_name:
                return value
        return default

    def _get_decompressed_body(
        self,
        msg: "Optional[ParseResult]",
        direction: str,
        encoding_override: str = "",
        max_out: Optional[int] = None,
    ) -> bytes:
        """Decompressed body of *msg*, output bounded by *max_out*.

        Defaults to ``MAX_DECOMPRESSED_BODY`` (explicit saves); the automatic paths pass the smaller
        ``MAX_AUTO_DECOMPRESS``. Cached in ``_body_cache`` per (direction, encoding, max_out).
        """
        if msg is None:
            return b""
        body = msg.body if msg.body else self.reconstruct_body(direction)
        if not body:
            return b""
        encoding = encoding_override or msg.content_encoding or self._content_encoding_header(msg)
        if not encoding:
            return body
        from friTap.parsers.decompress import MAX_DECOMPRESSED_BODY, decompress_body
        limit = MAX_DECOMPRESSED_BODY if max_out is None else max_out
        key = ("decompressed", direction, encoding, limit)
        cached = self._body_cache.get(key)
        if cached is not None and cached[0] is msg and cached[1] is body:
            return cached[2]
        data, _err = decompress_body(body, encoding, limit)
        self._body_cache[key] = (msg, body, data)
        return data

    @staticmethod
    def _content_encoding_header(msg: "ParseResult") -> str:
        """The message's Content-Encoding header value, or ``""``."""
        for key, value in msg.headers.items():
            if key.lower() == "content-encoding":
                return value
        return ""

    def get_request_header(self, name: str, default: str = "") -> str:
        """Get a request header value by name (case-insensitive)."""
        return self._get_header(self.request, name, default)

    def get_response_header(self, name: str, default: str = "") -> str:
        """Get a response header value by name (case-insensitive)."""
        return self._get_header(self.response, name, default)

    @property
    def request_body(self) -> bytes:
        """The request body (possibly compressed), or ``b""``."""
        if self.request and self.request.body:
            return self.request.body
        return self.reconstruct_body("write")

    @property
    def response_body(self) -> bytes:
        """The response body (possibly compressed), or ``b""``."""
        if self.response and self.response.body:
            return self.response.body
        return self.reconstruct_body("read")

    def get_decompressed_request_body(self) -> bytes:
        """Return the request body after decompressing (gzip/br/zstd/deflate)."""
        from friTap.parsers.decompress import MAX_AUTO_DECOMPRESS
        return self._get_decompressed_body(self.request, "write", max_out=MAX_AUTO_DECOMPRESS)

    def get_decompressed_response_body(self) -> bytes:
        """Return the response body after decompressing (gzip/br/zstd/deflate)."""
        from friTap.parsers.decompress import MAX_AUTO_DECOMPRESS
        return self._get_decompressed_body(self.response, "read", max_out=MAX_AUTO_DECOMPRESS)

    @property
    def response_content_type(self) -> str:
        """The response Content-Type, or ``""`` if unavailable."""
        if self.response is not None and self.response.content_type:
            return self.response.content_type
        return self.get_response_header("content-type")

    @property
    def request_content_type(self) -> str:
        """The request Content-Type, or ``""`` if unavailable."""
        if self.request is not None and self.request.content_type:
            return self.request.content_type
        return self.get_request_header("content-type")

    def decode_request_protobuf(
        self,
        schema_path: Optional[str] = None,
        force: bool = False,
        message_type: Optional[str] = None,
    ) -> Optional["ProtobufView"]:
        """Decode the request body as protobuf (gRPC framing auto-detected).

        Returns a :class:`~friTap.parsers.protobuf.view.ProtobufView`, or ``None`` when the
        body is empty or not protobuf-like. A view may hold no decoded message: check
        ``view.ok``, ``view.notes`` / ``view.error``.

        Args:
            schema_path: Optional ``.proto`` or compiled ``.desc`` descriptor set, used with *message_type*.
                Without both, the wire decoder runs and a note says why.
            force: If ``True``, decode regardless of content type.
            message_type: Fully qualified message type for *schema_path*.
        """
        return self._decode_protobuf(
            self.get_decompressed_request_body(),
            self.request_content_type,
            self.get_request_header("grpc-encoding"),
            schema_path=schema_path,
            force=force,
            message_type=message_type,
        )

    def decode_response_protobuf(
        self,
        schema_path: Optional[str] = None,
        force: bool = False,
        message_type: Optional[str] = None,
    ) -> Optional["ProtobufView"]:
        """Decode the response body as protobuf (gRPC framing auto-detected).

        Same contract as :meth:`decode_request_protobuf`, for the response.
        """
        return self._decode_protobuf(
            self.get_decompressed_response_body(),
            self.response_content_type,
            self.get_response_header("grpc-encoding"),
            schema_path=schema_path,
            force=force,
            message_type=message_type,
        )

    @staticmethod
    def _decode_protobuf(
        body: bytes,
        content_type: str,
        grpc_encoding: str,
        schema_path: Optional[str] = None,
        force: bool = False,
        message_type: Optional[str] = None,
    ) -> Optional["ProtobufView"]:
        """Internal: gate on content type, then delegate to the shared view decoder."""
        if not body:
            return None

        from friTap.parsers.protobuf import (
            decode_protobuf_view,
            is_grpc_content_type,
            is_likely_protobuf,
            is_protobuf_content_type,
        )

        is_proto_ct = is_grpc_content_type(content_type) or is_protobuf_content_type(content_type)
        if not force and not is_proto_ct and not is_likely_protobuf(body):
            return None

        return decode_protobuf_view(
            body,
            content_type=content_type,
            grpc_encoding=grpc_encoding,
            schema_path=schema_path or "",
            message_type=message_type or "",
        )

request_body property

The request body (possibly compressed), or b"".

response_body property

The response body (possibly compressed), or b"".

stream_id property

Real transport stream id when every id-carrying chunk agrees (None if none or mixed).

Chunks are append-only, so the cache is keyed on the list object (held, so its id cannot be reused) and only the new tail is scanned.

to_dict(include_bodies=False)

Return a JSON-safe dict view of this flow for API/web consumers.

Covers identity, transport/timing, parsed request/response, layer names, annotations and findings. Chunk bytes are never included; bodies only (hex) when include_bodies. Uses the non-mutating :meth:layer so serializing never grows the layer stack.

Source code in friTap/flow/models.py
def to_dict(self, include_bodies: bool = False) -> dict:
    """Return a JSON-safe dict view of this flow for API/web consumers.

    Covers identity, transport/timing, parsed request/response, layer names, annotations
    and findings. Chunk bytes are never included; bodies only (hex) when *include_bodies*.
    Uses the non-mutating :meth:`layer` so serializing never grows the layer stack.
    """
    tls = self.layer("tls")
    total_bytes = self._total_bytes or sum(len(c.data) for c in self.chunks)
    out = {
        "flow_id": self.flow_id,
        "connection_id": self.connection_id,
        "src_addr": self.src_addr,
        "src_port": self.src_port,
        "dst_addr": self.dst_addr,
        "dst_port": self.dst_port,
        "ssl_session_id": self.ssl_session_id,
        "transport": self.transport,
        "state": self.state.value if isinstance(self.state, FlowState) else str(self.state),
        "started": self.started,
        "ended": self.ended,
        # Deterministic: avoid the wall-clock ``duration`` for in-progress flows.
        "duration": (self.ended - self.started) if self.ended > 0 else 0.0,
        "protocol": self.display_protocol,
        "detected_protocol": self.detected_protocol,
        "total_bytes": total_bytes,
        "process_name": self.process_name,
        "package_name": self.package_name,
        "tls_sni": tls.sni if tls is not None else "",
        "tls_alpn": tls.alpn if tls is not None else "",
        "layers": [ly.name for ly in self.layers],
        "tags": list(self.tags),
        "notes": self.notes,
        "request": self.request.to_dict(include_body=include_bodies) if self.request else None,
        "response": self.response.to_dict(include_body=include_bodies) if self.response else None,
        "findings": [f.to_dict() for f in self.findings],
    }
    return put_stream_id(out, self.stream_id)

layer(name)

Return the layer named name, or None if not present.

Source code in friTap/flow/models.py
def layer(self, name: str) -> Optional[ProtocolLayer]:
    """Return the layer named *name*, or None if not present."""
    for ly in self.layers:
        if ly.name == name:
            return ly
    return None

reconstruct_body(direction)

Reconstruct the body for direction from raw chunks, cached per direction.

Source code in friTap/flow/models.py
def reconstruct_body(self, direction: str) -> bytes:
    """Reconstruct the body for *direction* from raw chunks, cached per direction."""
    if direction in self._body_cache:
        return self._body_cache[direction]

    proto = self.display_protocol.lower()
    if "http/2" in proto:
        body = self._extract_h2_body(direction)
    elif "http/3" in proto:
        body = self._extract_h3_body(direction)
    elif "http/1" in proto:
        body = self._extract_h1_body(direction)
    elif "websocket" in proto:
        body = self._extract_ws_body(direction)
    else:
        body = b""

    self._body_cache[direction] = body
    return body

Stream ids

Each FlowChunk has a stream_id (int | None): the real transport stream id the chunk travelled on (HTTP/2 stream id, QUIC stream id), or None when it is unknown. Flow.stream_id is the one id shared by all id-carrying chunks of the flow, and None when there is none or more than one. Flow.to_dict() and FlowSummary.to_dict() include stream_id only when it is set. The stream_id of an HTTP/3 flow.request / flow.response is a collector-internal remapped id, not the QUIC stream id.

flow.stream_id                          # e.g. 4, or None
[ch.stream_id for ch in flow.chunks]    # per chunk

Serializing a flow

Flow.to_dict(include_bodies=False) returns a JSON-safe view: identity, transport/timing, parsed request/response, layer names, tags/notes and findings. Raw chunk bytes are never included; request/response bodies are included (hex-encoded) only when include_bodies=True.

flow_dict = flow.to_dict()                  # bodies omitted
flow_dict = flow.to_dict(include_bodies=True)  # bodies hex-encoded

Layer access

Each flow carries a stack of protocol layers. Access them two ways:

  • Attribute access — flow.tls, flow.quic, flow.ssh lazily materialize a typed layer if that protocol is registered (raises AttributeError for an unregistered name). Typed layers expose fields such as flow.tls.sni and flow.tls.alpn.
  • flow.layer(name) — a non-mutating lookup returning the existing layer or None. Use this in serialization paths so reading never grows the stack: flow.layer("http2"), then inspect .parsed / .data.
sni = flow.tls.sni                  # typed accessor
http2 = flow.layer("http2")         # None if absent
if http2 is not None:
    parsed = http2.parsed

Why pr.body may be empty

Bodies are not accumulated in the parser's ParseResult anymore — to keep .tap files small, identical/large bodies are reconstructed on demand from the flow's raw chunks. So a ParseResult (flow.request / flow.response) can have an empty .body even when data was captured. To get the bytes, reconstruct from the flow:

req = flow.request_body        # bytes, reconstructed + cached
resp = flow.response_body
# equivalently: flow.reconstruct_body("write") / flow.reconstruct_body("read")

FlowSummary

FlowSummary is the lightweight index entry used when listing flows without reading their full bodies (returned by read_flow_summaries() / get_summaries()).

friTap.flow.models.FlowSummary dataclass

Lightweight scalar snapshot of a Flow (~200 bytes, no chunks/bodies) for list display and filtering.

request/response stubs use ParseResult's attribute names for the filter engine.

Source code in friTap/flow/models.py
@dataclass(frozen=True, slots=True)
class FlowSummary:
    """Lightweight scalar snapshot of a Flow (~200 bytes, no chunks/bodies) for list display and filtering.

    ``request``/``response`` stubs use ParseResult's attribute names for the filter engine.
    """
    flow_id: str = ""
    connection_id: str = ""
    src_addr: str = ""
    src_port: int = 0
    dst_addr: str = ""
    dst_port: int = 0
    ssl_session_id: str = ""
    state: FlowState = FlowState.ACTIVE
    started: float = 0.0
    ended: float = 0.0
    # Mirrors Flow.transport ("tls", "quic", "signal", ...) for grouping without the full Flow.
    transport: str = "tls"
    request: Optional[_ParseStub] = None
    response: Optional[_ParseStub] = None
    has_ohttp: bool = False
    has_trailing_data: bool = False
    trailing_protocol: str = ""
    has_response_trailing_data: bool = False
    response_trailing_protocol: str = ""
    total_bytes: int = 0
    detected_protocol: str = ""
    # Schema v2 additive enrichment scalars (cheap to surface in list/filter views)
    process_name: str = ""
    tls_sni: str = ""
    tls_alpn: str = ""
    tag_count: int = 0
    finding_count: int = 0
    has_notes: bool = False
    # Layered protocol-display scalars (display.display_protocol_layered).
    outer_app_protocol: str = ""
    inner_e2e_protocol: str = ""
    inner_summary: str = ""
    # Primary TL operation from message-bearing layers (display.method_from_messages), shown in the Method column.
    flow_method: str = ""
    # Real transport stream id of a single-stream flow (see Flow.stream_id).
    stream_id: Optional[int] = None
    # Precomputed display-filter inputs so filtering matches on a live flow and on a row rebuilt
    # from a .tap (no layer stack): ``filter_attrs`` maps per-protocol fields (friTap.filter.layer_fields)
    # to values, ``protocols`` holds canonical names (friTap.filter.protocols). Plain dict (pickles,
    # survives asdict) and READ-ONLY; unhashable, so excluded from hash().
    filter_attrs: Mapping[str, tuple] = field(default_factory=dict, hash=False)
    protocols: frozenset[str] = frozenset()

    @property
    def duration(self) -> float:
        if self.ended > 0:
            return self.ended - self.started
        return time.time() - self.started

    @property
    def display_protocol(self) -> str:
        return _display.display_protocol(self)

    @property
    def display_protocol_layered(self) -> str:
        return _display.display_protocol_layered(self)

    @property
    def display_method(self) -> str:
        return _display.display_method_layered(self)

    @property
    def display_host(self) -> str:
        return _display.display_host(self.request, self.dst_addr, self.dst_port)

    @property
    def display_status(self) -> str:
        return _display.display_status(self.response)

    @property
    def display_size(self) -> str:
        return _display.display_size(self.response, self.total_bytes)

    @property
    def display_source(self) -> str:
        return _display.display_source(self.src_addr, self.src_port)

    @property
    def display_connection(self) -> str:
        return _display.display_connection(
            self.request, self.response,
            self.src_addr, self.src_port, self.dst_addr, self.dst_port,
            literal_direction=_display.is_message_transport(self),
        )

    # Aliases for filter engine compatibility
    @property
    def _total_bytes(self) -> int:
        return self.total_bytes

    @property
    def ohttp_inner_request(self):
        # Filter engine only checks `is not None`: return a truthy sentinel.
        return _OHTTP_SENTINEL if self.has_ohttp else None

    @property
    def ohttp_inner_response(self):
        return _OHTTP_SENTINEL if self.has_ohttp else None

    @staticmethod
    def from_flow(
        flow: "Flow", filter_attrs: Optional[Mapping[str, tuple]] = None,
    ) -> "FlowSummary":
        """Summary snapshot of a full Flow; *filter_attrs* reuses attributes already extracted for an unchanged layer stack (live rebuilds)."""
        fields = shared_summary_fields(flow, filter_attrs)
        # Prefer a scalar already set on the flow (a layerless flow rebuilt from a .tap summary),
        # since layered_scalars_from_flow returns "" without a layer stack.
        for name in ("outer_app_protocol", "inner_e2e_protocol", "inner_summary"):
            fields[name] = getattr(flow, name, "") or fields[name]
        return FlowSummary(
            **fields,
            state=flow.state,
            request=_parse_stub(flow.request),
            response=_parse_stub(flow.response),
            has_trailing_data=flow.trailing_bytes is not None,
            trailing_protocol=flow.trailing_protocol,
            has_response_trailing_data=flow.response_trailing_bytes is not None,
            response_trailing_protocol=flow.response_trailing_protocol,
            total_bytes=flow._total_bytes,
            detected_protocol=flow.detected_protocol,
            process_name=flow.process_name,
        )

    @staticmethod
    def from_tap_summary(tap_summary: Any, flow: "Flow") -> "FlowSummary":
        """Summary for a replay row: *flow* is the layerless synthetic Flow rebuilt from *tap_summary*.

        It has no layer stack or OHTTP payloads, so the filter inputs decoded from the tap's
        ``meta["layers"]`` are taken over; only protocols of a re-parsed request/response are added.
        """
        summary = FlowSummary.from_flow(flow, filter_attrs=tap_summary.filter_attrs)
        return replace(
            summary,
            protocols=tap_summary.protocols | summary.protocols,
            has_ohttp=summary.has_ohttp or tap_summary.has_ohttp,
            # The synthetic flow has no chunks to derive the stream id from.
            stream_id=(summary.stream_id if summary.stream_id is not None
                       else tap_summary.stream_id),
        )

    def to_dict(self) -> dict:
        """Return a JSON-safe, body-free overview dict.

        Same key set as :meth:`friTap.flow.tap_format.FlowSummary.to_dict`, so live and offline summaries render identically.
        """
        req, resp = self.request, self.response
        out = {
            "flow_id": self.flow_id,
            "connection_id": self.connection_id,
            "src_addr": self.src_addr,
            "src_port": self.src_port,
            "dst_addr": self.dst_addr,
            "dst_port": self.dst_port,
            "ssl_session_id": self.ssl_session_id,
            "state": self.state.value if isinstance(self.state, FlowState) else str(self.state),
            "started": self.started,
            "ended": self.ended,
            "transport": self.transport,
            # Deterministic, parity with the offline shape: the ``duration`` property uses
            # wall-clock time for in-progress flows, so a snapshot uses 0.0 until completion.
            "duration": (self.ended - self.started) if self.ended > 0 else 0.0,
            "protocol": (self.detected_protocol
                         or (req.protocol if req else "")
                         or (resp.protocol if resp else "")
                         or "unknown"),
            "method": req.method if req else "",
            "url": req.url if req else "",
            "host": req.host if req else "",
            "status_code": resp.status_code if resp else 0,
            "total_bytes": self.total_bytes,
            "detected_protocol": self.detected_protocol,
            "process_name": self.process_name,
            "tls_sni": self.tls_sni,
            "tls_alpn": self.tls_alpn,
            "tag_count": self.tag_count,
            "finding_count": self.finding_count,
            "has_notes": self.has_notes,
            "outer_app_protocol": self.outer_app_protocol,
            "inner_e2e_protocol": self.inner_e2e_protocol,
            "inner_summary": self.inner_summary,
            "flow_method": self.flow_method,
        }
        # Omitted when unknown, in parity with tap_format.FlowSummary.to_dict.
        return put_stream_id(out, self.stream_id)

Offline conversion

Reconstruct a .tap file from an existing packet capture plus its key material — no device, no live capture.

Requires Wireshark / tshark ≥ 4.x

pcap_to_tap() (and the --from-pcap CLI) shell out to tshark for dissection. If tshark is not on PATH, set tshark_path= (or the $FRITAP_TSHARK environment variable). The example below is offline but needs tshark installed.

from friTap import pcap_to_tap

result = pcap_to_tap(
    "chrome.pcap",
    keylog_path="chromekeys.log",   # SSLKEYLOGFILE; raises NoDecryptionKeysError if given but unusable
    tap_path="out.tap",
    run_scan=True,                  # also run analyzers and embed findings
)
print(result.to_dict())

Import path

pcap_to_tap is importable from the package root (from friTap import pcap_to_tap), but not from friTap.offline — re-exporting it there would shadow the friTap.offline.pcap_to_tap module. The no-manifest core is friTap.convert_pcap_to_tap(...).

pcap_to_tap() reads a manifest sidecar <pcap>.fritap.json (keys keylog/tls_ports/quic_ports) when use_manifest=True; explicit arguments win over the manifest, which wins over defaults.

Keylogs for friTap's own offline decryptors go in protocol_keylogs, a {protocol_name: keylog_path} map. It covers the built-in decryptors (mtproto, telegram, signal, rc4, schannel) and registered plugins:

result = pcap_to_tap("tg.pcapng", protocol_keylogs={"mtproto": "tg.keys"})

The older <protocol>_keylog= keywords (e.g. mtproto_keylog=) still work and are folded into protocol_keylogs; an explicit protocol_keylogs entry wins. resync_search_depth= is forwarded only to decryptors whose OfflineDecryptorEntry names the receiving keyword in resync_param (MTProto and Telegram: obf_max_blocks). An offline-decryptor plugin whose emitter takes **kwargs therefore no longer receives obf_max_blocks unless its entry sets resync_param.

It returns a ConvertResult:

friTap.offline.pcap_to_tap.ConvertResult dataclass

Summary of a pcap-to-tap conversion.

Source code in friTap/offline/pipeline_api.py
@dataclass
class ConvertResult:
    """Summary of a pcap-to-tap conversion."""
    tap_path: str
    flow_count: int = 0
    decrypted_packet_count: int = 0
    stream_count: int = 0
    # QUIC per-packet drops (e.g. misaligned id/payload lists), distinct from dropped TLS streams.
    dropped_packet_count: int = 0
    # TLS streams that could not be followed/decoded (whole-stream drops).
    dropped_stream_count: int = 0
    findings_count: int = 0
    # Encrypted streams skipped by a keyless conversion; drives the CLI's "pass --keylog" hint.
    encrypted_streams_skipped: int = 0
    # ``{counter_prefix: {"messages": 6, ...}}`` via :meth:`record_protocol`; the frozen v1 flat counters are read-only views of it.
    per_protocol: dict = field(default_factory=dict)

    def record_protocol(self, prefix: str, **counters) -> None:
        """Accumulate one decryptor's counters into ``per_protocol[prefix]``.

        A new bucket starts as a deep copy of the frozen v1 defaults
        (:data:`~friTap.offline.result_schema.V1_BUCKET_DEFAULTS`). Counters merge by
        type (:func:`_merge_counter`): booleans OR, numbers add, mappings merge-add,
        ``None`` is skipped, anything else raises :class:`TypeError`. Unknown names
        become new bucket keys.
"""
        bucket = self.per_protocol.get(prefix)
        if bucket is None:
            bucket = self.per_protocol[prefix] = copy.deepcopy(V1_BUCKET_DEFAULTS)
        for name, value in counters.items():
            _merge_counter(bucket, name, value)

    def to_dict(self) -> dict:
        """JSON-safe dict in the frozen v1 key order: fields, flat counters just before ``per_protocol``."""
        data = asdict(self)
        per_protocol = data.pop("per_protocol")
        for flat_name, _prefix, _key in V1_FLAT_COUNTERS:
            data[flat_name] = getattr(self, flat_name)
        data["per_protocol"] = per_protocol
        return data

to_dict()

JSON-safe dict in the frozen v1 key order: fields, flat counters just before per_protocol.

Source code in friTap/offline/pipeline_api.py
def to_dict(self) -> dict:
    """JSON-safe dict in the frozen v1 key order: fields, flat counters just before ``per_protocol``."""
    data = asdict(self)
    per_protocol = data.pop("per_protocol")
    for flat_name, _prefix, _key in V1_FLAT_COUNTERS:
        data[flat_name] = getattr(self, flat_name)
    data["per_protocol"] = per_protocol
    return data

Key fields: tap_path, flow_count, decrypted_packet_count, stream_count, dropped_packet_count, dropped_stream_count, findings_count, and encrypted_streams_skipped (streams that could not be decrypted and were tallied rather than emitted).

per_protocol holds the per-protocol counters, keyed by protocol prefix ("mtproto", "telegram", "signal", "rc4", or a plugin's prefix). Each bucket has the same keys (messages, streams, undecryptable, degraded, partial, short, unknown_key_ids, …):

mt = result.per_protocol.get("mtproto", {})
print(mt.get("messages", 0), mt.get("undecryptable", 0))

The flat mtproto_messages, mtproto_streams, mtproto_records_undecryptable and mtproto_streams_degraded keys are deprecated. They are read-only aliases of per_protocol["mtproto"] and stay in to_dict() for compatibility.

Flow ids are <connection id>:<sequence>. A connection id is cr:<protocol>:<client_random> or sid:<protocol>:<token> when either is known. Otherwise it is the endpoint pair: net:<4-tuple> for TLS, and net:<protocol>:<canonical 4-tuple> for every other protocol (e.g. net:mtproto:…, net:quic:…, net:rc4:…, net:signal:…). See offline pcap → tap for details.

Caveats

Keyless 1-RTT-only QUIC captures are undetectable without keys. Encrypted streams that cannot be decrypted are skipped and counted in encrypted_streams_skipped. SSH banners and HTTP/2 control frames become synthetic, metadata-only flows. NoDecryptionKeysError is raised when keylog_path is given but no usable keys (and no embedded DSB) are found.


Reading .tap files

friTap captures persist as a binary .tap file (see the .tap binary format). Two readers are provided.

TapReader

TapReader is the low-level streaming reader.

friTap.flow.tap_reader.TapReader

Indexed reader for .tap capture files.

Usage: reader = TapReader(path); reader.open(); reader.read_flow_summaries() (fast, metadata only); reader.read_flow(flow_id) (full, on demand); reader.close().

Source code in friTap/flow/tap_reader.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
class TapReader:
    """Indexed reader for .tap capture files.

    Usage: ``reader = TapReader(path); reader.open(); reader.read_flow_summaries()``
    (fast, metadata only); ``reader.read_flow(flow_id)`` (full, on demand); ``reader.close()``.
    """

    def __init__(self, path: str) -> None:
        self._path = str(Path(path).resolve())
        self._file = None
        self._header: Optional[TapHeader] = None
        self._meta: Optional[TapMeta] = None
        self._data_start: int = 0  # byte offset where records begin
        # Index: flow_id -> file_offset of the FLOW record envelope
        self._flow_offsets: dict[str, int] = {}
        # Lazily-built findings index: flow_id -> list[Finding]. None = not built.
        self._findings_index: Optional[dict[str, list]] = None
        # Finding presence, determined cheaply at open(): None = undetermined (the footer-index path
        # does not walk records), False = the open-time scan saw no REC_FINDING (read_findings
        # returns [] without a second scan), True = seen; offsets are captured below.
        self._saw_finding_record: Optional[bool] = None
        # Byte offsets of REC_FINDING envelopes from the open-time scan, so read_findings can seek to them.
        self._finding_offsets: list[int] = []
        self._opened: bool = False
        # Serializes every seek+read on the shared file handle: get_flow runs on the UI thread,
        # the analyzer worker and the filter content index, and an interleaved seek would read the
        # wrong bytes (CRC mismatch). Re-entrant: open() and the findings-index build nest record reads.
        self._io_lock = threading.RLock()

    @property
    def path(self) -> str:
        return self._path

    @property
    def header(self) -> Optional[TapHeader]:
        return self._header

    @property
    def meta(self) -> Optional[TapMeta]:
        return self._meta

    @property
    def flow_count(self) -> int:
        return len(self._flow_offsets)

    def open(self) -> TapMeta:
        """Open the .tap file, parse the header, build the flow index and return the file-level TapMeta."""
        with self._io_lock:
            self._file = open(self._path, "rb")
            self._opened = True

            try:
                # Read header
                header_raw = self._file.read(_HEADER_STRUCT.size + 4096)  # read extra for ext
                self._header, self._data_start = decode_header(header_raw)

                # Try to read META record (should be the first record)
                self._file.seek(self._data_start)
                self._meta = self._try_read_meta()

                # Build flow index
                if self._header.flags & FLAG_HAS_INDEX:
                    self._build_index_from_footer()
                else:
                    logger.info("No index in .tap file, performing linear scan")
                    self._build_index_linear_scan()
            except Exception:
                self.close()
                raise

            logger.info(
                "TapReader opened: %s (%d flows)",
                self._path, len(self._flow_offsets),
            )
            return self._meta or TapMeta()

    def read_flow_summaries(self) -> list[FlowSummary]:
        """Read lightweight flow metadata for all flows (no chunks/bodies), sorted by start timestamp."""
        self._ensure_open()
        summaries = []

        for flow_id, offset in self._flow_offsets.items():
            try:
                payload = self._read_record_payload_at(offset)
                if payload is not None:
                    summary = decode_flow_summary(payload, file_offset=offset)
                    summaries.append(summary)
            except Exception:
                logger.debug("Failed to read flow summary at offset %d", offset, exc_info=True)

        # Enrich finding_count: findings live in separate REC_FINDING records, so decode_flow_summary
        # leaves it 0. has_findings() is a cheap no-op for findings-free captures; only captures with
        # findings pay for the (cached) index build.
        if self.has_findings():
            for summary in summaries:
                summary.finding_count = len(self.read_findings(summary.flow_id))

        summaries.sort(key=lambda s: s.started)
        return summaries

    def iter_flows(self) -> Iterator[tuple[int, FlowSummary, "Flow"]]:
        """Yield ``(ordinal, summary, flow)`` for every decodable flow, in summary order.

        ``ordinal`` is the 1-based position in :meth:`read_flow_summaries` (the ``f<N>`` flow-ref
        numbering), counted even for flows that cannot be decoded (skipped).
        """
        # Each FLOW meta is parsed twice (summaries, then read_flow) on purpose: holding all parsed metas would raise
        # peak memory ~10x on large taps (12.7 MB -> 142.5 MB on a 68 MB tap), and avoiding it needs a .tap format change.
        for ordinal, summary in enumerate(self.read_flow_summaries(), start=1):
            flow = self.read_flow(summary.flow_id)
            if flow is not None:
                yield ordinal, summary, flow

    def read_flow(self, flow_id: str) -> Optional["Flow"]:
        """Read a full Flow (with chunks/bodies) by flow_id; None if not in the index."""
        self._ensure_open()
        offset = self._flow_offsets.get(flow_id)
        if offset is None:
            return None

        try:
            payload = self._read_record_payload_at(offset)
            if payload is None:
                return None
            flow = decode_flow(payload)
        except Exception:
            logger.error("Failed to read flow %s at offset %d", flow_id, offset, exc_info=True)
            return None

        # Findings are best-effort: a corrupt findings index must NEVER suppress an already-decoded valid flow.
        try:
            findings = self.read_findings(flow_id)
            if findings:
                flow.findings = list(findings)
        except Exception:
            logger.warning(
                "Failed to read findings for flow %s; returning flow without findings",
                flow_id, exc_info=True,
            )
        return flow

    def read_all_flows(self) -> list["Flow"]:
        """Read all flows fully (large captures: prefer summaries + :meth:`read_flow`)."""
        self._ensure_open()
        flows = []

        for flow_id, offset in self._flow_offsets.items():
            try:
                payload = self._read_record_payload_at(offset)
                if payload is None:
                    continue
                flow = decode_flow(payload)
            except Exception:
                logger.debug("Failed to read flow at offset %d", offset, exc_info=True)
                continue

            # Best-effort findings: a corrupt index must NEVER drop a decoded valid flow.
            try:
                findings = self.read_findings(flow.flow_id)
                if findings:
                    flow.findings = list(findings)
            except Exception:
                logger.warning(
                    "Failed to read findings for flow %s; flow kept without findings",
                    flow.flow_id, exc_info=True,
                )
            flows.append(flow)

        flows.sort(key=lambda f: f.started)
        return flows

    def read_findings(self, flow_id: str) -> list:
        """Return analysis findings persisted for *flow_id*; the index is built lazily (one linear pass) and cached."""
        self._ensure_open()
        return self._ensure_findings_index().get(flow_id, [])

    def iter_keylogs(self) -> Iterator[tuple[str, float]]:
        """Yield ``(key_data, timestamp)`` for every intact REC_KEYLOG record.

        Key material: callers must gate this behind an explicit secrets opt-in. Records are read in
        file order under the I/O lock before the first item is yielded; bad-CRC/undecodable ones are skipped.
        """
        self._ensure_open()
        for payload in self._intact_payloads(_KEYLOG_ONLY):
            try:
                key_data, timestamp = decode_keylog(payload)
            except (ValueError, AttributeError, RecursionError):  # bad/deep JSON, not an object
                logger.debug("Skipping undecodable KEYLOG record")
                continue
            if not isinstance(key_data, str):  # untrusted file: callers expect text
                logger.debug("Skipping KEYLOG record whose key_data is not a string")
                continue
            yield key_data, timestamp if isinstance(timestamp, (int, float)) else 0.0

    def iter_request_contexts(self) -> Iterator[dict]:
        """Yield intact REC_REQUEST_CONTEXT records, re-bounded; skipped for a
        closed file without FLAG_HAS_REQUEST_CONTEXTS."""
        self._ensure_open()
        if not self._may_hold_request_contexts():
            return
        for payload in self._intact_payloads(_REQUEST_CONTEXT_ONLY):
            record = decode_request_context(payload)
            if record is None:
                logger.debug("Skipping undecodable REQUEST_CONTEXT record")
                continue
            yield record

    def _intact_payloads(self, rec_types: frozenset) -> list[bytes]:
        """CRC-valid payloads of *rec_types*, read in one pass under the I/O lock."""
        with self._io_lock:
            return [
                payload
                for _offset, _rec_type, payload, stored_crc in self._iter_records(
                    self._data_start, rec_types=rec_types)
                if verify_payload_crc(payload, stored_crc)
            ]

    def _may_hold_request_contexts(self) -> bool:
        """False only for an indexed file written without request contexts."""
        flags = self._header.flags if self._header is not None else 0
        return not (flags & FLAG_HAS_INDEX) or bool(flags & FLAG_HAS_REQUEST_CONTEXTS)

    def has_findings(self) -> bool:
        """True if the file contains any persisted findings."""
        self._ensure_open()
        return bool(self._ensure_findings_index())

    def _ensure_findings_index(self) -> dict:
        """Build the findings index once under the I/O lock; returns the index itself so a concurrent ``close()`` (resetting ``_findings_index`` to None) cannot turn the caller's lookup into ``None.get``."""
        index = self._findings_index
        if index is not None:
            return index
        with self._io_lock:
            if self._findings_index is None:
                self._build_findings_index()
            return self._findings_index or {}

    def close(self) -> None:
        """Close the file. Safe to call multiple times."""
        with self._io_lock:
            if self._file is not None:
                try:
                    self._file.close()
                except Exception:
                    pass
                self._file = None
            self._opened = False
            self._flow_offsets.clear()
            self._findings_index = None
            self._saw_finding_record = None
            self._finding_offsets = []

    def __enter__(self) -> "TapReader":
        self.open()
        return self

    def __exit__(self, *args) -> None:
        self.close()


    def _build_index_from_footer(self) -> None:
        """Read the FLOW_INDEX from the footer pointer."""
        assert self._file is not None

        # Findings presence is in the header flags: a closed/indexed file (FLAG_HAS_INDEX, this path)
        # without FLAG_HAS_FINDINGS provably has no REC_FINDING records.
        if self._header is not None and self._header.flags & FLAG_HAS_FINDINGS:
            # Findings exist; offsets unknown on this path, so _build_findings_index scans once, lazily.
            self._saw_finding_record = True
        else:
            self._saw_finding_record = False

        # Read footer (last 16 bytes)
        self._file.seek(0, 2)  # seek to end
        file_size = self._file.tell()

        if file_size < _HEADER_STRUCT.size + _FOOTER_STRUCT.size:
            logger.warning("File too small for footer, falling back to linear scan")
            self._build_index_linear_scan()
            return

        self._file.seek(file_size - _FOOTER_STRUCT.size)
        footer_raw = self._file.read(_FOOTER_STRUCT.size)

        try:
            footer_magic, index_offset = _FOOTER_STRUCT.unpack(footer_raw)
        except Exception:
            logger.warning("Footer unpack failed, falling back to linear scan")
            self._build_index_linear_scan()
            return

        if footer_magic != FOOTER_MAGIC:
            logger.warning("Footer magic mismatch, falling back to linear scan")
            self._build_index_linear_scan()
            return

        # Read FLOW_INDEX record at index_offset
        payload = self._read_record_payload_at(index_offset)
        if payload is None:
            logger.warning("Failed to read FLOW_INDEX, falling back to linear scan")
            self._build_index_linear_scan()
            return

        entries = decode_flow_index(payload)
        for entry in entries:
            self._flow_offsets[entry["flow_id"]] = entry["offset"]

    def _build_index_linear_scan(self) -> None:
        """Scan all records sequentially to build the flow index (FLOW_INDEX missing: partial capture / crash); keeps the last FLOW record per flow_id."""
        assert self._file is not None

        # This pass visits every record, so capture finding presence/offsets here: afterwards the
        # flag is authoritative (False if none seen) and _build_findings_index skips a second scan.
        self._saw_finding_record = False
        self._finding_offsets = []

        for offset, rec_type, payload, stored_crc in self._iter_records(self._data_start):
            # Record finding presence/offset only; the payload is decoded lazily on first read_findings.
            if rec_type == REC_FINDING:
                self._saw_finding_record = True
                self._finding_offsets.append(offset)
                continue

            if rec_type == REC_FLOW:
                if verify_payload_crc(payload, stored_crc):
                    try:
                        summary = decode_flow_summary(payload)
                        self._flow_offsets[summary.flow_id] = offset
                    except Exception:
                        logger.debug("Skipping corrupt FLOW at offset %d", offset)
                else:
                    logger.debug("CRC mismatch for FLOW at offset %d, skipping", offset)
            elif rec_type == REC_META and self._meta is None:
                if verify_payload_crc(payload, stored_crc):
                    try:
                        self._meta = decode_meta(payload)
                    except Exception:
                        pass

    def _build_findings_index(self) -> None:
        """Build the lazy flow_id -> [Finding] index from REC_FINDING records, cached in ``_findings_index``.

        Performance gate on the open-time scan results:

          * ``_saw_finding_record is False``: no findings seen, short-circuit to an empty index
            without re-reading the (potentially large) file (the common case).
          * ``_finding_offsets`` populated: seek directly to each known finding record.
          * ``_saw_finding_record is None``: undetermined (footer-index path), fall back to a full linear pass.
        """
        assert self._file is not None

        # Short-circuit: open-time scan proved there are no findings.
        if self._saw_finding_record is False:
            self._findings_index = {}
            return

        index: dict[str, list] = {}
        # Lazy import to avoid an import cycle (analysis imports flow.models).
        try:
            from friTap.analysis import Finding
        except Exception:
            Finding = None  # type: ignore

        def add_record(payload: bytes, stored_crc: int, offset: int) -> None:
            if not verify_payload_crc(payload, stored_crc):
                logger.debug("CRC mismatch for REC_FINDING at offset %d, skipping", offset)
                return
            try:
                flow_id, finding_dicts = decode_finding_record(payload)
            except Exception:
                logger.debug("Skipping corrupt REC_FINDING at offset %d", offset)
                return
            bucket = index.setdefault(flow_id, [])
            for fd in finding_dicts:
                bucket.append(Finding.from_dict(fd) if Finding is not None else fd)

        # Fast path: seek directly to the offsets captured at open() time.
        if self._saw_finding_record is True and self._finding_offsets:
            for offset in self._finding_offsets:
                self._file.seek(offset)
                envelope_raw = self._file.read(_RECORD_ENVELOPE.size)
                if len(envelope_raw) < _RECORD_ENVELOPE.size:
                    continue
                try:
                    rec_type, payload_len, stored_crc, _ = decode_record_envelope(envelope_raw)
                except ValueError:
                    continue
                if rec_type != REC_FINDING or payload_len == 0:
                    continue
                try:
                    payload = self._read_payload(payload_len)
                except BoundsError:
                    logger.debug("Skipping REC_FINDING at offset %d: bad length", offset)
                    continue
                add_record(payload, stored_crc, offset)
            self._findings_index = index
            return

        # Fallback (presence undetermined, e.g. footer-index path): full scan.
        for offset, rec_type, payload, stored_crc in self._iter_records(self._data_start):
            if rec_type != REC_FINDING:
                continue
            add_record(payload, stored_crc, offset)

        self._findings_index = index

    def _iter_records(self, start_offset: int, rec_types: Optional[frozenset] = None):
        """Yield ``(offset, rec_type, payload, stored_crc)`` for every record.

        With *rec_types*, other records' payloads are seeked past unread (same bounds/truncation
        checks, so the scan ends at the same point). Shared by ``_build_index_linear_scan`` and the
        findings fallback; recovers via sync marker on a corrupt envelope and skips zero-length
        records. The CRC is yielded unverified so each caller applies its own policy.
        """
        assert self._file is not None
        self._file.seek(start_offset)

        while True:
            offset = self._file.tell()
            envelope_raw = self._file.read(_RECORD_ENVELOPE.size)

            if len(envelope_raw) < _RECORD_ENVELOPE.size:
                break  # EOF or truncated

            try:
                rec_type, payload_len, stored_crc, _ = decode_record_envelope(envelope_raw)
            except ValueError:
                # Try to find next sync marker for recovery
                recovered = self._recover_from(offset + 1)
                if recovered < 0:
                    break
                continue

            if payload_len == 0:
                continue

            wanted = rec_types is None or rec_type in rec_types
            try:
                if not wanted:
                    self._skip_payload(payload_len)
                    continue
                payload = self._read_payload(payload_len)
            except BoundsError as exc:
                # Truncated payload at EOF, or a length no record can have.
                logger.debug("Ending record scan at offset %d: %s", offset, exc)
                break

            yield offset, rec_type, payload, stored_crc

    def _recover_from(self, start_offset: int) -> int:
        """Scan forward from start_offset for the next valid sync marker; its offset, or -1."""
        assert self._file is not None
        self._file.seek(start_offset)

        # Read in 4KB chunks to find sync marker
        while True:
            chunk_start = self._file.tell()
            chunk = self._file.read(4096)
            if not chunk:
                return -1

            idx = find_sync_marker(chunk)
            if idx >= 0:
                recovered_offset = chunk_start + idx
                self._file.seek(recovered_offset)
                return recovered_offset


    def _read_record_payload_at(self, offset: int) -> Optional[bytes]:
        """Read and verify one record's payload at *offset*; None if invalid. Thread-safe (seek+read under ``_io_lock``)."""
        with self._io_lock:
            raw = self._read_record_raw_at(offset)
        if raw is None:
            return None
        payload, stored_crc = raw

        if not verify_payload_crc(payload, stored_crc):
            logger.warning("CRC mismatch at offset %d", offset)
            return None

        return payload

    def _read_record_raw_at(self, offset: int) -> Optional[tuple[bytes, int]]:
        """Seek to *offset* and read ``(payload, stored_crc)``; caller holds the lock."""
        if self._file is None:
            return None
        self._file.seek(offset)

        envelope_raw = self._file.read(_RECORD_ENVELOPE.size)
        if len(envelope_raw) < _RECORD_ENVELOPE.size:
            return None

        try:
            _, payload_len, stored_crc, _ = decode_record_envelope(envelope_raw)
            payload = self._read_payload(payload_len)
        except ValueError:  # bad envelope, or BoundsError on the payload length
            return None
        return payload, stored_crc

    def _read_payload(self, payload_len: int) -> bytes:
        """Read a *payload_len*-byte payload at the current position.

        Length is checked against :data:`MAX_RECORD_PAYLOAD` and the bytes left in the file *before*
        allocating, so a crafted u32 cannot trigger a multi-GiB ``read()``. Raises
        :class:`~friTap.binary_bounds.BoundsError` (a ``ValueError``) on an implausible or truncated payload.
        """
        assert self._file is not None
        return read_bounded(self._file, payload_len, "tap record payload", MAX_RECORD_PAYLOAD)

    def _skip_payload(self, payload_len: int) -> None:
        """Advance past a payload without reading it, with :meth:`_read_payload`'s bounds checks (bounded read fallback when the file size is unknown)."""
        assert self._file is not None
        remaining = remaining_bytes(self._file)
        if remaining is None:
            self._read_payload(payload_len)
            return
        check_length(payload_len, "tap record payload", MAX_RECORD_PAYLOAD, remaining)
        self._file.seek(payload_len, 1)

    def _try_read_meta(self) -> Optional[TapMeta]:
        """Try to read a META record at the current position."""
        assert self._file is not None
        pos = self._file.tell()

        envelope_raw = self._file.read(_RECORD_ENVELOPE.size)
        if len(envelope_raw) < _RECORD_ENVELOPE.size:
            self._file.seek(pos)
            return None

        try:
            rec_type, payload_len, stored_crc, _ = decode_record_envelope(envelope_raw)
        except ValueError:
            self._file.seek(pos)
            return None

        if rec_type != REC_META:
            self._file.seek(pos)
            return None

        try:
            payload = self._read_payload(payload_len)
        except BoundsError:
            self._file.seek(pos)
            return None

        if not verify_payload_crc(payload, stored_crc):
            self._file.seek(pos)
            return None

        return decode_meta(payload)

    def _ensure_open(self) -> None:
        if not self._opened or self._file is None:
            raise RuntimeError("TapReader is not open. Call open() first.")

open()

Open the .tap file, parse the header, build the flow index and return the file-level TapMeta.

Source code in friTap/flow/tap_reader.py
def open(self) -> TapMeta:
    """Open the .tap file, parse the header, build the flow index and return the file-level TapMeta."""
    with self._io_lock:
        self._file = open(self._path, "rb")
        self._opened = True

        try:
            # Read header
            header_raw = self._file.read(_HEADER_STRUCT.size + 4096)  # read extra for ext
            self._header, self._data_start = decode_header(header_raw)

            # Try to read META record (should be the first record)
            self._file.seek(self._data_start)
            self._meta = self._try_read_meta()

            # Build flow index
            if self._header.flags & FLAG_HAS_INDEX:
                self._build_index_from_footer()
            else:
                logger.info("No index in .tap file, performing linear scan")
                self._build_index_linear_scan()
        except Exception:
            self.close()
            raise

        logger.info(
            "TapReader opened: %s (%d flows)",
            self._path, len(self._flow_offsets),
        )
        return self._meta or TapMeta()

read_flow_summaries()

Read lightweight flow metadata for all flows (no chunks/bodies), sorted by start timestamp.

Source code in friTap/flow/tap_reader.py
def read_flow_summaries(self) -> list[FlowSummary]:
    """Read lightweight flow metadata for all flows (no chunks/bodies), sorted by start timestamp."""
    self._ensure_open()
    summaries = []

    for flow_id, offset in self._flow_offsets.items():
        try:
            payload = self._read_record_payload_at(offset)
            if payload is not None:
                summary = decode_flow_summary(payload, file_offset=offset)
                summaries.append(summary)
        except Exception:
            logger.debug("Failed to read flow summary at offset %d", offset, exc_info=True)

    # Enrich finding_count: findings live in separate REC_FINDING records, so decode_flow_summary
    # leaves it 0. has_findings() is a cheap no-op for findings-free captures; only captures with
    # findings pay for the (cached) index build.
    if self.has_findings():
        for summary in summaries:
            summary.finding_count = len(self.read_findings(summary.flow_id))

    summaries.sort(key=lambda s: s.started)
    return summaries

read_flow(flow_id)

Read a full Flow (with chunks/bodies) by flow_id; None if not in the index.

Source code in friTap/flow/tap_reader.py
def read_flow(self, flow_id: str) -> Optional["Flow"]:
    """Read a full Flow (with chunks/bodies) by flow_id; None if not in the index."""
    self._ensure_open()
    offset = self._flow_offsets.get(flow_id)
    if offset is None:
        return None

    try:
        payload = self._read_record_payload_at(offset)
        if payload is None:
            return None
        flow = decode_flow(payload)
    except Exception:
        logger.error("Failed to read flow %s at offset %d", flow_id, offset, exc_info=True)
        return None

    # Findings are best-effort: a corrupt findings index must NEVER suppress an already-decoded valid flow.
    try:
        findings = self.read_findings(flow_id)
        if findings:
            flow.findings = list(findings)
    except Exception:
        logger.warning(
            "Failed to read findings for flow %s; returning flow without findings",
            flow_id, exc_info=True,
        )
    return flow

read_all_flows()

Read all flows fully (large captures: prefer summaries + :meth:read_flow).

Source code in friTap/flow/tap_reader.py
def read_all_flows(self) -> list["Flow"]:
    """Read all flows fully (large captures: prefer summaries + :meth:`read_flow`)."""
    self._ensure_open()
    flows = []

    for flow_id, offset in self._flow_offsets.items():
        try:
            payload = self._read_record_payload_at(offset)
            if payload is None:
                continue
            flow = decode_flow(payload)
        except Exception:
            logger.debug("Failed to read flow at offset %d", offset, exc_info=True)
            continue

        # Best-effort findings: a corrupt index must NEVER drop a decoded valid flow.
        try:
            findings = self.read_findings(flow.flow_id)
            if findings:
                flow.findings = list(findings)
        except Exception:
            logger.warning(
                "Failed to read findings for flow %s; flow kept without findings",
                flow.flow_id, exc_info=True,
            )
        flows.append(flow)

    flows.sort(key=lambda f: f.started)
    return flows

close()

Close the file. Safe to call multiple times.

Source code in friTap/flow/tap_reader.py
def close(self) -> None:
    """Close the file. Safe to call multiple times."""
    with self._io_lock:
        if self._file is not None:
            try:
                self._file.close()
            except Exception:
                pass
            self._file = None
        self._opened = False
        self._flow_offsets.clear()
        self._findings_index = None
        self._saw_finding_record = None
        self._finding_offsets = []
from friTap import TapReader

reader = TapReader("capture_20260507_153933.tap")   # offline / CI-runnable
meta = reader.open()
for summary in reader.read_flow_summaries():
    print(summary.flow_id, summary.host, summary.status_code)
reader.close()

ReplayController

ReplayController is the higher-level facade used by the TUI replay view. It adds an LRU cache (128 flows), a context-manager interface, and convenience properties. It implements IFlowSource (get_flows() / get_flow(id)).

friTap.flow.replay.ReplayController

Loads and serves flows from a .tap file for replay mode (satisfies :class:IFlowSource).

Source code in friTap/flow/replay.py
class ReplayController:
    """Loads and serves flows from a .tap file for replay mode (satisfies :class:`IFlowSource`)."""

    def __init__(self, path: str) -> None:
        self._path = path
        self._reader: Optional[TapReader] = None
        self._meta: Optional[TapMeta] = None
        self._summaries: list[FlowSummary] = []
        self._flow_cache: _LRUCache = _LRUCache(maxsize=128)
        self._reparse_results: dict[str, tuple[Optional["ParseResult"], Optional["ParseResult"]]] = {}
        # Flow ids whose stale trailing-data segments are dropped on every (re)load
        # (e.g. decrypted TL flows once mis-parsed as WebSocket).
        self._clear_trailing_ids: set[str] = set()

    @property
    def replay_file(self) -> str:
        return self._path

    @property
    def flow_count(self) -> int:
        return len(self._summaries)

    @property
    def meta(self) -> Optional[TapMeta]:
        return self._meta

    @property
    def header(self):
        return self._reader.header if self._reader else None

    def load(self) -> TapMeta:
        """Open the .tap file, load flow summaries and return the file-level TapMeta."""
        self._reader = TapReader(self._path)
        self._meta = self._reader.open()
        self._summaries = self._reader.read_flow_summaries()
        logger.info(
            "Replay loaded: %s (%d flows)", self._path, len(self._summaries)
        )
        return self._meta

    def get_summaries(self) -> list[FlowSummary]:
        """Return all flow summaries (lightweight, no chunk/body data)."""
        return list(self._summaries)

    def get_flows(self) -> list["Flow"]:
        """Return all flows as full Flow objects (loads everything; prefer summaries + :meth:`get_flow`)."""
        if self._reader is None:
            return []
        return self._reader.read_all_flows()

    def get_flow(self, flow_id: str) -> Optional["Flow"]:
        """Load a full Flow by ID (on-demand from disk, LRU-cached)."""
        flow = self._flow_cache.get(flow_id)
        if flow is None:
            if self._reader is None:
                return None
            flow = self._reader.read_flow(flow_id)
            if flow is None:
                return None
            self._flow_cache[flow_id] = flow
        # Re-apply reparse results that survive cache eviction.
        if flow_id in self._reparse_results:
            req, resp = self._reparse_results[flow_id]
            if req is not None:
                flow.request = req
            if resp is not None:
                flow.response = resp
        if flow_id in self._clear_trailing_ids:
            from friTap.flow.reparse import clear_trailing_data
            clear_trailing_data(flow)
        return flow

    def store_reparse(
        self,
        flow_id: str,
        request: Optional["ParseResult"],
        response: Optional["ParseResult"],
        clear_trailing: bool = False,
    ) -> None:
        """Store reparse results so they survive cache eviction.

        With *clear_trailing*, every later :meth:`get_flow` also drops trailing-data state.
        """
        self._reparse_results[flow_id] = (request, response)
        if clear_trailing:
            self._clear_trailing_ids.add(flow_id)

    def read_all_findings(self) -> list:
        """Return persisted findings (dicts) across all flows (REC_FINDING records); empty if none."""
        if self._reader is None:
            return []
        findings: list = []
        for summary in self._summaries:
            findings.extend(self._reader.read_findings(summary.flow_id))
        return findings

    def read_request_contexts(self) -> list:
        """Return the persisted Java request contexts (REC_REQUEST_CONTEXT dicts); empty when none or not loaded."""
        if self._reader is None:
            return []
        return list(self._reader.iter_request_contexts())

    def has_findings(self) -> bool:
        """True if the .tap file contains any persisted findings."""
        if self._reader is None:
            return False
        return self._reader.has_findings()

    def close(self) -> None:
        """Close the reader and release resources."""
        if self._reader is not None:
            self._reader.close()
            self._reader = None
        self._flow_cache.clear()
        self._reparse_results.clear()
        self._clear_trailing_ids.clear()
        self._summaries.clear()

    def __enter__(self) -> "ReplayController":
        self.load()
        return self

    def __exit__(self, *args) -> None:
        self.close()

load()

Open the .tap file, load flow summaries and return the file-level TapMeta.

Source code in friTap/flow/replay.py
def load(self) -> TapMeta:
    """Open the .tap file, load flow summaries and return the file-level TapMeta."""
    self._reader = TapReader(self._path)
    self._meta = self._reader.open()
    self._summaries = self._reader.read_flow_summaries()
    logger.info(
        "Replay loaded: %s (%d flows)", self._path, len(self._summaries)
    )
    return self._meta

get_summaries()

Return all flow summaries (lightweight, no chunk/body data).

Source code in friTap/flow/replay.py
def get_summaries(self) -> list[FlowSummary]:
    """Return all flow summaries (lightweight, no chunk/body data)."""
    return list(self._summaries)

get_flows()

Return all flows as full Flow objects (loads everything; prefer summaries + :meth:get_flow).

Source code in friTap/flow/replay.py
def get_flows(self) -> list["Flow"]:
    """Return all flows as full Flow objects (loads everything; prefer summaries + :meth:`get_flow`)."""
    if self._reader is None:
        return []
    return self._reader.read_all_flows()

get_flow(flow_id)

Load a full Flow by ID (on-demand from disk, LRU-cached).

Source code in friTap/flow/replay.py
def get_flow(self, flow_id: str) -> Optional["Flow"]:
    """Load a full Flow by ID (on-demand from disk, LRU-cached)."""
    flow = self._flow_cache.get(flow_id)
    if flow is None:
        if self._reader is None:
            return None
        flow = self._reader.read_flow(flow_id)
        if flow is None:
            return None
        self._flow_cache[flow_id] = flow
    # Re-apply reparse results that survive cache eviction.
    if flow_id in self._reparse_results:
        req, resp = self._reparse_results[flow_id]
        if req is not None:
            flow.request = req
        if resp is not None:
            flow.response = resp
    if flow_id in self._clear_trailing_ids:
        from friTap.flow.reparse import clear_trailing_data
        clear_trailing_data(flow)
    return flow

close()

Close the reader and release resources.

Source code in friTap/flow/replay.py
def close(self) -> None:
    """Close the reader and release resources."""
    if self._reader is not None:
        self._reader.close()
        self._reader = None
    self._flow_cache.clear()
    self._reparse_results.clear()
    self._clear_trailing_ids.clear()
    self._summaries.clear()
from friTap import ReplayController

with ReplayController("capture_20260507_153933.tap") as rc:   # offline / CI-runnable
    meta = rc.load()
    print("flows:", rc.flow_count)
    for summary in rc.get_summaries():
        print(summary.flow_id, summary.protocol, summary.host)
    flow = rc.get_flow(rc.get_summaries()[0].flow_id)
    print(flow.to_dict())

Traffic analysis

friTap can run its analyzers (credentials, ioc, privacy, protobuf, plus custom ones) over a captured .tap file — passive analysis, no network activity. Use analyze_tap_report() for programmatic access; it performs no I/O beyond reading the .tap and never calls sys.exit.

friTap.commands.analyze.analyze_tap_report(tap_file, *, scanners=None, analyzer_path=None, min_severity='info', report_format='table', include_private_ips=False, protobuf_schema=None, min_confidence=0.0, source=None, category=None, show_pii=False, masvs=False, applovin_sdk_keys=None, applovin_salts=None, app_bundles=None)

Run analyzers over tap_file and return findings + a rendered report.

Pure orchestration shared by the CLI and external tools: resolve analyzers (scanners is a comma-separated name list, or None/"all" for the built-ins) → run them → filter (severity/confidence/source/category) → render in report_format (one of :func:list_report_formats). Performs no stdout, no sidecar write and never calls sys.exit — callers decide how to surface the result.

The filter arguments are all additive and default to no-ops, so existing callers are unaffected:

  • min_confidence — keep findings with confidence at or above this value.
  • source — comma-separated analyzer source names to keep (which findings show; distinct from scanners, which selects analyzers that run).
  • category — comma-separated finding categories to keep (secret/pii/network/protocol).
  • show_pii — reveal PII/secret values instead of redacting them (forwarded to analyzers as reveal_pii; default redacts).
  • masvs — add the OWASP MASVS v2.1 controls each finding maps to (a column/field in the report); default off leaves the report unchanged.
  • applovin_sdk_keys / applovin_salts (hex) / app_bundles (APK/IPA paths) — key material for the applovin_mediate analyzer, merged with the FRITAP_APPLOVIN_* environment variables.

Raises ValueError for an unknown report_format or an unresolvable analyzer spec, ImportError for a bad analyzer_path; any .tap read/analyze failure propagates.

Source code in friTap/commands/analyze.py
def analyze_tap_report(
    tap_file: str,
    *,
    scanners: str | None = None,
    analyzer_path: str | None = None,
    min_severity: str = "info",
    report_format: str = "table",
    include_private_ips: bool = False,
    protobuf_schema: str | None = None,
    min_confidence: float = 0.0,
    source: str | None = None,
    category: str | None = None,
    show_pii: bool = False,
    masvs: bool = False,
    applovin_sdk_keys: "list[str] | None" = None,
    applovin_salts: "list[str] | None" = None,
    app_bundles: "list[str] | None" = None,
) -> AnalyzeReport:
    """Run analyzers over *tap_file* and return findings + a rendered report.

    Pure orchestration shared by the CLI and external tools: resolve analyzers
    (``scanners`` is a comma-separated name list, or ``None``/``"all"`` for the
    built-ins) → run them → filter (severity/confidence/source/category) →
    render in *report_format* (one of :func:`list_report_formats`). Performs no
    stdout, no sidecar write and never calls ``sys.exit`` — callers decide how
    to surface the result.

    The filter arguments are all additive and default to no-ops, so existing
    callers are unaffected:

    * ``min_confidence`` — keep findings with confidence at or above this value.
    * ``source`` — comma-separated analyzer source names to keep (which findings
      *show*; distinct from ``scanners``, which selects analyzers that *run*).
    * ``category`` — comma-separated finding categories to keep
      (``secret``/``pii``/``network``/``protocol``).
    * ``show_pii`` — reveal PII/secret values instead of redacting them
      (forwarded to analyzers as ``reveal_pii``; default redacts).
    * ``masvs`` — add the OWASP MASVS v2.1 controls each finding maps to
      (a column/field in the report); default off leaves the report unchanged.
    * ``applovin_sdk_keys`` / ``applovin_salts`` (hex) / ``app_bundles``
      (APK/IPA paths) — key material for the ``applovin_mediate`` analyzer,
      merged with the ``FRITAP_APPLOVIN_*`` environment variables.

    Raises ``ValueError`` for an unknown *report_format* or an unresolvable
    analyzer spec, ``ImportError`` for a bad ``analyzer_path``; any .tap
    read/analyze failure propagates.
    """
    from friTap.analysis.filtering import FindingFilter, apply, split_csv

    if report_format not in _REPORTER_REGISTRY:
        raise ValueError(
            f"unknown report format {report_format!r}; "
            f"choose from {', '.join(sorted(_REPORTER_REGISTRY))}"
        )

    analyzers = resolve_analyzers(
        scanners,
        analyzer_path=analyzer_path,
        include_private_ips=include_private_ips,
        protobuf_schema=protobuf_schema,
        reveal_pii=show_pii,
        applovin_sdk_keys=applovin_sdk_keys,
        applovin_salts=applovin_salts,
        app_bundles=app_bundles,
    )
    findings = analyze_tap_multi(analyzers, tap_file)
    normalized_severity = min_severity if min_severity in _SEVERITY_ORDER else "info"
    finding_filter = FindingFilter(
        min_severity=normalized_severity,
        sources=split_csv(source),
        categories=split_csv(category),
        min_confidence=min_confidence if min_confidence > 0.0 else None,
    )
    filtered = apply(findings, finding_filter)
    analyzer_names = [a.name for a in analyzers]
    meta = {"tap_file": tap_file, "analyzers": analyzer_names}
    # AnalyzeReport.findings keep full fidelity; only the rendered report is
    # redacted (unless show_pii). The reporter is the redaction enforcement point.
    rendered = _REPORTER_REGISTRY[report_format](redact_pii=not show_pii, masvs=masvs).report(filtered, meta)
    return AnalyzeReport(
        findings=filtered,
        rendered=rendered,
        report_format=report_format,
        analyzer_names=analyzer_names,
        meta=meta,
    )

friTap.commands.analyze.AnalyzeReport dataclass

Result of a programmatic .tap analysis.

Carries both the structured (already severity-filtered) findings and the report rendered in the requested format, so web / TUI / CLI callers can use whichever they need without re-running the analyzers. Returned by :func:analyze_tap_report.

Source code in friTap/commands/analyze.py
@dataclass(frozen=True)
class AnalyzeReport:
    """Result of a programmatic ``.tap`` analysis.

    Carries both the structured (already severity-filtered) findings and the
    report rendered in the requested format, so web / TUI / CLI callers can use
    whichever they need without re-running the analyzers. Returned by
    :func:`analyze_tap_report`.
    """

    findings: list["Finding"]
    rendered: str
    report_format: str
    analyzer_names: list[str] = field(default_factory=list)
    meta: dict[str, Any] = field(default_factory=dict)
    gate_severity: str = _GATE_SEVERITY

    @property
    def gate_tripped(self) -> bool:
        """True iff any finding is at or above :attr:`gate_severity`.

        Mirrors the condition under which :func:`run_analyze_cli` returns the
        non-zero CI-gate exit code.
        """
        return bool(_filter_min_severity(self.findings, self.gate_severity))

    @property
    def exit_code(self) -> int:
        """CLI-parity exit code: 2 when :attr:`gate_tripped`, else 0."""
        return 2 if self.gate_tripped else 0

gate_tripped property

True iff any finding is at or above :attr:gate_severity.

Mirrors the condition under which :func:run_analyze_cli returns the non-zero CI-gate exit code.

exit_code property

CLI-parity exit code: 2 when :attr:gate_tripped, else 0.

from friTap import analyze_tap_report

report = analyze_tap_report(
    "capture_20260507_153933.tap",   # offline / CI-runnable
    scanners="credentials,ioc",      # None or "all" → built-ins (which analyzers RUN)
    min_severity="info",
    report_format="table",
    # report-side filters (additive, keyword-only; all default to no-op):
    min_confidence=0.0,              # drop findings below this confidence (0.0–1.0)
    source=None,                     # comma-separated source names to SHOW (e.g. "credentials,privacy")
    category=None,                   # "secret,pii,network,protocol" to SHOW
    show_pii=False,                  # reveal PII/secret values instead of redacting
)

for finding in report.findings:
    print(finding.severity.name, finding.category, finding.title, finding.flow_id)

print(report.rendered)               # the rendered table/json/csv/md
raise SystemExit(report.exit_code)   # 2 if any finding ≥ medium, else 0 (CI gate)

min_confidence, source, category, and show_pii are additive, keyword-only parameters — older calls that omit them keep working unchanged. scanners chooses which analyzers run; source/category filter which of the resulting findings are shown.

Findings & severity

Each Finding has: severity (a Severity enum), title, description, source, flow_id, confidence (default 1.0), timestamp, and the dicts evidence and metadata. Severity is CRITICAL/HIGH/MEDIUM/LOW/INFO (rank 0 is most severe).

Every finding now carries a category in metadata["category"], surfaced as the Finding.category property: secret (credentials), pii (privacy), network (IOC), or protocol (protobuf). PII findings additionally carry metadata["compliance"] (e.g. GDPR, CCPA, PCI-DSS, HIPAA).

Filtering findings

FindingFilter (exported from the top-level friTap package) is the reusable filter behind the CLI's --source/--category/--min-confidence flags. Apply it to any iterable of Finding objects:

from friTap import FindingFilter

flt = FindingFilter(
    min_severity="info",                 # floor: this severity and above
    min_confidence=0.8,
    sources={"credentials", "privacy"},
    categories={"secret", "pii"},
)
kept = [f for f in report.findings if flt.matches(f)]

Use severities for an exact-bucket match instead of the min_severity floor — FindingFilter(severities={"high"}) keeps only high findings (this is what the TUI's per-severity dashboard chips use, so a chip's count matches its result):

high_only = [f for f in report.findings
             if FindingFilter(severities={"high"}).matches(f)]

PrivacyAnalyzer import path

The built-in analyzer classes are available under friTap.analysis.* — the privacy analyzer is from friTap.analysis.privacy import PrivacyAnalyzer (not re-exported from the top-level friTap package). Select it by name via scanners="privacy" rather than importing it directly.

CI gate

AnalyzeReport.gate_tripped is True when any finding is at or above the gate severity (medium), and exit_code returns 2 in that case (else 0) — mirroring the fritap analyze CLI exit code. Use it to fail a pipeline when a capture contains sensitive findings.

Discovery helpers

  • list_analyzers() → analyzer names (built-in plus any discovered).
  • list_analyzers_detailed() → richer AnalyzerInfo records (name, source/origin, description) for every analyzer, including discovered ones. Use this to render an analyzer catalog in your own tooling.
  • list_report_formats() → available report formats (json, csv, md, table).
from friTap import list_analyzers, list_analyzers_detailed

print(list_analyzers())              # ['credentials', 'ioc', 'privacy', 'protobuf', ...]
for info in list_analyzers_detailed():
    print(info.name, "-", info.description)

Custom analyzers can be loaded via the CLI --analyzer-path module:Class (the module:Class form skips the is_fritap_analyzer marker requirement), or made ambiently discoverable through the drop-in directory, the fritap.analyzers entry-points group, or a plugin's register_analyzers() hook — see Traffic Analysis → Ship it everywhere.

Discovery executes code

Discovered analyzers (drop-in directory, fritap.analyzers entry points, plugin bridge) execute as soon as list_analyzers()/list_analyzers_detailed() or any resolution runs. Set FRITAP_DISABLE_ANALYZER_DISCOVERY=1 to disable ambient discovery; explicit --analyzer-path / .analyzer_path() references still work.

Live scan from the builder

The FriTap builder exposes live passive analysis directly. scan() enables it, analyzer_path() is repeatable, and scan_report(), scan_min_severity(), and scan_show_pii() set the corresponding --scan-* options:

from friTap import FriTap

session = (
    FriTap("com.example.app")
    .mobile()
    .scan("all")                                  # or "credentials,ioc"
    .analyzer_path("my_analyzer:InternalHostAnalyzer")
    .scan_report("table")
    .scan_min_severity("medium")
    .start()
)

Protobuf utilities

Schema-less protobuf wire decoding with zero external dependencies — handy for inspecting captured gRPC/protobuf bodies.

friTap.parsers.protobuf.decode_raw(data, max_depth=16)

Decode raw protobuf wire format without a schema.

Recursively attempts sub-message parsing for LENGTH_DELIMITED fields.

Parameters:

Name Type Description Default
data bytes

Raw protobuf bytes.

required
max_depth int

Maximum nesting depth (default 16).

16

Returns:

Name Type Description
A ProtobufMessage

class:ProtobufMessage tree.

Fields nested deeper than max_depth are kept as raw bytes, and decoding stops gracefully after :data:MAX_FIELDS_PER_DECODE fields (the result then has truncated=True).

Raises:

Type Description
ValueError

If the data is malformed (or max_depth is not positive).

Source code in friTap/parsers/protobuf/wire.py
def decode_raw(data: bytes, max_depth: int = 16) -> ProtobufMessage:
    """Decode raw protobuf wire format without a schema.

    Recursively attempts sub-message parsing for ``LENGTH_DELIMITED`` fields.

    Args:
        data: Raw protobuf bytes.
        max_depth: Maximum nesting depth (default 16).

    Returns:
        A :class:`ProtobufMessage` tree.

    Fields nested deeper than *max_depth* are kept as raw bytes, and decoding
    stops gracefully after :data:`MAX_FIELDS_PER_DECODE` fields (the result
    then has ``truncated=True``).

    Raises:
        ValueError: If the data is malformed (or *max_depth* is not positive).
    """
    if not data:
        return ProtobufMessage()
    return _decode_raw_impl(data, max_depth)

friTap.parsers.protobuf.format_message(msg, indent=0)

Format a :class:ProtobufMessage as human-readable indented text.

Output resembles protoc --decode_raw.

Source code in friTap/parsers/protobuf/wire.py
def format_message(msg: ProtobufMessage, indent: int = 0) -> str:
    """Format a :class:`ProtobufMessage` as human-readable indented text.

    Output resembles ``protoc --decode_raw``.
    """
    return "\n".join(iter_message_lines(msg, indent))
from friTap import decode_raw, format_message

msg = decode_raw(b"\x08\x96\x01")   # offline / CI-runnable
print(format_message(msg))
# 1: 150

decode_raw(data, max_depth=16) returns a ProtobufMessage (a tree of ProtobufField objects); format_message(msg, indent=0) renders it as human-readable text. ProtobufProcessor provides higher-level, schema-aware processing.

Decoding a captured body (gRPC, schema, backends)

decode_protobuf_view is the decoder the TUI uses for every protobuf view (flow detail, body-processing preview, explorer). It validates gRPC framing instead of dropping five bytes blindly, decompresses compressed gRPC messages (grpc-encoding gzip/deflate/zstd), applies a schema when given one, and never raises on malformed input: problems become notes / error strings, and a truncated tail still yields the fields decoded before it.

from friTap.parsers.protobuf import decode_protobuf_view

body = b"\x00\x00\x00\x00\x03\x08\x96\x01"   # one gRPC frame
view = decode_protobuf_view(body, content_type="application/grpc")
print(view.framing)   # grpc
print(view.text())
# --- gRPC message 1 (3 bytes) ---
# 1: 150
Argument Meaning
grpc_mode True forces gRPC framing, False disables it, None (default) decides from content_type.
grpc_encoding The grpc-encoding header, used for compressed gRPC messages.
schema_path, message_type A .proto (compiled with protoc --include_imports, which must be on PATH) or .desc schema plus the fully qualified message type. Falls back to the raw wire decode with a note.
backend "wire" (built in, default) or "blackbox" (opt-in; falls back to "wire" with a note when not installed).
max_input_bytes Decode only a leading prefix (a preview cap).

The result's text() is plain text. Escape it before writing it into Rich markup.

Decoding a flow's body

Flow.decode_request_protobuf() and Flow.decode_response_protobuf() run the same decoder on a flow's (decompressed) request or response body. They pass on the flow's content type and grpc-encoding header:

view = flow.decode_response_protobuf(
    schema_path="api.proto",          # optional .proto or compiled .desc
    message_type="pkg.Reply",         # needed together with schema_path
    force=False,                      # True: decode whatever the content type
)
if view is not None and view.ok:
    print(view.text())

They return None when the body is empty, or when it neither has a protobuf/gRPC content type nor looks like protobuf (unless force=True). Otherwise they return a ProtobufView (from friTap.parsers.protobuf import ProtobufView):

Member Meaning
mode Decoder actually used: wire, blackbox or schema.
framing none, grpc, or grpc-invalid (gRPC was expected but the bytes are not gRPC framed, so they were decoded as bare protobuf).
messages One DecodedProtobufMessage per gRPC frame (or one for a bare body), with index, size, compressed, truncated, text, error, notes and ok.
notes, error Why something was not decoded, or why a schema was not applied.
ok True when at least one message decoded. A returned view can still have ok == False.
lines(), text() The rendered output, tagged by line kind or as plain text.

Breaking change in friTap 2.3.0

Before 2.3.0 these methods returned a list of ProtobufMessage trees (one per gRPC frame) or None, and ignored schema_path. They now return a ProtobufView and apply schema_path together with the new message_type argument. There is no compatibility shim. To get the old raw field tree, call decode_raw() on the body yourself.

Optional protobuf extras

The wire decoder has no dependencies. Schema decoding needs google.protobuf, and the alternative blackboxprotobuf backend needs the bbpb package. Install both with:

pip install "fritap[proto]"

In the TUI the "blackboxprotobuf backend" switch of the protobuf decoder dialog becomes available once bbpb is installed.


Legacy SSL_Logger (deprecated)

Deprecated — will be removed in friTap 3.0

SSL_Logger is retained only for backward compatibility. New code should use the FriTap builder (or pass a FriTapConfig — build one from old kwargs with FriTapConfig.from_legacy_params(...)).

Minimal legacy usage (live / device):

from friTap import SSL_Logger
import time

logger = SSL_Logger(
    app="firefox",
    pcap_name="traffic.pcap",
    keylog="keys.log",
    verbose=True,
)
logger.install_signal_handler()
logger.start_fritap_session()

while logger.running:
    time.sleep(1)

To migrate, replace the flat constructor with the builder:

from friTap import FriTap

session = FriTap("firefox").pcap("traffic.pcap").keylog("keys.log").verbose().start()

Next steps