<s>
In	O
computing	O
,	O
a	O
polyglot	O
is	O
a	O
computer	B-Application
program	I-Application
or	O
script	B-Language
written	O
in	O
a	O
valid	O
form	O
of	O
multiple	O
programming	O
languages	O
or	O
file	B-Application
formats	O
.	O
</s>
<s>
A	O
polyglot	O
file	B-Application
is	O
composed	O
by	O
combining	O
syntax	O
from	O
two	O
or	O
more	O
different	O
formats	O
.	O
</s>
<s>
When	O
the	O
file	B-Application
formats	O
are	O
to	O
be	O
compiled	B-Language
or	O
interpreted	B-Application
as	O
source	O
code	O
,	O
the	O
file	B-Application
can	O
be	O
said	O
to	O
be	O
a	O
polyglot	O
program	O
,	O
though	O
file	B-Application
formats	O
and	O
source	O
code	O
syntax	O
are	O
both	O
fundamentally	O
streams	O
of	O
bytes	O
,	O
and	O
exploiting	O
this	O
commonality	O
is	O
key	O
to	O
the	O
development	O
of	O
polyglots	O
.	O
</s>
<s>
A	O
notable	O
early	O
example	O
,	O
named	O
simply	O
polyglot	O
was	O
published	O
on	O
the	O
Usenet	B-Application
group	O
rec.puzzles	O
in	O
1991	O
,	O
supporting	O
8	O
languages	O
,	O
though	O
this	O
was	O
inspired	O
by	O
even	O
earlier	O
programs	O
.	O
</s>
<s>
In	O
2000	O
,	O
a	O
polyglot	O
program	O
was	O
named	O
a	O
winner	O
in	O
the	O
International	O
Obfuscated	O
C	B-Language
Code	O
Contest	O
.	O
</s>
<s>
A	O
file	B-Application
is	O
a	O
valid	O
polyglot	O
if	O
it	O
can	O
be	O
successfully	O
interpreted	B-Application
by	O
multiple	O
interpreting	O
programs	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
PDF-Zip	O
polyglot	O
might	O
be	O
opened	O
as	O
both	O
a	O
valid	O
PDF	O
document	O
and	O
decompressed	O
as	O
a	O
valid	O
zip	B-General_Concept
archive	I-General_Concept
.	O
</s>
<s>
To	O
maintain	O
validity	O
across	O
interpreting	O
programs	O
,	O
one	O
must	O
ensure	O
that	O
constructs	O
specific	O
to	O
one	O
interpreter	B-Application
are	O
not	O
interpreted	B-Application
by	O
another	O
,	O
and	O
vice	O
versa	O
.	O
</s>
<s>
This	O
is	O
often	O
accomplished	O
by	O
hiding	O
language-specific	O
constructs	O
in	O
segments	O
interpreted	B-Application
as	O
comments	O
or	O
plain	O
text	O
of	O
the	O
other	O
format	O
.	O
</s>
<s>
These	O
are	O
demonstrated	O
in	O
this	O
public	O
domain	O
polyglot	O
written	O
in	O
ANSI	O
C	B-Language
,	O
PHP	B-Application
and	O
bash	B-Operating_System
:	O
</s>
<s>
printf	B-Language
"	O
Hello	O
,	O
world	O
!	O
\n"true/*	O
2>	O
/dev/null	O
|	O
grep	O
-v	O
true*	O
/	O
;	O
</s>
<s>
A	O
hash	O
sign	O
marks	O
a	O
preprocessor	B-General_Concept
statement	O
in	O
C	B-Language
,	O
but	O
is	O
a	O
comment	O
in	O
both	O
bash	B-Operating_System
and	O
PHP	B-Application
.	O
</s>
<s>
"	O
//	O
"	O
is	O
a	O
comment	O
in	O
both	O
PHP	B-Application
and	O
C	B-Language
and	O
the	O
root	B-Application
directory	I-Application
in	O
bash	B-Operating_System
.	O
</s>
<s>
Shell	O
redirection	B-Device
is	O
used	O
to	O
eliminate	O
undesirable	O
outputs	O
.	O
</s>
<s>
Even	O
on	O
commented	O
out	O
lines	O
,	O
the	O
"	O
<?php" and "?>	O
"	O
PHP	B-Application
indicators	O
still	O
have	O
effect	O
.	O
</s>
<s>
The	O
statement	O
"	O
function	O
main( )	O
"	O
is	O
valid	O
in	O
both	O
PHP	B-Application
and	O
bash	B-Operating_System
;	O
C	B-Language
#defines	O
are	O
used	O
to	O
convert	O
it	O
into	O
"	O
int	O
main(void )	O
"	O
at	O
compile	B-Language
time	O
.	O
</s>
<s>
"	O
if	O
(($x )	O
)	O
"	O
is	O
a	O
valid	O
statement	O
in	O
both	O
bash	B-Operating_System
and	O
PHP	B-Application
.	O
</s>
<s>
printf	B-Language
is	O
a	O
bash	B-Operating_System
shell	I-Operating_System
builtin	O
which	O
is	O
identical	O
to	O
the	O
C	B-Language
printf	I-Language
except	O
for	O
its	O
omission	O
of	O
brackets	O
(	O
which	O
the	O
C	B-Language
preprocessor	I-Language
adds	O
if	O
this	O
is	O
compiled	B-Language
with	O
a	O
C	B-Language
compiler	B-Language
)	O
.	O
</s>
<s>
The	O
final	O
three	O
lines	O
are	O
only	O
used	O
by	O
bash	B-Operating_System
,	O
to	O
call	O
the	O
main	O
function	O
.	O
</s>
<s>
In	O
PHP	B-Application
the	O
main	O
function	O
is	O
defined	O
but	O
not	O
called	O
and	O
in	O
C	B-Language
there	O
is	O
no	O
need	O
to	O
explicitly	O
call	O
the	O
main	O
function	O
.	O
</s>
<s>
The	O
following	O
is	O
written	O
simultaneously	O
in	O
SNOBOL4	B-Application
,	O
Win32Forth	B-Application
,	O
PureBasicv4.x	O
,	O
and	O
REBOL	B-Application
:	O
</s>
<s>
The	O
following	O
file	B-Application
runs	O
as	O
a	O
DOS	B-Device
batch	O
file	B-Application
,	O
then	O
re-runs	O
itself	O
in	O
Perl	B-Language
:	O
</s>
<s>
This	O
allows	O
creating	O
Perl	B-Language
scripts	O
that	O
can	O
be	O
run	O
on	O
DOS	B-Device
systems	O
with	O
minimal	O
effort	O
.	O
</s>
<s>
Note	O
that	O
there	O
is	O
no	O
requirement	O
for	O
a	O
file	B-Application
to	O
perform	O
exactly	O
the	O
same	O
function	O
in	O
the	O
different	O
interpreters	B-Application
.	O
</s>
<s>
cavities	O
,	O
where	O
a	O
secondary	O
file	B-Application
format	O
is	O
hidden	O
within	O
null-padded	O
areas	O
of	O
the	O
primary	O
file	B-Application
.	O
</s>
<s>
Polyglot	O
markup	O
has	O
been	O
proposed	O
as	O
a	O
useful	O
combination	O
of	O
the	O
benefits	O
of	O
HTML5	B-Application
and	O
XHTML	B-Language
.	O
</s>
<s>
Such	O
documents	O
can	O
be	O
parsed	O
as	O
either	O
HTML	O
(	O
which	O
is	O
SGML-compatible	O
)	O
or	O
XML	B-Protocol
,	O
and	O
will	O
produce	O
the	O
same	O
DOM	B-General_Concept
structure	O
either	O
way	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
order	O
for	O
an	O
HTML5	B-Application
document	O
to	O
meet	O
these	O
criteria	O
,	O
the	O
two	O
requirements	O
are	O
that	O
it	O
must	O
have	O
an	O
HTML5	B-Application
doctype	O
,	O
and	O
be	O
written	O
in	O
well-formed	O
XHTML	B-Language
.	O
</s>
<s>
The	O
same	O
document	O
can	O
then	O
be	O
served	O
as	O
either	O
HTML	O
or	O
XHTML	B-Language
,	O
depending	O
on	O
browser	O
support	O
and	O
MIME	O
type	O
.	O
</s>
<s>
As	O
expressed	O
by	O
the	O
html-polyglot	O
recommendation	O
,	O
to	O
write	O
a	O
polyglot	O
HTML5	B-Application
document	O
,	O
the	O
following	O
key	O
points	O
should	O
be	O
observed	O
:	O
</s>
<s>
In	O
a	O
polyglot	O
markup	O
document	O
non-void	O
elements	O
(	O
such	O
as	O
script	B-Language
,	O
p	O
,	O
div	O
)	O
cannot	O
be	O
self-closing	O
even	O
if	O
they	O
are	O
empty	O
,	O
as	O
this	O
is	O
not	O
valid	O
HTML	O
.	O
</s>
<s>
The	O
DICOM	B-Algorithm
medical	O
imaging	O
format	O
was	O
designed	O
to	O
allow	O
polyglotting	O
with	O
TIFF	O
files	O
,	O
allowing	O
efficient	O
storage	O
of	O
the	O
same	O
image	O
data	O
in	O
a	O
file	B-Application
that	O
can	O
be	O
interpreted	B-Application
by	O
either	O
DICOM	B-Algorithm
or	O
TIFF	O
viewers	O
.	O
</s>
<s>
format	O
,	O
such	O
as	O
a	O
JPEG	O
file	B-Application
that	O
allows	O
arbitrary	O
data	O
in	O
its	O
comment	O
field	O
.	O
</s>
<s>
The	O
mismatch	O
between	O
what	O
the	O
interpreting	O
program	O
expects	O
,	O
and	O
what	O
the	O
file	B-Application
actually	O
contains	O
,	O
is	O
the	O
root	O
cause	O
of	O
the	O
vulnerability	O
.	O
</s>
<s>
SQL	B-Language
Injection	I-Language
is	O
a	O
trivial	O
form	O
of	O
polyglot	O
,	O
where	O
a	O
server	O
naively	O
expects	O
user-controlled	O
input	O
to	O
conform	O
to	O
a	O
certain	O
constraint	O
,	O
but	O
the	O
user	O
supplies	O
syntax	O
which	O
is	O
interpreted	B-Application
as	O
SQL	O
code	O
.	O
</s>
<s>
Note	O
that	O
in	O
a	O
security	O
context	O
,	O
there	O
is	O
no	O
requirement	O
for	O
a	O
polyglot	O
file	B-Application
to	O
be	O
strictly	O
valid	O
in	O
multiple	O
formats	O
;	O
it	O
is	O
sufficient	O
for	O
the	O
file	B-Application
to	O
trigger	O
unintended	O
behaviour	O
when	O
being	O
interpreted	B-Application
by	O
its	O
primary	O
interpreter	B-Application
.	O
</s>
<s>
Highly	O
flexible	O
or	O
extensible	O
file	B-Application
formats	O
have	O
greater	O
scope	O
for	O
polyglotting	O
,	O
and	O
therefore	O
more	O
tightly	O
constrained	O
interpretation	O
offers	O
some	O
mitigation	O
against	O
attacks	O
using	O
polyglot	O
techniques	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
PDF	O
file	B-Application
format	O
requires	O
that	O
the	O
magic	O
number	O
%PDF	O
appears	O
at	O
byte	O
offset	O
zero	O
,	O
but	O
many	O
PDF	O
interpreters	B-Application
waive	O
this	O
constraint	O
and	O
accept	O
the	O
file	B-Application
as	O
valid	O
PDF	O
as	O
long	O
as	O
the	O
string	O
appears	O
within	O
the	O
first	O
1024	O
bytes	O
.	O
</s>
<s>
This	O
creates	O
a	O
window	O
of	O
opportunity	O
for	O
polyglot	O
PDF	O
files	O
to	O
smuggle	O
non-PDF	O
content	O
in	O
the	O
header	O
of	O
the	O
file	B-Application
.	O
</s>
<s>
Detecting	O
malware	O
concealed	O
within	O
polyglot	O
files	O
requires	O
more	O
sophisticated	O
analysis	O
than	O
relying	O
on	O
file-type	O
identification	O
utilities	O
such	O
as	O
file	B-Application
.	O
</s>
<s>
In	O
2019	O
,	O
the	O
DICOM	B-Algorithm
medical	O
imaging	O
file	B-Application
format	O
was	O
found	O
to	O
be	O
vulnerable	O
to	O
malware	O
injection	O
using	O
a	O
PE-DICOM	O
polyglot	O
technique	O
.	O
</s>
<s>
The	O
polyglot	O
nature	O
of	O
the	O
attack	O
,	O
combined	O
with	O
regulatory	O
considerations	O
,	O
led	O
to	O
disinfection	O
complications	O
:	O
because	O
"	O
the	O
malware	O
is	O
essentially	O
fused	O
to	O
legitimate	O
imaging	O
files	O
"	O
,	O
"	O
incident	O
response	O
teams	O
and	O
A/V	O
software	O
cannot	O
delete	O
the	O
malware	O
file	B-Application
as	O
it	O
contains	O
protected	O
patient	O
health	O
information	O
"	O
.	O
</s>
<s>
A	O
Graphics	O
Interchange	O
Format	O
Java	B-Language
Archives	I-Language
(	O
GIFAR	B-Language
)	O
is	O
a	O
polyglot	O
file	B-Application
that	O
is	O
simultaneously	O
in	O
the	O
GIF	O
and	O
JAR	B-Language
file	I-Language
format	I-Language
.	O
</s>
<s>
This	O
technique	O
can	O
be	O
used	O
to	O
exploit	O
security	O
vulnerabilities	O
,	O
for	O
example	O
through	O
uploading	O
a	O
GIFAR	B-Language
to	O
a	O
website	O
that	O
allows	O
image	O
uploading	O
(	O
as	O
it	O
is	O
a	O
valid	O
GIF	O
file	B-Application
)	O
,	O
and	O
then	O
causing	O
the	O
Java	O
portion	O
of	O
the	O
GIFAR	B-Language
to	O
be	O
executed	O
as	O
though	O
it	O
were	O
part	O
of	O
the	O
website	O
's	O
intended	O
code	O
,	O
being	O
delivered	O
to	O
the	O
browser	O
from	O
the	O
same	B-Protocol
origin	I-Protocol
.	O
</s>
<s>
GIFARs	B-Language
are	O
possible	O
because	O
GIF	O
images	O
store	O
their	O
header	O
in	O
the	O
beginning	O
of	O
the	O
file	B-Application
,	O
and	O
JAR	B-Language
files	I-Language
(	O
as	O
with	O
any	O
ZIP	O
archive-based	O
format	O
)	O
store	O
their	O
data	O
at	O
the	O
end	O
.	O
</s>
<s>
Polyglot	O
programming	O
,	O
referring	O
to	O
the	O
practise	O
of	O
building	O
systems	O
using	O
multiple	O
programming	O
languages	O
,	O
but	O
not	O
necessarily	O
in	O
the	O
same	O
file	B-Application
.	O
</s>
<s>
Polyglot	B-General_Concept
persistence	I-General_Concept
is	O
similar	O
,	O
but	O
about	O
databases	O
.	O
</s>
