探索C语言中的Shellcode从提取到执行

发布时间 2023-12-06 19:47:21作者: 微软技术分享

ShellCode是一种独立于应用程序的机器代码,通常用于实现特定任务,如执行远程命令、注入恶意软件或利用系统漏洞。在网络安全领域,研究Shellcode是理解恶意软件和提高系统安全性的关键一环。本文将深入探讨如何在C语言中提取Shellcode,并通过XOR加密技术增加其混淆程度。最后,我们将演示如何将Shellcode写入文件并在内存中执行。

第一步:提取Shellcode

提取ShellCode的主要方法是通过Visual C++编译器的内嵌汇编功能,通过内嵌一条offset特殊的汇编伪指令分别得到内嵌汇编的开始和结尾,然后再利用灵活的内存拷贝命令即可对编译后的汇编指令进行动态的提取工作,当提取后直接将其输出为二进制格式即可,这里提供了两种提取模式,第一种是直接提取二进制机器码此类功能可以直接被运行,第二种则是提取unicode格式,通过向ShellCodeStart-ShellCodeEnd提取代码如下所示。

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

int main(int argc, char* argv[])
{
	DWORD Start, End, Len;
	goto GetShellCode;
	__asm
	{
	ShellCodeStart:
		xor eax, eax
			xor ebx, ebx
			xor ecx, ecx
			xor edx, edx
			int 3
	ShellCodeEnd:
	}

GetShellCode:
	__asm
	{
		mov Start, offset ShellCodeStart
			mov End, offset ShellCodeEnd
	}

	Len = End - Start;
	unsigned char* newBuffer = new unsigned char[Len + 1024];

	memset(newBuffer, 0, Len + 1024);
	memcpy(newBuffer, (unsigned char*)Start, Len);

	// 直接写出二进制
	FILE* fp_bin = fopen("d://shellcode.bin", "wb+");
	fwrite(newBuffer, Len, 1, fp_bin);
	_fcloseall();

	// 写出Unicode格式ShellCode
	FILE *fp_uncode = fopen("c://un_ShellCode.txt", "wb+");
	for (int x = 0; x < Len; x++)
	{
		fprintf(fp_uncode, "%%u%02x%02x", newBuffer[x + 1], newBuffer[x]);
	}
	_fcloseall();
	return 0;
}

第二步:XOR加密Shellcode

为了增加Shellcode的混淆性,我们引入异或(XOR)加密技术。以下是对提取的Shellcode进行异或加密的C代码:

unsigned char ch;
for (int x = 0; x < Len; x++)
{
  ch = ((unsigned char*)newBuffer)[x];
  ch = ch ^ 10;  // 异或加密
  newBuffer[x] = ch;
}

在这里,我们对Shellcode中的每个字节都执行异或运算,以提高其抵抗分析的能力。

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

int main(int argc, char* argv[])
{
  DWORD Start, End, Len;
  goto GetShellCode;
  __asm
  {
  ShellCodeStart:
    xor eax, eax
      xor ebx, ebx
      xor ecx, ecx
      xor edx, edx
      int 3
      ShellCodeEnd :
  }

GetShellCode:
  __asm
  {
    mov Start, offset ShellCodeStart
    mov End, offset ShellCodeEnd
  }

  Len = End - Start;
  unsigned char* newBuffer = new unsigned char[Len + 1024];

  memset(newBuffer, 0, Len + 1024);
  memcpy(newBuffer, (unsigned char*)Start, Len);

  // 使用异或加密ShellCode
  unsigned char ch;
  for (int x = 0; x < Len; x++)
  {
    ch = ((unsigned char*)newBuffer)[x];
    ch = ch ^ 10;
    newBuffer[x] = ch;
  }

  // 将ShellCode写出到文件
  FILE* fp = fopen("d://shellcode.txt", "wb+");
  fwrite("unsigned char Buf[] = {", 23, 1, fp);
  for (int x = 0; x < Len; x++)
  {
    if (x % 16 == 0)
      fwrite("\r\n", 2, 1, fp);
    fprintf(fp, "0x%02x,", newBuffer[x]);
  }
  fwrite("\n};", 3, 1, fp);
  _fcloseall();
  return 0;
}

第三步:执行Shellcode

最后,我们将动态读取Shellcode并在内存中执行它。以下是实现这一步的C代码:

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

int main(int argc, char * argv[])
{
  HANDLE fp;
  unsigned char * fBuffer;
  DWORD fSize, dwSize;

  fp = CreateFile(L"d://shellcode.bin", GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  fSize = GetFileSize(fp, 0);

  fBuffer = (unsigned char *)VirtualAlloc(NULL, fSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  ReadFile(fp, fBuffer, fSize, &dwSize, 0);
  CloseHandle(fp);

  __asm
  {
    mov eax,fBuffer
    push eax
    ret
    int 3
  }
  return 0;
}

此段代码打开文件,将Shellcode读入内存,然后通过汇编代码执行它。这是一个基本的Shellcode执行例子,实际上,执行Shellcode的方式取决于应用场景和操作系统。

总结

通过这个简单的实例,我们深入探讨了从C语言中提取Shellcode的过程,介绍了XOR加密技术以提高Shellcode的混淆性,最后演示了如何在内存中执行Shellcode。理解这些概念对于防范和分析恶意软件至关重要,同时也为安全研究提供了有趣而深刻的领域。

额外考虑因素

在使用Shellcode时,务必考虑到道德和法律问题。合法的安全研究和渗透测试是为了改善系统安全性,而非进行恶意攻击。遵循相关法规和道德准则是安全研究的基本原则。