Polyglot (computing) explained

In computing, a polyglot is a computer program or script (or other file) written in a valid form of multiple programming languages or file formats.[1] The name was coined by analogy to multilingualism. A polyglot file is composed by combining syntax from two or more different formats.[2]

When the file formats are to be compiled or interpreted as source code, the file can be said to be a polyglot program, though file formats and source code syntax are both fundamentally streams of bytes, and exploiting this commonality is key to the development of polyglots.[3] Polyglot files have practical applications in compatibility,[4] but can also present a security risk when used to bypass validation or to exploit a vulnerability.

History

Polyglot programs have been crafted as challenges and curios in hacker culture since at least the early 1990s. A notable early example, named simply polyglot was published on the Usenet group rec.puzzles in 1991, supporting 8 languages, though this was inspired by even earlier programs.[5] In 2000, a polyglot program was named a winner in the International Obfuscated C Code Contest.[6]

In the 21st century, polyglot programs and files gained attention as a covert channel mechanism for propagation of malware.[3] [7] Polyglot files have practical applications in compatibility,[8]

Construction

A polyglot is composed by combining syntax from two or more different formats, leveraging various syntactic constructs that are either common between the formats, or constructs that are language specific but carrying different meaning in each language. A file is a valid polyglot if it can be successfully interpreted by multiple interpreting programs. For example, a PDF-Zip polyglot might be opened as both a valid PDF document and decompressed as a valid zip archive. To maintain validity across interpreting programs, one must ensure that constructs specific to one interpreter are not interpreted by another, and vice versa.This is often accomplished by hiding language-specific constructs in segments interpreted as comments or plain text of the other format.[1]

Examples

C, PHP, and Bash

Two commonly used techniques for constructing a polyglot program are to make use of languages that use different characters for comments, and to redefine various tokens as others in different languages. These are demonstrated in this public domain polyglot written in ANSI C, PHP and bash:Highlighted for Bash #define a /* # /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include #define main int main(void) #define printf printf(#define true) #define function function main #define c /* main #*/Highlighted for PHP #define a /* # /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include #define main int main(void) #define printf printf(#define true) #define function function main #define c /* main #*/Highlighted for C #define a /* # /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include #define main int main(void) #define printf printf(#define true) #define function function main #define c /* main #*/

Note the following:

SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL

The following is written simultaneously in SNOBOL4, Win32Forth, PureBasicv4.x, and REBOL:

Highlighted for SNOBOL *BUFFER : A.A ; .(Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole : PrintN("Hello, world !") Repeat : Until Inkey : Macro SomeDummyMacroHere REBOL [Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on !Highlighted for Forth *BUFFER : A.A ; .(Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole : PrintN("Hello, world !") Repeat : Until Inkey : Macro SomeDummyMacroHere REBOL [Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on !Highlighted for BASIC *BUFFER : A.A ; .(Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole : PrintN("Hello, world !") Repeat : Until Inkey : Macro SomeDummyMacroHere REBOL [Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on !Highlighted for REBOL *BUFFER : A.A ; .(Hello, world !) @ To Including? Macro SkipThis; OUTPUT = Char(10) "Hello, World !" ;OneKeyInput Input('Char', 1, '[-f2-q1]') ; Char End; SNOBOL4 + PureBASIC + Win32Forth + REBOL = <3 EndMacro: OpenConsole : PrintN("Hello, world !") Repeat : Until Inkey : Macro SomeDummyMacroHere REBOL [Title: "'Hello, World !' in 4 languages" CopyLeft: "Developed in 2010 by Society" ] Print "Hello, world !" EndMacro: func [][] set-modes system/ports/input [binary: true] Input set-modes system/ports/input [binary: false] NOP:: EndMacro ; Wishing to refine it with new language ? Go on !

DOS batch file and Perl

The following file runs as a DOS batch file, then re-runs itself in Perl:

Highlighted for DOS batch @rem = ' --PERL-- @echo off perl "%~dpnx0" %* goto endofperl @rem '; #!perl print "Hello, world!\n"; __END__ :endofperlHighlighted for Perl @rem = ' --PERL-- @echo off perl "%~dpnx0" %* goto endofperl @rem '; #!perl print "Hello, world!\n"; __END__ :endofperl

This allows creating Perl scripts that can be run on DOS systems with minimal effort. Note that there is no requirement for a file to perform exactly the same function in the different interpreters.

Types

Polyglot types include:

Benefits

Polyglot markup

Polyglot markup has been proposed as a useful combination of the benefits of HTML5 and XHTML.[9] Such documents can be parsed as either HTML (which is

SGML-compatible) or XML, and will produce the same DOM structure either way. For example, in order for an HTML5 document to meet these criteria, the two requirements are that it must have an HTML5 doctype, and be written in well-formed XHTML. The same document can then be served as either HTML or XHTML, depending on browser support and MIME type.

As expressed by the html-polyglot recommendation, to write a polyglot HTML5 document, the following key points should be observed:

  1. Processing instructions and the XML declaration are both forbidden in polyglot markup
  2. Specifying a document’s character encoding
  3. The DOCTYPE
  4. Namespaces
  5. Element syntax (i.e. End tags are not optional. Use self-closing tags for void elements.)
  6. Element content
  7. Text (i.e. pre and textarea should not start with newline character)
  8. Attributes (i.e. Values must be quoted)
  9. Named entity references (i.e. Only amp, lt, gt, apos, quot)
  10. Comments (i.e. Use <!-- syntax -->)
  11. Scripting and styling polyglot markup

The most basic possible polyglot markup document would therefore look like this: The title element must not be empty.

In a polyglot markup document non-void elements (such as script, p, div) cannot be self-closing even if they are empty, as this is not valid HTML.[10] For example, to add an empty textarea to a page, one cannot use <nowiki><textarea/></nowiki>, but has to use <nowiki><textarea></textarea></nowiki> instead.

Composing formats

The DICOM medical imaging format was designed to allow polyglotting with TIFF files, allowing efficient storage of the same image data in a file that can be interpreted by either DICOM or TIFF viewers.[11]

Compatibility

The Python 2 and Python 3 programming languages were not designed to be compatible with each other, but there is sufficient commonality of syntax that a polyglot Python program can be written than runs in both versions.[12]

Security implications

A polyglot of two formats may steganographically compose a malicious payload within an ostensibly benign and widely accepted wrapper format, such as a JPEG file that allows arbitrary data in its comment field. A vulnerable JPEG renderer could then be coerced into executing the payload, handing control to the attacker. The mismatch between what the interpreting program expects, and what the file actually contains, is the root cause of the vulnerability.

SQL Injection is a trivial form of polyglot, where a server naively expects user-controlled input to conform to a certain constraint, but the user supplies syntax which is interpreted as SQL code.

Note that in a security context, there is no requirement for a polyglot file to be strictly valid in multiple formats; it is sufficient for the file to trigger unintended behaviour when being interpreted by its primary interpreter.

Highly flexible or extensible file formats have greater scope for polyglotting, and therefore more tightly constrained interpretation offers some mitigation against attacks using polyglot techniques. For example, the PDF file format requires that the magic number %PDF appears at byte offset zero, but many PDF interpreters waive this constraint and accept the file as valid PDF as long as the string appears within the first 1024 bytes. This creates a window of opportunity for polyglot PDF files to smuggle non-PDF content in the header of the file. The PDF format has been described as "diverse and vague", and due to significantly varying behaviour between different PDF parsing engines, it is possible to create a PDF-PDF polyglot that renders as two entirely different documents in two different PDF readers.[13]

Detecting malware concealed within polyglot files requires more sophisticated analysis than relying on file-type identification utilities such as file. In 2019, an evaluation of commercial anti-malware software determined that several such packages were unable to detect any of the polyglot malware under test.

In 2019, the DICOM medical imaging file format was found to be vulnerable to malware injection using a PE-DICOM polyglot technique.[14] The polyglot nature of the attack, combined with regulatory considerations, led to disinfection complications: because "the malware is essentially fused to legitimate imaging files", "incident response teams and A/V software cannot delete the malware file as it contains protected patient health information".[15]

GIFAR attack

A Graphics Interchange Format Java Archives (GIFAR) is a polyglot file that is simultaneously in the GIF and JAR file format.[16] This technique can be used to exploit security vulnerabilities, for example through uploading a GIFAR to a website that allows image uploading (as it is a valid GIF file), and then causing the Java portion of the GIFAR to be executed as though it were part of the website's intended code, being delivered to the browser from the same origin.[17] Java was patched in JRE 6 Update 11, with a CVE published in December 2008.[18] [19]

GIFARs are possible because GIF images store their header in the beginning of the file, and JAR files (as with any ZIP archive-based format) store their data at the end.[20]

Related terminology

See also

External links

Notes and References

  1. Book: Jonas Magazinius . Billy K. Rios . Andrei Sabelfeld . Proceedings of the 2013 ACM SIGSAC conference on Computer & communications security - CCS '13 . Polyglots . 2013-11-04 . 753–764 . 10.1145/2508859.2516685 . 9781450324779 . 16516484 . https://dl.acm.org/doi/10.1145/2508859.2516685 . 5 September 2022 . 5 September 2022 . https://web.archive.org/web/20220905003825/https://dl.acm.org/doi/10.1145/2508859.2516685 . live .
  2. 2012.09214. Bridges . Robert A. . Oesch . Sean . Verma . Miki E. . Iannacone . Michael D. . Huffer . Kelly M. T. . Jewell . Brian . Nichols . Jeff A. . Weber . Brian . Beaver . Justin M. . Smith . Jared M. . Scofield . Daniel . Miles . Craig . Plummer . Thomas . Daniell . Mark . Tall . Anne M. . Beyond the Hype: An Evaluation of Commercially Available Machine-Learning-Based Malware Detectors . Digital Threats: Research and Practice . 2023 . 4 . 2 . 1–22 . 10.1145/3567432 . 247218744 .
  3. 2203.07561. Koch . Luke . Oesch . Sean . Adkisson . Mary . Erwin . Sam . Weber . Brian . Chaulagain . Amul . Toward the Detection of Polyglot Files . 2022 . cs.CR .
  4. Web site: Benefits of polyglot XHTML5 . 4 September 2022 . 12 October 2011 . https://web.archive.org/web/20111012190748/http://xmlplease.com/xhtml/xhtml5polyglot/ . dead .
  5. Web site: Polyglot: A program in eight languages . 6 September 2022 . 6 September 2022 . https://web.archive.org/web/20220906233052/https://ideology.com.au/polyglot/ . live .
  6. Web site: 15th International Obfuscated C Code Contest (2000) . 6 September 2022 . 6 September 2022 . https://web.archive.org/web/20220906233051/https://www.ioccc.org/years.html#2000_tomx . live .
  7. 2407.01529. Koch . Luke . Oesch . Sean . Chaulagain . Amul . Dixon . Jared . Dixon . Matthew . Huettal . Mike . Sadovnik . Amir. Watson . Cory. Weber. Brian . Hartman. Jacob. Patulski. Richard . On the Abuse and Detection of Polyglot Files . 2024 . cs.CR .
  8. Web site: Benefits of polyglot XHTML5 . 4 September 2022 . 12 October 2011 . https://web.archive.org/web/20111012190748/http://xmlplease.com/xhtml/xhtml5polyglot/ . dead .
  9. Web site: Polyglot Markup: A robust profile of the HTML5 vocabulary . 4 September 2022 . 9 August 2022 . https://web.archive.org/web/20220809042911/https://www.w3.org/TR/html-polyglot/ . live .
  10. http://dev.w3.org/html5/html-xhtml-author-guide/html-xhtml-authoring-guide.html#empty-elements Polyglot Markup: HTML-Compatible XHTML Documents: 6.4 Void Elements
  11. Web site: DICOM-TIFF dual personality files . 5 September 2022 . 5 September 2022 . https://web.archive.org/web/20220905193806/https://learn.canceridc.dev/dicom/dicom-tiff-dual-personality-files . live .
  12. Web site: Schofield . Ed . Cheat Sheet: Writing Python 2-3 compatible code . 6 September 2022 . 6 September 2022 . https://web.archive.org/web/20220906225518/https://python-future.org/compatible_idioms.html . live .
  13. Web site: Wolf . Julia . OMG WTF PDF . 27th Chaos Communication Congress . 9 February 2011 . 6 September 2022 . 9 October 2022 . https://web.archive.org/web/20221009230207/https://fahrplan.events.ccc.de/congress/2010/Fahrplan/events/4221.en.html . live .
  14. DICOM Images Have Been Hacked! Now What? . American Journal of Roentgenology . April 2020 . 214 . 4 . 10.2214/AJR.19.21958 . 5 September 2022 . Desjardins . Benoit . Mirsky . Yisroel . Ortiz . Markel Picado . Glozman . Zeev . Tarbox . Lawrence . Horn . Robert . Horii . Steven C. . 727–735 . 31770023 . 208318324 . 5 September 2022 . https://web.archive.org/web/20220905193804/https://www.ajronline.org/doi/10.2214/AJR.19.21958 . live .
  15. Web site: Ubiquitous Bug Allows HIPAA-Protected Malware to Hide Behind Medical Images . 17 April 2019 . 5 September 2022 . 5 September 2022 . https://web.archive.org/web/20220905193806/https://threatpost.com/hipaa-protected-malware-medical-images/143890/ . live .
  16. Web site: Byrd . Christopher . How to Create a GIFAR . 6 March 2023 . 6 March 2023 . https://web.archive.org/web/20230306093631/https://www.riosec.com/articles/how-to-create-a-gifar . live .
  17. Web site: Eckel . Benjamin . The GIFAR Image Vulnerability . Hackaday . 5 August 2008 . 6 March 2023 . 6 March 2023 . https://web.archive.org/web/20230306091403/https://hackaday.com/2008/08/04/the-gifar-image-vulnerability/ . live .
  18. Web site: CVE-2008-5343 . cve.mitre.org . 20 April 2021 . 2008-12-04 . 20 April 2021 . https://web.archive.org/web/20210420204722/https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5343 . live .
  19. Web site: A photo that can steal your online credentials. Robert. McMillan. August 1, 2008. Infoworld.com. https://web.archive.org/web/20200918084425/https://www.infoworld.com/article/2653025/a-photo-that-can-steal-your-online-credentials.html. 2020-09-18.
  20. Web site: Rios . Billy . Billy (BK) Rios » SUN Fixes GIFARs . 20 April 2021 . https://web.archive.org/web/20160314083259/http://xs-sniper.com/blog/2008/12/17/sun-fixes-gifars/ . 14 March 2016 . 2008-12-17.
  21. Fjeldberg . Hans . 2008 . Polyglot Programming - A Business Perspective . M.Sc . Norwegian University of Science and Technology . 28 May 2015 . 4 March 2016 . https://web.archive.org/web/20160304053328/http://theuntitledblog.com/wp-content/uploads/2008/08/polyglot_programming-a_business_perspective.pdf . live .
  22. Web site: Analyzing Polyglot Microservices. Gupta. Tripta. 2018-12-19. Medium. en. 2019-08-05. 5 August 2019. https://web.archive.org/web/20190805144402/https://medium.com/capital-one-tech/analyzing-polyglot-microservices-f6f159a1a3e7. live.