-
-
Save pcaversaccio/9bda7737012e98b1c3935eeee95031a3 to your computer and use it in GitHub Desktop.
Protective wrappers for `curl`/`wget` that prevent homograph/IDN attacks by blocking URLs containing non-ASCII characters or Punycode domains.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -Eeuo pipefail | |
| RED=$'\033[1;31m' | |
| YELLOW=$'\033[1;33m' | |
| RESET=$'\033[0m' | |
| for arg in "$@"; do | |
| # Reject any non-ASCII byte. | |
| if LC_ALL=C printf "%s" "$arg" | grep -q "[^ -~]"; then | |
| printf "${RED}BLOCKED:${RESET} Non-ASCII characters detected!\n\n" >&2 | |
| visual=$(LC_ALL=C printf "%s" "$arg" | sed "s/[^ -~]/${RED}●${RESET}/g") | |
| printf " Visual: %b\n" "$visual" >&2 | |
| printf " Escaped: ${YELLOW}%q${RESET}\n\n" "$arg" >&2 | |
| printf " ${YELLOW}Suspicious bytes:${RESET}\n" >&2 | |
| LC_ALL=C printf "%s" "$arg" \ | |
| | grep -o "[^ -~]" \ | |
| | xxd -g1 \ | |
| | sed "s/^/ /" >&2 | |
| exit 1 | |
| fi | |
| # Reject Punycode (case-insensitive). | |
| if [[ "$arg" =~ [Xx][Nn]-- ]]; then | |
| printf "${RED}BLOCKED:${RESET} Punycode domain detected!\n\n" >&2 | |
| highlighted=${arg//xn--/${RED}xn--${RESET}} | |
| highlighted=${highlighted//Xn--/${RED}Xn--${RESET}} | |
| highlighted=${highlighted//XN--/${RED}XN--${RESET}} | |
| highlighted=${highlighted//xN--/${RED}xN--${RESET}} | |
| printf " URL: %b\n\n" "$highlighted" >&2 | |
| exit 1 | |
| fi | |
| done | |
| exec /usr/bin/curl "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -Eeuo pipefail | |
| RED=$'\033[1;31m' | |
| YELLOW=$'\033[1;33m' | |
| RESET=$'\033[0m' | |
| for arg in "$@"; do | |
| # Reject any non-ASCII byte. | |
| if LC_ALL=C printf "%s" "$arg" | grep -q "[^ -~]"; then | |
| printf "${RED}BLOCKED:${RESET} Non-ASCII characters detected!\n\n" >&2 | |
| visual=$(LC_ALL=C printf "%s" "$arg" | sed "s/[^ -~]/${RED}●${RESET}/g") | |
| printf " Visual: %b\n" "$visual" >&2 | |
| printf " Escaped: ${YELLOW}%q${RESET}\n\n" "$arg" >&2 | |
| printf " ${YELLOW}Suspicious bytes:${RESET}\n" >&2 | |
| LC_ALL=C printf "%s" "$arg" \ | |
| | grep -o "[^ -~]" \ | |
| | xxd -g1 \ | |
| | sed "s/^/ /" >&2 | |
| exit 1 | |
| fi | |
| # Reject Punycode (case-insensitive). | |
| if [[ "$arg" =~ [Xx][Nn]-- ]]; then | |
| printf "${RED}BLOCKED:${RESET} Punycode domain detected!\n\n" >&2 | |
| highlighted=${arg//xn--/${RED}xn--${RESET}} | |
| highlighted=${highlighted//Xn--/${RED}Xn--${RESET}} | |
| highlighted=${highlighted//XN--/${RED}XN--${RESET}} | |
| highlighted=${highlighted//xN--/${RED}xN--${RESET}} | |
| printf " URL: %b\n\n" "$highlighted" >&2 | |
| exit 1 | |
| fi | |
| done | |
| exec /usr/bin/wget "$@" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
Verification
Verify the installation locations:
Verify the files exist and are executable:
Verify they're Bash scripts:
Test non-ASCII blocking:
Test Punycode blocking:
Test legitimate URLs still work:
Verify the real binaries are still accessible: