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.
 
 
 
 

78 lines
1.8 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // Import "hosts" file as a black list
  3. // Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net>
  4. // Started March 23rd, 2022
  5. //
  6. // We want to read a file formatted as /etc/hosts and add all names
  7. // listed in it to the "dns" table as a "block" (status=2).
  8. //////////////////////////////////////////////////////////////////////
  9. #include <iostream>
  10. #include <fstream>
  11. #include <stdexcept>
  12. #include "../strutil.h"
  13. #include "appbase.h"
  14. struct BlakcImpApp: public TrafficMonBaseApp {
  15. int lnct;
  16. int impct;
  17. int cli_pass;
  18. virtual void do_switch_arg(const char *sw, const std::string &val) {
  19. // handle switches on pass 0
  20. if(!cli_pass) TrafficMonBaseApp::do_switch_arg(sw, val);
  21. }
  22. virtual void do_arg(const char *fname) {
  23. int x;
  24. std::string s;
  25. std::ifstream hosts(fname);
  26. cppdb::statement q;
  27. if(!cli_pass) return; // postpone until pass 1 (2)
  28. if(!db.is_open())
  29. throw CLIerror("You must specify a configuration file first");
  30. q = db <<
  31. "INSERT INTO dns (name,status,note) VALUES (?,2,'import from black list')";
  32. while(std::getline(hosts, s)) {
  33. s = trim(s);
  34. if(s=="" || s[0]=='#') continue;
  35. for(x=0; x<s.size() && s[x]>' '; x++);
  36. s=trim(s.substr(x));
  37. if(s=="") continue;
  38. // TODO: more than one host on a line
  39. lnct++;
  40. q.reset();
  41. try {
  42. q << s << cppdb::exec;
  43. impct+=q.affected();
  44. } catch(const std::exception &e) {
  45. std::cerr << e.what() << std::endl;
  46. }
  47. }
  48. }
  49. int main() {
  50. int x;
  51. cli_pass = lnct = impct = 0;
  52. if(x=TrafficMonBaseApp::main()) return x;
  53. ++cli_pass;
  54. x=TrafficMonBaseApp::main();
  55. std::cout << "Records: " << lnct << " imports: " << impct << '\n';
  56. return x;
  57. }
  58. };
  59. MAIN(BlakcImpApp)