Search in sources :

Example 11 with FailedNbt

use of com.ruinscraft.panilla.api.exception.FailedNbt in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws EntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field typeField = PacketPlayOutSpawnEntity.class.getDeclaredField("b");
            typeField.setAccessible(true);
            UUID entityId = (UUID) typeField.get(packet);
            org.bukkit.entity.Entity bukkitEntity = org.bukkit.Bukkit.getEntity(entityId);
            CraftEntity craftEntity = (CraftEntity) bukkitEntity;
            if (craftEntity == null) {
                return;
            }
            Entity entity = craftEntity.getHandle();
            if (entity != null) {
                if (entity instanceof EntityItem) {
                    EntityItem item = (EntityItem) entity;
                    if (item.getItemStack() == null) {
                        return;
                    }
                    if (!item.getItemStack().hasTag()) {
                        return;
                    }
                    INbtTagCompound tag = new NbtTagCompound(item.getItemStack().getTag());
                    String itemName = item.getItemStack().getItem().getName();
                    FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
                    if (FailedNbt.fails(failedNbt)) {
                        throw new EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, bukkitEntity.getWorld().getName());
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_12_R1.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) CraftEntity(org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) Field(java.lang.reflect.Field) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) UUID(java.util.UUID) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 12 with FailedNbt

use of com.ruinscraft.panilla.api.exception.FailedNbt in project Panilla by ds58.

the class InventoryCleaner method clean.

@Override
public void clean(IPanillaPlayer player) {
    CraftPlayer craftPlayer = (CraftPlayer) player.getHandle();
    Container container = craftPlayer.getHandle().activeContainer;
    for (int slot = 0; slot < container.slots.size(); slot++) {
        ItemStack itemStack = container.getSlot(slot).getItem();
        if (itemStack == null || !itemStack.hasTag()) {
            continue;
        }
        NBTTagCompound nmsTag = itemStack.getTag();
        INbtTagCompound tag = new NbtTagCompound(nmsTag);
        String itemName = itemStack.getItem().getName();
        if (nmsTag == null || itemName == null) {
            continue;
        }
        FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
        if (FailedNbt.failsThreshold(failedNbt)) {
            container.getSlot(slot).getItem().setTag(null);
        } else if (FailedNbt.fails(failedNbt)) {
            nmsTag.remove(failedNbt.key);
            container.getSlot(slot).getItem().setTag(nmsTag);
        }
    }
}
Also used : Container(net.minecraft.server.v1_13_R2.Container) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_13_R2.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NBTTagCompound(net.minecraft.server.v1_13_R2.NBTTagCompound) CraftPlayer(org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) ItemStack(net.minecraft.server.v1_13_R2.ItemStack) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 13 with FailedNbt

use of com.ruinscraft.panilla.api.exception.FailedNbt in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws EntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field typeField = PacketPlayOutSpawnEntity.class.getDeclaredField("b");
            typeField.setAccessible(true);
            UUID entityId = (UUID) typeField.get(packet);
            org.bukkit.entity.Entity bukkitEntity = org.bukkit.Bukkit.getEntity(entityId);
            CraftEntity craftEntity = (CraftEntity) bukkitEntity;
            if (craftEntity == null) {
                return;
            }
            Entity entity = craftEntity.getHandle();
            if (entity != null) {
                if (entity instanceof EntityItem) {
                    EntityItem item = (EntityItem) entity;
                    if (item.getItemStack() == null) {
                        return;
                    }
                    if (!item.getItemStack().hasTag()) {
                        return;
                    }
                    INbtTagCompound tag = new NbtTagCompound(item.getItemStack().getTag());
                    String itemName = item.getItemStack().getItem().getName();
                    FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
                    if (FailedNbt.fails(failedNbt)) {
                        throw new EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, bukkitEntity.getWorld().getName());
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_13_R2.nbt.NbtTagCompound) CraftEntity(org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) Field(java.lang.reflect.Field) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) UUID(java.util.UUID) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 14 with FailedNbt

use of com.ruinscraft.panilla.api.exception.FailedNbt in project Panilla by ds58.

the class NbtCheck_EntityTag method check.

