Welcome to track one, two o'clock talk. Hope you learned something new about SMB. This is William Martin. Hey, guys. So, as introduced, my name is William Martin. For anyone who saw me yesterday, it's going to be familiar. I'm an OSAP. I'm a pen tester based out of Charlotte, North Carolina. I'm a supervisor at RSM US LLP and this is my second time presenting at DEF CON. First time was yesterday. So, lessons learned from yesterday is that if you've got any questions about this talk, want me to clarify or expand or get in contact with me, take a note of the Twitter handle right now. That's the best way you're going to be able to reach me. So, it's quick reach. Anyways. So, before we get started, I want to make sure all the right people are in the room. We're going to be talking about SMB and SMB based man in the middle attacks. And for red teamers, this is useful to add a new kind of attack to your tool set, add a new technique to your arsenal. So, let's get started. So, let's get started. So, for blue teamers, you want to know how to stop the red teamers from using what they learned today. And for like the three guys curious about SMB, it's also for you. That guy right there. Thank you. So, first things first, I want to clarify some terminology. When I mention SMB server, do not think of like a server 2012 or 2008. It is any Windows PC typically connected to a domain. Every Windows PC runs an SMB server by default. But when you're on a domain, typically it's a server that anyone can talk to it. So, when I say SMB server on client, it can be Windows 7 to Windows 7, 2012 to 7, 2012 to 2012. There is no connotation tied to the actual operating system. So, what is SMB? In a nutshell, it's a way to, you know, access files remotely. It's Microsoft solutions for network based file sharing and file management. You can read and write files to it. You can authenticate to it. You can map network drives. And you can actually access some, you know, RPC pipes over it as, you know, hackers love, like, you know, through PS exec and whatnot. What I want to clarify is that through this talk, I'm going to be talking about SMB 1 and SMB versions 2 and versions 3. Now, back when Vista was released, Microsoft pretty much did an entire rework of the SMB protocol. Originally, it was based on an IBM system. And that's what we call like NTLM. And Microsoft uses NTLM everywhere. So, the names are SMB version 1 is what we're typically familiar with for like, you know, MS-17010 and things like that. SMB version 2 and 3 is an entire rework of the SMB protocol. And within that, there are different versions. They're called dialects. So, 2.0.2 is the first version of SMB 2.3. And then you got 2.1. Then you got 3.0. And then 3.0.2 and et cetera. So, a typical exchange in SMB looks like this. Someone says, hey, what's in this folder? And SMB will reply, documents, desktop, and, you know, some not password file. So, we love it for hash. We love it for system enumeration. And we love it for spidering shares and hunting for sensitive data. So, it's like, you know, the C password vulnerability in SysVault. And, obviously, for MS-17010. Now, the premise of this talk and the origin of this talk is SMB signing. In particular, how it defeats my absolute favorite attack. My favorite attack is the NTLM relay attack, especially against SMB servers. If anyone has used impact, it's an NTLM relay stuff PY tool. You know, that is it. In a nutshell, the attack works like this. A victim says, hey, where is my network share? An attacker says, hey, I'm totally that network share. Victim says, cool, let me log in. We then relay that to some random server we pick and say, hey, let me log in as this dude. Server says, yeah, that's cool. Just hash your password with this challenge. We send that back to the victim. They comply. We forward that on to the server. Server lets us log in. And then we kick the victim. We ghost them. And once we have that connection, we can enumerate the system. We can execute commands. We can dump it. That's now our box if the person is a local admin. Now, the thing that sucks about this attack is if anyone has run this, you might have seen this. Signature is required. Now, I have a lot of confusion. I just manipulated the entire authentication process for this victim. Yet they have somehow established a secret or trust between each other unbeknownst to me. So I didn't know how could they have done that. Was there some kind of like public key, you know, certificate kind of thing going on? I had no idea. So when I looked up what is SMB signing, I got vague descriptions. And if you've ever run a Nessus scan, I guarantee you've seen this. So naturally, like most of you, I Googled it. And I got answers like it stops man-in-the-middle attacks. Stops man-in-the-middle attacks. Trust us, it stops man-in-the-middle attacks. It's Microsoft solutions to stop man-in-the-middle attacks. That's about it. That's all the information I could find. So I knew that it protects the integrity of SMB messages because we edit it after signing is enabled. We get screwed. It's required by default on DCs. So we have to do this. It occurs only after the authentication process. And it stops my favorite attack, which is kind of like a dick move. So I dove into the MSN dedox. And this was supposed to be a GIF, but, you know, whatever. And I saw things like this. And I saw things like that for a while. Quite a while. But eventually I found the answer to what SMB signing actually is. And we're going to dive into that. At the end of the authentication phase, both the client and the server will share the same session-based key. It's just a 16-byte value. They then use that value to generate other keys to then sign the messages. Now, the goal of what we're about to cover is where does that key come from and how can we compromise it? By the way, that's what a signature looks like for anyone who hasn't used Wireshark on a DC. So I'm going to go ahead and show you what it looks like. So the session-based key actually depends on what authentication mechanism is used. So let's talk through NTLMv2, also my favorite type of authentication. There are three messages in NTLMv2. There's a negotiate, a challenge, and an authentication. So negotiate just says, hey, I want to log in. And they set some empty parameters. Like you can see in this capture, there is no user name. There's no workstation. There's no user name. There's no user name. There's no user name. There's no domain name. It just says I want to initiate this conversation. One flag that can be in this message is called the negotiate key exchange flag. And we're going to get into that later. But in a nutshell, what that means is that once both the client and the server establish that shared session-based key, they're going to change it again. But that's what that flag means. We'll get to that. The challenge says, cool, all right, hash your password with this data and then send it back to us. And this contains the server challenge information, server information like the host name, domain name, DNS name, stuff like that. The authentication message contains a lot of stuff. The user name, the domain, the client challenge, the NT proof string, which in a nutshell is the hash generated by the client. A bunch of response server versions. Things that are in plain text that we don't particularly care about. And an encrypted new session-based key. If you look in this message, you'll see there's a session key value, like B485 to whatever. And then that only exists there if the negotiate key exchange flag was set earlier. That is the encrypted version of the new session-based key. But if they didn't negotiate that, that value is going to be empty. And they're just going to proceed with the keys they generated on their own. So, that was very technical. But let's get down to how the keys are actually generated. And we need to know what HMACs and CMACs are before we can get into that. In a nutshell, they are message authenticators. They're the ones that generate the key. They're the ones that generate the keys. You can think of them, if you're familiar with password salting, you can think of them like a salt. Where you have a message and you're going to hash that message. But while you're hashing it, you're going to include a secret value. That only those, that secret value, can throw in that hashing algorithm to create the same hash. So, in HMAC or CMAC, the underlying protocol might vary. Some use AES and some use SHA-256. But in a nutshell, their goal is to hash the message in addition to some secret information. So, let's actually walk through generating those session-based keys. So, step one is we need the NT response. That is the user's NT hash. So, it goes from plain text, you know, NT hash. Step two, we take all of the information from the server challenge and we put that together and we use the HMAC algorithm with the password, with that hash password, the NT hash. And then we have our session-based key, which is the NT response, your password hashed again in addition to all of this server information that you already hashed. All together, it looks like that. The key takeaway, though, is the highlighted information is the only information we don't have in this exchange. To generate the keys that protect all S&B sessions, we just need the user's password. We don't need the password. We just need the user's password. We just need the user's password. Which is great, because they're always secure. We can never correct them. That was my response when I found out that the key concept protecting all these connections was just their password. I mean, think about if TLS was protected not by some random long string generated through the connection established phase, but instead, like, the dude's password on the website he's connected to. So, the key logic works like this. You get the password, it gets converted to the NT hash, it gets combined with all that authentication data, and then that generates the session base key. Once you get that session base key, you can use that to then sign packets. Or in other versions of S&B, you can use that to generate the keys to sign the packets. Long story short, this is the value we care about. So, as I mentioned earlier, if that flag is set and you generate that key, you're just going to use it to decrypt this one, and that becomes the new one. So, with that knowledge now of how those session base keys are generated, I want to go back to my favorite attack, the NTLM V2 relay attack, and show where that trust is established. And I know there are some Microsoft guys or AD admins in here who are going to laugh at this for leaving this step out. So, here's what it looked like. We just sent the challenge response. We just sent the authentication message to the server. The victim at this point, before the server replies, has already gathered all the information that we needed to get to the server. So, here's what it looks like. We got all the information required to generate that session base key. So, he's cool. The server doesn't have the user's password unless, you know, you're using a you're connecting to a DC itself. So, the server actually sends all of that data off to the DC through a mandatory encrypted channel. Please trust me on that one. And the DC will reply if it's a valid login. And if it is, the DC will generate the same key in the same way that the victim did and provide that to the server. That's the trusted secret that we don't get. So, I know you're all thinking, what about Kerberos? So, Kerberos is a bit more difficult to get into. Starting from step zero, when the user logs into their workstation, they log in and they send a request to the Kerberos server, typically the DC. Kerberos will reply saying, hey, encrypt this timestamp with your password so I know it's legitimate. We send the encrypted timestamp, and the Kerberos server replies with our TGT, which is our session key. We're going to use that for most everything on the domain. We're going to use that for future communication with the DC. We're going to use that to request tickets to other resources on the network, such as when we want to connect to something called a file server 1, just a random file server, and the DC will reply with an encrypted service session key in that packet. We'll send the ticket we just received off to the file server. File server, assuming it's valid, will reply and say, hey, cool, you're good. And if there is extended security, it will reply with another encrypted session key, which altogether looks like this. Password turns into the Kerberos session key we got when we first logged into Kerberos. That we use to decrypt the service session key we get when we ask for a ticket to a file server. And if they're using that extended logic, we use that last key to decrypt the now new key provided from the file server. These keys are then used to sign the SMB messages. These are the SMB session keys. And the algorithm used to generate these keys and to generate the signatures vary based on the version of SMB. So there's version 1, which uses MD5. Versions 2 and 2.1 kind of beefed it up a little bit with SHA-256. And versions 3 and 3.1.1 really beefed it up with AES-128. So now that we know the only thing protecting these sessions is the integrity and the complexity of the user's password, let's go after it. So first things first, we just talked about how to kind of break SMB signing keys and how to break signed SMB connections. But when SMB signing isn't used or we have that, it's essentially HTTP without HTTPS. If encryption's not used, we can steal copies of files passed over the wire, with or without the keys. And encryption's not used by default. If signing is not used, it's fair game. You can replace every file with an identical link that executes our code. You can swap out the contents of legitimate files passed over the network. You can inject fake files. You can inject fake files into directories. And that helps with social engineering. And if signing is used and we know the password, then we can do all of the above. Plus, we can also backdoor some stuff pulled from the DC by the user. So in SMB version 1, they don't really use signing by default. And in SMB version 2.3, the case is pretty much the same. Unless somebody requires signing on one of the sides, no one uses signing. So we just talked about how to break signing. Most of the time, it's not even in place, per the Nessus reference. Also, SMB version 1, there is no encryption. So for those plain text files passed over the network, we can just grab them. SMB versions 2 and 3, it supports encryption version 3, but it's not enabled by default. And enabling it is a bit of a pain. So chances are, you can also, regardless, steal files passed over the network. Broken down, there are various versions of SMB, and they are supported on various types of operating systems. Like Vista came out with SMB version 2, and that was the first one to come out with it, with 2.0.2. Then you've got the latest and greatest Server 2016 using 3.1.1, and Windows 10 using 3.1.1. Everyone else uses something earlier. Or rather not earlier, in between those. Notable exceptions in all of this, is that DCs require SMB signing by default. Windows 10 and 2016 have protected paths, that always require signing, if they're connected to a path that matches these paths. So anything that's like something sysvol, or something netlogon, the client's going to require signing, regardless of what the server says. And that's through a process called UNT hardening. One other notable exception, is that if a client supports SMB version 3, and that's 3.0.2 or 3.11, then regardless of if signing is used or not, at the end of the authentication phase, they're going to send a message to the server saying, hey, just to make sure we're all on the same page, here's the information you gave me during the negotiation phase. Is this still cool? And that's a signed message. So the server now has a way to kind of verify that no one messed with it in between. Even if they're not using signing for any of the subsequent messages. Only catch here is that in that message, the one that verifies we're all on the same page, it covers everything except for the authentication mechanisms used. So even with that, you can still downgrade them to NTLMB2 and get away with it. So, on the other end of the spectrum, SMB 3.1.1 is a beast. And I'll give kudos to Ned Pyle on that one. The SMB keys, rather than just being based on the authentication mechanism used, it's also based on the hash of every packet passed back and forth during the negotiation and authentication. So if you edit anything, then the server and the client are not going to have the same keys and the connection's going to drop. So, not bad on that one. So to recap, it's used on DCs and on 10 and 2016 on those paths. Encryption's only on SMB 3, but it's a manual process. Every dialect up except for 3.1.1 can be downgraded on NTLMB2. And signing and encryption keys are at the root based on the user's password. So, let's attack that. I built SMB Tray, and the goal is just to build a tool to really take advantage of these man-in-the-middle vulnerabilities. The attacks we've been running before are like the relay attack I described, where, you know, we're enumerating the system if we're putting ourselves in between a victim and a server. Or we're trying to pop it if they have LA. But if they don't have either of those, that's pretty much where the road ends for us on SMB attacks. Except for, you know, MS17 or patch-related vulnerabilities. So, I wanted to take the focus, instead of, you know, relaying or putting ourselves to the focus on the server, I wanted to go back on the client. I wanted to go back on the HTTP mindset of attacking the client. So, the biggest obstacle in all of that was putting ourselves in the position to attack the client. So, we wanted to be right there. We want to be between my buddy Damien's in the crowd. We want to be between Damien and his DC, his file share, and his other file share, because many users have multiple. We didn't want to just pick between, you know, being in between Damien and his DC, and that's it. So, we're missing a lot of data there. So, when I was trying to build this tool, and I was trying to put it on top of a man-in-the-middle framework, I kind of had two options. The first one was through the use of an RPG, an arbitrary upstream server. A lot of these man-in-the-middle tools will use an arbitrary upstream server as a way of stability. Long story short, you receive a connection during, like, an ARP cache poisoning attack. IP tables will redirect that connection right back to you. And then, in the socket layer, you will then forward it on to wherever you want. The catch is, when you're at that layer, you don't have the original destination of the connection you just hijacked. So, if Damien was trying to connect to his DC, and I intercepted that connection, I might be redirecting him to some random file share I picked, and on his end, that's not going to be what he wants, so he's going to drop the connection, and it might not give me all the things I want. I want him using that connection so I can take advantage of every opportunity. I want him downloading files so I can steal them over the wire. The other opportunity for a man-in-the-middle tool to kind of, you know, take advantage of that was through the use of NetFilterQueue. NetFilterQueue really gets down to the kernel level and allows you to edit those packets and intercept those packets live time, so you don't have to worry about losing the destination information as you're performing this intercept. So you can put yourself in between all of those servers. Only catch is that when you're putting yourself in between all of those servers, Python is what I was using, and Python would just snowball, it would drag. What would seem very quick to us was Eons for TCP. So this only solution that I found to do a full transparent man-in-the-middle attack was killing me because it wasn't fast enough. It wasn't parsing the data fast enough to pass the information back on. So I tried to combine them to get the solution that I wanted. I created a little library called EBClib, which was kind of play on. There was EtoCap and then there was BetterCap. So, you know, cheekishly I put EBC for even better cap as a man-in-the-middle TCP kind of framework. So it gives you all of the freedom of a transparent proxy built on NFQ where you can just say, hey, put myself in between Damien and whatever he's talking to without editing it or edit if I choose to with the connection stability of a BetterCap with an upstream proxy. And that's the technical of how it works. Long story short, you get the packet. NFQ, rather than trying to edit the packet the entire time and do whatever attack you intended, instead just performs one task, which it does pretty quickly, and that's why the connection doesn't drop. NFQ will get the packet. It will just take the source and the destination information and then store it in a shared piece of memory and then pass it on. That gets redirected, like the first type of man-in-the-middle attack, to a TCP server. And with there, you have the stability of TCP. That TCP server will reach out to that shared memory, find out where this dude was actually trying to go to, and then builds that connection dynamically out there. So, once you receive a connection that exists, it just passes through to the TCP server like business as usual. So, now that we compare ourselves in between the server that we want to hit and the user, let's go ahead and let's start attacking them. Let's downgrade the dialect and the authentication mechanisms used. Let's steal the passive files that I've been harping on. Let's inject some files. Let's swap out some content. And let's replace files with an identical link so that when they click it, it launches that thing. And, of course, if we know the password for the user, let's leverage that information to break those session keys. So, the way SMBetray is broken down kind of looks like this. We put ourselves in between the victim and whatever server they want, even multiple servers at a time. The victim will say, hey, what's in this folder? And that request occurs typically when you open up a folder in a share and it starts listing out all the files and folders in there. We pass that on to the receiving server. Receiving server will reply saying here, you know, file one, file two. We'll get that and if we know the keys or it's an insecure session, we'll throw in our own file in there or we'll modify the details of the files that were just listed, like their file size. The user might request one of the files we just lied about and said it was in that folder. Now, instead of passing that on to the server because the server would reply with an error, saying that file doesn't exist, we send them a convenient function in SMB called an echo. SMB loves to keep its messages in sync. Each request and response has a numeric ID. So if I say, I'm going to pick on Damien again, if I say, hi Damien, that has a message of one. When he replies, that's a message ID of one. That conversation is concluded. If I say another thing to him, message ID two, he replies message ID two. That helps verify that we haven't missed anything in this conversation. But if the user just requested a file that does not exist, and we're not going to pass it on to the server, there will now be a discrepancy in how they talk to each other. Their IDs are going to be misrepresented and they're going to be off. So to keep them in sync, for every request that is completely forged or malicious by the victim that we're handling, we're going to send an echo request to the destined server. When the server replies to that echo, we've now incremented the messages again, and now we're going to reply to the victim with whatever we wanted. And that way, when they continue their conversation with each other, no one will identify that they've been swindled. Old word. So now let's show a brief demo of what this is going to look like. So assume we've done an ARP cache poisoning attack on this victim, and we're between them and a switch. On the other end of the switch, it's going to be like a DC, and there's going to be just some file share. So first things first, we're just logging into a user's workstation to show you what a non-attack scenario looks like, what his average PC sees. He's got a file share, a map drive called some network share, and there are some files in there. Keep a mental note of the files that you see, because we're going to be injecting some and removing some. Cool. So now we're going to launch the ARP cache poisoning attack to really put ourselves in between him and whatever he wants to talk to. And also note, the server that he just connected to, the one with the files and his map network drive, requires SMB signing. We've got a file containing a lot of POP credentials, so if you've been running like an LLMNR or an NTN net BIOS poisoning attack on your LAN, you might have these credentials. And we're going to load these credentials, into the tool itself. It's just going to use them, and when it sees a connection coming from that user, it's going to try to leverage those credentials to pop the SMB signing keys. The attack we're running is going to be a link swap all attack, as well as an inject files. Inject files intuitively just injects files. It makes files appear in the directory that don't actually exist in the directory. And the link swap all is any file at all, we're going to replace it with instead a link that will run whatever we want. In this case, we're just running calc. And we're running calc because when I was writing up this demo, I didn't have time to fight with Windows Defender because they've been kicking ass lately. So calc was easy enough. So now Paul Davis is going to log back into his PC. You can see we captured his hash. And in the background, you can see that we, it's reporting that we broke his session base key through both NTLMV2 and it's cut off a little bit, but it says Kerberos. And now it shows that we just injected some files. So click me, inject me, seems legit, all those. The text file that we opened up earlier is now a link. And when opened, we'll run whatever we want. In this case, it was calc. Inject me, a totally fictitious file that doesn't exist on the server he thinks he's talking to is now there. And any file... Thank you. Any file that this user is now downloading or opening either directly or indirectly, you know, through backend processes in Windows, we will be passively stealing a copy of. And to show the impact of that, I'm going to run the attack again. But I'm going to run the attack again with no credentials and the only thing I'm going to do is I'm going to passively steal stuff. So this is useful if you are on a LAN, they're pretty hardened, you can't get LA anyway, you can't crack their creds, but you need to still, you know, make some progress on hacking them. So... We're removing the credentials and we're just going to listen passively. As it turns out, when Windows starts up, it downloads a lot of useful information from the DC. And if you are lucky enough to be running an attack like this, or if you DOSed your client or you DOSed your victim to convince them to reboot their PC, you might just be able to capture all of these valuable pieces of information from the DC. I recorded this demo on a PC from 2012, so it's not going to be the quickest thing. PC just started up, and we've already intercepted some connections. Paul Davis is logging in. And we're now stealing some data from the group policy... including the registry.pull file, which contains a lot of valuable information in terms of registry settings configured for the users on the domain. ... ... ... ... ... So, that was the demo. So, if you want to take a photo, it will be posted this evening. Cool. So, now that we've just kind of demonstrated that SMB keys are based on passwords and that even without the password, we can still intercept and modify a lot of connections and we can especially download some useful information from the group policy, how do we stop this from happening? Netpile is the guy who I would argue is in charge of SMB globally. Disable SMB1. Second thing, even though we're just demonstrating that you can defeat SMB signing, that's by no means an excuse not to require it. There is an option to support SMB signing, and there is one to require it always. I highly recommend you require it always. ways. Unless you're running XP systems, you're going to be able to support that. So S&P version 3 introduced encryption. And I would love to see organizations really start pushing this thing out. Any S&P 3 client supports it. So if you're running anything later than Windows 7 in your org, you can put encryption pretty much everywhere you want. And it is actually more efficient than signing the messages. So if you ‑‑ you can support encryption or you can require it. So if you support it, then S&P 3 negotiations are protected. If they negotiate that you can't support encryption, they're going to encrypt the session. So UNC hardening is one of the features used by default by Windows 10 in 2016 to protect the sysvol and the net logger. So you don't have to go and log on shares for any connection going out to something that looks like that. But you can manually require that on other shares or any other kind of path. So if you know in your organization that there's a very specific file share that your user is connecting to that you want to make sure is pretty locked down, you can lock down from the server side by requiring signatures, requiring encryption. But you can also lock it down on the client side, prevent them from connecting to rogue servers by throwing in UNC hardening saying, hey, if you're connecting to this server or a server that looks like this, do X, Y, Z. Man, I hate to say this because as a pen tester, I love NTLM. But NTLM really is outdated. It was made near the 90s. I was made near the 90s. So if you can, run through audits in your organization. See where NTLM is used. And if you can't disable it, just carte blanche. I don't know if that's the proper use of carte blanche. If you can't disable it across the org, then find out where it's used and then restrict it just to those systems. For me, that screws me over. But I guess we're all here for security. Now, Kerberos fast armoring is an interesting thing. We showed that if we know the user's password, we can intercept their Kerberos authentication phase and steal their Kerberos session key from the server. And that's what we're going to do. We're going to do a lot of initial log on and then use that to steal their Kerberos ticket session keys from every subsequent request. That can be protected, but it requires Kerberos fast armoring. In a nutshell, any request going to the Kerberos server is first locked up with the, man, I'm hoping my details right, with the machine account Kerberos tickets. Which means unless you can pop the machine account password with that obscenely large password, you're not going to be able to access the Kerberos password. You're not going to be able to modify or intercept that Kerberos session key even if you know the password. So, huge precautions when enabling fast. It requires Windows 8 and 2012 relayer throughout the environment. Just like SMB signing, it can be supported or it can be required. If you just support it, we're going to take advantage of the new version of the, you know, of the benevolence of the server and just downgrade clients. But if you require it, then you better make sure that all your clients can connect to it, otherwise you're going to brick them. They're not going to be able to talk to the DC. So Microsoft has great documentation on how to push out fast armoring, and I'd highly recommend you going through the procedure of pushing this out through your organization. You know, always use passphrases, not passwords. A lot of the SMB signing crap that we just covered in terms of breaking the keys lies that we, you know, compromised their password. So push out strong passwords. Push out strong passwords and require SMB signing, and you're a lot better than you were at the beginning of this talk, even if you don't do all the other complicated and involved things like fast armoring. All right. So that pretty much leaves us to, like, a Q&A, but right now I want to acknowledge that a few people have contributed on this talk. Ned Pyle. He's principal program manager at Microsoft, and it was awesome. I did my own research on MSFDN, and I said, hey, to the best of my knowledge, here's how SMB works, and here are the various circumstances that it behaves. Is this cool? And he was awesome in terms of providing feedback and giving me direction on yes and no things, so I didn't give you guys misinformation. So he was awesome. Matthew George also worked side-by-side with Ned on this one to kind of spot-check me on this. And Core Security for the MPacket library, for which all of SMB Trades, and I used their library when I was trying to figure out more on how SMB signing works. So that's about it. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.