#!/usr/bin/env bash
set -euo pipefail
# Script Name: encode
# Description: Encoding/Decoding Swiss Army Knife for security testing
# Usage: encode base64 "hello" # Base64 encode
# encode base64d "aGVsbG8=" # Base64 decode
# encode url "hello world" # URL encode
# encode urld "hello%20world" # URL decode
# encode html "\""
echo
echo -e "${BOLD}SECURITY NOTE:${NC}"
echo " This tool is for authorized security testing and educational purposes only."
}
# Read input from argument or stdin
get_input() {
if [[ $# -gt 0 ]]; then
echo "$*"
else
cat
fi
}
# Base64 operations
base64_encode() {
local input=$(get_input "$@")
echo -n "$input" | base64 -w 0 2>/dev/null || echo -n "$input" | base64
}
base64_decode() {
local input=$(get_input "$@")
echo -n "$input" | base64 -d 2>/dev/null || echo -n "$input" | base64 -D
}
# URL operations
url_encode() {
local input=$(get_input "$@")
python3 -c "import urllib.parse; print(urllib.parse.quote('$input'))"
}
url_decode() {
local input=$(get_input "$@")
python3 -c "import urllib.parse; print(urllib.parse.unquote('$input'))"
}
# HTML operations
html_encode() {
local input=$(get_input "$@")
python3 -c "import html; print(html.escape('$input'))"
}
html_decode() {
local input=$(get_input "$@")
python3 -c "import html; print(html.unescape('$input'))"
}
# Hex operations
hex_encode() {
local input=$(get_input "$@")
echo -n "$input" | xxd -p | tr -d '\n'
}
hex_decode() {
local input=$(get_input "$@")
echo -n "$input" | xxd -r -p
}
# Unicode operations
unicode_encode() {
local input=$(get_input "$@")
python3 << EOF
import sys
text = "$input"
result = ''.join(f'\\u{ord(c):04x}' for c in text)
print(result)
EOF
}
unicode_decode() {
local input=$(get_input "$@")
python3 -c "print('$input'.encode().decode('unicode_escape'))"
}
# JWT decode
jwt_decode() {
local token=$(get_input "$@")
# Split JWT into parts
IFS='.' read -ra PARTS <<< "$token"
if [[ ${#PARTS[@]} -ne 3 ]]; then
echo "${RED}Error: Invalid JWT format${NC}" >&2
return 1
fi
echo -e "${BOLD}${CYAN}=== JWT Header ===${NC}"
echo "${PARTS[0]}" | base64_decode | python3 -m json.tool
echo -e "\n${BOLD}${CYAN}=== JWT Payload ===${NC}"
echo "${PARTS[1]}" | base64_decode | python3 -m json.tool
echo -e "\n${BOLD}${YELLOW}Note: Signature not verified${NC}"
}
# Hash generation
generate_hash() {
local algo=$1
shift
local input=$(get_input "$@")
case "$algo" in
md5)
echo -n "$input" | md5sum | awk '{print $1}'
;;
sha1)
echo -n "$input" | sha1sum | awk '{print $1}'
;;
sha256)
echo -n "$input" | sha256sum | awk '{print $1}'
;;
sha512)
echo -n "$input" | sha512sum | awk '{print $1}'
;;
*)
echo "${RED}Error: Unknown hash algorithm: $algo${NC}" >&2
echo "Available: md5, sha1, sha256, sha512" >&2
return 1
;;
esac
}
# XSS payload variants
xss_variants() {
local input=$(get_input "$@")
echo -e "${BOLD}${CYAN}=== XSS Payload Variants ===${NC}\n"
echo -e "${YELLOW}[Original]${NC}"
echo "$input"
echo -e "\n${YELLOW}[URL Encoded]${NC}"
url_encode "$input"
echo -e "\n${YELLOW}[Double URL Encoded]${NC}"
url_encode "$(url_encode "$input")"
echo -e "\n${YELLOW}[HTML Entity Encoded]${NC}"
html_encode "$input"
echo -e "\n${YELLOW}[Hex Encoded]${NC}"
hex_encode "$input"
echo -e "\n${YELLOW}[Base64]${NC}"
base64_encode "$input"
echo -e "\n${YELLOW}[Unicode Escaped]${NC}"
unicode_encode "$input"
echo -e "\n${BOLD}${GREEN}Tip: Use these to bypass WAF filters${NC}"
}
# SQL injection variants
sqli_variants() {
local input=$(get_input "$@")
echo -e "${BOLD}${CYAN}=== SQL Injection Variants ===${NC}\n"
echo -e "${YELLOW}[Original]${NC}"
echo "$input"
echo -e "\n${YELLOW}[URL Encoded]${NC}"
url_encode "$input"
echo -e "\n${YELLOW}[Double URL Encoded]${NC}"
url_encode "$(url_encode "$input")"
echo -e "\n${YELLOW}[Uppercase]${NC}"
echo "$input" | tr '[:lower:]' '[:upper:]'
echo -e "\n${YELLOW}[Mixed Case]${NC}"
python3 << EOF
import random
text = "$input"
result = ''.join(c.upper() if random.random() > 0.5 else c.lower() for c in text)
print(result)
EOF
echo -e "\n${YELLOW}[With Comments]${NC}"
echo "$input" | sed 's/ /\/**\/ /g'
echo -e "\n${BOLD}${GREEN}Tip: Combine with timing to test blind SQLi${NC}"
}
# Main logic
if [[ $# -eq 0 ]]; then
show_help
exit 0
fi
operation=$1
shift
case "$operation" in
-h|--help|help)
show_help
;;
base64|b64)
base64_encode "$@"
;;
base64d|b64d)
base64_decode "$@"
;;
url)
url_encode "$@"
;;
urld)
url_decode "$@"
;;
html)
html_encode "$@"
;;
htmld)
html_decode "$@"
;;
hex)
hex_encode "$@"
;;
hexd)
hex_decode "$@"
;;
unicode|uni)
unicode_encode "$@"
;;
unicoded|unid)
unicode_decode "$@"
;;
jwt)
jwt_decode "$@"
;;
hash)
if [[ $# -eq 0 ]]; then
echo "${RED}Error: Hash algorithm required${NC}" >&2
echo "Usage: encode hash " >&2
exit 1
fi
generate_hash "$@"
;;
xss)
xss_variants "$@"
;;
sqli|sql)
sqli_variants "$@"
;;
*)
echo "${RED}Error: Unknown operation: $operation${NC}" >&2
echo "Run 'encode --help' for usage information" >&2
exit 1
;;
esac