Search in sources :

Example 6 with INbtTagCompound

use of com.ruinscraft.panilla.api.nbt.INbtTagCompound 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_16_R3.entity.CraftEntity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_16_R3.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) CraftEntity(org.bukkit.craftbukkit.v1_16_R3.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 7 with INbtTagCompound

use of com.ruinscraft.panilla.api.nbt.INbtTagCompound 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_14_R1.entity.CraftEntity) EntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException) NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_14_R1.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) CraftEntity(org.bukkit.craftbukkit.v1_14_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 8 with INbtTagCompound

use of com.ruinscraft.panilla.api.nbt.INbtTagCompound in project Panilla by ds58.

the class PacketInspector method checkPacketPlayOutSpawnEntity.

@Override
public void checkPacketPlayOutSpawnEntity(Object _packet) throws LegacyEntityNbtNotPermittedException {
    if (_packet instanceof PacketPlayOutSpawnEntity) {
        PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) _packet;
        try {
            Field idField = PacketPlayOutSpawnEntity.class.getDeclaredField("a");
            idField.setAccessible(true);
            int entityId = (int) idField.get(packet);
            Entity entity = getEntityById(entityId);
            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 LegacyEntityNbtNotPermittedException(packet.getClass().getSimpleName(), false, failedNbt, entityId);
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : NbtTagCompound(com.ruinscraft.panilla.craftbukkit.v1_8_R3.nbt.NbtTagCompound) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) Field(java.lang.reflect.Field) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound) FailedNbt(com.ruinscraft.panilla.api.exception.FailedNbt) LegacyEntityNbtNotPermittedException(com.ruinscraft.panilla.api.exception.LegacyEntityNbtNotPermittedException)

Example 9 with INbtTagCompound

use of com.ruinscraft.panilla.api.nbt.INbtTagCompound in project Panilla by ds58.

the class NbtCheck_pages method getCharCountForItem.

private static int getCharCountForItem(INbtTagCompound item) {
    int charCount = 0;
    if (item.hasKey("tag")) {
        INbtTagCompound tag = item.getCompound("tag");
        if (tag.hasKey("pages")) {
            INbtTagList pages = tag.getList("pages", NbtDataType.STRING);
            for (int i = 0; i < pages.size(); i++) {
                final String page = pages.getString(i);
                final String pageNoSpaces = page.replace(" ", "");
                charCount += pageNoSpaces.length();
            }
        }
    }
    return charCount;
}
Also used : INbtTagList(com.ruinscraft.panilla.api.nbt.INbtTagList) INbtTagCompound(com.ruinscraft.panilla.api.nbt.INbtTagCompound)

Example 10 with INbtTagCompound

use of com.ruinscraft.panilla.api.nbt.INbtTagCompound in project Panilla by ds58.

the class NbtCheck_BlockEntityTag method check.

@Override
public NbtCheckResult check(INbtTagCompound tag, String itemName, IPanilla panilla) {
    INbtTagCompound blockEntityTag = tag.getCompound(getName());
    int sizeBytes = blockEntityTag.getStringSizeBytes();
    int maxSizeBytes = panilla.getProtocolConstants().NOT_PROTOCOL_maxBlockEntityTagLengthBytes();
    // ensure BlockEntityTag isn't huge
    if (sizeBytes > maxSizeBytes) {
        return NbtCheckResult.CRITICAL;
    }
    if (blockEntityTag.hasKey("LootTable")) {
        String lootTable = blockEntityTag.getString("LootTable");
        lootTable = lootTable.trim();
        if (lootTable.isEmpty()) {
            return NbtCheckResult.CRITICAL;
        }
        if (lootTable.contains(":")) {
            String[] keySplit = lootTable.split(":");
            if (keySplit.length < 2) {
                return NbtCheckResult.CRITICAL;
            }
            String namespace = keySplit[0];
            String key = keySplit[1];
            if (namespace.isEmpty() || key.isEmpty()) {
                return NbtCheckResult.CRITICAL;
            }
        }
    }
    if (panilla.getPConfig().strictness == PStrictness.STRICT) {
        // locked chests
        if (blockEntityTag.hasKey("Lock")) {
            return NbtCheckResult.FAIL;
        }
        // signs with text
        if (blockEntityTag.hasKey("Text1") || blockEntityTag.hasKey("Text2") || blockEntityTag.hasKey("Text3") || blockEntityTag.hasKey("Text4")) {
            return NbtCheckResult.FAIL;
        }
    }
    // tiles with items/containers (chests, hoppers, shulkerboxes, etc)
    if (blockEntityTag.hasKey("Items")) {
        // only ItemShulkerBoxes should have "Items" NBT tag in survival
        itemName = itemName.toLowerCase();
        if (panilla.getPConfig().noBlockEntityTag) {
            return NbtCheckResult.FAIL;
        }
        if (panilla.getPConfig().strictness == PStrictness.STRICT) {
            if (!(itemName.contains("shulker") || itemName.contains("itemstack") || itemName.contains("itemblock"))) {
                return NbtCheckResult.FAIL;
            }
        }
        // Campfires should not have BlockEntityTag
        if (itemName.contains("campfire")) {
            return NbtCheckResult.FAIL;
        }
        INbtTagList items = blockEntityTag.getList("Items", NbtDataType.COMPOUND);
        FailedNbt failedNbt = checkItems(items, itemName, panilla);
        if (FailedNbt.fails(failedNbt)) {
            return failedNbt.result;
        }
    }
    // check the item within a JukeBox
    if (blockEntityTag.hasKey("RecordItem")) {
        INbtTagCompound item = blockEntityTag.getCompound("RecordItem");
        FailedNbt failedNbt = checkItem(item, itemName, panilla);
        if (FailedNbt.fails(failedNbt)) {
            return failedNbt.result;
        }
    }
    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)

Aggregations

INbtTagCompound (com.ruinscraft.panilla.api.nbt.INbtTagCompound)29 FailedNbt (com.ruinscraft.panilla.api.exception.FailedNbt)24 Field (java.lang.reflect.Field)12 UUID (java.util.UUID)11 EntityNbtNotPermittedException (com.ruinscraft.panilla.api.exception.EntityNbtNotPermittedException)10 INbtTagList (com.ruinscraft.panilla.api.nbt.INbtTagList)7 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