News for hackers. Stuff that matters.
Re:Writing a Polyglot Script by rcthompson on Saturday January 25, 2020 @02:12PM (#22147996)
Not exactly a polyglot script, but a while back I had to work on a cluster whose job management system always ran everything as shell scripts. Or rather, it had to be run as a shell script in order to set the right cluster options. I wanted to submit an R script, so what did I do? I wrote a file that was both a bash script and an R script. When run as an R script, it does what I want it to do. When run as a bash script, it runs itself as an R script (after setting the cluster options). Problem "solved".

Here is is, if anyone's interested: https://gist.github.com/DarwinAwardWinner/2eb3705fb6ddab08ee...

And here's the Python version: https://gist.github.com/DarwinAwardWinner/8a631d897d45f00552...

Re:Writing a Polyglot Script by fwip on Saturday January 25, 2020 @06:35PM (#22149347)
Wouldn't it have been easier to submit:

    #!/bin/bash
    Rscript myscript.R
Re:Writing a Polyglot Script by shakna on Saturday January 25, 2020 @07:28PM (#22149575)
They may have only been able to submit the single file to the system. (In which case I would have generated the Rscript to a tmpfile, but maybe that wasn't allowed either.)
Re:Writing a Polyglot Script by rcthompson on Saturday January 25, 2020 @08:01PM (#22149701)
No, I totally could have written a separate R script that was then run by the bash script. I just wanted to have it all in one file, and I thought it was a fun challenge figuring out how to do it.
Re:Writing a Polyglot Script by niedzielski on Saturday January 25, 2020 @07:06PM (#22149469)
I've been collected hacks for a couple languages and tools here: https://git.io/shebang
Re:Writing a Polyglot Script by rcthompson on Tuesday January 28, 2020 @12:24AM (#22166689)
I think the difference with mine is that I actually need to squeeze some shell code in there as well, to set up the cluster environment before "handing off" to R/Python.
Re:Writing a Polyglot Script by SeekingMeaning on Saturday January 25, 2020 @01:32PM (#22147753)
Slightly modified so that it runs either a Ruby file or a Python one:

  (0 and eval(IO.read("ruby.rb"))) or eval(compile(open("python.py").read(), "", "exec"))
Granted, this doesn’t close the Python file, which is delayed until garbage collection or program termination.
Re:Writing a Polyglot Script by coolreader18 on Sunday January 26, 2020 @12:20AM (#22150522)
You could also do `__import__("/abs/path/to/python.py")` (I think, I'm not completely sure on the rules for passing paths to `__import__`)
Re:Writing a Polyglot Script by blowski on Saturday January 25, 2020 @02:51PM (#22148244)

  /*<?php
  //*/public class PhpJava { public static void main(String[] args) { System.out.printf("/*%s",
  //\u000A\u002F\u002A
  class PhpJava {
      static function main() {
          echo(//\u000A\u002A\u002F
          "Hello World!");
  }}
  //\u000A\u002F\u002A
  PhpJava::main();
  //\u000A\u002A\u002F
It's both valid PHP and Java.

Not mine, stolen from https://gist.github.com/forairan/b1143f42883b3b0ee1237bc9bd0....

Re:Writing a Polyglot Script by clemParis on Saturday January 25, 2020 @05:29PM (#22149008)
On a similar topic, PoC||GTFO [0] features files that are valid with multiple file formats. Issue 19 [1], for instance, "is valid as a PDF document, a ZIP archive, and a HTMLpage. It is also available as a Windows PE executable, a PNG image and an MP4 video" (see "technical note"). They are made by Ange Albertini [2] who has a great talk [3] on how he does this kind of things

[0] https://www.alchemistowl.org/pocorgtfo/ [1] https://www.alchemistowl.org/pocorgtfo/pocorgtfo19.pdf [2] https://github.com/angea/pocorgtfo [3] https://www.youtube.com/watch?v=W9WnnMJ0RKg

Re:Writing a Polyglot Script by mathnmusic on Saturday January 25, 2020 @03:28PM (#22148450)
What I would like to see is a way to use multiple languages within the same script, to use the different abilities of different languages. For eg: Query a database via Ruby using ActiveRecord, then do some number crunching in Python using numPy or Pandas, and finally plot some statistical charts using R.

Jupyter's multiple language kernels come close to this, but not quite.

Re:Writing a Polyglot Script by fniephaus on Saturday January 25, 2020 @03:39PM (#22148493)
You can do this with GraalVM. Here's an example: https://github.com/graalvm/graalvm-demos/tree/master/polyglo...

And here's a polyglot notebook example: https://fniephaus.com/2019/px19-polyglot-notebooks.pdf

And finally, here's our GraalVM-powered Jupyter kernel: https://github.com/hpi-swa/ipolyglot

Re:Writing a Polyglot Script by roywiggins on Saturday January 25, 2020 @01:45PM (#22147828)
Bonus points if someone makes it into a polyglot quine somehow
Re:Writing a Polyglot Script by jrimbault on Saturday January 25, 2020 @02:20PM (#22148048)
Re:Writing a Polyglot Script by SeekingMeaning on Saturday January 25, 2020 @04:04PM (#22148604)
Behold my masterpiece

  p='"%s"';r="'%s'";_='p=%s;r=%s;_=%s;print(_%%((0and[r%%p,p%%r,r%%_])or(tuple([r%%p,p%%r,r%%_]))))';print(_%((0and[r%p,p%r,r%_])or(tuple([r%p,p%r,r%_]))))
with inspiration from https://stackoverflow.com/q/6223285 and https://stackoverflow.com/a/2475931
Re:Writing a Polyglot Script by anonsivalley652 on Sunday January 26, 2020 @03:00AM (#22151030)
Almost since doesn't quite work in Ruby:

Evaluating:

    p='"%s"';r="'%s'";_='p=%s;r=%s;_=%s;print(_%%((0and[r%%p,p%%r,r%%_])or(tuple([r%%p,p%%r,r%%_]))))';print(_%((0and[r%p,p%r,r%_])or(tuple([r%p,p%r,r%_]))))
becomes:

    p='"%s"';r="'%s'";_='p=%s;r=%s;_=%s;print(_%%((0and[r%%p,p%%r,r%%_])or(tuple([r%%p,p%%r,r%%_]))))'%_]))))

Too many '%'s in some places
Re:Writing a Polyglot Script by SeekingMeaning on Sunday January 26, 2020 @03:04PM (#22154377)
Wait, it doesn’t? What’s your setup?
Re:Writing a Polyglot Script by vbrandl on Saturday January 25, 2020 @03:13PM (#22148368)
In 2011 a PoC virus was described, that was a polyglot JS, VBS, Batch and MatLab: https://github.com/SPTHvx/SPTH/blob/master/articles/files/xs...