Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1.      * User name is not case sensitive any more.
  2.      * @param user
  3.      * @param password
  4.      * @param client
  5.      * @return
  6.      */
  7.     public boolean loginValid(String user, String password, L2LoginClient client)// throws HackingException
  8.     {
  9.         boolean ok = false;
  10.         InetAddress address = client.getConnection().getInetAddress();
  11.        
  12.         // player disconnected meanwhile
  13.         if ((address == null) || (user == null))
  14.         {
  15.             return false;
  16.         }
  17.        
  18.         try
  19.         {
  20.             MessageDigest md = MessageDigest.getInstance("SHA");
  21.             byte[] raw = password.getBytes("UTF-8");
  22.             byte[] hash = md.digest(raw);
  23.            
  24.             //Après avoir appliquer le SHA-1, on applique le SHA512 - CHANGEMENT ICI
  25.             md = MessageDigest.getInstance("SHA-512");
  26.             byte[] hash_2 = md.digest(hash);
  27.            
  28.             byte[] expected = null;
  29.             int access = 0;
  30.             int lastServer = 1;
  31.             List<InetAddress> ipWhiteList = new ArrayList<>();
  32.             List<InetAddress> ipBlackList = new ArrayList<>();
  33.             try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  34.                 PreparedStatement ps = con.prepareStatement(USER_INFO_SELECT))
  35.             {
  36.                 ps.setString(1, Long.toString(System.currentTimeMillis()));
  37.                 ps.setString(2, user);
  38.                 try (ResultSet rset = ps.executeQuery())
  39.                 {
  40.                     if (rset.next())
  41.                     {
  42.                         exp
  43.                    
  44.                         expected = Base64.decode(rset.getString("password"));
  45.                         access = rset.getInt("accessLevel");
  46.                         lastServer = rset.getInt("lastServer");
  47.                         if (lastServer <= 0)
  48.                         {
  49.                             lastServer = 1; // minServerId is 1 in Interlude
  50.                         }
  51.                         if (Config.DEBUG)
  52.                         {
  53.                             _log.fine("account exists");
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.            
  59.            
  60.            
  61.             try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  62.                 PreparedStatement ps = con.prepareStatement(ACCOUNT_IPAUTH_SELECT))
  63.             {
  64.                 ps.setString(1, user);
  65.                 try (ResultSet rset = ps.executeQuery())
  66.                 {
  67.                     String ip, type;
  68.                     while (rset.next())
  69.                     {
  70.                         ip = rset.getString("ip");
  71.                         type = rset.getString("type");
  72.                        
  73.                         if (!isValidIPAddress(ip))
  74.                         {
  75.                             continue;
  76.                         }
  77.                         else if (type.equals("allow"))
  78.                         {
  79.                             ipWhiteList.add(InetAddress.getByName(ip));
  80.                         }
  81.                         else if (type.equals("deny"))
  82.                         {
  83.                             ipBlackList.add(InetAddress.getByName(ip));
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88.            
  89.             // if account doesn't exists
  90.             if (expected == null)
  91.             {
  92.                 if (Config.AUTO_CREATE_ACCOUNTS)
  93.                 {
  94.                     if ((user.length() >= 2) && (user.length() <= 14))
  95.                     {
  96.                         try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  97.                             PreparedStatement ps = con.prepareStatement(AUTOCREATE_ACCOUNTS_INSERT))
  98.                         {
  99.                             ps.setString(1, user);
  100.                             ps.setString(2, Base64.encodeBytes(hash));
  101.                             ps.setLong(3, System.currentTimeMillis());
  102.                             ps.setInt(4, 0);
  103.                             ps.setString(5, address.getHostAddress());
  104.                             ps.execute();
  105.                         }
  106.                        
  107.                         if (Config.LOG_LOGIN_CONTROLLER)
  108.                         {
  109.                             Log.add("'" + user + "' " + address.getHostAddress() + " - OK : AccountCreate", "loginlog");
  110.                         }
  111.                        
  112.                         _log.info("Created new account for " + user);
  113.                         return true;
  114.                        
  115.                     }
  116.                     if (Config.LOG_LOGIN_CONTROLLER)
  117.                     {
  118.                         Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : ErrCreatingACC", "loginlog");
  119.                     }
  120.                    
  121.                     _log.warning("Invalid username creation/use attempt: " + user);
  122.                 }
  123.                 else
  124.                 {
  125.                     if (Config.LOG_LOGIN_CONTROLLER)
  126.                     {
  127.                         Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : AccountMissing", "loginlog");
  128.                     }
  129.                    
  130.                     _log.warning("Account missing for user " + user);
  131.                     FailedLoginAttempt failedAttempt = _hackProtection.get(address);
  132.                     int failedCount;
  133.                     if (failedAttempt == null)
  134.                     {
  135.                         _hackProtection.put(address, new FailedLoginAttempt(address, password));
  136.                         failedCount = 1;
  137.                     }
  138.                     else
  139.                     {
  140.                         failedAttempt.increaseCounter();
  141.                         failedCount = failedAttempt.getCount();
  142.                     }
  143.                    
  144.                     if (failedCount >= Config.LOGIN_TRY_BEFORE_BAN)
  145.                     {
  146.                         _log.info("Banning '" + address.getHostAddress() + "' for " + Config.LOGIN_BLOCK_AFTER_BAN + " seconds due to " + failedCount + " invalid user name attempts");
  147.                         this.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN * 1000);
  148.                     }
  149.                 }
  150.                 return false;
  151.             }
  152.            
  153.             // is this account banned?
  154.             if (access < 0)
  155.             {
  156.                 if (Config.LOG_LOGIN_CONTROLLER)
  157.                 {
  158.                     Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : AccountBanned", "loginlog");
  159.                 }
  160.                
  161.                 client.setAccessLevel(access);
  162.                 return false;
  163.             }
  164.            
  165.             // Check IP
  166.             if (!ipWhiteList.isEmpty() || !ipBlackList.isEmpty())
  167.             {
  168.                 if (!ipWhiteList.isEmpty() && !ipWhiteList.contains(address))
  169.                 {
  170.                     if (Config.LOG_LOGIN_CONTROLLER)
  171.                         Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : INCORRECT IP", "loginlog");
  172.                     return false;
  173.                 }
  174.                
  175.                 if (!ipBlackList.isEmpty() && ipBlackList.contains(address))
  176.                 {
  177.                     if (Config.LOG_LOGIN_CONTROLLER)
  178.                         Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : BLACKLISTED IP", "loginlog");
  179.                     return false;
  180.                 }
  181.             }
  182.  
  183.             // check password hash
  184.             ok = Arrays.equals(hash2, expected); //CHANGEMENT ICI
  185.             if (ok)
  186.             {
  187.                 client.setAccessLevel(access);
  188.                 client.setLastServer(lastServer);
  189.                 try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  190.                     PreparedStatement ps = con.prepareStatement(ACCOUNT_INFO_UPDATE))
  191.                 {
  192.                     ps.setLong(1, System.currentTimeMillis());
  193.                     ps.setString(2, address.getHostAddress());
  194.                     ps.setString(3, user);
  195.                     ps.execute();
  196.                 }
  197.             }
  198.         }
  199.         catch (Exception e)
  200.         {
  201.             _log.log(Level.WARNING, "Could not check password:" + e.getMessage(), e);
  202.             ok = false;
  203.         }
  204.        
  205.         if (!ok)
  206.         {
  207.             if (Config.LOG_LOGIN_CONTROLLER)
  208.             {
  209.                 Log.add("'" + user + "' " + address.getHostAddress() + " - ERR : LoginFailed", "loginlog");
  210.             }
  211.            
  212.             FailedLoginAttempt failedAttempt = _hackProtection.get(address);
  213.             int failedCount;
  214.             if (failedAttempt == null)
  215.             {
  216.                 _hackProtection.put(address, new FailedLoginAttempt(address, password));
  217.                 failedCount = 1;
  218.             }
  219.             else
  220.             {
  221.                 failedAttempt.increaseCounter(password);
  222.                 failedCount = failedAttempt.getCount();
  223.             }
  224.            
  225.             if (failedCount >= Config.LOGIN_TRY_BEFORE_BAN)
  226.             {
  227.                 _log.info("Banning '" + address.getHostAddress() + "' for " + Config.LOGIN_BLOCK_AFTER_BAN + " seconds due to " + failedCount + " invalid user/pass attempts");
  228.                 this.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN * 1000);
  229.             }
  230.         }
  231.         else
  232.         {
  233.             _hackProtection.remove(address);
  234.             if (Config.LOG_LOGIN_CONTROLLER)
  235.             {
  236.                 Log.add("'" + user + "' " + address.getHostAddress() + " - OK : LoginOk", "loginlog");
  237.             }
  238.         }
  239.        
  240.         return ok;
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement