Skip to content

Instantly share code, notes, and snippets.

@mokoshalb
Created April 21, 2020 21:26
Show Gist options
  • Save mokoshalb/26cb857983b867d3510d5292c8f2e6cd to your computer and use it in GitHub Desktop.
Save mokoshalb/26cb857983b867d3510d5292c8f2e6cd to your computer and use it in GitHub Desktop.
PHP code to obfuscate an HTML page on the browser to encode source code.
<?php
function randomString($length = 3) {
$randomString = '';
$characters = implode("", array_merge(range('a', 'z'), range('A', 'Z')));
for ($i = 0; $i < $length; $i++) $randomString .= $characters[mt_rand(0, strlen($characters) - 1)];
return $randomString;
}
function encode($output) {
$randomFunc = randomString();
$randomOut = randomString();
$randomNum = randomString();
$randomVal = mt_rand(999999, 99999999);
$return = '<!-- Obfuscator HTML -->
<script>var ' . $randomOut . ' = ""; var ' . $randomNum . ' = [';
foreach(str_split($output) as $x){ $return .= '"'.base64_encode(randomString().(ord($x) + $randomVal).randomString()) . '", '; if (mt_rand(0, 1)){ $return .= "\n"; } }
$return = rtrim($return, ', ');
$return .= ']; ' . $randomNum . '.forEach(function ' . $randomFunc . '(value) { ' . $randomOut . ' += String.fromCharCode(parseInt(atob(value).replace(/\D/g,\'\')) - ' . $randomVal . '); } ); document.write(decodeURIComponent(escape(' . $randomOut . '))); </script>' ;;
return $return;
}
ob_start("encode");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment