summaryrefslogtreecommitdiff
path: root/src/tools/InjectResource/InjectResource.cpp
blob: b7a1aaea80303d834479d0422dc446e0a265aac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include <daccess.h>

char* g_appName;

#define MAX(x,y) ((x) > (y) ? (x) : (y))

void
AddBinaryResourceToDll(__in_z char* dllName,
                       __in_z char* resName,
                       PVOID resData,
                       ULONG resDataSize)
{
    // a retry loop in case we get transient file access issues
    // does exponential backoff with retries after 1,2,4,8,16, and 32 seconds
    for(int seconds = 0; seconds < 60; seconds = MAX(seconds*2,1))
    {
        if(seconds != 0)
            Sleep(seconds * 1000);

        HANDLE dllUpdate = BeginUpdateResourceA(dllName, FALSE);
        if (!dllUpdate)
        {
            printf("Unable to open '%s' for update, error=%d\n",
                dllName, GetLastError());
            continue;
        }

        if (!UpdateResourceA(dllUpdate,
            RT_RCDATA,
            resName,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
            resData,
            resDataSize))
        {
            printf("Unable to update '%s', error=%d\n",
                dllName, GetLastError());
            continue;
        }

        if(!EndUpdateResource(dllUpdate, FALSE))
        {
            printf("Unable to write updates to '%s', error=%d\n",
                dllName, GetLastError());
            continue;
        }

        return;
    }

    printf("Stopping after excessive failures\n");
    exit(1);
}

void
GetBinFileData(__in_z char* binFileName, PVOID* binData, PULONG binDataSize)
{
    HANDLE binFileHandle;
    PVOID data;
    ULONG size, done;

    binFileHandle = CreateFileA(binFileName, GENERIC_READ, FILE_SHARE_READ,
                                NULL, OPEN_EXISTING, 0, NULL);
    if (!binFileHandle || binFileHandle == INVALID_HANDLE_VALUE)
    {
        printf("Unable to open '%s', %d\n",
               binFileName, GetLastError());
        exit(1);
    }

    size = GetFileSize(binFileHandle, NULL);
    data = malloc(size);
    if (!data)
    {
        printf("Out of memory\n");
        exit(1);
    }

    if (!ReadFile(binFileHandle, data, size, &done, NULL) ||
        done != size)
    {
        printf("Unable to read '%s', %d\n",
               binFileName, GetLastError());
        exit(1);
    }

    CloseHandle(binFileHandle);

    *binData = data;
    *binDataSize = size;
}

void
Usage(void)
{
    printf("Usage: %s [options]\n", g_appName);
    printf("Options are:\n");
    printf("  /bin:<file>   - Binary data to attach to DLL\n");
    printf("  /dll:<file>   - DLL to modify\n");
    printf("  /name:<name>  - resource name [Optional]\n");
    exit(1);
}

void __cdecl
main(int argc, __in_z char** argv)
{
    char* binFile = NULL;
    char* dllFile = NULL;
    char* resName = NULL;

    g_appName = argv[0];

    while (--argc)
    {
        argv++;

        if (!strncmp(*argv, "/bin:", 5))
        {
            binFile = *argv + 5;
        }
        else if (!strncmp(*argv, "/dll:", 5))
        {
            dllFile = *argv + 5;
        }
        else if (!strncmp(*argv, "/name:", 6))
        {
            resName = *argv + 6;
        }
        else
        {
            Usage();
        }
    }

    if (!binFile || !dllFile)
    {
        Usage();
    }

    PVOID resData;
    ULONG resDataSize;

    GetBinFileData(binFile, &resData, &resDataSize);

    AddBinaryResourceToDll(dllFile, resName?resName:DACCESS_TABLE_RESOURCE,
                           resData, resDataSize);

    free(resData);

    printf("Updated %s\n", dllFile);
}