Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to (ab)use sort

by Grimy (Pilgrim)
on Apr 17, 2012 at 08:56 UTC ( #965457=obfuscated: print w/replies, xml ) Need Help??

sort is, like, the single most abusable function in all of Perl. Seriously, side effects in the sorting code!
use strict; use warnings;()= sort{index+0x47,&$a($b)}sub{print&{;pop}}, sort{sub J{$!++&1}[0]}sub{exec''},map{@$_} sort{sub _{[(pop)x((1<<$!-->>47)|1)]}!{()= sort{my$_=$a;$a=_(sub{$_})if!/0/}$a,$b,0}} sort J('rke%ca hlrPe rhetoan tus;J')=~/./g
Obviously, this code depends on the internal, unspecified implementation of sort. I only tested it on Strawberry Perl 5.14.2 (64 bits); please let me know whether it works anywhere else. Edit: minor code improvements.

Replies are listed 'Best First'.
Re: How to (ab)use sort
by moritz (Cardinal) on Apr 17, 2012 at 09:04 UTC
    $ perl -v|head -n2 This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-li +nux $ perl obfu.pl Just another Perl hacker,Unknown warnings category 'CODE(0x1151998)' a +t obfu.pl line 6 BEGIN failed--compilation aborted at obfu.pl line 9.

    (with a pretty long pause after Just another Perl hacker,).

      Similar behaviour (plus a zero printed) in 5.10.1 for i486-linux-gnu-thread-multi.
        Thanks for the feedback :) Well, at least it prints what it's supposed to print before dying. s/use warnings/use warnings;@;=/ should fix this (both versions work with Strawberry, and the @;= version sounds less like an exploit).
Re: How to (ab)use sort
by biohisham (Priest) on Apr 22, 2012 at 16:33 UTC
    bioslayer@bioslayer:~/Desktop/Development/test$ perl - use strict; use warnings;()= sort{index+0x47,&$a($b)}sub{print&{;pop}}, sort{sub J{$!++&1}[0]}sub{exec''},map{@$_} sort{sub _{[(pop)x((1<<$!-->>47)|1)]}!{()= sort{my$_=$a;$a=_(sub{$_})if!/0/}$a,$b,0}} sort J('rke%ca hlrPe rhetoan tus;J')=~/./g __END__ Just another Perl hacker0bioslayer@bioslayer:~/Desktop/Development/tes +t$
    Works as advertised, printing an extra 0 at the trail.
    ##Platform## Perl 5, version 12, subversion 4 (v5.12.4) built for i686-linux-gnu-th +read-multi-64int

    David R. Gergen said "We know that second terms have historically been marred by hubris and by scandal." and I am a two y.o. monk today :D, June,12th, 2011...
Re: How to (ab)use sort
by kcott (Canon) on Apr 23, 2012 at 00:15 UTC

    G'day Grimy,

    I'm getting a different result under Mac OS X:

    ken@ganymede: ~/tmp $ cat pm_sort_abuse.pl use strict; use warnings;()= sort{index+0x47,&$a($b)}sub{print&{;pop}}, sort{sub J{$!++&1}[0]}sub{exec''},map{@$_} sort{sub _{[(pop)x((1<<$!-->>47)|1)]}!{()= sort{my$_=$a;$a=_(sub{$_})if!/0/}$a,$b,0}} sort J('rke%ca hlrPe rhetoan tus;J')=~/./g ken@ganymede: ~/tmp $ perl pm_sort_abuse.pl Can't use string ("J") as an ARRAY ref while "strict refs" in use at p +m_sort_abuse.pl line 4. ken@ganymede: ~/tmp $ perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-th +read-multi-2level ...

    -- Ken