Jim Lawless' Blog


Obfuscated Perl

Originally published on: Tue, 07 Jul 2009 11:33:26 +0000

In 1997, I entered a contest held by the publication The Perl Journal. It was their 1st annual Obfuscated Perl coding contest.

My entry in the category for simply printing "The Perl Journal" took 2nd place. ( You can now find this entry in the O'Reilly book Games, Diversions, and Perl Culture ).

I would like to briefly explain the genesis of my entry.

I had thought about listing my entry here, but I'm pretty certain that I don't own any kind of publication rights. One can still find it with a little Googling. However, the first two phases of the entry are still mine.

The program began with an idea that I had that involved allowing the Perl program read data from its own set of comments. The code opens itself as a file, loads its own source ( including the comments ).

To confuse the reader, I small p-machine and interpreted a short program to read from the comments and display The Perl Journal.


# Jim Lawless jimbo@radiks.net
# This program is an entry for the
# obfuscated Perl contest.

open(i,$0);@x=<i>;$y=join("",@x);close(i);
@z=split(/\ /,$y);
$c{"]"}='$d++;';
$c{">"}='$e++;';
$c{"?"}='print substr($z[$d],$e,1);';
$c{"["}='$d--;';
$c{"<"}='$e--;';
$c{"."}='print " ";';

$d=0; $e=0;

$f="]]]]]]?>?[[[[>>>?]]].]]]]]]]]]<<<<?>?>?>?[[[[[[[[.[[[[[<<<?" .
   "]]](fix this)]>>>>?]]]]]]]]<?[[[?]]]]]<?[[[[[[[[[[[[[<?>>?";

for($i=0;$i<length($f);$i++) {
   eval($c{substr($f,$i,1)});
}

The associative array %c holds the code for the six instruction machine.

The symbolic instructions are as follows:

  1. ] Increment variable $d
  2. > Increment variable $e
  3. ? Display the character in array $z, element $d, at position $e
  4. [ Decrement variable $d
  5. < Decrement variable $e
  6. . Print a space

The p-machine program that performed the output was embedded in the Perl code as the string:


$f="]]]]]]?>?[[[[>>>?]]].]]]]]]]]]<<<<?>?>?>?[[[[[[[[.[[[[[<<<?" .
   "]]](fix this)]>>>>?]]]]]]]]<?[[[?]]]]]<?[[[[[[[[[[[[[<?>>?";

Once I had the p-machine interpreter, I needed to make the code more unreadable. I took half of the top portion of the code and half of the lower portion and then interlaced the characters of each. I added a for loop to the final code that separated the two halves, concatenated them, then called eval() to interpret the original program.


   for($i=0;$i<368;$i+=2){$s=substr($v,$i,1);$q=$q .
   (($s eq '|') ? "'" : $s);$s=substr($v,$i+1,1);$r=$r .
   (($s eq '|') ? "'" : $s);}eval( $q . $r);print "\n";

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: A GUI for urlx
Next post:Removing IE Popups in C


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Mad Schemes : Learning Lisp via SICP

An Interview with Tom Zimmer: Forth System Developer

WSH JavaScript Includes

Twimmando: A Command-line Twitter Client

WSH2EXE part 1

A Quine in C

Internet Protocols and Rhino JavaScript

Envy

A Simple Media Control Interface Script Processor

Throwaway Software: HangUp


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service