love潇洒小峰 2015-10-29 08:08 采纳率: 75%
浏览 1899

关于Win32核心编程中DLL注入后无反应的解决办法

#include
#include
HINSTANCE hProcess;
PWSTR pszLibFileRemote;
HINSTANCE hThread;
void StartInject(int ProcessID, char * DllName);
void OverInject(char * DllName);
void EnableDebugPrivilege(HANDLE processHandle);
int main(int argc, char* argv[])
{
int processid = 0;
char DllName[50] = "Win32DLL.dll";
while (1)
{
//ZeroMemory(DllName, sizeof(DllName));
printf("please input the process id:\n");
scanf("%d", &processid);

    EnableDebugPrivilege(GetCurrentThread());
    StartInject(processid, DllName);
    OverInject(DllName);
}
return 0;

}
void StartInject(int ProcessID, char * DllName)
{

//打开进程,申请访问;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessID);

if (hProcess == NULL)
{
    printf("进程不允许访问!");
    getchar();
    exit(0);
}
int cch = 1 + lstrlenW(DllName);
int cb = cch * sizeof(WCHAR);

pszLibFileRemote = (PWSTR)VirtualAllocEx(hProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);//为DllName在远程线程空间分配内存;
if (pszLibFileRemote == NULL)
{
    printf("在远程线程空间为DllName分配内存失败!");
    getchar();
    exit(0);
}
if (!WriteProcessMemory(hProcess, pszLibFileRemote, (PVOID)DllName, cb, NULL))
{
    printf("在拷贝DllName到远程线程内存地址空间时失败!");
    getchar();
    exit(0);
}
/*得到LoadLibraryW在内核(kernel32.dll中的地址)*/
PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)
    GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
if (pfnThreadRtn == NULL)
{
    printf("在得到函数地址时出现错误:\n函数名:LoadLibraryW(加载动态链接库)");
    getchar();
    exit(0);
}
hThread = CreateRemoteThread(hProcess, NULL, 0,pfnThreadRtn, pszLibFileRemote, 0, NULL);
if (hThread == NULL)
{
    printf("创建远程线程失败,错误代码:%d",GetLastError());        
    getchar();
    exit(0);
}

}
void OverInject(char * DllName)
{
if (DllName != NULL)
VirtualFreeEx(hProcess, DllName, 0, MEM_RELEASE);

if (hThread != NULL)
    CloseHandle(hThread);

if (hProcess != NULL)
    CloseHandle(hProcess);

}

void EnableDebugPrivilege(HANDLE processHandle)
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;

if (!OpenProcessToken(processHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
    printf("OpenProcessToken");
    return;
}
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
{
    printf("LookupPrivilegeValue");
    CloseHandle(hToken);
    return;
}

tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
{
    printf("AdjustTokenPrivileges");
    CloseHandle(hToken);
}

}

然后DLL中是这样的:
#include
#include

int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{

switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
//The DLL is being mapped into the process's address space.
MessageBoxW(NULL, "这是一个确定 取消的消息框!", "标题", MB_OKCANCEL);
break;

case DLL_THREAD_ATTACH:
//A thread is being created.
break;

case DLL_THREAD_DETACH:
//A thread is exiting cleanly.
break;

case DLL_PROCESS_DETACH:
//The DLL is being unmapped from the process's address space.
break;
}
/*MessageBoxW(NULL, "这是一个确定 取消的消息框!", "标题", MB_OKCANCEL);*/
return(TRUE);  // Used only for DLL_PROCESS_ATTACH

}

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-10-29 08:28
    关注

    用processexplore等工具查看一下目标进程,dll是否加载进去。是不是有杀毒软件拦截注入

    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含