Here's what we know about how Gateway works...

Discussion in '3DS - Flashcards & Custom Firmwares' started by yifan_lu, Aug 12, 2013.

Thread Status:
Not open for further replies.
  1. yifan_lu
    OP

    yifan_lu @yifanlu

    Member
    663
    1,387
    Apr 28, 2007
    United States
    I'm writing this since I see a lot of speculation and incorrect information floating around about how gateway works. I'm going to post what I've learned from the smart guys in #3dsdev in a hopefully more understandable manner. The information I have is definitely not complete, and unfortunately may not be 100% accurate. I do not take any credit in this information, it's mostly learning from people like megazig, plutooo, and yellows8 in the chatroom. I just hope to make it more understandable.

    First, for people who don't understand what a ROP payload is, here's a quick explanation. Usually for buffer overflow exploits, you overflow some buffer to overwrite a return address in memory. Typically, the hacker will write "shellcode" which is compiled ARM code and inject it using the same buffer overflow. The return address overwritten will be directed to that memory location and now you're running your own code to do whatever. With the introduction of XN (execute never) in modern computers, this is not possible because memory locations that can be executed (game code for example) are read-only and only the kernel can write to it. This leads to ROP, which, instead of injecting one return address to point to shellcode, you inject a bunch of addresses (anywhere from 20 to 100 or more) that point of bits and pieces of code that already exists in the game (or in this case the System Settings application) and effectively do what you want. A (overly) simple example, say an existing function sums 100 numbers. You can use the existing code to add two numbers by pointing to somewhere in the middle of the function.

    So here's an overview of how Gateway works.

    * Quick layout of the DS mode NVRAM: there are two "profiles" stored in the DS NVRAM chip, these profiles store the message, profile name, birthday, etc. You can read it on the nocash docs. Here's an excerpt on why there's two of them: "When changing settings, the older area is overwritten with new data (and incremented Update Counter). The two areas allow to recover previous settings in case of a write-error (eg. on a battery failure during write)."

    1) The DS gateway installer overwrites the message length field of the second profile to be 110.
    2) The installer writes an ROP payload to 110 offset from the message field of the second profile. This happens to be in an not-used area of the profile, so the DS mode still boots and doesn't detect corruption. However, this not-used area is very small (0x86 bytes) and is not enough for code to decrypt and load Loader.dat. Instead this payload is in charge of loading the entirely of profile 1 as another ROP payload, and jumps to that.
    3) It then writes another ROP payload to the first profile. This second stage loader is responsible for 1) decrypting Loader.dat, 2) loading Loader.dat into memory, 3) jumping to Loader.dat
    4) It then fixes some CRC16 redundancy checks for the modified settings, and changes the update counter so profile 2 will be used (remember, profile 1 is completely "corrupted" with the ROP data. this is why you need to rerun the installer after using DS mode because the 3DS will detect this corruption and reset the profile).

    Now, the user reboots into 3DS mode and visits the DS profile settings. When this happens, the message will be loaded into memory; overflow, and the first stage ROP payload runs loading the second stage payload, which then decrypts Loader.dat (another ROP payload) which does all the cool stuff.

    Here's some things yellows8 found out that explains why the DS payload is so "small":
    1) Loader.dat is encrypted using AES-128-CBC with an IV value of all 00s.
    2) The encryption key for Loader.dat is actually the first 16 bytes of the system menu code found at 0x00100100. This means that the Gateway people don't need to store the decryption key anywhere in memory, it already exists when the system menu launches.
    3) Same thing with the file name "Launcher.dat". Any programmer would question how they're loading "Loader.dat" into memory and not have that string appear in the installer. The answer is that that string exists as "SYS:/Launcher.dat," an unrelated file that exists in the 3DS system, but the string was modified to point to a file in the SD card to open. Not really interesting exploit-wise, but interesting programming-wise.

    Now the Launcher.dat payload is NOT ARM code, it's still a very large ROP chain. According to yellows8, most of the ROP chain is obfuscation.

    Speculation
    Those are the facts. Now, some commentary from me.
    First, although I despise piracy and disapprove of groups like Gateway that hack for profit, I still admire the work they did which most likely involves:
    1) Physically dumping the RAM (as neimod did; if you saw those flickr pictures, you'll see how much skills it takes to build such a contraption)
    2) Analyzing the dumped code for user level exploits and kernel level exploits. They might have cheated with the DS profile exploit since that was on 3dbrew for months, but still a feat to get it working.
    3) Getting ROP gadgets (they might have had a tool or built a tool for this) and creating the ROP chain. I admire their obfuscation techniques like using existing system code as a decryption key. This is also the first time I've ever heard of code obfuscation in an ROP chain.
    4) Creating the kernel patches.
    5) Creating a physical card and patching the kernel in a way that hinders the cloning of their physical card.

    The rush to market may also explain their promises of more firmware support in the future. Remember, ROP can only use existing code, and this code changes from firmware version to version. For something like 4.1 - 4.2, the code may not change that much; but for something like 5.0, even if the UI is the same, the code could be very different. In order to make sure all their obfuscation works, they can't simply find new gadgets and rebuild the ROP chain. neimod also said that the kernel exploit has been fixed after 4.5; but if that is the case, the GW team should not make promises.

    The other thing I both admire and curse the GW team is that it is impossible to analyze their work without hacking the 3DS ourselves. In order to see what code is being run, you need to dump the 3DS memory (or have a decrypted NAND dump, which usually requires dumping the memory to get keys). For experts like neimod and yellows8, who already have such dumps, it would be a matter of deobfuscating the ROP chains to figure out the exploits and such, and I suspect they already know; but they are unwilling to share and help additional piracy and I respect them for that. For enthusiasts like me, or clone companies, it makes our lives harder as the time and investment spent decoding the GW might be close to the time and effort spent cracking the 3DS from scratch, since the hard part is dumping the RAM and decoding/figuring out the code. By using ROP, even if they didn't obfuscate it, there is no easy way to figure out what is happening. Unlike stuff like the R4 or the PSJailbreak, where the security is in the shellcode, most of the security is based on the 3DS security itself. Effectively, the 3DS security that prevents hackers from seeing the memory is also preventing people like me from seeing how GW works.

    On what the GW is theoretically capable of (on 4.5 at least since neimod said the kernel exploit is patched above that). Since it has kernel access, you can think of it like what the PSP lite custom firmwares are able to do. Although you have to rerun the exploit on each reboot, you should be able to do anything from loading unsigned code (homebrew) to theming the system menu to cheats and etc. Unfortunately, as it currently stands the only people who knows how GW works are the GW group themselves and #3dsdev hackers like neimod and yellows8. We can only hope that GW will work on getting homebrew to run and other nice stuff instead of just focusing on getting pirated ROMs with better and better compatibility to work.
     
    cearp, dot7z, RubenCantuVota and 20 others like this.
  2. RenegadeKid

    RenegadeKid GBAtemp Fan

    Member
    446
    138
    Aug 11, 2013
    France
    I honestly think this should be kept secret. Even if Nintendo already got the Gateway, giving them more information is not a good idea imo.
     
  3. indask8

    indask8 New Member Forever

    Member
    987
    119
    Apr 19, 2007
    France
    Look at the Flag...
    If this guy has access to #3dsdev so does Nintendo devs so I don't think that'll change anything, pretty sure the exploit was patched as soon as the files were released on the gateway website (possibly even before), and said patch is currently under testing and will be released soon (or maybe they'll wait and add a feature that makes updating worth it).
     
    cearp and RedCoreZero like this.
  4. Snailface

    Snailface My frothing demand for 3ds homebrew is increasing

    Member
    4,324
    1,983
    Sep 20, 2010
    Engine Room with Cyan, watching him learn.
    The gateway's already patched with v5. They can lock out any chance of a kernel hack update for > v6.2 by patching the mset hack (the one that runs on the DS card).
     
  5. yifan_lu
    OP

    yifan_lu @yifanlu

    Member
    663
    1,387
    Apr 28, 2007
    United States
    All nintendo has to do is pop the run the gateway installer on a development 3DS with a debugger attached. It will take them an hour max (and that's only because nintendo SDKs are hard as hell to set up). They don't even need the red/blue card.
     
  6. indask8

    indask8 New Member Forever

    Member
    987
    119
    Apr 19, 2007
    France
    Look at the Flag...
    That's what I meant, now that the files are available, they can easily patch the DS part of the exploit.
     
  7. RenegadeKid

    RenegadeKid GBAtemp Fan

    Member
    446
    138
    Aug 11, 2013
    France
    OK, but look dudes, Nintendo always stated that the 3DS will be never hacked... So I think the Gateway team is much more clever than the developpers of Nintendo.... I'm not an expert, but if the deloppers of Nintendo were that clever, they would have patched the vulnerabilities much before. But they only did on 5.1.
     
  8. naddel81

    naddel81 GBAtemp Advanced Maniac

    Member
    1,521
    186
    Dec 14, 2009
    United States
    why should Big N actually care? they patched the Gateway out of existence in 5.X and I am sure they do not give a damn about a few 4.5 users with the possibility to pirate as there is no possibility known to downgrade.
     
  9. naddel81

    naddel81 GBAtemp Advanced Maniac

    Member
    1,521
    186
    Dec 14, 2009
    United States
    now is the time that Gateway team can show they got what it takes. If they really make it compatible for 5.X and above then I'll certainly buy one (even though I can afford the games and own 10+ games up till now).
    if they are not able to make it work I'll not buy their product.
    and they already know that so you can bet they are at least trying to make it work on 5.x and above since that is their selling point nr.1!
     
  10. RenegadeKid

    RenegadeKid GBAtemp Fan

    Member
    446
    138
    Aug 11, 2013
    France
    yes, but who knows? Maybe Nintendo will make other mistakes and will let other vulnerabilities in the future updates, and I don't mean like on 6.3... but maybe on 7... or maybe never, but anyway, the Gateway is still worth it imo and is a great flashcart, I just got mine yesterday and it rocks:) I just updated my 3DS to 4.5, it works like a charm^^
     
  11. yifan_lu
    OP

    yifan_lu @yifanlu

    Member
    663
    1,387
    Apr 28, 2007
    United States
    The people who built the titanic claimed it would never sink. Were they stupid? No, just prideful. Also, when did Nintendo claim that?

    The engineers at Nintendo are pretty smart. More smart than you or I. Lets try not to claim we know their motivations.
     
    kenjixx, justapei and McHaggis like this.
  12. json

    json MUSCLEMAN

    Member
    693
    781
    Aug 9, 2013
    Burkina Faso
    Sucks for you, but those of us that are on 4.5 are FREEEEEEEE to do whatever we want!! Just let Nintendo try, but with kernel access everything Nintendo does can be undone.
    So glad I did not update :yaysp:
     
    yuyuyup likes this.
  13. the_randomizer

    the_randomizer The Temp's official fox whisperer

    Member
    22,691
    10,961
    Apr 29, 2011
    United States
    Dr. Wahwee's castle


    Thanks for rubbing it in, we truly appreciate itB-) Best of luck using newer games that require firmware updates:creep:
     
  14. RenegadeKid

    RenegadeKid GBAtemp Fan

    Member
    446
    138
    Aug 11, 2013
    France
    Can't remember where I saw this but Iwata said that once.
     
  15. json

    json MUSCLEMAN

    Member
    693
    781
    Aug 9, 2013
    Burkina Faso

    Didn't you see the FW spoofing ? GW already showing that they can beat Nintendo at the update game. I have no doubt that ANY future game CAN and WILL be supported
     
  16. the_randomizer

    the_randomizer The Temp's official fox whisperer

    Member
    22,691
    10,961
    Apr 29, 2011
    United States
    Dr. Wahwee's castle
    Well, they weren't smart enough to make the Wii hack-proof and pretty much shot themselves in the foot, letting hackers make the Wii impossible to be updated with Priiloader, bootmii, etc ;)



    Because that clearly helps those who inadvertently updated a few days prior to this major announcement, but I digress. What happens to those people? It would be unfair to exclude them, because the GW team will eventually find a universal solution :P Disabling the WiFi would be a good first step for those who made the mistake.
     
  17. RenegadeKid

    RenegadeKid GBAtemp Fan

    Member
    446
    138
    Aug 11, 2013
    France

    Same for the Wii U, we can already play Wii roms^^
     
  18. json

    json MUSCLEMAN

    Member
    693
    781
    Aug 9, 2013
    Burkina Faso

    I am sure GW team will eventually find something for 6.x, have faith! Just for the love of god don't update anymore! Hold strong! :)

    Or you can get a 4.5 and enjoy this right now :grog:
     
    the_randomizer likes this.
  19. the_randomizer

    the_randomizer The Temp's official fox whisperer

    Member
    22,691
    10,961
    Apr 29, 2011
    United States
    Dr. Wahwee's castle
    RenegadeKid Lolwut? I'm confused at the picture. What's that got to do with those who can't use it with 6.x firmware?:blink: Why did you quote me again? :wacko: Please elaborate why you posted the picture in response to my concerns.....


    Can't really afford to spend that much money for a while, I just got a 3DS XL by trading in my regular 3DS (and paid the difference, but that system too wasn't 4.x.x), but I'll turn off the WiFi/internet connection and won't get any new games until the new versions are supported ;)

    Besides, there's no way to tell if it's 4.5.x before getting it.

    BTW, why the crap did RenegadeKid quote what I said with a picture?

    Edit: ninja'd
     
  20. Pong20302000

    Pong20302000 making notes on everything

    Member
    8,076
    1,933
    Sep 8, 2009
    One's inner self
    Gateway still need to solve the NAND saving as im sure new nintendo games will opt for that to make it harder for them
     
Thread Status:
Not open for further replies.