Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. /**
  2. # g++ -std=c++14 -pthread -lboost_system -lboost_filesystem BotBan.cpp -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/local/lib -lmysqlpp -lmysqlclient -Ofast -flto -march=native -o BotBan
  3. # # # # # # # # # # # # # # # # # # Bot Ban Programm (C++) # # # # # # # # # # # # # # # # # #
  4. # (c) 2016, AntiDot
  5. **/
  6.  
  7. #include <iostream>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <sstream>
  11. #include <vector>
  12. #include <ctime>
  13. #include <stdio.h>
  14. #include <iterator>
  15. #include <cstring>
  16. #include <time.h>
  17. #include <string.h>
  18. #include <fstream>
  19. #include <algorithm>
  20. #include <stdlib.h>
  21. #include <thread>
  22. #include <boost/filesystem.hpp>
  23. #include <mysql.h>
  24. #include <mysql++.h>
  25. #include <mysql/mysql.h>
  26.  
  27. using namespace std;
  28.  
  29. namespace fs = boost::filesystem;
  30.  
  31. // just going to input the general details and not the port numbers
  32. struct connection_details
  33. {
  34.     char *server;
  35.     char *user;
  36.     char *password;
  37.     char *database;
  38. };
  39.  
  40. MYSQL* mysql_connection_setup(struct connection_details mysql_details)
  41. {
  42.      // first of all create a mysql instance and initialize the variables within
  43.     MYSQL *connection = mysql_init(NULL);
  44.  
  45.     // connect to the database with the details attached.
  46.     if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
  47.       printf("Conection error : %s\n", mysql_error(connection));
  48.       exit(1);
  49.     }
  50.     return connection;
  51. }
  52.  
  53. MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
  54. {
  55.    // send the query to the database
  56.    if (mysql_query(connection, sql_query))
  57.    {
  58.       printf("MySQL query error : %s\n", mysql_error(connection));
  59.       exit(1);
  60.    }
  61.  
  62.    return mysql_use_result(connection);
  63. }
  64.  
  65. // Split
  66.  
  67. vector<string> &split(const string &s, char delim, vector<string> &elems) {
  68.     stringstream ss(s);
  69.     string item;
  70.     while (getline(ss, item, delim)) {
  71.         elems.push_back(item);
  72.     }
  73.     return elems;
  74. }
  75.  
  76.  
  77. vector<string> split(const string &s, char delim) {
  78.     vector<string> elems;
  79.     split(s, delim, elems);
  80.     return elems;
  81. }
  82.  
  83. // Vars
  84.  
  85. vector<string> banlist;
  86. vector<string> whitelist = split("151.80.110.115 94.23.163.192 127.0.0.1 0.0.0.0", ' ');
  87. string fban;
  88. string fbots;
  89. string flow;
  90. size_t gfound;
  91. string orgflow;
  92. size_t orgfound;
  93. string orgflow1;
  94. size_t orgfound1;
  95.  
  96. // Date
  97.  
  98. string datan() {
  99.   time_t rawtime;
  100.   struct tm * timeinfo;
  101.   char buffer [80];
  102.  
  103.   time ( &rawtime );
  104.   timeinfo = localtime ( &rawtime );
  105.  
  106.   strftime (buffer,80,"%H:%M:%S",timeinfo);
  107.   return buffer;
  108. }
  109.  
  110. // Write Open File
  111.  
  112. void WOpenFile(string name1, string atr1, string data1) {
  113.   const char* name = name1.c_str();
  114.   const char* atr = atr1.c_str();
  115.   const char* data = data1.c_str();
  116.   FILE * file = fopen(name, atr);
  117.   if (file)
  118.     {
  119.         fputs(data, file);
  120.     }
  121.     else
  122.     cout << "File error!" << endl;
  123.     fclose(file);
  124. }
  125.  
  126. // Read Open File
  127.  
  128. string ROpenFile(string name1) {
  129.   const char* name = name1.c_str();
  130.   FILE * file = fopen(name, "r");
  131.   if (file)
  132.     {
  133.         char buf[1000];
  134.         string result;
  135.         while (fgets(buf,1000, file)!=NULL) {
  136.           string buf1 = string(buf);
  137.           result = result+buf1;
  138.         }
  139.         fclose(file);
  140.         return result;
  141.     }
  142.     else
  143.     cout << "File error!" << endl;
  144.     fclose(file);
  145.     return 0;
  146. }
  147.  
  148.  
  149. // Ban
  150.  
  151. string Ban(string ip) {
  152.   bool found3;
  153.   bool found2;
  154.   found3 = ( find(whitelist.begin(), whitelist.end(), ip) == whitelist.end() ? false : true );
  155.   found2 = ( find(banlist.begin(), banlist.end(), ip) == banlist.end() ? false : true );
  156.   if (found2 == false and found3 == false) {
  157.     banlist.push_back(ip);
  158.     string t = "["+datan()+"] [\x1b[31mBot\x1b[0m] -> Забанен "+ip+"!";
  159.     string syss = "ipset -A dropips "+ip;
  160.     system(syss.c_str());
  161.     return t;
  162.   } else {
  163.     return "No";
  164.   }
  165. }
  166.  
  167. // BAN thread
  168.  
  169. void Thrd() {
  170.   vector<string> bans;
  171.   string tobanstr;
  172.   string line;
  173.   int sll = 0;
  174.   while (true) {
  175.  
  176.    MYSQL *conn;
  177.    MYSQL_RES *res;
  178.    MYSQL_ROW row;
  179.  
  180.    struct connection_details mysqlD;
  181.    mysqlD.server = (char *)"localhost";
  182.    mysqlD.user = (char *)"root";
  183.    mysqlD.password = (char *)"rly";
  184.    mysqlD.database = (char *)"lol";
  185.  
  186.    conn = mysql_connection_setup(mysqlD);
  187.  
  188.    res = mysql_perform_query(conn, (char *)"SELECT * FROM `Blacklist`");
  189.    while ((row = mysql_fetch_row(res)) !=NULL) {
  190.     tobanstr = Ban(row[1]);
  191.     if (tobanstr != "No") {
  192.       cout << tobanstr << endl;
  193.     }
  194.     bans.clear();
  195.     struct timespec tw = {0,000500000};
  196.     struct timespec tr;
  197.     nanosleep (&tw, &tr);
  198.    }
  199.  
  200.    mysql_free_result(res);
  201.    mysql_close(conn);
  202.    
  203.    struct timespec tw1 = {sll,500000000};
  204.    struct timespec tr1;
  205.    nanosleep (&tw1, &tr1);
  206.   }
  207.  
  208. }
  209.  
  210. // MAIN
  211.  
  212. int main()
  213. {
  214.     string start = "["+datan()+"] [\x1b[34mBot\x1b[0m] -> Старт Ban Bot v.0.1!";
  215.   cout << start << endl;
  216.   string mes;
  217.   int ssize;
  218.   string s;
  219.   stringstream out;
  220.   thread Ban(Thrd);
  221.  
  222.     while (true) {
  223.  
  224.     if (banlist.size() > 0) {
  225.       ssize = (int)banlist.size();
  226.       out << ssize;
  227.       s = out.str();
  228.       mes = "["+datan()+"] [\x1b[34mWAF\x1b[0m] -> Забанено всего "+s+" ботов!";
  229.       cout << mes << endl;
  230.     }
  231.  
  232.     out.str("");
  233.     banlist.clear();
  234.  
  235.     struct timespec tw = {20,000000000};
  236.     struct timespec tr;
  237.     nanosleep (&tw, &tr);
  238.  
  239.     }
  240.     return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement