简体   繁体   English

如何检查可公开访问的网址?

[英]How can I check a url is publicly accessible?

I have a web application that you can use to import information from another site by giving it a url. 我有一个Web应用程序,您可以通过给它一个URL来从另一个站点导入信息。 It's been pointed out that you could use this feature to access a private site that is hosted on the same web server. 有人指出,您可以使用此功能来访问位于同一Web服务器上的私有站点。
So... 所以...
How can I check that a given url is publicly accessible (whether on the same web server or somewhere different)? 如何检查给定的URL是否可以公开访问(无论是在同一Web服务器上还是在其他地方)?

FIX: I ended up doing this: FIX:我最终这样做:

    protected static bool IsHostWithinSegment(string Host)
    {
        Ping pinger = new Ping();
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        PingOptions options = new PingOptions();
        options.Ttl = 1;

        PingReply reply = pinger.Send(Host, 1000, buffer, options);

        return reply.Status == IPStatus.Success;
    }

    private static Uri BindStringToURI(string value)
    {
        Uri uri;
        if (Uri.TryCreate(value, UriKind.Absolute, out uri))
            return uri;

        // Try prepending default scheme
        value = string.Format("{0}://{1}", "http", value);
        if (Uri.TryCreate(value, UriKind.Absolute, out uri))
            return uri;

        return null;
    }

The only requirement of mine that it doesn't fulfil is that some installations of our product will exist alongside each other and you won't be able to import information across them - I suspect this will require using a proxy server to get an extrenal view of things but as it's not a requirement for my project I'll leave it for someone else. 我唯一不满足的要求是我们产品的某些安装将彼此并存,并且您将无法跨它们导入信息-我怀疑这将需要使用代理服务器来获取外部视图的东西,但由于这不是我的项目的要求,因此我将其留给其他人使用。

-- I've just realised that this does entirely solve my problem since all the publicly accessible urls resolve to virtual or routable ips meaning they hop. -我刚刚意识到这完全解决了我的问题,因为所有可公开访问的url都解析为虚拟或可路由的ip,这意味着它们会跳。

Only two things spring to mind. 我只想到两件事。

  1. Have a trusted external server verify the visibility of the address (like an HTTP Proxy) 让受信任的外部服务器验证地址的可见性(例如HTTP代理)
  2. Check the DNS record on the site -- if it resolves to something internal ( 127.0.0.1 , 10.* , 192.168.* , etc) the reject it -- of course, this might not work depending on how your internal network is set up 检查站点上的DNS记录-如果它解析为内部内容( 127.0.0.1 10.*192.168.*等),则拒绝它-当然,这可能无法正常工作,具体取决于内部网络的设置向上

Not knowing if this is on a 3rd-party hosting solution or inside your/your company's internal network makes it hard to say which solution would be best; 不知道这是在第三方托管解决方案上还是在您/您公司的内部网络内部,因此很难说哪种解决方案是最好的。 good luck. 祝好运。

EDIT: On second thought, I've canceled the second suggestion as it would still leave you open to DNS rebinding. 编辑:经过深思熟虑,我已经取消了第二条建议,因为它仍然使您可以进行DNS重新绑定。 I'll leave this here for that purpose, but I don't think it's a good idea. 为此,我将其留在此处,但我认为这不是一个好主意。

That said, if you have some ability to control the network makeup for this server, then it should probably live in its own world, dedicated, with nothing else on its private network. 就是说,如果您具有控制此服务器的网络组成的能力,则该服务器可能应该生活在专用的专用环境中,而专用网络上没有其他内容。

