Click here to Skip to main content
Click here to Skip to main content
Go to top

PHP Obfuscator

, 16 Aug 2006
Rate this:
Please Sign up or sign in to vote.
A basic PHP obfuscator written in C# for .NET 2.0.

Main Obfuscator Screen

Introduction

One of my recent projects at Raizlabs required the distribution of PHP software to customers as a trial demo. We wanted to allow customers to demo the software before they committed to a purchase. To limit piracy, we wanted to protect the PHP code itself.

We looked into various solutions for PHP source code protection, among them other open source encoders and obfuscators, as well as some closed source byte-code compilers. Our issue with the byte-code compilers was that in every case, they either required additional run-time loadable modules be shipped with our application, or they required that server extensions such as Zend be installed. Given that we could not guarantee the functional state of the server on which our scripts will be installed, we decided against any solution involving byte-code compilation.

The popular open source obfuscators, without fail, broke our source code, or required us to change our source code to suit the obfuscator. It was quicker to write our own obfuscator in C# than to change all of our PHP code to conform to the arbitrary code guidelines imposed by the obfuscators. Our own obfuscator though, of course, imposes our own guidelines on the code it is capable of obfuscating, due to our own coding style and internal PHP coding practices. The major rule, if using this obfuscator to encode your PHP scripts, is that it does not understand PHP variables declared in the body of the HTML. This means that if you name your input tag foo, you cannot use the variable $foo in your PHP code, unless it is explicitly excluded by the user of the PHP Obfuscator application. The way around this, of course, is to use the $_REQUEST, $_POST, or $_GET arrays for all HTML input variables, as $_REQUEST[‘foo’] will still be valid, even after the scripts are obfuscated.

Background

This obfuscator was written mainly to encode a piece of PHP software called PHP Email Manager. This application required that we be able to exclude various files and variables from the obfuscation process, in order to support user defined configurations in a config.php file. This file will set up variables for use by the rest of the application, but it is important that this file remains readable, and that the variable names expressed in this file remain unchanged throughout the rest of the application.

There are three main parts to the obfuscation application:

  1. The PHP Obfuscator GUI, which allows the user to select source code to be encoded, source code to be excluded, functions and variables to be excluded, and obfuscation preferences. Obfuscation can be executed from the GUI, or an obfuscation project file can be persisted in XML for use by the command line tool.
  2. The PHP Obfuscator command line tool, which allows the user to automate a script into their build process for obfuscation. The one command line argument accepted by the command line tool is the filename of an obfuscation project file, which is created by the GUI.
  3. The Obfuscator class, which is used by both the GUI and the command line tool, to perform the obfuscation on the target PHP code.

Using the code

Use of the Obfuscator class is a simple process of instantiating an instance of the Obfuscator class with an ObfuscatorUI object as a parameter, and calling Start. The ObfuscatorUI is an interface implementation of IObfuscatorUI that provides the obfuscator with the following functions: StatusUpdate(String), Done(), and Error(string). Through these three functions, the Obfuscator class can communicate back to whatever component called it as it proceeds to encode your source code.

The first parameter to the Start function on the obfuscator is an ObfuscatorConfig object. All this object does is the instantiation of the class that was persisted by the GUI when the user selected to save a project file. A PHP obfuscator project file is a persisted ObfuscatorConfig object, using built-in .NET XML serialization.

ObfuscatorUI ui = new ObfuscatorUI();
Obfuscator obfuscator = new Obfuscator(ui);
obfuscator.Start(config, false);

In the above example, the ObfuscatorUI UI object is a simple class, defined in Program.cs, which just outputs the status returned by the obfuscator to the console window.

/// <summary>
/// UI implimentation for the Obfuscation class.
/// Through this class we will recieve and handle events
/// </summary>
class ObfuscatorUI : Obfuscation.IObfuscatorUI
{
    #region IObfuscatorUI Members

    public void StatusUpdate(string status)
    {
        Console.WriteLine(status);
    }

    public void Done()
    {
        Console.WriteLine("Done.");
    }

    public void Error(string errorText)
    {
        Console.WriteLine("Error: " + errorText);
    }
    #endregion
}

In the case of the main PHP obfuscator UI, the implementation of the IObfuscatorUI interface is actually done on the main Form class of the application.

Just a note: the second parameter to the Start function on the obfuscator is the asynchronous operation flag. This will determine whether all the obfuscation of your source code occurs synchronously (as was the case in the command line tool) or synchronously (as is the case in the GUI). This allows for any GUI that uses this class to remain responsive as encoding is taking place.

