Well, thank you for coming to my talk. The talk today is When Space Elephants Attack, and it's about a new game, a new contest that I'm bringing to DEF CON today, or this weekend, which is called the Schemaverse, and it's just basically a game within a database. So, first quickly, who am I? I'm an expert in designing video game UI. I'm specialized in computer-human interaction. I'm a graphic designer, and I'm also a very skilled technical writer. Except I'm actually none of those things whatsoever. I made a game in a database. That wouldn't make any sense. So who am I really? Just a database geek. So, Schemaverse 101. It's a space battle game played entirely within a PostgreSQL database. There's actually no UI whatsoever, just glorious SQL queries constantly. The game itself is a test game. It's a tick-based game, so basically everyone has a chance to do one move, and then the tick finishes, some things are calculated, and then the next tick goes on. The database itself is completely open to the internet. Everyone who gets the address for it will be able to connect, so long as you come to me first, at the contest booth, to sign up. Security is implemented within the database itself. There's also a couple of dirty little Perl scripts here and there, but Perl's always dirty. Surprise! Surprisingly, this game is actually a pretty good time. So I hope a couple people come, sign up, and give it a try. The game itself does also play after the con. There's just a constant running server you can join in. So why did I do this? Great question. Every geek needs a project. This just happens to be mine. I'm sure everyone can respect that. I also wanted to be educated myself on database security, learn a lot more, and learn about even just optimization and non-security elements. And I wanted to do the same and educate others on the matter. I also wanted to push database systems a little bit further. When I started creating this system, a lot of people told me, well, I asked the internet, because the internet is all-knowing, I said, what would you do to secure an open database on the internet? The only answer I got was don't. Well, why? What's wrong with that? It's just another application. What's missing from this system? That would make this a bad idea. So this is what I wanted to find out. And I might find out this weekend. And finally, it's also pretty funny. It's a game in a database. Come on. So why bring it to DEFCON? Clearly the last DEFCON that I've been at have just been too relaxing. Just drinking and having a good time. I felt I needed a little bit more stress while here. There also wasn't enough to break here, especially for the database geeks. So I've brought this here in hopes that you have a good time. So, getting into the actual gameplay. As I've mentioned, it's a tick-based game. Everyone goes, finished, new tick starts up. Basically, you command an army of ships who go out, conquer planets, and fight each other and blow things up. Sounds pretty good. So in the game, there's a bunch of different goals you can actually have. You can be a space jerk, running around just blowing people up. Not really actually playing. You can be the emperor. Just going and conquering every planet that you can find. You can be the planet pillager. And just rape the crap out of all the land resources that you can find. Using those resources to then build bigger and better things. Or, size matters. Build the biggest and best ships constantly. And you'll also win. But of course, there's one overall goal. Which is to do all of those things. And become the scheme of supremacy. So, how do you actually play this game? As I mentioned, you come to me. Or anyone who's at the contest booth. And you ask to register. When you register, you get inserted into the player table. And then you get an actual user account on the database. Yes, you are a user on the database. You also get two things. You get a row in the player's table. And you get a planet assigned to you. As your home planet. So you can see when you start out in the player table, you got some money there. You got, looks like 10 million, I don't really remember. And then you got a fuel reserve to get you started too. You also have something called an air channel. Which I'll get to later on. And finally, on your planet, you can see your planet has a location. Has a name. Which, it's your planet. You can update the name if you'd like. It also has a mine limit. That limit is only that number of ships. Can actually mine that planet per tick. And I'm going to get into that a little bit more later. And finally, the conqueror ID, which is you. Great work. So the planet you get off the beginning is somewhere off the far center of the universe. The center of the universe is where most of the battle will happen. So you get this planet off nowhere. And it's kind of your own little home that no one's likely to find. So now you've got your player. You've got a planet. Great, what's next? Well, I said it's a space battle game, so you need some ships. So I wasn't kidding when I said this is a sequel game. So what you want to do to create some ships, which will cost you a thousand dollars. Virtual dollars. Is insert into your ships table. Now there's a couple ways you can do that. It's fairly dynamic. You can either just give it a name and it'll be created at the center of the universe. You can give it some skill. Every ship gets 20 skill to start with. And you can kind of disperse that however you want. You can upgrade those more later on, but it's good just to have that initial go. And you can also specify the location of where you want to build. Now there's a little bit of extra rules there. Basically if you're building from the center, 3,000 in every direction from 0, 0. You can build wherever you want within that. That's fine. Outside of that range, you can only build on planets that you've already conquered. So. You've got your home planet where no one can get to it. You go there, mine some resources, and then hit center earth for the actual battle. So. What can ships actually do? Each of your ships have the ability to do three different actions. They can only do one of those actions per tick. So you have to decide what it's doing. When performing that action, both objects have to be within range of each other. Range is just another skill that you're... ship has. So what's the first action? Attacking, of course. So again, this is a sequel game. So there's a ships in range view to find all your enemy ships because you can't normally see them. But if they're within range of your ships, then you can. So using the attack function, you can actually attack from one of your ships to an enemy ship that is within range of you. Make sense? Actual attacking uses your attack skill and their defense skill plus a little bit of luck and then calculates that to your future health. That doesn't get committed until the end of the tick. Of course, if there's attacking, there's also repair. Repair is the same thing. You can actually repair anything within range of you. Repairing uses the engineering skill. No luck is really involved in that one. It's just engineering. And finally, there's mining. Mining is how you get all the resources in the game. Mining is how you continue on and build up your fleets and improve. So when you mine a planet, nothing happens instantly. You get added to a table called... Don't remember. Anyways. It's added to a mining table where it's just temporary for that tick that says this ship is mining this planet. At the end of the tick, all ships that tried to mine each planet try and do so. Now keep in mind there's that mine limit. So only a certain number of ships can mine a planet per tick. If you mine a planet more than anyone else that tick, you now conquer that planet. Oh, and you also get the resources. You don't have to conquer to get the resources though. So obviously ships can also move around. And this is the most finicky part of the game. It's the thing that people complain the most about. But you can write a better move function in SQL. The game is open source. Please do so. So there's really two ways you can use the move function. And keep in mind this is a 2D game. A 3D game in a database would just be weird. So the first way you can do it is actually give a speed. And then a destination. If you give a speed and a destination, your ship must have enough fuel to start and then slow down. If you want to start, or if you want to speed up to 100, then it'll take 100 fuel. When you get to your destination, it's going to take another 100 fuel to slow down. You also require fuel for changing direction while moving. If you don't have enough fuel to slow down, but you have enough fuel to speed up, you're not going to be able to use that first methodology. It's going to stop you and say, this isn't going to work because I can't stop you and you have a destination. But it will at least return back the direction that it was trying to go to your air channel, which I'll get to later on. So the second way you can do it, if you don't have that much fuel and you don't actually care about stopping anywhere, is just specifying the speed and then the direction and then just null and null for the actual location. Your ship is just going to go off into wherever it wants to go. It doesn't really matter. You can refuel it later and stop it when you feel like it. So that's pretty much moving around. It's not too hard, but for some reason it is difficult. Nice little graph there on how moving actually goes as far as the direction. Not too complicated. So you've got these awesome ships, but you want them to be better. You want that size matters award, maybe. Well, there are a lot of upgrades you can do. You can upgrade the health, the fuel, the speed. Range. Range is a good one. Attack, defense, engineering, and prospecting. Prospecting is your mining. I should have mentioned that. Every skill does have a limit that you can upgrade it to. Attack, defense, engineering, and prospecting have a limit of 500 total. They can't surpass that. Everything else has its own limit. It's right there. It's also on the CD if you want to reference it. So just kind of a recap of what's going on in the game, what happens in a tick. Every ship can move once in the tick. The amount it moves is based on its speed. Every ship performs a single action, or they might do nothing. That's really up to you. After that, the perform mining function takes action and actually calculates who is successful in mining that planet. Gives out resources and awards new planet conqueror to anyone who's successfully done it. Planets will run out of fuel. So if that happens, some planets randomly will receive more fuel after each tick. Afterwards, damage and repair is committed. As I mentioned, attacking and repair happens to the future health. Future health becomes current health. Pretty simple. Any ship that has been at zero health for 60 ticks is going to explode. And you can no longer use that ship. All your upgrades are gone, but you do get a thousand virtual dollars of currency back. So that way you can build a new ship somewhere. And finally, tick underscore sequence dot next val. Tick goes to the next. Pretty standard. So general strategy, as I talked about earlier. Basically, you want to go build on your home planet that you've been given. Build up to your mine limit. And just mine the crap out of that planet. That's going to give you all the resources you need to then build some great ships for the center where everyone else is going to converge and try and branch out to get all the other planets. You're going to want to make sure you actually defend any planet you conquer. If you conquer a new planet, build some new ships around it to attack anything and to defend. It's pretty much the game. So anything that's happening in the game does enter your my events view that you have. Most things that happen successfully at least. Some events are public, some are private. You don't necessarily care when someone upgrades a ship or you don't necessarily should know when someone upgrades a ship. The events, my events view itself is designed to be read very programmatically. Because this is a game where you want to be able to pull details and actually figure out what you're going to do based on those details. But if you want to read it in a more human readable way of what's actually happening. If you just use the read underscore event function with the ID. It'll actually concatenate it into a nice little string that you can read. So there's a couple other little helper functions like that. Refuel ship obviously is fairly helpful. Every time you mine a planet, that fuel goes to your players fuel reserve. And then you can give that fuel to your players. To your ship. Doesn't go directly to the ship that mined it. Convert resource is important. Because you mine fuel but you want money. You need money to build things. It's actually a one to one relationship. Originally I had some crazy system where it would fluctuate. But the only thing you ever do is convert fuel to money. You never convert money to fuel. So it was kind of tanked. There's a couple other character variables. Get variable, get numeric variable. There's a variables table that holds the maximums and things that you can upgrade to. So it's just an easy way to get that value. And get player ID and get player username. You're going to use those a lot if you write some queries based on the events table. Or if you want to figure out who someone is that's attacking you. Other key views that you might find interesting. I talked about ships and range. Again, you can't see other ships in your my ships table. Because it's not your ships table. It's the my ships table. If you use the ships and range then you can actually see other ships around you. The my ships flight recorder is just kind of neat really. It's actually completely useless. But if you wanted to graph out and see where all your ships went then it's kind of fun. Finally there's also online players. Obviously what it is. Current stats just gives how many planets have been conquered. How many ships total are in the system. How many players are online. How many players are in the system. Etc, etc. So just how much money is in the system too. So it just kind of gives you a benchmark to see how you're doing. You can't really see very much about how anyone else is doing in the game. But you can see the overall stats. So you can kind of guess how well you're doing off of that. And then there's the public variable table again that I mentioned. Which you can just use those functions to graph. So finally fleet scripts. This is where the game actually gets super cool. At least in my opinion. I might have a weird sense of cool. Basically a fleet script allows you to automate all the gameplay for you. Because I love SQL. If you're sitting in this room you're either my friends or you like SQL as well. But you don't want to be writing SQL for the next 72 hours. To win a contest. Because we're at DEFCON. DEFCON is a little bit more fun than doing that constantly. So how do you actually do this? And how does it work? Well if you insert into your MyFleets table. Give it a name. First start. If you update your MyFleets table. With the script declarations column. And the script column. And then enable it. What's going to happen. Is it's going to take those sections there. Which you can see for this one. I'm taking all of the ships within range. And attacking the crap out of them. The system will take that data you just updated into those columns. And then create a function out of it. So you can see this will be called fleet underscore script underscore nine. Because we updated fleet script ID equals nine. The declarations go under declare. And the scripts go under begin. Just in case nothing does get returned. Got a quick little return one at the end there. If you wanted to return something that makes you feel better it's fine. Do so. So. Now you've just got this function. Which isn't really doing a lot. It makes it easier because now you don't have to rewrite the scripts every time. You just call them. But if you want that to happen automatically for you every tick. You can do that. The script that controls all the ticks is tick dot PL. Tick dot PL is just one of my dirty little Perl scripts. And it will actually connect as your user. And run that function on your behalf. Every single tick. To do so you just have to buy a minimum of one minute of processing time for it. Which is ten million. They're expensive because time is money. And you have to set it to enabled. The enabled is really just for your own little reference. So if you wanted something to stop. Then you could do so. If your script runs past that one minute. Everything's cancelled. Gets reverted back. And you just lose what you did. So you have to keep it within that one minute. Or buy another minute onto it. To buy any time for it. You just use the upgrade function again. You pass it the fleet ID. And you pass it fleet run time as the code. And then you can specify the quantity. So this would buy one more minute. For my fleet nine. So obviously you don't necessarily see what's happening in the fleet. Because you're not running it. So if there's been any problems. You just notice I'm not getting any money. This kind of sucks. So I have actually made a way. Where you can see what's going on too. Postgresql has a notify system built into it. And I utilize that. To use that error channel. That I threw in my players. Any errors that your script will have. Will be piped to that. So if you type listen. And add your error channel. Every time you run a sequel query. Within the results payload. It's going to include any new errors. That you've had. In the script. In that queue. I also wrote a super insecure python script. Which you shouldn't run. From the defcon network. Because you will be on the wall of sheep. There's no SSL on it. If you want to download the script. It's on schemaverse.com. If you have some server off defcon. That you feel more comfortable using. Then it's fine to use it. It's not a big deal. But please don't use it here. Here's a little example of what it looks like. It's dead simple. It's the schemaverse output stream or SOS. And it will just list anything that's going on. There's also a couple other things in the system. That will notify here. Not just fleet scripts. But any time move fails or things like that. It just gives it here. I did have an error table at first. But after 10 million records in a couple days. It didn't seem like a good idea to continue. So this is defcon. Security in this game. As I mentioned earlier. Most people's response was don't. But hopefully. I think it might last. It lasted the first night. So I'm pretty happy about that. Architecture wise in the game. As I said. You are users. So the players are users in the system. Then you've got the database. And then you've got the two Perl scripts. And I didn't really talk about the one Perl script yet. But I'll get to it. And that's really all the system is. So what was I worried about when writing this game? Well query limits was the first one. All it takes is one person to run a couple queries of generate series 1 to infinity. And then. No one could ever have another query actually run on this system. So I needed to find a way to stop that. Obviously authentication is a bit of an issue. Luckily I've got super secure MD5 going on. That was the best I could do. SQL injections. Obviously a problem. Although admittedly at first I thought. Well. A user can only do. What they have the ability to do on the system. And I kind of forgot a key fact of. There was another higher power user. Controlling some things. And finally. Postgres exploits and quirks. I don't really know what the next zero day is for Postgres. If it happens this weekend. I have things logging. So I hope I find it. As I mentioned earlier. Most communities were less than helpful to me. On pretty much all of this. The only. I think the one response I got was use SSL. So someone connects securely to my box. And then destroy it. So I was really happy about that. So the basics that I kind of went on. Basically. As far as the actual game play goes. Almost everything in the game is based off of. Player ID equals get player ID session user. Session user is just a variable. System variable in Postgres. That's who's connecting. So because you're a user. Next to your my players table. That's how I figure out what you're allowed to do. And all the views. All the triggers. Everything is based off that. Which gets me to the next thing. How do I control data? It's basically just as I would hope you would develop. Your regular application security within your database. I use roles. To make sure that users should only do. What users can do. And can only see what they should be able to see. Triggers and functions control most of the data changes. That users don't have direct access to update. Rules do the same I should say. And then views just make sure you can only see. What you're supposed to see. Again you shouldn't be able to see. Where other players ships are. I did enable SSL. Because I do want people to connect securely. And destroy my box. Well I'd rather they win. But I'd rather someone not find the user name. And finally availability. Which was those query limits. That generate series. Postgres has a very very helpful. Variable within the configuration. Called set query timeout. Or statement timeout. Perfect. I thought this is exactly what I needed. No query should last longer than a minute. Was my original thought. So I set that up. And then I played around. And I found out that every single user. Who connects to Postgres. Has the ability to type set. Statement timeout equals zero. This is one of the things. Where I'm talking about. Using database systems a little bit. How is that acceptable? When you have something. That you configure. As a security measure. And a user. Any user can connect. And run a statement to set it to infinity. Didn't make any sense. That's where ref.pl comes into play. That's just another Perl script. That monitors all the processes running. And will kill anything that surpassed its time limit. It's actually a lot more dynamic. Than the statement would have been. And make less. I don't like them. So. Here's a little fun example. Of SQL injections that I thought. Oh it's fine. They're a player. They can only do so many things. That was really dumb. So in that fleet creation script. Where you take. What you've updated the fleets table to be. And then thrown it into the actual table itself. Or then that gets taken out. Put into the function. You see. Script declaration gets put there. And then the new script. I hope you can see the problem with this. Not only. Can someone privilege escalate. Their script. The function that they're creating. To be security definer. Security definer in Postgres means. The administrator is now running this basically. Or the person who created the function. Which. Only Schemaverse itself has the ability to do. Now your fleet script. Is going to run as Schemaverse. Every time. So I've got a nice little update. Player set balance equals zero. I may want to specify except me. But you get the gist of it. But not only that. After that. By inserting more nice code. I actually rewrite the move function as well. Because I. Why not. So I can send every player backwards. If I want. Or I can make the move function more convoluted. Great. So this was a bit of a problem. And I wasn't really sure how to fix it. I still don't know if I have fixed it. But I like my solution. I just added a secret. So let me go back to that quickly. Right underneath the initial create and replace. You have fleet under source script. It's just a quick string declaration. To say this is where it starts. This is where it ends. To fix this. I just made that random. If you don't know. How to end the script. Then you can't. Forcefully stop it. The error. The actual creation. Will just error out. Now I have had people with ideas. Come to me and say. What if I find a way to just make it. So that I throw an exception. And then my exception does something. I have no idea if this works. Again. It's DEFCON. It certainly can. So some final words on security. I hope that if someone does find a bug in this. That can destroy it. Instead of destroying it. They use it to win. Because if you can win this game. Through fairly unnecessarily good ways. It's DEFCON. Full respect to you. You win. I have no problem with that. So my only deterrent is the fact that. I'm running the first ever. Schemaverse tournament. I do have prizes. So if at the end of it. Someone does. Drop database schemaverse. Then no one wins my prizes. And they're actually pretty good. I have backups. But I don't. Once it goes down. I'm not sure if there's going to be time to fix it. I might be too drunk. But we'll see how it goes. So the tournament did start yesterday. Although in everyone's manual. I think it says the tournament starts tomorrow. But the tick actually. Like the game can take a while to play. Usually it's played over a week. I've tried to tweak it a little bit. To be a three day game. So here we are. It does end at Sunday at noon. Registration. If you want to come play. And I really hope that some people here do. It's going to be in the contest area. Just come find us. Register. And then you're good to go. I have no idea. How many users this will support. I've played with probably about 15 different players. And it's gone pretty good. I don't know if there's going to be more than that here. I don't know if there's going to be more power users. We'll see how it goes. At some point I may have to limit it. If I see it starting to slow down. Because as I mentioned. The tick won't end. Until everyone's scripting is done. And a tick will never end. It will only start until that first one is finished. So if I get 100 players. Then that's 140 minutes worth of processing time. If everyone takes up their limit. That they get off the beginning. So it can be a fairly long time. Which normally a tick would last one minute. So my prizes. To the person who gets first blood. The first person to attack in this game. Postgresql. Themselves. Have donated some nice swag. They've given me a t-shirt to give away. And some posters. Commemorating the release of 9.1. Navicat has given me one license. Of Navicat Postgres. Which is a pretty sweet piece of software. For Mac, Windows, etc. I'm not going to be a salesman here. And I have one free admission. To Postgres West. Or PG West. I should say. It's happening end of September. 27th to 30th. I thought that was a great prize too. Not everyone might have the ability. To travel again in a month. That's why. Some of the prizes might be a little bit. Kind of decisive. Once we see some results. And I've also got an extra $50. American. From an anonymous donor. Who will basically. Whoever he sees fit in the game. Will get it. So those are my prizes. So finally. How do you actually connect to this game? However the hell you want. It's a database. On the net. Anything that can connect to the database. Be it any language you want. Python, Pearl, PHP, Ruby.net, Java, TCL. I super don't care. Use it. It's fine. Any database client you feel comfortable with. I'm going to say pgadmin. Might be your best bet. Because I know pgadmin has SSL. So I would suggest you use that. If you have a cat. Will automatically pipe it through some SSH for you. So that could be useful too. Again. We are at DEFCON. Wall of sheep will be plentiful with schemaverse accounts. If we're not careful. And I'd rather that not happen. So as you can see. Under my host. DB.schemaverse.com. With the super secret host name. That you use to connect. And it's just on a regular port of 5432. So. If you want more information. I've got website. DEFCON.schemaverse.com. And then I've got. Some project homes. That said it's open source. Now. My talk has ended up pretty fast. I apologize for that. I thought it was going to be too long. From yesterday. So I'll kind of talk a little bit more about strategy. And how to win if you actually want to. I do know some people. Who instead of. Using my tick system. Or the actual fleet system that I've developed. Instead of spending. Wasted money on that. They actually just write Python scripts instead. To run everything for them. Every minute. Roughly the same thing. You don't necessarily know when the tick ends. But you can always pull the system. And find out if there's a new tick. So you can be fairly creative. With how you play this game. Since I've got time. I'd love to answer questions. If anyone has it. Yeah. How do you find nearby planets? Nearby planets. You can see every planet in the game. Using the planets table. Well it's a view. Oh sorry. How can you see. The question was how can you see planets within range. And originally there was a planets in range view. And then I realized. Well we can kind of see most things. And it makes sense that you can kind of see most things too. So. Basically any planet that you want to see. Is in the view. And you can also see who's conquered it. So if you want to be a jerk. And target one of your friends. Then you can go find what he's got. And go attack it. Other questions? Over there. Sorry. Yes. You can conquer another player's home system. Now keep in mind. Most of the planets were developed very close to the center. And then as you get out of the kind of center area of the map. Then it becomes more and more sparse. So your home planet. It might actually be impossible to find. If you upgrade the ship speed completely. It might still be impossible to get to another planet. So it might be protected. But. If you can find a sequel injection. That gives you their home planet. Then that could work too. Right. Any other questions? More questions? Alright. How long have you been on this project? How long? I've been working on it for about a year and a half. It's 2300. Oh sorry. How long have I been working on it? 2300 lines of sequel code at this point. Couple hundred lines of Perl. You can see my git repository history. Which is like. Activity down. Activity down. It's been a fun pet project. It actually. No one knew about it for ages. And then. Merlin on PostgreSQL. On the mailing list. Found it. And was like hey this is awesome. But it wasn't overly tested at that point. This is last December. Which is great for testing. Because people basically just connected it. Just brutally destroyed it. Within a couple days. Which is great. Reroad everything. Most things at least. And now it's in a very more stable. Actually playable time. It was basically beforehand. It was just you've got some chips. You can go around and blow some things up. But now there's actually like gameplay to it. Other interesting things. If you wanted. You could actually add a goal in the game. The trophies table. Or the trophy table I should say. Is something you can insert into. Every trophy that I showed earlier. You've got space jerk etc. Space jerk is just the best one. Pillager is pretty good. Anyways any of those. Are just sequel statements again. Just like the fleet system. Except these ones have a little bit more control. Because they can actually go into all the tables. Your player can only see the views most of the time. But the trophies can see every table there is. So. If you want to make up a trophy. You can actually add it into the game. And if I allow it. Because there's nothing ridiculous in the code. Then it will become a trophy. That will get calculated out at the end of the round. Pretty fun. There's also items in the game. I didn't really talk about it. Because I haven't had time to create any items. But just like the trophies and the fleets. It's just sequel code that you throw into a table. And you're allowed to insert. Into the items table. So if you can insert into that table. Create an item that you want. I'll see who made it. I'll see which user created this. And I might be nice and give you this item. And then I'm going to scatter them around the universe. And you can find them. So the game is. Can be a little bit more complex. Or you can. You know. Take it how you wish. Any other questions? Good question. The question was. What happens after the game ends. Users. Users stay. Players stay. All ships are destroyed. Planets don't move. You get a new planet though. Kind of change things up. All the fuel is redone on all the planets. And all the events and everything is destroyed. Basically. Originally I wanted to just kind of be an ongoing game. But. After playing it once. For a while. It didn't really make sense. Because at some point a new user just can't compete. So. That would be kind of interesting in its own way. The question was. What happens. At the end of a game. When basically planets are re-given out. But you do lose your original home planet. Which I didn't specify. Yeah. No? Question? Alright. Well. I guess I'm done early. But if you do have any further questions. That you want in a more intimate space. QA room 4. I believe is my room. Or if you want to complain about move to me. That's fine too. I hope you come to the contest area. And sign up for the game. I'd love for this to become successful. And I'd love to bring it back to DEFCON 20. I think that would be fantastic. Anyways. Thank you for coming.