r/AskProgramming • • Mar 07 '24

How do game developers protect their code when other people 'download' their game?

Although I could never find the source code, I have to assume it's somewhere locally on my computer right? What are the security measures taken to make sure people don't tamper with the code.

But at the same time, sometimes I see people writing mods for a video game because they will run gdb/deassembler and see the source code.

My question is how can they see it. It would be very interesting to read.

80 Upvotes

43 comments sorted by

12

u/WOTDisLanguish Mar 07 '24 edited Sep 10 '24

friendly compare wakeful pen grandfather cake chubby different paint chase

This post was mass deleted and anonymized with Redact

15

u/GxM42 Mar 07 '24

Nobody really cares about your code, to be honest. People playing games like to play, not disassemble assembly language. And even if they did, who cares. Most games succeed due to graphics, an idea, and good writing. Not assembly language. So I wouldn’t spend much time worrying about it.

5

u/blueg3 Mar 07 '24

The source code isn't on your computer. What is on your computer varies a bit, but normally it will be machine code.

If you want to understand what machine code does, you'd use a reverse engineering tool, like Ghidra or IDA Pro.

From there there are two separate questions: how do you stop people from reading it and how do you stop people from modifying it.

The basic technique to make it harder to read is obfuscation: changing the code so that it's harder for a reverse engineer to understand.

Preventing people from modifying it is a different story, and that is more or less the point of anti-cheat libraries.

3

u/martinbean Mar 07 '24

How do game developers protect their code when other people 'download' their game?

Nope. You download an executable. It’s a binary files that’s made from source code, but isn’t the source itself.

It’s like receiving a cake. You receive the cake, not its raw ingredients (flour, eggs, etc).

8

u/agritite Mar 07 '24

Continuing the analogy, you can't possibly prevent customers from sending the cake to a lab to find out the composition, because it's out of your control. Nor is it possible to alter the cake in some way that the cake is non-analyzable, because, well it has to be edible and taste good.

1

u/threedubya Mar 10 '24

True but there are limits of what they can do. Did they use cow or goat milk chicken eggs or quail. Then what you cannt sell your version of windows 11 or gta online to anyone.

3

u/Shadowwynd Mar 07 '24 edited Mar 07 '24

Imagine that you buy a cake from a fancy bakery. Surely this gives you all the knowledge of how to bake this same exact cake yourself, right?

For most people, the answer is no. The thing you buy is the finished cake, not the actual recipe to make the cake (Which might include special ingredients, special tools, and the years of skill to use them).

There are master guru levels of people who can take a bite of of bread, or a piece of cake, and write down a “close enough” recipe immediately. A computer game or any other non-trivial program is much, much, much more complicated than a recipe. You get the compiled version of the game to play, the developers keep the source code of the game and that never hits your computer at all in the same way the cake is baked from the recipe.

There are special tools that can let someone of sufficient skill break down computer code and look at it instruction by instruction, just like you could use advanced laboratory tools like a microscope and chromatograph to eventually derive the recipe for a cake. This is also how some hackers figure out ways to break into a system, or people to figure out your file format or your proprietary formula. For most things though, it tends to be insane amounts of work and not a lot of reward.

3

u/jmacey Mar 07 '24

Unreal Engine source code is here https://github.com/EpicGames/UnrealEngine have a look (it's massive and really hard to grok! however there are loads of documents on their website).

There are many older games released on github if you are interested in seeing stuff. This one is really interesting and lots has been written about it https://github.com/TerryCavanagh/VVVVVV for example this https://www.polygon.com/2020/1/13/21064100/vvvvvv-source-code-game-development-terry-cavanagh-release

2

u/minneyar Mar 07 '24

Although I could never find the source code, I have to assume it's somewhere locally on my computer right?

No. The source code, which is in a language like C++ that is intended to be easy for humans to read and write, is compiled into machine code, a series of architecture-specific binary instructions that is much, much harder (but not impossible) for humans to understand and modify.

What are the security measures taken to make sure people don't tamper with the code.

For one, being compiled into machine code will stop most people. It's very difficult and time-consuming to understand and modify machine code in any useful way.

But it is still possible if you're dedicated, and so people have created a variety of technologies to try to make it even harder for people to do that, like the dreaded Denuvo Anti-Tamper, which does things such as check the machine code on your computer and compare it to an online database to verify that it hasn't been modified. Denuvo will then stop the program from running if it thinks you've modified it. This is usually used in conjunction with copy protection measures that check whether your Steam account owns the game in question, or with anti-cheating software that tries to stop you from modifying the game as it's running in memory.

But at the same time, sometimes I see people writing mods for a video game because they will run gdb/deassembler and see the source code.

Most modern games use external libraries or frameworks like Unity or DirectX, and although they are machine code, people have spent enough time analyzing them that their inner workings are fairly well understood. If you're using Unity, for example, somebody who understand how Unity works can probably find the places in your code that access it in order to do things like inject custom textures or change game logic.

3

u/EarhackerWasBanned Mar 07 '24

Our rule is "never trust the client".

I'm a web developer and any idiot can open up the right tool in their browser and see my code. If I put e.g. database passwords in the website code I'm goosed. I have to have other ways of dealing with databases.

My code is compiled somewhat but the whole point of the web is that everything is human-readable. That's not a concern for games devs, and in fact they heavily optimise their code to be efficiently machine-readable. The compiled game code is only one step away from being pure binary numbers.

Game code is very difficult (but not impossible) for humans to read and understand.

How they do it is leaning on experience, the few facts they do know, making assumptions, and a lot of trial and error. The format of a Windows exe file is well-known. It's safe to assume that it contains Assembly code for Intel-compatible CPUs. It's safe to assume it was written in C++ because that's the games industry standard, and it's highly probable that it was compiled with gcc (GNU C Compiler) because it's the most widely used compiler. From that little knowledge you can start to pick apart the puzzle, which is what gdb/deassembler and friends are doing to turn it back into something human-readable.

Worst case scenario, none of that works. They can still open up the exe as a binary file, as well as inspect what's going on in their RAM as the game runs, change the 1s and 0s and see what happens. In practice they'd open it up in a hex editor (hexadecimal numbers map nicely to binary numbers) and change a few values.

1

u/[deleted] Mar 07 '24

They download the installer or an executable file. It isn't the source code. It's a binary file made with the source code.

1

u/Ikkepop Mar 07 '24

Sourcecode gets transformed from human readable to computer readable code, which is not really human readable. I mean with patience, time ans practice you can learn to read machine code but it's difficult to say the least

1

u/Quantum-Bot Mar 07 '24

The same way food companies protect their recipes. When you get a bowl of fried chicken from a restaurant, you don’t get all the ingredients laid out separately with a recipe for how to combine them, you just get the sheet prepared food. With lots of expertise and time and the right tools, you could probably reverse engineer what spices and ingredients went into the batter and recreate the recipe yourself, but it wouldn’t be 100% accurate and it would take almost as much with as just coming up with your own recipe from scratch.

Similarly, when you install a game, you’re not getting the source code. You’re getting code that’s been compiled, which is kind of like cooking the fried chicken. Compiling is a process that’s necessary in order to actually run the code, but it’s impossible to undo and it makes it difficult to decipher what the original product was.

1

u/wrosecrans Mar 07 '24

No, when you install a game, you generally don't get the source. And when you disassemble a game binary, you don't get the full original source code back out of it. Look at source vs disassembly of a small and simple example program here:

https://godbolt.org/z/8qca3srxx

Left is source. Right is the assembly. Which of those two is more obvious to you when you try to read it? The left is meant to be human readable. It has comments in plain English, and the notation looks like normal math. The right is a lot closer to the raw binary that gets blasted straight at the CPU. Trying to read both will make the difference very clear.

1

u/backwards_watch Mar 07 '24

The thing is: most people playing don’t have the technical skill either to read the code or decompile the binaries and reverse engineer your code.

However, skilled people can do that and some of them spend their life doing it. That is why we have reversed engineered games.

The analogy is locking your house. You protect yourself and you are safe just because most people won’t break into your house. But someone who really wants to get in and don’t my applying some brute force can get in quite easily.

1

u/[deleted] Mar 07 '24

Protecting games from tampering is a very extensive topic. But I'll try to give you a high level overview:

Most programs written in compiled languages don't have the source code accessible. These can be among the hardest games to temper with as sometimes entire functions get inlined. Meaning the compiler chooses to duplicate the same effective code in hundreds of places. Even if you disassemble it or attempt to decompile it, it will be a complete mess to read and understand, and can take hundreds if not thousands of hours to decode.

Many games are written in a game engine, this means that common patterns with other games already exist and the hard work in disassembling it has already been done. Often time the core of these engines are the same.

Game files probably the easiest area to get started with modding. Often time there's sometype of resource packing format which you can essentially open in the same way you might open a zip file. This gives you access to not functionality, but appearance of the game. For example sound, image/texture or 3D model files. Some game engines also allow you package behaviors into their scene/level file formats.

Lots of games don't use a compiler, instead are interrupted. Lua, javascript are examples of languages which aren't compiled to machine code, instead they run ontop of an interrupted which is sometimes built directly into a game engine. These are pretty easy to modify and are popular design choice for games which want to make modding accessible. Often time a game will use it's own modding pathways to create the base game, as this ensures modders have the most accessible.

Some languages pick a middle ground between compiled and interrupted. These would be the byte code languages. Java and C# are popular choices (albeit calling C# a byte code language is an over simplification). These have some of the benefits of the compiled languages, with the added benefit the "binary" file is very portable. Jar files (Java's ARchieve format) is actually just a zip archive. If you rename your minecraft .jar file to .zip, you can decompress it and view it's contents. The reason you see so much minecraft modding on Java but not the bedrock edition is due to the differences between modding a game in bytecode, and modding one written in C++

Beyond the compilation stage being the first line of defense. Lots of programs employ obfuscation or encryption to protect the game.

Obfuscation is more common with byte and interrupted languages, and basically make elements of the code harder to read by randomizing variable and function names. This is why forge exists for minecraft which has gone though the work of creating bindings mods can use, while they translate that to the obfuscated byte code. Which is why you might hear an author say they're waiting for forge to be updated.

Encryption is more often used to protect game files (asset files). The goal is to prevent creation of tools which can access the archive files. You can sometimes encrypt the game's code files, even binaries. Which basically means the game exe contains the game's binary, then another binary design to decrypt and run the game's binary code.

Then you have the world of active cheating/modding software. These are programs that run along side the game and modify it's memory while it's running. Cheat engine is an example of this. Programs might also try to decode or modify network traffic. Or even just modify the screen data and human user inputs. Some cheats and pirating software modifies the binary as it's running.

GPU shader mods are also another level. Shaders are basically programs that run on the computer's GPU and handle many rendering aspects. Shaders are often compiled when the game starts up since different GPUs and drivers might need to compile the shader programs differently. But it's very easy to add, remove or modify shaders from a game from a program running behind the game. Reshade and other programs can do this.

DRM and anticheat software like denuvo or VAC installs itself into the operating system itself in order to monitor for any programs trying to modify a game's memory or binary files.

This is just scratching the surface but hopefully gives you insights in the complex depths of games modding. Really it depends on the game, the language and engine the game is written in, protections that might exist and what you want to do.

1

u/Far_Archer_4234 Mar 07 '24

There are tools available that make it relatively easy to reverse engineer even obfuscated code. If you want your IP to be sonfidential, you should look at using a cloud based solution, because determined individualls have ways of getting at it if you let them download it.

1

u/MuForceShoelace Mar 07 '24

It's called source code because it's the source of the final code, which is in assembly and generally not super human readable.

1

u/sepease Mar 07 '24

Lawsuits.

Games are separated into the code and the art (including pictures, voice acting, pre-rendered cutscenes, etc)

Producers are generally more concerned with someone ripping off their franchise, because that’s the IP that really makes them money. People play a game because of the Star Wars name on it, not because it was made with Unreal Engine 5.

If you want to look at an older game engine, back from when game engines were built for specific games:

https://github.com/scp-fs2open/fs2open.github.com

With some guides:

https://wiki.hard-light.net/index.php/Source_Code_Reference

https://wiki.hard-light.net/index.php/Coding_In_C

1

u/flPieman Mar 07 '24

You can eat a fancy dinner at an upscale restaurant, but that doesn't mean you know the recipe or could prepare it yourself at home.

1

u/t0b4cc02 Mar 07 '24

download x64dbg and open up a exe. you can see it. modify it and watch how it works.

1

u/Charcoalcat000 Mar 08 '24

Many developers just don't.

Modders use DnSpy to decompile games made with Unity, then they inject their own code into it to create wonders.

1

u/sbarbary Mar 08 '24

I don't think this has been mentioned.

Code Obviscation. Basically you load some of you code into ram from some sort of literal you write it out and execute it then you over write it again. This means a disassembly is very hard to read and a live ram dump when the game is running wont actually run.

It was very common for games in the 90's but I wonder if it's used these days as there were always ways round it.

0

u/[deleted] Mar 07 '24

[deleted]

38

u/alkatori Mar 07 '24

This.

But even in the case of interpreted code - most people wouldn't bother reading it.

I'm a software developer, I've only glanced briefly at open source games out of interest but I already do enough writing at work.

10

u/WOTDisLanguish Mar 07 '24 edited Sep 10 '24

squealing dependent file hard-to-find somber spark upbeat sort aback quarrelsome

This post was mass deleted and anonymized with Redact

14

u/alkatori Mar 07 '24

Back when I was getting started, I wanted to make video games for a living and sell them. That never panned out:

  1. Business Applications and Embedded Software was a lot less stress and more money.

  2. Without the money aspect, I tended to satiate my curiosity of how to do something and then stop. Because after I figured out the puzzle - I just wasn't interested anymore.

Now that I've been around awhile, I would trying to 'protect your code' isn't really worth it. If you are making money off it, license it to your customers and use lawyers if they break the license.

We used to license to police stations in an early job, they would always try and get around a license dongle or key to try and allow more users than they would pay for.

1

u/W1D0WM4K3R Mar 08 '24

The problem is, once the cat's out of the bag, the loss in profit can be much more than any amount you'd get via lawyer.

2

u/alkatori Mar 08 '24

It can be, it all depends on who your target customer is.

IMO. I hated the fact that 911 systems / radios / recorders had licensed concurrent connections and things would be dropped once you hit the limit.

I was arguing that the recording system should function up to the limits of the hardware, record that we went past the licensed operating limits and go in to a limited mode that turned off advanced features or archive retrieval.

But I didn't win that argument. It was too hard to explain I guess.

That's why some 911 calls and the like are lost. They didn't scope the system to cover such high utilization due to cost.

1

u/Send_me_any_pics Mar 09 '24

Am I reading this correctly?

That a police department tried to break laws in order not to pay you more?

1

u/alkatori Mar 09 '24

Yep, happened all the time.

1

u/justdisposablefun Mar 10 '24

Obfuscation really is just security through obscurity ... sure, it can stop the casual hacker. But mostly, you're just making your own life harder for very little benefit ... most people that jumped through enough hoops to find out is obfuscated are not going to be too upset ... especially these days with AI being what it is, I haven't actually tried it but I'd bet reversing obfuscation isn't a hard task with their help and a bit of know how.

6

u/HunterIV4 Mar 07 '24

but at the end of the day, the computer can execute the code, it's not encrypted, with enough effort you can get back to assembly language, change stuff, and assemble it back into a .exe and run it.

Essentially how cracks work, although they tend to just hex edit the change. The cracker analyzes your executable, finds the security check code, and skips/removes it so the rest executes without the test.

