Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Obfuscators will run for cover now...

by princepawn (Parson)
on May 02, 2001 at 21:13 UTC ( #77419=perlmeditation: print w/replies, xml ) Need Help??

Have you seen PerlTidy?

It reformats Perl scripts for readability. Take a look at the clean-up job it did on this Fifth Annual Obfuscated Perl Contest entry

Replies are listed 'Best First'.
Re: Obfuscators will run for cover now...
by turnstep (Parson) on May 02, 2001 at 21:42 UTC

    Very interesting. Of course, it will break obfuscated scripts that require a certain whitespace alignment, but it does a pretty good job. Here's how it did on one of my shorter ones. First, the original:

    open(0,$0);$/=$;;<0>=~m{(ET / /).*(C[^\*]+)}s;$b=$1;($G=$g=$2) =~s/.*(en).*/s$1d(t,/s;$_=join');',S($g),S($g),"$G\"G$b\n\n\", 0","\$/=<t>";substr($_,26,1,'"'.pack("cx2c5x8",2,80,209,85,3, 25).'"');sub S{split(//,shift);$_[3]=chr(ord($_[3])-($*?6:9)); $_[5]=chr(ord($_[5])-($*?11:-6));$_[7]='(t,';if(!$*++){$_[8]= join(',',split(/5?/,(ord($_[10])+104)));$_[2]='c';$_[6]=""; $_[0]='s'}$#_=8;lc join('',@_);}$_=eval;s/.*\r\n//s; s#(<[^>]+>)*(\w{0,2})#"$1<FONT COLOR=".(int rand 999999)."> $2</FONT>"#eg;print;#Content-type

    And now, the perltidy version:

    open( 0, $0 ); $/ = $;; <0> =~ m{(ET / /).*(C[^\*]+)}s; $b = $1; ( $G = $g = $2 ) =~ s/.*(en).*/s$1d(t,/s; $_ = join ');', S($g), S($g), "$G\"G$b\n\n\", 0", "\$/=<t>"; substr( $_, 26, 1, '"' . pack( "cx2c5x8", 2, 80, 209, 85, 3, 25 ) . '" +' ); sub S { split ( //, shift ); $_[3] = chr( ord( $_[3] ) - ( $* ? 6 : 9 ) ); $_[5] = chr( ord( $_[5] ) - ( $* ? 11 : -6 ) ); $_[7] = '(t,'; if ( !$*++ ) { $_[8] = join ( ',', split ( /5?/, ( ord( $_[10] ) + 104 ) ) ); $_[2] = 'c'; $_[6] = ""; $_[0] = 's'; } $#_ = 8; lc join ( '', @_ ); } $_ = eval; s/.*\r\n//s; s#(<[^>]+>)*(\w{0,2})#"$1<FONT COLOR=".(int rand 999999)."> $2</FONT>"#eg; print; #Content-type

    Not bad. I like it. Always meant to write some sort of de-obfuscator myself. This is a good start, the next obvious step would be to have it replace those oft-used global vars with something simpler like $x,$y, etc..

Re: Obfuscators will run for cover now...
by Dominus (Parson) on May 03, 2001 at 00:57 UTC
Re: Obfuscators will run for cover now...
by dws (Chancellor) on May 02, 2001 at 21:50 UTC
    Nifty. This'll be a great help in understanding some of the wilder JAPHs here. One obvious limitation is that obfuscations that reference themselves (via rewinding DATA) get broken in the tidying process.

      I prefer B::Deparse. Obfuscations get extra points if they break that! Although from what I hear on p5p a lot of work is going into making it more robust at the moment. I like the way B::Deparse normalises things, something that PerlTidy doesn't seem to do.

      % perl -MO=Deparse -e '$foo="bar"."rat"' -e syntax OK $foo = 'barrat';

      --
      g r i n d e r

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://77419]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-11-28 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?