Tag Archives: Nintendo 3DS

파도타기 피카츄 배포

이번에 포켓몬스파에서 파도타기를 배운 피카츄를 배포한다는 광고를 보았습니다.

그런데 멀기도 멀고 가격도 매우 비싸네요.
그냥 만들어야겠습니다.

예전에 뚫어놓았던 npfl 서버의 선물목록을 확인하니 미리 테스트 중이였는지 이미 올라와 있었습니다.

2016-07-01 04_50_32-Mozilla Firefox

뭐 일단 예전에 제가 포켓몬스터 선물 배포서버에 대해 올린 이후로 별다른 소프트웨어 업데이트도 없었기에 예전과 같은 방식으로 배포를 진행하겠지 생각하며 선물 파일을 다운로드 받아 사설 선물서버에 올렸습니다.

IMG_9924

그런데 이상하게도 소포가 없다는 말만 덩그러니 남긴 채 선물을 받는것이 불가능해 지더군요

이것은 제가 처음 작업을 시작할 때에는 전혀 못 보던 현상이였습니다.

그래서 좀 더 알아보기로 했죠.

사실 한참동안 서버와의 프로토콜을 분석한다고 시간을 허비했습니다.

전에 제가 언급했던 서버 말고 npul 등과 같은 많은 서버들이 3ds와 교신하기 때문에 혹시나 그쪽에서 별도의 인증 과정을 거치지는 않나 싶었습니다.

그리고 한가지 파일을 비교하면서 찾아낸 것이

2016-07-01 04_59_56-Binary(fast) Compare - UltraCompare Professional

그때 당시에 받은 선물 파일과 지금 다시받은 선물 파일이 다르다는 것이였습니다.

조금 놀랐죠, 단순히 정적일것 같았던 파일이 그때와는 다른 형식을 취하고 있었습니다.

예전과 같이 AES-CTR로 복호화를 해 보았습니다.

2016-07-01 05_02_24-Binary(fast) Compare - UltraCompare Professional

앞의 헤더값이 틀려지면서 뒤에 암호화된 부분이 완전히 달라집니다.

몇 개의 파일을 돌려가며 분석해보니 맨 앞의 비트값에 따라 게임이 인식하여 선물이 비활성화 되는 듯 합니다.
ORAS는 해당 값이 C5이여야 선물을 받을 수 있습니다.

하지만 BOSS형식 파일 내부의 포켓몬 선물 데이터 자체도 닌텐도가 서명을 하기때문에 임의로 내용을 변경하면 게임 자체에서 오류를 내고

어느 단계에서 선물의 활성화 비활성화를 결정할지 생각해 보았습니다.

문득 npdl 서버에서 선물 파일을 다운로드 받을 때 보내는 요청문이 떠올랐습니다.

2016-07-01 05_11_16-C__Windows_system32_cmd.exe

npdl 서버로 요청을 보낼 때는 변형된 http 프로토콜을 사용하는 데 유저에이전트에 상당한 양의 정보를 포함시킵니다.

그래서 여기서 아마 재인증을 시도할 것이라 생각하고 그 값을 실제 3ds에서 나온 값과 동일하게 맞춰서 파일을 다운로드 하는 클라이언트를 만들어 보았습니다.

2016-07-01 05_17_32-Binary(fast) Compare - UltraCompare Professional

예측은 정확했습니다.

유저에이전트를 조작해주자 그에 맞춘 선물 파일을 서버가 되돌려 줍니다.

이제는 게임 종류 언어 종류에 따라 해당하는 기기에서만 활성화가 되도록 즉석에서 파일을 조작하여 전달해 주는 듯 합니다.

분명 예전에는 그렇지 않았는데 지금은 이런 식으로 바뀐 것을 보면

닌텐도에서 제 이전글을 보고 나름대로의 조치를 취한 것으로 생각되기도 합니다.

예전에는 단순히 클라이언트 인증서만 확인했기 때문에 ssl 인증이 성공하면 선물파일의 캐쉬를 무한정 생성할 수 있었는데

이제는 게임별 기기별로 파일을 다 가지고 있어야 하니 아마도 그것을 방지하기 위한 것이 아니였나 생각이 듭니다.

IMG_9925

삽질 끝에 이제 선물 받기를 시작합니다.

https://youtu.be/yS5n1_5oI_Y

여튼 파도타기를 배운 피카츄를 받는 영상입니다.

중요한 피카츄의 스탯은 이렇습니다.

IMG_9926

pkx 파일은 선물 배포일에 맞추어서 블로그에 따로 업로드 할 예정입니다.

매너티는 최고 존엄입니다.

Hacking Pokemon 6th gen Mystery gift server

While receiving the Hoopa gift from the 18th Pokemon movie series, I suddenly felt like hacking into how this entire mystery gift process works.

I looked up on GBATemp and Project Pokemon but interestingly I could not find any technical insights on it. People had pretty much no progress after capturing the encrypted connections.

So, challenge accepted.

1

First, I went down to the basics like everyone else; Packet capture.
I used my laptop as a bogus wifi network to capture the packets from the 3DS

The 3DS was communicating with Nintendo servers with SSL as I expected.

2

I guess that it might be storing the certificate chain in the game file, as far as I know Wii games store their certificates within the game file.
I wasted a day trying to decompile the rom filesystem and searching for any meaningful information using grep. But eventually I found out that the certificates are not stored in the game file but it is embedded within the firmware SSL module.

3

I looked up for the ID of the SSL module and extracted the cia file from nintendo update server.
As all the update cias are encrypted, I needed to decrypt them using a 3DS.

4

Using Decrypt9, I retrieved the decrypted CIA file.
Lets get into the actual data using ctrtool.

5

Extracted the CXI portion from the dumped CIA file.
Now we need to get it splitted into proper partition.

6

I extracted the executable code from the exefs.

Lets take a look…

7

It is compressed with reverse LZ77 so lets uncompress it again.

8

After a bit of skimming I located the actual root certificate used for connection to 3ds-fushigi server.

Thank god Nintendo didn’t use complicated cert chain. As the root certificate is directly used for the communication we replace that and the work here is done.

The format seemed to be DER so I carefully exported the corresponding bytes.

9

Successfully retrieved the certificate in one shot.

Now lets create a certificate that is almost the same except that it will use MY private key.

10

Using OpenSSL I created the copy of the certificate, self signed it and converted it to DER format again.

11

I replaced the certificate in the code binary with the new one then I recompressed the code.bin with reverse LZ77.

12

But the header of the exefs requires the hash of the code.bin file, as I could not find any software that does the job for me I ended up patching the hash myself.

Then I redid the entire CIA decompiling process in the reverse order to create the new modified CIA file.

I installed the new CIA file onto the 3DS using devtool but it half bricked the 3DS upon reboot.

13

This is what a half bricked 3DS looks like. there is no menu on the bottom and it refuses to execute any of the softwares.
Trying to run Camera app using L+R freezes the console.

I wasted another 2 days trying to figure out what is going wrong, then I eventually found out that encrypting the CXI in the CIA file and using a sigpatched firmware could get around the problem.
By the time I worked on this there was no software capable of doing this but now one can use Decrypt9 to encrypt the CXI.

Anyway as the patched certificate is now in position we are moving onto server setup.

I changed the DNS settings of my 3DS and used redirected 3dsX-fushigi.pokemon-gl.com to my server’s IP address.

14

15

I created the certificate for secure https using IIS.

Then I started the mystery gift from the 3DS to see what happens.

Looking into the log I found that the 3DS was sending a request using a non-standard http format.

2016-02-03 12:07:40 *.*.*.* POST /api/serial.auth – 443 – *.*.*.* – 500 0 64 60

