c3f0017193
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
115 lines
4.1 KiB
Bash
115 lines
4.1 KiB
Bash
#!/usr/bin/env bash
|
|
# start_mitm_capture — Arranca mitmdump en segundo plano como proxy de interceptación
|
|
# con rotación de capturas cada N minutos vía addon Python del registry.
|
|
|
|
start_mitm_capture() {
|
|
local port=8080
|
|
local out_dir="$HOME/captures"
|
|
local rotate_min=20
|
|
local addon_path=""
|
|
local mitmdump_bin=""
|
|
local log_path=""
|
|
|
|
# Parseo de argumentos
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--port) port="$2"; shift 2 ;;
|
|
--out) out_dir="$2"; shift 2 ;;
|
|
--rotate-min) rotate_min="$2"; shift 2 ;;
|
|
--addon) addon_path="$2"; shift 2 ;;
|
|
--mitmdump) mitmdump_bin="$2"; shift 2 ;;
|
|
--log) log_path="$2"; shift 2 ;;
|
|
*)
|
|
echo "ERROR: argumento desconocido: $1" >&2
|
|
echo "Uso: start_mitm_capture [--port N] [--out DIR] [--rotate-min N] [--addon PATH] [--mitmdump BIN] [--log PATH]" >&2
|
|
return 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Derivar raíz del registry cuando FN_REGISTRY_ROOT no está seteado
|
|
local registry_root
|
|
if [[ -n "${FN_REGISTRY_ROOT:-}" ]]; then
|
|
registry_root="$FN_REGISTRY_ROOT"
|
|
else
|
|
# bash/functions/cybersecurity/ -> 3 niveles arriba = raíz del registry
|
|
registry_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
|
|
fi
|
|
|
|
# Default addon usando la raíz derivada
|
|
if [[ -z "$addon_path" ]]; then
|
|
addon_path="${registry_root}/python/functions/cybersecurity/rotate_capture_flows.py"
|
|
fi
|
|
|
|
# Default log path (depende de out_dir, se resuelve después de crear el dir)
|
|
# Se asigna más abajo si sigue vacío
|
|
|
|
# Crear directorio de capturas si no existe
|
|
if [[ ! -d "$out_dir" ]]; then
|
|
mkdir -p "$out_dir" || {
|
|
echo "ERROR: no se pudo crear el directorio de capturas: $out_dir" >&2
|
|
return 1
|
|
}
|
|
fi
|
|
|
|
# Asignar log por defecto ahora que out_dir existe
|
|
if [[ -z "$log_path" ]]; then
|
|
log_path="${out_dir}/mitmdump.log"
|
|
fi
|
|
|
|
# Resolver binario mitmdump
|
|
if [[ -z "$mitmdump_bin" ]]; then
|
|
if command -v mitmdump &>/dev/null; then
|
|
mitmdump_bin="$(command -v mitmdump)"
|
|
elif [[ -x "$HOME/.local/bin/mitmdump" ]]; then
|
|
mitmdump_bin="$HOME/.local/bin/mitmdump"
|
|
else
|
|
echo "ERROR: mitmdump no encontrado; instala con: uv tool install mitmproxy" >&2
|
|
return 1
|
|
fi
|
|
else
|
|
if [[ ! -x "$mitmdump_bin" ]]; then
|
|
echo "ERROR: el binario indicado no existe o no es ejecutable: $mitmdump_bin" >&2
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
# Verificar que el addon existe
|
|
if [[ ! -f "$addon_path" ]]; then
|
|
echo "ERROR: addon no encontrado: $addon_path" >&2
|
|
echo " Asegúrate de que FN_REGISTRY_ROOT apunta a la raíz del registry" >&2
|
|
echo " o pasa --addon con la ruta correcta a rotate_capture_flows.py" >&2
|
|
return 1
|
|
fi
|
|
|
|
# Arrancar mitmdump en background con setsid (sobrevive al cierre de la shell).
|
|
# El redirect </dev/null desacopla stdin: sin él, el proceso retiene el pipe
|
|
# heredado y la shell sale con exit 144 (SIGTERM) al cerrarse. disown lo saca
|
|
# de la tabla de jobs para que la shell no le envíe señales al terminar.
|
|
setsid "$mitmdump_bin" \
|
|
-s "$addon_path" \
|
|
--set "rotate_min=${rotate_min}" \
|
|
--set "capture_dir=${out_dir}" \
|
|
--listen-port "$port" \
|
|
</dev/null >> "$log_path" 2>&1 &
|
|
local pid=$!
|
|
disown "$pid" 2>/dev/null || true
|
|
|
|
# Esperar ~1s y verificar que el proceso sigue vivo
|
|
sleep 1
|
|
if ! kill -0 "$pid" 2>/dev/null; then
|
|
echo "ERROR: mitmdump murió al arrancar. Últimas líneas del log:" >&2
|
|
tail -20 "$log_path" >&2
|
|
return 1
|
|
fi
|
|
|
|
# Emitir JSON con información del proceso arrancado
|
|
printf '{"pid": %d, "port": %d, "out_dir": "%s", "rotate_min": %d, "log": "%s"}\n' \
|
|
"$pid" "$port" "$out_dir" "$rotate_min" "$log_path"
|
|
}
|
|
|
|
# Ejecutar si se llama directamente (fn run / bash <file>)
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
start_mitm_capture "$@"
|
|
fi
|