The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

140 lines
3.8 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // Dump Black Listed DNS entries
  3. // Written by Jonathan A. Foster <jon@jfpossibilities.com>
  4. // Started Ocotber 27th, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. //
  7. // Read the "dns" table and dump all black listed host names as
  8. // entries for a "hosts" file. This could also be easily done with a
  9. // script but I want to be able to use the same config file as every-
  10. // thing else and parsing in SH is clumsy at best.
  11. //////////////////////////////////////////////////////////////////////
  12. #include <string>
  13. #include <map>
  14. #include <iostream>
  15. #include <stdio.h>
  16. #include <libgen.h>
  17. #include "../strutil.h"
  18. #include "appbase.h"
  19. using namespace std;
  20. //////////////////////////////////////////////////////////////////////
  21. // Class to manage and test hoset names agains bad domains
  22. //////////////////////////////////////////////////////////////////////
  23. struct DomainList: public StringList {
  24. bool operator==(const std::string host) {
  25. DomainList::const_iterator i;
  26. int dl, hl = host.size();
  27. for(i=begin(); i!=end(); i++) {
  28. if(*i==host) return true;
  29. dl = i->size()+1;
  30. if(hl>dl && host.substr(hl-dl)=="."+*i) return true;
  31. }
  32. return false;
  33. }
  34. inline bool operator!=(const std::string host) { return !(*this==host); }
  35. };
  36. namespace cppdb {
  37. session &operator>>(cppdb::session &db, DomainList &doms) {
  38. cppdb::result qry;
  39. std::string s;
  40. doms.clear();
  41. qry = db << "SELECT name FROM dns_wild WHERE status=2";
  42. while(qry.next()) {
  43. qry >> s;
  44. doms.push_back(s);
  45. }
  46. return db;
  47. }
  48. } // cppdb
  49. //////////////////////////////////////////////////////////////////////
  50. // Connection Report Generator Application Class
  51. //////////////////////////////////////////////////////////////////////
  52. struct DNSblackList: BlackListBaseApp {
  53. bool all;
  54. unsigned do_switch(const char *arg) {
  55. if(*arg=='a' && !arg[1]) { all=1; return 0; }
  56. return BlackListBaseApp::do_switch(arg);
  57. }
  58. int help() {
  59. std::cerr << " FORMAT: " << basename(command_args[0]) << " -c {config} [-a] [-4 {address}] [-6 {address}]\n"
  60. << '\n'
  61. << "The config file must have a [Traffic Mon] section with the database\n"
  62. << "credentials in it. -4 & -6 set the addresses to pin blocked names to.\n"
  63. << "They default to the 'localhost' address in the respective family. Set\n"
  64. << "to '' to turn off output of that family. -a dumps all blocked host\n"
  65. << "names otherwise host names that are covered by a domain block will\n"
  66. << "not be shown." << std::endl;
  67. return ExitCode = 1;
  68. }
  69. int main() {
  70. DomainList baddoms;
  71. cppdb::result qry;
  72. string s;
  73. int x;
  74. /// SETUP & VALIDATE CLI ///
  75. all = false;
  76. if(x=BlackListBaseApp::main()) return x; // Parse CLI args, open conf & db
  77. if(ipv6!="" && ipv6.size()<8) ipv6+='\t'; // an extra \t to line up columns. :-)
  78. /// Load list of bad domains ///
  79. // These should be excluded from the list below since they should be
  80. // blocked by other means and the point of domain wide blocking is to
  81. // relieve the burden on the blocking tools (dnsmasq).
  82. if(!all) db >> baddoms;
  83. /// Query & load data ///
  84. qry = db <<
  85. "SELECT name "
  86. "FROM dns "
  87. "WHERE status=2 " // 2 = blocked... need this doc'd somewhere...
  88. "ORDER BY name";
  89. while(qry.next()) {
  90. qry >> s;
  91. if(all || baddoms!=s) { // exclude blocked domains
  92. if(ipv4!="") cout << ipv4 << '\t' << s << '\n';
  93. if(ipv6!="") cout << ipv6 << '\t' << s << '\n';
  94. }
  95. }
  96. return 0;
  97. }
  98. };
  99. //////////////////////////////////////////////////////////////////////
  100. // Lets run the report and dump it out
  101. //////////////////////////////////////////////////////////////////////
  102. MAIN(DNSblackList)