JNUSLib - Java NUS Library

Discussion in 'Wii U - Homebrew' started by Maschell, Dec 12, 2016.

  1. Maschell
    OP

    Maschell GBAtemp Advanced Fan

    Member
    887
    1,282
    Jun 14, 2008
    Gambia, The

    JNUSLib - All in one library to handle NUS content


    JNUSLib is a library written in Java to handle NUS Contents (.app, tmd, tik, cert files) from different sources. It can be easily used in other Applications and
    is directed to devolpers. It's based on JNUSTool, but is heavily rewritten and extented with even more features.

    Features

    Loading NUS Contents from different sources. Currently implemented:


    For files loaded from one of the sources, the following features can be used.
    • Only on-the-fly actions. Files will be downloaded only on demand, splitted files can be handled natively, and .wux have to be decompressed!
    • Extracting all files. Needed decryption will be done on the fly.
    • Extracting single files. Needed decryption will be done on the fly.
    • Extracting the content files (.app, .h3, tmd, etc.) Useful for all sources except "local".
    • This means title downloading, and .woomy, .wux, .wud and splitted .wud extracting into the "installable" format.
    • Providing a InputStream to the decrypted data of a file.
    • Decrypt a full content (.app) file.

    For WUD files, following additional operations are possible:
    • Compressing into .wux file format (from .wud and splitted .wud)
    • Verifing (comparing) two different wud images.

    How to use it

    At first you have to import the jnuslib.jar and the common key in the Settings class.
    Code:
    Settings.commonKey = Utils.StringToByteArray("12345678901234567890123456789012");
    
    Then for each different source type, you need to use a an own NUSTitleLoader.

    Code:
    //REMOTE
    
      //Loading a title with a public ticket
      NUSTitle nusRemote = NUSTitleLoaderRemote.loadNUSTitle(0x0005000E12345678L);
     
      //Loading a title with an own ticket (key, titleid)
      Ticket ticket = Ticket.createTicket(Utils.StringToByteArray("12345678901234567890123456789012"), 0x0005000E12345678L);
      NUSTitle nusRemoteWithTicket = NUSTitleLoaderRemote.loadNUSTitle(0x0005000E12345678L);
    //LOCAL
    
      //With ticket on disk
      NUSTitle nusLocal = NUSTitleLoaderLocal.loadNUSTitle("path-to-app-files");
     
      //Loading a title with an own ticket (key, titleid)
      Ticket ticket = Ticket.createTicket(Utils.StringToByteArray("12345678901234567890123456789012"), 0x0005000E12345678L);
      NUSTitle nusRemoteWithTicket = NUSTitleLoaderLocal.loadNUSTitle("path-to-app-files");
    
    //Loading a .woomy file
      NUSTitle nusWoomy = NUSTitleLoaderWoomy.loadNUSTitle("testfile.woomy");
     
    //WUD
      //Loading a uncompressed WUD
      NUSTitle nusWUD = NUSTitleLoaderWUD.loadNUSTitle("game.wud");
      //Loading a compressed WUD (WUX)
      NUSTitle nusWUX = NUSTitleLoaderWUD.loadNUSTitle("game.wux");
      //Loading a uncompressed splitted WUD (2gb parts)
      NUSTitle nusWUDSplitted = NUSTitleLoaderWUD.loadNUSTitle("game_part1.wud");
    
    Once the title is loaded, you can use one of the services to extract and decrypt files.
    Here are some of the operations you can do. Look at the code for the full list of methods.

    Decryption:

    Code:
    DecryptionService decrypt = DecryptionService.getInstance(title);
    
    //Decrypt the whole FST into a folder called "DecryptedTitle"
    decrypt.decryptAllFSTEntriesTo("DecryptedTitle");
    
    //Decrypt the code folder into a folder called "code_folder"
    decrypt.decryptFSTEntriesTo("/code/.*", code_folder);
    
    //Decrypt all .js files into a folder called "js_files"
    decrypt.decryptFSTEntriesTo(".*.js", js_files);
    
    
    //Decrypt all .app files into a folder called "decrypted_contents"
    decrypt.decryptAllPlainContents("decrypted_contents");
    
    
    //Use decrypting inputstream. Data will be only loaded/decrypted on demand.
    
    //Display the app.xml as hey dump
    FSTEntry appXMLEntry = title.getFSTEntryByFullPath("code/app.xml");
    decrypt.getDecryptedOutputAsInputStream(appXMLEntry);
    //Lets just print the app.xml as hex data
    int BUFFER_SIZE = 0x40;
    byte[] buffer = new byte[BUFFER_SIZE];
    int i = 0; 
    while(in.read(buffer) > 0){
      System.out.println(String.format("0x%04X: ", (i++ * BUFFER_SIZE)) + Utils.ByteArrayToString(buffer));
    }
    in.close();
    [...]
    
    Extraction:
    Code:
    //Get the Service for the NUSTitle
    ExtractionService extract = ExtractionService.getInstance(title);
    
    //Saving all .app/.h3/tmd/tik/cert files into the folder "encryptedFiles"
    extract.extractAll("encryptedFiles");
    
    //Save all .h3 files into "contentHashes"
    extract.extractAllEncrpytedContentFileHashes("contentHashes");
    
    //Save all -.app files into "contents"
    extract.extractAllEncryptedContentFilesWithoutHashesTo("contents");
    
    //Save tmd, cert and ticket
    extract.extractTMDTo(output);
    extract.extractTickeTo(output);
    extract.extractCertTo(output);
    
    [...]
    
    Example for compressing and verifing into .wux files.

    Code:
      WUDImage imageUncompressed = new WUDImage(new File("game_part1.wud")); //Splitted and not splitted .wud possible here
     
      WUDService.compressWUDToWUX(imageUncompressed, "compressedImage","game.wux");
     
      WUDImage imageCompressed = new WUDImage(new File("compressedImage" + File.separator + "game.wux"));
     
      //Verify compression
      WUDService.compareWUDImage(imageUncompressed, imageCompressed);
    
    Cleanup:
    Call the method cleanup() for a NUSTitle to cleanup/close all opened ressources.

    Sources

    https://github.com/Maschell/JNUSLib
    Credits

    Maschell

    Thanks to:
    Crediar for CDecrypt (https://github.com/crediar/cdecrypt)
    All people who have contributed to vgmtoolbox (https://sourceforge.net/projects/vgmtoolbox/)
    Exzap for the .wux file format (https://gbatemp.net/threads/wii-u-image-wud-compression-tool.397901/)
    FIX94 for wudump (https://gbatemp.net/threads/wudump-dump-raw-images-from-a-wiiu-game-disc.451736/)
    The creators of lombok for lombok https://projectlombok.org/index.html
     
    Last edited by Maschell, Dec 13, 2016
    Hiccup, josamilu, fejich and 8 others like this.
  2. Maschell
    OP

    Maschell GBAtemp Advanced Fan

    Member
    887
    1,282
    Jun 14, 2008
    Gambia, The
    Thats the current state of the library I'm currently working on.

    I hope everything works as expected, but its definitally still in developement and just a library.

    My plan is to make JNUSTool 1.0 based on this lib (Does anyone want to help me with the GUI :>)

    I hope some people/devs can actually test it,find bugs, give feedback and contribute!

    (I didn't check all features in all possible scenarios/sources/operationen, so probably this still has bug!)
     
    Last edited by Maschell, Dec 13, 2016
    iAqua, WiiuGold and OriginalHamster like this.
  3. WiiuGold

    WiiuGold GBAtemp Advanced Fan

    Member
    515
    423
    Jan 5, 2015
    I can't wait how this will all play out :) I can't wait ..
     
  4. BullyWiiPlaza

    BullyWiiPlaza Nintendo Hacking <3

    Member
    1,691
    1,375
    Aug 2, 2014
    Germany
    That's nice but nobody else is using Java pretty much except for me. This might come in handy though^^
     
    cucholix and Maschell like this.
  5. kornychaos

    kornychaos Organized.

    Member
    388
    90
    Jul 7, 2007
    United States
    @Maschell

    I know you've mentioned going JNUSTool 1.0 with this.
    I had originally used JNUSTool to download the encrypted app files -dlEncrypted.
    Now I have a couple of games that are encrypted, with their .tik files in their respective tmp_[ID] folders.
    With FAT32 coming up, I'm going to need to decrypt all of these versus letting the Wii U/wupinstaller do it.

    When I run:
    java -jar JNUSTool.jar 00050000101C5d00
    java -jar JNUSTool.jar 00050000101B4600
    Unfortunately I have to wait to select the root and click download and repeat for /each/ title.
    I'm looking to batch decrypt these encrypted game files that I've already downloaded without interaction,
    provided they have their appropriate .tik files in place. Will this be in your JNUSTool 1.0?

    or can I go ahead and request an addition [option] to:
    java -jar JNUSTool.jar 00050000101B4600 -silent
    Which would auto-select the root of each game and extract them without me clicking root and download and repeating?
     
  6. Maschell
    OP

    Maschell GBAtemp Advanced Fan

    Member
    887
    1,282
    Jun 14, 2008
    Gambia, The
    java -jar jnustool.jar 0005000E12345678 -file .*

    Should do the job
     
    kornychaos likes this.
  7. vgmoose

    vgmoose GBAtemp Regular

    Member
    210
    609
    Jan 31, 2016
    United States
    very cool :D
     
    Maschell likes this.