Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions csharp/src/security/CWE-350/ReverseDnsConditionalBypass.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>

<overview>
<p>
Host names returned by a reverse DNS lookup of an IP address are controlled by whoever controls the DNS (PTR)
records for that address. An attacker can make their own IP address resolve to a trusted host name, so a condition
that compares a reverse DNS host name with an expected value must not decide whether a sensitive action (such as
logging a user in) is performed.
</p>
<p>
This query extends <code>cs/user-controlled-bypass</code> with reverse DNS sources it does not model:
<code>Dns.GetHostEntry(IPAddress)</code> and <code>Dns.GetHostEntryAsync(IPAddress)</code>. It also models
<code>Dns.GetHostByAddress</code>, which overlaps with the standard query for constant comparisons, and follows
comparisons of the host name against non-constant values, such as a configured host name.
</p>
</overview>

<recommendation>
<p>
Do not use reverse DNS to make security decisions. Authenticate the peer with a cryptographic mechanism (for
example TLS client certificates), or, if network location must be used, compare the peer IP address against an
allow-list of addresses.
</p>
</recommendation>

<example>
<p>
In the following example, the user is logged in only when the reverse DNS host name of the peer matches a trusted
host name. An attacker who controls the PTR record of their address can bypass this check.
</p>

<sample language="csharp"><![CDATA[
IPHostEntry hostInfo = Dns.GetHostEntry(peerAddress);
if (hostInfo.HostName.Equals("trusted.example.com"))
{
Login(user, password);
}
]]></sample>

<p>
Instead, base the decision on the peer address itself:
</p>

<sample language="csharp"><![CDATA[
if (AllowedAddresses.Contains(peerAddress))
{
Login(user, password);
}
]]></sample>
</example>

<references>
<li>
MITRE: <a href="https://cwe.mitre.org/data/definitions/350.html">CWE-350: Reliance on Reverse DNS Resolution for a Security-Critical Action</a>.
</li>
<li>
MITRE: <a href="https://cwe.mitre.org/data/definitions/807.html">CWE-807: Reliance on Untrusted Inputs in a Security Decision</a>.
</li>
<li>
MITRE: <a href="https://cwe.mitre.org/data/definitions/247.html">CWE-247: Reliance on DNS Lookups in a Security Decision</a>.
</li>
</references>
</qhelp>
27 changes: 27 additions & 0 deletions csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @name Sensitive action guarded by reverse DNS lookup
* @description Deciding whether to perform a sensitive action based on the host name returned
* by a reverse DNS lookup allows an attacker who controls the DNS (PTR) record of
* their IP address to bypass the check.
* @kind path-problem
* @problem.severity error
* @security-severity 7.5
* @precision high
* @id githubsecuritylab/cs/reverse-dns-user-controlled-bypass
* @tags security
* experimental
* external/cwe/cwe-350
* external/cwe/cwe-807
* external/cwe/cwe-247
*/

import csharp
import semmle.code.csharp.security.dataflow.ConditionalBypassQuery as ConditionalBypassQuery
import ReverseDnsConditionalBypassQuery
import ReverseDnsConditionalBypass::PathGraph

from ReverseDnsConditionalBypass::PathNode source, ReverseDnsConditionalBypass::PathNode sink
where ReverseDnsConditionalBypass::flowPath(source, sink)
select sink.getNode(), source, sink, "This condition guards a sensitive $@, but a $@ controls it.",
sink.getNode().(ConditionalBypassQuery::Sink).getSensitiveMethodCall(), "action",
source.getNode(), "reverse DNS host name"
73 changes: 73 additions & 0 deletions csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Provides a taint-tracking configuration for reasoning about sensitive actions that are
* guarded by a condition on the result of a reverse DNS lookup (CWE-350).
*
* This reuses the sinks and sanitizers of `cs/user-controlled-bypass`, but adds reverse DNS
* sources that are not modeled there (`Dns.GetHostEntry(IPAddress)` and
* `Dns.GetHostEntryAsync(IPAddress)`). It also includes `Dns.GetHostByAddress`, whose constant
* comparison coverage overlaps with the stock query, to detect non-constant host-name comparisons.
*/

import csharp
private import semmle.code.csharp.commons.ComparisonTest
private import semmle.code.csharp.frameworks.system.Net
private import semmle.code.csharp.security.dataflow.ConditionalBypassQuery as ConditionalBypassQuery

/**
* A call to a `System.Net.Dns` method that performs a reverse DNS lookup of an IP address.
* The host names in the resulting `IPHostEntry` are controlled by whoever controls the PTR
* record of that address, so they must not be trusted for security decisions.
*/
class ReverseDnsLookupCall extends MethodCall {
ReverseDnsLookupCall() {
exists(SystemNetDnsClass dns, Method m | m = this.getTarget() and m.getDeclaringType() = dns |
// `GetHostByAddress(string)` and `GetHostByAddress(IPAddress)` are always reverse lookups.
m = dns.getGetHostByAddressMethod()
or
// `GetHostEntry(IPAddress)`, `GetHostEntryAsync(IPAddress)` and their overloads.
m.hasName(["GetHostEntry", "GetHostEntryAsync"]) and
m.getParameter(0).getType().(Class).hasFullyQualifiedName("System.Net", "IPAddress")
)
}
}

/** A reverse DNS lookup result, which may be controlled by an attacker. */
class ReverseDnsSource extends DataFlow::ExprNode {
ReverseDnsSource() { this.getExpr() instanceof ReverseDnsLookupCall }
}

/**
* A taint-tracking configuration for sensitive actions guarded by a condition on
* the result of a reverse DNS lookup.
*/
private module ReverseDnsConditionalBypassConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ReverseDnsSource }

predicate isSink(DataFlow::Node sink) { sink instanceof ConditionalBypassQuery::Sink }

predicate isBarrier(DataFlow::Node node) { node instanceof ConditionalBypassQuery::Sanitizer }

/**
* Holds if `pred` is an operand of a comparison `succ`. The default taint steps only
* propagate through a comparison when the other operand is a constant, but any comparison
* of a reverse DNS host name (for example against an allow-list held in a variable or a
* field) is a decision based on reverse DNS.
*/
predicate isAdditionalFlowStep(DataFlow::Node pred, DataFlow::Node succ) {
exists(ComparisonTest ct |
ct.getExpr() = succ.asExpr() and
ct.getAnArgument() = pred.asExpr()
)
}

predicate observeDiffInformedIncrementalMode() { any() }

Location getASelectedSinkLocation(DataFlow::Node sink) {
result = sink.getLocation()
or
result = sink.(ConditionalBypassQuery::Sink).getSensitiveMethodCall().getLocation()
}
}

/** Taint-tracking flow for sensitive actions guarded by a reverse DNS lookup result. */
module ReverseDnsConditionalBypass = TaintTracking::Global<ReverseDnsConditionalBypassConfig>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Net;
using System.Threading.Tasks;

public class ReverseDnsAccessControl
{
private const string TrustedHost = "www.domain.nonexistenttld";

// BAD: reverse DNS of the peer address (GetHostEntry(IPAddress)) decides whether to log in.
public void GetHostEntryBad(IPAddress peer, string user, string password)
{
IPHostEntry hostInfo = Dns.GetHostEntry(peer); // source
if (hostInfo.HostName.Equals(TrustedHost)) // BAD: reported
{
Login(user, password);
}
}

// BAD: reverse DNS with a substring comparison on the host name.
public void GetHostEntryEndsWithBad(string user, string password)
{
IPAddress peer = IPAddress.Parse("127.0.0.1");
IPHostEntry hostInfo = Dns.GetHostEntry(peer); // source
if (hostInfo.HostName.EndsWith(".nonexistenttld")) // BAD: reported
{
Login(user, password);
}
}

// BAD: asynchronous reverse DNS lookup.
public async Task GetHostEntryAsyncBad(IPAddress peer, string user, string password)
{
IPHostEntry hostInfo = await Dns.GetHostEntryAsync(peer); // source
if (hostInfo.HostName == TrustedHost) // BAD: reported
{
Login(user, password);
}
}

// BAD: obsolete reverse DNS API (already covered by cs/user-controlled-bypass).
public void GetHostByAddressBad(IPAddress peer, string user, string password)
{
#pragma warning disable CS0618
IPHostEntry hostInfo = Dns.GetHostByAddress(peer); // source
#pragma warning restore CS0618
if (hostInfo.HostName == TrustedHost) // BAD: reported
{
Login(user, password);
}
}

// BAD: reverse DNS host name compared with a non-constant (configured) host name.
public void GetHostEntryConfiguredHostBad(IPAddress peer, string configuredHost, string user, string password)
{
IPHostEntry hostInfo = Dns.GetHostEntry(peer); // source
if (hostInfo.HostName == configuredHost) // BAD: reported
{
Login(user, password);
}
}

// GOOD: the reverse DNS host name is only logged, the sensitive action does not depend on it.
public void GetHostEntryLogOnlyGood(IPAddress peer, string user, string password)
{
IPHostEntry hostInfo = Dns.GetHostEntry(peer);
if (hostInfo.HostName.Equals(TrustedHost))
{
Console.WriteLine(hostInfo.HostName);
}
Login(user, password);
}

// GOOD: authorization decision is based on the peer IP address, not on reverse DNS.
public void IpAddressAllowListGood(IPAddress peer, string user, string password)
{
if (peer.Equals(IPAddress.Loopback))
{
Login(user, password);
}
}

// GOOD: forward DNS lookup of a constant host name, compared with the peer address.
public void ForwardLookupGood(IPAddress peer, string user, string password)
{
IPHostEntry trusted = Dns.GetHostEntry(TrustedHost);
if (Array.IndexOf(trusted.AddressList, peer) >= 0)
{
Login(user, password);
}
}

// GOOD: the same sensitive action happens on both branches.
public void BothBranchesGood(IPAddress peer, string user, string password)
{
IPHostEntry hostInfo = Dns.GetHostEntry(peer);
if (hostInfo.HostName == TrustedHost)
{
Login(user, password);
}
else
{
Login(user, password);
}
}

private static void Login(string user, string password) { }
}
Loading