Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple 401 requests with Titanium Proxy. #1

Open
JDCain opened this issue Apr 16, 2020 · 0 comments
Open

Multiple 401 requests with Titanium Proxy. #1

JDCain opened this issue Apr 16, 2020 · 0 comments

Comments

@JDCain
Copy link

JDCain commented Apr 16, 2020

I spoke to soon regarding my success. The following code works for a site, but after the initial success other items on the page may also return 401 wanting to reauthorize and this seems to fail. Also seems to effect other pages. Wanted to know if you had any ideas on why images or other items on a page hosted in the same location might do this.

I can't access the stream with Titanium like you do in your examples or in the benderproxy.

I may also just not understand NTLM enough. After the passing of type 3 do I need to pass it again with every 401 reply?

private async Task OnResponse(object sender, SessionEventArgs e)
{
    // Only do any processing on the response if the response is 401,
    if (e.HttpClient.Response.StatusCode == 401)
    {
        var authHeaders = e.HttpClient.Response.Headers.Where(h => h.Name.IndexOf("WWW-Authenticate", StringComparison.InvariantCultureIgnoreCase) >= 0);
        ISeleniumProxyAuth auth = Settings.Proxy.Auth.FirstOrDefault(a =>
            a.Hosts.Any(h => h.IsMatch(e.HttpClient.Request.Host))
            && authHeaders.Any(header => header.Value.StartsWith(a.Type.ToString())));
        switch (auth?.Type)
        {
            case AuthType.Basic:
            {
                var basicAuthHeaderValue = $"{auth.User}:{auth.Pass}";
                var encodedHeaderValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(basicAuthHeaderValue));
                e.HttpClient.Request.Headers.AddHeader("Authorization", "Basic " + encodedHeaderValue);
                e.ReRequest = true;
                break;
            }
            case AuthType.NTLM:
            {
                HttpHeader header = authHeaders.FirstOrDefault(x =>
                    x.Value.StartsWith(NtlmGenerator.AuthorizationHeaderMarker));

                if (header?.Value == NtlmGenerator.AuthorizationHeaderMarker)
                {
                    var type1 = new NtlmNegotiateMessageGenerator();
                    var type1HeaderValue = type1.GenerateAuthorizationHeader();
                    e.HttpClient.Request.Headers.AddHeader("Authorization", type1HeaderValue);
                    e.ReRequest = true;
                }
                else
                {
                    var type2 = new NtlmChallengeMessageGenerator(header?.Value);
                    var type3 = new NtlmAuthenticateMessageGenerator(null, null, auth.User, auth.Pass, type2);
                    var type3HeaderValue = type3.GenerateAuthorizationHeader();
                    e.HttpClient.Request.Headers.RemoveHeader("Authorization");
                    e.HttpClient.Request.Headers.AddHeader("Authorization", type3HeaderValue);
                    e.ReRequest = true;
                }

                break;
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant