VC++进程注入

【2014/10/19 11:12】

项目编码:

头文件:

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

//myself
#include <Windows.h>

#include <TlHelp32.h>

#include <iostream>
using namespace std;

// TODO: 在此处引用程序需要的其他头文件

程序代码:

// Inject.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

/*
* 提升进程的权限
*/
int EnableDebugePriv(const char * name){

	HANDLE hToken;
	/*
	* 访问令牌特权结构体
	* PrivilegeCount: the number of  entries in the Privileges array
	* Privileges[ANYSIZE_ARRAY]:
	*   LUID :特权属性的名字
	*   SE_PRIVILEGE_ENABLED :启用了特权
	*/
	TOKEN_PRIVILEGES tp;
	/*
	* locally unique identifier (LUID) is guaranteed only until the system is restarted.
	* call the LookupPrivilegeName function, passing the address of the LUID as the value of the lpLuid parameter.
	*/
	LUID luid;

	/*
	* The OpenProcessToken function opens the access token associated with a process.(打开进程令牌环)
	*   ProcessHandle [in]: A handle to the process whose access token is opened.
	*   DesiredAccess [in]:Required to enable or disable the privileges in an access token.
	*   TokenHandle [out] :A pointer to a handle that identifies the newly opened access token when the function returns.
	*/
	OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken);

	/*
	* [lpSystemName]- [in, optional] :If a null string is specified, the function attempts to find the privilege name on the local system.
	* [lpName]-[in]  : 可以指定为一个常量
	* [lpLuid]-[out] : 返回一个指定的LUID
	*/
	LookupPrivilegeValue(NULL,name,&luid);

	tp.PrivilegeCount=1;
	tp.Privileges[0].Luid=luid;
	tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;

	/*
	* Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access.
	*   TokenHandle [in]: The handle must have TOKEN_ADJUST_PRIVILEGES access to the token. If the PreviousState parameter is not NULL,
	*                    the handle must also have TOKEN_QUERY access.
	*   DisableAllPrivileges [in]: Specifies whether the function disables all of the token's privileges.If it is FALSE, the function
	*							   modifies privileges based on the information pointed to by the NewState parameter.
	*   NewState [in, optional]: the DisableAllPrivileges parameter is FALSE, the AdjustTokenPrivileges function enables,
	*							 disables, or removes these privileges for the token.
	*   BufferLength [in]: Specifies the size, in bytes, of the buffer pointed to by the PreviousState parameter.
	*   PreviousState [out, optional]:  If the PrivilegeCount member of TOKEN_PRIVILEGES is zero,
	*									then no privileges have been changed by this function. This parameter can be NULL.
	*   ReturnLength [out, optional]:  pointer to a variable that receives the required size, in bytes, of the buffer pointed to
	*									by the PreviousState parameter. This parameter can be NULL if PreviousState is NULL.
	*
	*/
	AdjustTokenPrivileges(hToken,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);

	return 0;

}

BOOL InjectDLL(const char * DllFullPath, const DWORD dwRemoteProcessId){

	HANDLE hRemoteProcess;
	/*
	*
	*/
	EnableDebugePriv(SE_DEBUG_NAME);

	//打开远程线程
	/*
	* Opens an existing local process object.
	*   dwDesiredAccess [in] : 对进程的访问类型
	*	bInheritHandle [in]  :
	*	dwProcessId [in]	 : The identifier of the local process to be opened.
	*/
	hRemoteProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwRemoteProcessId);

	char * pszLibFileRemote;
	//使用VirtualAllocEx函数在远程进程的内存地址空间分配DLL文件名空间
	/*
	* Reserves or commits a region of memory within the virtual address space of a specified process.
	* The function initializes the memory it allocates to zero
	*   hProcess [in] : The handle to a process. The function allocates memory within the virtual address space of this process.
	*	lpAddress [in, optional] : If lpAddress is NULL, the function determines where to allocate the region.
	*	dwSize [in] : The size of the region of memory to allocate, in bytes.
	*	flAllocationType [in] : The type of memory allocation.
	*	flProtect [in] :The memory protection for the region of pages to be allocated.
	*/
	pszLibFileRemote=(char *)VirtualAllocEx(hRemoteProcess,NULL,lstrlen(DllFullPath)+1,MEM_COMMIT,PAGE_READWRITE);

	//使用WriteProcessMemory函数将DLL的路径名写入到远程进程的内存空间
	/*
	* Writes data to an area of memory in a specified process.
	*   hProcess [in]: A handle to the process memory to be modified.
	*	lpBaseAddress [in] :A pointer to the base address in the specified process to which data is written.
	*	lpBuffer [in] :A pointer to the buffer that contains data to be written in the address space of the specified process.
	*	nSize [in] :The number of bytes to be written to the specified process.
	*	lpNumberOfBytesWritten [out] : If lpNumberOfBytesWritten is NULL, the parameter is ignored.
	*
	*/
	WriteProcessMemory(hRemoteProcess,pszLibFileRemote,(void *)DllFullPath,lstrlen(DllFullPath)+1,NULL);

	//在宿主中启用新的线程使用LoadLibraryA()函数加载的
	//计算LoadLibraryA的入口地址
	/*
	* Retrieves(检索) the address of an exported function or variable from the specified dynamic-link library (DLL).
	*   hModule [in] : A handle to the DLL module that contains the function or variable.
	*				   The LoadLibrary, LoadLibraryEx, LoadPackagedLibrary, or GetModuleHandle function returns this handle.
	*	lpProcName [in] : The function or variable name, or the function's ordinal value. If this parameter is an ordinal value,
	*					   it must be in the low-order word; the high-order word must be zero.
	*/

	//计算LoadLibraryA的入口地址
	PTHREAD_START_ROUTINE pfnStartAddr=(PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32.dll")),"LoadLibraryA");

	//启动远程线程LoadLibraryA,通过远程线程调用创建新的线程
	HANDLE hRemoteThread;

	/*
	* Creates a thread that runs in the virtual address space of another process.
	*   hProcess [in] : A handle to the process in which the thread is to be created.
	*	lpThreadAttributes [in] : A pointer to a SECURITY_ATTRIBUTES structure that specifies a security descriptor
	*							  for the new thread and determines whether child processes can inherit the returned handle.
	*							  If lpThreadAttributes is NULL, the thread gets a default security descriptor and the handle
	*							  cannot be inherited.
	*   dwStackSize [in] : The initial size of the stack, in bytes.If this parameter is 0 (zero),
	*					   the new thread uses the default size for the executable.
	*	lpStartAddress [in] : A pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread
	*					      and represents the starting address of the thread in the remote process.
	*	lpParameter [in] : A pointer to a variable to be passed to the thread function.
	*	dwCreationFlags [in] : The flags that control the creation of the thread.
	*	lpThreadId [out] :
	*/
	if((hRemoteThread=CreateRemoteThread(hRemoteProcess,NULL,0,pfnStartAddr,pszLibFileRemote,0,NULL))==NULL){

		cout<<"线程注入失败!"<<endl;
		return FALSE;
	}

	//释放句柄
	CloseHandle(hRemoteThread);
	CloseHandle(hRemoteProcess);

	return TRUE;
}

DWORD GetProcessId(){
	DWORD Pid=-1;

	/*
	* Takes a snapshot of the specified processes
	*   dwFlags [in]: TH32CS_SNAPPROCESS-Includes all processes in the system in the snapshot.
	*   th32ProcessID [in]: The process identifier of the process to be included in the snapshot.
	*						This parameter can be zero to indicate the current process. This parameter is used when the TH32CS_SNAPHEAPLIST,
	*						TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, or TH32CS_SNAPALL value is specified.
	*						Otherwise, it is ignored and all processes are included in the snapshot.
	*/
	HANDLE hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

	//创建系统快照
	/*
	* Describes an entry from a list of the processes residing in the system address space when a snapshot was taken.
	*   th32ProcessID: The process identifier.
	*	szExeFile : The name of the executable file for the process.
	*/
	PROCESSENTRY32 lPrs;
	ZeroMemory(&lPrs,sizeof(PROCESSENTRY32));
	lPrs.dwSize=sizeof(lPrs);
	char * TargetFile="QQ.exe";

	/*
	* Retrieves information about the first process encountered in a system snapshot.
	*   hSnapshot [in] : A handle to the snapshot returned from a previous call to the CreateToolhelp32Snapshot function.
	*	lppe [in, out] : A pointer to a PROCESSENTRY32 structure.
	*/
	Process32First(hSnap,&lPrs);
	if(strstr(TargetFile,lPrs.szExeFile)!=NULL){
		Pid=lPrs.th32ProcessID;
		return Pid;
	}
	while(1){
		ZeroMemory(&lPrs,sizeof(PROCESSENTRY32));
		lPrs.dwSize=sizeof(lPrs);

		/*
		* Returns TRUE if the next entry of the process list has been copied to the buffer or FALSE otherwise.
		* The comparison is case-sensitive.
		*/
		if(!Process32Next(hSnap,&lPrs)){

			Pid=-1;
			break;
		}
		/*
		* Returns the address of the first occurrence of the matching substring if successful, or NULL otherwise.
		*/
		if(strstr(TargetFile,lPrs.szExeFile)!=NULL){

			Pid=lPrs.th32ProcessID;
			break;
		}

	}
	return Pid;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char myFile[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,myFile);

	//取得当前路径给myFile
	strcat(myFile,"\\door.dll");
	InjectDLL(myFile,GetProcessId());

	return 0;
}

程序运行的过程:

ALL代码在资源中可以下载

时间: 2024-10-28 18:51:58

VC++进程注入的相关文章

C++进程注入(通过远程线程注入进程)

