There are many ways to encode and decode PHP code. From the perspective of site security, there are three PHP functions —
str_rot13()
, base64_encode()
, and gzinflate
— that are frequently used to obfuscate malicious strings of PHP code. For those involved in the securing of websites, understanding how these functions are used to encode and decode encrypted chunks of PHP data is critical to accurate monitoring and expedient attack recovery.For those already familiar with the concept, but could use a quick reference with examples, I put together a concise summary of this article. The article here is intended as a “behind-the-scenes” look at the decoding and encoding happening on the examples page. Here’s the quick-jump menu:
Encoding and decoding with str_rot13()
As explained in the PHP documentation,str_rot13()
is a simple function used for rotating everyletter “13 places in the alphabet” while ignoring non-alphanumeric characters. This type of encoding is called ROT13 encoding and it’s very straightforward using the str_rot13()
function. Let’s look at an example..Let’s say we want to ROT13-encode the following string:
<?php $string = 'Encoding and Decoding Encrypted PHP Code'; ?>
We run this string through
str_rot13()
and set it as a variable named $encoded
like so:<?php $encoded = str_rot13($string); ?>
Echoing the
$encoded
variable to the browser, we get this string of gibberish:Rapbqvat naq Qrpbqvat Rapelcgrq CUC Pbqr
To decode a string encoded with
str_rot13()
, we simply run it back through the function to restore the original string. Here is an example that returns the original string to a variable named$decoded
:$decoded = str_rot13(str_rot13($string))
Echoing
$decoded
, we see the original string as expected:Encoding and Decoding Encrypted PHP Code
Copy/paste example (try it for yourself):
<?php // str_rot13() example
$string = 'Encoding and Decoding Encrypted PHP Code';
$encoded = str_rot13($string);
$decoded = str_rot13(str_rot13($string));
echo $encoded ."\n";
echo $decoded;
?>
Encode and decode with base64_encode() & base64_decode()
Also explained in the PHP documentation, thebase64_encode()
function “encodes the given data with base64.” Rather than get into the mechanics of encoding with base64, I’ll repeat the enigmatic haiku given in the docs:Ahh, I love taking stuff out of context, but I digress.. Let’s get back on track with a quick example showing how
base64_encode()
works its magic. Let’s say we want to encode the following string with base64:<?php $string = 'Encoding and Decoding Encrypted PHP Code'; ?>
We run this string through
base64_encode()
and set it as a variable named $encoded
like so:<?php $encoded = base64_encode($string); ?>
Echoing the
$encoded
variable to the browser, we get this string of gibberish:RW5jb2RpbmcgYW5kIERlY29kaW5nIEVuY3J5cHRlZCBQSFAgQ29kZQ==
As you may count, the base64-encoded string contains around 33% more data than the original. Now to decode a string encoded with
base64_encode
, we use the converse function,base64_decode. Here is an example that returns the original string to a variable named $decoded
:<?php $decoded = base64_decode(base64_encode($string)); ?>
Echoing
$decoded
, we see the original string as expected:Encoding and Decoding Encrypted PHP Code
Copy/paste example (try for yourself):
<?php // base64_encode()/base64_decode() example
$string = 'Encoding and Decoding Encrypted PHP Code';
$encoded = base64_decode($string);
$decoded = base64_decode(base64_encode($string));
echo $encoded ."\n";
echo $decoded;
?>
Deflate and inflate with gzdeflate() & gzinflate()
A third visit to the PHP docs gives us our third function,gzdeflate()
is used to “compress the given string using the DEFLATE data format.” Again, not gonna veer off — let’s stay focused with a quick example.Let’s say we want to “gzdeflate” the following string:
<?php $string = 'Encoding and Decoding Encrypted PHP Code'; ?>
We run this string through
gzdeflate()
and set it as a variable named $compressed
:<?php $compressed = gzdeflate($string); ?>
Echoing the
$compressed
variable to the browser, we get this bizarre-looking gibberish:s�K�O��KWH�KQpI�r\���* JRS < ��SR
To “decode” this alien-speak, we inflate it with the converse function, gzinflate(), to restore the original string. Here is an example that returns the original string to a variable named
$uncompressed
:$uncompressed = gzinflate(gzdeflate($string));
Echoing
$uncompressed
, we see the original string as expected:Encoding and Decoding Encrypted PHP Code
Copy/paste example:
<?php // gzinflate()/gzdeflate() example
$string = 'Encoding and Decoding Encrypted PHP Code';
$compressed = gzdeflate($string);
$uncompressed = gzinflate($compressed);
echo $compressed ."\n";
echo $uncompressed;
?>
Parameters
Also worth mentioning: whereas the first two functions —str_rot13()
and base64_encode()
— accept only one parameter (the input $string
), the inflate/deflate functions accept twoparameters. Perhaps surprisingly, these second parameters are different between the two functions:Returning to our example, let’s employ these second parameters to help visualize:
<?php // example using second parameters
$string = 'Encoding and Decoding Encrypted PHP Code';
$compressed = gzdeflate($string, 9); // compression level set to 9 = maximum
$uncompressed = gzinflate($compressed, strlen($string)); // length set to same as $string
echo $compressed ."\n";
echo $uncompressed;
?>
And the result as displayed in a browser:s�K�O��KWH�KQpI�r\���* JRS < ��SR
Encoding and Decoding Encrypted PHP Code
Combined example: gzinflate(str_rot13(base64_decode()))
Malicious scripts often combine multiple encoding methods to further obfuscate data strings. Using the numerous PHP encoding-type functions (and their various parameters), it’s possible to scramble data with many layers of obfuscation. For example, on common technique for encrypting malicious scripts combines all three of the functions described in this article. The structure of such technique looks like this:$gibberish = eval(gzinflate(str_rot13(base64_decode($string))));
The concept is straightforward, however decoding the
$gibberish
can be tricky. The easiest way to decode such a string is to use an online decoding tool. And that’s my slick segue into the next section..
Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
ReplyDeleteRegards,
PHP Training in Chennai
Great Post, Actually PHP is a beautiful source for developing a database driven web application, I love this post, thanks for spending your time for discussing about this topic.
ReplyDeletePHP Course in Chennai
Well post, Thanks for sharing this to our vision. In recent day’s customer relationship play vital role to get good platform in business industry, Sales force crm tool helps you to maintain your customer relationship enhancement.
ReplyDeleteRegards,
Salesforce training in Chennai|Salesforce training institute in Chennai|Salesforce courses in Chennai
Nice Article! Mostly I have gathered knowledge from the blogger, because its provides more information over the books & here I can get more experienced skills from the professional, thanks for taking your to discussing this topic.
ReplyDeleteRegards,
cognos Training in Chennai|cognos tm1 Training in Chennai|cognos Certification
Hi Dude,
ReplyDeleteAwesome Post!!! With unique content, I really get reading interest when I am following your article, I hope I ll help many of them who looking this pretty information.
Regards,
Python Training in Chennai|Python Training Institutes in Chennai|python training chennai|FITA Academy reviews
Well defined. And good article..
ReplyDeleteTo learn SAS business analytics, check here
SAS Training in Chennai| SAS Course in Chennai
This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
ReplyDeleteOracle training in pune
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDevops Training in Chennai
Devops Training in Bangalore
AWS Training in chennai
AWS Training in bangalore
Outstanding blog post, I have marked your site so ideally I’ll see much more on this subject in the foreseeable future.
ReplyDeletepython training in Bangalore
python training in pune
python online training
I am really impressed with your efforts and really pleased to visit this post.
ReplyDeleteData Science training in kalyan nagar | Data Science training in OMR
Data Science training in chennai | Data science training in velachery
Data science online training | Data science training in jaya nagar
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleteData Science course in kalyan nagar | Data Science course in OMR
Data Science course in chennai | Data science course in velachery
Data science online course | Data science course in jaya nagar
Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
ReplyDeleteangularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
angularjs interview questions and answers
This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
ReplyDeletePython training in bangalore | Python course in pune | Python training in bangalore
I always enjoy reading quality articles by an individual who is obviously knowledgeable on their chosen subject. Ill be watching this post with much interest. Keep up the great work, I will be back
ReplyDeleteJava training in Bangalore | Java training in Btm layout
Java training in Bangalore | Java training in Jaya nagar
Java training in Bangalore | Java training in Electronic city
Java training in Chennai | Java training institute in Chennai | Java course in Chennai
I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
ReplyDeleteData Science training in kalyan nagar
Data Science training in OMR | Data science training in chennai
Data Science training in chennai | Best Data science Training in Chennai
Data science training in velachery | Data Science Training in Chennai
Data science training in tambaram | Data Science training in Chennai
Data science training in jaya nagar | Data science Training in Bangalore
Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
ReplyDeleteangularjs Training in bangalore
angularjs Training in bangalore
angularjs online Training
angularjs Training in marathahalli
angularjs interview questions and answers
Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs
ReplyDeleteindustrial course in chennai
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteDotnet Training in Chennai |Best Dotnet Training course in Chennai
Android Training in Chennai |Best Android Training course in Chennai
CCNA Training in Chennai | Best CCNA Training course in Chennai
MCSE Training in Chennai |Best MCSE Training course in Chennai
This comment has been removed by the author.
ReplyDelete
ReplyDeleteclick here for info more info.
Well said its very useful for us thank you
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune