Obfuscation, Golfing and Secret Operators in Perl

137
OGSOP Obfuscation, Golfing and Secret Operators in Perl 難読化・Perl Golf・ひみつの演算子 1

description

Everything you always wanted to know about Obfuscation and Golfing, some strange techniques and odd operators many are not aware of. Also, everything you *never* wished to know about Obfuscation and Golfing, some even stranger techniques and even odder operators many wished they were *not* aware of. Video available at http://conferences.yapcasia.org/ya2008/talk/1007.

Transcript of Obfuscation, Golfing and Secret Operators in Perl

Page 1: Obfuscation, Golfing and Secret Operators in Perl

OGSOPObfuscation, Golfing and Secret Operators in Perl

難読化・Perl Golf・ひみつの演算子

1

Page 2: Obfuscation, Golfing and Secret Operators in Perl

Obfuscation難読化

2

Page 3: Obfuscation, Golfing and Secret Operators in Perl

How do I impress my friends who don’t know much Perl

with obfuscation?

3

Page 4: Obfuscation, Golfing and Secret Operators in Perl

With simple things

4

Page 5: Obfuscation, Golfing and Secret Operators in Perl

#!/usr/bin/perl $:?do{exit&&exit}:print"Just another Perl hacker,"

5

Page 6: Obfuscation, Golfing and Secret Operators in Perl

#!/usr/bin/perl $:?do{exit||exit}:print"Just another Perl hacker,"

6

Page 7: Obfuscation, Golfing and Secret Operators in Perl

Spaces? What spaces?

$:?do{exit&&exit}:print"Just another Perl hacker,"

スペース? どれよ?

7

Page 8: Obfuscation, Golfing and Secret Operators in Perl

#!/usr/bin/perl$:? do {exit &&exit } : print“Just another”.“ Perl hacker,”

Use newlines...

8

Page 9: Obfuscation, Golfing and Secret Operators in Perl

#!/usr/bin/perl$:?do{exit&&exit }:print“Jus”.“t a”.“not”.“her”.“ Pe”.“rl “.“hac”.“ker,”

As many as you can...

9

Page 10: Obfuscation, Golfing and Secret Operators in Perl

if($var) {$var = 0 }else {$var = 1 }

Forget about indentation

10

Page 11: Obfuscation, Golfing and Secret Operators in Perl

if($var) {$var = 0 } else {$var = 1 }

Or at least use it wrongly

11

Page 12: Obfuscation, Golfing and Secret Operators in Perl

print "Hello " ;print "World"

Semicolons: use wrongly

12

Page 13: Obfuscation, Golfing and Secret Operators in Perl

;;;;;;;;;;;;;;;;;;;;;;;;;print "Hello World";;;;;;;;;;;;;;;;;;;;;;;;;

;;s;;print "Hello World";e;;

Semicolons: use plenty

13

Page 14: Obfuscation, Golfing and Secret Operators in Perl

$a++ ; $b--$a++ , $b--$a++ . $b--$a++ .. $b--

Semicolons: use alternatives

14

Page 15: Obfuscation, Golfing and Secret Operators in Perl

s//Just another Perl hacker,/ ; print

Use things indirectly

15

Page 16: Obfuscation, Golfing and Secret Operators in Perl

Bend the rules

s/ / /;y/ / /;

16

Page 17: Obfuscation, Golfing and Secret Operators in Perl

Bend the rules

s; ; ; ;s { }[ ]s s s sy ; ; ;;y y y y

17

Page 18: Obfuscation, Golfing and Secret Operators in Perl

And use unneeded things

; s ; ; ; ;{ };s{ }[ ]s s s s s; y ; ; ; ;

18

Page 19: Obfuscation, Golfing and Secret Operators in Perl

Mix them together

y s y s y ss y s y s y

19

Page 20: Obfuscation, Golfing and Secret Operators in Perl

Mix things up

1?s::::0

20

Page 21: Obfuscation, Golfing and Secret Operators in Perl

Mix things up

1 ? s::: : 0

21

Page 22: Obfuscation, Golfing and Secret Operators in Perl

Is it code?

y//No, it is not/;

22

Page 23: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s##No, it is not#;

23

Page 24: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s##we want to print "Just another Perl hacker,"##

24

Page 25: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s;;;;s ##we want to print "Just another Perl hacker,"##

25

Page 26: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s;;;;s##we want to print "Just another Perl hacker,"##

26

Page 27: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s;old;new;g;s##we want to print "Just another Perl hacker,"##

27

Page 28: Obfuscation, Golfing and Secret Operators in Perl

Is it a comment?

s;old;new;g;s##we want to print "Just another Perl hacker,"##;s^.{11}^^&&eval;

28

Page 29: Obfuscation, Golfing and Secret Operators in Perl

Are those brackets closed?

{y/}/{/}

29

Page 30: Obfuscation, Golfing and Secret Operators in Perl

WYSINWYG

$ perl -le ‘print “All digits!” if 0x20 =~ /^\d+\z/’All digits!

$ perl -le ‘print 0x2032

30

Page 31: Obfuscation, Golfing and Secret Operators in Perl

WYSINWYG

print "No underscores!" if 1_234_567_890 !~ /_/; print "No e!" if 1e6 !~ /e/; print "There is an e!" if 6666666666666666 =~ /e/; print "There is a plus!" if 1e15 =~ /\+/; print "No dot!" if 0.00 !~ /\./;

31

Page 32: Obfuscation, Golfing and Secret Operators in Perl

Hexadecimal or... whatever

eval "\x70\x72\x69\x6e\x74";

eval "\160\x72\151\x6e\164";

$_="0741171151160320971101111161041011140320801011". "14108032104097099107101114046";s;...;print chr$&;eg

32

Page 33: Obfuscation, Golfing and Secret Operators in Perl

$;$:$_$^@^

Variable names

33

Page 34: Obfuscation, Golfing and Secret Operators in Perl

$ ;$ :$ _$ ^@ ^

Variable names: and behold

34

Page 35: Obfuscation, Golfing and Secret Operators in Perl

1 ? s : $ s : : : 0

Variable names: mix them

35

Page 36: Obfuscation, Golfing and Secret Operators in Perl

1 ? s : s $ s : s $ s : : 0

Variable names: cleverly

36

Page 37: Obfuscation, Golfing and Secret Operators in Perl

print ${1+1}

Variable names: calculate

37

Page 38: Obfuscation, Golfing and Secret Operators in Perl

print ${$_}

Variable names: store them

38

Page 39: Obfuscation, Golfing and Secret Operators in Perl

@_=(Just,another,Perl,hacker);$,=$”;print@_

$,=” another Perl “;print(“Just”,”hacker”);

Special variables

39

Page 40: Obfuscation, Golfing and Secret Operators in Perl

y/a-z/:-z/

Behind human capabilities

40

Page 41: Obfuscation, Golfing and Secret Operators in Perl

|&^~

Use uncommon things

41

Page 42: Obfuscation, Golfing and Secret Operators in Perl

print “a” | “c”;print ~0 & 2 ;print 1 ^ 2 ;print ~0;

Use uncommon things

42

Page 43: Obfuscation, Golfing and Secret Operators in Perl

sub _ { “something tricky” }_$_

so you can do clever things

43

Page 44: Obfuscation, Golfing and Secret Operators in Perl

$_ = sub _ { “something tricky” }&{$_}

or don’t name it at all

44

Page 45: Obfuscation, Golfing and Secret Operators in Perl

s;;;

And be inventive

45

Page 46: Obfuscation, Golfing and Secret Operators in Perl

;;;;s;;;;

And be *very* inventive

46

Page 47: Obfuscation, Golfing and Secret Operators in Perl

g;;;s;;;g

And be *VERY* inventive

47

Page 48: Obfuscation, Golfing and Secret Operators in Perl

Example例

48

Page 49: Obfuscation, Golfing and Secret Operators in Perl

print "Just another Perl hacker,"

JAPH

49

Page 50: Obfuscation, Golfing and Secret Operators in Perl

print "Just ", "another ", "Perl ", "hacker,"

JAPH

50

Page 51: Obfuscation, Golfing and Secret Operators in Perl

$, = " ";print "Just", "another", "Perl", "hacker,"

JAPH

51

Page 52: Obfuscation, Golfing and Secret Operators in Perl

$, = " "; print reverse "hacker,", "Perl", "another", "Just"

JAPH

52

Page 53: Obfuscation, Golfing and Secret Operators in Perl

$, = " "; print reverse"hacker,","Perl","another","Just"

JAPH

53

Page 54: Obfuscation, Golfing and Secret Operators in Perl

$, = " "; push@,,$_ for reverse"hacker,","Perl","another",”Just”;

print @_

JAPH

54

Page 55: Obfuscation, Golfing and Secret Operators in Perl

$, = $" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;

print @_

JAPH

55

Page 56: Obfuscation, Golfing and Secret Operators in Perl

$, =+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;

print @_

JAPH

56

Page 57: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;

print @_

JAPH

57

Page 58: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”; join$,,@_ ;print $_

JAPH

58

Page 59: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s //join$,,@_/e;print $_

JAPH

59

Page 60: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s //”join”.’$,,@_’/ee;print $_

JAPH

60

Page 61: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s //"\x6a\x6f\x69\x6e".’$,,@_’/ee;print $_

JAPH

61

Page 62: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s //"\x6a\x6f\x69\x6e".’$,,@,’/ee;print $_

JAPH

62

Page 63: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s //"\x6a\x6f\x69\x6e".’$,,@,’/ee;print

JAPH

63

Page 64: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{}/"\x6a\x6f\x69\x6e".’$,,@,’/ee;print

JAPH

64

Page 65: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"\x6a\x6f\x69\x6e".’$,,@,’;ee;print

JAPH

65

Page 66: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"print \x6a\x6f\x69\x6e".’$,,@,’;ee;eval

JAPH

66

Page 67: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"print \x6a\x6f\x69\x6e".’$,,@,’;eee

JAPH

67

Page 68: Obfuscation, Golfing and Secret Operators in Perl

+$,=+$" ; push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".’$,,@,’;eee

JAPH

68

Page 69: Obfuscation, Golfing and Secret Operators in Perl

(+$,=+$"). push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".’$,,@,’;eee

JAPH

69

Page 70: Obfuscation, Golfing and Secret Operators in Perl

(+$,=+$").#s# most people # think # this is a comment #push@,,$_ for reverse"hacker,","Perl","another",”Just”;s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".’$,,@,’;eee

JAPH

70

Page 71: Obfuscation, Golfing and Secret Operators in Perl

Warning警告

71

Page 72: Obfuscation, Golfing and Secret Operators in Perl

Don’t you ever, ever, ever, run obfuscated code you don’t

understand and cannot trust

72

Page 73: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

73

Page 74: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

74

Page 75: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

75

Page 76: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

76

Page 77: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

77

Page 78: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

78

Page 79: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

79

Page 80: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

80

Page 81: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

81

Page 82: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

82

Page 83: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

83

Page 84: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

84

Page 85: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

85

Page 86: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

86

Page 87: Obfuscation, Golfing and Secret Operators in Perl

$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|‘{; ; y; -/:-@[-‘{-};‘-{/" -;;s;;$_;see

Random code on the web

87

Page 88: Obfuscation, Golfing and Secret Operators in Perl

So what’s on $_ afterall?

Random code on the web

88

Page 89: Obfuscation, Golfing and Secret Operators in Perl

So what’s on $_ afterall?

system”rm -rf /”

Random code on the web

89

Page 90: Obfuscation, Golfing and Secret Operators in Perl

Golfingゴルフ

90

Page 91: Obfuscation, Golfing and Secret Operators in Perl

One character variables

$c $d $e $f $g

91

Page 92: Obfuscation, Golfing and Secret Operators in Perl

If you don’t need it...

print “something”;

print”something”

92

Page 93: Obfuscation, Golfing and Secret Operators in Perl

Forget about \n

s/\n//;

s///;

$_=”something\n”

$_=”something“

93

Page 94: Obfuscation, Golfing and Secret Operators in Perl

END in the beginning

something;END{something_else}

END{something_else}something

94

Page 95: Obfuscation, Golfing and Secret Operators in Perl

Learn the alternatives

map{[$_]}@array

map[$_],@array

95

Page 96: Obfuscation, Golfing and Secret Operators in Perl

for the other way around

for(@a){something}

something for@a

96

Page 97: Obfuscation, Golfing and Secret Operators in Perl

Learn the switches

#!/usr/bin/perlprint”this\n”;print”that\n”;print”the other\n”;

#!/usr/bin/perl -lprint”this”;print”that”;print”the other”;

97

Page 98: Obfuscation, Golfing and Secret Operators in Perl

Learn the special variables

#!/usr/bin/perl@_=qw/Just another Perl hacker,/;print join” “,@_

#!/usr/bin/perl@_=qw/Just another Perl hacker,”;print”@_”

98

Page 99: Obfuscation, Golfing and Secret Operators in Perl

Golfing techniques

99

Page 100: Obfuscation, Golfing and Secret Operators in Perl

Golfing techniques

$_ x=

100

Page 101: Obfuscation, Golfing and Secret Operators in Perl

Golfing techniques

What does this do?

$_ x= / /;

101

Page 102: Obfuscation, Golfing and Secret Operators in Perl

Golfing techniques

y///c

102

Page 103: Obfuscation, Golfing and Secret Operators in Perl

Secret Operatorsひみつの演算子

103

Page 104: Obfuscation, Golfing and Secret Operators in Perl

Secret Eskimo Greeting

}{

104

Page 105: Obfuscation, Golfing and Secret Operators in Perl

Secret Eskimo Greeting

#!/usr/bin/perl -n# What does this do?}{print$.

105

Page 106: Obfuscation, Golfing and Secret Operators in Perl

Secret Eskimo Greeting

$ perldoc -perlrun

[...]

#!/usr/bin/perlLINE: while (<>) { ... # your program goes here }

[...]

106

Page 107: Obfuscation, Golfing and Secret Operators in Perl

Secret Eskimo Greeting

#!/usr/bin/perlLINE: while (<>) {}{print$. }

107

Page 108: Obfuscation, Golfing and Secret Operators in Perl

Supermarket Trolley

@{[]}

108

Page 109: Obfuscation, Golfing and Secret Operators in Perl

Supermarket Trolley

my @stuff = qw/pocket_watch 3ninja_tshirts/;

print "Our list includes: @{[map {ucfirst} @things_for_my_other_talks]}."

print "Our list includes: ", map {ucfirst} @things_for_my_other_talks], "."

109

Page 110: Obfuscation, Golfing and Secret Operators in Perl

Supermarket Trolley

my @stuff = qw/pocket_watch 3ninja_tshirts/;

print "Our list includes: @{[map {ucfirst} @things_for_my_other_talks]}."

print "Our list includes: ", map {ucfirst} @things_for_my_other_talks], "."

my @new_list = map {ucfirst} @things_for_my_other_talks;

110

Page 111: Obfuscation, Golfing and Secret Operators in Perl

Supermarket Trolley

my @stuff = qw/pocket_watch 3ninja_tshirts/;

print "Our list includes: @{[map {ucfirst} @things_for_my_other_talks]}."

print "Our list includes: ", map {ucfirst} @things_for_my_other_talks], "."

my @new_list = map {ucfirst} @things_for_my_other_talks;

print "Our list includes: @new_list."

111

Page 112: Obfuscation, Golfing and Secret Operators in Perl

Goatse

=()=

112

Page 113: Obfuscation, Golfing and Secret Operators in Perl

Goatse

my $_ = "Just another Perl hacker,";my $spaces =()= / /g

113

Page 114: Obfuscation, Golfing and Secret Operators in Perl

Inchworm on a stick

~-

114

Page 115: Obfuscation, Golfing and Secret Operators in Perl

Inchworm on a stick

What does this do?

$y = ~-$x*4;

115

Page 116: Obfuscation, Golfing and Secret Operators in Perl

Inchworm on a stick

What does this do?

$y = ~-$x*4;$y = ($x-1)*4;

116

Page 117: Obfuscation, Golfing and Secret Operators in Perl

Inchworm on a stick

What does this do?

$y = ~-$x*4;$y = ($x-1)*4;

High precedence decrement

(or a high precedence increment, if $x<0)

117

Page 118: Obfuscation, Golfing and Secret Operators in Perl

///

///

118

Page 119: Obfuscation, Golfing and Secret Operators in Perl

///

What does this do?

print ///4;

119

Page 120: Obfuscation, Golfing and Secret Operators in Perl

///

What does this do?

print ///4;print //; # prints 1

120

Page 121: Obfuscation, Golfing and Secret Operators in Perl

///

What does this do?

print ///4;print //; # prints 1print 1/4; # prints 0.25

121

Page 122: Obfuscation, Golfing and Secret Operators in Perl

///

What does this do?

print ///4;print //; # prints 1print 1/4; # prints 0.25print ///4; # prints 0.25

122

Page 123: Obfuscation, Golfing and Secret Operators in Perl

Inchworm

~~

123

Page 124: Obfuscation, Golfing and Secret Operators in Perl

Inchworm

What does this do?

print ~~ gmtime

124

Page 125: Obfuscation, Golfing and Secret Operators in Perl

Inchworm

$ perl -e 'print gmtime'31441214410831340

125

Page 126: Obfuscation, Golfing and Secret Operators in Perl

Inchworm

$ perl -e 'print gmtime'31441214410831340

$ perl -e 'print ~~ gmtime'Wed May 14 12:44:35 2008

126

Page 127: Obfuscation, Golfing and Secret Operators in Perl

Min Max

[ ]->[ ]

127

Page 128: Obfuscation, Golfing and Secret Operators in Perl

Min Max

What does this do?

[ $x => $y ]->[ $y <= $x ]

128

Page 129: Obfuscation, Golfing and Secret Operators in Perl

Min Max

What does this do?

[ $x => $y ]->[ $y <= $x ]

...that’s the lesser of $x and $y

129

Page 130: Obfuscation, Golfing and Secret Operators in Perl

Min Max

What does this do?

[ $x => $y ]->[ $y <= $x ]

...that’s the lesser of $x and $y

[ $x => $y ]->[ $x <= $y ]

...and that’s the greater of $x and $y

130

Page 131: Obfuscation, Golfing and Secret Operators in Perl

Examples例

131

Page 132: Obfuscation, Golfing and Secret Operators in Perl

Erudil’s Camel Code #!/usr/bin/perl -w # camel codeuse strict;

$_='ev al("seek\040D ATA,0, 0;");foreach(1..3) {<DATA>;}my @camel1hump;my$camel; my$Camel ;while( <DATA>){$_=sprintf("%-69s",$_);my@dromedary 1=split(//);if(defined($_=<DATA>)){@camel1hum p=split(//);}while(@dromeda ry1){my$camel1hump=0 ;my$CAMEL=3;if(defined($_=shif t(@dromedary1 ))&&/\S/){$camel1hump+=1<<$CAMEL;} $CAMEL--;if(d efined($_=shift(@dromedary1))&&/\S/){ $camel1hump+=1 <<$CAMEL;}$CAMEL--;if(defined($_=shift( @camel1hump))&&/\S/){$camel1hump+=1<<$CAMEL;}$CAMEL--;if( defined($_=shift(@camel1hump))&&/\S/){$camel1hump+=1<<$CAME L;;}$camel.=(split(//,"\040..m`{/J\047\134}L^7FX"))[$camel1h ump];}$camel.="\n";}@camel1hump=split(/\n/,$camel);foreach(@ camel1hump){chomp;$Camel=$_;y/LJF7\173\175`\047/\061\062\063\ 064\065\066\067\070/;y/12345678/JL7F\175\173\047`/;$_=reverse; print"$_\040$Camel\n";}foreach(@camel1hump){chomp;$Camel=$_;y /LJF7\173\175`\047/12345678/;y/12345678/JL7F\175\173\0 47`/; $_=reverse;print"\040$_$Camel\n";}';;s/\s*//g;;eval; eval ("seek\040DATA,0,0;");undef$/;$_=<DATA>;s/\s*//g;( );;s ;^.*_;;;map{eval"print\"$_\"";}/.{4}/g; __DATA__ \124 \1 50\145\040\165\163\145\040\157\1 46\040\1 41\0 40\143\141 \155\145\1 54\040\1 51\155\ 141 \147\145\0 40\151\156 \040\141 \163\16 3\ 157\143\ 151\141\16 4\151\1 57\156 \040\167 \151\164\1 50\040\ 120\1 45\162\ 154\040\15 1\163\ 040\14 1\040\1 64\162\1 41\144 \145\ 155\14 1\162\ 153\04 0\157 \146\ 040\11 7\047\ 122\1 45\15 1\154\1 54\171 \040 \046\ 012\101\16 3\16 3\15 7\143\15 1\14 1\16 4\145\163 \054 \040 \111\156\14 3\056 \040\ 125\163\145\14 4\040\ 167\1 51\164\1 50\0 40\160\ 145\162 \155\151 \163\163 \151\1 57\156\056

132

Page 133: Obfuscation, Golfing and Secret Operators in Perl

Saturn

#!/usr/bin/perl ;;;;;; ;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;; +$I=sub{+s+^+ ;;;;;;; ;;;;;;;;; $"x$_[1]+gem;$/x$_# ;;;; ;;;;;;;; [0].$_.$/};$W=sub{$~=!q~ ;;;;;;; ~.pop();system($^O=~Win?ClS:# ;;;;;;; 'clear'),print,select$Z,$Z,$Z,!" ;;;;;; "||$~for@_};$H=sub{+join$/,map($_# ;;;;;; x$_[0],pop=~m-.+-g),!_};$_=!Mima,s-- ;;;;; "@{['=9+)w'^RINGS]}\%;local@{[Saturn^# ;;;;; wNXIBP]}"-see;s-^#!..+?$/(?=$"+;)--is ;;;; y-;-'-;s-\w-~-gi;$S=$_;#--Beautiful] ;;;; @S=m-.+-g;$N=1+.6-!th_,$--=-82-$--- ;;; $_.=$"x-(y---c-$-)for@S;$R=sub{$i# ;;; -d =0;join$/,map{$j=$%;join!_,grep# ;;; Rhea !($j++%$_[$%]),m-.-g}grep!($i# ;;; -Titan ++%$_[0]),@S};$L=join!_,map# ;;; -Huygens ~~reverse.$/,@S;@R=(&$I(q- ;;; -&&20,051, $_=_^q-q-),&$I(20,41-!q- ;;; -,$_=F|K),$ I->(15,31,$_=&$R(4-!q- ;;; -)),&$I(13-!" ;;",28,$_=&$R(3)),&${ ;;; _^_^I}(10,20-!" ;;;;;",$_=$R->(2)),q- ;;; -&&$S);@O=map&{" ;;;;;; "&&$H}($_,&${ ;;; R.!-_}($_))x$_,!" ;;;;; "+2..2*~~2 ;;; @Y=reverse@R#Dione ;;;;;; &${m-- ;;; S|A|T|U}(@R,$N)||!q- ;;;;;;; b- ;;; &$W(@O[0,1,2,1,0!=!q- ;;;;;;; ;;;; -],!1!~~1);&$W($S.!q- ;;;;;;;;; ;;;;; -,$L,0.16)for$%..5+!q- ;;;;;;;;;; ;;;;;;;;; Cassini-;&{$W||q- ;;;;;;;;;;;;;;;;;;;;;; -}(@Y,1.6) ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;

133

Page 134: Obfuscation, Golfing and Secret Operators in Perl

Space Invaders

#!/usr/bin/perl

use strict; use Term'ReadKey;ReadMode 'cbreak';$,=$/;$/=++$b+$b;$=='8';$b=$= x28;$*=$=x2;@!=(qw(4 _ " a 1 1 0 0 ));$![7]=`clear`;push(@!,split shift(@!),q 7$=W8W$=8$b eq "j"8W$=8$=W8 $b eq"l"8W$=8W!8($b eq"k"&&$_!~/!/)a#W$=8#$=W8$=W#8W$=#8#!8##a\*8$=8p$=8$=p 8$=q8q$=8p(#.{59})$=8$=$ 1q8#q(.{60})$=8#$=$1 pap(.{60})!8$![0]++;"\*$ 1$="8q(.{60})!8$![0] ++;"\*$1$="8(t.*)!8$b=(l ength$1)-61;substr($ 1,0,$b )."!". substr($1, $b+1).$=4x"gs'"x"c"es'"c"z"gs if'.shift"z"l"just another perl hacker"l4'SpaceInvaders'.($=x12).'[email protected] j=left k=fire l=right'_'#'x58_("$*p$*"x11).$=x3_$*.("$*$*q"x11).$=_("p$*$*"x11).$=x3_($=x3).("$*$*q"x11)_1_$b.$=."W$b"_'#'x 587);j(split shift@!,pop@!);a(split shift@!,pop@!);push(@!,split shift@!,pop@!);$_=$a;map{s/\n//g}@!;map{s/v//g}@!;map{s/ //g}@!;&p;sub'j{$_=shift;$a.=m/^1/?("#$b$=$=$b#$,"x12):"#".eval( )."#$,";@_&&j(@_)}sub'END{ReadMode 'normal' }sub'a{eval"sub'".pop().'{eval\'s/\'.(shift).\'/\'.(shift).\'/'.pop().';@_&&'.pop().'(@_)}';@_&&a(@_)}sub'p{$b=ReadKey -1;$![1]%3==0&&x(split$=,$![6]);z(split$=,$![4]);x(split$=,$![5]);c(split$=,$![7]);$![1]++;select+(@-,@-,@-,0.05);($![2]=$_)=~s/$=/ /g;/pW|Wq/&&h('Game over');$_!~/p|q/&&h('You saved our planet. Yeah');print$![3],$![2 ];&p}sub h{print $![ 3],shift,"You killed $![0] space invaders ",$,;exit}

134

Page 135: Obfuscation, Golfing and Secret Operators in Perl

See alsoも参照ください

135

Page 136: Obfuscation, Golfing and Secret Operators in Perl

• The Art of Obfuscation, by Thomas Klausner

• How to Write Unmaintainable Code, by Roedy Green

136

Page 137: Obfuscation, Golfing and Secret Operators in Perl

Thank you

137