<s>
In	O
information	O
security	O
and	O
programming	B-General_Concept
,	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
,	O
or	O
buffer	B-General_Concept
overrun	I-General_Concept
,	O
is	O
an	O
anomaly	B-Error_Name
whereby	O
a	O
program	B-Application
,	O
while	O
writing	O
data	B-General_Concept
to	O
a	O
buffer	B-General_Concept
,	O
overruns	O
the	O
buffer	B-General_Concept
's	O
boundary	O
and	O
overwrites	O
adjacent	O
memory	B-General_Concept
locations	I-General_Concept
.	O
</s>
<s>
Buffers	B-General_Concept
are	O
areas	O
of	O
memory	O
set	O
aside	O
to	O
hold	O
data	B-General_Concept
,	O
often	O
while	O
moving	O
it	O
from	O
one	O
section	O
of	O
a	O
program	B-Application
to	O
another	O
,	O
or	O
between	O
programs	O
.	O
</s>
<s>
Buffer	B-General_Concept
overflows	I-General_Concept
can	O
often	O
be	O
triggered	O
by	O
malformed	O
inputs	O
;	O
if	O
one	O
assumes	O
all	O
inputs	O
will	O
be	O
smaller	O
than	O
a	O
certain	O
size	O
and	O
the	O
buffer	B-General_Concept
is	O
created	O
to	O
be	O
that	O
size	O
,	O
then	O
an	O
anomalous	O
transaction	O
that	O
produces	O
more	O
data	B-General_Concept
could	O
cause	O
it	O
to	O
write	O
past	O
the	O
end	O
of	O
the	O
buffer	B-General_Concept
.	O
</s>
<s>
If	O
this	O
overwrites	O
adjacent	O
data	B-General_Concept
or	O
executable	B-General_Concept
code	I-General_Concept
,	O
this	O
may	O
result	O
in	O
erratic	O
program	B-Application
behavior	O
,	O
including	O
memory	O
access	O
errors	O
,	O
incorrect	O
results	O
,	O
and	O
crashes	B-General_Concept
.	O
</s>
<s>
Exploiting	O
the	O
behavior	O
of	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
is	O
a	O
well-known	O
security	O
exploit	O
.	O
</s>
<s>
On	O
many	O
systems	O
,	O
the	O
memory	O
layout	O
of	O
a	O
program	B-Application
,	O
or	O
the	O
system	O
as	O
a	O
whole	O
,	O
is	O
well	O
defined	O
.	O
</s>
<s>
By	O
sending	O
in	O
data	B-General_Concept
designed	O
to	O
cause	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
,	O
it	O
is	O
possible	O
to	O
write	O
into	O
areas	O
known	O
to	O
hold	O
executable	B-General_Concept
code	I-General_Concept
and	O
replace	O
it	O
with	O
malicious	O
code	O
,	O
or	O
to	O
selectively	O
overwrite	O
data	B-General_Concept
pertaining	O
to	O
the	O
program	B-Application
's	O
state	O
,	O
therefore	O
causing	O
behavior	O
that	O
was	O
not	O
intended	O
by	O
the	O
original	O
programmer	O
.	O
</s>
<s>
Buffers	B-General_Concept
are	O
widespread	O
in	O
operating	B-General_Concept
system	I-General_Concept
(	O
OS	O
)	O
code	O
,	O
so	O
it	O
is	O
possible	O
to	O
make	O
attacks	O
that	O
perform	O
privilege	O
escalation	O
and	O
gain	O
unlimited	O
access	O
to	O
the	O
computer	O
's	O
resources	O
.	O
</s>
<s>
Programming	B-General_Concept
languages	O
commonly	O
associated	O
with	O
buffer	B-General_Concept
overflows	I-General_Concept
include	O
C	B-Language
and	O
C++	B-Language
,	O
which	O
provide	O
no	O
built-in	O
protection	O
against	O
accessing	O
or	O
overwriting	O
data	B-General_Concept
in	O
any	O
part	O
of	O
memory	O
and	O
do	O
not	O
automatically	O
check	O
that	O
data	B-General_Concept
written	O
to	O
an	O
array	B-Data_Structure
(	O
the	O
built-in	O
buffer	B-General_Concept
type	O
)	O
is	O
within	O
the	O
boundaries	O
of	O
that	O
array	B-Data_Structure
.	O
</s>
<s>
Bounds	B-Data_Structure
checking	I-Data_Structure
can	O
prevent	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
but	O
requires	O
additional	O
code	O
and	O
processing	O
time	O
.	O
</s>
<s>
Modern	O
operating	B-General_Concept
systems	I-General_Concept
use	O
a	O
variety	O
of	O
techniques	O
to	O
combat	O
malicious	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
notably	O
by	O
randomizing	O
the	O
layout	O
of	O
memory	O
,	O
or	O
deliberately	O
leaving	O
space	O
between	O
buffers	B-General_Concept
and	O
looking	O
for	O
actions	O
that	O
write	O
into	O
those	O
areas	O
(	O
"	O
canaries	O
"	O
)	O
.	O
</s>
<s>
A	O
buffer	B-General_Concept
overflow	I-General_Concept
occurs	O
when	O
data	B-General_Concept
written	O
to	O
a	O
buffer	B-General_Concept
also	O
corrupts	O
data	B-General_Concept
values	O
in	O
memory	O
addresses	O
adjacent	O
to	O
the	O
destination	O
buffer	B-General_Concept
due	O
to	O
insufficient	O
bounds	B-Data_Structure
checking	I-Data_Structure
.	O
</s>
<s>
This	O
can	O
occur	O
when	O
copying	O
data	B-General_Concept
from	O
one	O
buffer	B-General_Concept
to	O
another	O
without	O
first	O
checking	O
that	O
the	O
data	B-General_Concept
fits	O
within	O
the	O
destination	O
buffer	B-General_Concept
.	O
</s>
<s>
In	O
the	O
following	O
example	O
expressed	O
in	O
C	B-Language
,	O
a	O
program	B-Application
has	O
two	O
variables	O
which	O
are	O
adjacent	O
in	O
memory	O
:	O
an	O
8-byte-long	O
string	O
buffer	B-General_Concept
,	O
A	O
,	O
and	O
a	O
two-byte	O
big-endian	O
integer	O
,	O
B	O
.	O
</s>
<s>
Now	O
,	O
the	O
program	B-Application
attempts	O
to	O
store	O
the	O
null-terminated	B-Data_Structure
string	I-Data_Structure
with	O
ASCII	B-Protocol
encoding	O
in	O
the	O
A	O
buffer	B-General_Concept
.	O
</s>
<s>
Writing	O
data	B-General_Concept
past	O
the	O
end	O
of	O
allocated	O
memory	O
can	O
sometimes	O
be	O
detected	O
by	O
the	O
operating	B-General_Concept
system	I-General_Concept
to	O
generate	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
error	O
that	O
terminates	O
the	O
process	O
.	O
</s>
<s>
To	O
prevent	O
the	O
buffer	B-General_Concept
overflow	I-General_Concept
from	O
happening	O
in	O
this	O
example	O
,	O
the	O
call	O
to	O
strcpy	O
could	O
be	O
replaced	O
with	O
strlcpy	O
,	O
which	O
takes	O
the	O
maximum	O
capacity	O
of	O
A	O
(	O
including	O
a	O
null-termination	O
character	O
)	O
as	O
an	O
additional	O
parameter	O
and	O
ensures	O
that	O
no	O
more	O
than	O
this	O
amount	O
of	O
data	B-General_Concept
is	O
written	O
to	O
A	O
:	O
</s>
<s>
When	O
available	O
,	O
the	O
strlcpy	O
library	O
function	O
is	O
preferred	O
over	O
strncpy	O
which	O
does	O
not	O
null-terminate	O
the	O
destination	O
buffer	B-General_Concept
if	O
the	O
source	O
string	O
's	O
length	O
is	O
greater	O
than	O
or	O
equal	O
to	O
the	O
size	O
of	O
the	O
buffer	B-General_Concept
(	O
the	O
third	O
argument	O
passed	O
to	O
the	O
function	O
)	O
,	O
therefore	O
A	O
may	O
not	O
be	O
null-terminated	O
and	O
cannot	O
be	O
treated	O
as	O
a	O
valid	O
C-style	O
string	O
.	O
</s>
<s>
The	O
techniques	O
to	O
exploit	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
vulnerability	O
vary	O
by	O
architecture	B-General_Concept
,	O
by	O
operating	B-General_Concept
system	I-General_Concept
and	O
by	O
memory	O
region	O
.	O
</s>
<s>
For	O
example	O
,	O
exploitation	O
on	O
the	O
heap	O
(	O
used	O
for	O
dynamically	O
allocated	O
memory	O
)	O
,	O
differs	O
markedly	O
from	O
exploitation	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
In	O
general	O
,	O
heap	B-General_Concept
exploitation	I-General_Concept
is	O
dependent	O
on	O
the	O
heap	O
manager	O
used	O
on	O
the	O
target	O
system	O
,	O
stack	B-General_Concept
exploitation	O
is	O
dependent	O
on	O
the	O
calling	O
convention	O
used	O
by	O
the	O
architecture	B-General_Concept
and	O
compiler	O
.	O
</s>
<s>
A	O
technically	O
inclined	O
user	O
may	O
exploit	O
stack-based	O
buffer	B-General_Concept
overflows	I-General_Concept
to	O
manipulate	O
the	O
program	B-Application
to	O
their	O
advantage	O
in	O
one	O
of	O
several	O
ways	O
:	O
</s>
<s>
By	O
overwriting	O
the	O
return	O
address	O
in	O
a	O
stack	B-General_Concept
frame	O
to	O
point	O
to	O
code	O
selected	O
by	O
the	O
attacker	O
,	O
usually	O
called	O
the	O
shellcode	O
.	O
</s>
<s>
By	O
overwriting	O
a	O
local	O
variable	O
(	O
or	O
pointer	O
)	O
of	O
a	O
different	O
stack	B-General_Concept
frame	O
,	O
which	O
will	O
be	O
used	O
by	O
the	O
function	O
which	O
owns	O
that	O
frame	O
later	O
.	O
</s>
<s>
The	O
attacker	O
designs	O
data	B-General_Concept
to	O
cause	O
one	O
of	O
these	O
exploits	O
,	O
then	O
places	O
this	O
data	B-General_Concept
in	O
a	O
buffer	B-General_Concept
supplied	O
to	O
users	O
by	O
the	O
vulnerable	O
code	O
.	O
</s>
<s>
If	O
the	O
address	O
of	O
the	O
user-supplied	O
data	B-General_Concept
used	O
to	O
affect	O
the	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
is	O
unpredictable	O
,	O
exploiting	O
a	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
to	O
cause	O
remote	O
code	O
execution	O
becomes	O
much	O
more	O
difficult	O
.	O
</s>
<s>
One	O
technique	O
that	O
can	O
be	O
used	O
to	O
exploit	O
such	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
is	O
called	O
"	O
trampolining	B-General_Concept
"	O
.	O
</s>
<s>
In	O
that	O
technique	O
,	O
an	O
attacker	O
will	O
find	O
a	O
pointer	O
to	O
the	O
vulnerable	O
stack	B-General_Concept
buffer	B-General_Concept
,	O
and	O
compute	O
the	O
location	O
of	O
their	O
shellcode	O
relative	O
to	O
that	O
pointer	O
.	O
</s>
<s>
Then	O
,	O
they	O
will	O
use	O
the	O
overwrite	O
to	O
jump	O
to	O
an	O
instruction	B-Language
already	O
in	O
memory	O
which	O
will	O
make	O
a	O
second	O
jump	O
,	O
this	O
time	O
relative	O
to	O
the	O
pointer	O
;	O
that	O
second	O
jump	O
will	O
branch	O
execution	O
into	O
the	O
shellcode	O
.	O
</s>
<s>
The	B-Application
Metasploit	I-Application
Project	I-Application
,	O
for	O
example	O
,	O
maintains	O
a	O
database	O
of	O
suitable	O
opcodes	B-Language
,	O
though	O
it	O
lists	O
only	O
those	O
found	O
in	O
the	O
Windows	B-Application
operating	I-Application
system	I-Application
.	O
</s>
<s>
A	O
buffer	B-General_Concept
overflow	I-General_Concept
occurring	O
in	O
the	O
heap	O
data	B-General_Concept
area	O
is	O
referred	O
to	O
as	O
a	O
heap	B-General_Concept
overflow	I-General_Concept
and	O
is	O
exploitable	O
in	O
a	O
manner	O
different	O
from	O
that	O
of	O
stack-based	O
overflows	O
.	O
</s>
<s>
Memory	O
on	O
the	O
heap	O
is	O
dynamically	O
allocated	O
by	O
the	O
application	O
at	O
run-time	O
and	O
typically	O
contains	O
program	B-Application
data	B-General_Concept
.	O
</s>
<s>
Exploitation	O
is	O
performed	O
by	O
corrupting	O
this	O
data	B-General_Concept
in	O
specific	O
ways	O
to	O
cause	O
the	O
application	O
to	O
overwrite	O
internal	O
structures	O
such	O
as	O
linked	O
list	O
pointers	O
.	O
</s>
<s>
The	O
canonical	O
heap	B-General_Concept
overflow	I-General_Concept
technique	O
overwrites	O
dynamic	O
memory	O
allocation	O
linkage	O
(	O
such	O
as	O
malloc	B-Language
meta	O
data	B-General_Concept
)	O
and	O
uses	O
the	O
resulting	O
pointer	O
exchange	O
to	O
overwrite	O
a	O
program	B-Application
function	B-Language
pointer	I-Language
.	O
</s>
<s>
Microsoft	O
's	O
GDI+	B-Operating_System
vulnerability	O
in	O
handling	O
JPEGs	O
is	O
an	O
example	O
of	O
the	O
danger	O
a	O
heap	B-General_Concept
overflow	I-General_Concept
can	O
present	O
.	O
</s>
<s>
Manipulation	O
of	O
the	O
buffer	B-General_Concept
,	O
which	O
occurs	O
before	O
it	O
is	O
read	O
or	O
executed	O
,	O
may	O
lead	O
to	O
the	O
failure	O
of	O
an	O
exploitation	O
attempt	O
.	O
</s>
<s>
Manipulations	O
could	O
include	O
conversion	O
to	O
upper	O
or	O
lower	O
case	O
,	O
removal	O
of	O
metacharacters	B-Algorithm
and	O
filtering	O
out	O
of	O
non-alphanumeric	O
strings	O
.	O
</s>
<s>
However	O
,	O
techniques	O
exist	O
to	O
bypass	O
these	O
filters	O
and	O
manipulations	O
;	O
alphanumeric	O
shellcode	O
,	O
polymorphic	O
code	O
,	O
self-modifying	B-Application
code	I-Application
and	O
return-to-libc	B-Language
attacks	O
.	O
</s>
<s>
A	O
NOP-sled	O
is	O
the	O
oldest	O
and	O
most	O
widely	O
known	O
technique	O
for	O
exploiting	O
stack	B-General_Concept
buffer	I-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
It	O
solves	O
the	O
problem	O
of	O
finding	O
the	O
exact	O
address	O
of	O
the	O
buffer	B-General_Concept
by	O
effectively	O
increasing	O
the	O
size	O
of	O
the	O
target	O
area	O
.	O
</s>
<s>
To	O
do	O
this	O
,	O
much	O
larger	O
sections	O
of	O
the	O
stack	B-General_Concept
are	O
corrupted	O
with	O
the	O
no-op	B-Language
machine	O
instruction	B-Language
.	O
</s>
<s>
At	O
the	O
end	O
of	O
the	O
attacker-supplied	O
data	B-General_Concept
,	O
after	O
the	O
no-op	B-Language
instructions	O
,	O
the	O
attacker	O
places	O
an	O
instruction	B-Language
to	O
perform	O
a	O
relative	O
jump	O
to	O
the	O
top	O
of	O
the	O
buffer	B-General_Concept
where	O
the	O
shellcode	O
is	O
located	O
.	O
</s>
<s>
This	O
collection	O
of	O
no-ops	B-Language
is	O
referred	O
to	O
as	O
the	O
"	O
NOP-sled	O
"	O
because	O
if	O
the	O
return	O
address	O
is	O
overwritten	O
with	O
any	O
address	O
within	O
the	O
no-op	B-Language
region	O
of	O
the	O
buffer	B-General_Concept
,	O
the	O
execution	O
will	O
"	O
slide	O
"	O
down	O
the	O
no-ops	B-Language
until	O
it	O
is	O
redirected	O
to	O
the	O
actual	O
malicious	O
code	O
by	O
the	O
jump	O
at	O
the	O
end	O
.	O
</s>
<s>
This	O
technique	O
requires	O
the	O
attacker	O
to	O
guess	O
where	O
on	O
the	O
stack	B-General_Concept
the	O
NOP-sled	O
is	O
instead	O
of	O
the	O
comparatively	O
small	O
shellcode	O
.	O
</s>
<s>
Because	O
of	O
the	O
popularity	O
of	O
this	O
technique	O
,	O
many	O
vendors	O
of	O
intrusion	O
prevention	O
systems	O
will	O
search	O
for	O
this	O
pattern	O
of	O
no-op	B-Language
machine	O
instructions	O
in	O
an	O
attempt	O
to	O
detect	O
shellcode	O
in	O
use	O
.	O
</s>
<s>
It	O
is	O
important	O
to	O
note	O
that	O
a	O
NOP-sled	O
does	O
not	O
necessarily	O
contain	O
only	O
traditional	O
no-op	B-Language
machine	O
instructions	O
;	O
any	O
instruction	B-Language
that	O
does	O
not	O
corrupt	O
the	O
machine	O
state	O
to	O
a	O
point	O
where	O
the	O
shellcode	O
will	O
not	O
run	O
can	O
be	O
used	O
in	O
place	O
of	O
the	O
hardware	O
assisted	O
no-op	B-Language
.	O
</s>
<s>
As	O
a	O
result	O
,	O
it	O
has	O
become	O
common	O
practice	O
for	O
exploit	O
writers	O
to	O
compose	O
the	O
no-op	B-Language
sled	O
with	O
randomly	O
chosen	O
instructions	O
which	O
will	O
have	O
no	O
real	O
effect	O
on	O
the	O
shellcode	O
execution	O
.	O
</s>
<s>
Exploits	O
using	O
this	O
technique	O
still	O
must	O
rely	O
on	O
some	O
amount	O
of	O
luck	O
that	O
they	O
will	O
guess	O
offsets	O
on	O
the	O
stack	B-General_Concept
that	O
are	O
within	O
the	O
NOP-sled	O
region	O
.	O
</s>
<s>
An	O
incorrect	O
guess	O
will	O
usually	O
result	O
in	O
the	O
target	O
program	B-Application
crashing	O
and	O
could	O
alert	O
the	O
system	B-Application
administrator	I-Application
to	O
the	O
attacker	O
's	O
activities	O
.	O
</s>
<s>
Another	O
problem	O
is	O
that	O
the	O
NOP-sled	O
requires	O
a	O
much	O
larger	O
amount	O
of	O
memory	O
in	O
which	O
to	O
hold	O
a	O
NOP-sled	O
large	O
enough	O
to	O
be	O
of	O
any	O
use	O
.	O
</s>
<s>
This	O
can	O
be	O
a	O
problem	O
when	O
the	O
allocated	O
size	O
of	O
the	O
affected	O
buffer	B-General_Concept
is	O
too	O
small	O
and	O
the	O
current	O
depth	O
of	O
the	O
stack	B-General_Concept
is	O
shallow	O
(	O
i.e.	O
,	O
there	O
is	O
not	O
much	O
space	O
from	O
the	O
end	O
of	O
the	O
current	O
stack	B-General_Concept
frame	O
to	O
the	O
start	O
of	O
the	O
stack	B-General_Concept
)	O
.	O
</s>
<s>
Despite	O
its	O
problems	O
,	O
the	O
NOP-sled	O
is	O
often	O
the	O
only	O
method	O
that	O
will	O
work	O
for	O
a	O
given	O
platform	O
,	O
environment	O
,	O
or	O
situation	O
,	O
and	O
as	O
such	O
it	O
is	O
still	O
an	O
important	O
technique	O
.	O
</s>
<s>
The	O
"	O
jump	O
to	O
register	O
"	O
technique	O
allows	O
for	O
reliable	O
exploitation	O
of	O
stack	B-General_Concept
buffer	I-General_Concept
overflows	I-General_Concept
without	O
the	O
need	O
for	O
extra	O
room	O
for	O
a	O
NOP-sled	O
and	O
without	O
having	O
to	O
guess	O
stack	B-General_Concept
offsets	O
.	O
</s>
<s>
The	O
strategy	O
is	O
to	O
overwrite	O
the	O
return	B-General_Concept
pointer	I-General_Concept
with	O
something	O
that	O
will	O
cause	O
the	O
program	B-Application
to	O
jump	O
to	O
a	O
known	O
pointer	O
stored	O
within	O
a	O
register	O
which	O
points	O
to	O
the	O
controlled	O
buffer	B-General_Concept
and	O
thus	O
the	O
shellcode	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
register	O
A	O
contains	O
a	O
pointer	O
to	O
the	O
start	O
of	O
a	O
buffer	B-General_Concept
then	O
any	O
jump	O
or	O
call	O
taking	O
that	O
register	O
as	O
an	O
operand	O
can	O
be	O
used	O
to	O
gain	O
control	O
of	O
the	O
flow	O
of	O
execution	O
.	O
</s>
<s>
In	O
practice	O
a	O
program	B-Application
may	O
not	O
intentionally	O
contain	O
instructions	O
to	O
jump	O
to	O
a	O
particular	O
register	O
.	O
</s>
<s>
The	O
traditional	O
solution	O
is	O
to	O
find	O
an	O
unintentional	O
instance	O
of	O
a	O
suitable	O
opcode	B-Language
at	O
a	O
fixed	O
location	O
somewhere	O
within	O
the	O
program	B-Application
memory	O
.	O
</s>
<s>
In	O
figure	O
E	O
on	O
the	O
left	O
is	O
an	O
example	O
of	O
such	O
an	O
unintentional	O
instance	O
of	O
the	O
i386	B-General_Concept
jmp	O
esp	O
instruction	B-Language
.	O
</s>
<s>
The	O
opcode	B-Language
for	O
this	O
instruction	B-Language
is	O
FF	O
E4	O
.	O
</s>
<s>
This	O
two-byte	O
sequence	O
can	O
be	O
found	O
at	O
a	O
one-byte	O
offset	O
from	O
the	O
start	O
of	O
the	O
instruction	B-Language
call	O
DbgPrint	O
at	O
address	O
0x7C941EED	O
.	O
</s>
<s>
If	O
an	O
attacker	O
overwrites	O
the	O
program	B-Application
return	O
address	O
with	O
this	O
address	O
the	O
program	B-Application
will	O
first	O
jump	O
to	O
0x7C941EED	O
,	O
interpret	O
the	O
opcode	B-Language
FF	O
E4	O
as	O
the	O
jmp	O
esp	O
instruction	B-Language
,	O
and	O
will	O
then	O
jump	O
to	O
the	O
top	O
of	O
the	O
stack	B-General_Concept
and	O
execute	O
the	O
attacker	O
's	O
code	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
this	O
is	O
the	O
technique	O
most	O
commonly	O
used	O
in	O
Internet	O
worms	O
that	O
exploit	O
stack	B-General_Concept
buffer	I-General_Concept
overflow	I-General_Concept
vulnerabilities	O
.	O
</s>
<s>
This	O
method	O
also	O
allows	O
shellcode	O
to	O
be	O
placed	O
after	O
the	O
overwritten	O
return	O
address	O
on	O
the	O
Windows	B-Application
platform	O
.	O
</s>
<s>
Since	O
executables	O
are	O
mostly	O
based	O
at	O
address	O
0x00400000	O
and	O
x86	O
is	O
a	O
Little	O
Endian	O
architecture	B-General_Concept
,	O
the	O
last	O
byte	O
of	O
the	O
return	O
address	O
must	O
be	O
a	O
null	O
,	O
which	O
terminates	O
the	O
buffer	B-General_Concept
copy	O
and	O
nothing	O
is	O
written	O
beyond	O
that	O
.	O
</s>
<s>
This	O
limits	O
the	O
size	O
of	O
the	O
shellcode	O
to	O
the	O
size	O
of	O
the	O
buffer	B-General_Concept
,	O
which	O
may	O
be	O
overly	O
restrictive	O
.	O
</s>
<s>
Used	O
in	O
this	O
way	O
,	O
the	O
method	O
is	O
often	O
referred	O
to	O
as	O
"	O
DLL	B-General_Concept
trampolining	I-General_Concept
"	O
.	O
</s>
<s>
Various	O
techniques	O
have	O
been	O
used	O
to	O
detect	O
or	O
prevent	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
with	O
various	O
tradeoffs	O
.	O
</s>
<s>
Assembly	O
,	O
C	B-Language
and	O
C++	B-Language
are	O
popular	O
programming	B-General_Concept
languages	O
that	O
are	O
vulnerable	O
to	O
buffer	B-General_Concept
overflow	I-General_Concept
in	O
part	O
because	O
they	O
allow	O
direct	O
access	O
to	O
memory	O
and	O
are	O
not	O
strongly	O
typed	O
.	O
</s>
<s>
C	B-Language
provides	O
no	O
built-in	O
protection	O
against	O
accessing	O
or	O
overwriting	O
data	B-General_Concept
in	O
any	O
part	O
of	O
memory	O
;	O
more	O
specifically	O
,	O
it	O
does	O
not	O
check	O
that	O
data	B-General_Concept
written	O
to	O
a	O
buffer	B-General_Concept
is	O
within	O
the	O
boundaries	O
of	O
that	O
buffer	B-General_Concept
.	O
</s>
<s>
The	O
standard	O
C++	B-Language
libraries	O
provide	O
many	O
ways	O
of	O
safely	O
buffering	O
data	B-General_Concept
,	O
and	O
C++'s	O
Standard	B-Application
Template	I-Application
Library	I-Application
(	O
STL	O
)	O
provides	O
containers	O
that	O
can	O
optionally	O
perform	O
bounds	B-Data_Structure
checking	I-Data_Structure
if	O
the	O
programmer	O
explicitly	O
calls	O
for	O
checks	O
while	O
accessing	O
data	B-General_Concept
.	O
</s>
<s>
For	O
example	O
,	O
a	O
vector	O
's	O
member	O
function	O
at( )	O
performs	O
a	O
bounds	B-Data_Structure
check	I-Data_Structure
and	O
throws	O
an	O
out_of_range	O
exception	B-General_Concept
if	O
the	O
bounds	B-Data_Structure
check	I-Data_Structure
fails	O
.	O
</s>
<s>
However	O
,	O
C++	B-Language
behaves	O
just	O
like	O
C	B-Language
if	O
the	O
bounds	B-Data_Structure
check	I-Data_Structure
is	O
not	O
explicitly	O
called	O
.	O
</s>
<s>
Techniques	O
to	O
avoid	O
buffer	B-General_Concept
overflows	I-General_Concept
also	O
exist	O
for	O
C	B-Language
.	O
</s>
<s>
Languages	O
that	O
are	O
strongly	O
typed	O
and	O
do	O
not	O
allow	O
direct	O
memory	O
access	O
,	O
such	O
as	O
COBOL	O
,	O
Java	B-Device
,	O
Python	O
,	O
and	O
others	O
,	O
prevent	O
buffer	B-General_Concept
overflow	I-General_Concept
from	O
occurring	O
in	O
most	O
cases	O
.	O
</s>
<s>
Many	O
programming	B-General_Concept
languages	O
other	O
than	O
C	B-Language
or	O
C++	B-Language
provide	O
runtime	O
checking	O
and	O
in	O
some	O
cases	O
even	O
compile-time	O
checking	O
which	O
might	O
send	O
a	O
warning	O
or	O
raise	O
an	O
exception	B-General_Concept
when	O
C	B-Language
or	O
C++	B-Language
would	O
overwrite	O
data	B-General_Concept
and	O
continue	O
to	O
execute	O
further	O
instructions	O
until	O
erroneous	O
results	O
are	O
obtained	O
which	O
might	O
or	O
might	O
not	O
cause	O
the	O
program	B-Application
to	O
crash	B-General_Concept
.	O
</s>
<s>
Examples	O
of	O
such	O
languages	O
include	O
Ada	B-Language
,	O
Eiffel	B-Language
,	O
Lisp	B-Language
,	O
Modula-2	B-Language
,	O
Smalltalk	B-Application
,	O
OCaml	B-Language
and	O
such	O
C-derivatives	O
as	O
Cyclone	B-Language
,	O
Rust	B-Application
and	O
D	B-Application
.	O
The	O
Java	B-Device
and	O
.NET	B-Application
Framework	I-Application
bytecode	O
environments	O
also	O
require	O
bounds	B-Data_Structure
checking	I-Data_Structure
on	O
all	O
arrays	O
.	O
</s>
<s>
Nearly	O
every	O
interpreted	B-Application
language	I-Application
will	O
protect	O
against	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
signaling	O
a	O
well-defined	O
error	O
condition	O
.	O
</s>
<s>
Often	O
where	O
a	O
language	O
provides	O
enough	O
type	O
information	O
to	O
do	O
bounds	B-Data_Structure
checking	I-Data_Structure
an	O
option	O
is	O
provided	O
to	O
enable	O
or	O
disable	O
it	O
.	O
</s>
<s>
The	O
problem	O
of	O
buffer	B-General_Concept
overflows	I-General_Concept
is	O
common	O
in	O
the	O
C	B-Language
and	O
C++	B-Language
languages	I-Language
because	O
they	O
expose	O
low	O
level	O
representational	O
details	O
of	O
buffers	B-General_Concept
as	O
containers	O
for	O
data	B-General_Concept
types	O
.	O
</s>
<s>
Buffer	B-General_Concept
overflows	I-General_Concept
must	O
thus	O
be	O
avoided	O
by	O
maintaining	O
a	O
high	O
degree	O
of	O
correctness	O
in	O
code	O
which	O
performs	O
buffer	B-General_Concept
management	O
.	O
</s>
<s>
It	O
has	O
also	O
long	O
been	O
recommended	O
to	O
avoid	O
standard	O
library	O
functions	O
which	O
are	O
not	O
bounds	O
checked	O
,	O
such	O
as	O
gets	O
,	O
scanf	B-Language
and	O
strcpy	O
.	O
</s>
<s>
Well-written	O
and	O
tested	O
abstract	O
data	B-General_Concept
type	O
libraries	O
which	O
centralize	O
and	O
automatically	O
perform	O
buffer	B-General_Concept
management	O
,	O
including	O
bounds	B-Data_Structure
checking	I-Data_Structure
,	O
can	O
reduce	O
the	O
occurrence	O
and	O
impact	O
of	O
buffer	B-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
The	O
two	O
main	O
building-block	O
data	B-General_Concept
types	O
in	O
these	O
languages	O
in	O
which	O
buffer	B-General_Concept
overflows	I-General_Concept
commonly	O
occur	O
are	O
strings	O
and	O
arrays	O
;	O
thus	O
,	O
libraries	O
preventing	O
buffer	B-General_Concept
overflows	I-General_Concept
in	O
these	O
data	B-General_Concept
types	O
can	O
provide	O
the	O
vast	O
majority	O
of	O
the	O
necessary	O
coverage	O
.	O
</s>
<s>
Still	O
,	O
failure	O
to	O
use	O
these	O
safe	O
libraries	O
correctly	O
can	O
result	O
in	O
buffer	B-General_Concept
overflows	I-General_Concept
and	O
other	O
vulnerabilities	O
;	O
and	O
naturally	O
,	O
any	O
bug	B-Error_Name
in	O
the	O
library	O
itself	O
is	O
a	O
potential	O
vulnerability	O
.	O
</s>
<s>
The	O
OpenBSD	B-Operating_System
operating	B-General_Concept
system	I-General_Concept
's	O
C	B-Language
library	I-Language
provides	O
the	O
strlcpy	O
and	O
strlcat	O
functions	O
,	O
but	O
these	O
are	O
more	O
limited	O
than	O
full	O
safe	O
library	O
implementations	O
.	O
</s>
<s>
In	O
September	O
2007	O
,	O
Technical	O
Report	O
24731	O
,	O
prepared	O
by	O
the	O
C	B-Language
standards	O
committee	O
,	O
was	O
published	O
;	O
it	O
specifies	O
a	O
set	O
of	O
functions	O
which	O
are	O
based	O
on	O
the	O
standard	B-Language
C	I-Language
library	I-Language
's	O
string	O
and	O
IO	O
functions	O
,	O
with	O
additional	O
buffer-size	O
parameters	O
.	O
</s>
<s>
However	O
,	O
the	O
efficacy	O
of	O
these	O
functions	O
for	O
the	O
purpose	O
of	O
reducing	O
buffer	B-General_Concept
overflows	I-General_Concept
is	O
disputable	O
;	O
it	O
requires	O
programmer	O
intervention	O
on	O
a	O
per	O
function	O
call	O
basis	O
that	O
is	O
equivalent	O
to	O
intervention	O
that	O
could	O
make	O
the	O
analogous	O
older	O
standard	O
library	O
functions	O
buffer	B-General_Concept
overflow	I-General_Concept
safe	O
.	O
</s>
<s>
Buffer	B-Error_Name
overflow	I-Error_Name
protection	I-Error_Name
is	O
used	O
to	O
detect	O
the	O
most	O
common	O
buffer	B-General_Concept
overflows	I-General_Concept
by	O
checking	O
that	O
the	O
stack	B-General_Concept
has	O
not	O
been	O
altered	O
when	O
a	O
function	O
returns	O
.	O
</s>
<s>
If	O
it	O
has	O
been	O
altered	O
,	O
the	O
program	B-Application
exits	O
with	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
.	O
</s>
<s>
Three	O
such	O
systems	O
are	O
Libsafe	O
,	O
and	O
the	O
StackGuard	B-Error_Name
and	O
ProPolice	O
gcc	B-Application
patches	O
.	O
</s>
<s>
Microsoft	O
's	O
implementation	O
of	O
Data	B-General_Concept
Execution	O
Prevention	O
(	O
DEP	O
)	O
mode	O
explicitly	O
protects	O
the	O
pointer	O
to	O
the	O
Structured	O
Exception	B-General_Concept
Handler	I-General_Concept
(	O
SEH	O
)	O
from	O
being	O
overwritten	O
.	O
</s>
<s>
Stronger	O
stack	B-Error_Name
protection	I-Error_Name
is	O
possible	O
by	O
splitting	O
the	O
stack	B-General_Concept
in	O
two	O
:	O
one	O
for	O
data	B-General_Concept
and	O
one	O
for	O
function	O
returns	O
.	O
</s>
<s>
This	O
split	O
is	O
present	O
in	O
the	O
Forth	B-Application
language	I-Application
,	O
though	O
it	O
was	O
not	O
a	O
security-based	O
design	O
decision	O
.	O
</s>
<s>
Regardless	O
,	O
this	O
is	O
not	O
a	O
complete	O
solution	O
to	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
as	O
sensitive	O
data	B-General_Concept
other	O
than	O
the	O
return	O
address	O
may	O
still	O
be	O
overwritten	O
.	O
</s>
<s>
Buffer	B-General_Concept
overflows	I-General_Concept
work	O
by	O
manipulating	O
pointers	O
,	O
including	O
stored	O
addresses	O
.	O
</s>
<s>
PointGuard	O
was	O
never	O
released	O
,	O
but	O
Microsoft	O
implemented	O
a	O
similar	O
approach	O
beginning	O
in	O
Windows	B-Application
XP	I-Application
SP2	O
and	O
Windows	B-Application
Server	I-Application
2003	I-Application
SP1	O
.	O
</s>
<s>
This	O
can	O
allow	O
an	O
attack	O
to	O
succeed	O
if	O
the	O
attacker	O
is	O
able	O
to	O
attempt	O
the	O
exploit	O
multiple	O
times	O
or	O
is	O
able	O
to	O
complete	O
an	O
attack	O
by	O
causing	O
a	O
pointer	O
to	O
point	O
to	O
one	O
of	O
several	O
locations	O
(	O
such	O
as	O
any	O
location	O
within	O
a	O
NOP	B-Language
sled	O
)	O
.	O
</s>
<s>
Executable	O
space	O
protection	O
is	O
an	O
approach	O
to	O
buffer	B-Error_Name
overflow	I-Error_Name
protection	I-Error_Name
which	O
prevents	O
execution	O
of	O
code	O
on	O
the	O
stack	B-General_Concept
or	O
the	O
heap	O
.	O
</s>
<s>
An	O
attacker	O
may	O
use	O
buffer	B-General_Concept
overflows	I-General_Concept
to	O
insert	O
arbitrary	O
code	O
into	O
the	O
memory	O
of	O
a	O
program	B-Application
,	O
but	O
with	O
executable	O
space	O
protection	O
,	O
any	O
attempt	O
to	O
execute	O
that	O
code	O
will	O
cause	O
an	O
exception	B-General_Concept
.	O
</s>
<s>
Some	O
CPUs	O
support	O
a	O
feature	O
called	O
NX	B-General_Concept
(	O
"	O
No	B-General_Concept
eXecute	I-General_Concept
"	O
)	O
or	O
XD	B-General_Concept
(	O
"	O
eXecute	O
Disabled	O
"	O
)	O
bit	O
,	O
which	O
in	O
conjunction	O
with	O
software	O
,	O
can	O
be	O
used	O
to	O
mark	O
pages	B-Architecture
of	I-Architecture
data	I-Architecture
(	O
such	O
as	O
those	O
containing	O
the	O
stack	B-General_Concept
and	O
the	O
heap	O
)	O
as	O
readable	O
and	O
writable	O
but	O
not	O
executable	O
.	O
</s>
<s>
Some	O
Unix	B-Application
operating	I-Application
systems	I-Application
(	O
e.g.	O
</s>
<s>
OpenBSD	B-Operating_System
,	O
macOS	B-Application
)	O
ship	O
with	O
executable	O
space	O
protection	O
(	O
e.g.	O
</s>
<s>
Newer	O
variants	O
of	O
Microsoft	B-Application
Windows	I-Application
also	O
support	O
executable	O
space	O
protection	O
,	O
called	O
Data	B-General_Concept
Execution	O
Prevention	O
.	O
</s>
<s>
Proprietary	B-Application
add-ons	O
include	O
:	O
</s>
<s>
Executable	O
space	O
protection	O
does	O
not	O
generally	O
protect	O
against	O
return-to-libc	B-Language
attacks	O
,	O
or	O
any	O
other	O
attack	O
which	O
does	O
not	O
rely	O
on	O
the	O
execution	O
of	O
the	O
attackers	O
code	O
.	O
</s>
<s>
However	O
,	O
on	O
64-bit	B-Device
systems	O
using	O
ASLR	O
,	O
as	O
described	O
below	O
,	O
executable	O
space	O
protection	O
makes	O
it	O
far	O
more	O
difficult	O
to	O
execute	O
such	O
attacks	O
.	O
</s>
<s>
Address	O
space	O
layout	O
randomization	O
(	O
ASLR	O
)	O
is	O
a	O
computer	O
security	O
feature	O
which	O
involves	O
arranging	O
the	O
positions	O
of	O
key	O
data	B-General_Concept
areas	O
,	O
usually	O
including	O
the	O
base	O
of	O
the	O
executable	O
and	O
position	O
of	O
libraries	O
,	O
heap	O
,	O
and	O
stack	B-General_Concept
,	O
randomly	O
in	O
a	O
process	O
 '	O
address	O
space	O
.	O
</s>
<s>
Randomization	O
of	O
the	O
virtual	B-Architecture
memory	I-Architecture
addresses	O
at	O
which	O
functions	O
and	O
variables	O
can	O
be	O
found	O
can	O
make	O
exploitation	O
of	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
more	O
difficult	O
,	O
but	O
not	O
impossible	O
.	O
</s>
<s>
A	O
similar	O
but	O
less	O
effective	O
method	O
is	O
to	O
rebase	B-Library
processes	O
and	O
libraries	O
in	O
the	O
virtual	O
address	O
space	O
.	O
</s>
<s>
The	O
use	O
of	O
deep	O
packet	O
inspection	O
(	O
DPI	O
)	O
can	O
detect	O
,	O
at	O
the	O
network	O
perimeter	O
,	O
very	O
basic	O
remote	O
attempts	O
to	O
exploit	O
buffer	B-General_Concept
overflows	I-General_Concept
by	O
use	O
of	O
attack	O
signatures	O
and	O
heuristics	B-Algorithm
.	O
</s>
<s>
These	O
are	O
able	O
to	O
block	O
packets	O
which	O
have	O
the	O
signature	O
of	O
a	O
known	O
attack	O
,	O
or	O
if	O
a	O
long	O
series	O
of	O
No-Operation	O
instructions	O
(	O
known	O
as	O
a	O
NOP-sled	O
)	O
is	O
detected	O
,	O
these	O
were	O
once	O
used	O
when	O
the	O
location	O
of	O
the	O
exploit	O
's	O
payload	O
is	O
slightly	O
variable	O
.	O
</s>
<s>
Packet	O
scanning	O
is	O
not	O
an	O
effective	O
method	O
since	O
it	O
can	O
only	O
prevent	O
known	O
attacks	O
and	O
there	O
are	O
many	O
ways	O
that	O
a	O
NOP-sled	O
can	O
be	O
encoded	O
.	O
</s>
<s>
Shellcode	O
used	O
by	O
attackers	O
can	O
be	O
made	O
alphanumeric	O
,	O
metamorphic	O
,	O
or	O
self-modifying	B-Application
to	O
evade	O
detection	O
by	O
heuristic	B-Algorithm
packet	O
scanners	O
and	O
intrusion	O
detection	O
systems	O
.	O
</s>
<s>
Checking	O
for	O
buffer	B-General_Concept
overflows	I-General_Concept
and	O
patching	O
the	O
bugs	B-Error_Name
that	O
cause	O
them	O
naturally	O
helps	O
prevent	O
buffer	B-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
Edge	O
case	O
testing	O
can	O
also	O
uncover	O
buffer	B-General_Concept
overflows	I-General_Concept
,	O
as	O
can	O
static	O
analysis	O
.	O
</s>
<s>
Once	O
a	O
potential	O
buffer	B-General_Concept
overflow	I-General_Concept
is	O
detected	O
,	O
it	O
must	O
be	O
patched	O
;	O
this	O
makes	O
the	O
testing	O
approach	O
useful	O
for	O
software	O
that	O
is	O
in	O
development	O
,	O
but	O
less	O
useful	O
for	O
legacy	O
software	O
that	O
is	O
no	O
longer	O
maintained	O
or	O
supported	O
.	O
</s>
<s>
Buffer	B-General_Concept
overflows	I-General_Concept
were	O
understood	O
and	O
partially	O
publicly	O
documented	O
as	O
early	O
as	O
1972	O
,	O
when	O
the	O
Computer	O
Security	O
Technology	O
Planning	O
Study	O
laid	O
out	O
the	O
technique	O
:	O
"	O
The	O
code	O
performing	O
this	O
function	O
does	O
not	O
check	O
the	O
source	O
and	O
destination	O
addresses	O
properly	O
,	O
permitting	O
portions	O
of	O
the	O
monitor	O
to	O
be	O
overlaid	O
by	O
the	O
user	O
.	O
</s>
<s>
The	O
earliest	O
documented	O
hostile	O
exploitation	O
of	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
was	O
in	O
1988	O
.	O
</s>
<s>
The	O
program	B-Application
exploited	O
was	O
a	O
service	O
on	O
Unix	B-Application
called	O
finger	B-Application
.	O
</s>
<s>
Later	O
,	O
in	O
1995	O
,	O
Thomas	O
Lopatic	O
independently	O
rediscovered	O
the	O
buffer	B-General_Concept
overflow	I-General_Concept
and	O
published	O
his	O
findings	O
on	O
the	O
Bugtraq	O
security	O
mailing	O
list	O
.	O
</s>
<s>
A	O
year	O
later	O
,	O
in	O
1996	O
,	O
Elias	O
Levy	O
(	O
also	O
known	O
as	O
Aleph	O
One	O
)	O
published	O
in	O
Phrack	O
magazine	O
the	O
paper	O
"	O
Smashing	B-General_Concept
the	I-General_Concept
Stack	I-General_Concept
for	O
Fun	O
and	O
Profit	O
"	O
,	O
a	O
step-by-step	O
introduction	O
to	O
exploiting	O
stack-based	O
buffer	B-General_Concept
overflow	I-General_Concept
vulnerabilities	O
.	O
</s>
<s>
Since	O
then	O
,	O
at	O
least	O
two	O
major	O
internet	O
worms	O
have	O
exploited	O
buffer	B-General_Concept
overflows	I-General_Concept
to	O
compromise	O
a	O
large	O
number	O
of	O
systems	O
.	O
</s>
<s>
In	O
2001	O
,	O
the	O
Code	O
Red	O
worm	O
exploited	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
in	O
Microsoft	O
's	O
Internet	B-Application
Information	I-Application
Services	I-Application
(	O
IIS	O
)	O
5.0	O
and	O
in	O
2003	O
the	O
SQL	O
Slammer	O
worm	O
compromised	O
machines	O
running	O
Microsoft	O
SQL	O
Server	O
2000	O
.	O
</s>
<s>
In	O
2003	O
,	O
buffer	B-General_Concept
overflows	I-General_Concept
present	O
in	O
licensed	O
Xbox	B-Application
games	O
have	O
been	O
exploited	O
to	O
allow	O
unlicensed	O
software	O
,	O
including	O
homebrew	B-Application
games	I-Application
,	O
to	O
run	O
on	O
the	O
console	O
without	O
the	O
need	O
for	O
hardware	O
modifications	O
,	O
known	O
as	O
modchips	O
.	O
</s>
<s>
The	O
PS2	B-Device
Independence	O
Exploit	O
also	O
used	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
to	O
achieve	O
the	O
same	O
for	O
the	O
PlayStation	B-Device
2	I-Device
.	O
</s>
<s>
The	O
Twilight	O
hack	O
accomplished	O
the	O
same	O
with	O
the	B-Operating_System
Wii	I-Operating_System
,	O
using	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
in	O
The	B-Application
Legend	I-Application
of	I-Application
Zelda	I-Application
:	I-Application
Twilight	I-Application
Princess	I-Application
.	O
</s>
