编译已经编写的C脚本。 Visual Studios 2017

时间:2018-12-10 15:03:45

标签: c

我有一个已经编写好的C脚本,可以与Texmod一起使用。很久以前有关于它的帖子,但是我无法访问。基本上,它使您可以将TexMod与.exe的参数一起使用,例如-log。我已经下载了Visual Studios 2017,并尝试使用开发人员控制台通过cd将其编译到文件夹中,而不是使用cl'script.c'进行编译。它制作了一个.exe和.obj,但除此之外却无能为力,即使我双击.exe也是如此。问题是我知道Java,但从未使用C语言做过任何事情。这是代码:

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

UINT WINAPI EzGetPid(LPCSTR procName, UINT *pid, UINT size);

int main(int argc, char *argv[])
{
    if (argc < 1) {
        puts("You must specifie the arguments");
        return 1;
    }

    UINT pid = 0;
    if (!EzGetPid("Texmod.exe", &pid, 1)) {
        puts("You must open Texmod first.");
        return 1;
    }

    BYTE shellcode_tramp[] = "\x58\x6A\x00\x6A\x00\x68\x00\x00\x00\x00\xFF\xE0";
    UINT size_tramp = 12;

    char arguments[0x500] = {0};
    strcpy(arguments, argv[1]);

    HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
    LPVOID remote_tramp = VirtualAllocEx(proc, NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    LPVOID remote_args = (LPVOID)((DWORD)remote_tramp + size_tramp);

    *(DWORD*)(&shellcode_tramp[6]) = (DWORD)remote_args;
    WriteProcessMemory(proc, remote_tramp, shellcode_tramp, size_tramp, NULL); // Write the trampoline
    WriteProcessMemory(proc, remote_args, arguments, strlen(arguments), NULL); // Write the arguments

    BYTE firstCall[] = "\xE8\x00\x00\x00\x00\x90";
    BYTE secondCall[] = "\xE8\x00\x00\x00\x00\x90";
    *(DWORD*)(&firstCall[1]) = (DWORD)remote_tramp - 0x4012E1 - 5;
    *(DWORD*)(&secondCall[1]) = (DWORD)remote_tramp - 0x40145B - 5;
    WriteProcessMemory(proc, (LPVOID)0x4012E1, firstCall, 6, NULL); // Write first detour call
    WriteProcessMemory(proc, (LPVOID)0x40145B, secondCall, 6, NULL); // Write second detour call

    CloseHandle(proc);
    return 0;
}

UINT WINAPI EzGetPid(LPCSTR procName, UINT *pid, UINT size)
{
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32 buffer = {0};
    buffer.dwSize = sizeof(PROCESSENTRY32);

    UINT count = 0;

    while (Process32Next(hSnap, &buffer) && count < size) {
        if (!strcmp(buffer.szExeFile, procName))
        pid[count++] = buffer.th32ProcessID;
    }

    CloseHandle(hSnap);
    return count;
}

p.s。我添加了最后一个封闭的括号,因为我以为很早以前就将其复制了。

我需要使用Visual Studios界面来执行此操作吗?我想知道知道C的人是否可以查看我尝试编译的代码,并帮助解释我所缺少的任何内容或有关如何运行它的任何特殊说明。 非常感谢您的所有帮助。

1 个答案:

答案 0 :(得分:0)

您的程序需要参数才能正确运行。您编写程序不执行任何操作的原因是,双击该程序时,您没有传递任何参数。在您看到调试消息“您必须指定参数”之前,控制台正在关闭。

通常,使用内部注入的DLL方法(而不是此外部方法)处理蹦床挂钩要容易得多。您可以在挂起状态下启动该过程,然后注入DLL来执行蹦床挂钩。然后,您可以轻松地修改要挂钩的函数的函数参数。

这是我用于x86蹦床挂钩的代码

bool Detour32(BYTE* src, BYTE* dst, const uintptr_t len)
{
    if (len < 5) return false;

    DWORD curProtection;
    VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);

    uintptr_t relativeAddress = dst - src - 5;

    *src = 0xE9;

    *(uintptr_t*)(src + 1) = relativeAddress;

    VirtualProtect(src, len, curProtection, &curProtection);
    return true;
}

BYTE* TrampHook32(BYTE* src, BYTE* dst, const uintptr_t len)
{
    if (len < 5) return 0;

    //Create Gateway
    BYTE* gateway = (BYTE*)VirtualAlloc(0, len, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    //write the stolen bytes to the gateway
    memcpy_s(gateway, len, src, len);

    //Get the gateway to destination address
    uintptr_t gatewayRelativeAddr = src - gateway - 5;

    // add the jmp opcode to the end of the gateway
    *(gateway + len) = 0xE9;

    //Write the address of the gateway to the jmp
    *(uintptr_t*)((uintptr_t)gateway + len + 1) = gatewayRelativeAddr;

    //Perform the detour
    Detour32(src, dst, len);

    return gateway;
}

这里是我如何使用它来执行OpenGL的SwapBuffers函数的示例,在此示例中,我们将hDc参数更改为1337。

typedef BOOL(__stdcall* twglSwapBuffers) (HDC hDc);

twglSwapBuffers owglSwapBuffers;

BOOL __stdcall hkwglSwapBuffers(HDC hDc)
{
    hDc = 1337;
    return owglSwapBuffers(hDc);
}

owglSwapBuffers = (twglSwapBuffers)mem::TrampHook32((BYTE*)owglSwapBuffers, (BYTE*)hkwglSwapBuffers, 5);