Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obfuscation Puzzle: Can you figure out what this Perl function does? [closed]

sub foo {[$#{$_[!$||$|]}*@{$_[!!$_^!$_]}?@{$_[!$..!!$.]}[$_[@--@+]%
@{$_[$==~/(?=)//!$`]}..$#{$_[$??!!$?:!$?]},($)?!$):!!$))..$_[$--$-]%@{
$_[$]/$]]}-(!!$++!$+)]:@{$_[!!$^^^!$^^]}]}

update: I thought the word "puzzle" would imply this, but: I know what it does - I wrote it. If the puzzle doesn't interest you, please don't waste any time on it.

like image 627
ysth Avatar asked Sep 19 '08 06:09

ysth


1 Answers

Here is how you figure out how to de-obfuscate this subroutine.

Sorry for the length

First let's tidy up the code, and add useful comments.

sub foo {
  [
    (
      # ($#{$_[1]})
      $#{
        $_[
          ! ( $| | $| )
          # $OUTPUT_AUTOFLUSH === $|
          # $| is usually 0
          # ! ( $| | $| )
          # ! (  0 |  0 )
          # ! (  0 )
          # 1
        ]
      }

      *

      # @{$_[1]}
      @{
        $_[
          !!$_ ^ !$_

          # !! 1 ^ ! 1
          # !  0 ^   0
          #    1 ^   0
          # 1

          # !! 0 ^ ! 0
          # !  1 ^   1
          #    0 ^   1
          # 1
        ]
      }
    )

    ?


    # @{$_[1]}
    @{
      $_[
        !$. . !!$.
        # $INPUT_LINE_NUMBER === $.
        # $. starts at 1
        # !$. . !!$.
        # ! 1 . !! 1
        #   0 . ! 0
        #   0 . 1
        #   01
      ]
    }

    [
      # $_[0]
      $_[
        # @LAST_MATCH_START - @LAST_MATCH_END
        # 0
        @- - @+
      ]

      %


      # @{$_[1]}
      @{
        $_[
          $= =~ /(?=)/ / !$` #( fix highlighting )`/
          # $= is usually 60
          # /(?=)/ will match, returns 1
          # $` will be ''
          # 1 / ! ''
          # 1 / ! 0
          # 1 / 1
          # 1
        ]
      }

      ..

      # $#{$_[1]}
      $#{
        $_[
          $? ? !!$? : !$?

          # $CHILD_ERROR === $?
          # $? ? !!$? : !$?

          #  0 ? !! 0 : ! 0
          #  0 ?    0 :   1
          # 1

          #  1 ? !! 1 : ! 1
          #  1 ?    1 :   0
          # 1
        ]
      }

      ,

      # ( 0 )
      (
        $) ? !$) : !!$)

        # $EFFECTIVE_GROUP_ID === $)

        # $) ? !$) : !!$)

        #  0 ? ! 0 : !! 0
        #  0 ?   1 :    0
        # 0

        #  1 ? ! 1 : !! 1
        #  1 ?   0 :    1
        # 0
      )

      ..

      # $_[0]
      $_[
        $- - $- # 0

        # $LAST_PAREN_MATCH = $-

        # 1 - 1 == 0
        # 5 - 5 == 0
      ]

      %

      # @{$_[1]}
      @{
        $_[
          $] / $]
          # $] === The version + patchlevel / 1000 of the Perl interpreter.

          # 1 / 1 == 1
          # 5 / 5 == 1
        ]
      }

      -

      # ( 1 )
      (
        !!$+ + !$+

        # !! 1 + ! 1
        # !  0 + 0
        #    1 + 0
        # 1
      )
    ]

    :

    # @{$_[1]}
    @{
      $_[
        !!$^^ ^ !$^^

        # !! 1 ^ ! 1
        # !  0 ^   0
        #    1 ^   0
        # 1

        # !! 0 ^ ! 0
        # !  1 ^ 1
        #    0 ^ 1
        # 1
      ]
    }
  ]
}

Now let's remove some of the obfuscation.

sub foo{
  [
    (
      $#{$_[1]} * @{$_[1]}
    )

    ?

    @{$_[1]}[
      ( $_[0] % @{$_[1]} ) .. $#{$_[1]}

      ,

      0 .. ( $_[0] % @{$_[1]} - 1 )
    ]

    :

    @{$_[1]}
  ]
}

Now that we have some idea of what is going on, let's name the variables.

sub foo{
  my( $item_0, $arr_1 ) = @_;
  my $len_1  = @$arr_1;

  [
      # This essentially just checks that the length of $arr_1 is greater than 1
      ( ( $len_1 -1 ) * $len_1 )
      # ( ( $len_1 -1 ) * $len_1 )
      # ( (      5 -1 ) *      5 )
      #             4   *      5
      # 20
      # 20 ? 1 : 0 == 1

      # ( ( $len_1 -1 ) * $len_1 )
      # ( (      2 -1 ) *      2 )
      #             1   *      2
      # 2
      # 2 ? 1 : 0 == 1

      # ( ( $len_1 -1 ) * $len_1 )
      # ( (      1 -1 ) *      1 )
      #             0   *      1
      # 0
      # 0 ? 1 : 0 == 0

      # ( ( $len_1 -1 ) * $len_1 )
      # ( (      0 -1 ) *      0 )
      #            -1   *      0
      # 0
      # 0 ? 1 : 0 == 0

    ?

      @{$arr_1}[
        ( $item_0 % $len_1 ) .. ( $len_1 -1 ),
        0 .. ( $item_0 % $len_1 - 1 )
      ]

    :

      # If we get here, @$arr_1 is either empty or has only one element
      @$arr_1
  ]
}

Let's refactor the code to make it a little bit more readable.

sub foo{
  my( $item_0, $arr_1 ) = @_;
  my $len_1  = @$arr_1;

  if( $len_1 > 1 ){
    return [
      @{$arr_1}[
        ( $item_0 % $len_1 ) .. ( $len_1 -1 ),
        0 .. ( $item_0 % $len_1 - 1 )
      ]
    ];
  }elsif( $len_1 ){
    return [ @$arr_1 ];
  }else{
    return [];
  }
}
like image 114
Brad Gilbert Avatar answered Oct 01 '22 07:10

Brad Gilbert