1

I've been recently working on a project in which I need to access a asp.net web API in order to get some data. The way I've been gaining access to this API so far is by manually setting the cookies manually within the code and then using requests to get the information that I need. My task now is to automate this process. I get the cookies by using the Chrome developer tools, in the network tab. Now obviously the cookies change every once in a while so I've been trying to make something that will automatically change the cookies inside.

I should mention that the network at which this is being done is air-gaped and getting python libraries inside is kind of tedious, so I am trying to avoid that. It is also the reason why getting code examples here is very complicated.

The way the log-in process works in this web app is as follows (data from chrome dev tools):

  1. Upon entering the URL there are a bunch of redirects which seem to do nothing.
  2. A request is made to /login.aspx which returns a "set-cookie: 'sessionId=xyz'" header and redirects to /LandingPage.aspx
  3. A request is made to /LandingPage.aspx with said cookie which returns a "set-cookie" header with a bunch of cookies (ASP.NET etc'). These are the cookies that I need in order to make the python script access the API.

What's written above is the browser way of doing things, when I try to imitate this in python requests, I get the first cookie from /login.aspx but when it redirects to /LandingPage.aspx, I get a 401 Unauthorized with the following headers:

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

After having done some reading I understood that these response headers are related to NTLM and Kerberos protocols (side question: if it responds with both headers does it mean that I need to provide both authentications or that either one will suffice?).

Quick google search yielded that after these mentioned responses should follow a request with the Kerberos/NTLM token (which I have no idea how to acquire) in order to get a 200 response. I find this pretty weird considering the browser doesn't make any of these requests and the web app just gives it the cookies without it seemingly transferring any NTLM or Kerberos data.

I've thought of a few ways to overcome this and hopefully you could help me figure out whether this would work.

  1. Trying to get the requests-kerberos or requests-ntlm libraries for python and using those to overcome this problem. I would like your opinion to whether this would work. I am reluctant to use this method though, because of what was mentioned above.

  2. Somehow using PowerShell to get these tokens and then somehow using these tokens in python requests without the above mentioned libraries. But I have no idea if this would work either.

I would very much appreciate anyone who could maybe further explain the process that's happening here in general, and of course would greatly appreciate any help with solving this.

Thank you very much!

MesserOr
  • 23
  • 7
  • re: `request-kerberos` / `requests-ntlm`, and `I am reluctant to use this method though, because of what was mentioned above.` I don't really understand which part you mentioned makes you reluctant to do that, but I would start with these. And specifically I would try to use Kerberos and avoid NTLM wherever possible. I think your description `after these mentioned responses should follow a request with the Kerberos/NTLM token` is backwards; if the auth is provided first, there's no need to respond with 401, and this is probably what happens in the browser, and why you don't see those. – briantist May 08 '22 at 18:42
  • I am reluctant because the network is completely air-gaped and getting stuff in is pretty hard. Do you happen to know the difference between requests-kerberos and requests-negotiate? Also, it says that in order for requests-kerberos to work there has to be a TGT cached already on the PC. This program is supposed to run for weeks without being interfered with and to my understanding these tickets expire after about 10 hours. Any idea how to remedy this? – MesserOr May 08 '22 at 18:49
  • I can't fully speak to your unique network's needs. The Kerberos ticket will need to be refreshed; so will NTLM. Those are separate from the cookie you get back from the application after having authenticated with one of those methods. It sounds like the _application_ considers you authenticated far beyond the life of the credentials used to perform a login to the application. – briantist May 08 '22 at 18:53
  • Negotiate btw means to try Kerberos first, and then fall back to NTLM if Kerberos is not available or supported (I don't think the fallback happens if Kerberos was supported but login was unsuccessful, but I am not certain). More information: https://learn.microsoft.com/en-us/windows/win32/secauthn/microsoft-negotiate – briantist May 08 '22 at 18:54
  • re: `It sounds like the application considers you authenticated far beyond the life of the credentials used to perform a login to the application.` I don't think that's true actually but nevermind that. Is there a way in which I can force a TGT ticket to be refreshed from python? – MesserOr May 09 '22 at 05:05
  • I'm afraid I'm not sure about that. – briantist May 09 '22 at 05:23

1 Answers1

2

Trying to get the requests-kerberos or requests-ntlm libraries for python and using those to overcome this problem. I would like your opinion to whether this would work. I am reluctant to use this method though, because of what was mentioned above.

Yes, requests-kerberos would work. HTTP Negotiate means Kerberos almost 100% of the time.

For Linux I'd slightly prefer requests-gssapi, which is based on a more maintained 'gssapi' backend, but at the moment it's limited to Unix-ish systems only – while requests-kerberos has the advantage of supporting Windows through the 'winkerberos' backend. But it doesn't really matter; both will do the job fine.

Don't use NTLM if you can avoid it. Your domain admins will appreciate being able to turn off NTLM domain-wide as soon as they can.

Somehow using PowerShell to get these tokens and then somehow using these tokens in python requests without the above mentioned libraries. But I have no idea if this would work either.

Technically it's possible, but doing this via PowerShell (or .NET in general) is going the long way around. You can achieve exactly the same thing using Python's sspi module, which talks directly to the actual Windows SSPI interface that handles Kerberos ticket acquisition (and NTLM, for that matter).

(The gssapi module is the Linux equivalent, and the spnego module is a cross-platform wrapper around both.)

You can see a few examples here – OP has a .NET example, the answer has Python.

But keep in mind that Kerberos tokens contain not only the service ticket but also a one time use authenticator (to prevent replay attacks), so you need to get a fresh token for every HTTP request.

So don't reinvent the wheel and just use requests-kerberos, which will automatically call SSPI to get a token whenever needed.

it says that in order for requests-kerberos to work there has to be a TGT cached already on the PC. This program is supposed to run for weeks without being interfered with and to my understanding these tickets expire after about 10 hours.

That's typical for all Kerberos use, not just requests-kerberos specifically.

If you run the app on Windows, from an interactive session, then Windows will automatically renew Kerberos tickets as needed (it keeps your password cached in LSA memory for that purpose). However, don't run long-term tasks in interactive sessions...

If you run the app on Windows, as a service, then it will use the "machine credentials" aka "computer account" (see details), and again LSA will keep the tickets up-to-date.

If you run the app on Linux, then you can create a keytab that stores the client credentials for the application. (This doesn't need domain admin rights, you only need to know the app account's password.)

On Linux there are at least 4 different ways to use a keytab for long-term jobs: k5start (third-party, but common); KRB5_CLIENT_KTNAME (built-in to MIT Kerberos, but only in recent versions); gss-proxy (from RedHat, might already be part of the OS); or a basic cronjob that just re-runs kinit to acquire new tickets every 4-6 hours.

I find this pretty weird considering the browser doesn't make any of these requests and the web app just gives it the cookies without it seemingly transferring any NTLM or Kerberos data.

It likely does, you might be overlooking it.

Note that some SSO systems use JavaScript to dynamically probe for whether the browser has Kerberos authentication properly set up – if the main page really doesn't send a token, then it might be an iframe or an AJAX/XHR request that does.

user1686
  • 13,155
  • 2
  • 35
  • 54