需要本文代码请直接跳到文章最底部下载 注入进程的方法有很多,本文主要介绍通过远程线程来注入进程的方法: 我们知道,每个进程都有4GB的地址空间,windows可用的大小大概为1.5GB左右,远程线程注入的方法主要是,打开一个线程以后,将要注入的动态库的地址写入这个地址空间,然后调用开启远程线程的函数,来执行LoadLibraryA或者LoadLibraryW(其实不存在LoadLibrary这个函数,他只是一个宏,如果是UNICODE环境的话会调用LoadLibraryW,否则就是LoadLib

“聊天剽窃手”--ptrace进程注入型病毒

近日,百度安全实验室发现了一款"聊天剽窃手"病毒,该病毒能够通过ptrace方式注入恶意代码至QQ.微信程序进程,恶意代码能够实时监控手机QQ.微信的聊天内容及联系人信息.该病毒是目前发现的首款通过ptrace进程注入方式进行恶意窃取私密资料的病毒. 简介 该病毒主要是通过ptrace注入QQ和微信进程进行信息窃取的,主程序调用assets中的inject_appso,libcall.so以及conn.jar联合进行"作案",在conn.jar中获取聊天信息/最近联

Android中通过进程注入技术修改系统返回的Mac地址

致谢 感谢看雪论坛中的这位大神,分享了这个技术:http://bbs.pediy.com/showthread.php?t=186054,从这篇文章中学习到了很多内容,如果没有这篇好文章,我在研究的过程中会遇到很多困难,说不定我就放弃了~~在此感谢他. 前言 之前的几篇文章都是在介绍了OC的相关知识,之前的半个月也都是在搞IOS的相关东西,白天上班做Android工作,晚上回家还有弄IOS,感觉真的很伤了.不过OC的知识也学习了差不多了.不过在这段时间遗留了很多Android方面的问题都没有进行

进程注入的研究与实现

为了对内存中的某个进程进行操作,并且获得该进程地址空间里的数据,或者修改进程的私有数据结构,必须将自己的代码放在目标进程的地址空间里运行,这时就避免不了使用进程注入方法了. 进程注入的方法分类如下: 带DLL的注入 利用注册表注入 利用Windows Hooks注入 利用远程线程注入 利用特洛伊DLL注入 不带DLL的注入 直接将代码写入目标进程,并启动远程线程 1. 利用注册表注入 在Windows NT/2000/XP/2003中,有一个注册表键值HKEY_LOCAL_MACHINE\Sof

向其他进程注入代码的三种方法

http://huaidan.org/archives/838.html 原版地址: http://www.codeproject.com/threads/winspy.asp?df=100&forumid=16291&select=1025152&msg=1025152 pdf格式下载: http://netxfly.blogbus.com/files/1163491746.pdf 作者:Robert Kuster 翻译:袁晓辉([email protected]) 摘要:如何向

Android进程注入

全部代码在这里下载:http://download.csdn.net/detail/a345017062/8133239 里面有两个exe.inj是一个C层进程注入的样例.inj_dalvik是我写的Java注入.C层的进程注入借直接拉的洗大师的开源项目(https://code.google.com/p/libandroidinjector/) Android的基础服务大部分都是使用Java写的,仅仅是C层的注入,想要直接訪问的话还是比較麻烦.所以须要在远端载入一个dex包(代码中写死了载入/

进程注入的学习(中)

3. 利用远程线程注入DLL 1).取得远程进程的进程ID:  2).在远程进程空间中分配一段内存用来存放要注入的DLL完整路径:  3).将要注入的DLL的路径写到刚才分配的远程进程空间:     4 ).从Kernel32.dll中取得LoadLibray的地址:  5).调用CreateRemoteThread函数以从Kernel32.dll中取得的LoadLibrary函数的地址为线程函数的地址,以我们要注入的DLL文件名为参数,创建远程线程: ---------------------

Android中通过进程注入技术修改广播接收器的优先级

前言 这个周末又没有吊事,在家研究了如何通过进程的注入技术修改广播接收器的优先级,关于这个应用场景是很多的,而且也很重要,所以就很急的去fixed了. Android中的四大组件中有一个广播:Broadcast 关于它的相关知识可以转战:http://blog.csdn.net/jiangwei0910410003/article/details/19150705 我们这里就不做太多解释了,现在来看一下问题: 知识前提 这篇文章和我之前介绍一篇文章: Andrdoid中对应用程序的行为拦截实现方

android 进程注入流程

概述 我们平时所说的代码注入,主要静态和动态两种方式: 静态注入,针对是可执行文件,比如修改ELF,DEX文件等,相关的辅助工具也很多,比如IDA.ApkTool等: 动态注入,也可以叫进程注入,针对是进程,比如修改进程的寄存器.内存值等: 动态跟静态最大的区别是,动态不需要改动源文件,但需要高权限(通常是root权限),而且所需的技术含量更高. 基本思路 关键点在于让目标进加载自定义的动态库so,当so被加载后,so就可以加载其他模块.dex文件等, 具体的注入过程大致如下: 1) attac