So because this video might end up being quite long because we're gonna cover quite a lot of things I'm gonna start with just a quick breakdown of what exactly we will be covering the first thing I'm going to do is just give a quick summary of what exactly Kerberos is because maybe you've never heard of it maybe you don't know what this whole video is about so I'll just be a brief description of what the purpose of Kerberos is and the way I've decided to do things after that is that we're going to go through
the same process multiple times but in a lot more detail each time essentially so we'll start with the process of how a user logs on and then how they request access to a service and then how they actually access that service so that's the kind of overall process that we're going to go through multiple times we'll start off with a very simple summary of that so you can Just get a general idea for it then we'll go through it again in a bit more detail covering some technical details some of the nuances and some of
the small things that are important but that are kind of left out in a high-level summary and then finally we'll look at it again but this time looking at the actual messages that get sent across the network the format of them the kind of information they contain and how everything actually goes together as Part of that you'll see exactly how service principle names or SPNs come into all of this I'll also highlight the things that's the allow silver ticket and golden ticket attacks to happen and then to kind of sum everything up we're going to
take a look at a live demo of an application actually authenticating with Kerberos and we'll look at the Wireshark Network trace and actually see where all of this stuff that we've talked about Actually fits in in the real world and how it actually looks so hopefully after all of that you'll have a good understanding of how Kerberos works and also a good understanding of exactly how and why certain attacks work against it speaking of which if you're new to the channel there'll be a link in the description to a playlist that contains all of my
previous Kerberos attack demonstration and explanation videos so take a look at those if you're curious How those attacks actually work in the real world so with all of that said let's get started with a very basic explanation of what Kerberos is and what its purpose is it is essentially an authentication protocol what that means is that it's a way to prove that a user is who they say they are across the network so if you've got a program running on one computer over here and it wants to talk to this service over here across the
network and let's say that Service only allows certain user accounts to access it or at least contains some some items in it that it only wants certain users to access think of like an SQL database which is the example we're going to use throughout this whole video there might be some databases that one user account as well to access and then some other databases that other user accounts are allowed to access and that first user account is not allowed to access so we Need some way of proving that a user is authentic hence the name
authentication proving that they're authentic in that they are who they say they are if we didn't have something like this then well any user can just send us a request saying yet I'm the domain admin let me access everything obviously we don't want that so we need some way to prove that user actually is domain up in or is whoever else they say there of course this is a very common situation so There's plenty of other protocols that do this kind of thing one of them is very common in Windows networks is ntlm you've probably
heard of that and even though Kerberos is supposed to be the kind of default authentication mechanism used in Active Directory domains there's still quite a lot of situations where ntlm will get used instead one example being if you connect to something via its IP address instead of its hostname that will not use Kerberos that will fall back to aunty LM authentication so as you've probably guessed by the way I'm saying it will fall back to ntlm kerberos is generally considered to be much more secure than ntlm as you'll see throughout this video though that doesn't
mean that it can't be attacked or it's not vulnerable in certain ways but on the whole is generally considered to be much better than ntlm so how does Kerberos actually work then let's look at this first Run-through of the whole process of user logging on and then accessing a service we've got our user account over here called John Smith his username is just J dot Smith when he logs onto this computer he's going to talk to a domain controller and that's one things points out straight away actually is that even though this is Kerberos in
general that we're talking about I'm only going to be explaining how it works in an Active Directory environment because to me That's what's most useful it's ok talking about the Kerberos spec in general but 99% of the time when you're dealing with Kerberos it's going to be in a Windows network with an Active Directory domain so that's all I'm going to be focusing on here and Microsoft did make some changes to the Kerberos spec when they implemented it their implementation is called MS Kyle Kile I'll leave a link to the documentation for that in the
Description for the most part though it does very closely follow the Kerberos specification so yeah coming back to the process of logging on we've got a user J Smith he communicates with a domain controller and he says I would like a ticket for J Smith the domain controller sends him back a ticket for that user account again don't worry if you don't understand ticket is at this point we're going to go into that in a lot more detail this Is just a high-level overview so the domain controller sends him back a ticket and generally the
Windows machine that he's on will catch that ticket for the duration of his logon session so that ticket his initial use a ticket or a TGT as it's sometimes called that ticket will stay on his machine and he doesn't have to repeat these first two steps again until either he logs off or until the ticket expires but we'll get into that later so once you've got this Initial ticket you can then use that to request access to a service again here we're using SQL Server as a good example because it's a fairly common thing that
everybody knows about and it supports Kerberos authentication so obviously bear in mind going through all of these examples we're gonna be using SQL but you could swap that out for any service that allows a user to authenticate by Kerberos so the user wants to access the SQL service on a certain server it sends A request to the domain controller asking for a ticket for that service and as part of that request to prove that the user is who they say they are they supply the original ticket that they got as part of their logon process
so they take that user ticket they send that to the domain controller and say I would like to use this ticket to ask for a ticket for the SQL service gets a little bit confusing but don't worry they are the only two tickets in this whole Process the domain controller doesn't actually do any kind of check at this point to see if the user is allowed to do that it will just send back a service ticket for the SQL service regardless of whether the user is actually allowed to access the SQL service so any user
can request a service ticket for any service and in the next step you'll see why in theory that doesn't matter it's not a security risk in reality it can be but we'll get to that later on so the users Now got their original user ticket which they just kept in their cache and they could use to request more service tickets but they've also now got this service ticket for the SQL service that the domain controller has sent back to them now what they can do is actually initiate a connection to the SQL server so this
is the first time they actually communicate with the server and the service that they're trying to access so it sends that ticket to the SQL service As part of the initial authentication request and the ticket proves that the user is J Smith and it also includes a list of the groups that they're a member of and other information about the user account and because of the way this whole process works the SQL server doesn't in theory need to contact with the main controller to confirm any of this it trusts that everything in the ticket is
accurate and that the user is J Smith and they are a member of these Groups or the ticket says they remember of now obviously from just looking at the the summary there but doesn't really make any sense why the service would just trust that ticket has been accurate but as you'll see in a minute when we go through this in more detail in theory there's no way that somebody should be able to fake the ticket so just a real quick recap again when the user logs on so these first two steps only happen during the
initial Logon we ask for a ticket for J Smith the Tamika troll sends us a ticket J Smith then when we want to access the service we ask for a ticket for that service and we supply user ticket to prove that we are J Smith we get back a service ticket for the SQL service and then we send that ticket to the SQL service that proves that we are the user we say we are so now it's up to the SQL server to decide if it wants to let us in and that's why in theory
step four Isn't a security issue the domain controller sending out a service ticket for any service to any user that shouldn't be a security issue because the point of Kerberos is just to authenticate people it's just to prove they are who they say they are it's not to decide if they should be allowed in that's up to the SQL server to decide that once it knows who they are so when we get to step six where the service ticket has proved that this user is J Smith well now the SQL server can say actually J
Smith shouldn't be allowed access to this database and just deny them so hopefully that makes sense is a very high level overview I'm sure you've probably got a lot of questions as to how this is all actually secure so let's take a look at the same process again but in a bit more detail the first thing that we need to explain that's not gonna be actually on this diagram is that every Active Directory domain has an Account called the krbtgt account if we flick over to my example domain control here we've got just a bog-standard
Active Directory domain if we go in to the users container you won't see them by default but if you go to view and then advanced features and that will get back to that same users container you see this account here this krb TGT account and it's described as the key distribution center service account now key distribution center or KDC as it's Commonly shortened to in Kerberos language that basically just means the central database that stores the users and passwords and that handles incoming ticket requests so in Active Directory that is basically every domain controller so
whenever you seek a DC or key distribution center you can just think that that means to make controller and even though it is a user accounts you can't actually log on with that account you'll see it's disabled by Default don't think you can actually enable it either no you can't and it has its password set by the system to something really really complicated you don't ever need to actually know what it is you won't ever have to log on as that count you can reset the password and we'll come to that later on well it's
probably a good idea to do every now and then but for now just know that there's this user account in the domain and then it has a Really long complex password that the system knows and that nothing else knows and that's the important point is that nothing else should ever know this password so now that we understand that let's look at this whole process again so a user requests a ticket for their user account they don't send their password with that user account what they do send is some data that was encrypted using their password
this is known as preauth indication data and if You've watched my video on cope Osprey authentication and how to attack accounts that don't have that enabled then you'll understand why it's important for now we're just going to assume that this account has pre authentication enabled because that's the default so because the user account has pre authentication enabled we're going to send the current time encrypted with the users password the domain controller receives that request because The domain controller can read everybody's passwords it decrypts that timestamp using the user's password because that's what it was encrypted
with and then you can see if that timestamp is current so this is a way to help detect replay attacks basically but again not too important for now let's just assume that the user did use the correct password the domain controller checks it it's all good so it sends back the TGT which is the Ticket granting ticket and it's fairly obvious wise name that because it's a ticket that can then be used to request other tickets for other services like SQL as we saw and along with that TGT it also sends a session key now
this key at first doesn't seem too important but it's actually kind of the key no pun intended for how this whole thing works that session key is both inside the TGT and also sent separately and in each case is encrypted in a different way so Let's just say the session key was hello like it's not actually text it'll just be random bytes but let's just assume this session key is the word hello the word hello we'll get encrypted using the krbtgt accounts password as part of the ticket so it'll get stored inside the ticket and
the whole ticket is encrypted with the krbtgt account password which nobody else knows all the domain controllers and alongside the ticket it also sends that same session key so the Word hello but encrypted with the users password so John Smith's password and case so now the user has got this ticket that they can't read because it's encrypted with the krbtgt account password and they've got a session key which they can read once they decrypt it using their password so if an attacker was to request a ticket for J Smith and they don't have his password
well they're going to get back a little Useless data they're going to get the TGT is encrypted with a password they don't have the care bt GT account and they're going to get the session key which is encrypted with a password they don't have so they can't perform the next step which is to send the request to the domain controller for a service ticket that we saw before and pass on that user ticket which nobody can read apart from the domain controller and they also send what's called an Authenticator which is just some data there's
nothing particularly special in the data it's just like the user's username time stamp but it's encrypted with the session key there are M but the session key is inside the tickets that nobody can read apart from the domain controllers which we're sending to the domain controller the only other way to get the session key was to have that user's password so if we were an attacker we don't have the users Password we can't do this whole step because even if we somehow got hold of a ticket that was sent for that user account we can't
send the Authenticator which is just data encrypted with the session key because we couldn't decrypt the session key so we couldn't get that word hello in our example so say we use the wrong password to encrypt this data we pretend we know the session key is an attacker we send that with the ticket off to the domain controller the domain Controller is going to decrypt the ticket using its krbtgt account password it will read the session key from inside the ticket we could again in our example just be the word hello it then tries to
use that session key to decrypt the data that we sent as an Authenticator and obviously that's going to fail because we don't have the session key because the only way to get the session key was if we had a Smiths password in which case this is all legitimate so hopefully That makes sense is the fact that the session key is in the ticket which we can't change or fake because it's encrypted with the krbtgt accounts password the fact that the session key is in there and that we have to use that same session key to
encrypt some data and send that with the request if those two keys don't match which they're not going to unless we have the password for J Smith because that's how we get the session key in the first place well then This request is going to fail the only time I switch seeds is if we do have the password for J Smith we completed step two which gave us the ticket with the session key and and also a copy of the session key encrypted with our password then we can pass on the ticket and some more
data encrypted with the session key and everything's fine I feel like I've repeated myself a lot there but it took me a little while to get my head around So I'm trying to kind of really drive it home for you guys but apologies if I made things more confusing that hopefully you get the idea so that brings us on to step four and five which is pretty much repeat of step two or three but with the service ticket instead of with the ticket granting ticket so the user sent the request to the DC in step
three assuming everything checks out the DC sends back a service ticket for the service that they've Requested SQL in this example and a new session key like I say this is very similar to the previous steps the service ticket has a session key inside of it the service ticket is encrypted with the service accounts password not the krbtgt account this time the account that runs that service how does a domain controller know which account is responsible for which service that's where service principle names come in so SP ends or spins there's lots Of different ways
to pronounce them but yeah a service principle name which let's just take a quick look at now on the domain controller if we go to service here SQL service user and we look at their attributes there's other ways to do this but this is just a quick simple way we look at this attribute called service principle name you'll see here it's got mssql service and then the name of the machine that the service is running on there's a few different ways That the service principle name can be constructed which is why we also have this
one with a port number but you get the idea it's the service name and then the machine that's running on so if we go back to our diagram in step 4 here on the domain controller sending the service ticket back and it's encrypting that service ticket with the service accounts password basically all it's doing is just search from the domain for the account that has a service principle Name that matches the request that the client sent so when the client sends a request in step 3 it comes structs the service principle name because the client
knows he wants to connect to SQL Server 1 dot my domain data local and it wants to connect to the MS SQL service so it can construct that service principle name send that with the request in step 3 so the domain controller searches the domain find the account that's responsible for that Service on that machine uses that accounts password to encrypt the ticket and then same as before there's a session key inside that ticket and then there's also a copy of that same session key but encrypted with the users password sent along with it so
now we've got the exact same scenario where the user decrypt the session key with their own password and then step five is very similar to step three but instead of Sending a ticket to a domain controller and asking for a service ticket back now we're sending a ticket to the actual service we want to authenticate with and we're using the exact same method to prove that everything's legitimate we send them the service ticket encrypted with the service accounts password and some data that we've encrypted with the session key which again we can only get the
session key if we actually have the password for J Smith and remember the Whole purpose of this is to prove that we are J Smith to prove that we have that user accounts password that's basically what indication is it is proving that you have the password for that account so in step 6 the SQL service decrypt that ticket but it was sent the service ticket because again it was encrypted using the SQL servers against password the SQL service is running as well account if it wasn't if you'd left the SQL services on this Machine running
as local system instead of running as this user account in Active Directory that has the SPN associated with it then this would just fail but assuming that the SQL service is actually running as that SQL service use your account it can decrypt the ticket because it has access to its own password and again it can get the session key from that ticket and then compare that for the session key that was used to encrypt the Authenticator That was sent with it if they match then we know that this request is legitimate or at least that
it's come from somebody that has the password for this account and again that's all we're checking for now when I first looked into this there was one part that confused me about all of this well the whole thing confused me when I first looked into it but after spending a bit of time getting my head around it all in step 6 where we're saying that the SQL service Can just decrypt the ticket because it has access to its own password that's not strictly true because if you write the program again SQL isn't anything special it's
just a program that somebody's written you can't just read the password from Active Directory or from your own logon session of the user account that you're running as but it turns out there's a set of windows API such as accept security context and acquire credentials handle all these Things I'll leave a link to them in the description to basically take care of this for you because the operating system does have access to a hash of the user's password even if a program that's running doesn't actually directly have access to it also just to find out
when we say credentials and passwords is not literally the actual password that a user account has it'll be an ntlm hash of that password so there's still no direct Wait to just get users password that's not stored in plain text anywhere this will just be a hash of the password and that's what's used to encrypt these tickets so now that we know all of this let's take a look at some of the ways that this could be compromised we'll go through it in the order of the process that everything happens so first of all as
I've already covered in a whole separate video if this user account doesn't have pre authentication enabled We can just skip straight to step to get this user ticket and session key back for the domain controller even though we don't have the users password at first glance that doesn't seem like it would be of any use but the session key part that is sent along with the ticket the session key that's encrypted with the users password isn't just a session key I simplified it slightly for the diagram there's actually a whole structure that is Encrypted with
the users password the session key being one of the things in that structure the reason I'm not included the rest of them is because they're not very interesting there's things like the domain name and the last time the ticket was requested for this user things like that aren't particularly interesting oh because this is a structure and certain parts of the structure will always have the same value we can take that whole structure That's encrypted and just throw a load of different passwords at it with something like hash cat so program like hash cat is going
to just loop through a list of common passwords thousands of thousands of passwords or just brute-force single characters at a time it'll loop through all those passwords try to use those as the password to decrypt this whole structure and when it sees certain values in certain positions it knows that that's what the structure Should look like and then you've got the users password so that's the first attack is if pre authentication is not enabled we can just get some data back straight away that is encrypted using the user's password we can crack that offline unless
they have a really long complex password if we carry on the second place that we can see an issue is that the TGT the user ticket is encrypted using the krbtgt account password if somebody manages to get the Password for that account well then they can just pretend to be anyone they want because now they can skip step one and to create their own fake ticket using that password to encrypt it make up a random session key put that inside the ticket and use that to encrypt the extra data or the Authenticator as it's called
and send that as a request to the domain controller the main control is going to think that's completely legitimate this user is whoever they say they are That they are a member of domain admins or whatever you've put in the ticket and it will send back service ticket for any service in the domain that they can then use to do step five and six completely legitimately and now when we get to step six the service is going to think that they are the domain admin now obviously it's not easy to get the krbtgt account password
so that's not such a big worry I've done a previous video that shows One way you can do that with a DC sync attack but again you need to have compromised some some high-level accounts for that to happen really so that's not going to be a particularly common one but it is a vulnerability and that's what's known as a Kerberos golden ticket attack if you hear that phrase that is what they're referring to it means that somebody's got the krbtgt account password and now they're creating themselves these golden tickets They call golden tickets because as
we've seen you can use them to request tickets for any service on any machine on the domain and also you can set the expiry date to be like ten years so it won't need to be renewed it will just keep working forever until somebody changes the password for the krbtgt account which a lot of places never ever do the next potential problem is with step three and four in that any user can request a service ticket for any service Regardless of if they should actually have permission to access that service so if we've just got
some bog-standard domain user credentials they're not in any admin groups or anything like that we can go and request a service ticket for the SQL service well then get back a ticket there's encrypted with that service account password and we can use the same technique with something like hash cat to try and crack that offline again based on the fact that the ticket Is in a certain format so we can keep trying different passwords as the decryption key and when we get the right one will know because we'll see a certain value in a certain
position in that structure and then we know we've got that service accounts password this is a technique called Kerberos ting I've done a separate video explaining that in detail and giving a demonstration of that so I'm not going to go into that too much here but you can actually see Well outfits in which leads nicely into a silver ticket attack which as you can guess is pretty similar to a golden ticket except it only works for one service so let's say we've done the Kerberos ting technique to get the service account password for the SQL
service well now we can go straight to step five we can just create our own ticket because if you remember in Step five where we're authenticating with the service the krbtgt password and Everything like that doesn't come into it here we're assuming that that's all already taken place but if we actually have the password for the SQL service we can just create a fake ticket and again create a fake session key encrypt the Authenticator with that same session key put that session key inside the ticket which we can encrypt because we've got the password for
this service account send the whole thing to the SQL server and it's going to treat it as Being completely legitimate which means we could just also put in the ticket that we are the domain administrator and it will believe that and treat us as the domain admin letters into all of the SQL databases that the domain admin should be able to access so the defense against that whole thing is to just have really long complex passwords for your service accounts don't give any service accounts simple short common passwords if they're going to be using Kerberos
Authentication I mean don't do that in general but especially with Kerberos because now anyone with basic user credentials can get some data encrypted with that password and try and brute force crack it offline so you'll be pleased to hear that pretty much covers it for attack vectors at least ones that I know of I'm sure there are others as well and like I say if you want to see those attacks in action I've done the separate videos Covering those already and just to clarify something here before we move on in this example where we're requesting
a service ticket for the SQL service we've created a separate user account in Active Directory to run that service and that's what we've associated the SPN with the service principle name but what about network services that is built into the operating system they're just run as local system it's like when we access an SMB share on a Machine that supports Kerberos authentication remote powershell sessions support Kerberos authentication there's plenty of services that are built into windows on each machine that will allow you to authenticate with Kerberos but that we don't set to run as a
separate user account that we create in Active Directory so where does the service principle name exist for those how does the domaine controller know which password to use to Equip their service tickets well it's pretty simple it'll be the computer account that has the service principle name associated with it and this gets done automatically there's a special service principle named called host which kind of encompasses a lot of built-in windows services again all the ones I mentioned like SMB win RM remote registry all these things there's lots and lots of services you can look up
what's in that list but basically each Computer account in Active Directory will automatically have this host service principle name associated with it so when something connects to say n SMB share and wants to use Kerberos authentication the controller is going to look for an account that has that host entry it will find the computer account that has that service principal name because again the service principal name is the service followed by the machine name so if we're Connecting to a server called server two they'll be looking for a service principal name of host slash server
two it will find that computer account and it will use the computer accounts password to encrypt the service ticket the computer account password is a very long complex and they changed it automatically every 30 days by the system so we're not gonna be able to brute force or crack those offline like we can with actual user accounts that Are set to run services and support Kerberos there is a special user account you can use that behaves like a computer account it's called an MSA or a managed service account you can use that to run services
but it will have its password changed automatically every 30 days by the system like a computer account so that's kind of one solution to these attacks anyway got a bit sidetracked there let's get back to what we were talking about so let's take a look at The actual messages that get sent across the network and what's contained within each of them and like I said at the start we'll do a live demo in a second showing how all of this actually looks in Wireshark so to start with when we're sending the request to a domain
controller for a ticket we have what's called an Aes req on a s request when the domain controller sends back a ticket and the session key that is an a s rep so req req being request and then Rep repe being response then when we want to request a service ticket we have a TGS req TGS being ticket granting service and then req again for a request and we get back the service ticket and the session key for that in a TDS response once the users got the service ticket it sends it to the SQL
server and ap request so let's look at what's contained in each of those messages we'll go through it in order first of all we've got the a s req as we said at The start this contains the pre authentication data assuming preauth is enabled for the user account so that's going to be the current time encrypted with the users password it'll contain the username that we're requesting a ticket for and kind of strangely that will also contain the service name even though it will always just be krbtgt because we're requesting an initial ticket and as
we know that ticket will be encrypted with the krbtgt account Password so the domain controller receives that and it sends back the AOS rep which is the user ticket or the TGT and the session key so let's look at what's actually inside this ticket because we keep talking about tickets all over the place what does it actually look like now this isn't absolutely everything that's in a ticket I've just picked out the interesting parts so we have the name of the service that this ticket is for in This case again because this is a TGT
an initial logon ticket then this service will always be krbtgt again that seems really redundant but it actually makes sense in the ticket because this exact same ticket format and structure is used for the service tickets as well and for those it is important to have the service name in there but again for this initial a s request and a s response service name isn't going to come into it really because it's always going to be Krbtgt we have the expiry dates by default that will be ten hours you can set the limit let me
show you quickly on domain controller if we open up the group policy management console it's already in there if we open that up we look at the default domain policy this is where this will be set normally just go to settings to get a summary of it you'll see we've got Kerberos policy here you see here maximum lifetime for a service ticket And maximum lifetime for a user ticket and they're both actually set to the same value but one is in minutes mourners in hours for some reason the other both set to 10 hours so
that is the default lifetime for a user ticket or a TGT and then for a service ticket which is sent in the TGS request and response yeah coming back to the ticket itself the expiry dates and everything else that you see in here other than the service name is encrypted with the Krbtgt accounts password so all of this stuff here is kind of secure and in theory can't be changed or forged again if you get the password to that account then you can easily forge the ticket but in theory no one can force this so
the expiry date is secure the authorization data also known as the pact is something that Microsoft added to the Kerberos spec when they implemented it well the authorization data is a feature of the Kerberos spec but the actual Format and this whole pack structure is a Microsoft thing so this is kind of the important bit that we want to be able to fake when we're creating a fake ticket because this is what tells a service who this user account is basically tells you their security ID and their group membership lists and those are the things
that the service is going to use to decide if your user is allowed in so that's definitely a very important part of the ticket and then we've got the Session key which as we've discussed is the same as the session key that's sent alongside the ticket or just will be encrypted with a different password so in the ticket it will be encrypted with the service accounts password and case that service is krbtgt because this is a user ticket or a TGT and then you can see on the left hand side that that same session key
is in the a s response but encrypted with the users passwords so they can get it if they've got the Correct password so now let's move on to step three which is where we've already got a TGT and we use that to request a service ticket from the domain controller so we do that with a TGS request and that contains the name of the service that we are requesting a ticket for so this is where the service principle name will get constructed and placed in there and then when the DC receives that a concert Active
Directory for the account that has that service Principle name also in the TGS request is the ticket that we got from the initial logon process so from the a s rec and the a s rep again this ticket here is encrypted with the krbtgt account so we can't modify that or anything we're just passing it on as part of the pre off data for this TDS request to prove that we have already logged on as that user account and again to prove that the ticket is authentic we're also sending this Authenticator Which is encrypted using
the session key that is inside the TGT that was also sent with the TGT in the AAS response message in the previous step if you're not following that part then just take another look at the a s response message the a s rep what that has inside it and how the session key is included in that and also in the ticket that is included in the message as well so the domain controller receives this TGS req it sends back a service ticket And the session key again the ticket format is exactly the same as it
was for the TGT ticket so this time the service name is actually important and again the rest of the data other than the service name is encrypted with the password of the service account so not the krbtgt account this time but the account is running the service the account that has the SPN for this service in Active Directory and again the ticket contains an expiry date which is set by that Service lifetime policy we looked at earlier and also the pack so the list of all the groups that the user is a member of which
is copied directly from the TGT that was sent in the TGS request so that pretty much covers the TGS exchange so now on to step five which is actually sending the request to the SQL Server now here we include the service ticket that we got back in Step four in the TGS response and again this includes the session key which we also use to encrypt The Authenticator which proves that we have knowledge of the session key and therefore proves that this is a legitimate ticket and this will literally be the exact same ticket that we
received in the TDS response just forwarded on as part of this message so that's all of the theory out the way let's take a look at how this looks in practice to help demonstrate this I've written an extremely simple program that just uses The built-in negociate stream class in the.net framework which basically will negotiate whether to use Kerberos or ntlm so we're going to run this on my machine and also run on the server this half of the code is when it's run on the server because we'll use /s and this half is the client
again it's super simple and it's not in any way meant to be a good demonstration of coding or anything there's loads of hard-coded passwords and stuff is is not a good Program it's just purely for demonstration but just to run through it real quick on the server side we're going to start a TCP listener on any IP address on port 4 6 4 6 just a random port I decided on and we'll wait for a client to connect when the client connects we use this negotiate stream class that I mentioned and that has a function
in it called Authenticator server and we'll just let that do all the work for us basically that will Handle the Kerberos authentication once it's completed the authentication will just print out the user's name their groups that they're a member of and also the authentication type that got used so that should say Kerberos if we do everything correctly otherwise it will fall back to ntlm will see ntlm in there and then in the client side were just using TCP client to connect to the remote server we are specifying some credentials here now normally in Windows You're
not going to need to specify credentials to use Kerberos that's one of the good things about it is that when a user connects to a network service it doesn't prompt them for their any credentials when the user first logs on they get their TGT that will get cached and then whenever they need to request a service ticket it will just use that TGT from the cache so they don't have to keep putting their password in to get a new TGT in this case though I'm going to Be running this from a non domain machine so
we need to specify some credentials because we didn't do that initial logon to get a cache ticket this second parameter here for the authenticators client function is the service principle name which we've talked a lot about so here I'm just going to pass that in the command line arguments you'll see what I enter for that one we've run the program so I've copied the program over to this machine Now this is the domain controller as well but just pretend this is the separate SQL server doesn't have to be on the domain controller it's just that
I've only got the one VM here so we're going to try and simulate the SQL service thanks setting the authentication by running this program as our SQL service account so we'll just do run as user SQL SVC start new command prompt except we can't because they can't log on to the PC or The server sorry there we go now the gun cool so we've got this client prompt running as that SQL service account and we need to be running as that because that's the account that has the SPN associated with it so just curb demo
slash s because that was how we set it up and then on our client machine we're on curb demo specify the machine that we want to connect to now you remember that I said you can't use Kerberos to connect to an IP address normally that's because it's the service principal name that matters and normally the service principal name will get constructed from whatever address you are trying to connect to I've written that program slightly differently so that's not the case but yeah ordinarily with most tools if you tried to connect to an IP address it's
not going to use Kerberos but as long as we specify the service principal name manually here in the correct way we Should be fine so it was ms SQL Tessa PC / then the machine name which was that if we do that correctly she was successfully authenticated using Kerberos so now if we go back onto the domain controller we will see that the authentication type was Kerberos if thinks we're running as that J Smith account because that's the you know and password we entered and I can see that we're a member of all these groups
and they are the correct groups that that J Smith account is a member of and again that's because here we're specifying the credentials of J Smith so let's look at that with Wireshark running so we'll start the capture and then we will do that caption again it's connected let's stop the wireshark capture or filter it to Kerberos and now we can see the whole process and kind of tie that in to all things that we've been talking about so we can kind of ignore these first two because that's just because pre Authentication is enabled on
this account which is the default so if free authentication wasn't enabled we wouldn't see this error message but because it is we get that error and then it tries again with pre authentication so here we'll see the AS rec which remember from hopefully so this is the first part where we are requesting a TGT or a user ticket for this user account and we can see that we've got this PA data pre Authentication data which is a timestamp and encrypted timestamp so that much is up nicely to everything else that we've seen already we can
also see we got the username in this client name or cname string so that's the J Smith account and then we get the service name which will be krbtgt krbtgt HTTP dot local so this is where the service principle name goes basically you'll see when we do that all TGS requests that'll make a bit more Sense but for now you can just see that the service name or s name is krbtgt as we showed in the diagram so then we get to step two on our diagram where the server sends back the ticket for the
user in the AS rep message suppose have a look at that again we can see that the username to this ticket is for is J Smith and then we get the actual ticket so this is the ticket structure that we looked at has the service name in plain text anyone can read this and then it Has the encrypted part which is all of those things like the expiry date and the pack or the group list and everything is in there so that's all encrypted so we can't just read that in Wireshark then we've got the
second encrypted part which is not part of the ticket so this is where the session key goes that's encrypted with the users password so again in the ticket here in the encrypted part of the ticket there will Be that session key encrypted with the krbtgt account password and then in the encrypted part here it's just part of the a a threat message this will be encrypted with the users password and this also has a copy of that session key in so that matches up with everything that we've seen so far then we get on to
the TGS request which is the user using that ticket to actually request a service ticket so the original ticket the users ticket the TGT Is in here if we go in to pre authentication data and then pierdes of TGS request and then we get an AP request so you might notice that that's actually the same type of message that we use at the very end of this to actually authenticate with the SQL service an AP request and that's just because we're doing the exact same thing here when you think about it we're using a ticket
to authenticate with a Kerberos service and that's exactly what we do When we connect to the SQL Server that's also exactly what we do at this point we use our user ticket our TGT to authenticate with the Kerberos service in this case that service happens to be part of the whole Kerberos system which is the TGS the ticket panting service so we send it the ticket that we got encrypted with the krbtgt camp password this is the exact same ticket that we got back in the a s response and then we also send to the
Authenticator which like we said is a load of data that's encrypted with the session key that is also present in the ticket that we got back in the a s response hopefully I've already done a good enough job in explaining that but they can kind of see it in action then the main part of the request here is just requesting that we actually get a service ticket for a specific service so this is where the service principle name comes in again the service principle Name that we specified we see here was this mssql Sui C
/ DC 1 / blah and that's what we have here split into different parts based on where the slashes are basically but you can have another slash here with another identifier at the end and that will just get split into a third part here but this is essentially the service principle name being sent across the network so that's how when the DC receives this request it knows which Account to go and look for to get the password from so it does that it goes and gets the password from that account and then it sends back
this TGS response and then the service ticket itself so this time we don't see krbtgt in this we see the NS SQL service because that's the service that this ticket is for this is no longer a TGT this is a service ticket this part and then again it's got the session key and all the pack everything encrypted in here with the Service accounts password so whichever account has this service principle name associated with it this whole encrypted part is encrypted with that password and then we also get a copy of the session key and some
other data encrypted with the users password here so that we can retrieve that and then we send all that to the service now you might wonder why you're not seeing that last part where we actually send the AP request and that's because up until now this has all Been happening over the Kerberos protocol whereas at this point when we start to actually send the authentication request to the server itself the application server so SQL in this case now that needs to be done over whatever protocol it is that SQL server expects incoming data to come
in so it'll be some proprietary SQL Server protocol in our case because we're using this dotnet program that just uses the built-in negociate stream Class it'll be some proprietary protocol that uses so we take the Kerberos filter off and now we look for wherever the TGS rep is that was the last message that we could see we'll now see some more messages being sent that just general data if we look through them we actually keep an eye on the data down here pretty sure we will see you on somewhere yes so here we can see
we've got HTV dot local mssql service bah-bah-bah so this will Probably be part of the proprietary protocol or whatever that is sending the data over now I'm making some assumptions there I don't know exactly what format the negotiate stream class sends its data in but hopefully you get the idea though the final part the AAP requests step 5 in our diagrams that gets sent kind of inside whatever protocol is that the two programs are communicating with so it might be HTTP HTTP might be something completely Proprietary like this is probably using or it could be
the SQL Server Network protocol whatever it is that will contain the actual Kerberos ap request which will then get kind of stripped out of it and processed by the operating system as a Kerberos ap request so I think that pretty much covers it that was a very long video I've got sore throat now from talking so much but I hope they explained some things to you sorry for many things more Confusing in some parts than they needed to be hopefully if you look at the diagrams enough even with my waffling over the top of them
you can still get a good idea of what's going on I will also post some static images of each of those parts of the process and all of the protocol messages and stuff like that I will post those on my blog which I'll leave a link to in the description so that you can go and have a look at those without having to watch this 10 hour Video again to find the bits you want and with that I will leave you in peace and I'll see you in the next video