Welcome back to the second day of live CTF here at the DEF CON CTF finals. I'm Zita2 and I'm here with Mark. Mark Griffin here. Yep, so we're jumping back into it. Same format as we've been doing. Excited to see this challenge. Yeah, this is going to be great. I think let's not keep this for long. Let's get the game started and then we can talk more once they have started looking at the challenge. So I'm just going to get a final confirmation from production. We have that. So we're going to start the second round of the second day in 5, 4, 3, 2, 1, go. All right. Let's jump over and see. Yep, let me just find this. So we have the teams participating in this match that we're casting. Shellfish versus TWN48. Yeah, this is in the lower bracket of the tournament. So whoever loses this match is unfortunately eliminated from the tournament. Yeah, there's definitely a lot on the line. But with the double elimination bracket, they also have the chance to go all the way back up and win the whole thing. Right. So maybe they just had a bit of a rusty start yesterday and now they will just go all the way back to the top. And we can see we've got Shellfish over here already digging into the code. It's a Python challenge. And so they've got the whole challenge right in front of them. They can read the code. They just have to figure out what to do. And so I think we saw a prompt already. And this is an interesting problem. I wanted to do a riff on a standard Python jail. So Python jail problems are, hey, we're going to establish some sort of constraint that makes it hard to do arbitrary things from within the jail. Instead, this one is more of a, it looks kind of like a jail, but it's actually a bytecode problem. Right. So roughly they have to provide some Python code that goes through a filter. But then after the filter, they are allowed to swap out the bytecode, which is part of the whole code object. So they can basically modify the code object. And this way they can then change the code from doing something that passes the check, supposedly something benign, into something malicious that will get them there. Yeah. And so there's not a ton of code in this. There's enough that it's, you know, they've got to take a little bit of time and read through it. There's some stuff that sounds like it's debug related, which does kind of mirror real world problems where developers leave in some extra debug functionality. They didn't intend it to go into production and can be leveraged. Yeah. So a little bit of... So I was attempting to try to solve this challenge. I also noticed that, well, we don't have the camera on, but our banner is falling down behind us. But that's fine. We're overcoming today. Yeah. No, but what I was going to say that, yeah, there is like a bunch of stuff in the code, almost like, you know, decoy code or like stuff that's not quite relevant. Which I think it's, we kind of balanced this a little bit by most of the challenges we have are like compiled machine code. But very, very minimal, like pretty much all the code in the challenges are relevant to the solution. In this case, it's Python. So it's like by default, more readable. But then there's a little bit more stuff there that you don't actually care about. Yeah. And it's just trying to toe that line, right? Get it so it's not going to be solved in like five minutes, but it's not too hard that you can separate the chaff from what actually matters here. And we've already seen the competitors looking at that, the code module, which allows you to build these Python objects, which is ultimately what we're going to see modified. Right. And if we switch over to Shellfish here, you can see that they are playing around, having a more dynamic approach. You know, trying various inputs. You saw that they were running the program with various inputs. You see like the answer is they are looking at what different objects are available. This looks to me a little bit like the approach you would take if you were to the jail challenge. You're looking at like what various attributes. In Python, there's a lot of like reflection and like various ways to access, you know, like parent objects and global kind of meta objects and stuff like this. Exactly. And, you know, with a problem like this, I certainly expected that maybe they could come up with something that I didn't expect and they'd bypass the check and win that way. I'm not open to anything here. These competitors, not only have they seen and studied Python jail problems from the past, but they may even have stuff in their back pocket that is non-public, you know, bypass type stuff that could come into play here. Yeah, definitely. I mean, it would not be the first time we saw, you know, zero days and so on at the DEF CON CTF. So, yeah. Let's switch back over to TWN and you can see them like going around in the code. I feel like they are taking a more static analysis approach to the problem. And they're also, though, because this is a Python problem, it's very easy to modify. And I think I've seen both players do something along these lines, but certainly TWN48, they're changing the code at least to make it more readable. But they're also thinking, hey, I can just skip this or add an extra print here, changing the challenge a little bit so that they can understand what they've got to deal with. And you can see the comment there, cannot call built-in and function. So they're like, you know, kind of like figuring out the implications of the constraints that are given in this problem. Also, like looking here, cannot use DEF and class. So they're like, you know, just trying to figure out what the playing field is here. Yeah, and both competitors immediately trying to find the bounds of what they can do. I think that's a great approach. It is kind of interesting to see what the limits are, because the limit is done at that bytecode level. So they may not be used to seeing that. I know that writing this challenge, it was interesting to see. I was checking a lot of different things that competitors might do and making sure that the filter was sort of going to stop those things. Right. We saw there that CWM, they were trying to define a lambda function, but they were having some problems with that. So, you know, we'll see how that turns out. If we can switch over to Shellfish here, I see them highlighting some of these functions. This is really some keyword checking is what's going on here. And you can see that there's some different if conditions here. I see that they've highlighted AI log text, which looks like it's logging. There's also another function you can see sort of sandwiched in between those that's different AI log code. And that's really the it's a little bit of an innocuous sounding function, but really what they're going to be most interested in. Right. Yeah. So I think a lot of the, as we kind of mentioned, like a lot of the initial work here will be focused on trying to like separate, you know, what are we actually interested in here? Like which part of this code actually is relevant? Yeah, and this is this is actually a problem that I've been very fascinated about recently, which is when you're looking at a whole file full of Python, how do you understand how these functions relate to each other? How do you understand the overall whole? I actually feel like the linear view, actually reading the code is a pretty inefficient way to do it. If you could see all of the functions sort of arranged, I feel like it would give you a big advantage in this case. Right. That's, you know, get some kind of flow view, some flow visualization, like a call graph and, you know, something like this to help you understand the flow that these things are going through. Yeah, because in this case, there's really one core module here that that is being used for that bytecode manipulation. And so obviously anything related to that module is going to be very interesting to the compiler. And speaking of that module, we can switch over to TWN again. I saw, let's see if it comes up again, but it looked like they were looking into the documentation around the code module. And, oh, it seems like... It looks like they're trying to figure out what keywords do I need to put in to get to the different functions there. And so they were looking at AI log code, I think. And they're adding print statements again to make it easier for them to debug. Hey, how do I get to this spot? And I think, yep, you can see that they're enumerating what each of these words as they relate to a bit field, they're sort of like binary flags packed. And they're seeing that this is a function of interest. And I think we're on the right track here to see them explore this. So it was cool to see how they went from testing the bounds of the Python jail to enumerating what these keyword functions were. Yeah. And maybe start trying to figure out how they can circumvent these things or what things they are going to need to care about more here. So this AI log code function, what is it doing? Yeah, so really you can see the argument input obj is the same code object that they put in earlier. And so it has to pass a filter check. But then you can see, if you get a good look at the code again, it also allows the user to, so that defend the eggs prompt is saying, hey, take in some more input here and then we're going to do something with it. And it looks like they're exploring exactly what they can do with it here. But it's going to modify that code object. And so that really the path to exploitation here is to understand that you have to set up an object that has the pieces that you want in order to run your payload. But it still needs to bypass that filter. That's very nice. And I think we just saw here that they just tried inputting this hex string here. They just took like the string hello and sent it as this additional input. So now we have like a Python crash. Right, because it's raw bytecode is what is being selected. So it's platform specific, interpreter specific. I expect that they will quickly move from trying to put raw hex into it to getting some sort of bytecode production. And this is a little bit of a challenge when it comes to solving this, right? Because they need to kind of match the version running on the server, right? For the Python. That's right. I believe there's a comment in the code that says what it was tested on. And they can verify that. Yeah, I think it will print out the version. So if they connect to the, if I remember correctly, like if you connect to the server, it will print out the version as well, right? You can also query using this. Right, since you can execute code. Right. Yeah. That was the way we made it so that they can use the remote servers to get the rights. And I want to go over back to TWN because I saw that they had a, you know, here they're like printing the, like they have the disassembly of the bytecode. So you can see there's a load constant and return value. Right. There's a hidden debug function that allows them. And so, again, this is trying to point them in the right direction. Hey, we'll show you this object, give you an idea of what's in it, which should help you understand how to solve it, which is, you know, modifying that bytecode as we talked about. Right. And maybe just, you know, to kind of explain this for people who might not be like too familiar with Python. So like the Python programming language, you have like a bunch of different implementations of this with the kind of standard one that most people use is CPython, which when it executes Python code, it will take the human readable text code, do all the parsing and so on and compile this into an internal bytecode format. And then it will execute that within its virtual machine. Right. So they are allowed to modify like not the text code that they send in, but like the bytecode that the interpreter has produced after, you know, parsing and so on the code. That's right. It's almost the lower level language. So you're typing the text, your print hello world that we're seeing, but really what they're going to want to do here is they're going to want to take the code that they want to run at that high level language and then get the bytecode that does the equivalent and then stuff that into the code object that they're able to modify. Yep. And let's see if we can see what's going on here with TWN48. They're looking at the filter again. They've got the debug output going on. So they're starting to think about, okay, what is really possible here? I'm not sure that I've seen them look and see if they've found the code modification bit, but that is the first hint that we tell them that it's perhaps not the problem that it first appears to be. It's not a Python GL problem. It's more of a patching problem. Right. So at this stage, I would say that Shellfish might be just a little bit ahead in the progress. And let's switch over to them and see that they are, in fact, I think you could see it there very briefly, but they were compiling some code there in the script. So they have this test script. You can see there. So they have these two calls to compile command. And they have two different code inputs. I think this is very much along the right line. They are trying to, well, if I would guess, they might be trying to produce two different text inputs, which result in maybe the same byte code or different byte code. And they're trying to see how changing the text of the code changes the compiled result of that. Exactly. You can see that they've got this fn.replace.co.code. That's exactly the construct that they've got in the challenge. Right. So they have basically taken the whole big program and they have distilled it down to the core thing that's going on here. And then moved that out into a small test environment, which is excellent. Great approach. And you can see in that other terminal window there, you can see the byte code, that load name, load const, very simple operations there. But it also is going to refer to these strings, for example. The strings aren't in the byte code. They're being referenced. And so what they're determining now is how do I end up with the things that I want to have happen? The references have to line up. Right. And I think you can see that they're printing out these various attributes of the code object. So if I were to take a guess, they realize that they can only change those... The byte code. The byte code. So they need to make sure that all the other things agree. Exactly. But if we go back to TWN, we can see here that they're also looking at the documentation for the compile command function and doing some... Yeah, looks like they're looking right at it. Yeah. And it's funny. If you look at the documentation for this, a lot of the documentation is kind of like, don't poke around with this. It's negative and you should be using other stuff. Yeah. But here we're a competition for hackers and that's what we're asking them to do. Right. And you can see here they were copying the system command and trying to... They have here a piece of code that they want to run. So this is kind of the goal, I think. But now I guess they're trying to get... this looks like they were trying to figure out the byte code of this. So what they probably kind of missed there is that they need to compile that code into code object and then get the code attributes. Yeah, they're definitely in the phase of understanding the primitives that they have, the code module itself, and what they can do with it. Right. And I want to switch back over to Shellfish because you can see here now there's some progress on their script here. You can see the two pieces of code. They have print high and import OS and now comparing these two things. And what I think is an important thing that they will want to look at is this list of constants. The CO constants there, you can see there is a high in one of them and... OK, no, they're only printing one object, so it's high in all of them. So, yeah, definitely look like some progress there. But then that's exactly the approach, though, is they're looking at, hey, I've got two objects and I understand I can only change one of these other things. What do these other parts need to be so that when I do make the swap, it'll all work? Right. I feel like they're making great progress and they're on the right track. Yep. And they're here again looking at just a little bit of documentation for the compile command function. Yeah, this is actually a key detail here. That compile command, the third argument there, the eval argument, controls how the input is going to be parsed, whether it's a single expression, whether you can do multiple expressions, a whole block of code like a module. Right. And this explains why, so it's set to eval, which is a single expression, which explains why earlier when TWN were trying to define their lambda function, because an assignment in Python is not an expression, it's a statement. So you cannot have that in an eval context. However, with the new walrus operator that was, well, relatively recently introduced in Python, you can have an assignment as a statement. I actually wrote a little bit of a blog post about this, how this enables you to write certain types of these Python payloads more easily with this. Yeah, so if they would have changed the code that they had, it was like A equals lambda something something. If they would have just added a colon to the assignment thing there, that would have worked fine. They would still not have been able to call that function, but they could have defined it, right? Right, and that's exactly what I was talking about. When you've got competitors at this level, I totally expect stuff like that. The walrus operator was not something I was thinking about when I was writing this challenge. But it's modern, right? Python 3.10, we've got some extra tools in the toolkit. Right, and if we do switch over to TWN, we see that they are looking at these code objects. You can see that they did compile a piece of code, and they were looking at what different attributes. And now googling again about the code attributes. Yeah, and that co underscore code, that is the key to zero in on here, so I'm glad that we've got both competitors looking at that. And that's just the byte code. Right. As we've talked about. Yeah. So the larger code object has these other pieces. I think that they've both realized they have the ability to modify that co underscore code. So at this point, it's off to the races who can figure out exactly how to use that to gain execution. Yeah, and I feel like, even though I still think Shellfish is a little bit ahead, I feel the gap has become smaller. Definitely. Because TWN is definitely in the right area here, looking at these, compiling this code, looking at how it behaves in various contexts like exec here. So yeah, it's definitely still very much an open game. Yeah, I think that we see this in all sorts of challenges where one competitor will find something. That's the insight that they need. They'll sprint ahead to the next roadblock. And then it's really, it's not over. They get a temporary advantage, but then the other competitor can bust through that insight and then maybe overtake them really quickly. It stops and starts. Absolutely. Looking here at Shellfish again, you can see now that they have the thing they want to do down there. There was an interesting approach where they make use of a second layer of evaluation. So it seems like their goal is to just take the thing they want to execute, put it in a string, and then in their initial code just print that string, but then in their second code do eval on that string again. That's right. I really like this approach. And whenever we see somebody jumping back to the rule page there when they're close to the end, we are watching out because that shows them exactly what they need to run to win the challenge. It looks like they're very close. They've got the sort of secret keyword there, which is omelet. Again, I don't think that's the first time we've seen reference to eggs and shells. So Python shellcode kind of challenge here. I think they were going back to the info page because we released the hint there. So you can see the hint that said this is not a jail problem, it's a patching problem. These competitors both see what the problem is. I think we're pretty close here. It's going to be really cool to see there if they can get this going. They seem like they're making some small changes to the exact code they want to run. Yeah, try and subprocess instead of OS system. Right. And you can see here they are going straight for running the submitter binary instead of popping a shell. So that's a good opportunity to talk about how the goal of this challenge is to solve the challenge and it will always end in you getting code execution on the server. And then on the server you have a small binary called submitter which you need to run to kind of prove that you have code execution. So most of the times you usually just go for like popping a shell and then you manually run that binary. But here you're baking in the submitter run straight into the code. That's right, yeah. And the reference solution that I wrote was more of the shell approach. But I can see that just looking to iterate, get that submitter run, feels it is close. Yep. And over on the TWM side, we have again some more progress around investigating these code objects, compiling stuff, printing these objects, trying to like... It seems like this person is not that familiar, so they're trying to just understand how these things work. What are these attributes and how do they interact with each other? Right, looking at that eval, understanding exactly how it works. So I'm dropping the keyword omelette in there and trying to understand, okay, what does this need to be? Is this going to cause problems if I need to have it in there? But yeah, zeroed in on the ALogCode function so that they know they need to get in there. It's just a question of what's the next step. I actually want to go back again to Shellfish because I saw some really interesting progress here. You can see they compiled the code, they're inputting this, and they win. I think they ran it. They win. I think they dropped the hex bytecode in there. Do we have the confirmation? Yep, Shellfish won. Shellfish takes it. So very, very good job. Let's switch over here. It went from they kind of had it and then suddenly they won. So that's really, really cool to see. Great job. Congratulations, Shellfish, advancing to the next round. Unfortunately, that means goodbye to TWM. Yeah, best of luck to them in the rest of the finals. But this was really a great challenge to see. Both competitors start moving down the right track. Really, I feel like they kept up their lead. But this was actually one of the problems that I think we thought internally was potentially a harder one that might have gone the distance. Oh, absolutely. I think because it's like, you know, maybe not something you're not quite as familiar with. So, yeah, definitely... Yeah, could have been definitely a challenge. Could be stuck. To be fair, we do have... So in this tournament, this is a good time to mention this, is that we are running two matches in parallel. Right. So we are commentating one of the matches. But then there's another match going on simultaneously. And that one has not finished yet. Yeah, and all of these competitors are from DEF CON finals teams. So everybody is top, top tier. We do give the teams a little bit of a heads up in that they know that this is a Python challenge coming in. So they're probably sending in their people who are the very best on their team at dealing with Python. Yeah, totally. And I mean, it's really, you know, the pressure, the stress of being there. Just you, no help from your team. You know that you're kind of like representing your whole team. And there's a bunch of points on the line for this. It adds up. Right. So definitely not an easy task to play these matches. Yeah, a lot of pressure. And again, you can't, it can't be understated or overstated how much the fatigue is going to set in. We're in day two of the finals. Tomorrow is going to be even tougher. Yep. And before we go to a break here, let's bring up the bracket. We do have, as we said there, Shellfish beating TWN. We still have the Orge crowd versus Pigbuttsad going on in the background. So, yeah, the winners of these two matches actually will face off in the next round, which will be coming up... Very shortly. Very shortly. So it's going to be exhausting for the players. Yeah, that's tough. But I think when we were putting the bracket together, it was, hey, somebody is going to have to play back to back to jam in all of these matches. That's just the way it's going to have to be. So I think that's it for now. It was a great match. Really cool to see these teams approaching this problem. And, yeah, we will go to a break. We will be back with the next match in a bit. You can check out, if you want to see the raw feed from the other match, we're just streaming the screens without any commentary. You can go to livectf.com and there will be links there to the YouTube channel and so on. But otherwise, yeah, we will be back in a bit. Yeah. Yeah, great match. Great commentating with you, Carl. Yeah, it was great. And I will be back for the next match. And, yeah, they will see you later. Yep. Great. Till then. Bye.