Okay, good afternoon everyone. Welcome to the Packet. Welcome back to the Packet Hacking Village. It is now the top of the hour, 1 p.m., and we have an old friend. He has spoken at Black Hat, Vendepcon, a number of occasions. Our old friend, who we'll be talking about, an old friend who we'll be talking about PowerShell today, Nick Patel. Am I audible? Yes, I am. Thank you everyone for coming to this session. Thank you very much for having me, guys. So, I'm going to talk about PowerShell for penetration testers. First of all, a couple of things about me. You know, even if they have introduced me, I always like... I like to increase my Twitter followers. So, I am a hacker, I'm a trainer, speaker, penetration tester, etc. I maintain two open source toolkits, Cotilia and Nishan. Cotilia is useful for using human interface devices for penetration testing. I spoke about it yesterday. And the second one is Nishan, which we will be using today. It helps you in using PowerShell. So, this is my Twitter ID, please do follow me, and that's my blog. I'm generally interested in new ways of breaking into machines, obviously, when I'm authorized to do so. And I've spoken at a couple of conferences previously. So, if you don't know, then we'll be talking about PowerShell, and why do we as hackers... Why will we use Windows and PowerShell as an attack platform? So, if you don't know, then we'll be talking about PowerShell, and why do we as hackers... Why will we use Windows and PowerShell as an attack platform? Then we'll see how do we quickly not really, it will not leave take a long time for that. We'll see how we can execute codes, commands and PowerShell scripts that is necessary so, that you can follow on the live demonstrations. So, they would be only live demonstrations, even if I fail at them. There are no recorded demonstrations. And then, we'll pick a penetration testing scenario, and pri our work or try to work with them. work out that only using PowerShell. And then a couple of obligatory slides for defenses as well. So, for Microsoft, PowerShell is something which was designed to automate things for system administrators and whatever is useful for a system administrator is always useful for an attacker. So we use PowerShell because it is present on all modern Windows OS, post Windows Vista. It is installed by default on all Windows machines. It is really tightly integrated with .NET. In fact, it is based on the .NET framework. So .NET, WMI, Windows registry, file system, Windows API, certificate stores, and in fact other machines on the network. It is always expandable with the use of the .NET framework. It is easy to learn. Although we are not going to learn it today. It also allows you to have less dependency to some extent on MSF to a really small extent. And also on tools which create your Python or Ruby scripts to executables. No more executables are required on for your penetration test gigs anymore. So PowerShell contains two parts. If you notice, the title of the slides are in a format typical to PowerShell so that you can grab something more out of this. PowerShell.exe is the console part of PowerShell. It is where all the action happens. You run commands, scripts, command lets, everything here. It supports tab completion, context sensitive help, et cetera. And also the last thing, it provides various options and execution parameters which are really helpful to us. Okay. So there are two slides. Sorry. Thank you. Next, one command that's a PowerShell has command list. One command list which I would like to emphasize upon is get help. Consider it like man pages for PowerShell. Everything, every built-in command list, all of the offensive PowerShell tools, defensive PowerShell tools, everyone tries to be compatible with this thing. You just type get help or its alias help and the script name and you'll get a nice list of help topics available. This is the first place to go if you're trying to learn PowerShell. And if you're version 3 or more, you may have to run update help before that. So, for example, let's run help about get help. Is it visible to the back? Is it visible? Yeah. Thank you. So, let's try get help. Get help. That is the list help about the help system. Okay. So this is how you run PowerShell just. Okay. You can just go to start menu and type PowerShell or you can just run it from the command prompt. This is the help about get help. It comes with a couple of users. You just download a useful features or parameters, sorry, like this. If you say hyphen examples, it will list only the examples for that particular topic. Commandlets. Commandlets are heart and core of PowerShell. They are written CMD let but spoken commandlet. They are certainly the best features of PowerShell, task based, ready to use commands, built in. So that's just something which you will always dream of on Windows. They follow a verb noun naming convention. That is a verb dash a noun. For example, get dash help. Get dash process. Invoke dash something. Things like this. And you have aliases for commandlets. For example, have you ever seen LS working on Windows but it works because it is an alias? Yes. For the get child item commandlet. You can define your own aliases as well. Commandlets always return objects so you can do inert objects but if you want to edit them, that's not possible but a lot of things could be done. Another commandlet which could be considered part of the help system and it is really useful to explore PowerShell is the get command commandlet. If you run it, if you have your window machine right now with you connected to your DefCon open network, just type this. So, there are a lot of commandlets on this machine and it depends on the version of PowerShell, I am running PowerShell version 3. So I have 298 built in commandlets. For version 5, it's like I think in thousands. It also depends on the number of Microsoft things you have. But you can take a lot of on your machine. Okay so PowerShell.exe is the shell part and the ISU is the development part of the PowerShell or the scripting part. It is also a built-in thing which looks like this. It comes with the Windows Management Framework so if you have PowerShell you will have this as well. PowerShell scripts are saved within extension of .ps1 like many other development environments. ISE also supports context-sensitive help, type completion, selective running, etc. Execution policies. If you work with Windows machines and you hack things you will often listen to this from our sysadmin friends or even from our hacker friends that if there is an execution policy enabled you may not be able to execute things. For example, so that the default execution policy on machines is either this or all sign. So now if I try to run a command or sorry a script let's say it says that because the execution of scripts has been disabled on this machine you cannot execute this script. But if you run this EP is a shot for the execution policy and now if I run this the script has been loaded in my current PowerShell session and I can run it. So this is not something which I did, it is by design by Microsoft. Because execution policy is not supposed to be a security control. In fact I forgot to add a tweet by Jeffrey Snover who is the designer of PowerShell. They, they add this deliberately to make it obvious that this is not a security control. You can always bypass this. It is meant to be by bypassed. It is just there to users from accidentally executing scripts. Just like this thing. So if you double click a PowerShell script, it opens up in a notepad. Just to stop you from executing it by mistake. In fact, there are so many ways that the nice guys at NetSPI have listed 15 ways of bypassing it. So go for it. The last thing for the theoretical part is basics of modules. So modules are, as in other languages, a collection of functions or things which you can do. And you may like to do in a single file or in a single script. So you can always run these commands. You can run get module to list all the available modules on your machine. The one of integration. The one of interest for us right now is the import module commandlet. We'll use this. We'll see how we can use this to import modules. And after you have inserted a module, you can use this to list the modules imported. The commands exported by a module. So our penetration testing scenario is going to be this. We need to grab domain admin token or credentials or access some of these. So we're going to do that somehow. Note that that domain admin is not a good target. If you do penetration testing professionally, you may like to discuss a goal with your client prior to the engagement. So DA is really not a good goal. But for this demonstration, let's go for it. So back to our scenario. In our scenario, the goal is to get DA. But server-side attacks have been largely unsuccessful, which is generally the case nowadays if you are going from the outside. And metasploit payloads are being detected by antivirus. Let's assume this. I know you guys are smart. There are so many ways of bypassing antivirus. But let's just assume this thing. So this is how we are going to do that. We will run a client-side attack. And one of the ways is to get access to a token with DA privs. So that was the initial thing I planned, that red-dotted arrow. But that's really very simple. So what we'll have from client-side attack, we're going to run a client-side attack. We're going to have access to a SQL server. That SQL server may be running as a local admin or system. Or as a domain admin. That depends on the time we have at hand and how patient you guys are with me. So at the end, we will have shell access to the domain as a domain admin. That's the target. So client-side attacks. PowerShell is the really ideal tool for client-side attacks. It is built-in onto the machines. antivirus and countermeasures generally don't care about it and not not only it could be used to weaponize of a word file or something it could also be used to generate them as we'll see in a second so we'll use client-side experts from my tool and use it for nasty so this is how we'll use them let's try outward which we executed just now so outward as the name suggests it will create a let me go to the classroom first of all let's import nishan to the current session so that we need not run each script independently please ignore this warning this has nothing to do with the maliciousness I just didn't follow the naming convention verb noun naming convention for some of the scripts so let's use let me clear that let's use outward and then it asks for a payload we will provide it a PowerShell payload for now let's just say this thing you won't be able to see any output right now and that is it so now if you see here on the desktop of my machine there is a word file called salaried details talk about you can always use whatever name you want so this is how we do it i'll show you to how to use payloads with this in a couple of minutes So, this is basically how outward works. You just need to provide a payload to it. It will generate an auto, so the basic thing is there is an auto-executable macro inside it. Please note that that macro is not password protected. As far as I know, there is no way of programmatically password protect a macro, at least if you're using com object of what it doesn't expose that API or function at all. That's at least what I know. So, that was the one way. Also, you can provide a payload URL to a PowerShell script. So, the target need to have access to this web server. It will pull the script and execute it in memory. So, we're just going through the different methods or attacks available with client-side attack category in Nishang. So, outward, out-excel. We can also, we can also use out-HTA, which generates a HTA, which is a Microsoft, which gets executed by a Microsoft signed executable MSHTA, and out-CHM, out-shortcut, which creates a compiled HTML file and LNK file, respectively. So, we are going to use out-CHM, because this is going to be our target, and this one. And I have, so there's a small, small, small, small, small, small script running over there, and there is a mail server running over there. So, if a CHM attachment is signed to it, it executes it automatically. So, that is why we are going to use CHM. And note that our target, this machine, it's really restrictive. It is a Windows 8. It has incoming traffic only on port 25, outgoing only on 80, 443, and a couple of them. Only on port 53 for UDP, and I think ICMPV4. That's it. So, it's quite a restrictive machine. Let's generate, okay, prior to that, we need a reverse shell access. So, let's go through the shell slides, as well. Okay, we assume that we can run successful client-side attacks on our target. So, now let's use a couple of shells, again, from Nishang, to be able to get an interactive shell access on the target machine. So, we'll use this TCP reverse shell from Nishang. So, it needs to have only these options, that is, the IP address to which it will connect, and on the port on which our listener would be listening. So, what we are going to do is, one thing which I forgot to show you is this, PowerShell supports encoded command parameters, that is, hyphen e. So, you can run scripts, like, PowerShell.exe hyphen e, and the base64 encoded command. So, that thing is useful, because, first of all, if your script contains so many double quotes and single quotes, that problem is resolved. Also, it gets executed in memory, as far as I know. So, that thing never touches disk. So, okay. So, let's get started. I'm going to use this shell from Nishang. This is invoke PowerShell TCP one line. Let me open it in the ISE. So, this is just a one line shell. So, the second one is even smaller, but that doesn't show the output back. So, I'm going to encode this and use this. In fact, I think I've already prepared one here. So, okay, we're going to use this one, which is the same thing. So, let's just quickly run invoke encode. And the TCP, we're going to use the reverse shell. And we want it to be executable with the hyphen E parameter of PowerShell. So, it is returned to this file. So, this is our base 64 encoded shell. Let me start a listener, but not using Linux, because we are going to use only PowerShell. Let's use PowerCut, which is an awesome port, another single port in the network. And we think we could go on and we really need a lot of So, this is the output. This is the output. And it's very easy to add here, We are going to run the Bird Map. We will do this, So, right now, we're going to see. We're going to test our�� napht. our reverse shell if it works or not let's do it on local machine so this is how we use it so this is what we are going to run on our target and yes because it works let's use it on the target so here we're going to use okay we are going to use out CHM because MS office is not there on that machine you now to use this one you must have html help workshop installed on on your machine that is the attackers machine so so we have all compiled html help file on at this directory now now let's send an email to to a target if you see if this is proper no it's not i forgot to start a listener i think oh it's there so let's see okay so now we have a reverse shell from the target machine which is 112 of windows 8 machine and we have a reverse shell from the target machine which is 112 of windows 8 machine and we are running as a domain user which may or may not be a domain admin so this mimics obviously it is not really this easy in an external gig but if you do for like 100 users you'll get at least 30 or 40 connectbacks that's at least my my dear clients give give to me so we can assume that now we have a photo machine in fact we have there's no need to assume it so this starts our post exploitation phase obviously i skipped a couple of starting phases so now again powershell is one of the best tools for post exploitation for the same reasons which we discussed it's built in it's powerful and microsoft is going really big on powershell so this is going to be one of the best things you will ever learn not this session powershell so now we have an interactive partial session on one of the machines we can use various powershell tools for it so let's use power view these are from the guys from veal awesome tool so it has a lot of commandlets or functions or scripts whatever you want to call them to enumerate a domain but instead of going through everything we'll just use this one so invoke user hunter could be used to enumerate a domain to find all the machines where a domain admin is logged in and if you use the parameter check access with it it will check that if you have a local admin access to that machine as well which is really sweet so to now there are two ways either i can use this reverse shell or okay i'll use this one as well never mind okay what we are going to do now is we will pull power view or we load power view into the memory of of this powershell session using this one liner so as you can see right now on theQueue Right now we are using .NET to extend the capabilities of PowerShell. So this, those of you who work with .NET or C Sharp, nat.webclient is a .NET class. And this is invoke expression, we are just going to, I mean it is up to you, if you want you can just upload this file there and work that way or we can use this in memory. So right now this thing will not touch disk at all, it would be in the memory. As you can see it has been downloaded, so now we must, we should have invoke user hunted there. Let's see if. This is get help compatible, oh yes it is. So every PowerShell tool you are going to use is the one named there, the one which are really popular or at least are out there of beta things, they'll support this thing. So let's just copy this so that I do not have to type this out. I think I should run this with verbose. Because we will not see anything, okay. Let me see if there is any verbose option with this. No effect of verbosity. Okay so it, you have to believe this, that it looked for, for machines where a domain admin is logged in and it has local admin but it was unable to find that thing. Because there is, there is no such machine, there are only these three machines running on my lab right now. So I demonstrated it just because. Because I have been able to get into, to get the domain admin access by using just this thing. So let's move on with this thing. So after having to fail the, having to get a domain admin with single command, let's try a couple of more things. So this is from the nice guys from NetSPI, this script, we will check if the current user, that is the lab user. Okay. With whose privileges we have the access to that machine, we'll see that if it has access to a database server. So again, we will do the same thing. We'll pull our script in the memory. Of course. Let's run this PowerShell session. And let's run this. Reset. So what's the name? Okay. Let's see. Let's check access, so. So as conspired by me, we got access to a couple of SQL servers. Okay. No access to only one server on this machine. Which is, which appears to be a file server. And there is, okay, there is an SQL server running on the local machine as well. But we are not interested in that. And in fact, we have no access to that as well. So we got access to a database. You can obviously use this script to execute SQL queries and a lot of things there. But to execute commands or PowerShell scripts, I'm going to use this thing, which is from my tool, because I need to promote my tool as well. I don't want you guys to use others' tools always. So I'm going to use this thing. Note this parameter, Windows authentication. That means we will use the trust relationship we had with the database server, which we enumerated. Just now. So as a lab user, as this, this domain user, which is called lab user, we have access to the database. And we have sysadmin, which may or may not be the case. I mean, it depends on the, on the role of that user in that organization. So now I will load execute command mssql into sysadmin. So here is a SQL release. So now I've loaded the code. I'm going to see the status. I'm going to say css. I'm going to have this. I'm going to have this. Our tab completion enabled. I mean, you need not type everything. But since we are in a remote shell, this doesn't support tab completion. So I need to type out everything. So the server name was pfptlab file. And Windows authentication. I don't think we need to provide anything else. And payload. Because we won't be able to... So generally when you execute it, it asks you if you want a PowerShell session or a plain old command prompt or an SQL session. But that won't be visible here because we are already on a reverse shell. So let's just try this first. Okay. So as you can see that, we have access as a service user. And this is on the remote machine. If you don't believe me... Sorry. Let me just IP config. I think that dollar... Okay. We are on this machine, 230.200. So right now, if you... If you see this thing which we discussed earlier, we are here. We had access to SQL server or we are here. We can say that we are here. So in fact, the truth is that we are already... We already have domain admin. Because I was... I don't know about the time. If you want, I can just restart that as a local system and do all that thing. So if you notice, we were running as Grave service, which is actually a domain admin in my lab. To check that, I'll just try... Okay. Let's try to run the command on the domain controller. So invoke command is a built-in PowerShell command. So invoke command is a built-in PowerShell command. This is the domain controller. Let's run IP config there and see if it works. Okay. Let's run IP config there. We are able to execute commands on the domain controller. So that was one of the simpler cases. But this is from one of my actual penetration tests. I used it. I did that exactly the same way. Created client side attacks, sent it to the clients. Of course, that involved a lot of enumeration where I need to figure out the email addresses and the potential targets for phishing and their roles in the organization of my client. So that was one of the cases. Now, I don't know if I'm going to try this because this is something really scary and takes a long time. But I don't do video demonstrations, so I don't have a video as well. If we did not have access as a domain controller or our SQL server was not running as a domain controller or as a system or a local service, or not as a local service, if it was running at least as a system, we can use this script from PowerSploit, another really, really awesome collection of scripts for PowerShell. In fact, the guy who wrote this just gave a talk in the lab this year, over WMI. So you can use invoked token manipulation. Okay, invoked token manipulation is written by Joe, who is here as well. So you can use it to manipulate or reuse or replay tokens of different users on a machine. So imagine this. Because we have system access on that machine, we would be able to use it to manipulate or reuse or replay tokens on that machine. And on that database server, there is a service running as the domain admin. So if you want, I can give it a try. But it takes time. I can show you the process. So there's something which is there to show at least. So it takes actually this long. I plan to do it. But when I tested it in my lab, it took really long, like half an hour to do that. There were so many fields. So do you want me to try that? Hands up. Oh, you guys are in minority, so no. I'll definitely show you. So if you're really interested, I'll definitely show you maybe after this. Or I don't want to ruin everyone else's mood that, okay, this was a Windows attack platform and didn't work. So let's just move on. But I do have time. Let me try this. So let me try this. Okay. So please be aware that there could be fails. There would be things which I can figure out. So what I'm going to do, I'm going to restart the SQL Server service which is currently running as a domain admin to a local system. I'm going to show you one of the things that can be done to this at the end. Okay. So I think I shall just close this thing. Now if I run Swatch Closely. If this works, because I don't know whether this will work or not, but I'm just going to run it in a Windows authentication may work or may not work because of that change. If it works, okay, it says the target principal name is incorrect and cannot generate SSPI context. That's bad. Okay, but this is the first time I'm having a look at this thing, actually. So we have to make a couple of assumptions. I will log into that Windows 8 machine and assume that I have RDP access to that. There's no other way around. Is that acceptable? Thanks. Okay, this is how our CHM after execution looks like. So this is something which is not really good. It shows the CHM file. But if you use outward or out-excel, they're really clear, really clean. User doesn't see anything. So okay, it has been executed for so many times. I think I'll just clean this. So the script on the target, it just saves the attachments to this directory and it gets executed, just to mimic a normal user. They always open every attachment. So let me just close this. And I think I do have everything here on this target. I don't know, I think I should not have this error. Wow, wow, it's not there. So it took some time. Wow, nice, yes. So as you can see that, we are system now. Now demo gods are happy with me. So we are now running a system. Now we'll go through a painful process of trying to get domain admin access on that machine using this thing. So we must run PowerCat on attacker's machine, okay? On port 443. So here it goes. Okay, it's already there. So it's the complete thing. Yeah, we did code exec using client-side attacks. Now here, we have the execute command MSSQL running as well. Now, if you see this command, what we are trying to do here is, we will pull PowerCat in our existing reverse shell, add a relay, because we do not have direct connection, to the PFPT lab file. So we are sitting here. I do not have a network diagram. So if this is our attacker's machine, and this is the client-side, so the third portal is PFPT lab. So I do not have direct access. I'll go there. And from this machine, I have to go there. So for that, we need relays. PowerCat, because it's awesome, it has the capability of adding temporary relays as well. So let's use that. And this relay is, it, as you can see, it forwards port 8000 of our Windows 8 machine to port 80 of our attacker's machine. Let's just quickly run this. I'm running it as a PowerShell job. That's what the start job command let do. Okay, looks good. Let's try. Okay, so the job has been started. That means it may be working. Now we need to start another listener. Okay. I need to run it on this machine. This port, and try it again. Okay. Okay. And we get the Atlas. okay so the idea is to pull power cut here on the windows 8 machine that's our target and add a relay okay but that's not working right now so so let me check the check the power rule see if I have the portal out or not it should not be sorry no no no windows is too long because we have these four ports allowed on on the outside so they should be absolutely fine let me just try running this once more I have to move move from this thing I'm sorry so maybe catch me after this I mean this works trust me but sorry no video demonstrations still so so the basic thing is this if you have even if you can use tokens you can use tokens you can use tokens you can use tokens you can use tokens you can do it remotely now I can show it really ugly way if you want really ugly I mean really really ugly do not try this at home it's that ugly I can just turn off the firewall on this machine so that I have a direct access oh it's off Wow because of some testing well that's that's great so now we'll have a direct shell access to that machine assuming that thing so this is our let's quickly run a listener on some oh okay it's already in use so I'll use this thing so I have a listener running here so I'll just change the network so that we can quickly have a look at that thing I always wish I can do this in real penetration tests so now we have access to that machine and now if we just put into with inside the traffic control on that at the time at the logic side we want to work with this so basically we want this to run along the rural network so let's try a little bit quit the linear here I want to realize that it's running like this backwards so to start with right so right we're going to log into orbits system so first we're going to have to activate that one right where it can it can run it can just and we go back to the project manager so let's add in there we need to do PTX input on that behavioral meaning user cmd operation which involved two different options one is empty key which is greater than one I should say I would have to I might have to use double just now so just easier except for the contract Okay, and now if we run it quickly, wait, okay, let me try it again. Let me check if it is there now. Okay, it's there, so let's run it. Let me just copy this. Okay, let me try the lucky port. Okay. Okay, let me try the lucky port. Okay. Okay. Okay. Okay. Okay. Okay. So this is my last attempt. Okay. Okay. Let me just move this thing. Okay. So in any case, you can use this to enumerate them, enumerate the tokens, and then reuse them. So the basic idea was that we'll create a reverse shell with the token of the domain admin, and then we'll have domain admin access. But never mind, we had the access already in some other scenario. So how do we do it? Admit to use it? Let's eaten it and expect tourists to experience the true nature of anھیb 1940 s. Here is some training. Here's the basic idea we did to explicar Havingehienna is wild. Acitz? And this is a place where you don't come straight up to look. I'm going to use it right now . Okay. We have done it in one of the experiments. useful part. Also, make your users aware. Work on user awareness that if someone is sending you, if you get an unsolicited email, do not open attachments. I mean, that fails a lot, but keep trying. Keep trying to make them aware. And this is because in my talk, system admins generally feel like that I'm targeting them. No. If a determined hacker or an attacker can get inside your machine, a determined system admin would be able to catch them. It's your environment. It's your backyard. You will be able to catch him. And shouts to these guys. They're all PowerShell hackers. Many of them are here. So ask them on Twitter. Meet them. If you're really into PowerShell hacking, you must meet these guys. And closing remarks. Bad guys are already dead. Using it, you will see so many malwares or bad guys using PowerShell. Use this. And of course, this is my original code. PowerShell is not the feature of Windows penetration testing. It is the present. And at last, some shameless self-promotion. I do PowerShell training. So just check out the schedule. If you like it, attend it. And all the information you may like. Just catch me for some stickers if you want. Off to the top. So that's it.