Dll injection classic method full version

Source: Internet
Author: User

Pnig0s1992: a review of the most typical textbook Dll injection.

Summarize the basic injection process, including injection and uninstallation.

Inject Dll:

1. OpenProcess obtains the handle of the process to be injected.

2. VirtualAllocEx creates a memory segment in a remote process. The length is strlen (dllname) + 1;

3. WriteProcessMemory writes the Dll name to the memory opened in step 2.

4. CreateRemoteThread uses LoadLibraryA as the thread function. The parameter is the Dll name and a new thread is created.

5. CloseHandle closes the thread handle

Uninstall Dll:

1. CreateRemoteThread injects GetModuleHandle into a remote process. The parameter is the injected Dll name.

2. GetExitCodeThread uses the exit code of the thread as the handle value of the Dll module.

3. CloseHandle closes the thread handle

3. CreateRemoteThread injects FreeLibraryA into a remote process. The parameter is the handle value obtained in step 2.

4. WaitForSingleObject waits for the object handle to return

5. CloseHandle closes the thread and process handle.

 
 
  1. // Code By Pnig0s1992
  2. // Date: Maid, 13
  3. # Include <stdio. h>
  4. # Include <Windows. h>
  5. # Include <TlHelp32.h>
  6.  
  7.  
  8. DWORD getProcessHandle (LPCTSTR lpProcessName) // query the process PID based on the process name
  9. {
  10. DWORD dwRet = 0;
  11. HANDLE hSnapShot = createconlhelp32snapshot (TH32CS_SNAPPROCESS, 0 );
  12. If (hSnapShot = INVALID_HANDLE_VALUE)
  13. {
  14. Printf ("\ n failed to get process snapshot % d", GetLastError ());
  15. Return dwRet;
  16. }
  17.  
  18. PROCESSENTRY32 pe32; // declare the process entry object
  19. Pe32.dwSize = sizeof (PROCESSENTRY32); // fill in the size of the Process entry object
  20. Process32First (hSnapShot, & pe32); // traverses the Process List
  21. Do
  22. {
  23. If (! Lstrcmp (pe32.szExeFile, lpProcessName) // find the PID of the specified process name
  24. {
  25. DwRet = pe32.th32ProcessID;
  26. Break;
  27. }
  28. } While (Process32Next (hSnapShot, & pe32 ));
  29. CloseHandle (hSnapShot );
  30. Return dwRet; // return
  31. }
  32.  
  33. INT main (INT argc, CHAR * argv [])
  34. {
  35. DWORD dwPid = getProcessHandle (LPCTSTR) argv [1]);
  36. LPCSTR lpDllName = "EvilDll. dll ";
  37. HANDLE hProcess = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, dwPid );
  38. If (hProcess = NULL)
  39. {
  40. Printf ("\ n error in obtaining Process Handle % d", GetLastError ());
  41. Return-1;
  42. }
  43. DWORD dwSize = strlen (lpDllName) + 1;
  44. DWORD dwHasWrite;
  45. LPVOID lpRemoteBuf = VirtualAllocEx (hProcess, NULL, dwSize, MEM_COMMIT, PAGE_READWRITE );
  46. If (WriteProcessMemory (hProcess, lpRemoteBuf, lpDllName, dwSize, & dwHasWrite ))
  47. {
  48. If (dwHasWrite! = DwSize)
  49. {
  50. VirtualFreeEx (hProcess, lpRemoteBuf, dwSize, MEM_COMMIT );
  51. CloseHandle (hProcess );
  52. Return-1;
  53. }
  54.  
  55. } Else
  56. {
  57. Printf ("\ n error in writing remote process memory space % d. ", GetLastError ());
  58. CloseHandle (hProcess );
  59. Return-1;
  60. }
  61.  
  62. DWORD dwNewThreadId;
  63. LPVOID lpLoadDll = LoadLibraryA;
  64. HANDLE hNewRemoteThread = CreateRemoteThread (hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) lpLoadDll, lpRemoteBuf, 0, & dwNewThreadId );
  65. If (hNewRemoteThread = NULL)
  66. {
  67. Printf ("\ n failed to establish remote thread % d", GetLastError ());
  68. CloseHandle (hProcess );
  69. Return-1;
  70. }
  71.  
  72. WaitForSingleObject (hNewRemoteThread, INFINITE );
  73. CloseHandle (hNewRemoteThread );
  74.  
  75. // Prepare to uninstall the injected Dll
  76. DWORD dwHandle, dwID;
  77. LPVOID pFunc = GetModuleHandleA; // obtain the handle of the Dll injected in the remote thread
  78. HANDLE hThread = CreateRemoteThread (hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) pFunc, lpRemoteBuf, 0, & dwID );
  79. WaitForSingleObject (hThread, INFINITE );
  80. GetExitCodeThread (hThread, & dwHandle); // The end code of the thread is the handle of the Dll module.
  81. CloseHandle (hThread );
  82. PFunc = FreeLibrary;
  83. HThread = CreateRemoteThread (hThread, NULL, 0, (LPTHREAD_START_ROUTINE) pFunc, (LPVOID) dwHandle, 0, & dwID); // inject FreeLibraryA into a remote thread to uninstall the Dll.
  84. WaitForSingleObject (hThread, INFINITE );
  85. CloseHandle (hThread );
  86. CloseHandle (hProcess );
  87. Return 0;
  88. }

 

This article is from the "About: Blank H4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/804484

Related Article

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.