Friday, November 18, 2011

How do you convert a chunk of obfuscated or just plain messy and hard to read Perl code into readable format?

We can take advantage of the "Deparse" module. It compiles, then decompiles the program it is given, expanding it out and formatting it nicely.

To run it at the command line, type "perl -MO=Deparse prog.pl". Here is an example
of its usage, showing the input program (red) and the output of Deparse (blue).

$ cat messy.pl
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");


$ perl -MO=Deparse messy.pl
foreach $_ (74, 117, 115, 116) {
    $a .= chr $_;
}
;
$_ .= 'qwertyui' and tr/eiqrtuwy/nr oteah/ foreach ($b);
foreach $_ ($c) {
    $_ .= $^X;
    /(p.{2}l)/;
    $_ = $1;
}
$b =~ /(..)$/;
print "$a$b $c hack$1.";

No comments :