r/programming • u/AyrA_ch • Aug 13 '17
Arbitrary code execution in Pokemon Yellow
http://tasvideos.org/5384S.html62
9
Aug 14 '17
Could this be pulled off on a hardware gameboy rather than an emulator?
22
u/AyrA_ch Aug 14 '17
In theory, yes. The almost processor cycle perfect inputs make it rather difficult though. You would need a device that allows you to single step the processor for the inputs.
5
u/pelrun Aug 14 '17
That's not as hard as you'd think, although you would need access to more internal signals than just the control inputs.
You can manage it with just the clock and the reset signal - control the reset signal to synchronise with the console, and count clock pulses to use as your master timebase.
14
u/Yuzumi Aug 14 '17
There's a GDQ TAS block from a year or so ago where they two or three consoles, one was the NESClassic I think, and rig it up so they use some for sound and are able to stream video to one (including a skype call) over the controller inputs.
edit: found it: https://www.youtube.com/watch?v=7CgXvIuZR40
3
u/RenaKunisaki Aug 14 '17
Two original NES consoles for sound (they don't even have stereo!) and a SNES for video.
I think they did NES Classic stuff another time, but nothing that crazy.
9
u/Daniel15 Aug 14 '17
From the article:
This run plays back correctly on a reasonably accurate emulator. However, it is unlikely this run will sync on actual hardware. However, what was done is deemed legitimate. With enough tweaking, a run with the same output should be possible on a real Gameboy Color. Since it does not actively exploit any emulator bugs,
Sounds like it's possible with some tweaking.
2
u/treefroog Aug 14 '17
In theory yes, but he would need to do some hardware hacking too since the instructions are written using the buttons to input code almost continuously throughout the video.
2
u/Steve132 Aug 14 '17
A hardware gameboy with a custom chip hardwired into the system clock and joypad/volume/button inputs, yes.
229
u/deadstone Aug 13 '17
There's a long detailed explanation of the whole video on its TASVideos page. My favourite part is the one about sound:
Portal credits
After the success of playing back GB game content using ACE, where the sound was merely a side aspect, I wondered how capable the sound hardware is, and what you can do with it. Sound in a Gameboy turns out to be very limited in its abilities. It has 4 sound generating channels that can be connected to two output terminals. The first two channels generate square waves of different frequencies and amplitudes, with limited control over frequency and amplitude over time, and the last channel produces static noise.
Only the third channel is interesting, as it allows arbitrary wave patterns to be played. However, the RAM that holds the wave pattern only contains 32 samples that are repeated over and over, with only 4 bits per sample (i.e. 16 different possible values). It was clearly not designed for complex sounds like voice, but rather as an alternative way to creating waves with unusual shapes. You can hear this clearly in the title screen of Pokémon Yellow, with the very crude sound they achieved by overlaying multiple waves: You can hear the words, but it's not pleasant.
However, you can use the third channel to play longer pieces of arbitrary audio, by managing to update the wave RAM while the sound is playing. This of course requires perfect precision when to update them, to ensure they are played once and only once. The sound can only be played at very specific frequencies of 2097152/x Hz, where x is an integer between 1 and 2048. For this to line up nicely with the Gameboy's frames, only specific values of x work, exactly multiples of 57. All arbitrary sounds in this movie use x=114, which results in exactly 2 samples played every 912 cycles, so it lines up perfectly with the line timings of the screen, resulting in a sample frequency of ~18396 Hz.
Still, the problem remains that there are only 4 bits available per sample, not nearly enough to produce acceptable-quality sound. But there's one more audio control we can abuse: the volume control. The volume control provides a linear scaling of the audio with 8 discrete levels. By adjusting the volume for each sample, we can use it to increase the resolution of different amplitudes that can be achieved, from 16 to ~100 (some sample/volume conbinations result in the same effective amplitude). These effectively possible amplitudes are not evenly distributed though, there are more values available for the small amplitudes than for the large ones (which is actually exactly what you want).
So, what this movie does to produce high quality sounds (for a GB that is), is writing the wave RAM at exactly 2 samples every 912 cycles to update the samples data, while also rapidly adjusting the volume control at exactly the right times to tweak the resulting amplitudes. These processes need to be time shifted by 32 samples, meaning that the volume control affects the currently played sample, while the newly written sample is only played 32 samples into the future.
This requires a lot of precision and cycle counting, and is performed by a special assembly function that is loaded with the initial payload, and fed the sound data using the joypad inputs as usual. In the idle times between two audio samples, it updates the tiles on the screen to render the accompanying text and pictograms, so it also needs to be synced up with the LCD operations to only write when the memory is accessible.
73
u/mirhagk Aug 13 '17
Man could you imagine if some game had done that in a game during the gameboys time? Full audio/video cutscenes? It would have blown people's minds.
80
u/Alphaetus_Prime Aug 13 '17
If you think that would have blown people's minds, check this out.
25
Aug 14 '17
I hate reading blocks of text in videos (always wondering if I'll have enough time before the text changes), especially when there are visual elements I don't want to miss. But definitely really cool stuff. I hope GameBoys become commonplace again and this virus gets a chance to make its rounds.
15
u/Alphaetus_Prime Aug 14 '17
The virus works with the 3DS virtual console release of Pokemon Red/Blue, so it could happen!
1
u/ThrowawayusGenerica Aug 15 '17
Is the virtual console cycle accurate, though?
1
u/Alphaetus_Prime Aug 15 '17
There are some emulation differences, but the virus was created with them in mind (or it was modified to accommodate them, anyway).
4
u/salgat Aug 14 '17
He said he would have to fix it so that it doesn't get wiped when the hall of fame comes up, so for practical purposes this virus wouldn't spread far unless it was intentionally done by victims.
5
u/jfb1337 Aug 14 '17
If this existed while the GB was in its prime I think it would have spread fairly far, if given to enough people to spread intentionally, since a lot of people trade pokemon more often than they enter the hall of fame.
5
u/salgat Aug 14 '17
You may not remember but in Red/Blue the only way to power level Pokemon was either glitching rare candies with MissingNo. or leveling through the elite 4. My friends and I were all either making new games or leveling through the elite 4 most of the time.
7
u/mirhagk Aug 14 '17
That's a really neat idea. I kinda hope someone does this with a modern game, just to confuse the heck out of the devs
3
u/maskedbyte Aug 14 '17
Usually not possible unless the game is programmed at a very low level.
1
u/mirhagk Aug 14 '17
Usually not yes, just as most software is usually not exploitable, but there certainly have been cases. But it certainly does happen where a buffer overflow or some careless development leads to an open exploit.
Also quite a lot of games are developed using C++ for performance reasons, which is extra susceptible to exploits (due to manual memory management)
1
u/maskedbyte Aug 15 '17
Arbitrary code execution without some sort of external software (like Cheat Engine) on modern games is in most cases, sadly not possible. Arbitrary code execution usually (I think, I haven't looked very far into it, something close to this though) works by taking advantage of the input bytes and using them as opcodes, since they were read into a byte or 2 on old game consoles. The way input is polled now is very different, it goes through multiple hardware / software layers and is read at a high level, usually with callbacks. Also, there's DEP on Windows.
1
u/mirhagk Aug 15 '17
Like I said, in most cases it's not going to be. But a buffer overflow could still allow for it. Or a sloppy developer could screw up the mod/command engine
6
u/Klathmon Aug 14 '17
Holy fucking shit. This is fucking incredible! I can't even imagine my little 10 year old head if someone managed to do something like this in the 2000
4
Aug 14 '17
How does the virus cause Mew to become accessible?
22
u/RenaKunisaki Aug 14 '17
Exploits transfer protocol to get code uploaded and executed
Hooks a routine in memory to be able to run in the background at all times
When game is saved, patches the map script pointer in the save data to point to a bootstrap routine
When game is started, it executes the map script (really just a function pointer) and thus loads the virus again
When player is in the dock map, at certain coordinates, has Strength, and presses the right direction, runs an animation and triggers wild Mew encounter
When player is connected to another player for trading, hijacks the link to propagate itself, then restores the normal link function to cover its tracks and not interfere with the game
2
16
u/Steve132 Aug 14 '17 edited Aug 14 '17
Yeah, but that would have been impossible. Remember, the way he's accomplishing it is by streaming the data directly off of the 'joypad' as a series of real-time 18000hz button presses and volume control adjustments. Even if you were data from star trek, the mere friction of this would reduce your GBA to a melted puddle of lead and plastic.
9
u/mirhagk Aug 14 '17 edited Aug 14 '17
Well for this situation he streamed it from the inputs, but could it be streamed from memory? It looks like there were up to 8MB ROM Cards which in theory is enough for ~7 minutes of this. I don't know what the memory speed was though. The only thing I can find at the moment is a comment which suggests it takes 400 ns to read from ROM. If that's the case that's more than enough speed, though it does seem high.
It certainly would require lots of magic and 7 minutes of cutscenes is tiny for a game, but people would have freaked over it.
EDIT: Wait nvm I realized it wouldn't work because the volume control was simply an input, not something controllable by games. So they couldn't do the trick he used to get decent sound out of it.
10
u/RenaKunisaki Aug 14 '17
The volume knob is only the master volume control; each channel also has its own volume set by the game. So it could certainly be done. You could even design a cartridge with a DMA controller inside that would turn all of "ROM" area into a FIFO, and have the CPU run a tight loop in RAM of just copying ROM to VRAM/audio, probably much faster than you can with the button inputs. (Some SNES games do similar things!)
The main limitations would be the cost of such big ROMs (and extra logic if you use the DMA method), and the amount of battery drain it would cause.
Also, Nintendo would have been more strict about letting games pull these kinds of tricks, because the hardware wasn't necessarily finalized. Relying on "unspecified" things like precise memory timings or behavior of unused registers meant your game might not work on a newer model if they changed something under the hood. Today though, it's pretty safe to assume there won't be a new revision of the GBC in the future.
4
u/mirhagk Aug 14 '17
Interesting.
I'm not entirely sure about nintendo banning those kinds of tricks however. Many video games relied on crazy hacks and tricks in order to squeeze every last bit of performance out of hardware. It would've been very hard to keep that in check. For instance on the SNES many games drew black lines on the right side of the screen in order to up their compute time, and this very much relied on the specific refresh rate of the screen. Nintendo seemed fairly accepting of giving games pretty large amounts of control, for instance allowing extending hardware through cartridges, and even as late as Wii allowing direct control over the system cache.
This is the reason why game emulators are so difficult to make. It's not that emulation itself is difficult, it's that doing it while preserving exact semantics of a machine is extremely difficult to do (especially while retaining performance). It's also the reason why even though Xbox One allows backwards compatibility it's done only through a whitelist after careful playtesting and patches for every single game.
Although it wouldn't be any old game that could probably get away with this hack, but I imagine if someone like Square Enix wanted to do it for a final fantasy game Nintendo would probably be okay with it. It'd very much sell the platform as more powerful than it was and Nintendo would want to appease Square Enix.
3
107
u/DJ_Beardsquirt Aug 13 '17
Can the people who solved this problem tackle climate change next?
81
u/TonySu Aug 13 '17
First we port Doom to CO2 molecules, then use it as a platform for arbitrary code execution!
32
u/Chii Aug 13 '17
climate change is a much harder problem than any computing problem, since it involves changing human behaviour...
12
u/Yuzumi Aug 14 '17
Yeah, we know what causes and fixes climate change. Unlike using ACE to make a gameboy do whatever you want, getting ALL humans to do the better thing is impossible. Especially when they are still making money on fucking the climate.
4
u/Flight714 Aug 14 '17
All of those tricks are pretty much Retro Audio Hardware 101 (source: been learning audio programming tricks as a hobby for years). They're definitely clever, but they're all well-known techniques.
Writing the assembler to implement them with precisely the correct timings is something I have no idea about, however. Maybe that's the genius part.
10
u/NoMoreNicksLeft Aug 14 '17
I've already figured out a solution to this. You might not like it. But if you dislike it, you will only dislike it for a short time, after which you will neither like nor dislike anything again.
I will celebrate your sacrifice (and the sacrifice of the other 6.5 billion people) annually. You will be remembered as heroes.
1
11
u/cypherreddit Aug 13 '17
stop burning shit
1
u/F14D Aug 14 '17
Burning isn't a problem if you've still got lots of forests.
8
u/cypherreddit Aug 14 '17
forests are barely better than carbon neutral
1
u/Blecki Aug 14 '17
How do you figure? Trees are literally sucking carbon out of the air to make themselves. Look at how overgrown everything is these days. The plants are trying hard to save us.
2
u/cypherreddit Aug 14 '17
Tree use carbon as building material. When they shed leaves, branches, fruits, or just die, nearly all that carbon will be released when that material is broken down. The only way forests are any significant carbon sink is if you bury them deep.
Pulling carbon from the atmosphere isnt enough. It needs to go somewhere to be undisturbed forever. Like oil and coal used to be.
2
u/PM_ME_OS_DESIGN Aug 14 '17
Good thing we're cutting down an area equivalent to a Rhode Island's worth of forest every year then!
18
u/chazzeromus Aug 13 '17
I thought this was arbitrary execution on the host machine by using a vulnerability in the emulator's sound emulation lmao
4
3
u/so_you_like_donuts Aug 13 '17
Here's another (earlier) arbitrary code execution video: https://www.youtube.com/watch?v=3UnB1fomvAw
4
u/PointyOintment Aug 14 '17
reads judging section
Wow. Even if I was into this stuff, and really good at it, I would never submit anything to that site.
2
1
1
Aug 14 '17
Came here to write something about Flappy Mario Bird. Stayed for quite a bunch of awesome WTFs.
1
0
Aug 14 '17
[deleted]
14
u/Roboguy2 Aug 14 '17 edited Aug 14 '17
That's not how GameBoy emulators work. It lets you run arbitrary code within the confines of the emulator. In fact, the reason that this isn't an issue is essentially the same reason that virus researchers are able to intentionally run infected code on virtual machines without problems.
Some specific examples: The GameBoy architecture never had, say, an IP stack so you would not be able to access the Internet in any way. It had a storage system, but emulators usually just keep a file (or a few files, in a certain directory) around that simulate the non-volatile memory (and has allows no capability to access the file system beyond that, in the first place).
0
Aug 14 '17
[deleted]
9
u/Roboguy2 Aug 14 '17 edited Aug 14 '17
Well, yeah. Just as there potentially is with any program that you can give 3rd party data to. That is an entirely different topic though.
That's not what this is about. This is specifically about exploiting the GameBoy Color architecture (it says in the description that it uses no emulator exploits).
Even if there were an emulator bug, it would be very unlikely (though not impossible of course) to have much of a real effect on the underlying machine due to the fundamental limitations of the GameBoy Color architecture: The lack of Internet access, hard drive access and the fact that the emulator only has to support 16-bit pointer addresses. For true, correct emulation integers that exceed their maximum value should wrap around back to zero which prevents buffer overflow attacks that "escape" the emulator at a pretty fundamental level. All of those things severely limit what can be done by an exploit, unless there is a very major bug in the emulator. But, it then boils down to the fact that it can have bugs, just as any other software can (and so is not really relevant to this topic...).
4
u/RenaKunisaki Aug 14 '17
There have been examples of pwning emulators from the inside, but that's not what this is doing.
0
0
Aug 13 '17
[deleted]
114
u/Vociferix Aug 13 '17
If you read the article, you would see that it absolutely is programming. Sure, it's assembly on old hardware, but it is certainly programming.
I would say assembly and buffer overflow exploits are very relevant here and quite interesting IMO.
0
34
u/[deleted] Aug 13 '17
I wish I could understand how he did this or replicate it. If there was a walk-though detailing how he set this up it would be great to follow along with.