Settings

Theme

Ask HN: Any ideas what code this is?

4 points by ahmett 11 years ago · 5 comments · 1 min read


    $_='^#(/||/@!@[{@:^[-['^";@@@\\>])@.".
    "{)/];)^{";$,+=(++$,);$_.=">&$,";`$_`;
Saw it in a colleague's email signature. Seemed like Perl to me but I don't know Perl at all, certainly not at this level. So any ideas?
jcr 11 years ago

Original Code

  $_='^#(/||/@!@[{@:^[-['^";@@@\\>])@.".
  "{)/];)^{";$,+=(++$,);$_.=">&$,";`$_`;
$ man perlop

= Assignment Operator.

. Concatenation Operator.

.= Concatenate and Assign.

^ Binary XOR. The binary "^" and "|" operators have lower precedence than relational operators like concatenate.

' Text between single quotes is an uninterpreted string.

" Text between double quotes is an interpreted string.

; Statement terminator, just like C, java, javascript, ...

$ man perlvar

$_ The default input and pattern-searching space. A lot of perl code operates on this variable by default.

$, The output field separator for the print operator. If defined, this value is printed between each of print's arguments. The default is "undef".

The first statement is:

$_='^#(/||/@!@[{@:^[-['^";@@@\\>])@."."{)/];)^{";

String1: '^#(/||/@!@[{@:^[-['

String2: ";@@@\\>])@."

String3: "{)/];)^{"

So we get:

Result = String1 XOR String2 CONCAT String3

Since XOR has a lower precedence than Concatenate, we Concatenate first and then do the XOR.

  # First String:
  $_='^#(/||/@!@[{@:^[-[';
  printf "HEX1: %*v2.2X\n", ' ', $_;
  print  "STR1: " . $_ . "\n";
  $str1 = $_;
  # OUTPUT:
  # HEX1: 5E 23 28 2F 7C 7C 2F 40 21 40 5B 7B 40 3A 5E 5B 2D 5B
  # STR1: ^#(/||/@!@[{@:^[-[

  # Second String:
  $_=";@@@\\>])@.";
  printf "HEX2: %*v2.2X\n", ' ', $_;
  print  "STR2: " . $_ . "\n";
  $str2 = $_;
  # OUTPUT:
  # HEX2: 3B 40 40 40 5C 3E 5D 29 40 2E
  # STR2: ;@@@\>])@.

  # Third String:
  $_="{)/];)^{";
  printf "HEX3: %*v2.2X\n", ' ', $_;
  print  "STR3: " . $_ . "\n";
  $str3 = $_;
  # OUTPUT:
  # HEX3: 7B 29 2F 5D 3B 29 5E 7B
  # STR3: {)/];)^{

  # Since the "^" binary XOR operator has lower precedence
  # than the "." concatenation operator, XOR str2 and str3 to
  # get the Fourth String:
  $_ = $str2 . $str3;
  printf "HEX4: %*v2.2X\n", ' ', $_;
  print  "STR4: " . $_ . "\n";
  $str4 = $_;
  # OUTPUT:
  # HEX4: 3B 40 40 40 5C 3E 5D 29 40 2E 7B 29 2F 5D 3B 29 5E 7B
  # STR4: ;@@@\>])@.{)/];)^{

  # Now we can do a binary XOR on Sring #1 and String #4 to
  # get Fifth String:
  $_ = $str1 ^ $str4;
  printf "HEX5: %*v2.2X\n", ' ', $_;
  print  "STR5: " . $_ . "\n";
  $str5 = $_;
  # OUTPUT:
  # HEX5: 65 63 68 6F 20 42 72 69 61 6E 20 52 6F 67 65 72 73 20
  # STR5: echo Brian Rogers

  # This is wickedly bad juju. The "(++$,)" portion increments an
  # undefined variable and would normally be an error, but with errors and
  # warnings shut off, it increments an undefined variable to 1, then adds
  # it to itself with "+=" to get 2.
  $,+=(++$,);

  # Here we concatenate shell redirection to stderr, which is file
  # descriptor 2, with the usual ">&2" since "$," now equals 2.
  $_.=">&$,";
  print  "\n";
  printf "HEX-: %*v2.2X\n", ' ', $_;
  print  "STR-: " . $_ . "\n";
  # OUTPUT:
  # HEX-: 65 63 68 6F 20 42 72 69 61 6E 20 52 6F 67 65 72 73 20 3E 26 32
  # STR-: echo Brian Rogers >&2

  # Finally, the statement is evaluated in the shell:
  `$_`;
Note: You should ask your friend Brian Rodgers if he always writes his name to Standard ERROR! ;)
mc_hammer 11 years ago

    $_ = 'somestring';
    `$_`;     // backticks are shell or exec(), $_ is the previous string.
type echo "'^#(/||/@!@[{@:^[-['^";@@@\\>])@."."{)/];)^{";$,+=(++$,);$_.=">&$," to see what it does

;$,+=(++$,); looks like a for loop

looks like php to me - backticks work in perl, bash and php

edoceo 11 years ago

The $ vars lead me to either Shell, Perl or PHP.

$_ is a special var in Bash and Perl.

The '.' char is for string concat in Perl and PHP

So, I think this is perl.

The first statement evaluates to: "echo Brian Rogers >&2" The second statement `$_` runs that command.

The end result is that this line of code prints:

Brian Rogers

andor 11 years ago

It's Perl. The last `$_`; evals what's in $_, so change it to just print the variable:

  $_='^#(/||/@!@[{@:^[-['^";@@@\\>])@.".
  "{)/];)^{";$,+=(++$,);$_.=">&$,";
  print "$_";
The output is:

  echo Brian Rogers >&2
psophis 11 years ago

Look like a regular expression.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection