当PHP应用程序建立数据库连接时,当然通常需要传递登录名和密码。如果我对我的应用程序使用一个最小权限的登录,那么PHP需要知道这个登录名和密码。保护密码的最好方法是什么?只在PHP代码中编写它似乎不是一个好主意。
当前回答
一个额外的技巧是使用一个PHP单独的配置文件,如下所示:
<?php exit() ?>
[...]
Plain text data including password
这并不妨碍您正确地设置访问规则。但如果你的网站被黑客攻击,“要求”或“包括”将在第一行退出脚本,因此更难获得数据。
然而,不要让配置文件在可以通过web访问的目录中。你应该有一个“Web”文件夹,包含你的控制器代码,css,图片和js。这是所有。其他的都放在脱机文件夹中。
其他回答
是否可以在存储凭据的同一文件中创建数据库连接。将凭据内联到connect语句中。
mysql_connect("localhost", "me", "mypass");
否则最好在connect语句之后取消设置凭据,因为不在内存中的凭据不能从内存中读取;)
include("/outside-webroot/db_settings.php");
mysql_connect("localhost", $db_user, $db_pass);
unset ($db_user, $db_pass);
我们是这样解决的:
Use memcache on server, with open connection from other password server. Save to memcache the password (or even all the password.php file encrypted) plus the decrypt key. The web site, calls the memcache key holding the password file passphrase and decrypt in memory all the passwords. The password server send a new encrypted password file every 5 minutes. If you using encrypted password.php on your project, you put an audit, that check if this file was touched externally - or viewed. When this happens, you automatically can clean the memory, as well as close the server for access.
对于非常安全的系统,我们在配置文件中加密数据库密码(该文件本身由系统管理员保护)。在应用程序/服务器启动时,应用程序会提示系统管理员输入解密密钥。然后从配置文件中读取数据库密码,解密并存储在内存中以供将来使用。仍然不是100%安全,因为它存储在内存中解密,但你必须在某些时候称它“足够安全”!
如果你在别人的服务器上托管,在你的webroot之外没有访问权限,你可以把你的密码和/或数据库连接放在一个文件中,然后使用.htaccess锁定文件:
<files mypasswdfile>
order allow,deny
deny from all
</files>
之前我们将DB user/pass存储在一个配置文件中,但后来进入了偏执模式——采用了深度防御策略。
如果您的应用程序被泄露,用户将拥有对您的配置文件的读访问权,因此黑客就有可能读取这些信息。配置文件也可能在版本控制中被捕获,或者在服务器中被复制。
我们已经切换到在Apache VirtualHost中设置的环境变量中存储user/pass。这个配置只能由根用户读取——希望您的Apache用户不是以根用户身份运行。
这样做的缺点是,现在密码在一个全局PHP变量中。
为了降低这种风险,我们有以下预防措施:
The password is encrypted. We extend the PDO class to include logic for decrypting the password. If someone reads the code where we establish a connection, it won't be obvious that the connection is being established with an encrypted password and not the password itself. The encrypted password is moved from the global variables into a private variable The application does this immediately to reduce the window that the value is available in the global space. phpinfo() is disabled. PHPInfo is an easy target to get an overview of everything, including environment variables.
推荐文章
- 将值从同一表中的一列复制到另一列
- 如何将XML转换成PHP数组?
- 什么是数据库池?
- 如何将对象转换为数组?
- 关于数据库,每个开发人员应该知道些什么?
- 从IP地址获取位置
- 获取数组值的键名
- "where 1=1"语句
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 是使用各有一个模式的多个数据库更好,还是使用一个数据库有多个模式更好?
- 如何从Oracle的表中获取列名?