简体   繁体   English

如何确保数据库连接安全?

[英]How do I make my database connection secure?

I'm currently working on a website for my church's college group, and am started to get a little worried about the security of what I'm writing. 我目前正在为我教会的大学团队建立一个网站,我开始对我正在写的东西的安全性感到有些担忧。 For instance, I use this function: 例如,我使用这个函数:

function dbConnect() 
  {
  global $dbcon;

  $dbInfo['server'] = "localhost";
  $dbInfo['database'] = "users";
  $dbInfo['username'] = "root";
  $dbInfo['password'] = "password";

  $con = "mysql:host=" . $dbInfo['server'] . "; dbname=" . $dbInfo['database'];
  $dbcon = new PDO($con, $dbInfo['username'], $dbInfo['password']);
  $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $error = $dbcon->errorInfo();

  if($error[0] != "") 
    {
    print "<p>DATABASE CONNECTION ERROR:</p>";
    print_r($error);
    }
  }

to connect to the database whenever I do a query of some sort. 每当我进行某种查询时连接到数据库。 I always use the PDO prepared statements to prevent SQL injection from any user input, and I use htmlspecialchars to escape before outputting. 我总是使用PDO预处理语句来阻止任何用户输入的SQL注入,并且我在输出之前使用htmlspecialchars来转义。 My question is this: How do I protect my username and password for my database? 我的问题是: 如何保护我的数据库的用户名和密码? I don't know if someone can view the source for my PHP files, but if they could, I can only imagine I would be hosed. 我不知道是否有人可以查看我的PHP文件的来源,但如果他们可以,我只能想象我会被软管。 What do I do? 我该怎么办?

You should put your database credentials in a file outside of the document root, so if something messes up and your PHP gets shown to users un-parsed, no-one will be able to see your password. 您应该将数据库凭据放在文档根目录之外的文件中,因此如果某些内容混乱并且您的PHP显示给未解析的用户,则任何人都无法看到您的密码。

Have a look at 看一下 this article on the subject 这篇关于这个主题的文章 this article on the subject : 这篇关于这个主题的文章

The solution is simple. 解决方案很简单。 Place all sensitive data outside of your web server's document root. 将所有敏感数据放在Web服务器的文档根目录之外。 Many experts now advocate placing most, if not all, of your php code outside of your web server's document root. 许多专家现在主张将大部分(如果不是全部)PHP代码放在Web服务器的文档根目录之外。 Since PHP is not limited by the same restrictions are you web server, you can make a directory on the same level as your document root and place all of your sensitive data and code there. 由于PHP不受Web服务器相同的限制,您可以在与文档根目录相同的级别创建目录,并将所有敏感数据和代码放在那里。

Ok, this needs some clarification. 好的,这需要一些澄清。 Some have suggested that you put sensitive data outside the document root. 有人建议您将敏感数据放在文档根目录之外。 This has some merit but is more placebo than anything else in practicality. 这有一些优点,但在实用性方面比其他任何东西都更安慰。

You have to consider potential sources of problems. 你必须考虑潜在的问题来源。

  • Someone with shell access to the machine will compromise your database connection information regardless of where you put it. 无论您在何处放置数据库连接信息,具有shell访问权限的人都将损害您的数据库连接信息。 This can include both authorized users and those who exploit a vulnerability to get shell access (this has happened a lot); 这可以包括授权用户和利用漏洞获取shell访问权限的用户(这已经发生了很多);

  • If PHP is disabled or the Web server is fooled into thinking it is then there is the possibility that PHP files are served in raw form. 如果PHP被禁用或Web服务器被认为是那么PHP文件可能以原始形式提供。 Putting them outside the document root will protect you against this; 将它们放在文档根目录之外将保护您免受此攻击;

  • If someone somehow manages to write a PHP script to the document root, it's basically the same as someone having shell access so no measure will protect you. 如果有人以某种方式设法将PHP脚本编写到文档根目录,那么它与拥有shell访问权限的人基本相同,因此没有任何措施可以保护您。

Practically, if your Web server is compromised it is, the cases where your config files are outside the document root will protect you are fairly unlikely. 实际上,如果您的Web服务器受到损害,那么您的配置文件位于文档根目录之外的情况将保护您的可能性很小。

The main point of security with databases is to ensure that someone from the internet can't connect directly. 数据库的安全要点是确保来自互联网的人无法直接连接。 This can be done with a firewall, binding the database to a private IP address or putting the database on a private server. 这可以通过防火墙完成,将数据库绑定到专用IP地址或将数据库放在专用服务器上。

There are precautions you can take. 您可以采取预防措施。 Create a mySQL user that is specific to what your application needs to be able to do. 创建一个特定于您的应用程序需要能够执行的mySQL用户。 This can limit the amount of damage an attacker can do if he's compromised your username and password. 这可以限制攻击者在用户名和密码泄露时可以造成的破坏程度。 For instance, allow the user to insert, update, select etc, but NOT drop, etc. Further, as cletus mentioned, the database should not be accessible to the outside. 例如,允许用户插入,更新,选择等,但不要丢弃等。此外,正如提到的那样,外部不应该访问数据库。 On shared hosting environment, this usually means the db can only be connected to from your www server or localhost. 在共享主机环境中,这通常意味着只能从您的www服务器或localhost连接数据库。

Re: kalpaitch, don't pass your password around in some reversible hash. Re:kalpaitch,不要在一些可逆的哈希中传递你的密码。 People should never see your source. 人们永远不应该看到你的来源。

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

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