用于混淆的垃圾 perl 代码生成器
Junk perl code generator for obfuscation
有一个代码:
Line1
Line2
..
LineN
我要自动生产
Line1
JunkLines2-100
Line2
JunkLines102-200
Line3
etc.
其中 JunkLines 是 Perl 垃圾代码,它不会改变程序的状态,但看起来像是合法程序的延续。
我不想用混淆器混淆我的代码,比如说,将变量名重命名为不可读的东西 - 这种不可读性是代码被混淆的信号。
我不确定您是否因缺乏信息或其他原因而收到反对票,但这是一个相对容易的解决方法。假设您想在 Perl 中执行此操作:
只需创建另一个 perl 文件,它一次读入您的文件一行,将其打印到一个新文件中,并附加一行随机的废话后记(在这种情况下,来自一个假定为包含一堆随机的 perl 代码行 "obfuscatedlines.txt")
一个小例子:
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++;
}
我不明白为什么有这么多反对票:这看起来是个有趣的问题。
我有一些空闲时间并自己尝试了一些东西:
- 垃圾代码在单独的文件中,块由#---分隔
- Ofuscator 正在更改垃圾代码中变量和函数的名称,如果它存在于原始的好代码中(我假设您使用
my
定义变量,使用 sub
定义函数)
- Ofusctaor 正在从垃圾文件中随机添加内容(如果需要则循环)
看起来像这样
好代码 (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*$//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{}=undef; }
elsif ($line=~/sub \s*(\w+)/) { $list_func{}=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,); }
while ($line=~/sub \s*(\w+)/g) { push(@list_func_garbage,); }
}
#-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{}/g } @garbage;
map { s/(\w+)\(/$list_func{}(/g } @garbage;
map { s/[$@%]\K(\w+)/$list_var{}/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");
它可能没有考虑到所有情况,但这绝对是一个很好的工作原型。
干杯
有一个代码:
Line1
Line2
..
LineN
我要自动生产
Line1
JunkLines2-100
Line2
JunkLines102-200
Line3
etc.
其中 JunkLines 是 Perl 垃圾代码,它不会改变程序的状态,但看起来像是合法程序的延续。
我不想用混淆器混淆我的代码,比如说,将变量名重命名为不可读的东西 - 这种不可读性是代码被混淆的信号。
我不确定您是否因缺乏信息或其他原因而收到反对票,但这是一个相对容易的解决方法。假设您想在 Perl 中执行此操作:
只需创建另一个 perl 文件,它一次读入您的文件一行,将其打印到一个新文件中,并附加一行随机的废话后记(在这种情况下,来自一个假定为包含一堆随机的 perl 代码行 "obfuscatedlines.txt")
一个小例子:
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++;
}
我不明白为什么有这么多反对票:这看起来是个有趣的问题。
我有一些空闲时间并自己尝试了一些东西:
- 垃圾代码在单独的文件中,块由#---分隔
- Ofuscator 正在更改垃圾代码中变量和函数的名称,如果它存在于原始的好代码中(我假设您使用
my
定义变量,使用sub
定义函数) - Ofusctaor 正在从垃圾文件中随机添加内容(如果需要则循环)
看起来像这样
好代码 (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*$//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{}=undef; }
elsif ($line=~/sub \s*(\w+)/) { $list_func{}=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,); }
while ($line=~/sub \s*(\w+)/g) { push(@list_func_garbage,); }
}
#-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{}/g } @garbage;
map { s/(\w+)\(/$list_func{}(/g } @garbage;
map { s/[$@%]\K(\w+)/$list_var{}/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");
它可能没有考虑到所有情况,但这绝对是一个很好的工作原型。
干杯