In computer programming, DLL injection is a technique used for running code within the address space of another process by forcing it to load a dynamic-link library. DLL injection is often used by external programs to influence the behavior of another program in a way its authors did not anticipate or intend. For example, the injected code could hook system function calls, or read the contents of password textboxes, which cannot be done the usual way. A program used to inject arbitrary code into arbitrary processes is called a DLL injector.
There are multiple ways on Microsoft Windows to force a process to load and execute code in a DLL that the authors did not intend:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs are loaded into every process that loads User32.dll during the initial call of that DLL. Beginning with Windows Vista, AppInit_DLLs are disabled by default. Beginning with Windows 7, the AppInit_DLL infrastructure supports code signing. Starting with Windows 8, the entire AppInit_DLL functionality is disabled when Secure Boot is enabled, regardless of code signing or registry settings.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\AppCertDLLs are loaded into every process that calls the Win32 API functions CreateProcess, CreateProcessAsUser, CreateProcessWithLogonW, CreateProcessWithTokenW and WinExec. That is the right way to use legal DLL injection on current version of Windows - Windows 10. DLL must be signed by a valid certificate.CreateRemoteThread or code injection techniques such as AtomBombing, can be used to inject a DLL into a program after it has started.
32.dll, it would be possible to load a library named 32.dll[citation needed]. This technique has in the past been demonstrated to be effective against a method of guarding processes against DLL injection.LoadLibrary and the argument set to the address of the string just uploaded into the target.
LoadLibrary, one can write the code-to-be-executed to the target and start the thread at that code.DLL_THREAD_ATTACH notifications sent to every loaded module as a thread starts.SetWindowsHookEx.SuspendThread or NtSuspendThread function to suspend all threads, and then use SetThreadContext or NtSetContextThread function to modify an existing thread's context in the application to execute injected code, that in turn could load a DLL.LoadLibrary or LoadLibraryEx function without specifying a full-qualified path to the DLL being loaded.On Unix-like operating systems with the dynamic linker based on ld.so (on BSD) and ld-linux.so (on Linux), arbitrary libraries can be linked to a new process by giving the library's pathname in the LD_PRELOAD environment variable, that can be set globally or individually for a single process.
For example, on a Linux system, this command launches the command "prog" with the shared library from file "test.so" linked into it at the launchtime:
LD_PRELOAD="./test.so" prog
Such a library can be created in the same way as other shared objects. With GCC, this involves compiling the source file containing the new globals to be linked, with the -fpic or -fPIC option, and linking with the -shared option. The library has access to external symbols declared in the program like any other library.
On macOS, the following command launches the command "prog" with the shared library from file "test.dylib" linked into it at the launchtime:
DYLD_INSERT_LIBRARIES="./test.dylib" DYLD_FORCE_FLAT_NAMESPACE=1 prog
It is also possible to use debugger-based techniques on Unix-like systems.
-fpic Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. sqq.-shared Produce a shared object which can then be linked with other objects to form an executable. sqq.