Skip to content

Commit 8ce259c

Browse files
fix(security): forward-verify PTR result in remote_client_authorized()
Use dns_get_record(DNS_A + DNS_AAAA) for forward verification to support dual-stack and round-robin DNS. Sanitize PTR hostname in log output. Use SECURITY logging category. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
1 parent 02acd9b commit 8ce259c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

remote_agent.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,30 @@ function remote_client_authorized() {
145145

146146
$client_name = gethostbyaddr($client_addr);
147147

148+
/* Forward-verify PTR result to prevent DNS spoofing */
149+
if ($client_name != $client_addr) {
150+
$forward_records = @dns_get_record($client_name, DNS_A + DNS_AAAA);
151+
$forward_match = false;
152+
153+
if (is_array($forward_records)) {
154+
foreach ($forward_records as $record) {
155+
$ip = isset($record['ip']) ? $record['ip'] : (isset($record['ipv6']) ? $record['ipv6'] : '');
156+
157+
if ($ip === $client_addr) {
158+
$forward_match = true;
159+
break;
160+
}
161+
}
162+
}
163+
164+
if (!$forward_match) {
165+
$safe_name = preg_replace('/[^a-zA-Z0-9.\-:]/', '', $client_name);
166+
cacti_log('WARNING: PTR record for ' . $client_addr . ' resolves to ' . $safe_name . ' but forward lookup does not match. Rejecting.', false, 'SECURITY');
167+
168+
return false;
169+
}
170+
}
171+
148172
if ($client_name == $client_addr) {
149173
cacti_log('NOTE: Unable to resolve hostname from address ' . $client_addr, false, 'WEBUI', POLLER_VERBOSITY_MEDIUM);
150174
}

0 commit comments

Comments
 (0)