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

nat/vps detection

six2dezbf96bb54eb0e
diff --git a/modules/utils.sh b/modules/utils.sh
index 888fe89..9aeea43 100644
--- a/modules/utils.sh
+++ b/modules/utils.sh
@@ -1059,13 +1059,35 @@ _ip_is_public_ipv4() {
return 0
}
-# Detect if running behind NAT (home router / CGNAT / unknown network) by checking local IP.
-# Returns 0 (true) if behind NAT or undetectable, 1 (false) if public IP (VPS/dedicated).
-_is_behind_nat() {
- local ip
- ip=$(_get_local_ip)
- _ip_is_public_ipv4 "$ip" && return 1
- return 0
+# Check if a cloud metadata endpoint is reachable (AWS, GCP, Azure, DO, Hetzner, Oracle).
+# Returns 0 if running on a cloud VPS, 1 otherwise.
+_is_cloud_vps() {
+ curl -sf --max-time 2 -o /dev/null http://169.254.169.254/ 2>/dev/null && return 0
+ return 1
+}
+
+# Get external public IPv4 via a remote service. Returns empty on failure.
+_get_external_ipv4() {
+ local ext=""
+ ext=$(curl -4 -sf --max-time 5 ifconfig.me 2>/dev/null)
+ [[ -z "$ext" ]] && ext=$(curl -4 -sf --max-time 5 api.ipify.org 2>/dev/null)
+ _ip_is_public_ipv4 "$ext" && echo "$ext" && return 0
+ return 1
+}
+
+# Determine if puredns is safe to use (public network / cloud VPS).
+# Returns 0 (true = puredns safe) or 1 (false = use dnsx).
+# Priority:
+# 1. Local IP is public → puredns (bare-metal / dedicated)
+# 2. Cloud metadata endpoint responds → puredns (cloud VPS with 1:1 NAT)
+# 3. External IPv4 reachable and public → puredns (VPS behind transparent NAT)
+# 4. None of the above → dnsx (home/office/unknown)
+_can_use_puredns() {
+ local ip="$1"
+ _ip_is_public_ipv4 "$ip" && return 0
+ _is_cloud_vps && return 0
+ _get_external_ipv4 >/dev/null 2>&1 && return 0
+ return 1
}
# Initialize and cache DNS resolver selection (evaluate once per run).
@@ -1076,10 +1098,9 @@ init_dns_resolver() {
ip=$(_get_local_ip)
# Expose NAT detection and local IP for UI/logging.
- # Note: This is a best-effort heuristic based on the primary local IPv4 address.
RECON_LOCAL_IP="${ip:-}"
RECON_BEHIND_NAT="yes"
- if _ip_is_public_ipv4 "$ip"; then
+ if _can_use_puredns "$ip"; then
RECON_BEHIND_NAT="no"
fi
export RECON_LOCAL_IP RECON_BEHIND_NAT
@@ -1090,7 +1111,7 @@ init_dns_resolver() {
DNS_RESOLVER_SELECTED="$mode"
;;
auto|"")
- if _ip_is_public_ipv4 "$ip"; then
+ if [[ "$RECON_BEHIND_NAT" == "no" ]]; then
DNS_RESOLVER_SELECTED="puredns"
else
DNS_RESOLVER_SELECTED="dnsx"
@@ -1098,7 +1119,7 @@ init_dns_resolver() {
;;
*)
print_warnf "DNS_RESOLVER invalid: '%s' (use auto|puredns|dnsx). Defaulting to auto." "$mode"
- if _ip_is_public_ipv4 "$ip"; then
+ if [[ "$RECON_BEHIND_NAT" == "no" ]]; then
DNS_RESOLVER_SELECTED="puredns"
else
DNS_RESOLVER_SELECTED="dnsx"
@@ -1107,8 +1128,8 @@ init_dns_resolver() {
esac
export DNS_RESOLVER_SELECTED
- printf "[%s] DNS resolver selected: %s (DNS_RESOLVER=%s, local_ip=%s)\n" \
- "$(date +'%Y-%m-%d %H:%M:%S')" "$DNS_RESOLVER_SELECTED" "${DNS_RESOLVER:-auto}" "${ip:-}" >>"${LOGFILE:-/dev/null}"
+ printf "[%s] DNS resolver selected: %s (DNS_RESOLVER=%s, local_ip=%s, behind_nat=%s)\n" \
+ "$(date +'%Y-%m-%d %H:%M:%S')" "$DNS_RESOLVER_SELECTED" "${DNS_RESOLVER:-auto}" "${ip:-}" "$RECON_BEHIND_NAT" >>"${LOGFILE:-/dev/null}"
}
# Select DNS resolver based on DNS_RESOLVER config and NAT detection.
diff --git a/tests/unit/test_dns_resolver_auto.bats b/tests/unit/test_dns_resolver_auto.bats
index e5fa8c3..e499871 100644
--- a/tests/unit/test_dns_resolver_auto.bats
+++ b/tests/unit/test_dns_resolver_auto.bats
@@ -24,23 +24,39 @@ setup() {
done
}
-@test "_is_behind_nat returns true for RFC1918 and CGNAT and false for public IPv4" {
- _get_local_ip() { echo "192.168.1.10"; }
- run _is_behind_nat
+@test "_can_use_puredns returns true for public local IP" {
+ _is_cloud_vps() { return 1; }
+ _get_external_ipv4() { return 1; }
+ run _can_use_puredns "8.8.8.8"
[ "$status" -eq 0 ]
+}
- _get_local_ip() { echo "100.64.10.20"; }
- run _is_behind_nat
+@test "_can_use_puredns returns true for private IP on cloud VPS (metadata)" {
+ _is_cloud_vps() { return 0; }
+ _get_external_ipv4() { return 1; }
+ run _can_use_puredns "10.0.0.5"
[ "$status" -eq 0 ]
+}
- _get_local_ip() { echo "8.8.8.8"; }
- run _is_behind_nat
+@test "_can_use_puredns returns true for private IP with external IPv4 reachable" {
+ _is_cloud_vps() { return 1; }
+ _get_external_ipv4() { echo "203.0.113.50"; return 0; }
+ run _can_use_puredns "10.0.0.5"
+ [ "$status" -eq 0 ]
+}
+
+@test "_can_use_puredns returns false for private IP without cloud or external" {
+ _is_cloud_vps() { return 1; }
+ _get_external_ipv4() { return 1; }
+ run _can_use_puredns "192.168.1.10"
[ "$status" -ne 0 ]
}
@test "init_dns_resolver caches auto selection (does not re-evaluate on each _select_dns_resolver)" {
export DNS_RESOLVER="auto"
_get_local_ip() { echo "192.168.1.10"; }
+ _is_cloud_vps() { return 1; }
+ _get_external_ipv4() { return 1; }
init_dns_resolver
[ "$DNS_RESOLVER_SELECTED" = "dnsx" ]
@@ -53,6 +69,8 @@ setup() {
@test "DNS_RESOLVER override forces puredns/dnsx regardless of network" {
_get_local_ip() { echo "192.168.1.10"; }
+ _is_cloud_vps() { return 1; }
+ _get_external_ipv4() { return 1; }
export DNS_RESOLVER="puredns"
init_dns_resolver