How to secure database passwords in PHP?

后端 未结 16 1209
闹比i
闹比i 2020-11-21 22:41

When a PHP application makes a database connection it of course generally needs to pass a login and password. If I\'m using a single, minimum-permission login for my applica

16条回答
  •  天涯浪人
    2020-11-21 23:37

    Previously we stored DB user/pass in a configuration file, but have since hit paranoid mode -- adopting a policy of Defence in Depth.

    If your application is compromised, the user will have read access to your configuration file and so there is potential for a cracker to read this information. Configuration files can also get caught up in version control, or copied around servers.

    We have switched to storing user/pass in environment variables set in the Apache VirtualHost. This configuration is only readable by root -- hopefully your Apache user is not running as root.

    The con with this is that now the password is in a Global PHP variable.

    To mitigate this risk we have the following precautions:

    • 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.

提交回复
热议问题