From f74253f4cb167cb85a3a844d945014861280e360 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 7 Mar 2023 15:37:57 +0100 Subject: [PATCH] AntiNocom false flag reduction --- .../de/steamwar/core/events/AntiNocom.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java b/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java index 0799523..ea7759d 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java @@ -27,12 +27,20 @@ import de.steamwar.techhider.ProtocolUtils; import de.steamwar.techhider.TechHider; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; -public class AntiNocom { +public class AntiNocom implements Listener { + + private final Map flags = new ConcurrentHashMap<>(); public AntiNocom() { + Bukkit.getServer().getPluginManager().registerEvents(this, Core.getInstance()); TinyProtocol.instance.addFilter(blockDig, this::onDig); if(Core.getVersion() > 8) { @@ -40,6 +48,11 @@ public class AntiNocom { } } + @EventHandler + public void onQuit(PlayerQuitEvent e) { + flags.remove(e.getPlayer()); + } + private void registerUseItem() { Class useItem = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseItem"); @@ -56,16 +69,7 @@ public class AntiNocom { TinyProtocol.instance.addFilter(useItem, (player, packet) -> { Object pos = getPosition.apply(packet); - int x = TechHider.blockPositionX.get(pos); - int z = TechHider.blockPositionZ.get(pos); - - if(!player.getWorld().isChunkLoaded(ProtocolUtils.posToChunk(x), ProtocolUtils.posToChunk(z))) { - Bukkit.getScheduler().runTask(Core.getInstance(), () -> player.kickPlayer(null)); - SWException.log(player.getName() + " kicked for using item on unloaded block (potential NoCom-DOS attack)", x + " " + z); - return null; - } - - return packet; + return isValid(player, "UseItem", TechHider.blockPositionX.get(pos), TechHider.blockPositionZ.get(pos)) ? packet : null; }); } @@ -73,13 +77,20 @@ public class AntiNocom { private static final Reflection.FieldAccessor digPosition = Reflection.getField(blockDig, TechHider.blockPosition, 0); private Object onDig(Player player, Object packet) { Object pos = digPosition.get(packet); - int x = TechHider.blockPositionX.get(pos); - int z = TechHider.blockPositionZ.get(pos); - if((x != 0 || z != 0) && !player.getWorld().isChunkLoaded(ProtocolUtils.posToChunk(x), ProtocolUtils.posToChunk(z))) { - Bukkit.getScheduler().runTask(Core.getInstance(), () -> player.kickPlayer(null)); - SWException.log(player.getName() + " kicked for digging an unloaded block (potential NoCom-DOS attack)", x + " " + z); - return null; + return isValid(player, "Dig", TechHider.blockPositionX.get(pos), TechHider.blockPositionZ.get(pos)) ? packet : null; + } + + private boolean isValid(Player player, String type, int x, int z) { + if((x == 0 && z == 0) || player.getWorld().isChunkLoaded(ProtocolUtils.posToChunk(x), ProtocolUtils.posToChunk(z))) + return true; + + int amount = flags.compute(player, (p, a) -> a == null ? 1 : a+1); + if(amount % 8 == 0) { // Only after 8 and every 8 to reduce false flags and spam + if(amount == 8) // Schedule player kick only once + Bukkit.getScheduler().runTask(Core.getInstance(), () -> player.kickPlayer(null)); + + SWException.log(player.getName() + " kicked for potential NoCom-DOS attack", x + " " + z + " " + type + " " + amount); } - return packet; + return false; } }