Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PHP file to binary

Tags:

php

binary

Is it possible to convert a PHP file to binary and deploy it on a webserver?

like image 903
Hulk Avatar asked Dec 04 '09 06:12

Hulk


People also ask

How to convert image into binary format in PHP?

How to convert to a binary image in PHP. In PHP, the base64_encode() method converts the original image to a binary image. It needs the base URL of an image to perform the task. base64_encode($image);

Is PHP used for binary content?

PHP has functions to convert a binary stream of data into individual values, but for whole streams in a specific format, parsing data may be a complicated task. This class simplifies that process by the means of functions that read files and extract values of some expected types.

Can I compile PHP code?

The short answer is "no". The current implementation of PHP is that of an interpreted language.


5 Answers

Since PHP is a (relatively)portable language, compiling PHP source to bytecode instead of binary code is more preferable. For this purpose there is an experimental project called bcompiler.

like image 187
Emre Yazici Avatar answered Oct 05 '22 00:10

Emre Yazici


Some months ago I searched for it (php code protection or script protection) and my results were:

I provide some description but try to google them and pardon me.

Be careful with your search because you may encounter to php compressors instead and compressors are just about GZIP and other http transfer compression mechanisms.

The most important issues with php script protecting tools are:

  1. How much speed of php runtime performance will get low(down)(how much speed down)

  2. Will we need an extra environment or installed tools on the php server?

  3. Easy handling

You will find about whole description and comparison on: http://www.seocompany.ca/software/free-encryption-software.html

Obfuscators

A lot of obfuscators you will find on the web but there's no guarantee for your codes to work.

Even I found a mixed way with Visual C++ and MSVS for obfuscating php and because also I'm experienced with asp.net I tested it however didn't work.

Converters and Lockers

  • ion protector (http://www.ioncube.com/): Oh not free but I heard many about his famous name

  • phc (php compiler): free and was a good case but "Brendan Long" said the truth because phc has a painful install way. However result is magic and has famous name such as ion

  • php locker: I got it and test it. It had error with compiled code and it's released for lower than php5. Absolutely not free and for who thinking about ccr-ac*ks finding it for php locker is impossible.

  • Zend Guard: A perfect way to guard but it's harder than phc or ion because you need Zend runtime environment (ZRE) on your server and Zend Guard is absolutely non-free however if you are a Zend guy (zend framework+zend IDE+zend Guard) you should know Zend Guard is compatible with zend IDE and code blocking process will be so easy. I'm not a Zend guy because either of zend framework and zend guard get down runtime speed obviously. I love php speeded runtime.

  • php -> C/C++ compiling (YES IT'S POSSIBLE) (BEST PERFORMANCE THROUGH THE WORLD): It's not so famous so it's normal if no one talked about.

  • facebook hiphop: This tool is using by facebook to compile php files on c++/c for getting a better performance however finally we have unreadable compiled codes. I don't remeber but I think it needs a good familiarity to linux or about php recompile mechanism on Microsoft Visual C++. (However DON'T TRUST to my weak memory and google it) (AND FOR LEARNING ABOUT PHP RECOMPILE WAY PLZ REFER TO latest release of php_manual.chm)

Finally I made my self simple php obfuscator using regular expressions which worked on php 5.3 and my complex scripts so I moved on OpenSSL and different encryption mechanisms and got a review on php secure development.

Wishing you be successful.

like image 41
Abbas Ali Hashemian Avatar answered Oct 04 '22 22:10

Abbas Ali Hashemian


This is way overdue but should do what you want.

Make your own custom extension with the zend engine as they compile to a c++ dll. Add that dll to your ext location in the php directory. After that you can call your own php scripts as php functions. Keep in mind that php is server side only so no one can get your scripts unless they are copied from the server and handed out.

Download the php source library for the version that you have. [windows.php.net] The zend stuff you need will be included.

Step 1. Create a new project and select "General" under Visual C++ and the empty project option. Give it a name and Click ok. Mine is Project1 so I will be using that.

Step 2. Right click the solution name and select properties. Select Dynamic Library (.dll) Under Project Defaults/Configuration Type in the Configuration Properties/General item. Be sure that you are changing the Debug config as Release is not used.

Step 3. Under VC++ Directiories add the following under the Include libraries: C:\php-7.1.8-src\Zend C:\php-7.1.8-src\win32 C:\php-7.1.8-src\TSRM C:\php-7.1.8-src\main C:\php-7.1.8-src Libraries: C:\php-7.1.8\dev

Step 4. Under C/C++ Preprocessor add ZEND_DEBUG=0;ZTS=1;ZEND_WIN32;PHP_WIN32 to Preprocessor Definitions

Step 5. Under Linker Input add php7ts.lib. This needs to match the lib for your version of php

Click ok to save the Property settings for your solution.

Now lets add a code file for your function (Project1)

Under the source files folder right click and add Code. I called mine Project1.cpp and here is the source. You'll need to rename config.w32.h.in to config.w32.h and copy it to the directories where it is needed. Intellisense should tell you where it's looking and config.w32.h.in is in the C:\php-7.1.8-src\win32 directory.

// this needs to match the compiler version that php was compiled with
#define _CRT_SECURE_NO_WARNINGS
#define PHP_COMPILER_ID "VC14"

#pragma once
/* You must include config.w32.h first */
#include "win32/config.w32.h"

#include "php.h"

ZEND_FUNCTION(ReturnString);

zend_function_entry Project1_functions[] =
{
    ZEND_FE(ReturnString, NULL)
    {
        NULL, NULL, NULL
    }
};

zend_module_entry Project1_module_entry =
{
    STANDARD_MODULE_HEADER,
    "Project1 Module",
    Project1_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(Project1)

ZEND_FUNCTION(ReturnString)
{
    zval* value;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE)
    {
        RETURN_FALSE;
    }

    convert_to_string(value);

    RETURN_STRING(Z_STRVAL_P(value), true);
}

Press F7 to compile and it should create Project1\Debug\Project1.dll. Copy Project1 to your ext directory C:\php-7.1.8\ext and add an extension=Project1.dll to your php.ini and you should be good to go. If it doesn't work double check your properties to make sure you didn't miss something.

The script is quite simple:

<?php
echo ReturnString("The Returning String");
?>
like image 38
developer68 Avatar answered Oct 05 '22 00:10

developer68


If the web server is Linux based, you can create a package like .deb or .rpm (depending on the Linux distribution) and easily distribute/deploy it.

BR, Dawid.

like image 43
Czlowiekwidmo Avatar answered Oct 04 '22 22:10

Czlowiekwidmo


It depends on what you mean by "binary".

If you want to compile (or just obfuscate) your PHP code to keep someone else from modifying then use a bytecode compiler. One example are the tools from Zend, among others. (I prefer Zend's tools because they the primary company behind PHP and fully QA all their tools against all the versions of PHP).

If you want to compile your PHP code and link the PHP runtime to it and then deploy it (like C\C++), then no. Maybe in theory that would be possible but it would be a mess. (Not practical or feasible and don't think anyone has put anything together to try and the output would also be tied to a particular architecture).

like image 20
Zac Bowling Avatar answered Oct 04 '22 23:10

Zac Bowling