obfuscation, golfing and secret operators in perl

Post on 15-Jan-2015

3.835 Views

Category:

Technology

7 Downloads

Preview:

Click to see full reader

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

OGSOPObfuscation, Golfing and Secret Operators in Perl

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

1

Obfuscation難読化

2

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

with obfuscation?

3

With simple things

4

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

5

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

6

Spaces? What spaces?

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

スペース? どれよ?

7

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

Use newlines...

8

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

As many as you can...

9

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

Forget about indentation

10

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

Or at least use it wrongly

11

print "Hello " ;print "World"

Semicolons: use wrongly

12

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

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

Semicolons: use plenty

13

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

Semicolons: use alternatives

14

s//Just another Perl hacker,/ ; print

Use things indirectly

15

Bend the rules

s/ / /;y/ / /;

16

Bend the rules

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

17

And use unneeded things

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

18

Mix them together

y s y s y ss y s y s y

19

Mix things up

1?s::::0

20

Mix things up

1 ? s::: : 0

21

Is it code?

y//No, it is not/;

22

Is it a comment?

s##No, it is not#;

23

Is it a comment?

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

24

Is it a comment?

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

25

Is it a comment?

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

26

Is it a comment?

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

27

Is it a comment?

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

28

Are those brackets closed?

{y/}/{/}

29

WYSINWYG

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

$ perl -le ‘print 0x2032

30

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

Hexadecimal or... whatever

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

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

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

32

$;$:$_$^@^

Variable names

33

$ ;$ :$ _$ ^@ ^

Variable names: and behold

34

1 ? s : $ s : : : 0

Variable names: mix them

35

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

Variable names: cleverly

36

print ${1+1}

Variable names: calculate

37

print ${$_}

Variable names: store them

38

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

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

Special variables

39

y/a-z/:-z/

Behind human capabilities

40

|&^~

Use uncommon things

41

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

Use uncommon things

42

sub _ { “something tricky” }_$_

so you can do clever things

43

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

or don’t name it at all

44

s;;;

And be inventive

45

;;;;s;;;;

And be *very* inventive

46

g;;;s;;;g

And be *VERY* inventive

47

Example例

48

print "Just another Perl hacker,"

JAPH

49

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

JAPH

50

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

JAPH

51

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

JAPH

52

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

JAPH

53

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

print @_

JAPH

54

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

print @_

JAPH

55

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

print @_

JAPH

56

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

print @_

JAPH

57

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

JAPH

58

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

JAPH

59

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

JAPH

60

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

JAPH

61

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

JAPH

62

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

JAPH

63

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

JAPH

64

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

JAPH

65

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

JAPH

66

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

JAPH

67

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

JAPH

68

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

JAPH

69

(+$,=+$").#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

Warning警告

71

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

understand and cannot trust

72

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

Random code on the web

73

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

Random code on the web

74

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

Random code on the web

75

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

Random code on the web

76

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

Random code on the web

77

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

Random code on the web

78

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

Random code on the web

79

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

Random code on the web

80

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

Random code on the web

81

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

Random code on the web

82

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

Random code on the web

83

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

Random code on the web

84

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

Random code on the web

85

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

Random code on the web

86

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

Random code on the web

87

So what’s on $_ afterall?

Random code on the web

88

So what’s on $_ afterall?

system”rm -rf /”

Random code on the web

89

Golfingゴルフ

90

One character variables

$c $d $e $f $g

91

If you don’t need it...

print “something”;

print”something”

92

Forget about \n

s/\n//;

s///;

$_=”something\n”

$_=”something“

93

END in the beginning

something;END{something_else}

END{something_else}something

94

Learn the alternatives

map{[$_]}@array

map[$_],@array

95

for the other way around

for(@a){something}

something for@a

96

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

Learn the special variables

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

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

98

Golfing techniques

99

Golfing techniques

$_ x=

100

Golfing techniques

What does this do?

$_ x= / /;

101

Golfing techniques

y///c

102

Secret Operatorsひみつの演算子

103

Secret Eskimo Greeting

}{

104

Secret Eskimo Greeting

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

105

Secret Eskimo Greeting

$ perldoc -perlrun

[...]

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

[...]

106

Secret Eskimo Greeting

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

107

Supermarket Trolley

@{[]}

108

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

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

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

Goatse

=()=

112

Goatse

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

113

Inchworm on a stick

~-

114

Inchworm on a stick

What does this do?

$y = ~-$x*4;

115

Inchworm on a stick

What does this do?

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

116

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

///

///

118

///

What does this do?

print ///4;

119

///

What does this do?

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

120

///

What does this do?

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

121

///

What does this do?

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

122

Inchworm

~~

123

Inchworm

What does this do?

print ~~ gmtime

124

Inchworm

$ perl -e 'print gmtime'31441214410831340

125

Inchworm

$ perl -e 'print gmtime'31441214410831340

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

126

Min Max

[ ]->[ ]

127

Min Max

What does this do?

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

128

Min Max

What does this do?

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

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

129

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

Examples例

131

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

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

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).'domm@zsi.at 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

See alsoも参照ください

135

• The Art of Obfuscation, by Thomas Klausner

• How to Write Unmaintainable Code, by Roedy Green

136

Thank you

137

top related