Spell

sQflood by Spell

Apr 24th, 2013
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.21 KB | None | 0 0
  1. /*
  2. *
  3. --------------------------------------------------------------------------------
  4.                 sQflood: PROTECTION for Query flood.
  5. --------------------------------------------------------------------------------
  6. *
  7. *
  8. *   CREDITS:
  9. *       *- Spell (script)
  10. *       *- JaTochNietDan (plugin exec)
  11. *       *- Y_Less (fixes2)
  12. *
  13. *           RESPECT CREDITS!!!!
  14. *           RESPETA CREDITOS!!!!
  15. */
  16.  
  17. #include <a_samp>
  18. #include <fixes2>
  19.  
  20. #define _sQflood_version    (1.0)
  21.  
  22. #define MAX_INDEX_IP                (150)//EN: If you have too many users, increase this value | ES: si tienes mucha gente, aumentá este valor
  23. #define MAX_QUERIES_FOR_BLOCK       (100)//Dont change
  24. #define isnull(%1)                  ((%1[0] == 0) || (%1[0] == 1 && %1[1] == 0))
  25.  
  26. #define LANG_EN     (0)
  27. #define LANG_ES     (1)
  28.  
  29. // ==> Languaje
  30. #define lang LANG_EN // Options: LANG_ES | LANG_EN
  31.  
  32. native exec(cmd[]);
  33.  
  34. new
  35.     Name_IP[MAX_INDEX_IP][28],
  36.     Last_query[MAX_INDEX_IP],
  37.     Warn_IP[MAX_INDEX_IP];
  38.  
  39. public OnFilterScriptInit()
  40. {
  41.     SetTimer("StartLogQueries", 5000,  false);
  42.     SetTimer("ClearOldQueries", 20000, true);
  43.     printf("\n\n\t========================================================\n");
  44.     #if lang == LANG_EN
  45.     printf("\t  -- sQflood v(%.1f) (anti queryflood) by Spell loaded! --",   _sQflood_version);
  46.     #elseif lang == LANG_ES
  47.     printf("\t  -- sQflood v(%.1f) (anti queryflood) por Spell cargado! --", _sQflood_version);
  48.     #endif
  49.     printf("\n\t========================================================\n\n");
  50.    
  51.    
  52.     // ==-> Speed test
  53.     /*
  54.    
  55.     new
  56.         t = GetTickCount(),
  57.         IP[40];
  58.     for(new i; i < 4000; i++)
  59.     {
  60.         format(IP, 40, "[query:x] from %d.%d.%d.%d", random(500), random(500), random(500), random(500));
  61.         printf(IP);
  62.         CallRemoteFunction("OnServerMessage", "s", IP);
  63.     }
  64.     new
  65.         Float: Time_passed = float(GetTickCount()-t) / 4000;
  66.        
  67.     printf("Time for executing OnServerMessage: [1 time: %f | 4000 times: %d]", Time_passed, GetTickCount()-t);
  68.    
  69.     */
  70.     return 1;
  71. }
  72.  
  73. public OnFilterScriptExit()
  74. {
  75.     printf("\n\n\t========================================================\n");
  76.     #if lang == LANG_EN
  77.     printf("\t  -- sQflood v(%.1f) (anti queryflood) by Spell unloaded --",   _sQflood_version);
  78.     #elseif lang == LANG_ES
  79.     printf("\t  -- sQflood v(%.1f) (anti queryflood) por Spell descargado --", _sQflood_version);
  80.     #endif
  81.     printf("\n\t========================================================\n\n");
  82.  
  83.     return 1;
  84. }
  85.  
  86.  
  87. //============================================================================//
  88. forward StartLogQueries();
  89. forward ClearOldQueries();
  90.  
  91. public StartLogQueries()
  92.     return SendRconCommand("logqueries 1");
  93.    
  94. public ClearOldQueries()
  95. {
  96.     for(new i; i < MAX_INDEX_IP; i++) if(!isnull(Name_IP[i]) && !Warn_IP[i])
  97.     {
  98.         if(GetTickCount() - Last_query[i] > 30000)
  99.         {
  100.             ClearIPData(i);
  101.             continue;
  102.         }
  103.     }
  104.     return 1;
  105. }
  106.  
  107. //============================================================================//
  108.  
  109. public OnServerMessage(const msg[])
  110. {
  111.     if(strfind(msg, "[query:") == 0)
  112.     {
  113.         new IP[27];
  114.        
  115.         strmid(IP, msg, 15, strlen(msg));
  116.        
  117.         if(!IsIPRegistered(IP))
  118.         {
  119.             //printf("[sQflood] IP %s is not registered, register ip %s..", IP, IP);
  120.             RegisterIP(IP);
  121.             return 1;
  122.         }
  123.  
  124.         new
  125.             i = GetIPIndex(IP);
  126.  
  127.         //printf("[sQflood] GetIPIndex(\"%s\") = (%d).", IP, i);
  128.         if((GetTickCount() - Last_query[i]) < 2000)
  129.         {
  130.             Warn_IP[i] ++;
  131.             if(Warn_IP[i] >= MAX_QUERIES_FOR_BLOCK)//100 queries in 2 sec? ..BAN!!
  132.             {
  133.                 BanIP(IP, true);
  134.                 ClearIPData(i);
  135.                 //SendRconCommand("logqueries 0");//Activa esto si queres testear que el FS funciona, y no tenes linux.
  136.                 //SetTimer("StartLogQueries", 5000,  false);
  137.                 return 1;
  138.             }
  139.         }
  140.         else
  141.         {
  142.             Warn_IP[i] = 0;
  143.             Last_query[i] = GetTickCount();
  144.         }
  145.     }
  146.     return 1;
  147. }
  148.  
  149. stock BanIP(IP[], player = false)
  150. {
  151.     if(strcmp(IP, "255.255.255.255") == 0) return 0;
  152.    
  153.     if(player)
  154.     {
  155.         new
  156.             Temp_IP[28];
  157.         for(new i; i < MAX_PLAYERS; i++) if(IsPlayerConnected(i) && !IsPlayerNPC(i))
  158.         {
  159.             GetPlayerIp(i, Temp_IP, 28);
  160.             if(strcmp(Temp_IP, IP) == 0)
  161.             {
  162.                 #if lang == LANG_EN
  163.                 SendClientMessage(i, -1, "You has been banned by {FFFF00}sQflood. {FFFFFF}Reason: {FF0000}[QUERY-FLOOD FROM YOUR IP]");
  164.                 #elseif lang == LANG_ES
  165.                 SendClientMessage(i, -1, "Fuiste baneado por {FFFF00}sQflood. {FFFFFF}Razón: {FF0000}[QUERY-FLOOD DETECTADO DESDE TU IP]");
  166.                 #endif
  167.                 BanEx(i, "Query flood");
  168.                
  169.             }
  170.         }
  171.     }
  172.    
  173.     new
  174.         banString[100],
  175.         File: Log = fopen("sQflood_log.txt", io_append),
  176.         Data[5];
  177.  
  178.     format(banString, sizeof(banString), "iptables -A INPUT -s %s -j DROP", IP);
  179.     exec(banString);
  180.    
  181.     getdate(Data[2], Data[1], Data[0]);
  182.     gettime(Data[3], Data[4]);
  183.    
  184.     #if lang == LANG_EN
  185.     format(banString, sizeof(banString), "[%02d/%02d/%d - %02d:%02d] -- Blocking attack from IP %s..\r\n", Data[0], Data[1], Data[2], Data[3], Data[4], IP);
  186.     printf("[sQflood] Blocked attack from IP %s ..", IP);
  187.     #elseif lang == LANG_ES
  188.     format(banString, sizeof(banString), "[%02d/%02d/%d - %02d:%02d] -- Bloqueado ataque de la IP %s..\r\n", Data[0], Data[1], Data[2], Data[3], Data[4], IP);
  189.     printf("[sQflood] Ataque bloqueado desde la IP %s ..", IP);
  190.     #endif
  191.     fwrite(Log, banString);
  192.     fclose(Log);
  193.    
  194.     return 1;
  195. }
  196.  
  197.  
  198. stock RegisterIP(IP[])
  199. {
  200.     for(new i; i < MAX_INDEX_IP; i++)
  201.     {
  202.         if(isnull(Name_IP[i]))
  203.         {
  204.             strcat(Name_IP[i], IP);
  205.             Last_query[i] = GetTickCount();
  206.             return 1;
  207.         }
  208.     }
  209.     return 0;
  210. }
  211.  
  212. stock IsIPRegistered(IP[])
  213. {
  214.     for(new i; i < MAX_INDEX_IP; i++)
  215.     {
  216.         if(!isnull(Name_IP[i]) && strcmp(Name_IP[i], IP) == 0)
  217.         {
  218.             return 1;
  219.         }
  220.     }
  221.     return 0;
  222. }
  223.  
  224. stock GetIPIndex(IP[])
  225. {
  226.     for(new i; i < MAX_INDEX_IP; i++)
  227.     {
  228.         if(strcmp(Name_IP[i], IP) == 0)
  229.         {
  230.             return i;
  231.         }
  232.     }
  233.     return -1;
  234. }
  235.  
  236. stock ClearIPData(index)
  237. {
  238.     if(0 <= index < MAX_INDEX_IP)
  239.     {
  240.         Name_IP[index][0] = 0;
  241.         Last_query[index] = 0;
  242.         Warn_IP[index] = 0;
  243.         return 1;
  244.     }
  245.     return 0;
  246. }
  247.  
  248. /*
  249. --------------------------------------------------------------------------------
  250.                 EOF
  251. --------------------------------------------------------------------------------
  252. */
Advertisement
Add Comment
Please, Sign In to add comment