|
发表于 2020-6-15 20:57:31
|
查看: 4424 |
回复: 0
枚举注册表启动项: 通过添加注册表启动项,可以很方便地完成自启动,常用的启动位置有CurrentVersion,BootExecute,Active Setup.
#include <stdio.h>
#include <Windows.h>
void Enum_Regedit(HKEY Reg_Root,const char *Reg_Path)
{
HKEY hKey = NULL;
DWORD dwType = 0;
char szValueName[MAXBYTE], szValueKey[MAXBYTE] = { 0 };
DWORD dwBufferSize = MAXBYTE, dwKeySize = MAXBYTE;
// 打开注册表项
LONG lRet = RegOpenKeyEx(Reg_Root, Reg_Path, 0, KEY_ALL_ACCESS, &hKey);
int index = 0;
while (1)
{
// 枚举注册表键项
lRet = RegEnumValue(hKey, index, szValueName, &dwBufferSize, NULL,
&dwType, (unsigned char *)szValueKey, &dwKeySize);
if (lRet == ERROR_NO_MORE_ITEMS)
break;
printf("序号: %3d 名称: %-25s 值: %-50s 类型: ", index, szValueName, szValueKey);
switch (dwType)
{
case 1: printf("REG_SZ \n"); break;
case 2: printf("REG_EXPAND_SZ \n"); break;
case 4: printf("REG_DWORD \n"); break;
case 7: printf("REG_MULTI_SZ \n"); break;
default: printf("None \n"); break;
}
dwBufferSize = MAXBYTE;
dwKeySize = MAXBYTE;
index++;
}
RegCloseKey(hKey);
}
int main(int argc, char *argv[])
{
// 枚举普通启动项
Enum_Regedit(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run");
Enum_Regedit(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run");
// 枚举Boot启动项
// HKLM\System\CurrentControlSet\Control\Session Manager\BootExecute
Enum_Regedit(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Session Manager\\");
// 枚举ActiveX启动项,在子键中添加SubPath即可完成开机自启动.
Enum_Regedit(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\");
system("pause");
return 0;
}
添加注册表启动项:
#include <stdio.h>
#include <Windows.h>
#define Reg_Path "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
BOOL Reg_Regedit(HKEY Reg_Root,char *lpszFileName, char *lpszValueName)
{
HKEY hKey;
if (ERROR_SUCCESS != RegOpenKeyEx(Reg_Root, Reg_Path, 0, KEY_WRITE, &hKey))
{
return FALSE;
}
if (ERROR_SUCCESS != RegSetValueEx(hKey, lpszValueName, 0, REG_SZ, (BYTE *)lpszFileName, (1 + ::lstrlen(lpszFileName))))
{
RegCloseKey(hKey);
return FALSE;
}
RegCloseKey(hKey);
return TRUE;
}
int main(int argc, char *argv[])
{
TCHAR szPath[MAX_PATH] = { 0 };
if (GetModuleFileName(NULL, szPath, MAX_PATH))
{
int ret = Reg_Regedit(HKEY_CURRENT_USER, szPath, "main1");
if (ret == 1)
printf("添加自身启动项成功 \n");
}
system("pause");
return 0;
}
判断启动项是否存在:
#include <stdio.h>
#include <Windows.h>
#define Reg_Path "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
BOOL Check_Regedit(HKEY Reg_Root,char * name)
{
HKEY hKey;
if (ERROR_SUCCESS == RegOpenKeyEx(Reg_Root, Reg_Path, NULL, KEY_ALL_ACCESS, &hKey))
{
DWORD dwSize = 255, dwType = REG_SZ;
char String[256];
if (ERROR_SUCCESS == RegQueryValueEx(hKey, name, 0, &dwType, (BYTE *)String, &dwSize))
{
return true;
}
}
RegCloseKey(hKey);
return false;
}
int main(int argc, char *argv[])
{
int ret = Check_Regedit(HKEY_CURRENT_USER, "ctfmon");
printf("启动项存在: %d \n", ret);
system("pause");
return 0;
}
删除注册表启动项:
#include <stdio.h>
#include <Windows.h>
void Delete_Regedit(const char *Reg_Path,const char *Key_Name)
{
char szKeyName[MAXBYTE] = { 0 };
HKEY hKey = NULL;
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Reg_Path, 0, KEY_ALL_ACCESS, &hKey);
RegDeleteValue(hKey, Key_Name);
RegCloseKey(hKey);
}
int main(int argc, char *argv[])
{
Delete_Regedit("Software\\Microsoft\\Windows\\CurrentVersion\\Run", "main1");
Delete_Regedit("Software\\Microsoft\\Windows\\CurrentVersion\\Run", "main2");
system("pause");
return 0;
}
常用注册表操作:
#include <stdio.h>
#include <Windows.h>
// 禁用系统任务管理器
void RegTaskmanagerForbidden()
{
HKEY hkey;
DWORD value = 1;
RegCreateKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", &hkey);
RegSetValueEx(hkey, "DisableTaskMgr", NULL, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
RegCloseKey(hkey);
}
// 禁用注册表编辑器
void RegEditForbidden()
{
HKEY hkey;
DWORD value = 1;
RegCreateKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", &hkey);
RegSetValueEx(hkey, "DisableRegistryTools", NULL, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
RegCloseKey(hkey);
}
// 干掉桌面壁纸
void RegModifyBackroud()
{
DWORD value = 1;
HKEY hkey;
RegCreateKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", &hkey);
RegSetValueEx(hkey, "Wallpaper", NULL, REG_SZ, (unsigned char *)"c://", 3);
RegSetValueEx(hkey, "WallpaperStyle", NULL, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
}
int main(int argc, char* argv[])
{
RegModifyBackroud();
system("pause");
return 0;
}
枚举系统服务操作:
#include <stdio.h>
#include <Windows.h>
void Enum_Services(DWORD dwServiceType)
{
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == hSCM)
return;
DWORD ServiceCount = 0, dwSize = 0;
LPENUM_SERVICE_STATUS lpInfo;
BOOL bRet = EnumServicesStatus(hSCM, dwServiceType, SERVICE_STATE_ALL, NULL, 0, &dwSize, &ServiceCount, NULL);
if (!bRet && GetLastError() == ERROR_MORE_DATA)
{
// 分配缓冲区,保存服务列表
lpInfo = (LPENUM_SERVICE_STATUS)(new BYTE[dwSize]);
bRet = EnumServicesStatus(hSCM, dwServiceType, SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)lpInfo,
dwSize, &dwSize, &ServiceCount, NULL);
if (NULL == hSCM)
return;
// 逐个遍历获取服务信息
for (int x = 0; x < ServiceCount; x++)
{
printf("服务名称: %-30s --> 显示名称: %50s --> 状态: ", lpInfo[x].lpServiceName,lpInfo[x].lpDisplayName);
switch (lpInfo[x].ServiceStatus.dwCurrentState)
{
case SERVICE_PAUSED: printf("暂停 \n"); break;
case SERVICE_STOPPED: printf("停止 \n"); break;
case SERVICE_RUNNING: printf("运行 \n"); break;
default: printf("其他 \n");
}
}
delete lpInfo;
}
CloseServiceHandle(hSCM);
}
int main(int argc, char *argv[])
{
// 0x0 => 设备驱动程序 0x2=>内核模式文件系统驱动程序
// 0x8 => 文件系统识别器驱动程序 0x10 => 独占一个进程的服务
// 0x20 => 与其他服务共享一个进程的服务
Enum_Services(0x10);
system("pause");
return 0;
}
设置服务自启动: 我们需要将服务放到C://windows/system32/目录下,然后注册服务才能运行.
#include <Windows.h>
void AutoRunService()
{
char szFileName[MAX_PATH] = { 0 };
GetModuleFileName(NULL, szFileName, MAX_PATH);
SC_HANDLE scHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE scHandleOpen = OpenService(scHandle, "lyshark", SERVICE_ALL_ACCESS);
if (scHandleOpen == NULL)
{
char szSelfFile[MAX_PATH] = { 0 };
char szSystemPath[MAX_PATH] = { 0 };
GetSystemDirectory(szSystemPath, MAX_PATH);
strcat(szSystemPath, "\\main.exe");
GetModuleFileName(NULL, szSelfFile, MAX_PATH);
CopyFile(szSelfFile, szSystemPath, FALSE);
SC_HANDLE scNewHandle = CreateService(scHandle,"lyshark","lyshark",
SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,szSystemPath,NULL,NULL,NULL,NULL,NULL);
StartService(scNewHandle, 0, NULL);
CloseServiceHandle(scNewHandle);
MessageBox(NULL, "服务已注册", "lyshark", MB_OK);
}
CloseServiceHandle(scHandleOpen);
CloseServiceHandle(scHandle);
}
int main(int argc,char * argv[])
{
// 注册为自启动
AutoRunService();
return 0;
}
创建标准服务程序:
#include <Windows.h>
// 服务入口函数以及处理回调函数
void __stdcall ServiceMain(DWORD dwArgc, char *lpszArgv);
void __stdcall ServiceCtrlHandle(DWORD dwOperateCode);
BOOL TellSCM(DWORD dwState, DWORD dwExitCode, DWORD dwProgress);
void DoTask();
// 全局变量
char g_szServiceName[MAX_PATH] = "ServiceTest.exe"; // 服务名称
SERVICE_STATUS_HANDLE g_ServiceStatusHandle = { 0 };
int main(int argc, char * argv[])
{
// 注册服务入口函数
SERVICE_TABLE_ENTRY stDispatchTable[] = { { g_szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, { NULL, NULL } };
::StartServiceCtrlDispatcher(stDispatchTable);
return 0;
}
void __stdcall ServiceMain(DWORD dwArgc, char *lpszArgv)
{
g_ServiceStatusHandle = ::RegisterServiceCtrlHandler(g_szServiceName, ServiceCtrlHandle);
TellSCM(SERVICE_START_PENDING, 0, 1);
TellSCM(SERVICE_RUNNING, 0, 0);
// 自己程序实现部分代码放在这里
// !!注意!! 此处一定要为死循环, 否则在关机再开机的情况(不是点击重启), 不能创建用户进程
while (TRUE)
{
Sleep(5000);
DoTask();
}
}
void __stdcall ServiceCtrlHandle(DWORD dwOperateCode)
{
switch (dwOperateCode)
{
case SERVICE_CONTROL_PAUSE:
{
// 暂停
TellSCM(SERVICE_PAUSE_PENDING, 0, 1);
TellSCM(SERVICE_PAUSED, 0, 0);
break;
}
case SERVICE_CONTROL_CONTINUE:
{
// 继续
TellSCM(SERVICE_CONTINUE_PENDING, 0, 1);
TellSCM(SERVICE_RUNNING, 0, 0);
break;
}
case SERVICE_CONTROL_STOP:
{
// 停止
TellSCM(SERVICE_STOP_PENDING, 0, 1);
TellSCM(SERVICE_STOPPED, 0, 0);
break;
}
case SERVICE_CONTROL_INTERROGATE:
{
// 询问
break;
}
default:
break;
}
}
BOOL TellSCM(DWORD dwState, DWORD dwExitCode, DWORD dwProgress)
{
SERVICE_STATUS serviceStatus = { 0 };
BOOL bRet = FALSE;
::RtlZeroMemory(&serviceStatus, sizeof(serviceStatus));
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = dwState;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
serviceStatus.dwWin32ExitCode = dwExitCode;
serviceStatus.dwWaitHint = 3000;
bRet = ::SetServiceStatus(g_ServiceStatusHandle, &serviceStatus);
return bRet;
}
void DoTask()
{
// 自己程序实现部分代码放在这里
}
实现服务管理器:
#include <stdio.h>
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
BOOL SystemServiceOperate(char *lpszDriverPath, int iOperateType)
{
BOOL bRet = TRUE;
char szName[MAX_PATH] = { 0 };
lstrcpy(szName, lpszDriverPath);
PathStripPath(szName); // 只保留文件名称,去掉文件路径
SC_HANDLE shOSCM = NULL, shCS = NULL;
SERVICE_STATUS ss;
DWORD dwErrorCode = 0;
BOOL bSuccess = FALSE;
// 打开服务控制管理器数据库
shOSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (iOperateType != 0)
{
// 打开一个已经存在的服务
shCS = OpenService(shOSCM, szName, SERVICE_ALL_ACCESS);
if (!shCS)
{
CloseServiceHandle(shOSCM);
shOSCM = NULL;
return FALSE;
}
}
switch (iOperateType)
{
case 0:
{
// 创建服务
// SERVICE_AUTO_START 随系统自动启动
// SERVICE_DEMAND_START 手动启动
shCS = ::CreateService(shOSCM, szName, szName, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, lpszDriverPath, NULL, NULL, NULL, NULL, NULL);
break;
}
case 1:
{
// 启动服务
if (!::StartService(shCS, 0, NULL)) { bRet = FALSE; }
break;
}
case 2:
{
// 停止服务
if (!::ControlService(shCS, SERVICE_CONTROL_STOP, &ss)) { bRet = FALSE; }
break;
}
case 3:
{
// 删除服务
if (!::DeleteService(shCS)) { bRet = FALSE; }
break;
}
default: break;
}
return bRet;
}
int main(int argc, char *argv[])
{
char *path = "c://1.exe";
// 0 加载服务 1 启动服务 2 停止服务 3 删除服务
SystemServiceOperate(path, 3);
system("pause");
return 0;
}
|
温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【 投诉建议】板块发帖举报。
|