C # process injection code

Source: Internet
Author: User

Turn, still don't know can use don't http://community.csdn.net/Expert/topic/5645/5645084.xml? Temp =. 8446161.

The steps for using createremotethread and loadlibrary are as follows:
1. Get the handle of the Remote Process (using OpenProcess ).
2. allocate memory (virtualallocex) for the DLL file name in a remote process ).
3. Write the DLL file name (full path) to the allocated memory (writeprocessmemory)
4. Use createremotethread and loadlibrary to map your DLL to a remote process.
5. Wait for the remote thread to end (waitforsingleobject), that is, wait for loadlibrary to return. That is to say, when our dllmain (called with the dll_process_attach parameter) returns, the remote thread will immediately end.
6. Get the end code (getexitcodethtread) of the remote thread, that is, the return value of loadlibrary-the base address (hmodule) of our DLL load ).
7. Release the memory allocated in step 1 (virtualfreeex ).
8. Use createremotethread and freelibrary to detach the DLL from a remote process. When calling, pass the hmodule obtained in step 1 to freelibrary (using the lpparameter parameter of createremotethread ).
9. Wait for the end of the thread (waitsingleobject ).

10. Use C ++/CLI to write:

// Injectdll. h

# Pragma once

# Include
< Windows. h >
Using   Namespace System;

Namespace Injectdll {

Public   Ref   Class Injectdllmanager
{
Public :
// Szlibpath-DLL to be loaded, hprocess-mounted remote process, ilibpathsize-length of the DLL name to be loaded
Void Injectlib2process ( Char Szlibpath [], handle hprocess, Int Ilibpathsize)
{
Handle hthread;
Void * Plibremote;
DWORD hlibmodule;
// The base address (hmodule) of the loaded DLL );
Hmodule hkernel32 = Getmodulehandle (L " Kernel32 " );
// Initialize szlibpath
//
// 1. allocate memory for szlibpath in a remote process
// 2. Write szlibpath to the allocated memory
Plibremote = Virtualallocex (hprocess, null, ilibpathsize,
Mem_commit, page_readwrite );
Writeprocessmemory (hprocess, plibremote ,(
Void * ) Szlibpath,
Sizeof (Szlibpath), null );
// Load "szlibpath. dll" to Remote Process
// (Through createremotethread & loadlibrary)
Hthread = Createremotethread (hprocess, null, 0 ,
(Lpthread_start_routine) getprocaddress (hkernel32,
" Loadlibrarya " ), Plibremote, 0 , Null );
Waitforsingleobject (hthread, infinite );
// Obtain the base address of the DLL
Getexitcodethread (hthread, & Hlibmodule );
// Tail Scanning
Closehandle (hthread );
Virtualfreeex (hprocess, plibremote, ilibpathsize, mem_release );
// Detach libspu. dll from the target process
// (Through createremotethread & freelibrary)
Hthread = Createremotethread (hprocess, null, 0 ,
(Lpthread_start_routine) getprocaddress (hkernel32,
" Freelibrary " ),( Void * ) Hlibmodule, 0 , Null );
Waitforsingleobject (hthread, infinite );
// Tail Scanning
Closehandle (hthread );
}

}
;
}

 

Generate the DLL, reference it in the C # project, and write the following code:

Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}


Private   Void Form1_load ( Object Sender, eventargs E)
{
Injectdll. injectdllmanager imanager
= NewInjectdll. injectdllmanager ();
Imanager. injectlib2process (.);
// 
}

}


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.