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

dns: detect a metadata service that exists but requires a token (#1050)

six2dez1fe734822119
diff --git a/modules/utils.sh b/modules/utils.sh
index c9d710a..29e6047 100644
--- a/modules/utils.sh
+++ b/modules/utils.sh
@@ -1398,7 +1398,27 @@ _ip_is_public_ipv4() {
# Returns 0 if running on a cloud VPS, 1 otherwise.
_is_cloud_vps() {
[[ "${DRY_RUN:-false}" == "true" ]] && return 1
- curl -sf --max-time 2 -o /dev/null http://169.254.169.254/ 2>/dev/null && return 0
+
+ # Probe for the PRESENCE of a metadata service, not for its willingness to
+ # serve an unauthenticated read (issue #1050).
+ #
+ # The previous `curl -sf` could not tell those apart. IMDSv2 has been the
+ # default on new EC2 instances since 2019 and answers an unauthenticated GET
+ # with 401; `-f` turns any >=400 status into exit 22, so a perfectly live
+ # metadata service was read as "no metadata service at all". Every modern
+ # AWS box was therefore misclassified as a home/NAT network — which is not
+ # cosmetic: _can_use_puredns() uses this to pick the resolver, so
+ # DNS_RESOLVER=auto silently downgraded puredns to dnsx on exactly the hosts
+ # puredns exists to be fast on.
+ #
+ # %{http_code} is '000' only when no HTTP response was received at all
+ # (connection refused, timeout, no route) — which is what a non-cloud host
+ # gives for link-local 169.254.169.254. Any real status (200, 401, 403, 404)
+ # proves something is listening there, and that is the whole question.
+ local _code
+ _code=$(curl -s --max-time 2 -o /dev/null -w '%{http_code}' \
+ http://169.254.169.254/ 2>/dev/null)
+ [[ -n "$_code" ]] && [[ "$_code" != "000" ]] && return 0
return 1
}
diff --git a/tests/unit/test_cloud_vps_detection.bats b/tests/unit/test_cloud_vps_detection.bats
new file mode 100644
index 0000000..12f3bbb
--- /dev/null
+++ b/tests/unit/test_cloud_vps_detection.bats
@@ -0,0 +1,113 @@
+#!/usr/bin/env bats
+#
+# Regression coverage for issue #1050.
+#
+# _is_cloud_vps() decides whether a metadata service exists at link-local
+# 169.254.169.254. _can_use_puredns() consumes that answer to pick the DNS
+# resolver, so a false negative is not cosmetic: DNS_RESOLVER=auto silently
+# downgrades puredns to dnsx on precisely the cloud hosts puredns is for.
+#
+# The old probe was `curl -sf`. IMDSv2 — the default on new EC2 instances since
+# 2019 — answers an unauthenticated GET with 401, and `-f` maps any >=400 status
+# to exit 22. A live metadata service was therefore read as no metadata service.
+#
+# These tests drive a stub `curl` so they assert the BRANCHING on HTTP status
+# rather than reaching the network, which unit-fast cannot do anyway.
+#
+# ASSERTION STYLE: `run` captures status, so each case asserts on $status
+# directly; the suite restores errexit via tests/helpers/common.bash.
+
+setup() {
+ source "$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)/../helpers/common.bash"
+ setup_recon_env
+
+ TEST_DIR="$(mktemp -d)"
+ export TEST_DIR
+ cd "$TEST_DIR"
+
+ # Stub curl: emulate a response by echoing $STUB_HTTP_CODE for -w '%{http_code}'.
+ # '000' is what real curl reports when no HTTP response arrived at all.
+ #
+ # It MUST honour -f, or these tests are decoration: the bug under test is
+ # precisely that -f maps a >=400 status to exit 22. A stub that ignored -f
+ # would keep reporting success for the old `curl -sf` probe, so reinstating
+ # the bug would leave every test below green. Verified by mutation, not
+ # assumed — see the note on the IMDSv2 case.
+ mkdir -p bin
+ cat > bin/curl <<'STUB'
+#!/usr/bin/env bash
+fail_on_error=0
+for _a in "$@"; do
+ case "$_a" in
+ -f | --fail) fail_on_error=1 ;;
+ -*f*) [[ "$_a" != --* ]] && fail_on_error=1 ;;
+ esac
+done
+code="${STUB_HTTP_CODE:-000}"
+printf '%s' "$code"
+if [[ "$fail_on_error" -eq 1 ]] && [[ "$code" =~ ^[45][0-9][0-9]$ ]]; then
+ exit 22
+fi
+[[ "$code" == "000" ]] && exit 7
+exit 0
+STUB
+ chmod +x bin/curl
+ export PATH="$TEST_DIR/bin:$PATH"
+
+ export DRY_RUN=false
+}
+
+teardown() {
+ cd /
+ rm -rf "$TEST_DIR"
+}
+
+@test "_is_cloud_vps detects AWS IMDSv2, which answers an unauthenticated GET with 401 (issue #1050)" {
+ # The exact case the issue reports. `curl -sf` returned exit 22 here and the
+ # instance was misclassified as a non-cloud NAT network.
+ export STUB_HTTP_CODE="401"
+ run _is_cloud_vps
+ [ "$status" -eq 0 ]
+}
+
+@test "_is_cloud_vps detects a metadata service that answers 403" {
+ export STUB_HTTP_CODE="403"
+ run _is_cloud_vps
+ [ "$status" -eq 0 ]
+}
+
+@test "_is_cloud_vps still detects a plain IMDSv1-style 200" {
+ export STUB_HTTP_CODE="200"
+ run _is_cloud_vps
+ [ "$status" -eq 0 ]
+}
+
+@test "_is_cloud_vps returns false when nothing answers at 169.254.169.254" {
+ # No HTTP response at all: the non-cloud case, which must stay false so a
+ # home/office network is never handed to puredns.
+ export STUB_HTTP_CODE="000"
+ run _is_cloud_vps
+ [ "$status" -ne 0 ]
+}
+
+@test "_is_cloud_vps returns false when curl produces no output" {
+ export STUB_HTTP_CODE=""
+ run _is_cloud_vps
+ [ "$status" -ne 0 ]
+}
+
+@test "_is_cloud_vps short-circuits to false under DRY_RUN" {
+ export DRY_RUN=true
+ export STUB_HTTP_CODE="200"
+ run _is_cloud_vps
+ [ "$status" -ne 0 ]
+}
+
+@test "_can_use_puredns now says yes for a private-IP EC2 box behind IMDSv2" {
+ # The end-to-end consequence of the fix: private IP + IMDSv2 401 is a cloud
+ # VPS with 1:1 NAT, which is safe for puredns. Before the fix this returned
+ # false and the run silently fell back to dnsx.
+ export STUB_HTTP_CODE="401"
+ run _can_use_puredns "10.0.0.5"
+ [ "$status" -eq 0 ]
+}