SteamWar/SpigotCore
Archiviert
13
0

Merge pull request 'AntiNocom false flag reduction' (#234) from nocom into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #234
Reviewed-by: YoyoNow <jwsteam@nidido.de>
Dieser Commit ist enthalten in:
Lixfel 2023-03-08 19:42:00 +01:00
Commit 953cbd844a

Datei anzeigen

@ -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<Player, Integer> 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;
}
return packet;
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 false;
}
}