Skip to content

reconFTW is a tool designed to perform automated recon on a target domain by running the best set of tools to perform scanning and finding out vulnerabilities

9.2 KB
Raw Blame
#!/bin/bash
# reconFTW Validation Library
# Provides type checking and input validation functions

set -o pipefail

# Error codes (guard for re-source in test harnesses)
# shellcheck disable=SC2034  # These are exported for use in modules
if [[ -z "${E_INVALID_DOMAIN+x}" ]]; then
    readonly E_INVALID_DOMAIN=20
    readonly E_INVALID_IP=21
    readonly E_INVALID_PATH=22
    readonly E_INVALID_URL=23
    readonly E_INVALID_EMAIL=24
fi

# validate_domain()
# Description: Validates a domain name format
# Arguments: $1 - Domain string to validate
# Returns: 0 if valid, 1 if invalid
function validate_domain() {
	local domain="$1"

	# Check if empty
	if [[ -z "$domain" ]]; then
		return $E_INVALID_DOMAIN
	fi

	# Check for command injection characters
	if echo "$domain" | grep -qE '[;|&$`\\(){}]'; then
		return $E_INVALID_DOMAIN
	fi

	# Check if it's an IP address (valid for scanning)
	if [[ "$domain" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
		# Validate IP octets
		local IFS='.'
		local -a octets
		read -ra octets <<<"$domain"
		for octet in "${octets[@]}"; do
			if [[ "$octet" -lt 0 || "$octet" -gt 255 ]]; then
				return $E_INVALID_DOMAIN
			fi
		done
		return 0
	fi

	# Validate domain name format (simplified RFC check)
	if [[ ! "$domain" =~ ^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$ ]]; then
		return $E_INVALID_DOMAIN
	fi

	# Check total length
	if [[ ${#domain} -gt 253 ]]; then
		return $E_INVALID_DOMAIN
	fi

	return 0
}

# NOTE: sanitize_domain() is defined in modules/utils.sh
# This avoids duplication and ensures consistent behavior

# validate_ipv4()
# Description: Validates an IPv4 address
# Arguments: $1 - IP string to validate
# Returns: 0 if valid, 1 if invalid
function validate_ipv4() {
	local ip="$1"

	if [[ ! "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
		return $E_INVALID_IP
	fi

	local IFS='.'
	local -a octets
	read -ra octets <<<"$ip"

	if [[ ${#octets[@]} -ne 4 ]]; then
		return $E_INVALID_IP
	fi

	for octet in "${octets[@]}"; do
		if [[ ! "$octet" =~ ^[0-9]+$ ]]; then
			return $E_INVALID_IP
		fi
		if [[ "$octet" -lt 0 || "$octet" -gt 255 ]]; then
			return $E_INVALID_IP
		fi
	done

	return 0
}

# validate_boolean()
# Description: Validates a boolean value
# Arguments: $1 - Value to validate
# Returns: 0 if valid boolean, 1 otherwise
function validate_boolean() {
	local value="$1"

	if [[ "$value" =~ ^(true|false|1|0|yes|no)$ ]]; then
		return 0
	fi

	return 1
}

# validate_integer()
# Description: Validates an integer within optional range
# Arguments: $1 - Value, $2 - Min (optional), $3 - Max (optional)
# Returns: 0 if valid, 1 if invalid
function validate_integer() {
	local value="$1"
	local min="${2:-}"
	local max="${3:-}"

	if [[ ! "$value" =~ ^-?[0-9]+$ ]]; then
		return 1
	fi

	if [[ -n "$min" && "$value" -lt "$min" ]]; then
		return 1
	fi

	if [[ -n "$max" && "$value" -gt "$max" ]]; then
		return 1
	fi

	return 0
}

# validate_port()
# Description: Validates a TCP/UDP port number
# Arguments: $1 - Port number
# Returns: 0 if valid port (1-65535), 1 otherwise
function validate_port() {
	local port="$1"
	validate_integer "$port" 1 65535
}

# sanitize_path()
# Description: Sanitizes a file path
# Arguments: $1 - Path to sanitize
# Returns: Echoes sanitized path
function sanitize_path() {
	local path="$1"

	# Remove control characters
	path=$(printf '%s' "$path" | tr -d '\000-\037')

	# Remove trailing slashes (except root)
	if [[ "$path" != "/" ]]; then
		path=$(printf '%s' "$path" | sed 's:/*$::')
	fi

	# Normalize path
	path=$(printf '%s' "$path" | sed 's://*:/:g')

	printf '%s' "$path"
}

# sanitize_interlace_input()
# Description: Sanitizes input for interlace tool
# Arguments: $1 - Input file, $2 - Output file (optional)
# Returns: 0 on success
function sanitize_interlace_input() {
	local infile="$1"
	local outfile="${2:-$1}"

	# Remove lines containing shell metacharacters
	local pattern='[;|&$`\(){}]'

	if [[ "$infile" == "$outfile" ]]; then
		local tmpfile
		tmpfile=$(mktemp)
		grep -v "$pattern" "$infile" >"$tmpfile" 2>/dev/null || true
		mv "$tmpfile" "$outfile"
	else
		grep -v "$pattern" "$infile" >"$outfile" 2>/dev/null || true
	fi

	return 0
}

# is_in_scope_host()
# Description: Returns 0 if $1 (hostname) is exactly $2 (domain) or a proper
# subdomain of it. Prevents substring false positives (exampleXcom.evil) used by
# raw "grep -a $domain" checks.
# Arguments: $1 - hostname, $2 - in-scope domain
# Returns: 0 if in scope, 1 otherwise
function is_in_scope_host() {
	local host="${1,,}"
	local domain="${2,,}"
	host="${host%.}"
	domain="${domain%.}"
	[[ -n "$host" && -n "$domain" ]] || return 1
	[[ "$host" == "$domain" || "$host" == *.".$domain" || "$host" == *."$domain" ]]
}

# filter_in_scope_urls()
# Description: Reads URLs from stdin, writes to stdout only those whose parsed
# hostname is exactly $1 or a subdomain of $1. Rejects URLs containing userinfo
# (user:pass@) to block SSRF-via-basic-auth tricks, non-http(s) schemes, and
# substring look-alikes. Depends on python3.
# Arguments: $1 - in-scope domain
function filter_in_scope_urls() {
	local domain="$1"
	[[ -z "$domain" ]] && return 0

	# Use `python3 -c` (not `- <<HEREDOC`) so sys.stdin stays wired to the
	# incoming pipe; heredoc consumes fd 0 and breaks the pipeline.
	#
	# "Multi" is a sentinel used by list-mode (-l) runs where there is no single
	# in-scope root. In that case, accept any URL with an http/https scheme and
	# no userinfo — the scope is enforced upstream by the list itself.
	python3 -c '
import sys
from urllib.parse import urlsplit

domain = sys.argv[1].rstrip(".").lower()
multi_mode = domain == "multi"
if not domain:
    sys.exit(0)

for raw in sys.stdin:
    url = raw.strip()
    if not url:
        continue
    try:
        parsed = urlsplit(url)
    except ValueError:
        continue
    if parsed.scheme not in ("http", "https"):
        continue
    if parsed.username is not None or parsed.password is not None:
        continue
    host = (parsed.hostname or "").rstrip(".").lower()
    if not host:
        continue
    if multi_mode or host == domain or host.endswith("." + domain):
        print(url)
' "$domain"
}

# filter_in_scope_hosts()
# Description: Reads hostnames (one per line) from stdin, writes only those
# equal to $1 or a subdomain of $1 to stdout. Strips wildcard and trailing
# dots. Unlike `grep -a "$domain"` this anchors the match so `exampleXcom.evil`
# does NOT pass.
# Arguments: $1 - in-scope domain
function filter_in_scope_hosts() {
	local domain="$1"
	[[ -z "$domain" ]] && return 0

	python3 -c '
import sys

domain = sys.argv[1].rstrip(".").lower()
if not domain:
    sys.exit(0)

for raw in sys.stdin:
    host = raw.strip().rstrip(".")
    if not host:
        continue
    if host.startswith("*."):
        host = host[2:]
    host = host.lower()
    if host == domain or host.endswith("." + domain):
        print(host)
' "$domain"
}

# is_in_scope_url()
# Description: Single-URL wrapper around filter_in_scope_urls. Prints the URL
# to stdout if it passes all scope checks (scheme, no userinfo, host match),
# otherwise exits non-zero.
# Arguments: $1 - URL, $2 - in-scope domain
# Returns: 0 if in scope, 1 otherwise
function is_in_scope_url() {
	local url="$1"
	local domain="$2"
	local result
	result=$(printf '%s\n' "$url" | filter_in_scope_urls "$domain")
	[[ -n "$result" ]] || return 1
	printf '%s\n' "$result"
}

# is_empty()
# Description: Checks if a value is empty
# Arguments: $1 - Value to check
# Returns: 0 if empty, 1 if not empty
function is_empty() {
	local value="$1"

	if [[ -z "${value// /}" ]]; then
		return 0
	fi

	return 1
}

# is_numeric()
# Description: Checks if a value is numeric
# Arguments: $1 - Value to check
# Returns: 0 if numeric, 1 if not
function is_numeric() {
	local value="$1"

	if [[ "$value" =~ ^-?[0-9]+(\.[0-9]+)?$ ]]; then
		return 0
	fi

	return 1
}

# validate_file_exists()
# Description: Validates that a file exists
# Arguments: $1 - File path
# Returns: 0 if file exists, E_INVALID_PATH if not
function validate_file_exists() {
	local file="$1"

	if [[ -z "$file" ]]; then
		return $E_INVALID_PATH
	fi

	if [[ ! -e "$file" ]]; then
		return $E_INVALID_PATH
	fi

	return 0
}

# validate_file_readable()
# Description: Validates that a file exists and is readable
# Arguments: $1 - File path
# Returns: 0 if readable, E_INVALID_PATH if not
function validate_file_readable() {
	local file="$1"

	if ! validate_file_exists "$file"; then
		return $E_INVALID_PATH
	fi

	if [[ ! -r "$file" ]]; then
		return $E_INVALID_PATH
	fi

	if [[ ! -f "$file" ]]; then
		return $E_INVALID_PATH
	fi

	return 0
}

# validate_directory()
# Description: Validates that a directory exists
# Arguments: $1 - Directory path
# Returns: 0 if valid directory, E_INVALID_PATH if not
function validate_directory() {
	local dir="$1"

	if [[ -z "$dir" ]]; then
		return $E_INVALID_PATH
	fi

	if [[ ! -d "$dir" ]]; then
		return $E_INVALID_PATH
	fi

	return 0
}

# validate_writable_directory()
# Description: Validates that a directory exists and is writable
# Arguments: $1 - Directory path
# Returns: 0 if writable, E_INVALID_PATH if not
function validate_writable_directory() {
	local dir="$1"

	if ! validate_directory "$dir"; then
		return $E_INVALID_PATH
	fi

	if [[ ! -w "$dir" ]]; then
		return $E_INVALID_PATH
	fi

	return 0
}