<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
DLL	B-Operating_System
injection	I-Operating_System
is	O
a	O
technique	O
used	O
for	O
running	O
code	B-Language
within	O
the	O
address	B-General_Concept
space	I-General_Concept
of	O
another	O
process	B-Operating_System
by	O
forcing	O
it	O
to	O
load	O
a	O
dynamic-link	O
library	O
.	O
</s>
<s>
DLL	B-Operating_System
injection	I-Operating_System
is	O
often	O
used	O
by	O
external	O
programs	O
to	O
influence	O
the	O
behavior	O
of	O
another	O
program	O
in	O
a	O
way	O
its	O
authors	B-Application
did	O
not	O
anticipate	O
or	O
intend	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
injected	O
code	B-Language
could	O
hook	O
system	O
function	O
calls	O
,	O
or	O
read	O
the	O
contents	O
of	O
password	O
textboxes	O
,	O
which	O
cannot	O
be	O
done	O
the	O
usual	O
way	O
.	O
</s>
<s>
A	O
program	O
used	O
to	O
inject	O
arbitrary	O
code	B-Language
into	O
arbitrary	O
processes	O
is	O
called	O
a	O
DLL	O
injector	O
.	O
</s>
<s>
There	O
are	O
multiple	O
ways	O
on	O
Microsoft	B-Application
Windows	I-Application
to	O
force	O
a	O
process	B-Operating_System
to	O
load	O
and	O
execute	O
code	B-Language
in	O
a	O
DLL	O
that	O
the	O
authors	B-Application
did	O
not	O
intend	O
:	O
</s>
<s>
DLLs	O
listed	O
in	O
the	O
registry	B-Protocol
entry	O
HKEY_LOCAL_MACHINE	O
\SOFTWARE\Microsoft\Windows	O
NT	O
\CurrentVersion\Windows\AppInit_DLLs	O
are	O
loaded	O
into	O
every	O
process	B-Operating_System
that	O
loads	O
User32.dll	O
during	O
the	O
initial	O
call	O
of	O
that	O
DLL	O
.	O
</s>
<s>
Beginning	O
with	O
Windows	B-Application
Vista	I-Application
,	O
AppInit_DLLs	O
are	O
disabled	O
by	O
default	O
.	O
</s>
<s>
Beginning	O
with	O
Windows	O
7	O
,	O
the	O
AppInit_DLL	O
infrastructure	O
supports	O
code	B-Language
signing	O
.	O
</s>
<s>
Starting	O
with	O
Windows	B-Application
8	I-Application
,	O
the	O
entire	O
AppInit_DLL	O
functionality	O
is	O
disabled	O
when	O
Secure	O
Boot	O
is	O
enabled	O
,	O
regardless	O
of	O
code	B-Language
signing	O
or	O
registry	B-Protocol
settings	O
.	O
</s>
<s>
DLLs	O
listed	O
under	O
the	O
registry	B-Protocol
key	I-Protocol
HKEY_LOCAL_MACHINE	O
\SYSTEM\CurrentControlSet\Control\Session	O
Manager	O
\AppCertDLLs	O
are	O
loaded	O
into	O
every	O
process	B-Operating_System
that	O
calls	O
the	O
Win32	O
API	O
functions	O
CreateProcess	O
,	O
CreateProcessAsUser	O
,	O
CreateProcessWithLogonW	O
,	O
CreateProcessWithTokenW	O
and	O
WinExec	O
.	O
</s>
<s>
That	O
is	O
the	O
right	O
way	O
to	O
use	O
legal	O
DLL	B-Operating_System
injection	I-Operating_System
on	O
current	O
version	O
of	O
Windows	O
-	O
Windows	O
10	O
.	O
</s>
<s>
Process	B-Operating_System
manipulation	O
functions	O
such	O
as	O
CreateRemoteThread	O
or	O
code	B-Language
injection	O
techniques	O
such	O
as	O
AtomBombing	O
,	O
can	O
be	O
used	O
to	O
inject	O
a	O
DLL	O
into	O
a	O
program	O
after	O
it	O
has	O
started	O
.	O
</s>
<s>
Open	O
a	O
handle	O
to	O
the	O
target	O
process	B-Operating_System
.	O
</s>
<s>
This	O
can	O
be	O
done	O
by	O
spawning	O
the	O
process	B-Operating_System
or	O
by	O
keying	O
off	O
something	O
created	O
by	O
that	O
process	B-Operating_System
that	O
is	O
known	O
to	O
exist	O
–	O
for	O
instance	O
,	O
a	O
window	O
with	O
a	O
predictable	O
title	O
,	O
or	O
by	O
obtaining	O
a	O
list	O
of	O
running	O
processes	O
and	O
scanning	O
for	O
the	O
target	O
executable	O
's	O
filename	O
.	O
</s>
<s>
Allocate	O
some	O
memory	O
in	O
the	O
target	O
process	B-Operating_System
,	O
and	O
the	O
name	O
of	O
the	O
DLL	O
to	O
be	O
injected	O
is	O
written	O
to	O
it	O
.	O
</s>
<s>
This	O
step	O
can	O
be	O
skipped	O
if	O
a	O
suitable	O
DLL	O
name	O
is	O
already	O
available	O
in	O
the	O
target	O
process	B-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
if	O
a	O
process	B-Operating_System
links	O
to	O
‘	O
User32.dll	O
’	O
,	O
‘	O
GDI32.dll	O
’	O
,	O
‘	O
Kernel32.dll	O
’	O
or	O
any	O
other	O
library	O
whose	O
name	O
ends	O
in	O
‘	O
32.dll	O
’	O
,	O
it	O
would	O
be	O
possible	O
to	O
load	O
a	O
library	O
named	O
‘	O
32.dll	O
’	O
.	O
</s>
<s>
This	O
technique	O
has	O
in	O
the	O
past	O
been	O
demonstrated	O
to	O
be	O
effective	O
against	O
a	O
method	O
of	O
guarding	O
processes	O
against	O
DLL	B-Operating_System
injection	I-Operating_System
.	O
</s>
<s>
Create	O
a	O
new	O
thread	B-Operating_System
in	O
the	O
target	O
process	B-Operating_System
with	O
the	O
thread	B-Operating_System
's	O
start	O
address	O
set	O
to	O
be	O
the	O
address	O
of	O
LoadLibrary	O
and	O
the	O
argument	O
set	O
to	O
the	O
address	O
of	O
the	O
string	O
just	O
uploaded	O
into	O
the	O
target	O
.	O
</s>
<s>
Instead	O
of	O
writing	O
the	O
name	O
of	O
a	O
DLL-to-load	O
to	O
the	O
target	O
and	O
starting	O
the	O
new	O
thread	B-Operating_System
at	O
LoadLibrary	O
,	O
one	O
can	O
write	O
the	O
code-to-be-executed	O
to	O
the	O
target	O
and	O
start	O
the	O
thread	B-Operating_System
at	O
that	O
code	B-Language
.	O
</s>
<s>
Note	O
that	O
without	O
precautions	O
,	O
this	O
approach	O
can	O
be	O
detected	O
by	O
the	O
target	O
process	B-Operating_System
due	O
to	O
the	O
DLL_THREAD_ATTACH	O
notifications	O
sent	O
to	O
every	O
loaded	O
module	O
as	O
a	O
thread	B-Operating_System
starts	O
.	O
</s>
<s>
Use	O
the	O
SuspendThread	O
or	O
NtSuspendThread	O
function	O
to	O
suspend	O
all	O
threads	B-Operating_System
,	O
and	O
then	O
use	O
SetThreadContext	O
or	O
NtSetContextThread	O
function	O
to	O
modify	O
an	O
existing	O
thread	B-Operating_System
's	O
context	O
in	O
the	O
application	O
to	O
execute	O
injected	O
code	B-Language
,	O
that	O
in	O
turn	O
could	O
load	O
a	O
DLL	O
.	O
</s>
<s>
Operating	O
system-level	O
shims	B-General_Concept
.	O
</s>
<s>
On	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
with	O
the	O
dynamic	O
linker	O
based	O
on	O
ld.so	O
(	O
on	O
BSD	B-Operating_System
)	O
and	O
ld-linux.so	O
(	O
on	O
Linux	B-Application
)	O
,	O
arbitrary	O
libraries	O
can	O
be	O
linked	O
to	O
a	O
new	O
process	B-Operating_System
by	O
giving	O
the	O
library	O
's	O
pathname	O
in	O
the	O
LD_PRELOAD	O
environment	O
variable	O
,	O
that	O
can	O
be	O
set	O
globally	O
or	O
individually	O
for	O
a	O
single	O
process	B-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
on	O
a	O
Linux	B-Application
system	O
,	O
this	O
command	O
launches	O
the	O
command	O
"	O
prog	O
"	O
with	O
the	O
shared	O
library	O
from	O
file	O
"	O
test.so	O
 "	O
