简体   繁体   English

dll注入 - 简单主要

[英]dll injection - simple main

I'm tring to inject a dll to an .exe, i code this simple main but my file isn't created. 我想把一个dll注入一个.exe,我编写这个简单的主要编码,但我的文件没有创建。 I inject it with a cpp code, but i don't think the injector is the problem. 我用cpp代码注入它,但我不认为注入器是问题。

DWORD WINAPI Main_thread( LPVOID lpParam)
{
  std::ofstream myfile;
  myfile.open ("C:\\Users\\root\\Desktop\\example.txt");
  myfile << "success" << std::endl;
  myfile.close();
  return S_OK;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  _reason, LPVOID lpReserved)
{
  if (_reason == DLL_PROCESS_ATTACH)
     CreateThread(0, 0x1000, &Main_thread, 0, 0, NULL);
  return true;
}

the injector code: 注射器代码:

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

using namespace std;
bool InjectDLL(DWORD ProcessID);

char dllPath[250] = "C:\\Users\\root\\Desktop\\testdll\\bin\\Debug\\testdll.dll";
char ProcessName[] = "chrome.exe";
typedef HINSTANCE (*fpLoadLibrary)(char*);

int main()
{
DWORD processId = NULL;
PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)};
HANDLE hProcSnap;
hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(Process32First(hProcSnap, &pe32))
{
    do{
         if(!strcmp(pe32.szExeFile, ProcessName))
        {
            processId = pe32.th32ProcessID;
            break;
        }
    }while(Process32Next(hProcSnap, &pe32));
}
if (!InjectDLL(processId))
        cout << "DLL failed to inject" << endl;
}

bool InjectDLL(DWORD ProcessID)
{
HANDLE hProc;
LPVOID paramAddr;
HINSTANCE hDll = LoadLibrary("KERNEL32");
fpLoadLibrary LoadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA");
hProc = OpenProcess (PROCESS_ALL_ACCESS, false, ProcessID);
paramAddr = VirtualAllocEx(hProc, 0, strlen(dllPath)+1, MEM_COMMIT, PAGE_READWRITE);
bool memoryWritten = WriteProcessMemory(hProc, paramAddr, dllPath, strlen(dllPath)+1, NULL);
CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, paramAddr, 0, 0);
CloseHandle(hProc);
return memoryWritten;
}

the dll don't seem to be injected or he could'nt write, I didn't launch the injector as root dll似乎没有被注入或者他不能写,我没有以root身份启动注入器

You can diagnose your problem from Task Manager, add the PID column. 您可以从任务管理器诊断问题,添加PID列。 Or use SysInternals' Process Explorer. 或者使用SysInternals的Process Explorer。 You'll see that Chrome.exe starts up many instances of itself. 您会看到Chrome.exe启动了许多自身实例。 The primary one just display the UI and is not involved in browsing web pages. 主要的一个只显示UI,不参与浏览网页。 You'll see the other ones, one each for each tab you have opened in the browser. 您将看到其他的,您在浏览器中打开的每个选项卡各一个。

Those other instances are special, they run the add-ons and scripting code in a sandbox . 这些其他实例很特殊,它们在沙箱中运行附加组件和脚本代码。 Designed to make Chrome resilient to web pages or script that can crash or hang the browser. 旨在使Chrome能够恢复可能导致浏览器崩溃或挂起的网页或脚本。 But especially to run code in a runtime environment that removes all privileges so it cannot mess with the user's machine. 但特别是在运行时环境中运行代码,删除所有权限,以便它不会弄乱用户的机器。 Like the kind of code that you are trying to write. 就像你想写的那种代码一样。

So your Process32First/Next() iterator is way too simple, it will pick off whatever instance of Chrome.exe it finds first. 所以你的Process32First / Next()迭代器太简单了,它将挑选它首先找到的Chrome.exe的任何实例。 With a high likelihood that it is a sandboxed one, the kind that won't let you mess with it. 很可能它是一个沙盒,那种不会让你搞砸它的那种。 You could only inject the instance that the user started, the one that only displays the UI. 您只能注入用户启动的实例,即仅显示UI的实例。 Which is usually where the usefulness of this kind of hacking ends, there just isn't anything interesting to mess with in that instance. 通常这种黑客的有用性结束的地方,在那个实例中没有任何有趣的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM