Advertisement
Guest User

SafeCon 0.9

a guest
May 14th, 2011
2,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.04 KB | None | 0 0
  1. /*
  2.     SafeCon - Version 0.9
  3.     Provides different features to secure the RCON against attacks
  4.     and unwanted access
  5.     Currently:
  6.         Hardcoded password list
  7.         Name- and IP-Whitelists
  8.         Auto-Shutdown (quite useless atm)
  9.        
  10.     Made by Mauzen (msoll(at)web.de) - 05-11-2011
  11.     Do not remove this header
  12. */
  13.  
  14. #include <a_samp>
  15.  
  16. #define FILTERSCRIPT
  17.  
  18. #define USE_HARDCODED_PWS           (true)  // Use a changing PW list
  19. #define USE_NAME_WHITELIST          (false) // Only players with a listed name
  20.                                             // can login to the RCON
  21. #define USE_IP_WHITELIST            (false) // Only people with a listed IP
  22.                                             // can login to the RCON
  23. #define PW_CHANGE_INTERVAL          (120)   // Interval in seconds to change the PWs
  24. #define CLOSE_SERVER_ON_RISK        (false) // Auto-close the server on security risks
  25.  
  26. #define MAX_PW_LENGTH               (32)
  27. #define MIN_PW_LENGTH               (8)     // Shorter PWs will create security warnings
  28.  
  29.  
  30.  
  31. new pws[][MAX_PW_LENGTH] =              // Change this when using hardcoded
  32. {                                       // passwords
  33.     "PW_1",                             // Use as many different passwords
  34.     "PW_2",                             // as possible
  35.     "PW_3"
  36. };
  37.  
  38. new namewhitelist[][MAX_PLAYER_NAME] =  // Only the names in this list can login
  39. {                                       // as RCON admin, if USE_NAME_WHITELIST
  40.     "NAMES_HERE"                        // is activated
  41. };
  42.  
  43. new ipwhitelist[][16] =                 // Only the IPs in this list can login
  44. {                                       // as RCON admin, if USE_IP_WHITELIST
  45.     "127.0.0.1"                         // is activated
  46. };
  47.  
  48. new currenthcindex;
  49.  
  50.  
  51. public OnFilterScriptInit()
  52. {
  53.     print("\n--------------------------------------");
  54.     print(" SafeCon Server Protection V0.9");
  55.     print(" Made by Mauzen 5-11-2011");
  56.    
  57.     #if (USE_HARDCODED_PWS == true)
  58.         new hcpw = CheckHCPWSafety();
  59.         if (hcpw == -1) {
  60.             printf("[SafeCon] Using hardcoded passwords: Security checks passed.");
  61.         } else {
  62.             printf("[SafeCon] Using hardcoded passwords: WARNING: at least one password (%d) is not safe!", hcpw);
  63.             #if (CLOSE_SERVER_ON_RISK == true)
  64.                 print("[SafeCon] Security shutdown...");
  65.                 SendRconCommand("exit");
  66.             #endif
  67.         }
  68.         UpdateHCPWs();
  69.         SetTimer("UpdateHCPWs", PW_CHANGE_INTERVAL * 1000, 1);
  70.     #endif
  71.    
  72.     print("--------------------------------------\n");
  73.    
  74.     AntiDeAMX();
  75.    
  76.     return 1;
  77. }
  78.  
  79. public OnFilterScriptExit()
  80. {
  81.     #if (USE_HARDCODED_PWS == true)
  82.         printf("[SafeCon] WARNING: SafeCon unloaded. The current password index is: %d", currenthcindex);
  83.     #else
  84.         printf("[SafeCon] WARNING: SafeCon unloaded.");
  85.     #endif
  86.     return 1;
  87. }
  88.  
  89.  
  90. stock CheckHCPWSafety()
  91. {
  92.     for (new i = 0; i < sizeof(pws); i ++)
  93.     {
  94.         if (strlen(pws[i]) < MIN_PW_LENGTH) return i;
  95.     }
  96.     return -1;
  97. }
  98.  
  99. forward UpdateHCPWs();
  100. public UpdateHCPWs()
  101. {
  102.     currenthcindex = random(sizeof(pws));
  103.    
  104.     new chpwd[16 + MAX_PW_LENGTH];
  105.     format(chpwd, sizeof(chpwd), "rcon_password %s", pws[currenthcindex]);
  106.     SendRconCommand(chpwd);
  107. }
  108.  
  109. public OnPlayerCommandText(playerid, cmdtext[])
  110. {
  111.     #if (USE_HARDCODED_PWS == true)
  112.         if (strcmp("/rconpw", cmdtext, true) == 0)
  113.         {
  114.             new pwstring[48];
  115.             format(pwstring, sizeof(pwstring), "[SafeCon] The current password index is: %d", currenthcindex);
  116.             SendClientMessage(playerid, 0xFFFFFFFF, pwstring);
  117.             return 1;
  118.         }
  119.     #endif
  120.    
  121.     return 0;
  122. }
  123.  
  124. public OnRconCommand(cmd[])
  125. {
  126.     return 1;
  127. }
  128.  
  129. public OnRconLoginAttempt(ip[], password[], success)
  130. {
  131.     new playerid = GetPlayerIDbyIP(ip);
  132.     if (!success)
  133.     {
  134.         printf("[SafeCon] Bad RCON login attempt by %s (Player %d), PW: %s", ip, playerid, password);
  135.     } else {
  136.         new fail = USE_NAME_WHITELIST || USE_IP_WHITELIST;
  137.        
  138.         #if (USE_NAME_WHITELIST == true)
  139.             if(playerid != -1) {
  140.                 new name[MAX_PLAYER_NAME];
  141.                 GetPlayerName(playerid, name, MAX_PLAYER_NAME);
  142.                 for (new i = 0; i < sizeof(namewhitelist); i++)
  143.                 {
  144.                     if(!strcmp(name, namewhitelist[i], false)) fail = false;
  145.                 }
  146.             }
  147.         #endif
  148.        
  149.         #if (USE_IP_WHITELIST == true)
  150.             for (new i = 0; i < sizeof(ipwhitelist); i++)
  151.             {
  152.                 if(!strcmp(ip, ipwhitelist[i], false)) fail = false;
  153.             }
  154.         #endif
  155.        
  156.         if(!fail)
  157.         {
  158.             printf("[SafeCon] Succesful RCON login by %s (Player %d), PW: %s", ip, playerid, password);
  159.         } else
  160.         {
  161.             if(playerid != -1)
  162.             {
  163.                 SendClientMessage(playerid, 0xFFFFFFFF, "You are not allowed to login as RCON admin.");
  164.                 printf("[SafeCon] Succesful RCON login attempt by %s (Player %d), PW: %s BLOCKED BY WHITELIST", ip, playerid, password);
  165.                 Kick(playerid);
  166.             }
  167.         }
  168.     }
  169.     return 1;
  170. }
  171.  
  172. stock GetPlayerIDbyIP(const ip[])
  173. {
  174.     new pip[16];
  175.     for (new i = 0; i < GetMaxPlayers(); i ++)
  176.     {
  177.         if(!IsPlayerConnected(i)) continue;
  178.         GetPlayerIp(i, pip, 16);
  179.         if (!strcmp(ip, pip, true))
  180.         {
  181.             return i;
  182.         }
  183.     }
  184.     return -1;
  185. }
  186.  
  187. AntiDeAMX()
  188. {
  189.     new a[][] =
  190.     {
  191.             "Unarmed (Fist)",
  192.             "Brass K"
  193.     };
  194.     #pragma unused a
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement