App.cs 6.7 KB

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