#!/bin/bash
##author  = butzel
##license = GPLv3: https://www.gnu.org/licenses/gpl-3.0.txt
##year    = 2015,2018,2022
# Simple WIFI-DEAUTH-DETECTOR


W_DEVICE=${1:-"wlan0"}  # DEVICE for monitoring
DEAUTH_S=${2:-3}        # DEAUTH packages per sec needed
DURATION=${3:-7}        # DURATION in sec

##########################

airmon-ng start "$W_DEVICE"
INTERFACE=$(iw dev | awk '/Interface/ {print $2}' | grep -E "^${W_DEVICE}(mon)?$|mon$" | head -n1)

ALARM_COUNT=0

while true; do
    PACKET_COUNT=$(
        timeout 1 tcpdump \
            -i "$INTERFACE" \
            -n \
            -l \
            -s 0 \
            'type mgt subtype deauth' \
            2>/dev/null |
        wc -l
    )

    if [ "$PACKET_COUNT" -ge "$DEAUTH_S" ]; then
        ALARM_COUNT=$((ALARM_COUNT + 1))

        echo "Deauth: ${PACKET_COUNT} packets/sec"

        if [ "$ALARM_COUNT" -ge "$DURATION" ]; then
            echo "Sir, we are under attack!"
        fi
    else
        ALARM_COUNT=0
    fi
done
