【问题标题】:C# Http Basic Authentication BypassC# Http 基本身份验证绕过
【发布时间】:2012-01-03 22:20:12
【问题描述】:

在 C# ASP.NET 应用程序上,我设法绕过了基本身份验证(通过 HTTPWebRequest 上的“授权”标头发送用户名/密码),我终于解锁了受 htaccess 保护的目标页面(位于不同的服务器上,基本身份验证)并将流发送回浏览器。

一旦用户点击链接,问题就会出现,基本身份验证登录框会再次弹出。我们不希望用户再次输入用户名/密码。

看来我需要在标头中发回一些东西来告诉浏览器它用于授权的用户名/密码。

我试过了:

  • 旧的“用户名:密码@主机”格式(不安全,不再允许在 IE 上使用)。
  • HTTPWebRequest,这给了我之前描述的问题。

注意事项:

  • 被访问的远程服务器是一个黑盒子。

有没有办法做到这一点? (也可以在 JavaScript 中完成)。

这是我的 HttpRequest 函数:

    public void DoWebRequest(String email, String psw, String hostname, 
    int port, String req_method, String webpage)
    {

    String path = hostname + ":" + port + "/" + webpage;
    String userdata = email + ":" + psw;
    System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(path);
    byte[] authBytes = Encoding.UTF8.GetBytes(userdata.ToCharArray());
    String req_short_host_temp = hostname;
    String req_short_host = req_short_host_temp.Replace("http://", "");

    Uri uri = new Uri(path);
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri) as HttpWebRequest;
    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
    req.Method = req_method;
    req.PreAuthenticate = false;
    req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.Headers.Add("Accept-Language: en-us,en;q=0.5");
    req.Headers.Add("Accept-Encoding: gzip,deflate");
    req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    req.KeepAlive = true;
    req.Headers.Add("Keep-Alive: 1000");
    req.ReadWriteTimeout = 320000;
    req.Timeout = 320000;
    req.Host = req_short_host;
    req.AllowAutoRedirect = true;

    req.ContentType = "application/x-www-form-urlencoded";
    req.Headers.GetType().InvokeMember("ChangeInternal", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, req.Headers, new object[] { "Host", req_short_host });

    var headers = new MyHeaderCollection();
    req.Headers = headers;
    headers.Set("Host", req_short_host);

    StreamWriter sw = new StreamWriter(req.GetRequestStream());
    sw.Write("/" + "?user=" + email + "&password=" + psw);
    sw.Close();

    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string tmp = reader.ReadToEnd();

    foreach (Cookie cook in response.Cookies)
    {
        tmp += "\n" + cook.Name + ": " + cook.Value;
    }

    Response.Write(tmp);
    Response.End();

}

【问题讨论】:

  • 你知道有什么方法可以实现吗?我尝试过使用 cookie,但它并没有通过 html 持续存在。

标签: c# httpwebrequest basic-authentication


【解决方案1】:

不知道 javascript,但我相信在 c# 中没有办法做到这一点。您可以过滤所有用户交互,以便浏览器永远不会直接访问其他服务器。为此,请重写内容中的所有 url 以指向您的脚本(反向代理)。

【讨论】:

  • 谢谢,我想我在一次测试中以两种方式尝试了这个选项。第一个测试:我在目标位置编辑了 html,因此每个链接都指向整个目标位置路径(例如,对于原始的“/Frame.htm”,我输入“chesms.servebeer.com:8800/Frame.htm"”)但是当用户点击该链接时,无论如何都会出现登录屏幕。第二个测试:我还尝试将这些链接替换为发送请求的地址(chesms.com.ar),但这会在发送请求的服务器中出现 404 错误,即不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2015-05-13
  • 2021-03-21
  • 2011-11-12
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多