gpt4 book ai didi

PHP重命名代码中的所有变量

转载 作者:IT王子 更新时间:2023-10-29 00:11:39 25 4
gpt4 key购买 nike

我想将文件中的所有变量重命名为随机名称。

例如这个:

$example = "some $string";
function ($variable2) {
echo $variable2;
}
foreach ($variable3 as $key => $var3val) {
echo $var3val . "somestring";
}

会变成这样:

$frk43r = "some $string";
function ($izi34ee) {
echo $izi34ee;
}
foreach ($erew7er as $iure7 => $er3k2) {
echo $er3k2 . "some$string";
}

这看起来并不容易,所以任何建议都会有所帮助。

最佳答案

我会使用 token_get_all解析文档并映射已注册的 random string替换所有有趣的 token 。

要混淆所有变量名,一次替换T_VARIABLE,忽略所有the superglobals .

此外,对于赏金所需的函数名称,请替换第一遍中的所有 T_FUNCTION 声明。然后需要第二遍来替换所有 T_STRING 调用,因为 PHP 允许您在函数声明之前使用它。

对于这个例子,我生成了所有小写字母以避免与函数名称不区分大小写的冲突,但显然您可以使用任何您想要的字符并添加额外的条件检查以增加复杂性。请记住,它们不能以数字开头。

我还用 get_defined_functions 注册了所有内部函数名称以防止随机生成的字符串与其中一个函数名称匹配的可能性极小。请记住,这不会防止在运行混淆脚本的计算机上安装的特殊扩展,这些扩展在混淆脚本的服务器上不存在。发生这种情况的可能性是天文数字,但您始终可以逐渐增加随机生成的字符串的长度,以进一步降低这些可能性。

<?php

$tokens = token_get_all(file_get_contents('example.php'));

$globals = array(
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_POST',
'$_FILES',
'$_COOKIE',
'$_SESSION',
'$_REQUEST',
'$_ENV',
);

// prevent name clashes with randomly generated strings and native functions
$registry = get_defined_functions();
$registry = $registry['internal'];

// first pass to change all the variable names and function name declarations
foreach($tokens as $key => $element){
// make sure it's an interesting token
if(!is_array($element)){
continue;
}
switch ($element[0]) {
case T_FUNCTION:
$prefix = '';
// this jumps over the whitespace to get the function name
$index = $key + 2;
break;

case T_VARIABLE:
// ignore the superglobals
if(in_array($element[1], $globals)){
continue 2;
}
$prefix = '$';
$index = $key;
break;

default:
continue 2;
}

// check to see if we've already registered it
if(!isset($registry[$tokens[$index][1]])){
// make sure our random string hasn't already been generated
// or just so crazily happens to be the same name as an internal function
do {
$replacement = $prefix.random_str(16);
} while(in_array($replacement, $registry));

// map the original and register the replacement
$registry[$tokens[$index][1]] = $replacement;
}

// rename the variable
$tokens[$index][1] = $registry[$tokens[$index][1]];
}

// second pass to rename all the function invocations
$tokens = array_map(function($element) use ($registry){
// check to see if it's a function identifier
if(is_array($element) && $element[0] === T_STRING){
// make sure it's one of our registered function names
if(isset($registry[$element[1]])){
// rename the variable
$element[1] = $registry[$element[1]];
}
}
return $element;
},$tokens);

// dump the tokens back out to rebuild the page with obfuscated names
foreach($tokens as $token){
echo $token[1] ?? $token;
}

/**
* https://stackoverflow.com/a/31107425/4233593
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
* @return string
*/
function random_str($length, $keyspace = 'abcdefghijklmnopqrstuvwxyz')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}

鉴于此 example.php

<?php

$example = 'some $string';

if(isset($_POST['something'])){
echo $_POST['something'];
}

function exampleFunction($variable2){
echo $variable2;
}

exampleFunction($example);

$variable3 = array('example','another');

foreach($variable3 as $key => $var3val){
echo $var3val."somestring";
}

产生这个输出:

<?php

$vsodjbobqokkaabv = 'some $string';

if(isset($_POST['something'])){
echo $_POST['something'];
}

function gkfadicwputpvroj($zwnjrxupprkbudlr){
echo $zwnjrxupprkbudlr;
}

gkfadicwputpvroj($vsodjbobqokkaabv);

$vfjzehtvmzzurxor = array('example','another');

foreach($vfjzehtvmzzurxor as $riuqtlravsenpspv => $mkdgtnpxaqziqkgo){
echo $mkdgtnpxaqziqkgo."somestring";
}

关于PHP重命名代码中的所有变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com