Skip to content

Instantly share code, notes, and snippets.

@nhCoder
Last active February 27, 2022 16:49
Show Gist options
  • Save nhCoder/4a6b8e60e834d579556d145d08a26ff9 to your computer and use it in GitHub Desktop.
Save nhCoder/4a6b8e60e834d579556d145d08a26ff9 to your computer and use it in GitHub Desktop.
Pulls the cipher signature decipher JS code from youtube. Youtube protects its videos using a cipher signature ,which is decoded by player before playing so to decrypt the cipher use the code below code to get the cipher code from YouTube and run it in WebView or JavaScript engine to decipher the signature. Demo : http://detectivec.de/ytcipher.php
<?php
function startsWith ($string, $startString)
{
$len = strlen($startString);
return (substr($string, 0, $len) === $startString);
}
$url="https://www.youtube.com/";
//$regex_player="/(?<=ytplayer.config\s=).*?(\})(?=;)/m";
$regex_player_url="/(?<=PLAYER_JS_URL\":\").*?(?=\")/m";
$regex_desipher_function_code="/\\{[a-zA-Z]{1,}=[a-zA-Z]{1,}.split\\(\"\"\\);[a-zA-Z0-9$]{2}\\.[a-zA-Z0-9$]{2}.*?[a-zA-Z]{1,}.join\\(\"\"\\)\\};/s";
$regex_var_name="/[a-zA-Z0-9$]{2}\\.[a-zA-Z0-9$]{2}\\([a-zA-Z]\\,(\\d\\d|\\d)\\)/s";
$source=file_get_contents($url);
preg_match($regex_player_url, $source, $matches, PREG_OFFSET_CAPTURE, 0);
$player_url=str_replace("\\","" ,urldecode($matches[0][0]));
if(!startsWith($player_url,"wwww") || !startsWith($player_url,"http")){
$player_url="https://www.youtube.com".$player_url;
}
$js= file_get_contents($player_url);
preg_match($regex_desipher_function_code, $js, $ds_fun, PREG_OFFSET_CAPTURE, 0);
preg_match($regex_var_name, $ds_fun[0][0], $var_name, PREG_OFFSET_CAPTURE, 0);
$var_name=str_replace("$","\\$",$var_name[0][0]);
$varname=explode(".",$var_name);
$regex_var_code = "/var\\s" . $varname[0] . "=.*?\\};/s";
preg_match($regex_var_code, $js, $var_code, PREG_OFFSET_CAPTURE, 0);
$decipher_fun="decipher=function(a)".$ds_fun[0][0]."\n\n".$var_code[0][0];
echo ($decipher_fun);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment