Repository navigation
[New Query - githubsecuritylab/cs/reverse-dns-user-controlled-bypass] - Sensitive action guarded by a reverse DNS host name #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
felickz
wants to merge
3
commits into
main
Choose a base branch
from
copilot/add-reverse-dns-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
csharp/src/security/CWE-350/ReverseDnsConditionalBypass.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
27
csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
73
csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
127 changes: 127 additions & 0 deletions
127
csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsAccessControl.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| // BAD: reverse DNS host name is the receiver of Equals. | ||
| public void GetHostEntryEqualsConfiguredHostBad(IPAddress peer, string configuredHost, string user, string password) | ||
| { | ||
| IPHostEntry hostInfo = Dns.GetHostEntry(peer); // source | ||
| if (hostInfo.HostName.Equals(configuredHost)) // BAD: reported | ||
| { | ||
| Login(user, password); | ||
| } | ||
| } | ||
|
|
||
| // BAD: reverse DNS host name is the argument to Equals. | ||
| public void GetHostEntryConfiguredHostEqualsBad(IPAddress peer, string configuredHost, string user, string password) | ||
| { | ||
| IPHostEntry hostInfo = Dns.GetHostEntry(peer); // source | ||
| if (configuredHost.Equals(hostInfo.HostName)) // BAD: reported | ||
| { | ||
| Login(user, password); | ||
| } | ||
| } | ||
|
|
||
| private static void Login(string user, string password) { } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8624c4b. Both parameter-based
Equalsforms produce the expected alerts with the existing comparison step; the targeted test passes.