#!/usr/bin/env bash # ---------------------------------------------------------------------- # Copyright (c) 2024 Pablo Caro. All Rights Reserved. # Pablo Caro - https://pcaro.es/ # BaconHash client # ---------------------------------------------------------------------- set -euo pipefail API_ENDPOINT="https://api.baconhash.pw" TOKEN_FILE="$HOME/.baconhash" # ----------------------------------------------------------------------- # Resolve token: env var > token file # ----------------------------------------------------------------------- TOKEN="${BACONHASH_TOKEN:-}" if [[ -z "$TOKEN" && -f "$TOKEN_FILE" ]]; then TOKEN=$(head -n 1 "$TOKEN_FILE") fi if [[ -z "$TOKEN" ]]; then echo "ERROR: No API token found." >&2 echo " Set BACONHASH_TOKEN env variable, or store your token in $TOKEN_FILE:" >&2 echo " echo '' > $TOKEN_FILE" >&2 exit 1 fi # ----------------------------------------------------------------------- # Usage # ----------------------------------------------------------------------- function usage() { cat < $(basename "$0") DESCRIPTION: Look up MD5, NTLM, or SHA1 hashes against the BaconHash database. When a FILE is provided (one hash per line), the hashes are sent as a bulk POST request and results are printed as JSON. When a single HASH is provided, a GET request is made and the result is printed as JSON. TOKEN: Store your token in $TOKEN_FILE (one line), or export BACONHASH_TOKEN=. EXAMPLES: $(basename "$0") 2707569be0aff4a956388a851c68b4c6 $(basename "$0") hashes.txt EOF } if [[ $# -ne 1 ]]; then usage exit 0 fi ARG="$1" # ----------------------------------------------------------------------- # Pretty-print helper (jq if available, else raw) # ----------------------------------------------------------------------- function pretty() { if command -v jq &>/dev/null; then jq . else cat fi } # ----------------------------------------------------------------------- # Dispatch # ----------------------------------------------------------------------- if [[ -f "$ARG" ]]; then # Bulk lookup via POST: build JSON array from file lines JSON_HASHES=$(jq -Rn '[inputs | select(length > 0)]' < "$ARG" 2>/dev/null \ || python3 -c " import sys, json lines = [l.strip() for l in open('$ARG') if l.strip()] print(json.dumps({'hashes': lines})) ") # If jq constructed a plain array, wrap it; if python3 wrote full body, use it if echo "$JSON_HASHES" | jq -e 'type == "array"' &>/dev/null 2>&1; then JSON_BODY=$(echo "$JSON_HASHES" | jq '{hashes: .}') else JSON_BODY="$JSON_HASHES" fi curl -s -X POST "$API_ENDPOINT/search" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$JSON_BODY" | pretty else # Single hash via GET (URL-encode the input) ENCODED=$(python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$ARG" 2>/dev/null \ || python3 -c " import urllib.parse, sys print(urllib.parse.quote('$ARG', safe='')) ") curl -s "$API_ENDPOINT/search/$ENCODED" \ -H "Authorization: Bearer $TOKEN" | pretty fi