How to multilevel base64 decoder and show proper php code - php

How to multilevel base64 decoder and show proper php code....
i.e.....
base64_decode(
base64_decode(
'SkhSb2FYTXRQazh3TUU4d01FOVBUMDh3VDA4Z1BTQm1ZV3h6WlRzZ0NRa2tUekF3VDA4d1R6QXdUMDh3VHlBOUlITjBjblJ2Ykc5M1pYSW9jSEpsWjE5eVpYQnNZV05sS0dKaGMyVTJORjlrWldOdlpHVW9KMGw1T0hWTGFWRnFKeWtzSUNjbkxDQWtYMU5GVWxaRlVsdGlZWE5sTmpSZlpHVmpiMlJsS0NkVk1GWlRWbXRXVTFneFFsTlVNVkpRVVRBNVRTY3BYU2twT3c9PQ=='
)
);
Any buddy have php script to decode multi level base64_decode() and show proper php code.

Note:
Encoded string will be decoded to exact string if the string encoded or decoded equal no. of times.
It means two level encoded string can be decoded 2 level.
<?php
$string="Hello Stack Overflow";
echo $encodedString=base64_encode(base64_encode($string));//Multilevel(2) encode
echo base64_decode(base64_decode($encodedString));//Multilevel(2) decode

I would recommend to create a function similar to this:
function multiBase64Decode($string, int $iteration = 1){
for($i=0;$i<$iteration;$i++){
$string = base64_decode($string);
}
return $string;
}

Related

Decode string with \x in PHP that has char encoded over 255

My problem is about string decoding.
Let's assume a string like:
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
I want to decode it and to look like that:
λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
I tried to use
utf8_encode(utf8_encode($str));
But it's not what was expected.
In python something like that works:
_str = b"\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6"
_str = _str.decode()
print(_str)
You don't need to decode that. This is legal notation for strings in PHP.
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
echo $str; //λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
https://3v4l.org/0e0Po

PHP Encoding and Decode

I have a slice of php encoded code but I don't know how it encoded i mean by which why they encoded this code when i use unphp.net successfully it decode the code and get the real code
simple of encoded code define("\x53\x54\117\103\113\x5f\103\x48\105\x43\113", false) my question is how i can encode another code to be like this and it by which why is encoded please anyone have a knowledge about tell me or know any tool to suggest
I would need more information for me to be able to discern what obfuscator was actually used to encode this, but for this specific string, it appears to be a mix of hex- and octal- encoded letters
"\x53\x54\117\103\113\x5f\103\x48\105\x43\113"
\xFF - Hex
\000 - Octal
The sequence above is encoded as follows (where x is hex and 8 is octal): xx888x8x8x8
This sequence may be random or intentional. Assuming it's random, the following function may be used to encode any string in like manner using random_int() for selecting encoding method at random.
<?php
function hex_octal_rand_encode($s) {
$result = '';
foreach (str_split($s) as $c) {
$d = ord($c);
// select encoding method at random (0 = hex, 1 = oct)
$result .= (random_int(0, 1) === 0)
? '\\x' . dechex($d)
: '\\' . decoct($d);
}
return $result;
}
// Test
$input = "STOCK_CHECK";
$encoded = hex_octal_rand_encode($input);
echo sprintf("Encoded: %s\n", $encoded);
$cmd = sprintf('return "%s";', $encoded);
echo sprintf("Decoded: %s\n", eval($cmd));
?>
Sample outputs (notice the encoded value changes):
Encoded: \x53\x54\117\103\x4b\137\x43\x48\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \x53\124\x4f\x43\113\x5f\x43\110\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \123\124\x4f\x43\113\137\x43\x48\105\x43\113
Decoded: STOCK_CHECK

Array turn to json using php

