有代码:
Line1
Line2
..
LineN
我想自动制作
Line1
JunkLines2-100
Line2
JunkLines102-200
Line3
etc.
其中JunkLines是Perl垃圾代码,它不会改变程序状态,但看起来像是合法程序的延续。
我不想让我的代码混淆混淆器,比如说,将变量名称重命名为不可读的东西 - 这种不可读性是代码被混淆的信号。
答案 0 :(得分:2)
我不明白为什么这么多的投票:这看起来像一个有趣的问题。
我有空闲时间并且自己尝试了一些东西:
my
定义变量并使用sub
函数)看起来像这样
好代码(original.pl)
#!perl
use strict;
my $var1='hello';
my $var2='there';
sub f {
print "This is a function";
++$var1
}
print $var1;
print "\n$var2";
垃圾代码(garbage_code.pl)
#!perl
#---
my $var2='__NEW';
my $var3="This is garbage";
#---
sub g {
$var3+="changed";
print "This shall be removed";
return $var3;
}
#---
sub f {
return g(shift).$var2;
}
#---
f("function name shall be changed");
<强> Offuscator 强>
#!perl
use strict;
use Data::Dumper;
#-Get Original 'good' code
open CODE,"<original.pl" or die $!;
my @code=<CODE>;
close CODE;
#-Get Garbage code
open GARBAGE,"<garbage_code.pl" or die $!;
my @garbage=split(/^#---/m, join("",<GARBAGE>));
shift @garbage; #Remove header
map { s/^.*?(\w.*?)\s*$/\1/s } @garbage; #Trail spaces and EOL at beginning and end
map { s/print .*?;//g } @garbage; #Remove print calls
close GARBAGE;
#-List variables and functions in good code
my %list_var;
my %list_func;
for my $line (@code) {
if ($line=~/my \s*[\$@%](\w+)/) { $list_var{$1}=undef; }
elsif ($line=~/sub \s*(\w+)/) { $list_func{$1}=undef; }
else { }
}
#-List variables and functions in garbage code
my @list_var_garbage;
my @list_func_garbage;
for my $line (@garbage) {
while ($line=~/my \s*[\$@%](\w+)/g) { push(@list_var_garbage,$1); }
while ($line=~/sub \s*(\w+)/g) { push(@list_func_garbage,$1); }
}
#-Replace names of variables and functions in garbage code if it exists in good code
#Get equivalent name
for my $type ('var', 'func') {
my $rh_list = ($type eq 'var' ? \%list_var : \%list_func);
my @list_names=(keys %$rh_list, ($type eq 'var' ? @list_var_garbage : @list_func_garbage));
for my $name (@list_names) {
#Get new name
my $new_name=$name;
#For names of good code OR new names in garbage code
if (!defined $rh_list->{$new_name}) {
while (exists $rh_list->{$new_name}) { $new_name.="1"; }
#Store in hash table
$rh_list->{$new_name}=undef; #to ensure uniqueness of replacements
$rh_list->{$name}=$new_name; #Replacement name in garbage code
} else {}
}
}
#Replace
map { s/(?:sub \s*|&)\K(\w+)/$list_func{$1}/g } @garbage;
map { s/(\w+)\(/$list_func{$1}(/g } @garbage;
map { s/[\$@%]\K(\w+)/$list_var{$1}/g } @garbage;
#-Function to get garbage content
my $i_garbage=0;
sub get_garbage {
return $garbage[ ($i_garbage++) % scalar(@garbage) ]."\n";
}
#-Copy garbage in good code
my @new_code;
for my $line (@code) {
#-Add the line
push(@new_code, $line);
#-Add garbage
#Blocks: add garbage at the end
if ($line=~/\{/ .. $line=~/\}/) {
if ($line=~/\}/) { push(@new_code, get_garbage()); }
#Other: randomly add garbage
} else {
if (int(rand(2))) { push(@new_code, get_garbage()); }
}
}
#Print file with new code
open NEW_CODE, ">new.pl" or die $!;
print NEW_CODE @new_code;
close NEW_CODE;
<强>结果强>
#!perl
use strict;
my $var21='__NEW';
my $var3="This is garbage";
sub g {
$var3+="changed";
return $var3;
}
my $var1='hello';
sub f1 {
return g(shift).$var21;
}
my $var2='there';
f1("function name shall be changed");
sub f {
print "This is a function";
++$var1
}
my $var21='__NEW';
my $var3="This is garbage";
sub g {
$var3+="changed";
return $var3;
}
print $var1;
print "\n$var2";
sub f1 {
return g(shift).$var21;
}
f1("function name shall be changed");
它可能没有考虑所有情况,但这绝对是一个很好的工作原型。
干杯
答案 1 :(得分:1)
我不确定你是否因缺乏信息或什么而获得反对票,但这是一个相对容易的解决方案。假设您想在Perl中执行此操作:
只需创建另一个perl文件,该文件一次读取一行文件,将其打印到新文件,然后附加一行随机废话(在这种情况下,来自假定包含一个大文件的大文件)一堆随机的perl代码行&#34; obfuscatedlines.txt&#34;)
一个小例子:
use strict;
use warnings;
my @randomlines;
open (INHANDLE, "<perlfile.pl");
open (OBHANDLE, "obfuscatedlines.txt");
open (OUTHANDLE, ">newfile.pl");
while(<OBHANDLE>) {push @randomlines, $_;}
my $i=0;
while(<INHANDLE>)
{
print OUTHANDLE $_;
print OUTHANDLE $randomlines[$i];
$i++;
}