wpe/mon
2025-09-12 11:35:31 +02:00

102 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Display information about the required options
usage() {
cat <<'EOF'
Usage: $(basename "$0") -i <interface>
Options:
-i, --iface Network interface name (e.g., wlan1, eth1). Required.
-h, --help Show this help and exit.
Example:
$(basename "$0") -i wlan1
EOF
}
iface="wlan1"
# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
echo "[!] This script requires root permissions." >&2
exit 1
fi
# Translate long options to short for getopts
# Keeping the script POSIX-friendly without relying on GNU getopt
if [[ $# -gt 0 ]]; then
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--iface) args+=("-i"); shift ;;
--help) args+=("-h"); shift ;;
*) args+=("$1"); shift ;;
esac
done
set -- "${args[@]}"
fi
# Parse short options
while getopts ":i:h" opt; do
case "$opt" in
i) iface="$OPTARG" ;;
h) usage; exit 0 ;;
\?) echo "Error: Invalid option -$OPTARG" >&2; usage; exit 1 ;;
:) echo "Error: Option -$OPTARG requires an argument." >&2; usage; exit 1 ;;
esac
done
# Require -i/--iface
if [[ -z "${iface}" ]]; then
echo "Error: A network interface must be pecified with -i/--iface." >&2
echo "Example: $(basename "$0") -i wlan1" >&2
exit 1
fi
# Validate the interface exists on the system
if ! ip link show "$iface" &>/dev/null; then
echo "Error: Interface '$iface' does not exist on this system." >&2
exit 1
fi
# Check if airodump-ng is installed
if ! command -v airodump-ng &>/dev/null; then
read -r -p "[!] airodump-ng is not installed. Install it now? (Y/n): " choice
choice=${choice:-Y}
if [[ $choice =~ ^[Yy]$ ]]; then
if ! apt update || ! apt install -y aircrack-ng; then
echo "[!] Installation failed. Exiting."
exit 1
fi
else
echo "[!] airodump-ng is required. Exiting."
exit 1
fi
fi
echo "[+] Preparing $iface for monitor mode..."
nmcli device set "$iface" managed no
ip link set "$iface" down
iw dev "$iface" set type monitor
ip link set "$iface" up
# Check for hostapd-wpe.conf in the current directory
# For monitoring only used for using the same channel as the ap
if [[ -f "hostapd-wpe.conf" ]]; then
echo "[+] Found hostapd-wpe.conf in the current directory. Using it."
# Import channel variable only
source <(grep -E '^[[:space:]]*channel[[:space:]]*=' hostapd-wpe.conf)
else
echo "[!] hostapd-wpe.conf not found in the current directory. Using the default: /etc/hostapd-wpe/hostapd-wpe.conf"
source <(grep -E '^[[:space:]]*channel[[:space:]]*=' /etc/hostapd-wpe/hostapd-wpe.conf)
fi
TIMESTAMP="$(date '+%Y-%m-%d_%H-%M-%S')"
# Start monitoring
if ! airodump-ng --channel $channel -w wpa-${TIMESTAMP} "$iface"; then
echo "[!] Failed to start airodump-ng. Exiting."
exit 1
fi