need your help on this one...
I'm trying to create a code that will get a .txt file and convert all text content to json.
here's my sample code:
<?php
// make your required checks
$fp = 'SampleMessage01.txt';
// get the contents of file in array
$conents_arr = file($fp, FILE_IGNORE_NEW_LINES);
foreach($conents_arr as $key=>$value)
{
$conents_arr[$key] = rtrim($value, "\r");
}
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
echo $json_contents;
?>
I already got the result when i tried to echo the $json_contents
["Sample Material 1","tRAINING|ENDING","01/25/2018 9:37:00 AM","639176882315,639176882859","Y,Y","~"]
but when I tried to echo using like this method $json_contents[0]
I only got per character result.
Code
Result
hope you can help me on this one..
thank you
As PHP.net says
"Returns a string containing the JSON representation of the supplied value."
As you are using $json_contents[0] this will return the first char of the json string.
You can do this
$conents_arr[0]
Or convert your json string to PHP array using
$json_array = json_decode($json_contents, true);
echo $json_array[0];
It is happening because $json_contents is a string. It might be json string but it's string so string properties will apply here and hence when you echo $json_contents[0] it gives you first character of the string. You can either decode the encoded json string to object like below:
$json = json_decode($json_contents);
echo $json[0];
or echo it before the json_encode:
echo $conents_arr[0];
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
json_encode() function takes an array as input and convert it to json string.
echo $json_contents; just print out the string.
if you want to access it you have to decode the JSON string to array.
//this convert array to json string
$json_contents = json_encode($conents_arr, JSON_UNESCAPED_SLASHES);
//this convert json string to an array.
$json_contents = json_decode($json_contents, true);
//now you can access it
echo $json_contents[0];

Python equivalent of php base64_encode

PHP
<?php
$string = base64_encode(sha1( 'ABCD' , true ) );
echo sha1('ABCD');
echo $string;
?>
Output:
fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6
+y+FyIVn88jOm3mcfFRkLQx7QfY=
Python
import base64
import hashlib
s = hashlib.sha1()
s.update('ABCD')
myhash = s.hexdigest()
print myhash
print base64.encodestring(myhash)
Output:
'fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6'
ZmIyZjg1Yzg4NTY3ZjNjOGNlOWI3OTljN2M1NDY0MmQwYzdiNDFmNg==
Both the PHP and Python SHA1 works fine, but the base64.encodestring() in python returns a different value compared to base64_encode() in PHP.
What is the equivalent method for PHP base64_encode in Python?
base64.b64encode(s.digest()) have correct response
use sha1.digest() instead of sha1.hexdigest()
s = hashlib.sha1()
s.update('ABCD')
print base64.encodestring(s.digest())
The base64.encodestring expects to the string while you give it the hex representation of it.
You're encoding different sha1 results in PHP and Python.
In PHP:
// The second argument (true) to sha1 will make it return the raw output
// which means that you're encoding the raw output.
$string = base64_encode(sha1( 'ABCD' , true ) );
// Here you print the non-raw output
echo sha1('ABCD');
In Python:
s = hashlib.sha1()
s.update('ABCD')
// Here you're converting the raw output to hex
myhash = s.hexdigest()
print myhash
// Here you're actually encoding the hex version instead of the raw
// (which was the one you encoded in PHP)
print base64.encodestring(myhash)
If you base64 encode the raw and non-raw output, you will get different results.
It doesn't matter which you encode, as long as you're consistent.

PHP Serialize($array) without dumping array info a:1:{i:0;s:4.... and so on

Is there anything that i print the string without the array information?
(Without this: a:1:{i:0;s:4011:" ";} )?
The whole array must be in single variable and printed. Foreach doesn't help, neither print(array[0]).
You can use json_encode() which will give you less verbose string representation:
<?php
$a = array('a', 'bb' => 'ccc');
echo json_encode($a);
// outputs {"0":"a","bb":"ccc"}
You can use
echo json_encode($array);
This will print a json encoded string.
For you to decode it you can use
json_decode(json encoded string);
You can see docs of php about json encode and json decode

Categories