Run a traceroute (a series of pings with short TTL's to the address, if the firewall(s) is(are) one of the hops then it's visible from outside the organisation so should be acceptable. 运行一条traceroute(到该地址的一系列带有短TTL的ping,如果防火墙是跃点之一,那么从组织外部就可以看到它,因此应该可以接受)。

System.Net.NetworkInformation has a ping class that should give you enough information for a tracert like routine. System.Net.NetworkInformation具有ping类,该类应为您提供足够的信息,以使诸如例程之类的tracert。

This does sound like a big hole though, another approach should probably be considered. 尽管这确实听起来像是一个大漏洞,但应该考虑使用另一种方法。 Preventing the machine that runs this prog. 防止运行该程序的机器。 from accessing any other machine on the internal network may be better - a kind of internal firewall. 访问内部网络上的任何其他计算机可能会更好-一种内部防火墙。

I've added a simple traceroute, since you like the concept:- 我添加了一个简单的traceroute,因为您喜欢这个概念:-

class Program
{
    static void Main(string[] args)
    {
        PingReply reply = null;
        PingOptions options = new PingOptions();
        options.DontFragment = true;
        Ping p = new Ping();
        for (int n = 1; n < 255 && (reply == null || reply.Status != IPStatus.Success); n++)
        {
            options.Ttl = n;
            reply = p.Send("www.yahoo.com", 1000, new byte[1], options);
            if (reply.Address != null)
                Console.WriteLine(n.ToString() + " : " + reply.Address.ToString());
            else
                Console.WriteLine(n.ToString() + " : <null>");
        }
        Console.WriteLine("Done.");
        System.Console.ReadKey();
    }
}

Should be good enough for a reliable local network. 对于可靠的本地网络应该足够好。

The wininet dll has a function InternetCheckConnection Wininet dll具有InternetCheckConnection功能

Allso look at InternetGetConnectedState 大家看看InternetGetConnectedState

Check the URL address, and see if it matches your server address? 检查URL地址,看看它是否与您的服务器地址匹配?

edit : or check against a range of addresses... 编辑 :或检查地址范围...

But all this does not answer the question: could the client access it? 但是所有这些都不能回答问题:客户端可以访问吗?

Maybe some script in the browser to check that the url is accessible, and informing the server of the result. 也许浏览器中有一些脚本可以检查URL是否可访问,并将结果通知服务器。 But the user could edit the page, or simulate the result... 但是用户可以编辑页面或模拟结果...

Have the client read the url contents and send it back to the server, instead of having the server fetch it? 客户端是否已读取url内容并将其发送回服务器,而不是由服务器来获取它?

Don't worry about the public accessibility of anyone else's web assets, that question does not have a definite answer in all cases. 不必担心任何人的Web资产对公众的可访问性,该问题在所有情况下都不能给出明确的答案。 Just try not to compromise the access policy to your own (or your customer's etc.) web assets. 只是尽量不要对您自己(或客户的等)Web资产的访问策略做出妥协。

Use the existing access control mechanisms to control the web application's access. 使用现有的访问控制机制来控制Web应用程序的访问。 Don't just consult the access control mechanisms in order to duplicate them in the web application. 不要只是为了在Web应用程序中复制访问控制机制而咨询它们。 That would be relying on the web application to refrain from using its full access - a false reliance if the web application ever gets compromised or if it simply has a bug in the access control duplication functionality. 那将依赖于Web应用程序来避免使用其完全访问权限-如果Web应用程序遭到破坏或访问控制复制功能中仅存在错误,那将是虚假的依赖。 See http://en.wikipedia.org/wiki/Confused_deputy_problem . 参见http://en.wikipedia.org/wiki/Confused_deputy_problem

Since the web application acts as a deputy of external visitors, treat it if you can as if it resided outside the internal network. 由于Web应用程序充当外部访问者的代理,因此请尽可能将其视为位于内部网络之外。 Put it in the DMZ perhaps. 也许将其放在DMZ中。 Note that I'm not claiming that the solution is one of network configuration, I'm just saying that the solution should be at the same level at which it is solved if the visitor would try to access the page directly. 请注意,我并不是说该解决方案是网络配置之一,而是说如果访问者尝试直接访问该页面,则该解决方案应处于解决该问题的相同级别。

Make the web application jump through the same hoops the external visitor would have to jump. 使Web应用程序通过外部访问者必须跳过的同样的循环。 Let it fail to access resources the external visitors would have failed to access, too. 让它无法访问外部访问者也将无法访问的资源。 Provide an error page that does not let the external visitor distinguish between "page not found" and "access denied". 提供一个错误页面,该页面不允许外部访问者区分“找不到页面”和“拒绝访问”。

You are asking the wrong question. 您在问错问题。 You should be asking, how can I limit access to a given URL so that only people on a certain network can access it? 您应该问,如何限制对给定URL的访问,以便只有特定网络上的人才能访问它?

The fact is, that you cannot test in the way that you wanted, because you likely do not have access to other sites on the same web server, in order to run a script that attempts to retrieve a URL. 事实是,您无法以所需的方式进行测试,因为您可能无法访问同一Web服务器上的其他站点,从而无法运行尝试检索URL的脚本。 It is better to deny all access except the access that you wish to allow. 最好拒绝所有您希望允许的访问之外的访问。

Perhaps a firewall could do this for you, but if you want more finegrained control, so that some URLs are wide open, and others are restricted, then you probably either need help from the web server software or you need to code this into the application that serves the restricted URLs. 也许防火墙可以为您做到这一点,但是如果您想要更细粒度的控制,以便某些URL可以广泛打开,而其他URL受到限制,那么您可能需要Web服务器软件的帮助,或者需要将此代码编码到应用程序中提供受限制的网址。

If you are worried that your web application might be used to transfer data that comes from other servers protected by the same firewall which protects you, then you should change the application to disallow any URLs where the domain name portion of the URL resolves to an IP address in the range which is protected by the firewall. 如果您担心您的Web应用程序可能被用来传输来自受同一防火墙保护的其他服务器的数据,该防火墙为您提供保护,则应更改该应用程序以禁止将URL的域名部分解析为IP的任何URL。地址在受防火墙保护的范围内。 You can get that address range information from the firewall administrator. 您可以从防火墙管理员那里获取该地址范围信息。

This is only really a concern on in-house systems because in 3rd party data centers there should not be any private servers that don't have their own protection. 这实际上只是内部系统上的一个问题,因为在第三方数据中心中,不应有没有自己保护的私有服务器。 In other words, if it is at your company, they may expect their firewall to protect the whole data center and that is reasonable, if a bit risky. 换句话说,如果它在您的公司中,他们可能希望防火墙保护整个数据中心,并且这是合理的(如果有风险的话)。 But when you rent hosting from a 3rd party with a data center on the Internet, you have to assume that everything inside that data center is equally as potentially hostile as the stuff outside. 但是,当您从Internet上具有数据中心的第三方租用主机时,您必须假定该数据中心内的所有内容与外部内容一样具有潜在的恶意内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM