this is not about how to encrypt and decrypt. I want to know if it is possible to run a function after retrieving and decrypting it from a text file.
For example I will create a function that defines database password and then encrypt it as if it was just a text string and write it to a text file.
Then I have the function that defines the db password as an encrypted un-readable mess.
When I need the password, I would get the file contents and decrypt it but at this point I need to be able to run it as a function so it will define the DB_PASSWORD constant but it is just text in a string now.
It's the same as if I just did this:
$var = "function define_password(){
if(!defined('DB_PASSWORD')){define('DB_PASSWORD', 'password');}
}";
It's pretty obvious all I can do with this is echo it out because it's just text in a variable.
So, is there any way to make php see that as a function instead of a string variable?
Thanks
Even if it's redundant, it's educational so I'm posting the result of using the eval() method Xenon pointed out. Here's the encrypted and hidden file called .nothing in a directory outside www with read only access to www-data owned by root:
Pswo0DlrEPMNEs7ExyFs8Zh2n+bSj6yr4NI9zV7gTP9qFiesrFKLxsjoo3R3CnsYJNkojeo0v2gQtI4iJGLzKUS8zdhePLElk3BwhliB3dxYwvRkJFMqbtw7k/UBo0pHPLR/jVRnWq+cTByr0xp8p7X9v8Olbfrz4zwo+VXIDwLA6GsOJTK14Hy0E4jksgeuEQ7/PDtxCgWoMPQ2OomPwjFjukdrAofbF5jxU9zCUK5Cs1UZ8+PYA79w0lccEpUA9vWBPZ6Xuwhr4KuGeyoUCchK9wGgaXDD6Oc845OsmnR/wi+EMnYacvGGeLxN96wEAt9vh/dwmYIkHmpuBtPWUP1vRT/HzTv39HfXoFFKx1kGww4Xph/cjS8joYwAgh+C+LT61sBjlfazkDRNabpmZFd2yyocD+6lQeHrmKuvYxa0cfMSYa8ScAQaBz6Ycg99ldOinEbd+mTnZKltFialAoHOvha4Surc0XZX7vtFx1TxSMctjkgCmLw4bHhJ6B3htLkhOysb6Oz5M1rniogFwEZnFaqLsqD1etpYv1RpceB8FPSss7/Zu0vGwbGeSMldr06FeHylRlB6n25QiH3qaKieHuD8ErKvQjm3YAVCshU04ydR0lTU5ckKnFGGxAGtiXjK9rV3Hle69vk7RjtYJVVuPCbmdSETdE/zHem+w1THKw4/NUROdxiOIDYQtQ6oDY0ORiwAqjU9QHlqwFyadPrDG37AYXSCzLgwFCw76J6uGyYzDsIVDUnP6Dv3yV1yvjrpxAFb5lA9APrnU8qIgNrgEoaZAznpX91QQxt6ztgJFVHyOSyUP8DkkFpsPamHwg22jb1oUZlOVsCgzMSj9G1M4sgokW6lpQjXCtuDYXVrAwoWHk6Bh+UiBXVUoOPIvEjG4PtCIRQl84lEtKiUPAQSWA/7rgN1O308j9tRtNBzho2xYMTPg2g5DOS82RRSS7/ehGxEWlbh9cqig6Xux+oyLLXK4uIp8qA4kLqvuVX9w/UAfJRRfV89t5xQsshMP8TnFn3KrAZtGJ7lQDOduzMiHXpu9Wu91IsEfbHr/v2U0CD+sMc7h9K755fYpdNUptpaRvyz0sVZW96sldPsxY26LktnQaKIAAoDNgcIFbNOmGOBnIuUVOQBUVxbC+e2cYJy8xQsjsJBe0AChfTZB+Vi0TiERyA28OCti4T4PTA=
I made it with the encrypt function in the code below which came from someone here on stackoverflow.
<?php
define('SALT', 'whateveryouwant');
function encrypt($text)
{
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function decrypt($text)
{
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
$dec_db = decrypt(file_get_contents('.nothing'));
eval($dec_db);
defineit();
echo ET_APP_USERNAME;
The encrypted part is a function that defines usernames and passwords for 3 different databases but echoing out the first username shows me it works perfectly. And of course as was pointed out, if someone has access to the server they will see the SALT constant and the encrypt/decrypt functions and know from the file_get_contents method where it is so I guess this was just educational for me.