linked	O
into	O
it	O
at	O
the	O
launchtime	O
:	O
</s>
<s>
With	O
GCC	B-Application
,	O
this	O
involves	O
compiling	O
the	O
source	O
file	O
containing	O
the	O
new	O
globals	O
to	O
be	O
linked	O
,	O
with	O
the	O
or	O
option	O
,	O
and	O
linking	O
with	O
the	O
option	O
.	O
</s>
<s>
On	O
macOS	B-Application
,	O
the	O
following	O
command	O
launches	O
the	O
command	O
"	O
prog	O
"	O
with	O
the	O
shared	O
library	O
from	O
file	O
"	O
test.dylib	O
"	O
linked	O
into	O
it	O
at	O
the	O
launchtime	O
:	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
use	O
debugger-based	O
techniques	O
on	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
As	O
there	O
is	O
no	O
LoadLibrary( )	O
call	O
to	O
load	O
a	O
DLL	O
into	O
a	O
foreign	O
process	B-Operating_System
you	O
have	O
to	O
copy	O
a	O
locally	O
loaded	O
DLL	O
into	O
remotely	O
allocated	O
memory	O
.	O
</s>
<s>
The	O
following	O
commented	O
code	B-Language
shows	O
how	O
to	O
do	O
that	O
.	O
</s>
<s>
The	O
main	O
issue	O
solved	O
here	O
is	O
that	O
a	O
locally	O
loaded	O
DLL	O
copied	O
to	O
a	O
remote	O
process	B-Operating_System
must	O
occupy	O
the	O
same	O
addresses	O
as	O
in	O
the	O
injecting	O
process	B-Operating_System
.	O
</s>
<s>
The	O
above	O
code	B-Language
does	O
this	O
by	O
allocating	O
memory	O
for	O
the	O
same	O
address	B-General_Concept
range	I-General_Concept
as	O
occupied	O
before	O
in	O
the	O
injecting	O
process	B-Operating_System
.	O
</s>
<s>
If	O
this	O
fails	O
the	O
DLL	O
is	O
locally	O
freed	O
,	O
the	O
former	O
address	B-General_Concept
range	I-General_Concept
is	O
marked	O
as	O
reserved	O
,	O
and	O
the	O
LoadLibrary( )	O
call	O
is	O
tried	O
again	O
.	O
</s>
<s>
By	O
reserving	O
the	O
former	O
address	B-General_Concept
range	I-General_Concept
the	O
code	B-Language
prevents	O
that	O
the	O
next	O
LoadLibrary( )	O
apptempt	O
will	O
assign	O
the	O
same	O
address	B-General_Concept
range	I-General_Concept
as	O
used	O
before	O
.	O
</s>
<s>
The	O
main	O
drawback	O
with	O
that	O
approach	O
is	O
that	O
the	O
DLL	O
copied	O
into	O
the	O
foreign	O
process	B-Operating_System
is	O
that	O
there	O
are	O
n't	O
any	O
other	O
DLL	O
library	O
dependencies	O
of	O
that	O
DLL	O
loaded	O
into	O
the	O
foreign	O
address	B-General_Concept
space	I-General_Concept
or	O
pointers	O
,	O
f.e.	O
</s>
<s>
function	O
calls	O
,	O
to	O
DLLs	O
loaded	O
by	O
the	O
foreign	O
process	B-Operating_System
are	O
adjusted	O
according	O
to	O
the	O
dependencies	O
of	O
the	O
copied	O
DLL	O
.	O
</s>
<s>
Some	O
DLLs	O
like	O
kernel32.dll	O
are	O
reliably	O
loaded	O
in	O
the	O
early	O
beginning	O
when	O
the	O
process	B-Operating_System
address	B-General_Concept
space	I-General_Concept
is	O
occupied	O
by	O
the	O
executable	O
image	O
and	O
its	O
depending	O
DLLs	O
.	O
</s>
<s>
The	O
path	O
to	O
that	O
DLL	O
is	O
copied	O
to	O
the	O
foreign	O
address	B-General_Concept
space	I-General_Concept
and	O
given	O
as	O
a	O
void-parameter	O
to	O
the	O
thread-function	O
.	O
</s>
<s>
The	O
following	O
code	B-Language
is	O
the	O
source	O
of	O
the	O
remotely	O
copied	O
loader	O
DLL	O
which	O
only	O
does	O
kernel32.dll	O
calls	O
:	O
</s>
<s>
The	O
last	O
code	B-Language
shows	O
an	O
example	O
of	O
a	O
DLL	O
loaded	O
by	O
the	O
loader	O
DLL	O
which	O
prints	O
the	O
parameters	O
to	O
a	O
file	O
.	O
</s>
<s>
The	O
only	O
export	O
is	O
that	O
of	O
initData	O
,	O
which	O
receives	O
the	O
parameters	O
given	O
by	O
the	O
injecting	O
process	B-Operating_System
through	O
the	O
loader	O
DLL	O
.	O
</s>
<s>
And	O
one	O
must	O
be	O
aware	O
that	O
the	O
thread	B-Operating_System
created	O
from	O
a	O
DllMain-function	O
is	O
n't	O
scheduled	O
until	O
after	O
its	O
DLL_THREAD_ATTACH-function	O
has	O
succeeded	O
.	O
</s>
<s>
So	O
there	O
may	O
not	O
be	O
any	O
synchronization	O
from	O
inside	O
DllMain	O
with	O
the	O
created	O
thread	B-Operating_System
.	O
</s>
