首页
社区
课程
招聘
[旧帖] [求助]关于远程注入学习的一点问题 0.00雪花
发表于: 2009-7-2 13:45 3332

[旧帖] [求助]关于远程注入学习的一点问题 0.00雪花

2009-7-2 13:45
3332
帖代码:

//根据进程名称得到进程ID,如果有多个运行实例的话,返回第一个枚举到的进程的ID
DWORD processNameToId(LPCTSTR lpszProcessName)
{
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        PROCESSENTRY32 pe;
        pe.dwSize = sizeof(PROCESSENTRY32);
       
        if (!Process32First(hSnapshot, &pe)) {
                MessageBox(NULL,
                        "The frist entry of the process list has not been copyied to the buffer",
            "Notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
       
        while (Process32Next(hSnapshot, &pe)) {
                if (!strcmp(lpszProcessName, pe.szExeFile)) {
                        return pe.th32ProcessID;
                }
        }
       
        return 0;
}

int DoProcss()
{
                const DWORD dwThreadSize = 4096;
        DWORD dwWriteBytes;
        //提升进程访问权限
        //enableDebugPriv();
       
        //等待输入进程名称,注意大小写匹配
       
        char szExeName[MAX_PATH] = { 0 };
        strcpy(szExeName,"testme.exe");
       
        DWORD dwProcessId = processNameToId(szExeName);
       
        if (dwProcessId == 0) {
                ::MessageBox(NULL, "The target process have not been found !",
                        "Notice", MB_ICONINFORMATION | MB_OK);
                return -1;
        }
       
        //根据进程ID得到进程句柄
        HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
       
        if (!hTargetProcess) {
                ::MessageBox(NULL, "Open target process failed !",
                        "Notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
       
        //在宿主进程中为线程体开辟一块存储区域
        //在这里需要注意MEM_COMMIT | MEM_RESERVE内存非配类型以及PAGE_EXECUTE_READWRITE内存保护类型
        //其具体含义请参考MSDN中关于VirtualAllocEx函数的说明。
        void* pRemoteThread = VirtualAllocEx(hTargetProcess, 0,
                dwThreadSize, MEM_COMMIT , PAGE_EXECUTE_READWRITE);
       
        if (!pRemoteThread) {
                ::MessageBox(NULL, "Alloc memory in target process failed !",
                        "notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
       
        //将线程体拷贝到宿主进程中
        DWORD lpNumberOfBytes;
        if (!WriteProcessMemory(hTargetProcess,
                pRemoteThread, threadProc, dwThreadSize, &lpNumberOfBytes)) {
                ::MessageBox(NULL, "Write data to target process failed !",
                        "Notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
        //定义线程参数结构体变量
        RemoteParam remoteData;
        ZeroMemory(&remoteData, sizeof(RemoteParam));
       
        //填充结构体变量中的成员
        HINSTANCE hUser32 = LoadLibrary("User32.dll");
        remoteData.dwMessageBox = (DWORD)GetProcAddress(hUser32, "MessageBoxA");
        strcat(remoteData.szMsg, "Hello\0");
       
        //为线程参数在宿主进程中开辟存储区域
        RemoteParam* pRemoteParam = (RemoteParam*)VirtualAllocEx(
                hTargetProcess , 0, sizeof(RemoteParam), MEM_COMMIT, PAGE_READWRITE);
       
        if (!pRemoteParam) {
                ::MessageBox(NULL, "Alloc memory failed !",
                        "Notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
       
        //将线程参数拷贝到宿主进程地址空间中
        if (!WriteProcessMemory(hTargetProcess ,
                pRemoteParam, &remoteData, sizeof(remoteData), 0)) {
                ::MessageBox(NULL, "Write data to target process failed !",
                        "Notice", MB_ICONINFORMATION | MB_OK);
                return 0;
        }
       
        //在宿主进程中创建线程
        HANDLE hRemoteThread = CreateRemoteThread(
                hTargetProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pRemoteThread,
                (LPVOID)pRemoteParam, 0, &dwWriteBytes);
       
        if (!hRemoteThread) {
                ::MessageBox(NULL, "Create remote thread failed !", "Notice",   MB_ICONINFORMATION | MB_OK);
                return 0;
        }

        WaitForSingleObject(hRemoteThread, INFINITE);//等待線程結束

        VirtualFreeEx(hTargetProcess, pRemoteThread, 4096, MEM_RELEASE);
        CloseHandle(hRemoteThread);
        CloseHandle(hTargetProcess);
        return 0;
}

//线程函数定义
DWORD __stdcall threadProc(LPVOID lParam)
{
       

        RemoteParam* pRP = (RemoteParam*)lParam;
       
        PFN_MESSAGEBOX pfnMessageBox;
        pfnMessageBox = (PFN_MESSAGEBOX)pRP->dwMessageBox;
        pfnMessageBox(NULL, pRP->szMsg, pRP->szMsg, 0);
       
        return 0;
}


1.我用以下代码,一注入testme.exe(我自己写的一个测试程序),该进程马上出异常关闭,请问下有什么问题,实在是想不通了

2.为什么在线程代码不直接调用Messagebox这个API,而非得在注入程序中得到messagebox中的地址传入调用?据我所知,同一个DLL映射到不同程序 地址不一定相同吧?

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 264
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
问题一:
你注释掉了 //enableDebugPriv();
问题二:
需要动态的用loadlibrary和getprocaddress获得所要调用的函数地址。否则在被注入的进程中该符号是没有办法识别的。
2009-7-2 17:15
0
雪    币: 225
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
第一个问题,我的进程非系统的进程,所以注释掉了,就是加上目标进程也会异常 关闭,我在网上找了一天代码,一个一个的试,没有一个是成功的,全部都异常关闭掉了,补充说明一点,我用的测试程序分别试了WIN32窗口程序,MFC程序,控制台程序

第二个问题,已经解决
2009-7-3 08:58
0
游客
登录 | 注册 方可回帖
返回
//