@Override
public NbtCheckResult check(INbtTagCompound tag, String itemName, IPanilla panilla) {
    INbtTagCompound entityTag = tag.getCompound(getName());
    if (panilla.getPConfig().strictness == PStrictness.STRICT) {
        for (String armorStandTag : ARMOR_STAND_TAGS) {
            if (entityTag.hasKey(armorStandTag)) {
                return NbtCheckResult.FAIL;
            }
        }
    }
    if (entityTag.hasKey("CustomName")) {
        String customName = entityTag.getString("CustomName");
        if (customName.length() > panilla.getProtocolConstants().NOT_PROTOCOL_maxEntityTagCustomNameLength()) {
            return NbtCheckResult.CRITICAL;
        }
        try {
            panilla.getPacketInspector().validateBaseComponentParse(customName);
        } catch (Exception e) {
            return NbtCheckResult.CRITICAL;
        }
    }
    if (entityTag.hasKey("UUID")) {
        String uuid = entityTag.getString("UUID");
        try {
            UUID.fromString(uuid);
        } catch (Exception e) {
            return NbtCheckResult.CRITICAL;
        }
    }
    if (entityTag.hasKey("ExplosionPower")) {
        return NbtCheckResult.FAIL;
    }
    if (entityTag.hasKey("Invulnerable")) {
        return NbtCheckResult.FAIL;
    }
    if (entityTag.hasKey("Motion")) {
        return NbtCheckResult.FAIL;
    }
    if (entityTag.hasKey("power")) {
        return NbtCheckResult.FAIL;
    }
    if (entityTag.hasKey("ArmorItems")) {
        INbtTagList items = entityTag.getList("ArmorItems", NbtDataType.COMPOUND);
        FailedNbt failedNbt = checkItems(items, itemName, panilla);
        if (FailedNbt.fails(failedNbt)) {
            return failedNbt.result;
        }
    }
    if (entityTag.hasKey("HandItems")) {
        INbtTagList items = entityTag.getList("HandItems", NbtDataType.COMPOUND);
        FailedNbt failedNbt = checkItems(items, itemName, panilla);
        if (FailedNbt.fails(failedNbt)) {
            return failedNbt.result;
        }
    }
    boolean hasIdTag = entityTag.hasKey("id");
    if (hasIdTag) {
        if (panilla.getPConfig().strictness == PStrictness.STRICT) {
            return NbtCheckResult.FAIL;
        }
        String id = entityTag.getString("id");
        // prevent lightning bolt eggs
        if (id.toLowerCase().contains("lightning")) {
            return NbtCheckResult.FAIL;
        }
        // check for massive slime spawn eggs
        if (entityTag.hasKey("Size")) {
            if (entityTag.getInt("Size") > panilla.getProtocolConstants().maxSlimeSize()) {
                return NbtCheckResult.CRITICAL;
            }
        }
        // see nms.EntityAreaEffectCloud
        if (entityTag.hasKey("Age")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Duration")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("WaitTime")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("ReapplicationDelay")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("DurationOnUse")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("RadiusOnUse")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("RadiusPerTick")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Radius")) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Particle") && panilla.getPConfig().strictness == PStrictness.STRICT) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Color") && panilla.getPConfig().strictness == PStrictness.STRICT) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Potion") && panilla.getPConfig().strictness == PStrictness.STRICT) {
            return NbtCheckResult.FAIL;
        }
        if (entityTag.hasKey("Effects")) {
            return NbtCheckResult.FAIL;
        }
    }
    return NbtCheckResult.PASS;
}
Also used : INbtTagList(com.ruinscraft.panilla.api.nbt.INbtTagList) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt)

Example 15 with FailedNbt

use of com.ruinscraft.panilla.api.exception.FailedNbt in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws EntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field typeField = PacketPlayOutSpawnEntity.class.getDeclaredField("d");
            typeField.setAccessible(true);
            UUID entityId = (UUID) typeField.get(packet);
            Entity entity = null;
            for (WorldServer worldServer : MinecraftServer.getServer().getWorlds()) {
                entity = worldServer.G.d().a(entityId);
                if (entity != null)
                    break;
            }
            if (entity != null) {
                if (entity instanceof EntityItem) {
                    EntityItem item = (EntityItem) entity;
                    if (item.getItemStack() == null) {
                        return;
                    }
                    if (!item.getItemStack().hasTag()) {
                        return;
                    }
                    INbtTagCompound tag = new NbtTagCompound(item.getItemStack().getTag());
                    String itemName = item.getItemStack().getItem().getName();
                    FailedNbt failedNbt = NbtChecks.checkAll(tag, itemName, panilla);
                    if (FailedNbt.fails(failedNbt)) {
                        throw new EntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId, entity.getWorld().getWorld().getName());
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : PacketPlayOutSpawnEntity(net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity) Entity(net.minecraft.world.entity.Entity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_17_R1.nbt.NbtTagCompound) WorldServer(net.minecraft.server.level.WorldServer) Field(java.lang.reflect.Field) PacketPlayOutSpawnEntity(net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) UUID(java.util.UUID) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt) EntityItem(net.minecraft.world.entity.item.EntityItem)

Aggregations

FailedNbt (com.ruinscraft.panilla.api.exception.FailedNbt)25 INbtTagCompound (com.ruinscraft.panilla.api.nbt.INbtTagCompound)24 Field (java.lang.reflect.Field)12 EntityNbtNotPermittedException (com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException)10 UUID (java.util.UUID)10 INbtTagList (com.ruinscraft.panilla.api.nbt.INbtTagList)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 PacketPlayOutSpawnEntity (net.minecraft.network.protocol.game.PacketPlayOutSpawnEntity)3 WorldServer (net.minecraft.server.level.WorldServer)3 Entity (net.minecraft.world.entity.Entity)3 EntityItem (net.minecraft.world.entity.item.EntityItem)3 Container (net.minecraft.world.inventory.Container)3 ItemStack (net.minecraft.world.item.ItemStack)3 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_12_R1.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_13_R2.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_14_R1.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_15_R1.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R1.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R2.nbt.NbtTagCompound)2 NbtTagCompound (com.ruinscraft.panilla.craftbukkit.v1_16_R3.nbt.NbtTagCompound)2