App.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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;
  17. using System.IO;
  18. using System.Net;
  19. using System.Text.Json;
  20. using System.Threading.Tasks;
  21. using TechnitiumLibrary;
  22. using TechnitiumLibrary.Net;
  23. using TechnitiumLibrary.Net.Dns;
  24. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  25. namespace DropRequests
  26. {
  27. public sealed class App : IDnsApplication, IDnsRequestController
  28. {
  29. #region variables
  30. bool _enableBlocking;
  31. bool _dropMalformedRequests;
  32. NetworkAddress[] _allowedNetworks;
  33. NetworkAddress[] _blockedNetworks;
  34. BlockedQuestion[] _blockedQuestions;
  35. #endregion
  36. #region IDisposable
  37. public void Dispose()
  38. {
  39. //do nothing
  40. }
  41. #endregion
  42. #region public
  43. public async Task InitializeAsync(IDnsServer dnsServer, string config)
  44. {
  45. using JsonDocument jsonDocument = JsonDocument.Parse(config);
  46. JsonElement jsonConfig = jsonDocument.RootElement;
  47. _enableBlocking = jsonConfig.GetProperty("enableBlocking").GetBoolean();
  48. if (jsonConfig.TryGetProperty("dropMalformedRequests", out JsonElement jsonDropMalformedRequests))
  49. _dropMalformedRequests = jsonDropMalformedRequests.GetBoolean();
  50. else
  51. _dropMalformedRequests = false;
  52. if (jsonConfig.TryReadArray("allowedNetworks", NetworkAddress.Parse, out NetworkAddress[] allowedNetworks))
  53. _allowedNetworks = allowedNetworks;
  54. else
  55. _allowedNetworks = Array.Empty<NetworkAddress>();
  56. if (jsonConfig.TryReadArray("blockedNetworks", NetworkAddress.Parse, out NetworkAddress[] blockedNetworks))
  57. _blockedNetworks = blockedNetworks;
  58. else
  59. _blockedNetworks = Array.Empty<NetworkAddress>();
  60. if (jsonConfig.TryReadArray("blockedQuestions", delegate (JsonElement blockedQuestion) { return new BlockedQuestion(blockedQuestion); }, out BlockedQuestion[] blockedQuestions))
  61. _blockedQuestions = blockedQuestions;
  62. else
  63. _blockedQuestions = Array.Empty<BlockedQuestion>();
  64. if (!jsonConfig.TryGetProperty("dropMalformedRequests", out _))
  65. {
  66. config = config.Replace("\"allowedNetworks\"", "\"dropMalformedRequests\": false,\r\n \"allowedNetworks\"");
  67. await File.WriteAllTextAsync(Path.Combine(dnsServer.ApplicationFolder, "dnsApp.config"), config);
  68. }
  69. }
  70. public Task<DnsRequestControllerAction> GetRequestActionAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol)
  71. {
  72. if (!_enableBlocking)
  73. return Task.FromResult(DnsRequestControllerAction.Allow);
  74. if (_dropMalformedRequests && (request.ParsingException is not null))
  75. return Task.FromResult(DnsRequestControllerAction.DropSilently);
  76. IPAddress remoteIp = remoteEP.Address;
  77. foreach (NetworkAddress allowedNetwork in _allowedNetworks)
  78. {
  79. if (allowedNetwork.Contains(remoteIp))
  80. return Task.FromResult(DnsRequestControllerAction.Allow);
  81. }
  82. foreach (NetworkAddress blockedNetwork in _blockedNetworks)
  83. {
  84. if (blockedNetwork.Contains(remoteIp))
  85. return Task.FromResult(DnsRequestControllerAction.DropSilently);
  86. }
  87. if (request.Question.Count != 1)
  88. return Task.FromResult(DnsRequestControllerAction.DropSilently);
  89. DnsQuestionRecord requestQuestion = request.Question[0];
  90. foreach (BlockedQuestion blockedQuestion in _blockedQuestions)
  91. {
  92. if (blockedQuestion.Matches(requestQuestion))
  93. return Task.FromResult(DnsRequestControllerAction.DropSilently);
  94. }
  95. return Task.FromResult(DnsRequestControllerAction.Allow);
  96. }
  97. #endregion
  98. #region properties
  99. public string Description
  100. { get { return "Drops incoming DNS requests that match list of blocked networks or blocked questions."; } }
  101. #endregion
  102. class BlockedQuestion
  103. {
  104. #region variables
  105. readonly string _name;
  106. readonly bool _blockZone;
  107. readonly DnsResourceRecordType _type;
  108. #endregion
  109. #region constructor
  110. public BlockedQuestion(JsonElement jsonQuestion)
  111. {
  112. if (jsonQuestion.TryGetProperty("name", out JsonElement jsonName))
  113. _name = jsonName.GetString().TrimEnd('.');
  114. if (jsonQuestion.TryGetProperty("blockZone", out JsonElement jsonBlockZone))
  115. _blockZone = jsonBlockZone.GetBoolean();
  116. if (jsonQuestion.TryGetProperty("type", out JsonElement jsonType) && Enum.TryParse(jsonType.GetString(), true, out DnsResourceRecordType type))
  117. _type = type;
  118. else
  119. _type = DnsResourceRecordType.Unknown;
  120. }
  121. #endregion
  122. #region public
  123. public bool Matches(DnsQuestionRecord question)
  124. {
  125. if (_name is not null)
  126. {
  127. if (_blockZone)
  128. {
  129. if ((_name.Length > 0) && !_name.Equals(question.Name, StringComparison.OrdinalIgnoreCase) && !question.Name.EndsWith("." + _name, StringComparison.OrdinalIgnoreCase))
  130. return false;
  131. }
  132. else
  133. {
  134. if (!_name.Equals(question.Name, StringComparison.OrdinalIgnoreCase))
  135. return false;
  136. }
  137. }
  138. if ((_type != DnsResourceRecordType.Unknown) && (_type != question.Type))
  139. return false;
  140. return true;
  141. }
  142. #endregion
  143. }
  144. }
  145. }