App.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.ApplicationCommon;
  16. using System.Collections.Generic;
  17. using System.Net;
  18. using System.Text.Json;
  19. using System.Threading.Tasks;
  20. using TechnitiumLibrary;
  21. using TechnitiumLibrary.Net;
  22. using TechnitiumLibrary.Net.Dns;
  23. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  24. namespace DnsRebindingProtection
  25. {
  26. public sealed class App : IDnsApplication, IDnsPostProcessor
  27. {
  28. #region variables
  29. bool _enableProtection;
  30. HashSet<NetworkAddress> _privateNetworks;
  31. HashSet<string> _privateDomains;
  32. #endregion
  33. #region IDisposable
  34. public void Dispose()
  35. {
  36. // Nothing to dispose of.
  37. }
  38. #endregion
  39. #region private
  40. private static string GetParentZone(string domain)
  41. {
  42. int i = domain.IndexOf('.');
  43. if (i > -1)
  44. return domain[(i + 1)..];
  45. //dont return root zone
  46. return null;
  47. }
  48. private bool IsPrivateDomain(string domain)
  49. {
  50. domain = domain.ToLowerInvariant();
  51. do
  52. {
  53. if (_privateDomains.Contains(domain))
  54. return true;
  55. domain = GetParentZone(domain);
  56. }
  57. while (domain is not null);
  58. return false;
  59. }
  60. private bool IsRebindingAttempt(DnsResourceRecord record)
  61. {
  62. IPAddress address;
  63. switch (record.Type)
  64. {
  65. case DnsResourceRecordType.A:
  66. if (IsPrivateDomain(record.Name))
  67. return false;
  68. address = (record.RDATA as DnsARecordData).Address;
  69. break;
  70. case DnsResourceRecordType.AAAA:
  71. if (IsPrivateDomain(record.Name))
  72. return false;
  73. address = (record.RDATA as DnsAAAARecordData).Address;
  74. break;
  75. default:
  76. return false;
  77. }
  78. foreach (NetworkAddress networkAddress in _privateNetworks)
  79. {
  80. if (networkAddress.Contains(address))
  81. return true;
  82. }
  83. return false;
  84. }
  85. private bool TryDetectRebinding(IReadOnlyList<DnsResourceRecord> answer, out List<DnsResourceRecord> protectedAnswer)
  86. {
  87. for (int i = 0; i < answer.Count; i++)
  88. {
  89. DnsResourceRecord record = answer[i];
  90. if (IsRebindingAttempt(record))
  91. {
  92. //rebinding attempt detected!
  93. //prepare protected answer
  94. protectedAnswer = new List<DnsResourceRecord>(answer.Count);
  95. //copy passed records
  96. for (int j = 0; j < i; j++)
  97. protectedAnswer.Add(answer[j]);
  98. //copy remaining records with check
  99. for (int j = i + 1; j < answer.Count; j++)
  100. {
  101. record = answer[j];
  102. if (!IsRebindingAttempt(record))
  103. protectedAnswer.Add(record);
  104. }
  105. return true;
  106. }
  107. }
  108. protectedAnswer = null;
  109. return false;
  110. }
  111. #endregion
  112. #region public
  113. public Task InitializeAsync(IDnsServer dnsServer, string config)
  114. {
  115. using JsonDocument jsonDocument = JsonDocument.Parse(config);
  116. JsonElement jsonConfig = jsonDocument.RootElement;
  117. _enableProtection = jsonConfig.GetPropertyValue("enableProtection", true);
  118. _privateNetworks = new HashSet<NetworkAddress>(jsonConfig.ReadArray("privateNetworks", NetworkAddress.Parse));
  119. _privateDomains = new HashSet<string>(jsonConfig.ReadArray("privateDomains"));
  120. return Task.CompletedTask;
  121. }
  122. public Task<DnsDatagram> PostProcessAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  123. {
  124. // Do not filter authoritative responses. Because in this case any rebinding is intentional.
  125. if (!_enableProtection || response.AuthoritativeAnswer)
  126. return Task.FromResult(response);
  127. if (TryDetectRebinding(response.Answer, out List<DnsResourceRecord> protectedAnswer))
  128. return Task.FromResult(response.Clone(protectedAnswer));
  129. return Task.FromResult(response);
  130. }
  131. #endregion
  132. #region properties
  133. public string Description
  134. { get { return "Protects from DNS rebinding attacks using configured private domains and networks."; } }
  135. #endregion
  136. }
  137. }