Hey everybody, how's it going? My name is Michael Torres. I'm going to be talking about, uh, pseudo for Windows. Title of talk is Pseudos and Pseudonts. Here's a quick overview of what I'm going to talk about. You can read so I'm not going to bore you too much, but first we're going to go over what pseudo for Windows is because it sounds weird. Then we're going to hit, uh, some research, initial research on it, how it all works. Talk about some non-security issues according to MSRC. I'm supposed to use those words. Uh, then we're going to talk about some security issues that we found. One of them is unfortunately still under embargo. Talk more about how I found that one later, uh, but let's hit it. So who am I? Uh, I do operational technology security at Google. I also sort of do cyber depending on who you ask for the United States Marine Corps Reserve. I also volunteer my time with VETSEC which is a profit focused on helping, uh, veterans of the armed services for the U.S. and allied countries get careers in cyber security. I also just do random research projects like this that are not related to any of that when I get bored. So here I am. So what is pseudo for Windows? They, uh, Microsoft announced that they're going to introduce this utility in Windows 11 in the H2 update which usually drops around September, so in a month or so. It's currently an insider preview which is how I got access to test all this good stuff. The way it works at a very high level is it uses UAC, that standard process elevation thing to support the type of command line features you're probably used to if you use pseudo on Linux. So taking your input, your output, all that, putting it back into the prompt that you originally ran it in. Uh, that's both console input and output which are different on Windows than standard in, standard out, and standard error. It does this via fancy pipes and redirection and all that good stuff. It's written in Rust. It is now fully open source. At the time they just, at the time I did this research, they said it was open source but it wasn't published yet. Uh, which meant that I did a lot more work than I actually had to do but that's alright. That's what, that's what's fun I guess. Uh, I found two issues with, again, according to MSRC, no security impact. I have to use those words over and over again. One is a memory corruption issue which is fun cause like I said it's written in Rust. Uh, and the other one was a search order confusion issue. I found two issues with security impact. One allowed, uh, cross user code execution which, kind of sad, you don't really want other users to run code as you if you like your secrets being yours. And the other one allows you to data tamper the input and output data for sudo. And again I can't go into too much detail on that one but we'll talk more about it later. Most of my research was conducted against sudo version 1. Uh, you'll note that both of these bullet points say sudo version 1 but have different signature dates. I can't help you. They didn't increment the version number so I just had to use the signature date on the binary to distinguish the two. Uh, one is from February, one's from March. I don't know if they've released a more recent update cause I haven't checked in the last three weeks. So they might have but three weeks ago it was still the version from March. So precursor things to know. User account control. If you use Windows, which I assume most of you do cause you came to a talk about sudo for Windows, you've seen a user account control prompt, right? You always just hit yes cause you don't care what you're doing you just want it to work. Uh, the way it works is normal processes without elevation run in what's called medium integrity. That's pretty much everything except for processes or systems that are designed to use low integrity which quick shorthand is basically just web browsers in your PDF reader because they want that to have the absolute least privilege they can. Elevated processes, so the thing that runs once you hit that yes button cause you don't care you just want it to work. Uh, run as high integrity. Only processes running in high integrity are allowed to modify system resources which implicitly means that they can get system level permissions which is roughly equivalent to root on Windows, right? It's considered a defense in depth measure by Microsoft. They do not consider it a primary control. They do not consider UAC bypasses a security issue. They won't service them. They don't consider them part of bug bounty. Importantly for why sudo for Windows needs to exist, you can't spawn a process with UAC and inherit the standard in standard out standard error or console input and console output into the privileged process meaning that if you're in a command prompt and you try to run you know reg query as a privileged process, you can't then pipe that into another command or redirect it into a text file or something like that. It'll spawn it as a new process that can't put it back into your same window. Other things to know about uh the ALPC interface. It's an inter-process communications framework. It is officially undocumented and only used by Microsoft internal systems. The way it works is the server listens on a port which is just an object cause everything in Windows is an object. That port is just a string name. Sometimes it's a string as is the case with sudo. You can see the example there. Uh sometimes it's a GUID if it's a com object or things like that. The way that security is supposed to be implemented on ALPC is that the server can query attributes of the client like the process ID, the uh SID, the principal name and all of that of the caller. And the client is able to say I only want to connect to an ALPC server if it's also this SID or some other properties that I don't remember frankly and they're not in the slide so I'm not gonna remember them right now. Um the other thing to know is that in Rust if you want to use Windows native APIs like create process or shell execute X which are important if you're trying to run a process as an elevated user. Um you have to use one or you don't have to but Microsoft released two crates for it. Windows and Windows sys so those are first party owned by Microsoft. They both require you to run in unsafe Rust meaning that now we can get memory issues and buffer overreads and buffer overruns and all that good stuff. Um the only difference between Windows and Windows sys is the Windows crate does type coercion for you. So if you have a Rust string and you want to call a Windows API if you use the Windows crate you just give it the Rust string and magic happens. I don't really know Rust that well so sorry it just magically happens to me. Uh whereas with Windows sys you have to explicitly cast things into the right data type to get it to work in Rust. So Microsoft's blog post did actually include a fair bit of detail uh which was cool to see uh both from a security research perspective and just a curiosity perspective. It's cool to see them publish information about their tools like this. Uh there are three modes that Sudo for Windows supports. Those are new window, input closed and inline. The new window one is basically just the same as if you ran run as and then the command. So there's not really much novelty there. Uh the input closed and inline modes do this weird client server thing which you can see at the bottom uh right there. That sort of high level thing. We'll go into that more in a second. The difference between inline and input closed is in input closed the elevated process can't see the un-elevated console's standard end. So if you were to pipe the result of a command into a Sudo execution and your system is configured to run it in input closed mode, your elevated process won't be able to see it. They say that those are in order by most secure to least secure so new window being the most secure and inline being the least secure. Um they don't really go into a lot of details about that other than that it's it keeps things open for longer. Um again I mentioned earlier it in the blog post they say it's open source. They link to a GitHub repo. That GitHub repo was empty when I was doing this research so I just got to look at Rust code in Ghidra which if you haven't done it I don't recommend it. Um but now it's open source. You can go on GitHub and look at all their stuff. Um they included that diagram as I mentioned to explain the request flow. We'll go into that more in a second but you can see that there's a client server thing in there. So off the rip like this was actually found after the rest of this research because I was finally able to look at the source code. The new window mode still creates that RPC server so you're still able to do fun things with the RPC server. Um it doesn't always do what's on the tin. Sometimes it does actually pull in your standard in, standard out, standard error in your console input and console out and pipe it into the new process. Um again it's supposed to be the most secure uh but it does always get the standard in handle unlike the input disabled one. I don't know of any ways that that's necessarily a security issue but it's kind of an odd novelty because they describe it as being the most isolated from the unprivileged process perspective. So I started looking into this because I saw on Hacker News that veritable Y Combinator uh news aggregator that one of my co-workers at Google actually which I didn't know at the time made a blog post about Sudo for Windows and published some research. Um he pulled apart the RPC server some and found that at the time that RPC client server thing had zero authentication which is obviously very bad. That means you can just tell the server you would like to run a privileged command and it'll happily do it. Um that was fixed by the time I was testing it. Note that that's in quotes. Talk more about that in a minute. So I talked to again it pulled apart that RPC like client server API if you will and highlighted the methods of interest to call. Specifically that server do elevation API. That's what actually is called by the client into the server to coordinate execution. Passes handles that the process binds to and then you can get all your input and output and all that good stuff. It also passes a bunch of parameters relevant to the command being ran like the command to run, arguments to the command, the current working directory and optionally the environment depending on the flags you pass to sudo. Here is a more detailed look at run mode three in line mode. This is the one I did most of my research with because it is listed as the most insecure so it's probably the easiest target right? I'm lazy. Um you can see on the far side there you have the actual unprivileged console where the person runs sudo who am I and they're trying to run it as that elevated user. The way that works is it first spawns an elevated process to host that RPC service using normal run as. The user gets a prompt again they click yes like everyone does because they just want the thing to work. Um the client then sends an RPC into that privileged process telling it what command it wants to run. The privileged process binds to the handles that gets passed in there so that all your input output and standard error stuff just flows naturally. Privileged process that the user specified gets spawned and then magic happens you can do all your normal command line pipes and stuff. So summarizing everything we just talked about sort of high level notes. Uh it's written in Rust it's open source. Uh Rust requires you to enter into unsafe mode so again memory safety issues can happen. Uh to call Windows native APIs which things like this kind of have to do. Um and ALPC is what's used to coordinate the actual execution of the user specified process. So our first issue the memory safety issue in Rust. Um sort of odd right but as I just mentioned you have to use unsafe APIs. Uh they've removed this functionality before they published it to GitHub so I can't tell you exactly how it happened but at a high at a from my analysis what happened was if you gave a path that rewrote from the beginning meaning that you had a leading like backslash or C colon backslash right? It would write your path to the start and then when it passed it to an API function it didn't truncate at the end of where your path was it just read until the end of the buffer to the first null byte. I think that that happened because they assumed that a path as string would point to a null terminated buffer as you would kind of expect it to if you're used to C when in fact it just points to a vector of U8s that may or may not include a null byte at the end. Um here's sort of my mock up of the code uh you can pull these slides off the Defcon media server if you want to take a better look at it but this is just sort of demonstrating what I just talked about where if you assume that you can take the path as a string and then just get a pointer to it and pass it to the Windows API function you'll end up very sad because it'll just read until the first null byte not necessarily until the end of your target string meaning you have buffer over read in heap uh for arbitrary numbers of bytes that you can't necessarily control but because you can control the path you can control approximately how far into that heap buffer you read. Uh so like I just said you can read everything up to the first non-null byte you don't control which heap allocation you're reading and it doesn't let you write memory into arbitrary things that are outside of your heap boundary because it is rust it's a vector uh so it'll reallocate if you reach the capacity of the current heap allocation um but still you get to read things from previous heap allocations which in my testing tended to just be paths but it could be something like a token or something like that. Um it says at the bottom there why uh it's not really a security issue uh the main problem being this is only noticeable if you run pseudo with an unpro or a untrusted input which you shouldn't do anyway so it's not really security impacting if you run pseudo with arbitrary things someone you don't trust supplied and then you can read other memory because you shouldn't be doing that in the first place because you're going to execute someone else's code. This screenshot shows what I was talking about so you can see on the left there I run pseudo with a leading slash and then a path and then you can see in process monitor that at the end of the day a create file operation or trying to read the file um ends up re trying to read a file that's the thing that I was trying to read and then a bunch of things from presumably a previous heap allocation because that's the path that's the current working directory and then some garbage at the end until it reaches the first null byte. So the second issue uh search order inconsistency so in Windows just like actually I shouldn't say that I don't know if it's true in Linux but in Windows if you run a command without a path it is supposed to execute that command in the current working directory if it's present and then search your path. So if you run pseudoprogram.exe you expect it to run the program.exe in the current working directory not some other random program.exe. Path resolution didn't work that way the way it worked is again because of that client server thing it would try to resolve the path in the context of the privilege process which had a different current working directory meaning that you could end up running a command you didn't expect to run. Uh says up there it's a somewhat contrived example but if you had compiled your own binary and named it cmd.exe and then you tried to run pseudocmd.exe it wouldn't run your command.exe it would run the system32 command.exe. According to Microsoft it's not a vulnerability I have their exact response quoted there at the bottom. But they fixed it anyway. Um so they more or less looked up the in the unprivileged process they looked up the command you gave them and resolved it and then passed the fully resolved path into the privileged process. Um they fixed it I'm not going to complain too much but it's kind of odd that you accept input from people on what issues are fix them and then tell them yeah but we don't think it's a real issue. And this is a very well drawn I'm an artist if you didn't know uh example of how that would work from an attacker's perspective so you try to run command.exe you expect it to run yours it ends up running some other command in the privileged process path which isn't the one you wanted. So let's talk more about that RPC server again it's ALPC um source code wasn't available so I had to look in Giger to figure out what all these parameters meant. Uh I got pretty good results out of it I didn't know what those handles were or how to deal with them necessarily but luckily I didn't have to you can just bind to input and output without dealing or without worrying about the handles too much. Um importantly the unprivileged process ID so the one that you run before you hit that yes prompt as quick as humanly possible is what is used to create the socket name so it's predictable as long as you know the process ID that's uh trying to run sudo. And again I mentioned earlier after James Forshaw's blog post they added an authentication check which was checking that the client's process path so that unprivileged process was the same as the server's process path meaning both are running sudo.exe see windows system 32 sudo.exe right? Um over on the right side overview of that that off check on the right right just is the image name of my calling process the same as my image name if it is good to go we'll run your command. Which brings us to our first security issue. Um it turns out that that's not really a good authentication check. Um any user can issue elevation requests to that RPC server meaning that any user of a multi-user windows system to include you know unprivileged users because anyone can call an LPC socket like your web browser um can go ahead and run commands in the context of a user uh when they run sudo. So it's not latent I and a privileged user would have to try to run sudo and then you'd be able to exploit this to run it in the context of that user. Exploitation requires two things so like I mentioned insufficient authentication um the way I did that is I just injected a DLL into a sudo.exe sudo process that I controlled so that I could do whatever I want in the context of a process whose image name is the legitimate sudo.exe um and the other part of this was that predictable uh port name that predictable RPC control port name. Because users can view the process IDs of other users in Windows uh pretty much no matter what there are some types of tokens that can't do that. Uh you can get that information very easily and then you can just call the target sudo privileged sudo process and have it run whatever you command you want and gain execution in that user's context. This was patched in the version in the second version one of sudo the one that was uh code signed on the 4th of March. So again high level attack diagram you see at the top there we have our victim who's just trying to run sudo and get on with their day. We have we have our attacker with again I'm an artist very well drawn horns thank you. Um the attacker starts their code that's just waiting and find trying to find a victim sudo that is running and then as soon as it notices one it spawns its own sudo with a hooked method to connect to the target RPC server instead of one that it spawned uh to run whatever command the attacker wants. From the victim's perspective what this looks like is they ran sudo they as fast as humanly possible because again they're just trying to get the job done hit the yes button on the UAC prompt and then nothing happens from their perspective but in reality the attacker was able to run whatever command they wanted. Uh demo I think? Do I have that wrong? Hopefully it plays. Alright well that was surprising. So here I'm just showing there's nothing up my sleeve um uh the left prompt is gonna be our victim user so who am I? I'm MTU um it can run sudo just fine. The right prompt is now my unprivileged user or my non-administrative user not MTU. Right now I'm gonna show that uh sudo works and then that the base prompt is not running elevated. And then on the right on the right we're running that implant code that's just gonna spawn some stuff uh to try to target that victim sudo process. We are racing because if we don't beat the legitimate process um the RPC socket closes and we can't run our command. So we go slowly insane trying to win the race. I did test this in a debugger first so I was pretty confident it was supposed to work but man recording this video and just sitting there doing this for 2 minutes was not fun. Um at the end there you can see that it worked on the right side our unprivileged process again we were able to inject into that victim server process and run command.exe as this as the unprivileged user. We run whoami and we see that we're now running as that other user who is just trying to run something with sudo. Well I already played the video but we can play it again if you want. Oh nothing showing up there? Neat. Let's try it this way. Can you see this? This video now? So again uh again left prompt is the uh privileged user who's just trying to run sudo uh the right prompt that's about to spawn is the unprivileged user not MTU some evil attacker man. Um user runs command everything works. Let's see if I can skip this bit a bit. Um user keeps running command they keep working because you'll notice on the other side the attacker process is failing it's not winning that race. Uh like I mentioned I confirmed this in a debugger but still very frustrating watching it fail over and over and over again. Um but eventually it does work and we can see on that right side now we just got a command prompt back from the attacker's perspective. We run whoami and we see that we are running as a user other than ourselves which is not good I think is what it's called in the business. Alright. Uh work with me here. Nope. Yeah it's perfect. Um so I did find out after I tested this again after they made the source code available that they made that race window really short. So I assumed that the way it worked was the victim process ran its command successfully and then went ahead and uh then went ahead and closed the socket after it was done. In fact as soon as you called that elevation request API it locked all future calls of it. Which would explain why I had to run the command over and over again going slowly and sane until it worked. But I don't know not not really security control but good for them for locking out other users and thinking about it. So the second vulnerability I mentioned it's still under embargo unfortunately. Um I reported it 13th of June while the rest of this research was done April May time frame. Um sort of interesting story for you since I can't talk too much about the details of this one. The reason I thought of this was I was on a run trying to avoid working on the slides and I realized that there was some other issues with how they do that client server thing. So I was able to prove it out and get data tampering and information disclosure from again the process or the perspective of a non-privileged user on the privileged user's stuff. So public issues again it is a GitHub repo there are public issues uh that I think are sort of notable to talk about when we talk about Sudo for Windows. The first one um the elevation prompt is confusing. So there's a screenshot of it on the right you saw it earlier in the video. You don't know what hitting yes will do cause it's just telling you it's a Sudo invocation right? It doesn't tell you oh Sudo's actually gonna spawn this other process over here maybe you don't wanna do that it just says hi I'm Microsoft I'm a legitimate binary would you like me to run? Um not great from a security's perspective right? Admittedly again most of us probably just click yes as fast as humanly possible anyway or type in our credentials if we have that version of UAC enabled but um I believe that they committed to trying to figure out how to make it so it was more obvious what the actual command that's gonna run is instead of just saying yeah it's it says it's legitimate I'm just gonna hit yes and hope everything's okay. And the second issue uh which is critical to me because I like my computers to insult me is um Sudo for Linux or Unix if you will has a mode to have it insult you if you don't have legitimate access to run a command. Uh I want that in Windows cause I like my computers to be mean to me when I'm silly. So summary of everything we just talked about. Sudo for Windows is coming soon. Again it is scheduled to be in mainline Windows 11 and I think Windows server don't quote me on that one uh end of September-ish in the in the H2 update. Uh it's open source again we we love to see that it makes it so people like me or people like anyone in this audience can go look at the source code and pull it apart and understand not only how it works but maybe find some more security issues in it so we're not shipping products that are enabling bad things to happen on Windows. Or if you're on the other side of it using it to make bad things happen on Windows I don't know I don't judge. Um it is meant to be a drop in replacement for the way Sudo works on Linux so it lets you actually have standard in, standard out, standard air, piped and redirected and all that stuff within your command prompt or your PowerShell prompt. Um and it does all of that using user account control to elevate processes and inter-process uh communication to actually coordinate that across the not a trust boundary because that's not what Microsoft calls it of UAC. Uh sort of the big take away for me and it's informing some more stuff I'm looking into is you cannot call a Windows native API without going into unsafe rust. If you want to do that or for anything that uses a Windows API that means there is now the potential for you to get memory corruption act vulnerabilities in something written in rust which is counter to what everyone tells you about rust so be on the lookout for that if you're a bug hunter that if you see something calling a Windows native API from a rust program somewhere in that code it is unsafe. Poke at that boundary. And then I'll show some references and if anyone has any questions you can step up to that mic in the middle and I'll be happy to answer them. I don't know how much time I have left but I'll try to answer as many questions as I can. Thank you. Cool. Thanks everyone.