All regular expressions to be used during obfuscation for detection of variable names, function calls, function declarations, class declarations, and strings are stored in the Settings class of the obfuscation DLL.

How the encoding works

The Obfuscator class has three functional components:

  1. Encode variable names
  2. Encode function names
  3. Remove whitespace

Through the application of these three functions, the source code is rendered somewhat unreadable, but still fully functional.

When the obfuscation process is started, if the target directory already exists, the user is prompted so it can be removed. Its removal is essential because this is the directory into which every file from the source directory will be copied and modified. The target directory will become an exact replica of the source directory hierarchy, aside from the encoding that is performed on all the files.

After the target directory is created, a recursive copy from the source directory to the target directory takes place. All files are copied, regardless of whether they were selected to be encoded. The copying of every file takes place so that the end result in the target directory is a complete solution, not just the encoded files.

Once all the un-encoded files exist in the target directory, the obfuscation begins. Each file that was selected is opened, and PHP code blocks are extracted. No HTML parts of the PHP file should be processed. From every block of code, comments are removed, then variables are renamed (an MD5 is created from their original name), then whitespace is removed. Function and class declarations within this code block are then detected using a regular expression, and they are added to a list for renaming in the second pass of all the selected files, assuming they do not match a list of function names that are built into PHP (see the phpFunctions class).

After the above process has been applied to all the blocks in a file, the file is re-written with the replacement PHP code blocks, and we proceed to the next file. After all the files have been processed, a second pass of the files is made, renaming all of the detected function calls and class instantiations that we detected names for in the first pass.

Points of Interest

There is no one -right way- to encode PHP files for distribution; everyone has their own preferences and their own technique. We developed what works best for us for a very specific situation; others will have different techniques that are better suited for their unique situations. This is just the one that suited us, and I hope someone else can find it useful, and maybe add to its functionality.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Craig Spitzkoff
Web Developer
United States United States
Craig currently works as the VP of Development at Raizlabs Corporation, a User Interface and Design consulting firm based in Brookline, MA. Previous to this Craig spent time at various companies in the defense industry such as Lockheed Martin, BAE Systems and Tybrin, where he worked on PC based Mission Planning software.
 
As a side project, Craig founded JobVent.com, a site where people can post reviews about their jobs and employers.
 
Craig graduated from Tufts University in 2000 with a B.S. in Computer Science and a minor in Multimedia Arts. Craig completed his M.B.A at Northeastern University in 2003.

Comments and Discussions

 
QuestionObfuscated php Source code is not working in Linux Environment. PinmemberMember 100360999-May-13 3:46 
QuestionNice Article PinmemberThetPaing23-Oct-12 20:31 
QuestionCannot use with PHP Code Igniter framework? Pinmemberoattie18-Oct-11 21:41 
GeneralMy vote of 4 PinmemberAbinash Bishoyi4-Oct-11 20:58 
GeneralRegarding the .Net Obfuscator Pinmemberpratik v patel11-Apr-07 3:05 
GeneralRe: Regarding the .Net Obfuscator PinmemberCraig Spitzkoff14-Apr-07 8:49 
GeneralProblem with heredoc syntax Pinmember^CHAMPi^30-Nov-06 14:32 
GeneralFails to encode member variables of classes [modified] Pinmemberpiyushjethwa23-Aug-06 22:12 
GeneralProblem with read-only files PinmemberBotosu15-Aug-06 23:09 
GeneralRe: Problem with read-only files PinmemberCraig Spitzkoff16-Aug-06 3:23 
QuestionGreat! Commercial Projects? PinmemberPhil. J. T.10-Aug-06 13:59 
AnswerRe: Great! Commercial Projects? PinmemberCraig Spitzkoff10-Aug-06 14:29 
Phil... thanks for the feedback. If people want to release their own commercial PHP projects encoded with this thing, thats great. I dont want there to be any restrictions.
 
As far as changing the code, I've created a Google Code project where you can modify the files for this project... I really everyone to benefit from any changes people might add.
 
The files can be modified here:
http://code.google.com/p/phpobfuscator/[^]
You'll need a subversion client installed to check-in/check out files, but this way everyone can get access to the features you add, and you get added as a contributor.
 
Would you like me to add you as a member of that project on google?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 16 Aug 2006
Article Copyright 2006 by Craig Spitzkoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid