# research ## DS / DSi emulators wanted features: - **DS and DSi version emulation (DSi for later)** DS version of pictochat can still receive images with rainbow drawings, but doesn't support sending - **firmware booting** the phat DS and DS lite's firmware had pictochat built-in instead of as a standalone app like on the DSi. to launch pictochat, firmware booting must be possible. - **local multiplayer network emulation** required for testing sending and receiving - **local multiplayer packet capture** required for reverse engineering the packet format - **local multiplayer packet injection** required for testing forged packets - **open source** DIY packet injection anyone? - **linux-compatible** just a preference - **debugging (ideally using GDB or similar interface)** if all else fails, reading and/or injecting packets into memory using a debugger might be the only option for testing forged packets. a debugger could also help with reverse engineering the decoding process of a pictochat packet/message ### [DeSmuME][desmume] - phat ds only - no firmware booting - no local multiplayer emulation + open source + cross-platform - no debugger DeSmuME is focused on high quality/performance emulation for single player titles, and actively rejects requests for WiFi emulation. ### [melonDS][melonds] + ds and (experimental) dsi support + firmware booting support + local multiplayer emulation + allows patched-in or real network packet capture using loopback network adapter and wireshark + loopback network adapter also (probably) allows injecting forged packets + open source + cross-platform - no debugger melonDS is a very new emulator that is still actively being developed. melonDS is focused on accurate emulation and is the only one on this list with sufficiently advanced local multiplayer emulation. ### [no$gba][nogba] + supports DS and DSi games + firmware booting support - no local multiplayer emulation - closed source - windows only + very advanced debugger [toolchaingenericds]: https://bitbucket.org/Coto88/toolchaingenericds/src/master/ [nogba]: https://problemkaputt.de/gba.htm [melonds]: https://melonds.kuribo64.net/ [desmume]: https://desmume.org/ ## MelonDS hacking source: - melonDS @ Config > Wifi settings "Local multiplayer features do not use the same network protocols as online play" - comment @ src/Wifi.cpp:46 "multiplayer host TX sequence" - references to `RFTransfer_Type{2,3}` @ - nintendo ds ni-fi protocol @ - melonDS emulates actual 802.11b frames - the protocol does not appear to be encrypted: ![](../assets/ws-no-encrypt.png) the string `lork` is visible as plain text in the hexdump (offset 0x0056), which appears to be some kind of 16-bit encoding of the username set on the emulator used to capture these packets - The messages are not sent as single packets. The nifi protocol appears to set up a constant stream, and messages are sent across multiple frames. - PictoChat does not appear to send messages when you are in a chat room by yourself, so local multiplayer / nifi emulation is required for capturing message content ### Message sizing/cropping - PictoChat automatically crops messages to the smallest height (at the fixed intervals shown as notebook lines in the edit field on the bottom screen). Message content can be on the notebook line itself without causing the cropping algorithm to 'allocate' more space; the allocation only happens if any of the pixels on the line *below* the colored line are used. The method used to crop the messages also ensures that the username label in the top left does not obstruct or remove any content. ![](../assets/pictochat-msg-height-1-4.png) ![](../assets/pictochat-msg-height-5-draw.png) - Message content is likely only cropped when displaying ![](../assets/pictochat-msg-crop.png) ![](../assets/pictochat-msg-crop-lork2.png) The above image was obtained after the following steps: - Fill in the top row of the message with the black pen, ensuring the entire notebook line is colored in as well - Send message (displayed as top message) - Copy message - Erase small portion of black area on the right side (displayed as bottom message) Notable observations: - The message content has a 1 pixel border (padding/margin) on all sides (except the bottom and corners) that can not be filled in. The bottom border is correctly displayed for received messages, but the edit field has an additional row of pixels that directly touch the message border on the bottom. - When any message is cloned, additional pixels that were masked on the top screen become visible again in the edit field. This includes the bottom row of pixels, as well as the two rows of pixels shown in the single line message picture. - The drawable area (including obstructed top-left corner) is 228x80 pixels ### Message content ![](../assets/pictochat-msg-pattern.png) ![](../assets/ws-msg-pattern.png) - All messages with interesting content have NIFI header type 1 (CMD). - PictoChat messages appear to be sent over frames of length 0xf6 (246) regardless of actual message size. - All frames appear to be sent exactly 5 times. 'New' frames have a value of 0xe004 at offset 0x0026, while resends have a value of 0xf000 instead. - There are a lot of messages with length 0xaa (170), these appear to include a random 32-bit value at offset 0x0046. - The message content (suspected bitmap-like format) appears to be sent unencrypted (patterns of 0x0 and 0x1 nibbles clearly visible in hexdump). - 0x004d - 0x00ec appears to be used to send the actual message content: ![](../assets/pictochat-msg-crop-lork2.png) ![](../assets/ws-msg-fill-top.png) ![](../assets/ws-msg-fill-mid.png) ![](../assets/ws-msg-fill-bot.png) (excerpts from the start, middle and end frames of a completely filled message) - The completely filled message also shows an interesting pattern at the end, hinting at a slightly odd image codec #### Analysis ![](../assets/ws-msg-fill-mid.png) |offset|type|description| |-|-|-| |0x0000|`u32`|NIFI: Magic (0x4e494649)| |0x0004|`u32`|NIFI: SenderID (melonDS InstanceID)| |0x0008|`u32`|NIFI: Type| |0x000c|`u32`|NIFI: Length (after NIFI header)| |0x0010|`u64`|NIFI: Timestamp| | |0x0018|`u16`|PictoChat: 0| |0x001a|`u16`|PictoChat: (Resend???) (2=New, 0=Resend)| |0x0028|`u8[6]`|PictoChat: multiplayer CMD MAC (melonDS Wifi::MPCmdMAC)| |0x002e|`u8[6]`|PictoChat: sender MAC| |0x0034|`u8[6]`|PictoChat: sender MAC (again)| |0x004d|`u8[0xa0]`|PictoChat: Message data (encoding unknown)| |0x00ed|`u16`|PictoChat: (random???)| |0x00f0|`u8[6]`|PictoChat: (mac/id???)| ### Fiddling After editing melonDS source code to corrupt the local multiplayer message buffer in known content locations before sending the message to the FIFO buffer: ![](../assets/pictochat-msg-corrupt-lork.png) ![](../assets/pictochat-msg-corrupt-lork2.png) This shows a few important details: - Messages are not limited to the apparent 228x80 size (there is also message content behind the username label in the above screenshots, but the username label is on a different graphics layer) - The ordering of pixels in the messages is not reading order - Message content is not checked or validated in any way ## Unsure/notes - Is the endianness of the DS properly emulated? - Is the NIFI magic value also present in physical frames or is this something MelonDS did? - The DS implemented WEP(?) encryption for connecting to home network routers/APs, but is this encryption also used when the WiFi module is used in local multiplayer mode? Does this even matter inside the emulator?