So I coded a secure proxy to dig into the detail.

16

This is the request sent from the 3DS for validating the gift code with Nintendo servers.
It contains the player’s GameSync ID, language settings, rom type, country code and etc.
Also the gift code in plain format, in this case 1111222233330000.

At this point I thought pretty much everything is over… and I was wrong.

17

A simple diagram for non-techy users.
I used a rogue DNS to redirect the traffic to my server and my server is behaving as the Man-in-the-Middle.
But strangely the response from the Nintendo server did not contain any information about the gift Pokemon.

18

This is the original response from the fushigi server.
I used a couple different working keys for comparison and discovered that it only tells the 3DS if the code is correct or the gift number on successful validation.

19

I did the packet capture again to see what it does after getting the response from fushigi server.
After disconnecting from fushigi the 3DS was connecting to npfl.c.app.nintendowifi.net.

But unlike fushigi, npfl server requires client authentication using a clientside certificate.

20

The client SSL certificate resides in a module called ClCertA

Extracting the certificate is done the same way as extracting the root certificate. But unlike the latter the client certificate is encrypted with AES-CBC, but thankfully we have a tool for that purpose.

21

Successfully extracted the certificate and key from the module

22

let’s convert the certificate into a more PC-friendly format.

23

Importing the certificate into the browser now enables us to impersonate as legit nintendo 3DS’s

Now let’s see what the server has to show us.

24

I see that the NPFL server holds the list of available gifts.

If given a gift number it will output the corresponding gift name for that number, if no input is given it shows all the available gifts.

File names are pretty much self-explanatory, although there are a couple of confusing names like M18KO_A, M18KO_HU. Which I guess means Arceus and Hoopa for the 18th Pokemon movie in Korean region.

One interesting thing to note is that when a 3DS connects to this server, it displays “Searching for gift…” message. It is _literally_ doing it.

If NPDL server gives a response and 3DS does find a gift, it will then connect to NPDL server to download the actual gift file.

25

To summarize, The process is like the diagram above.

I coded a program for simulating each of the servers, but I discovered that NPFL server is using a different certificate chain than others. It is authenticated using Nintendo G3 root certificate as opposed to other servers using Nintendo Class 2 cert. I can also replace the G3 certificate in the same way I did for Class 2, but as G3 certificate is also used for connecting to NASC and NPPL server, which is used to authenticate the nintendo 3ds and the cartridge itself everytime it tries to connect to the internet. I couldn’t figure out a way to emulate those servers due to their cryptic response format, so I ended up coding a secure proxy server that redirects all the information to the legit servers.

26

So this is the final diagram.

fushigi, npfl and npdl servers are accessed consecutively, so there is no problem running everything on a single server. But nppl and nasc servers are accessed randomly during connection, somethimes even simultaneously so they must be ran on different machines.

Anyway now I proceed to analyzing the output of the npdl server.

27

Despite so many steps it is still encrypted…

28

It is the BOSS format used in SpotPass, but to this date nobody bothered to document anything about the service.

29

But thankfully the specs for the header are documented, I coded a boss format decrypter from the information.

30

It is encrypted almost the same way as the client certificate execpt that it used AES-CTR and different set of private key. I coded another homebrew that is capable of decrypting AES-CTR from the raw data. the starting bytes of the CTR is the 12 bytes following the header plus 1 in big endian, so for this file the starting bytes will be 58 9A A6 73 5B EA D2 47 99 63 80 42 E7 01 00 00 00.

31

Presto! the gift is successfully decrypted. Although the actual format of the gift I will investigate later.

Now finally we can give ourselves a gift…

32

This is Amaura which my servers are gifting to my nintendo 3DS, with an arbitary gift code of MANATEE.
But as the gift file itself is also signed by nintendo, I guess there will be more stuff to be done before we can start creating our own giveaway pokemon.

The source code of the server emulators and AES-CTR decryptor will be released soon.