Skip to content

Instantly share code, notes, and snippets.

@AAS
Forked from koolvn/01-WG UDP hack on AsusWRT-Merlin.md
Created August 30, 2024 07:53
Show Gist options
  • Select an option

  • Save AAS/b6ca1f458f9a3e0a8070aca82becf28b to your computer and use it in GitHub Desktop.

Select an option

Save AAS/b6ca1f458f9a3e0a8070aca82becf28b to your computer and use it in GitHub Desktop.

Revisions

  1. @koolvn koolvn revised this gist Aug 27, 2024. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,12 @@
    # UDP Trash Hack for WireGuard on AsusWRT Merlin
    # UDP Trash Hack for WireGuard on [AsusWRT Merlin](https://www.asuswrt-merlin.net/)

    ### Установка
    * Кладём файл `wgclient-start` в `/jffs/scripts/`
    * Делаем скрипт запускаемым
    ```bash
    chmod +x /jffs/scripts/wgclient-start
    ```
    * Done ✅
    * Done ✅


    Проверен на роутере ASUS RT-AX88U PRO Firmware: AsusWRT Merlin 3004.388.8_2
  2. @koolvn koolvn revised this gist Aug 26, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # UDP Trash Hack for WireGuard on AsusWRT Merlin

    * Кладём файл `wgclient-start` в `/jffs/scripts/`
    * Делаем скрипт запускаемым
    ```bash
  3. @koolvn koolvn created this gist Aug 26, 2024.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    * Кладём файл `wgclient-start` в `/jffs/scripts/`
    * Делаем скрипт запускаемым
    ```bash
    chmod +x /jffs/scripts/wgclient-start
    ```
    * Done ✅
    48 changes: 48 additions & 0 deletions wgclient-start
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/sh

    sleep 3
    # Log the start of the script
    logger -t WireGuardClientUp "Starting junk-udp-hack script after WireGuard client #$1 connection."

    # Enable exit on error
    set -e

    # Function to generate a random number between 49152 and 65535
    generate_random_port() {
    awk -v min=49152 -v max=65535 'BEGIN { srand(); print int(min + rand() * (max - min + 1)) }'
    }

    interface="wgc$1"

    logger -t WireGuardClientUp "Processing WG interface - $interface"

    # Retrieve the server and port
    wg_endpoint=$(wg show "$interface" endpoints)
    if [ -z "$wg_endpoint" ]; then
    logger -t WireGuardClientUp "Unable to retrieve endpoint for interface $interface" >&2
    fi

    wg_server=$(echo "$wg_endpoint" | awk '{print $2}' | cut -d':' -f1)
    wg_port=$(echo "$wg_endpoint" | awk '{print $2}' | cut -d':' -f2)

    if [ -z "$wg_server" ] || [ -z "$wg_port" ]; then
    logger -t WireGuardClientUp "Unable to extract server or port for interface $interface" >&2
    fi

    # Generate a random message
    message=$(dd if=/dev/urandom bs=228 count=5 2>/dev/null | tr -dc 'A-Za-z0-9')

    # Generate a new random port and ensure it's not in use
    l_port=$(generate_random_port)
    while netstat -an | grep -qE '(^|[^0-9])'"$l_port"'([^0-9]|$)'; do
    logger -t WireGuardClientUp "Port $l_port is already in use. Generating new one"
    l_port=$(generate_random_port)
    done

    logger -t WireGuardClientUp "WG server $wg_server:$wg_port Source port $l_port"

    # Send the message using socat
    echo "$message" | socat - UDP-SENDTO:"$wg_server:$wg_port",sourceport="$l_port"

    # Update the WireGuard interface with the new listen port
    wg set "$interface" listen-port "$l_port"