找回密码
 立即注册
首页 业界区 业界 用c语言手搓shellcode

用c语言手搓shellcode

利怡悦 7 小时前
手搓shellcode

为什么要用c语言搓个shellcode出来,为什么不用msfvenom?因为这玩意生成的shellcode是基于winsocket的,注进去还要启动个监听,我仅仅想要验证一下可行性而已,不如自己搓个弹出messagebox版本的shellcode
环境

windows 11,amd64, 编译器用的x64 Native Tools Command Prompt for VS 2022(MSVC)
原理

要写一个能在 Windows 上的 Shellcode,最大的挑战在于 PIC(Position Independent Code,位置无关代码)。你不能硬编码 API 的地址(因为重启或不同机器上地址会变),也不能直接引用数据段的字符串和全局变量,全局变量依赖重定位表,Shellcode 没有这东西。所有数据必须在栈(Stack)上。更不能用库函数,因为代码没有导入表(IAT)。
我们以 弹出MessageBox为例,需要解决四个主要问题:

  • 找到 kernel32.dll 的基地址
  • 在 kernel32 中找到 GetProcAddress 和 LoadLibraryA 的地址
  • 使用 LoadLibraryA 加载 user32.dll(MessageBox 在这里面)
  • 解析出 MessageBoxA 的地址并调用
1.找 kernel32.dll 的基地址

我们利用 TEB (Thread Environment Block)PEB (Process Environment Block) 来查找。
_WIN64 和 32 位使用不同的寄存器:64 位:gs:[0x60]32 位:fs:[0x30]
获取 PEB后,找到peb下的Ldr,再获取InMemoryOrderModuleList。链表顺序通常是: .exe -> ntdll.dll -> kernel32.dll. LDR_DATA_TABLE_ENTRY 结构体比较复杂,但在 InMemoryOrderLinks 偏移处    DllBase 通常在 entry 之后特定的偏移位置。  可以利用CONTAINING_RECORD的技巧或者直接偏移计算。
  1. HMODULE GetKernel32() {
  2. #ifdef _WIN64
  3.     PEB *peb = (PEB*)__readgsqword(0x60);
  4. #else
  5.     PEB *peb = (PEB*)__readfsdword(0x30);
  6. #endif
  7.     PEB_LDR_DATA *ldr = peb->Ldr;
  8.     LIST_ENTRY *head = &ldr->InMemoryOrderModuleList;
  9.     LIST_ENTRY *entry = head->Flink;
  10.     entry = entry->Flink;
  11.     entry = entry->Flink;
  12.    
  13.     LDR_DATA_TABLE_ENTRY *ldrEntry = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
  14.     return (HMODULE)ldrEntry->DllBase;
  15. }
复制代码
2.手动解析导出表

获取 DOS Header 和 NT Header。从 OptionalHeader.DataDirectory 获取导出表 RVA。然后获取三个主要数组的地址(addressOfFunctions、addressOfNames、addressOfNameOrdinals),遍历 AddressOfNames 找函数名。使用 ordinals 和 AddressOfFunctions 得到函数实际地址。
其实就是等价于 Windows API 的 GetProcAddress,但是自己实现了,不依赖任何导入表。
  1. FARPROC MyGetProcAddress(HMODULE hModule, const char *lpProcName) {
  2.     PIMAGE_DOS_HEADER dos = (PIMAGE_DOS_HEADER)hModule;
  3.     PIMAGE_NT_HEADERS nt = (PIMAGE_NT_HEADERS)((BYTE*)hModule + dos->e_lfanew);
  4.     DWORD exportDirRVA = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
  5.     if (exportDirRVA == 0) return NULL;
  6.     PIMAGE_EXPORT_DIRECTORY exportDir = (PIMAGE_EXPORT_DIRECTORY)((BYTE*)hModule + exportDirRVA);
  7.     DWORD *names = (DWORD*)((BYTE*)hModule + exportDir->AddressOfNames);
  8.     WORD *ordinals = (WORD*)((BYTE*)hModule + exportDir->AddressOfNameOrdinals);
  9.     DWORD *funcs = (DWORD*)((BYTE*)hModule + exportDir->AddressOfFunctions);
  10.     for (DWORD i = 0; i < exportDir->NumberOfNames; i++) {
  11.         char *name = (char*)((BYTE*)hModule + names[i]);
  12.         if (MyStrCmp(name, lpProcName) == 0) {
  13.             WORD ordinal = ordinals[i];
  14.             return (FARPROC)((BYTE*)hModule + funcs[ordinal]);
  15.         }
  16.     }
  17.     return NULL;
  18. }
复制代码
这里因为不能依赖库函数,所以 MyStrCmp是自己实现的
  1. int MyStrCmp(const char *s1, const char *s2) {
  2.     while (*s1 && (*s1 == *s2)) {
  3.         s1++;
  4.         s2++;
  5.     }
  6.     return *(const unsigned char*)s1 - *(const unsigned char*)s2;
  7. }
