Avicinnian
Avicinnian

Reputation: 1830

Using eval() and base64_decode() to obfuscate code - not working within a function?

In short, I'm building a self hosted application, and to create a basic level deterrent that'll stop those who have a small knowledge of development (i.e. my target market) from removing call backs, I've decided to use eval() and base64_decode() in order to obfuscate and execute a couple of lines of code - specifically those that deal with validating the users license key.

The problem I've run into however is that it seems that I can't run eval(base64_decode(..)); within a function.

For example, this works fine:

eval(base64_decode('c2Vzc2lvbl9uYW1lKCJfaW5zdCIpOyBzZXNzaW9uX3N0YXJ0KCk7ICRfU0VTU0lPTlsna2V5J10gPSB0cnVlOyBlY2hvICI8c2NyaXB0IHR5cGU9XCJ0ZXh0L2phdmFzY3JpcHRcIj53aW5kb3cubG9jYXRpb24gPSAnL2luc3QvYWRtaW4vc2V0dGluZ3MnPC9zY3JpcHQ+Ijs=');

executing the following,

session_name("_inst");
session_start();
$_SESSION['key'] = true;
echo "<script type=\"text/javascript\">window.location = '/inst/admin/settings'</script>";

But this on the other hand, fails:

function escapeOut() {
eval(base64_decode('c2Vzc2lvbl9uYW1lKCJfaW5zdCIpOyBzZXNzaW9uX3N0YXJ0KCk7ICRfU0VTU0lPTlsna2V5J10gPSB0cnVlOyAkZXNjYXBlID0gICI8c2NyaXB0IHR5cGU9XCJ0ZXh0L2phdmFzY3JpcHRcIj53aW5kb3cubG9jYXRpb24gPSAnL2luc3QvYWRtaW4vc2V0dGluZ3MnPC9zY3JpcHQ+IjsgcmV0dXJuICRlc2NhcGU7'));
}

echo escapeOut();

it should execute the following,

session_name("_inst");
session_start();
$_SESSION['key'] = true;
$escape = "<script type=\"text/javascript\">window.location = '/inst/admin/settings'</script>";
return $escape;

At first I wasn't returning $escape, but after realizing and rectifying that issue, I'm stumped. It's probably something pretty simple, but I'm pretty stumped.

Any answers as to why this doesn't work/what I can do to make it work would be greatly appreciated!

Upvotes: 0

Views: 4232

Answers (1)

Code Magician
Code Magician

Reputation: 24022

having return in your eval() statement will return from eval, not from the outer function.

i think you need something like this:

function escapeOut(){
    return eval(base64_decode('c2Vzc2lvbl9uYW1lKCJfaW5zdCIpOyBzZXNzaW9uX3N0YXJ0KCk7ICRfU0VTU0lPTlsna2V5J10gPSB0cnVlOyAkZXNjYXBlID0gICI8c2NyaXB0IHR5cGU9XCJ0ZXh0L2phdmFzY3JpcHRcIj53aW5kb3cubG9jYXRpb24gPSAnL2luc3QvYWRtaW4vc2V0dGluZ3MnPC9zY3JpcHQ+IjsgcmV0dXJuICRlc2NhcGU7'));
}

echo escapeOut();

also, keep in mind it's trivial to echo base64_decode('c2Vzc2lvbl9uYW1lKCJfaW5zdCIp...

Upvotes: 2

Related Questions