DRM systems generally attempt to obfuscate this test to make cracking more difficult, with somewhat inconsistent results. The most successful DRM systems are essentially rootkits combined with spyware to try and ensure you haven't modified the code with a crack, but having those on your system opens up you for other security risks as now viruses have a new vector of attack by utilizing the DRM system (which already bypasses most of your system security). It's why these DRM systems are so controversial.

The only way to truly protect software is to have a large portion (or all) of it running on a remote server that the user doesn't have access to. As long as your backend isn't leaked or reverse-engineered your stuff is "safe".

In the end, though, the best way to "protect" your software is to simply write really good software at a reasonable price that people want. All the evidence shows that even completely open software that is popular will be purchased the majority of the time as most users either don't have the technical skill to find and use cracks or any desire to save a few bucks when just buying and downloading it is so convenient. Cracks also make software harder to update and maintain as you need a new crack for each update (typically).

Most people simply aren't going to bother, and the high school and college kids that make up the majority of software pirates usually don't have enough money to buy the software in the first place and wouldn't actually become customers if they couldn't pirate it, they'd just go without. In fact, there's some evidence that piracy can increase sales (not just in software), as the younger people who can't afford the product end up liking it and buying more.

It's not a bad idea to have some level of protection, even if it's just basic compilation and maybe a serial number check, but for most devs I'd just recommend not worrying about piracy. If your software is popular enough that there is a lot of piracy it's probably also popular enough that it's making a ton of money. Not being noticed hurts your sales a heck of a lot more than piracy, so personally I'd spend more money on marketing than DRM from a raw business standpoint.

3

u/FloydATC Mar 07 '24

Underrated post. If the price is right and buying is less cumbersome than piracy, people with disposable income will pay just for the convenience of not having to bother with cracks and stuff.

Back in the day, I remember seeing people from a well-known international software giant at a LAN party where people were openly sharing and installing their software from obviously pirated CDs. They were perfectly fine with it, knowing full well that those kids now spent all their money on hardware but would later grow up to be intimately familiar with their software, thus increasing future sales.

1

u/GuiMenGre Mar 07 '24

Could you give me a little explanation on how the security check code works? Does it check to see if the machine that's executing it belong to the user who bought the game? Like, if I buy a game on steam, the .exe file will have information in it about my steam account, and only execute if it is through my account?

1

u/HunterIV4 Mar 07 '24

It depends on the type of DRM, but Steam DRM is typically a combination of a check in the .exe plus a .dll. Cracks for Steam games replace both. Note: cracks can also include viruses very easily, so use at your own risk, and I personally wouldn't recommend them.

In the case of Steam (or Epic or the EA launcher or whatever) the code basically just tells it to confirm you are logged in properly in the relevant app. It's just a server check. This is also why these particular types of DRM are relatively easy to beat; the crack simply modifies the check to always return true, or continue past where the check would return true or false. There's no reason to bother with trying to actually trick the servers.

To directly answer your question, no, the game .exe isn't going to contain any of your account information. It's basically just an API call to the Steam client saying "hey, is this guy properly logged into Steam and does that Steam account own this game?" and if the answer is "yes!" from the Steam app the game keeps running, otherwise you get an error message and the game quits.

