CreateRemoteThread remote thread injection DLL and hook

Source: Internet
Author: User

Although CreateRemoteThread is easy to detect, it is useful in some situations. Always try to find the previous code when you want to use it, and now record it here.

CreateRemoteThread Remote Injection
DWORD Dwoffect,dwargu; BOOL Createremotedll (Const Char*dllfullpath,ConstDWORD Dwremoteprocessid, DWORD Dwoffect,dword Dwargu)    {HANDLE htoken; if(OpenProcessToken (getcurrentprocess (),token_adjust_privileges,&htoken))        {Token_privileges TKP; Lookupprivilegevalue (Null,se_debug_name,&TKP. privileges[0]. LUID);//Modify Process PermissionsTkp. Privilegecount=1; Tkp. privileges[0]. attributes=se_privilege_enabled; AdjustTokenPrivileges (Htoken,false,&TKP,sizeofTkp,null,null);//notifies the system to modify process permissionsCloseHandle (Htoken);    } HANDLE hremoteprocess; //open a remote thread    if((hremoteprocess = openprocess (Process_create_thread |//allow remote creation of threadsprocess_vm_operation |//allow remote VM operationsProcess_vm_write,//allow remote VMs to writeFALSE, dwremoteprocessid) = =NULL) {        returnFALSE; }    Char*Pszlibfileremote; //The memory address space of the remote process allocates the DLL file name bufferPszlibfileremote = (Char*) VirtualAllocEx (hremoteprocess, NULL, Lstrlen (Dllfullpath) +1, Mem_commit, page_readwrite); if(Pszlibfileremote = =NULL)        {CloseHandle (hremoteprocess); returnFALSE; }    //Copy the path name of the DLL to the memory space of the remote process    if(WriteProcessMemory (Hremoteprocess, Pszlibfileremote, (void*) Dllfullpath, Lstrlen (Dllfullpath) +1, NULL) = =0) {CloseHandle (hremoteprocess); returnFALSE; }    //Calculate the entry address for the LoadLibraryAPthread_start_routine pfnstartaddr =(Pthread_start_routine) GetProcAddress (GetModuleHandle (TEXT ("Kernel32")),"LoadLibraryA"); if(Pfnstartaddr = =NULL) {        returnFALSE;    } HANDLE Hremotethread; Hremotethread= CreateRemoteThread (hremoteprocess, NULL,0, Pfnstartaddr, Pszlibfileremote,0, NULL);    WaitForSingleObject (Hremotethread,infinite); if(Hremotethread = =NULL)        {CloseHandle (hremoteprocess); returnFALSE;    } DWORD dwdlladdr; GetExitCodeThread (Hremotethread,&dwdlladdr); if(dwdlladdr!=0) {dwdlladdr+=Dwoffect;        HANDLE Hhookfunc; Hhookfunc= CreateRemoteThread (hremoteprocess, NULL,0, (Pthread_start_routine) dwdlladdr, (LPVOID) Dwargu,0, NULL);        WaitForSingleObject (Hhookfunc,infinite); if(Hhookfunc = =NULL)            {CloseHandle (hremotethread);            CloseHandle (hremoteprocess); returnFALSE;    } closehandle (Hhookfunc); }    Else{CloseHandle (hremoteprocess);        CloseHandle (Hremotethread); returnFALSE;    } closehandle (hremoteprocess);    CloseHandle (Hremotethread); returnTRUE;}voidHook (intdwpid) {     Charcurpath[260]; GetModuleFileName (Null,curpath,260); *STRRCHR (Curpath,'\\') =' /'; strcat (Curpath,"\\this.dll"); Hmodule Htmpdll=LoadLibrary (Curpath); Dwoffect= (DWORD) GetProcAddress (Htmpdll,"Hookfun"); Dwoffect-=(DWORD) Htmpdll;    FreeLibrary (Htmpdll); Createremotedll (Curpath,dwpid,dwoffect,dwargu);}

Hook code

__declspec (naked)voidmyhookgetres () {__asm {Pushad pushfd} myfun (); __asm {POPFD popad add esp,0xcjmp Uretaddr}} ULONG uhookaddr=0x11111+(DWORD) hmodule; HANDLE HANDLE=getcurrentprocess ();Charmyjmp[5]={0}; myjmp[0]=(Char)0xe9; ULONG utempaddr=(ULONG) myjmp;uretaddr= Uhookaddr +5; ULONG uskilljmp= (ULONG) myhookgetres-uhookaddr-5; __asm{mov eax,uskilljmp mov ebx, utempaddr add ebx,1mov [EBX],EAX mov ecx,[ebx]} WriteProcessMemory (handle, (LPVOID) (UHOOKADDR), (LPVOID) myjmp,5, NULL);

CreateRemoteThread remote thread injection DLL and hook

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.