Beispiel #1
0
static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
{
    HANDLE process;
    HMODULE module;
    DWORD required_size;

    process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
    if (!process)
        return FALSE;

    if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
    {
        CloseHandle(process);
        return FALSE;
    }

    if (!GetModuleBaseNameW(process, module, buf, chars))
    {
        CloseHandle(process);
        return FALSE;
    }

    CloseHandle(process);
    return TRUE;
}
Beispiel #2
0
/******************************************************************
 *		EnumerateLoadedModulesW64 (DBGHELP.@)
 *
 */
BOOL  WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
                                       PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
                                       PVOID UserContext)
{
    HMODULE*    hMods;
    WCHAR       baseW[256], modW[256];
    DWORD       i, sz;
    MODULEINFO  mi;

    hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
    if (!hMods) return FALSE;

    if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
    {
        /* hProcess should also be a valid process handle !! */
        FIXME("If this happens, bump the number in mod\n");
        HeapFree(GetProcessHeap(), 0, hMods);
        return FALSE;
    }
    sz /= sizeof(HMODULE);
    for (i = 0; i < sz; i++)
    {
        if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
            !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
            continue;
        module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
        EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
                                  UserContext);
    }
    HeapFree(GetProcessHeap(), 0, hMods);

    return sz != 0 && i == sz;
}
Beispiel #3
0
//wrapper
BOOL GetModuleName(HMODULE hModule, PWCHAR ModuleName, DWORD cbModuleName)
{
	DWORD RetLen;

	RetLen = GetModuleBaseNameW(GetCurrentProcess(), hModule, ModuleName, cbModuleName/sizeof(WCHAR));

	return !!RetLen;
}
Beispiel #4
0
  /// get the name of the process.
  // @param full true if you want the full path; otherwise returns the base name.
  // @function get_process_name
  def get_process_name(Boolean full) {
    HMODULE hMod;
    DWORD cbNeeded;
    wchar_t modname[MAX_PATH];

    if (EnumProcessModules(this->hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
      if (full) {
        GetModuleFileNameExW(this->hProcess, hMod, modname, sizeof(modname));
      } else {
        GetModuleBaseNameW(this->hProcess, hMod, modname, sizeof(modname));
      }
      return push_wstring(L,modname);
    } else {
      return push_error(L);
    }
  }
Beispiel #5
0
//
// Find out where our injected DLL is residing in Lightroom's address space,
// store the value in pRemoteDllBase and return a boolean indicating whether
// we were successful or not.
//
bool CLightroom::getRemoteDllBase()
{
	HMODULE hModule[ 1024 ];
	DWORD cb = 0;
	if( EnumProcessModules( hProcess, hModule, sizeof( hModule ), &cb ) )
	{
		DWORD nModules = cb / sizeof( hModule[ 0 ] );
		for( DWORD i = 0; i < nModules && !pRemoteDllBase; i++ )
		{
			WCHAR wszBaseName[ MAX_PATH ];
			if( GetModuleBaseNameW( hProcess, hModule[ i ], wszBaseName, MAX_PATH ) )
			{
				if( _wcsnicmp( wszBaseName, wszLrMouseWheelDll, wcslen( wszLrMouseWheelDll ) ) == 0 )
					pRemoteDllBase = ( DWORD )hModule[ i ];
			}
		}
	}
	return( pRemoteDllBase != 0 );
}
Beispiel #6
0
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	BOOL b_ret;

	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		{
			gdipp::h_self = hModule;

			wchar_t this_proc_name[MAX_PATH];
			DWORD dw_ret = GetModuleBaseNameW(GetCurrentProcess(), NULL, this_proc_name, MAX_PATH);
			assert(dw_ret != 0);

			if (gdipp::exclude_config::is_process_excluded(gdipp::config_instance, this_proc_name))
				return FALSE;

			OSVERSIONINFO ver_info = {sizeof(OSVERSIONINFO)};
			b_ret = GetVersionEx(&ver_info);
			if (!b_ret)
				return FALSE;
			gdipp::os_support_directwrite = (ver_info.dwMajorVersion >= 6);

			gdipp::scoped_rw_lock::initialize();

			if (!gdipp::init_rpc_client())
				return FALSE;

			gdipp::client_config_instance.parse(gdipp::config_instance);

			if (!gdipp::hook_instance.start())
				return FALSE;

			break;
		}
		case DLL_PROCESS_DETACH:
			gdipp::hook_instance.stop();
			break;
	}

	return TRUE;
}
void MessagerShm::sendMessage(Pid pid, const QString &message)
{
    const HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                        FALSE, pid);
    if (hProcess) {
        wchar_t buffer[1024];
        memset(buffer, 0, sizeof(buffer));
        DWORD sz = GetModuleBaseNameW(hProcess, 0, buffer, 1024u);
        CloseHandle(hProcess);
        QString procName = QString::fromWCharArray(buffer, sz);
        if (procName.length() > 0) {
            QSharedMemory shm(procName);
            if (shm.attach(QSharedMemory::ReadWrite)) {
                shm.lock();
                QByteArray utf8 = message.toUtf8();
                memset(shm.data(), 0, shm.size());
                memcpy(shm.data(), utf8.constData(), utf8.size());
                shm.unlock();
            }
        }
    }
}
Beispiel #8
0
DWORD
WINAPI
GetModuleBaseNameA(
    HANDLE hProcess,
    HMODULE hModule,
    LPSTR lpFilename,
    DWORD nSize
    )
{
    LPWSTR lpwstr;
    DWORD cwch;
    DWORD cch;

    lpwstr = (LPWSTR) LocalAlloc(LMEM_FIXED, nSize * 2);

    if (lpwstr == NULL) {
        return(0);
        }

    cwch = cch = GetModuleBaseNameW(hProcess, hModule, lpwstr, nSize);

    if (cwch < nSize) {
        //
        // Include NULL terminator
        //

        cwch++;
        }

    if (!WideCharToMultiByte(CP_ACP, 0, lpwstr, cwch, lpFilename, nSize, NULL, NULL)) {
        cch = 0;
        }

    LocalFree((HLOCAL) lpwstr);

    return(cch);
}
Beispiel #9
0
bool Platform::GetProcessInfo(ProcessId pid, ProcessInfo &info) {
  HANDLE processHandle;
  BOOL rc;

  info.clear();

  info.pid = pid;

  processHandle =
      OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  if (processHandle == nullptr) {
    goto error;
  }

  // Get process name.
  {
    HMODULE firstModuleHandle;
    DWORD bytesNeeded;
    WCHAR processName[MAX_PATH];

    rc = EnumProcessModules(processHandle, &firstModuleHandle,
                            sizeof(firstModuleHandle), &bytesNeeded);
    if (!rc)
      goto error;

    rc = GetModuleBaseNameW(processHandle, firstModuleHandle, processName,
                            sizeof(processName));
    if (!rc)
      goto error;

    info.name = WideToNarrowString(processName);
  }

  // Get process user ID.
  {
    HANDLE processToken;
    std::vector<char> userInfoBuffer;
    PTOKEN_USER userInfo;
    DWORD bytesNeeded;

    rc = OpenProcessToken(processHandle, TOKEN_QUERY, &processToken);
    if (!rc)
      goto error;

    GetTokenInformation(processToken, TokenUser, userInfoBuffer.data(),
                        userInfoBuffer.size(), &bytesNeeded);

    userInfoBuffer.resize(bytesNeeded);

    rc = GetTokenInformation(processToken, TokenUser, userInfoBuffer.data(),
                             userInfoBuffer.size(), &bytesNeeded);
    if (!rc) {
      CloseHandle(processToken);
      goto error;
    }

    userInfo = reinterpret_cast<PTOKEN_USER>(userInfoBuffer.data());

    DWORD size = GetLengthSid(userInfo->User.Sid);
    info.realUid = malloc(size);
    CopySid(size, info.realUid, userInfo->User.Sid);

    CloseHandle(processToken);
  }

  // TODO(sas): Fetch the process group ID. This looks like it's gonna
  // require some additional work as a process on Windows doesn't have
  // a single group but a list of group tokens instead.

  CloseHandle(processHandle);

  return true;

error:
  if (processHandle != nullptr)
    CloseHandle(processHandle);
  return false;
}
void ExportPatch(const std::wstring & templateContent, DBGPATCHINFO* patchList, size_t numPatches)
{
    printFileName();
    size_t idx_template = templateContent.find(L"$TEMPLATE_PREFIX:");
    size_t idx_module = templateContent.find(L"$MODULE_PREFIX:");
    size_t idx_patch = templateContent.find(L"$PATCH:");
    size_t idx_module_suffix = templateContent.find(L"$MODULE_SUFFIX:");
    size_t idx_template_suffix = templateContent.find(L"$TEMPLATE_SUFFIX:");
    if(idx_template == std::wstring::npos || idx_module == std::wstring::npos || idx_patch == std::wstring::npos || idx_module_suffix == std::wstring::npos || idx_template_suffix == std::wstring::npos)
    {
        MessageBox(hwndDlg, LoadWideString(IDS_INVALID_PATCH).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR);
        return;
    }

    HANDLE hProcess;
    wchar_t ProcessName[1024];
    memset(ProcessName, 0, sizeof(ProcessName));
    hProcess = (HANDLE)DbgValFromString("$hProcess");
    if(GetModuleBaseNameW(hProcess, 0, ProcessName, sizeof(ProcessName) / sizeof(wchar_t)) == 0)
    {
        MessageBox(hwndDlg, LoadWideString(IDS_HPROCESSFAIL).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR);
        return;
    }
    std::wstring text = templateContent.substr(idx_template + int(wcslen(L"$TEMPLATE_PREFIX:")), idx_module - idx_template - int(wcslen(L"$TEMPLATE_PREFIX:")));
    std::wstring modulePrefix = templateContent.substr(idx_module + int(wcslen(L"$MODULE_PREFIX:")), idx_patch - idx_module - int(wcslen(L"$MODULE_PREFIX:")));
    std::wstring patchText = templateContent.substr(idx_patch + int(wcslen(L"$PATCH:")), idx_module_suffix - idx_patch - int(wcslen(L"$PATCH:")));
    std::wstring moduleSuffix = templateContent.substr(idx_module_suffix + int(wcslen(L"$MODULE_SUFFIX:")), idx_template_suffix - idx_module_suffix - int(wcslen(L"$MODULE_SUFFIX:")));
    std::wstring templateSuffix = templateContent.substr(idx_template_suffix + int(wcslen(L"$TEMPLATE_SUFFIX:")));
    std::vector<std::pair<std::wstring, unsigned int>> modules;
    std::string firstModuleUTF8(patchList[0].mod);
    std::wstring firstModuleUTF16;
    unsigned int currentModuleCount = 1;
    utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16));
    modules.push_back(std::make_pair(firstModuleUTF16, 1));
    for(duint i = 1; i < numPatches; i++)
    {
        firstModuleUTF8 = std::string(patchList[i].mod);
        firstModuleUTF16.clear();
        utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16));
        if(firstModuleUTF16.compare(modules.back().first) != 0)
        {
            modules.back().second = currentModuleCount;
            currentModuleCount = 1;
            modules.push_back(std::make_pair(firstModuleUTF16, 1));
        }
        else
            currentModuleCount++;
    }
    modules.back().second = currentModuleCount;
    currentModuleCount = 0;
    duint patches = 0;
    duint modbase;
    unsigned int currentModule = 0;
    std::wstring moduleText;

    for(duint i = 0; i < numPatches; i++)
    {
        if(currentModuleCount == 0)
        {
            moduleText += modulePrefix + L"\r\n";
            modbase = DbgFunctions()->ModBaseFromName(patchList[i].mod);
        }
        std::wstring patchText2(patchText);
        std::wstring newByteText(printByte(patchList[i].newbyte));
        ReplaceWString(patchText2, L"$rva", printHex(patchList[i].addr - modbase));
        ReplaceWString(patchText2, L"$newByte", newByteText);
        ReplaceWString(patchText2, L"$patchIndex", printInto(++patches));
        moduleText += patchText2 + L"\r\n";
        if(currentModuleCount == modules.at(currentModule).second - 1)
        {
            moduleText += moduleSuffix + L"\r\n";
            ReplaceWString(moduleText, L"$moduleName", modules.at(currentModule).first);
            ReplaceWString(moduleText, L"$numPatches", printInto(modules.at(currentModule).second));
            text += moduleText;
            moduleText.clear();
            currentModuleCount = 0;
            patches = 0;
            currentModule++;
        }
        else
            currentModuleCount++;
    }

    text.append(templateSuffix);
    ReplaceWString(text, L"$numPatches", printInto(numPatches));
    ReplaceWString(text, L"$exeName", std::wstring(ProcessName));
    ReplaceWString(text, L"$date", printTime());
    std::wstring compiledate;
    std::string compiledateASCII(__DATE__);
    utf8::utf8to16(compiledateASCII.begin(), compiledateASCII.end(), std::back_inserter(compiledate));
    ReplaceWString(text, L"$compiledate", compiledate);
    
    // save
    if(SaveFile(exportedname, text))
        _plugin_logputs(LoadUTF8String(IDS_SAVESUCCESS).c_str());
    else
        _plugin_logputs(LoadUTF8String(IDS_SAVEFAIL).c_str());
}
Beispiel #11
0
gdipp_setting::gdipp_setting()
	: _xml_doc(NULL)
{
	DWORD dw_ret = GetModuleBaseNameW(GetCurrentProcess(), NULL, _process_name, MAX_PATH);
	assert(dw_ret != 0);
}
DevEnvStatus GetDevEnvStatus(MSIHANDLE hInstall)
{
    DevEnvStatus status = StatusNotRunning;

    const DWORD MaxProcessIds = 8192;
    DWORD* processIds = new DWORD[MaxProcessIds];
    const DWORD cbProcessIdsAllocated = MaxProcessIds * sizeof(*processIds);
    DWORD cbProcessIdsUsed;
    DWORD cProcesses;

    const int cchProcessName = MAX_PATH;
    wchar_t szProcessName[MAX_PATH + 1];

    if (!EnumProcesses(processIds, cbProcessIdsAllocated, &cbProcessIdsUsed))
    {
        status = StatusError;
        Log(LogError, hInstall, L"EnumProcesses failed, GetLastError() = %u.", GetLastError());
        goto Done;
    }

    cProcesses = cbProcessIdsUsed / sizeof(*processIds);

    for (DWORD i = 0; i < cProcesses; i++)
    {
        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processIds[i]);
        if (hProcess)
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
            {
                if (GetModuleBaseNameW(hProcess, hMod, szProcessName, cchProcessName))
                {
                    szProcessName[cchProcessName] = 0;
                    if (0 == _wcsicmp(szProcessName, DevEnvExe))
                    {
                        Log(LogInfo, hInstall, L"DevEnv.exe found (PID %u).", processIds[i]);
                        status = StatusRunning;
                        break;
                    }

                    if (0 == _wcsicmp(szProcessName, DexploreExe))
                    {
                        Log(LogInfo, hInstall, L"dexplore.exe found (PID %u).", processIds[i]);
                        status = StatusRunning;
                        break;
                    }

                    if (0 == _wcsicmp(szProcessName, WDExpressExe))
                    {
                        Log(LogInfo, hInstall, L"WDExpress.exe found (PID %u).", processIds[i]);
                        status = StatusRunning;
                        break;
                    }
                }
                else
                {
                    Log(LogInfo, hInstall, L"GetModuleBaseNameW failed for PID %u, GetLastError() = %u.", processIds[i], GetLastError());
                }
            }
            else
            {
                Log(LogInfo, hInstall, L"EnumProcessModules failed for PID %u, GetLastError() = %u.", processIds[i], GetLastError());
            }
        }
        else
        {
            Log(LogInfo, hInstall, L"OpenProcess failed for PID %u, GetLastError() = %u.", processIds[i], GetLastError());
        }
    }

Done:
    delete[] processIds;
    return status;
}