Other DRM systems like Denuvo typically create a profile of your system that is checked as part of the authentication, sort of a "system hash" (this is an oversimplification and some of the implementation is still hidden on their side, but should be mostly accurate). The actual check is then broken into pieces all throughout the code so it's hard to find in order to skip it, and unless you get a combination hash between your system and account you can't execute the game, which is usually generated on download (so Denuvo protected games are actually unique files for each user, making it hard to crack since a copy downloaded for one PC won't work on a different one).

This is why you'll hear things about Denuvo being "spyware" or a "root hack" because in order to do this it basically has to gain admin access to your computer and send private system data back to the company servers. Typically this is generic information, but nothing really prevents it from being used maliciously, and if something managed to modify the DRM to run other software it could allow a virus to gain admin access to your machine without needing it itself.

It also has been shown to reduce game performance, sometimes significantly. There have been tests that show games with Denuvo (or similar) that are cracked actually run faster on the same system than the standard game. It's one of the reasons I stopped playing the Assassin's Creed series many years ago...I bought the game and couldn't run it because it was basically a slide show even in the menus, and I read something about a crack fixing the issue and tried it and the game ran perfectly well after that. I didn't want to have to crack games I bought in order for them to be playable and refuse to purchase any game with this style of DRM on principle. Same for "always-online" DRM that prevents running the game at all if it can't ping the DRM servers, even for single-player games.

But back to Steam, their DRM is a lot less intrusive (and thus easy to crack), and many games will actually run even without Steam active as the Steamworks DRM is optional. I've bought many indie games that work just fine without it. It also pretty reliably maintains a token if your internet goes down as long as you don't quit and restart Steam, meaning many Steam games will run offline even if they have the DRM.

There are certainly valid criticisms of Valve and Steam out there (I think they are not the most fair to developers, for example), but one of the reasons I think they are so popular is that their DRM is both optional and fairly unobtrusive for the end user, and doesn't really cause performance issues beyond the resources used by the Steam app itself. The convenience and quality of the store are also factors for sure.

For someone who doesn't like DRM, though, GOG is easily the winner, as all of their games are DRM free. This causes them to have a much more limited selection, though, as many game companies aren't interested in selling DRM-free games.

I should note that I've never personally made a crack and so this is all based on second-hand research, in part motivated by my personal crusade against EA after I was forced to "steal" products I'd already bought to make the usable. It's also just an interesting subject.

But because of that, I don't know all the intricate details as I've never tried to do it myself or figure out how to do it, this is just experience in both being annoyed with DRM and investigating options for my own software (and my conclusion was that DRM isn't worth the time or money for indie devs; anything I release will be 100% DRM free).

2

u/GuiMenGre Mar 07 '24

I wasn't expecting such a complete answer. It really scratches an itch, I was curious for a long time about how this stuff was done. Thank very much! I hope the games you realese do well.

1

u/HunterIV4 Mar 08 '24

I hope the games you realese do well.

Thanks!

9

u/funkmasta8 Mar 07 '24

To add to this, it is much easier to simply get an illegal copy of a game than it is to decompile it to look at the code.

Anyway, most game developers aren't doing anything groundbreaking with their code. If you know enough about programming and computers to get that information out, you probably already know more than them so all you would really get out of it is avoiding reinventing the wheel.

2

u/emlun Mar 07 '24

You can't *really* 100% protect a game/app from people disassembling it and looking at the code.

The closest you can get is to make activities like this a violation of some terms of service. Many online games and streaming services do this, for example - they reserve the right to terminate your account(s) if you are found trying to disassemble, modify or otherwise reverse-engineer their code. Many professional services and device driver vendors do the same. But of course such restrictions are only really meaningful if you have some way to enforce them, such as terminating a service account or suing a business partner for contract violation.

I should also point out that not all software makers want to place restrictions like that, though - in the opposite camp you have the GNU/FSF philosophy that you should have the right to obtain the source code and make whatever modifications you like.

2

u/Weird_Cantaloupe2757 Mar 07 '24

It’s less that it’s beyond most developers to disassemble a program and reimplement it, and more that it’s an insane amount of effort with very little benefit generally.

1

u/Cerulean_IsFancyBlue Mar 07 '24

Some of the source code may be on your computer if some part of the game is implemented in an interpreted script. Some games implement part or all of these interface this way. Others implement pieces of the game internals with either hooks out to scripts or with scripted chunks.

Sometimes this is done specifically to facilitate mods. Sometimes it’s just better for internal development. For example, it might be quicker and easier to test modified gameplay by replacing a script, then by recompileling the entire game.

Sometimes games will implement large chunks of functionality in what amounts to structured data. Although this isn’t really source code, it does give you a large junk of game behavior that is easy to read and easy to edit.

What you generally don’t get is source code for internal functionality, which is often written in compiled languages. You also don’t get source code for third-party tools that the game developers have paid to include as part of their deliverable, like unreal or unity