复制代码
3.EntryPoint

首先函数名和 DLL 名手动写成 char 数组,避免直接引用字符串。获取 Kernel32 基址,再获取 GetProcAddress 函数指针、获取 LoadLibraryA 和 ExitProcess 函数指针,就可以加载 user32.dll, 获取 MessageBoxA 函数指针了
  1. void EntryPoint() {
  2.     char sLoadLib[] = {'L','o','a','d','L','i','b','r','a','r','y','A',0};
  3.     char sGetProc[] = {'G','e','t','P','r','o','c','A','d','d','r','e','s','s',0};
  4.     char sExit[]    = {'E','x','i','t','P','r','o','c','e','s','s',0};
  5.     char sUser32[]  = {'u','s','e','r','3','2','.','d','l','l',0};
  6.     char sMsgBox[]  = {'M','e','s','s','a','g','e','B','o','x','A',0};
  7.     char sText[]    = {'H','e','l','l','o',' ','F','r','o','m',' ','C',0};
  8.     char sTitle[]   = {'T','e','s','t','.',0};
  9.     HMODULE hKernel32 = GetKernel32();
  10.     P_GetProcAddress pGetProcAddress = (P_GetProcAddress)MyGetProcAddress(hKernel32, sGetProc);
  11.     if (!pGetProcAddress) return;
  12.     P_LoadLibraryA pLoadLibraryA = (P_LoadLibraryA)pGetProcAddress(hKernel32, sLoadLib);
  13.     P_ExitProcess pExitProcess   = (P_ExitProcess)pGetProcAddress(hKernel32, sExit);
  14.     HMODULE hUser32 = pLoadLibraryA(sUser32);
  15.     P_MessageBoxA pMessageBoxA = (P_MessageBoxA)pGetProcAddress(hUser32, sMsgBox);
  16.     if (pMessageBoxA) {
  17.         pMessageBoxA(NULL, sText, sTitle, MB_OK);
  18.     }
  19.     if (pExitProcess) {
  20.         pExitProcess(0);
  21.     }
  22. }
复制代码
编译

编译shellcode,这里建议开启O1优化把函数内联进去
  1. cl.exe /c /nologo /Gy /O1 /GS- /Tc shellcode.c /Fo:shellcode.obj
复制代码
链接shellcode
  1. link.exe /nologo /ENTRY:EntryPoint /SUBSYSTEM:WINDOWS /NODEFAULTLIB /ALIGN:16 /ORDER:@order.txt shellcode.obj /OUT:shellcode.exe
复制代码
注意这里因为shellcode必须要保证入口函数偏移为0,所以要指定函数的顺序(order.txt)
我采用的顺序如下:
  1. EntryPoint
  2. GetKernel32
  3. MyGetProcAddress
  4. MyStrCmp
复制代码
1.png

这里虽然error但是其实在用户态,shellcode.exe就已经可以执行了
然后把shellcode.exe的.text段抠出来,这里我们用python比较方便
  1. import pefile
  2. import os
  3. def extract_shellcode(file_path, out_file="shellcode.bin"):
  4.     try:
  5.         pe = pefile.PE(file_path)
  6.     except FileNotFoundError:
  7.         print(f"Error: File '{file_path}' not found!")
  8.         return
  9.     except pefile.PEFormatError as e:
  10.         print(f"Error: Invalid PE file: {e}")
  11.         return
  12.    
  13.     shellcode = b""
  14.     for section in pe.sections:
  15.         if b".text" in section.Name:
  16.             shellcode = section.get_data()
  17.             break
  18.             
  19.     if not shellcode:
  20.         print("Error: .text section not found!")
  21.         return
  22.    
  23.     print(f"Shellcode Length: {len(shellcode)} bytes\n")
  24.    
  25.     c_array = ''.join(f"\\x{byte:02x}" for byte in shellcode)
  26.     print(f"// C String Format:\n"{c_array}"")
  27.    
  28.     print("\n// Hex Format:")
  29.     print(shellcode.hex())
  30.    
  31.     try:
  32.         with open(out_file, "wb") as f:
  33.             f.write(shellcode)
  34.         print(f"\nShellcode written to '{out_file}'")
  35.     except Exception as e:
  36.         print(f"Error writing shellcode to file: {e}")
  37. if __name__ == "__main__":
  38.     exe_path = "shellcode.exe"
  39.     out_path = "shellcode.bin"  
  40.     extract_shellcode(exe_path, out_path)
复制代码
2.png

就得到了hex串
执行

然后简单写个加载器试试
[code]#include #include #include int main() {    unsigned char shellcode[] =         "";    void* exec_mem = VirtualAlloc(0, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);    if (exec_mem == NULL) {        std::cerr

相关推荐

您需要登录后才可以回帖 登录 | 立即注册