Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Extraction and unpacking of game archives and compression, encryption, obfuscation, decoding of unknown files
IamEox
Posts: 4
Joined: Tue Mar 23, 2021 6:04 pm

Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by IamEox »

Hi,
This question has already been asked twice on this forum, but since the answers didn't help me I ask it once again.

Yesterday I spent several hours trying to extract the content of the .tpk files from Club Penguin's Card-Jitsu Snow minigame. Here's what I learned from the other topics:
- The .tpk files are zlib archives and I managed to uncompress them with the command:

Code: Select all

offzip.exe -a file.tpk dest_dir 0`
- The files produced are .kap files. This guy says they contain raw image data. The actual content of the files start with "KAPD" (magic number?), then there's the binary data (image data?) and finally some JSON. For instance:

Code: Select all

{
    "frames": [
        {
            "filename": "rock_mountaintop.png",
            "frame": {
                "x": 0,
                "y": 0,
                "w": 75,
                "h": 69
            },
            "rotated": false,
            "trimmed": false,
            "spriteSourceSize": {
                "x": 0,
                "y": 0,
                "w": 75,
                "h": 69
            },
            "sourceSize": {
                "w": 75,
                "h": 69
            }
        }
    ],
    "meta": {
        "app": "http://www.texturepacker.com",
        "version": "1.0",
        "image": "rock_mountaintop.png",
        "format": "RGBA8888",
        "size": {
            "w": 128,
            "h": 128
        },
        "scale": "1",
        "smartupdate": "$TexturePacker:SmartUpdate:af546ed297e90557d402551245566a2f$"
    }
}
- Following this guy's advice, I tried to play a little bit with TexturePacker to understand where this JSON could come from. Both the images and the JSON were probably generated as separate files and then put together,
- I also played with PVRTexTool and tried the "Wrap Raw Data" menu to open the file. I think the binary data might be compressed because the example rock_mountaintop_small.kap was too small to be a 128x128 image in RGBA8888 format. But if it is, I can't find the correct algorithm in PVRTexTool's list and end up with noise.

So, can anyone help me with this? Is there any tool I can use to extract the .png data in this .kap file?
I don't know if the help given in the other topics was enough. As for me, I am fairly new at this and I need a little more help.

PS: I attached both the untouched .tpk file and the corresponding .kap file I talked about here. Both are in .txt because the actual extensions are not allowed here.
Last edited by IamEox on Sun May 08, 2022 1:18 pm, edited 1 time in total.
IamEox
Posts: 4
Joined: Tue Mar 23, 2021 6:04 pm

Re: Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by IamEox »

Hi again,

Today I spent several hours trying to reverse Club Penguin's SWF files. Here's what I found:
- The interesting bit that unpacks the .kap files is in the method unpack of the class com.disney.util.DPack in this file. It indicates the files' header format and sections. Here is the code:

Code: Select all

public static function unpack(param1:ByteArray, param2:Boolean = true) : Dictionary {
    var temp:ByteArray = null;
    var magic:uint = 0;
    var numFiles:uint = 0;
    var i:int = 0;
    var lengths:Array = null;
    var names:Array = null;
    var items:Dictionary = null;
    var bytes:ByteArray = null;
    var packedBytes:ByteArray = param1;
    var makeCopy:Boolean = param2;

    if (packedBytes == null) return null;

    if (makeCopy) {
        temp = new ByteArray();
        temp.writeBytes(packedBytes);
        packedBytes = temp;
    }

    try {
        packedBytes.endian = Endian.LITTLE_ENDIAN;
        packedBytes.uncompress();
        magic = packedBytes.readUnsignedInt();

        if (magic != MAGIC_CODE) {
            Log.error("unpack: Invalid magic code " + magic);
            return null;
        }

        numFiles = packedBytes.readUnsignedShort();
        lengths = [];
        i = 0;
        while (i < numFiles) {
            lengths.push(packedBytes.readUnsignedInt());
            i++;
        }

        names = [];
        i = 0;
        while (i < numFiles) {
            names[i] = packedBytes.readUTF();
            i++;
        }

        items = new Dictionary();
        i = 0;
        while (i < numFiles) {
            bytes = new ByteArray();
            packedBytes.readBytes(bytes,0,lengths[i]);
            items[names[i]] = bytes;
            i++;
        }

        return items;
   
    } catch(err:Error) {
        Log.error("DPack.unpack: Error unpacking pack file. Error = " + err);
        return null;
    }
}
- The files don't contain 2 differents sections as I initially thought, but 4: they are tagged "t" (textures), "j" (json), "i" (interval) and "m" (metadata?),
- The interesting section (the first one) appears to be a JPEG XR image. Editing the hex code of the .kap to keep only the "t" section hex gives a working .jxr image.

I hope this can help those who were, are and will be in the same situation I was yesterday.
I won't mark this post as solved immediately, I need to do/find a few more things (like a script) before that.

PS: I attached my attempt to decode the example .kap file.
IamEox
Posts: 4
Joined: Tue Mar 23, 2021 6:04 pm

Re: Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by IamEox »

I finally had some time to write a little script. Here it is: https://github.com/iameox/tpkator.
This thread can be closed although I don't know how to do it.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by aluigi »

Cool, let's keep it open in case other users need help.
Monke
Posts: 3
Joined: Fri Apr 15, 2022 5:12 pm

Re: Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by Monke »

Were you able to decompile the files?
IamEox
Posts: 4
Joined: Tue Mar 23, 2021 6:04 pm

Re: Help with Club Penguin's Card-Jitsu Snow .tpk/.kap files

Post by IamEox »

To extract them you can use the scripts I wrote last year: https://github.com/iameox/tpkator.

Or you can just get the png files here: https://mega.nz/file/2pxE0C5Y#9PjRX9yjU ... 9iDBlO2CdM.
I can't guarantee that this link stays up, though.