Skip to content

Instantly share code, notes, and snippets.

@diamondsea
Forked from asgrim/index.php
Created October 10, 2020 05:34
Show Gist options
  • Save diamondsea/5b81cf961b278ff17b05e28f0c1cc6ae to your computer and use it in GitHub Desktop.
Save diamondsea/5b81cf961b278ff17b05e28f0c1cc6ae to your computer and use it in GitHub Desktop.
Crazy Morse-encoded PHP executor. Dirty and quick, no apologies for this code.
<?php
require 'morselib.php';
$morse = new morselib();
eval($morse->decode(file_get_contents("site.morse")));
<?php
/**
* Standard morse code (as per translation table)
* Plus characters not in the standard set are enclosed in ------ and ......
* with the ascii character code in between
*
* @author james
*
*/
class morselib
{
private $_translation_table;
public function __construct()
{
$this->_translation_table = array(
".-" => "a",
"-..." => "b",
"-.-." => "c",
"-.." => "d",
"." => "e",
"..-." => "f",
"--." => "g",
"...." => "h",
".." => "i",
".-" => "j",
"-.-" => "k",
".-.." => "l",
"--" => "m",
"-." => "n",
"---" => "o",
".--." => "p",
"--.-" => "q",
".-." => "r",
"..." => "s",
"-" => "t",
"..-" => "u",
"...-" => "v",
".--" => "w",
"-..-" => "x",
"-.--" => "y",
"--.." => "z",
"-----" => "0",
".----" => "1",
"..---" => "2",
"...--" => "3",
"....-" => "4",
"....." => "5",
"-...." => "6",
"--..." => "7",
"---.." => "8",
"----." => "9",
".-.-.-" => ".",
"--..--" => ",",
"..--.." => "?",
);
}
public function encode($text)
{
// Reverse the translation table
$rx_translation = array_flip($this->_translation_table);
$output = "";
for ($i = 0; $i < strlen($text); $i++)
{
if (isset($rx_translation[$text[$i]]))
{
$output .= "{$rx_translation[$text[$i]]} ";
}
else
{
$code = (string)ord($text[$i]);
$chr = "";
for ($j = 0; $j < strlen($code); $j++)
{
$chr .= "{$rx_translation[$code[$j]]} ";
}
$chr = trim($chr);
$output .= "------ {$chr} ...... ";
}
}
return $output;
}
public function decode($text)
{
$bits = explode(" ", str_replace("\n", " ", $text));
$output = "";
$in_ascii_mode = false;
$ascii = "";
foreach($bits as $bit_id => $bit)
{
if ($bit == "------")
{
$in_ascii_mode = true;
$ascii = "";
}
else if ($bit == "......")
{
$in_ascii_mode = false;
$chr = chr((int)$ascii);
$output .= $chr;
}
else if ($in_ascii_mode)
{
$ascii .= $this->_translation_table[$bit];
}
else if (isset($this->_translation_table[$bit]))
{
$output .= $this->_translation_table[$bit];
}
else
{
die("Failed on character {$bit_id} [$bit].");
}
}
return $output;
}
}
. -.-. .... --- ------ ...-- ..--- ...... ------ ...-- ....- ...... ------ -....
----- ...... .... - -- .-.. ------ -.... ..--- ...... ------ ----. ..--- ......
-. ------ ----. ...... ------ -.... ----- ...... .... . ------ ----. --...
...... -.. ------ -.... ..--- ...... ------ ----. ..--- ...... -. ------ ----.
...... ------ ----. ...... ------ -.... ----- ...... - .. - .-.. . ------ -....
..--- ...... ------ ---.. ----- ...... ------ ----. --... ...... --. . ------
...-- ..--- ...... .. -. ------ ...-- ..--- ...... -- --- .-. ... . ------ ...--
..--- ...... -.-. --- -.. . ------ -.... ----- ...... ------ ....- --... ......
- .. - .-.. . ------ -.... ..--- ...... ------ ----. ..--- ...... -. ------
----. ...... ------ -.... ----- ...... ------ ....- --... ...... .... . ------
----. --... ...... -.. ------ -.... ..--- ...... ------ ----. ..--- ...... -.
------ ----. ...... ------ -.... ----- ...... -... --- -.. -.-- ------ -....
..--- ...... ------ ----. ..--- ...... -. ------ ----. ...... ------ ----.
...... ------ -.... ----- ...... .... .---- ------ -.... ..--- ...... ------
---.. ....- ...... .... .. ... ------ ...-- ..--- ...... .-- ------ ----. --...
...... ... ------ ...-- ..--- ...... .-- .-. .. - - . -. ------ ...-- ..---
...... .. -. ------ ...-- ..--- ...... -- --- .-. ... . ------ ...-- ..---
...... -.-. --- -.. . ------ ...-- ...-- ...... ------ -.... ----- ...... ------
....- --... ...... .... .---- ------ -.... ..--- ...... ------ ----. ..---
...... -. ------ ----. ...... ------ -.... ----- ...... ------ ....- --...
...... -... --- -.. -.-- ------ -.... ..--- ...... ------ ----. ..--- ...... -.
------ -.... ----- ...... ------ ....- --... ...... .... - -- .-.. ------ -....
..--- ...... ------ ----. ..--- ...... -. ------ ...-- ....- ...... ------ .....
----. ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment