Thread: DLL Injection

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    DLL Injection

    Hello,


    I built a DLL injector that injects a dll into a process (obvious xD) but my problem is that when i inject my DLL the code in it doesnt run.

    So after a lot of reserch i discovered that it would be necessary DllMain.

    I'm tired of trying to use DllMain and it nevers works.

    Could someone please paste a simple code of a DLL (like creating a blank file in C:\)
    My DLL Injector code
    Code:
    #define PROCESS_NAME "explorer.exe"
    #define DLL_NAME "inject.dll"
    #define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)
    unsigned long GetTargetProcessIdFromProcname(char *procName)
    {
       PROCESSENTRY32 pe;
       HANDLE thSnapshot;
       BOOL retval, ProcFound = 0;
    
       thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    
       if(thSnapshot == INVALID_HANDLE_VALUE)
       {
          puts("Erro");
          return 0;
       }
    
       pe.dwSize = sizeof(PROCESSENTRY32);
    
        retval = Process32First(thSnapshot, &pe);
    
       while(retval)
       {
          if(strstr(pe.szExeFile, procName) )
          {
             ProcFound = 1;
             break;
          }
    
          retval    = Process32Next(thSnapshot,&pe);
          pe.dwSize = sizeof(PROCESSENTRY32);
      }
    
       return pe.th32ProcessID;
    }
    main()
    {
    	unsigned long id;
    	DWORD *pid;
    	HANDLE hd;
    	LPVOID gp,rs;
    	
    	gp=(LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
    
    	id=GetTargetProcessIdFromProcname(PROCESS_NAME);
    
    	hd=OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
    
    	rs=(LPVOID)VirtualAllocEx(hd, NULL, strlen(DLL_NAME), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
    
    	if(!WriteProcessMemory(hd, (LPVOID)rs, DLL_NAME, strlen(DLL_NAME), NULL))
    	{
    		puts("merda");
    	}
    	if(!CreateRemoteThread(hd, NULL, 0, (LPTHREAD_START_ROUTINE)gp, (LPVOID)rs, 0, NULL))
    		puts("merda2");
    
    	system("pause");
    
    }
    I need help, this is driving me cr4zy
    PS: Using visual c++ 2010

    EDIT: Sry placed topic on wrong section it's c++
    Last edited by Cr4zYPT; 09-09-2011 at 11:01 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cr4zYPT View Post
    Hello,
    I built a DLL injector that injects a dll into a process (obvious xD) but my problem is that when i inject my DLL the code in it doesnt run.
    So after a lot of reserch i discovered that it would be necessary DllMain.
    That's because you are trying to write a program... not a library. DLL code, whether injected or not, does not run unless it is actually called...

    Dynamic-Link Libraries (Windows) <-- Everything you need is there.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    And what exactly are you trying to accomplish here? Please do note the following:
    Quote Originally Posted by Kermi3:Forum Guidelines
    6. Messages relating to cracking, (erroneously called "hacking" by many), copyright violations, or other illegal activities will be deleted. Due to the overlapping boundaries of code with malicious intent, and other legitimate uses of it, the moderators will assess each potential infraction on a case by case basis.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Nothing malicous, i'm trying to access the System account in windows. And i thought that if a injected a DLL in a system process i could access files with system level priviligies.

    I've read that link and i still don't have a clue about DllMain

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Cr4zYPT View Post
    Nothing malicous, i'm trying to access the System account in windows. And i thought that if a injected a DLL in a system process i could access files with system level priviligies.
    Those two statements are mutually exclusive.......... If you have rights on the system there are API calls that you can use to perform any task you require.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    I'm admnistrator but i want the system account, i've used systools and those granted me a console with system level priviligies now i'm trying with dll injection

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cr4zYPT View Post
    Nothing malicous, i'm trying to access the System account in windows. And i thought that if a injected a DLL in a system process i could access files with system level priviligies.

    I've read that link and i still don't have a clue about DllMain
    AHEM... THAT is malicious code. You are trying to access files you have no rights to access on the system. Those files are user settings, passwords, etc. There is no valid reason to bypass system security to get there.

    Sorry... help session over.
    Last edited by CommonTater; 09-09-2011 at 11:42 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post about what you are really (and legitimately) trying to do in the Windows programming forum.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dll injection
    By mundaneblur in forum Windows Programming
    Replies: 0
    Last Post: 09-16-2010, 12:58 AM
  2. DLL Injection, Help please
    By AlexWu in forum C Programming
    Replies: 12
    Last Post: 05-05-2010, 08:50 PM
  3. DLL Injection
    By n1mda in forum C Programming
    Replies: 25
    Last Post: 02-13-2008, 10:11 PM
  4. DLL Injection
    By Lionel in forum Windows Programming
    Replies: 6
    Last Post: 09-25-2005, 12:41 PM
  5. DLL Injection
    By Lionel in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2005, 05:18 PM