943,717 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9169
  • C++ RSS
Jun 23rd, 2008
0

Dll Loader/Injector

Expand Post »
Hey guys im just wondering if any guys have any clue on building a DLL Injector/Loader
As in, in a folder you have Blah.exe and Blah.dll when Blah.exe is run it injects the dll into a specified process (via blah.ini)

Now i know i need to use LoadLibrary or CreateRemote Thread but i dont know where to start.
Just wondering if you guys could get me started or point me in the direction of some examples.
I've been searching on google for a while now and havnt come up with anything that is remotely helpful.

Kind Regards
FTProtocol
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jun 23rd, 2008
0

Re: Dll Loader/Injector

You mean you want to write a program that injects a DLL into another already running program? As far as I know that can't be done because when the operating system loads a dll it adds the code in the dll to the address space of the program that uses it. Unless you write your own operating system that behavior won't be (easily) possible for you to duplicate.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jun 23rd, 2008
0

Re: Dll Loader/Injector

its been done in VB so i dont see why it cant be done in c++.....
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jun 23rd, 2008
0

Re: Dll Loader/Injector

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
its been done in VB so i dont see why it cant be done in c++.....
http://www.codeproject.com/KB/threads/winspy.aspx
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 24th, 2008
0

Re: Dll Loader/Injector

C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tlhelp32.h>
  4. #include <shlwapi.h>
  5.  
  6. #define PROCESS_NAME "target.exe"
  7. #define DLL_NAME "injected.dll"
  8.  
  9.  
  10. //I could just use PROCESS_ALL_ACCESS but it's always best to use the absolute bare minimum of priveleges, so that your code works in as
  11. //many circumstances as possible.
  12. #define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)
  13.  
  14. BOOL WriteProcessBYTES(HANDLE hProcess,LPVOID lpBaseAddress,LPCVOID lpBuffer,SIZE_T nSize);
  15.  
  16. BOOL LoadDll(char *procName, char *dllName);
  17. BOOL InjectDLL(DWORD ProcessID, char *dllName);
  18. unsigned long GetTargetProcessIdFromProcname(char *procName);
  19.  
  20. bool IsWindowsNT()
  21. {
  22. // check current version of Windows
  23. DWORD version = GetVersion();
  24. // parse return
  25. DWORD majorVersion = (DWORD)(LOBYTE(LOWORD(version)));
  26. DWORD minorVersion = (DWORD)(HIBYTE(LOWORD(version)));
  27. return (version < 0x80000000);
  28. }
  29.  
  30. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
  31. {
  32. if(IsWindowsNT())
  33. LoadDll(PROCESS_NAME, DLL_NAME);
  34. else
  35. MessageBox(0, "Your system does not support this method", "Error!", 0);
  36.  
  37. return 0;
  38. }
  39.  
  40.  
  41. BOOL LoadDll(char *procName, char *dllName)
  42. {
  43. DWORD ProcID = 0;
  44.  
  45. ProcID = GetTargetProcessIdFromProcname(procName);
  46.  
  47. if(!(InjectDLL(ProcID, dllName)))
  48. MessageBox(NULL, "Process located, but injection failed", "Loader", NULL);
  49.  
  50. return true;
  51. }
  52.  
  53. BOOL InjectDLL(DWORD ProcessID, char *dllName)
  54. {
  55. HANDLE Proc;
  56. char buf[50]={0};
  57. LPVOID RemoteString, LoadLibAddy;
  58.  
  59. if(!ProcessID)
  60. return false;
  61.  
  62. Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);
  63.  
  64. if(!Proc)
  65. {
  66. sprintf(buf, "OpenProcess() failed: %d", GetLastError());
  67. MessageBox(NULL, buf, "Loader", NULL);
  68. return false;
  69. }
  70.  
  71. LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  72.  
  73. RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_NAME), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
  74. WriteProcessMemory(Proc, (LPVOID)RemoteString, dllName, strlen(dllName), NULL);
  75. CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, (LPVOID)RemoteString, NULL, NULL);
  76.  
  77. CloseHandle(Proc);
  78.  
  79. return true;
  80. }
  81.  
  82. unsigned long GetTargetProcessIdFromProcname(char *procName)
  83. {
  84. PROCESSENTRY32 pe;
  85. HANDLE thSnapshot;
  86. BOOL retval, ProcFound = false;
  87.  
  88. thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  89.  
  90. if(thSnapshot == INVALID_HANDLE_VALUE)
  91. {
  92. MessageBox(NULL, "Error: unable to create toolhelp snapshot", "Loader", NULL);
  93. return false;
  94. }
  95.  
  96. pe.dwSize = sizeof(PROCESSENTRY32);
  97.  
  98. retval = Process32First(thSnapshot, &pe);
  99.  
  100. while(retval)
  101. {
  102. if(StrStrI(pe.szExeFile, procName) )
  103. {
  104. ProcFound = true;
  105. break;
  106. }
  107.  
  108. retval = Process32Next(thSnapshot,&pe);
  109. pe.dwSize = sizeof(PROCESSENTRY32);
  110. }
  111.  
  112. return pe.th32ProcessID;
  113. }

Someone gave me this yesterday but it doesnt seem to work? i changed the dll name and process name but it doesnt inject.

I dont really want to try and pull apart an example that doesnt work as well it may be all wrong and theres no point in learning something that is wrong.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jun 24th, 2008
0

Re: Dll Loader/Injector

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
Someone gave me this yesterday but it doesnt seem to work?
I dont really want to try and pull apart an example that doesnt work as well it may be all wrong and theres no point in learning something that is wrong.
What does it fail to do? What code do you have in the .DLL?

I gave the code you've tried a test ride and it worked. The code for the .DLL that I injected is below ...

C++ Syntax (Toggle Plain Text)
  1. BOOL APIENTRY DllMain( HANDLE hModule,
  2. DWORD ul_reason_for_call,
  3. LPVOID lpReserved)
  4. {
  5. if(ul_reason_for_call == DLL_PROCESS_ATTACH)
  6. {
  7. MessageBox(NULL,
  8. "inject.cpp -> DLL_PROCESS_ATTACH",
  9. "Injected",
  10. MB_ICONINFORMATION);
  11. }
  12.  
  13. return TRUE;
  14. }

Please note that the topic is non-trivial, so everything might not work out of the box.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 24th, 2008
0

Re: Dll Loader/Injector

my DLL is working and i know that for sure because when i use an already posted and compiled dll injector it works. but heres the code so i definately its not injection the dll correctly or something like this.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jun 25th, 2008
0

Re: Dll Loader/Injector

bump?
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jun 25th, 2008
0

Re: Dll Loader/Injector

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
my DLL is working and i know that for sure because when i use an already posted and compiled dll injector it works.
Umm .. out of that I sort of figured that you already have a working injection (?).

If not, then again, the example code you've posted, seems to be capable of doing the injection. So, I take that your DLL along with that example code, is doing something that breaks the injection or maybe you are erroneously expecting something to happen in your DLL. Really difficult to say anything more, not knowing a bit of your DLL's code.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: OUTPUT problem
Next Thread in C++ Forum Timeline: need help with ASCII





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC