Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Polyglot for jscript in batch
0</* :hello
@ECHO Hello, batch!
@cscript /nologo /E:jscript %~f0 %*
@goto :EOF
*/0;
WScript.Echo('Hello, jscript!');
Hello, batch!
Hello, jscript!

Windows lacks anything quite like Unix's shebang, which provides a way for a script to tell its interpreter how to interpret it, but polyglots can be used to provide similar functionality.

A polyglot is a script that works in multiple interpreters, hiding functionality and syntax specific to one language from another language's interpreter by clever use of each language/interpreter's specific features.

Let's look at how each of our two interpreters deal with the above script:

  • BATCH allows redirection to occur anywhere in a command line, not just a the tail. It does this by actually rearranging the command in pre-parsing, turning our first line 0<*/ :hello into :hello 0<*/, which is a label and is not executed. Line 2 is a hello from batch, Line 3 executes the called script in cscript with the appropriate arguments (subtitute your interpreter here), and line 4 sends us to :EOF, ignoring the rest of the script.

  • JAVASCRIPT: Lines 1-5 are reduced to a single instruction once the multi-line comment is ignored: 0<0;. This adds minimal weight and no memory overhead, and the script continues as normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.