I have a PHP script that's stored and run on my local computer only -- it's used to access a remote server.
Is there a way to securely store a password used by the script? I don't think PHP can access Keychain, or a secure disk image, so I'm wondering if there's another way?
Thanks.
Asked
Active
Viewed 342 times
2

Hope4You
- 1,927
- 4
- 21
- 45
-
3What's wrong with sticking the password directly in the script? – sarnold May 26 '12 at 00:19
-
It's an important password I want encrypted when not in use. – Hope4You May 26 '12 at 00:20
-
@Hope4You: You'll have to decrypt it sometime anyway, PHP will always access the string. – Alix Axel May 26 '12 at 01:15
-
What sort of connection are you initiating? Could it be done with authorized keys rather than a password? If so, perhaps you could do away with passwords altogether and store the private key in an encrypted disk image that you would only mount when you want to run the script. – steveax May 26 '12 at 02:22
2 Answers
3
If the machine or the PHP code is compromised, an attacker will have access to the PHP source and will be free to recreate whatever complicated steps you're willing to take. Just save your password in a folder outside of the document root and make sure your script isn't vulnerable to code injection.

Alix Axel
- 151,645
- 95
- 393
- 500
-
Actually, I just came up with a secure idea - store the actual PHP script, as well as the password, in an encrypted disk image! – Hope4You May 26 '12 at 23:02
-
@Hope4You: Well, the PHP interpreter still has to access the raw (unencrypted) PHP code, so, I assume if the machine is compromised, so is the encrypted disk. You can keep adding more secure layers but the bottom line is that eventually PHP (and the eventual attacker) will have access to the raw password. – Alix Axel May 27 '12 at 08:05
1
In order to do the encryption/decryption, you can use the answer supplied here:
Best way to use PHP to encrypt and decrypt passwords?
Note: If you use this method, someone on your local machine could still use your own script to decrypt the password but it does provide some extra security.

Community
- 1
- 1

christurnerio
- 1,469
- 11
- 13
-
I disagree, this is just as safe as storing the password in a variable as plain-text. If that's compromised, so is the key to decrypt the password and the encrypted password. – Alix Axel May 26 '12 at 01:16
-
@AlixAxel: I pointed that out in the note and added that this method provides "some extra security". It is easy for someone to open up a text file and read a plain text password. It is slightly harder for someone to modify some php code and use the key to decrypt the encrypted password. Isn't leaving your extra house key under the mat better than in the door? Secure, no. More secure, yes. – christurnerio May 26 '12 at 01:22
-
-
Fair enough. :) I just noticed you provided the answer I linked to. Props for the great answer. – christurnerio May 26 '12 at 01:26