diff --git a/mon b/mon index 7910ba5..aeb8395 100755 --- a/mon +++ b/mon @@ -1,36 +1,98 @@ #!/usr/bin/env bash +set -euo pipefail + +# Display information about the required options +usage() { + cat <<'EOF' +Usage: $(basename "$0") -i + +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 must be run as root!" >&2 + echo "[!] This script requires root permissions." >&2 exit 1 fi -# List network interfaces and their capabilities -echo "[+] Available network interfaces:" -iw dev | awk '/Interface/ {iface=$2; print "- Interface: " iface} /type/ {print " Current Mode: "$2}'; +# 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 -# Prompt user for the network card -while true; do - read -r -p "[?] Enter the interface to use for monitoring with airodump-ng (default: wlan1): " iface - iface=${iface:-wlan1} - - if ip link show "$iface" &>/dev/null; then - break - else - echo "[!] Interface $iface does not exist. Please enter a valid interface." - 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 ;; + end done -# Set up the interface +# 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." + source hostapd-wpe.conf +else + echo "[!] hostapd-wpe.conf not found in the current directory. Using the default: /etc/hostapd-wpe/hostapd-wpe.conf" + source /etc/hostapd-wpe/hostapd-wpe.conf +fi + # Start monitoring -if ! airodump-ng -w wpa "$iface"; then +if ! airodump-ng --channel $channel -w wpa "$iface"; then echo "[!] Failed to start airodump-ng. Exiting." exit 1 fi