Archive

Posts Tagged ‘software’

Hide And Unhide Code With PHP

April 27th, 2009 No comments

If you are selling a system the last thing you want is for people to copy the system and pass it on for free. There are numerous ways to implement parts of the system that will stop this from happening.

By far the easiest is to create a section of code that is hidden, the removal of which will cause the application to fall over. It could even be as simple as a link back to your site so that even if you give you application away for free, you will always have that link present.

This method involves the use of a function called eval(), which takes PHP code as a string and interprets it to produce output. Here is an example that prints a link to Talk In Code.

$code = "echo "<a href='http://www.talkincode.com/' title'Talk In Code'>Talk In Code</a>";";
eval($code);

So lets use some code to hide this from anyone who might be reading our source code. First we pass this string through our hiding function to produce non-human readable text. This function is called obfuscate() and works by taking each character in turn and converting it into the ascii equivalent.

function obfuscate($text) {
    $length = strlen($text);
    $scrambled = '';
    
    for ($i = 0; $i < $length; ++$i) {
        $scrambled .= ord($text[$i]). ' ';
    }
    
    return $scrambled;
}
$code = "echo "<a href='http://www.talkincode.com/' title'Talk In Code'>Talk In Code</a>";";
 
$obf = obfuscate($code);
echo $obf;

This will print out the following:

101 99 104 111 32 34 60 97 32 104 114 101 102 61 39 104 116 116 112 58 47 47 119 119 119 46 116 97 108 107 105 110 99 111 100 101 46 99 111 109 47 39 32 116 105 116 108 101 39 84 97 108 107 32 73 110 32 67 111 100 101 39 62 84 97 108 107 32 73 110 32 67 111 100 101 60 47 97 62 34 59

We can store this as a variable until we next need it. In order to run this code we need to convert it into something that eval() can understand, to do this we use the opposite of the obfuscate(), called unobfuscate(). This function works by taking a set of ascii values and converting them into their character equivalents, note that we also trim the text to remove the last space from the end of the code.

function unobfuscate($scrambled) {
    $text = '';
 
    $bits = explode(' ',$scrambled);
    
    foreach ( $bits as $bit ) {
        $text .= chr($bit);
    }
 
    return trim($text);
}

We can then transform our hidden code into PHP code, which is then passed to the eval() function and run.

$code = '101 99 104 111 32 34 60 97 32 104 114 101 102 61 39 104 116 116 112 58 47 47 119 119 119 46 116 97 108 107 105 110 99 111 100 101 46 99 111 109 47 39 32 116 105 116 108 101 39 84 97 108 107 32 73 110 32 67 111 100 101 39 62 84 97 108 107 32 73 110 32 67 111 100 101 60 47 97 62 34 59';
$code = unobfuscate($code);
eval($code);

This produces the following output.

<a href='http://www.talkincode.com/' title'Talk In Code'>Talk In Code</a>

Beware that doing this sort of thing will probably slow down your application, especially if you try to eval() a large block of code. A single link like this is probably as far as I would personally go as there are much better ways of verifying that a piece of software is properly licensed.

Categories: PHP Strings Tags: , , , , , , ,

Virtualization With VirtualBox

March 25th, 2009 2 comments

Virtualization is basically a term used to describe the creation of a computer in software. The main benefits of which are that if you want to try out an operating system or test client server communications you don’t have to get multiple computers. You can simply create a few computers virtually, which will act just like the real thing.

There are quite a few virtualization products available, some are free and some cost quite a bit of money. After messing about with quite a few different virtualisation products other the past few weeks I have uncovered a great bit of software called VirtualBox from Sun Microsystems.

VirtualBox

After installing VirtualBox it took me only a few minutes to set up a new virtual machine. I decided that I wanted to create a virtual machine with Ubuntu installed on it, so I selected the options that optimise VirtualBox for this system. Note that this doesn’t set up a virtual machine with Ubuntu already installed, you have to go and download it yourself! Here is a screenshot of the main interface, which is very easy to understand.

VirtualBox interface

VirtualBox interface

Using virtual machines in the past I have found that to install anything can take a very long time. Installing Ubuntu took not more than 30 minutes, at which point I was presented with the bootup screen.

VirtualBox Ubuntu Boot

VirtualBox Ubuntu Boot

Here is another shot of the operating system fully loaded.

VirtualBox Ubuntu Loaded

VirtualBox Ubuntu Loaded

VirtualBox also supports the creation of snapshots, so if you have a fresh install of an operating system that you want to mess about with (or break) then you can create a snapshot of the virtual machine before you begin. This way, after you trash the system, you can revert back to a snapshot of the system before you started messing about.

The only trouble I had was figuring out how to interface the virtual machine properly with my network, but after reading the documentation this all became very clear.

Overall VirtualBox is a great bit of software, with a detailed and comprehensive manual. I just can’t believe that this software is free!