简体   繁体   English

asp.net,c#,用户提供连接字符串和查询

[英]asp.net, c#, User provided Connection string and Query

We're trying to create a asp.net page that gives our users the ability to pull information directly from their own database to our website. 我们正在尝试创建一个asp.net页面,使我们的用户能够将信息直接从他们自己的数据库提取到我们的网站。 The user will have the ability to provide: 用户将有能力提供:

  • hostname, port, database name, username, password, and query. 主机名,端口,数据库名称,用户名,密码和查询。

I have some serious security concerns regarding this and was wondering how this page could be secured so that we're preventing users from pointing to the localhost database or other type hacks that could enable them to have access to our database. 我对此有一些严重的安全问题,并想知道如何保护此页面,以便我们阻止用户指向localhost数据库或其他类型的黑客,这些黑客可以使他们能够访问我们的数据库。 Can anyone please advise? 任何人都可以建议吗?

We're using SqlConnectionStringBuilder to build the connection to the user's database and doing some simple checks to ensure that the host cannot be "localhost" or other addresses that point to our server. 我们正在使用SqlConnectionStringBuilder来构建与用户数据库的连接,并进行一些简单的检查以确保主机不能是“localhost”或指向我们服务器的其他地址。 I feel like doing this leaves a potential security holes open. 我觉得这样做会留下潜在的安全漏洞。

Also, the query that they provide is checked against some keywords that shouldn't be allowed. 此外,还会针对某些不应允许的关键字检查它们提供的查询。 Again, I think this leaves a lot open if not properly implemented. 同样,我认为如果没有正确实施,这将留下很多开放。 (We essentially want them to only be able to do a SELECT from their own DB). (我们基本上希望它们只能从自己的DB中进行SELECT)。

Finally, we do an EXEC sp_executesql with the query of the user. 最后,我们使用用户的查询执行EXEC sp_executesql。

I'd love to hear how others have dealt with this? 我很想听听其他人如何处理这个问题? Klipfolio is an organization that has a similar type functionality so if anyone knows how they've addressed this issue, that would be really awesome!! Klipfolio是一个具有相似类型功能的组织,所以如果有人知道他们如何解决这个问题,那真是棒极了!

Thanks! 谢谢!

Without knowing all the particulars of your situation I don't think your approach is necessarily the best. 在不了解您所处情况的所有细节的情况下,我认为您的方法不一定是最好的。 "Normally" your database server is not exposed to the world, it is behind a firewall and direct connections from the outside world are not allowed. “通常”您的数据库服务器不会暴露给全世界,它位于防火墙后面,并且不允许来自外部世界的直接连接。 The people that will be using your webpage are likely to also have their database behind a firewall and thus even if were not trying to do anything malicious your webserver will not be able to make a direct connection to their database server because it is likely behind its own firewall. 将使用您的网页的人可能也将他们的数据库放在防火墙后面,因此,即使没有尝试做任何恶意的事情,您的网络服务器将无法直接连接到他们的数据库服务器,因为它可能在其后面自己的防火墙 They would have to either expose it to the world or know the IP address of your server to poke a hole in their firewall to allow your server to connect. 他们必须要将它暴露给世界,要么知道服务器的IP地址,以便在防火墙中挖出一个洞以允许服务器连接。

Whenever I've allowed clients to upload data to my server it is been via a text or csv file or an Excel file. 每当我允许客户端将数据上传到我的服务器时,它都是通过文本或csv文件或Excel文件。 This allows you to get around any firewall issues on the client side. 这使您可以解决客户端上的任何防火墙问题。 Now you need to worry about SQL Injection attacks within the data. 现在您需要担心数据中的SQL注入攻击。 So there are two things to do: first make sure you use parameters when performing an INSERT or UPDATE, and the second is to make sure the process that is performing the upload has the lowest possible privileges to your database. 因此,有两件事要做:首先确保在执行INSERT或UPDATE时使用参数,第二个是确保执行上载的进程对数据库具有最低权限。

If you must make a direct connection to the clients' database then I would do the following things to enhance security. 如果您必须直接连接到客户端的数据库,那么我会执行以下操作来增强安全性。

First is resolve the hostname to ip addresses. 首先是将主机名解析为ip地址。

IPAddress[] addresslist = Dns.GetHostAddresses(hostname);

Then check the results in the addresslist that they do not resolve to any private addresses as these would not work to connect to a client in any case. 然后检查地址列表中他们未解析的结果到任何私有地址,因为在任何情况下这些都无法连接到客户端。

10.0.0.0 to 10.255.255.255. 10.0.0.0至10.255.255.255。 172.16.0.0 to 172.31.255.255. 172.16.0.0至172.31.255.255。 192.168.0.0 to 192.168.255.255. 192.168.0.0到192.168.255.255。

Also make sure that the address does not resolve to your own public address(es). 还要确保该地址无法解析为您自己的公共地址。

The second thing is at least just for this one function do not use a Trusted Connection. 第二件事至少只是因为这个功能不使用可信连接。 Instead use a username/password and assign that user's security rights on the database to be the minimum possible to accomplish the task of uploading data. 而是使用用户名/密码并在数据库上分配该用户的安全权限,以尽可能完成上传数据的任务。 And still use parameterized calls when importing the data. 并且在导入数据时仍然使用参数化调用。

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

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