-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Research needed] Handling .cmd CMACs on SD card #340
Comments
hmm... |
Finally somebody noticed these |
It's for SD transfers between console rather than for CIA installation (doubt we'll ever fully figure out the |
I was finding a way to directly install CIA to sdcard from PC and got stuck at this ".cmd" file too. Not much information about this on 3dbrew or anywhere on Google. |
The next problem you'd be facing, @tranxuanthang, would be updating the |
I'm interested in CIA installation from PC as well. Are there some docs about how installation is performed on 3DS devices that I can examine? |
How does one calculate the CMAC for an arbitrary block of hex? I have a theory those 0x10 bytes CMACs in the How was this current conclusion arrived?
Edit 1 - I might be wrong about taking the CMAC of the decrypted form of the Edit 2 - The |
I think the first cmac is of a hash of the data following it. I figured it out by guessing once. The others are not simply of the NCCH headers (though it is related, as changing anything outside of it raises a different error), I would guess it's similar to DISA/DIFF where the cmac is for a hash of a data block. |
Actually, the first cmac is of the first 0x10 of the file (no hash). The rest I still don't know. |
I believe Content ID or Content Index influences the cmac. This is shown in the DLC for "Super Smash Bros. for Nintendo 3DS", where all content past the first is identical (same sha256), but cmacs for all the contents are different. Also interesting to note are parts of the cmd file where the Content ID is FFFFFFFF. I would guess DLC content that is not installed. image |
Maybe the CMACs are derived from the Edit - Count me out until I catch up. Studying how these various AES-MAC work. |
Okay, the In the
These are Content IDs in endian for:
If you edit the first four bytes (game), this will cause its HOME Menu banner to disappear. Let's say you try editing the name for block 0x20-23 along with the
The banner will be missing, and launching the game will fail. I also edited the mirror block 0x24-2F from:
This does not affect banner appearance or the launching of either game or manual. I tried injecting FBI in H&S system title. You can hex edit the first 512 bytes or If you make similiar edits to the I managed to rename injected FBI from:
If the CMD and APP are not matched in the edits, FBI banner will not appear / will not launch. With limited understanding of AES-MAC, OMAC1/CMAC, maybe the pieces to the formula:
Late Edit - If you swap places for the Content IDs and CMACs in the The Title ID of F-ZERO SNES VC is 000400000F701900. I used NSUI to custom inject Killer Instinct SNES VC with the same Title ID. I then swapped places for the games' Killer Instinct's banner shows and game launches. F-ZERO manual launches. Pressing (HOME) button in the game will crash the 3DS. |
@ihaveamac. I think I know how the Content ID relates to different CMACs for the Super Smash Bros DLC with identical I'm not sure if I can be of any further help as the biggest hurdle I'm facing is obtaining those 0x40 AES key slots and the secret constant C for the 3DS NormalKey generator formula... Second hurdle is playing catch-up watching YouTube examples and reading NIST publications in how AES & MAC stuff works like XOR, ROL, ROR, and other scrambling techniques. |
@TurdPooCharger - you mean you're unsure which AES keyslot to use? |
I'm posting these references here so those who aren't versed in this 3DS subject may get up to speed. I believe I'm now sufficiently knowledgeable enough to continue discussion.
@d0k3, would you happen to already know which specific key is responsible for the CMAC of the CMD header as shown in the below example? I'm aware there are separate, different keys that handle CMACs for the Edit - The above picture was originally posted with'0x16 bytes'. Corrections made to denote '16 bytes'. |
Small update. I figured out how the CMAC is calculated for the Slot 0x30 keyX: X##X#####X######X##XXX###X####X# Slot 0x30 keyY: console or user unique Calculate your 0x30 NormalKey using the AES Key Scrambler formula. From the above picture example, the 0x30 NormalKey is the key (k). Sadly to say, I haven't been able to determine if the 0x30 NormalKey works for the CMACs for the There might a different NormalKey slot at work. Grr. |
How the heck did any of you OG devs figured out this s*** in the first place? Did the idea of sticking it to Nintendo out of pure LoL spite was what fueled your ambitions? My mind can barely comprehend back tracing all of this let alone your average 3DS user. Readers who would like to follow how the CMACs are derived for the Since I possess both a n3DS and n3DSXL, I made a dummy profile using the Installing a fresh copy of F-ZERO on the n3DSXL, it has matching While not conclusive, this suggest the CMACs for the title contents (game, manual, dlc, etc.) share the same 0x30 NormalKey as that of the CMD header. I have to revise my findings about the NCCH header. For SD version you can edit the However, if you try editing FBI injected in H&S blocked 0x000-0FF, the reverse happens where banner disappears and icon turns black. This means system titles are verified not by CMACs found in @ihaveamac's theory of DISA/DIFF-like CMAC with Content ID may very well be a possibility. Icould not determine a simple relation of the SHA-256 hash or 0x100-1FF block in either decrypted or encrypted forms to be the message (m). We are now hitting the brickwall in figuring out if there's a AES-CMAC (intermediate), how it's structured if it exists, and whether there's some sort of MAGIC header that goes with it. |
System titles don't have any cmacs, except Download Play Child titles. Thanks for finding out the signature is probably not included, but I'm not sure how much farther we can go without someone finding the code that generates these cmacs. |
I believe I found a missing piece to the AES-CMAC (intermediate) puzzle. Sorry, no color coding for the picture below. There are some data fields that seem to correlate to either There's also the mysterious block 0x28-2B == 0x5FC-5FF == 0x9FC-9FF. This continuously updates in value and appears to be involved with the on-the-fly formation or building of those Another mystery block is 0x21C-21FF. My theory is that it's some sort of offset to keep track of where along in those The
My multiple Rosalina menu pauses and reboots to GodMode9 has not been able to "catch" the supposedly NCCH header placed in that I'm not sure if this is technically possible, but if only there's some way to probe, directly observe, or record how the Edit 2 - Has anyone seen FF like this before in other AES-CMACs? Is this Nintendo's countermesasure at garbling the AES-CMAC message to keep it hidden? Edit 3 - Forgot to add. The CMACs found on rows 0x200 and 0x600 are the SHA-256 hashes for their sections below as the message (m). NormalKey is slot 0x30. |
I have a suspicion that block 0x200-5FF in the Ideas I've tried thus far ... 1. Reformat 256 MB, 4 GB, and 32 GB micro SD cards in FAT32 + 512 bytes cluster size. 2. Examine memory dumps from the Rosalina menu process list. 3. Countless variations of guessing in trying to recreate the CMAC message data field. Another idea that hasn't been attempted. 4. Take an o3DS/o2DS system and downgrade to the lowest 3DS firmware version. Far fetch... :/ 5. Refer or back track to the person who originally derived or obtained those DIFF & DISA AES-CMAC types. Late Edit - Maybe some good news? 6. Scan SD card with data recovery program. |
It looks like memory dump approaches will not work. I attempted installing F-ZERO, rebooting to GodMode9, and then extracting the Searches in other GodMode9 When hex searching the decrypted From this subreddit thread and 3dbrew revision history, @TuxSH is credited in figuring out how those previously mentioned CMACs are generated. I have one simple question. TuxSH, did you use some sort of ARM disassembler like IDA Pro when you reverse engineered those CMAC message fields? |
I may be a bit out of the loop here. Thanks to everyone putting in as much work as you do, especially @TurdPooCharger, @BpyH64 and @ihaveamac. Sorry it takes a bit long these days to get back, real life stuff is getting in the way. If there's anything that needs my attention, please tag me. And, once that puzzle is solved it will get an implementation in GM9, of course. @BpyH64 - I wish I could understand russian. Google Translate fails for that page. Does that page contain any info we're still missing? |
@BpyH64, you magnificent bastard. Guys, what he stated is true! I'm getting CMACs matches for my F-ZERO SNES VC as per his instructions. When you PMed me at GBAtemp, I didn't replied back because I've given up pursuing this research topic by then. Dejection set in after spending days failing to guess the CMACs message formula for the game and e-manual Dude, I'm in tears. Thank you so much for solving this mystery. 😅 The boxed green 0x100-103 portion is 01 00 00 00 for the e-manual. |
The main applying is to install a 3GB game in 5 second. Game install and encryption on a PC and then copy on SD card |
@TurdPooCharger I’m lost on your last sentence. We could transfer the Nintendo 3DS folder years ago without even dealing with crypto. Just a small edit of movable.sed was needed, and that was it. |
@urherenow. That edit done to the Now that it's known how all those CMACs are derived in the The 1st half of the 0x30 KeyY is directly related to the LocalFriendCodeSeed_B. I think there's an aspect where having an invalid 1st half of that KeyY restricts access to Nintendo online services like the eShop... Also, it would not be wise having multiple people sharing the exact same KeyY if they intend to connect online at the same time. Changing the KeyY also means forfeiting your personal user info like the linked NNID. Other than using EmuNAND to set up another NAND profile, that also means giving up the Nintendo 3DS folder you're currently using when adopting someone else's. With the CMAC hurdles solved and out of the way, we get to have our cake and eat it too! |
Cool. The method I speak of involves no keys though. You zero out a certain spot, and the 3DS treats it like a system transfer (correcting everything on its own). I had written a tutorial on maxconsole back in the day, before we had decrypt9 and such. Edit: found it. If your movable.sed size is 0x140, you trim it to 0x120, then you just zero out the 4 bytes next to SEED (the second 4 bytes of the file). My original goal was to get my Mii Plaza data back, but after the whole thing, it could also read the SD files (and I could, in fact, move the SD data back and forth between both consoles, iirc). |
@BpyH64, thanks. I will make correction to the yellow subfield in that diagram. There is an exception to that it being |
New extra fast game installation method for 3DS |
@BpyH64, can you clarify what happened in the video? This is what I'm getting...
If I'm not mistaken, the reasons for those dummy
That's freakin' ingenious, man. 😃 |
That's pretty much the same thing that I would do for cia importing without reversing the dbs (Re-encrypt and copy the content over, then install the ticket and content IDs). Nevertheless this is awesome👍 |
If you create empty files title.db and import.db by size 3269632 bytes.Through godmod9 copy in SYSNAND SD/dbs/. |
Installing those dummy CIAs has a drawback. It records the wrong amount of blocks in Data Management and total size in FBI for each title. Everything else works fine as far as testing goes. |
One more thing to figure out is how the CMACs are generated for DSiWare titles. It doesn't seem to be generated the same way as an NCCH content. |
It seems CMACs for contents in twlnand are generated differently, since even NCCH contents there have different CMACs than expected. |
https://f.secretalgorithm.com/WzY2y/godmode9-v1.8.0-60-g87d4152d-20190610163307.zip Here's a testbuild of GodMode9 with the ability to check (and fix, of course) CMD CMACs. Testing is appreciated, especially in batch mode (It's the menu of the A:/B:/2:/5: drives). |
@d0k3 What do I risk in case the .cmd fixing goes awry? Titles vanishing from the Home Menu? |
@DMSalesman The title's banner would stop showing up, and the title wouldn't launch. Copying back a clean backup of the |
@aspargas2 Good. I'll manually bork some .cmd files, and let you all know how the fixing goes in the next days. |
I didn't test in batch mode, but I got some interesting results. Detecting corruption and fixing the .cmd mostly works, if the corrupted bytes aren't in the range 0x00 to 0x0F. Most specifically: |
Note that the some (all?) of the bytes in that range having incorrect values but a correct cmac will have the same symptoms as an incorrect cmac. Also note that the bytes in that range are the only bytes in the cmd file itself that influence any of the cmd file's cmacs, and only one cmac is influenced by them; the other cmacs in the cmd file are influenced only by the IDs, indexes, and NCCH headers of the contents. |
@DMSalesman - wait, you edited the offsets from @0x00 to @0x0f? Of course we can' recover from that, that's the CMD header. We only fix CMACs, we can't fix the whole file. If you edit the byte @0x02, corruption is detected for me (of course, the CMAC @0x10 is based on that). Bytes 0x04 to 0x0B contain two very important numbers. If they are incorrect, we can't understand the file, thus we cannot calculate the CMACs. |
@d0k3 So, provided that I spare 0x00 to 0x0F, I can meddle with everything else, correct? I'll whip up some script to do the testing for all the titles in the SD, then. |
I'll join in testing later.
|
@DMSalesman - well, no. We can only fix CMACs, and there are still the title id lists starting @0x20. There's a description of the CMD format here (use CTRL+F): "0x02: corruption not detected , game behaves as if .cmd was corrupted;" @TurdPooCharger - looking forward to your results. Maybe also check if stuff still runs. |
@d0k3 That's odd. From 0x20 onwards, GM9 was able to detect and fix the CMAC, and the checksum matched that of the original, working .cmd file. Edit: my mistake, will skip those bytes. When I changed the value at 0x02, GM9 said that the CMAC was valid. After rebooting into the Home Menu, though, the title's banner was missing and launching it resulted in a "card removed" exception. Edit: disregard the bit about 0x02, I can't reproduce it anymore. |
Tried to fix 55 CMACs belonging to titles, DLC and updates, worked perfectly. Every title's banner is displayed in the Home Menu, and they seem to launch fine. |
@DMSalesman - thanks! Well, the bytes starting at 0x20 may be a little more complicated. Depends on where you introduce corruption. I'm pretty sure not everything can be fixed. For example, try to corrupt every byte starting from there. |
@d0k3 I paid a look at the research you linked, so I just corrupted the actual CMACs (0x10 to 0x20, and places near the end of the .cmd). Seems interesting as a topic to research, but I don't quite go hand in hand with crypto, myself. |
I picked a variety of CIAs including homebrew apps, eshop exclusives, 3DS roms converted to CIA in both 3dsconv & GodMode9, official Virtual Console (except SNES), and several titles with accompanying DLCs and updates. Homebrew (as-is, no encryption)
To test if @d0k3 took the encrypted vs decrypted NCCH header to heart, Decrypted List
Encrypted (Re-encrypted with GodMode9)
homebrew: 14 ResultsAs stated in my previous post in how I would go about testing with both n3DSXL and o2DS, etc.,
Oddly enough, Animal Crossing - New Leaf Welcome Amiibo, is a known title with Secure Value where the save did successfully cross without auto-deleting. Reset assistance with FBI or Checkpoint was not performed. I chalked this incident as a one time thing since the o2DS did not have a user profile made.
Other Observation |
More info about the GBA VC save files. |
Alright, closing this as solved. @TurdPooCharger - if you want to, open a seperate issue for those saves that didn't transfer properly. Thanks everyone involved, especially @ihaveamac, @TurdPooCharger, @DMSalesman & @BpyH64 ! |
CTRNAND transfer works by transfering the contents of
1:/title
and1:/dbs
, then fixing the CMACs for all data in these dirs. On CTRNAND only, the.cmd
file can be ignored and all other relevant CMACs have been figured out. This is the reason why a transfer works for CTRNAND, but not for SD installed contents.To make transfers also work for SD installed contents, we need to figure out how the CMACs contained in the
.cmd
file are generated.The text was updated successfully, but these errors were encountered: