forked from cube0x8/loadlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci_x64.c
177 lines (150 loc) · 5.47 KB
/
fibonacci_x64.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright (C) 2017 Tavis Ormandy
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <sys/resource.h>
#include <sys/unistd.h>
#ifndef __APPLE__
#include <asm/unistd.h>
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
// #include <mcheck.h>
#include <err.h>
#include "winnt_types.h"
#include "pe_linker.h"
#include "ntoskernel.h"
#include "util.h"
#include "log.h"
#include "hook.h"
// struct pe_image image = {
// .entry = NULL,
// .name = "TestDll_x64.dll",
// };
// export functions
void WINAPI (*fibonacci_init)(const unsigned long long, const unsigned long long);
bool WINAPI (*fibonacci_next)();
unsigned long long WINAPI (*fibonacci_current)();
unsigned WINAPI (*fibonacci_index)();
char* WINAPI (*hello_world)();
// Any usage limits to prevent bugs disrupting system.
const struct rlimit kUsageLimits[] = {
[RLIMIT_FSIZE] = { .rlim_cur = 0x20000000, .rlim_max = 0x20000000 },
[RLIMIT_CPU] = { .rlim_cur = 3600, .rlim_max = RLIM_INFINITY },
[RLIMIT_CORE] = { .rlim_cur = 0, .rlim_max = 0 },
[RLIMIT_NOFILE] = { .rlim_cur = 10240, .rlim_max = 10240 },
};
VOID ResourceExhaustedHandler(int Signal)
{
errx(EXIT_FAILURE, "Resource Limits Exhausted, Signal %d", Signal);
}
int main(int argc, char **argv, char **envp)
{
PIMAGE_DOS_HEADER DosHeader;
PIMAGE_NT_HEADERS PeHeader;
pe_handle handle;
char* filename = "TestDll_x64.dll";
if (argc == 2) {
strncpy(filename, argv[1], 128);
}
// Load the mpengine module.
if (!pe_load_library(filename, &handle)) {
LogMessage("You must add the dll and vdm files to the engine directory");
return 1;
}
// Lookup export functions...
if (get_export(&handle, "fibonacci_init", &fibonacci_init) == -1) {
errx(EXIT_FAILURE, "failed to resolve function fibonacci_init");
}
if (get_export(&handle, "fibonacci_next", &fibonacci_next) == -1) {
errx(EXIT_FAILURE, "failed to resolve function fibonacci_next");
}
if (get_export(&handle, "fibonacci_current", &fibonacci_current) == -1) {
errx(EXIT_FAILURE, "failed to resolve function fibonacci_current");
}
if (get_export(&handle, "fibonacci_index", &fibonacci_index) == -1) {
errx(EXIT_FAILURE, "failed to resolve function fibonacci_index");
}
// Fetch the headers to get base offsets.
DosHeader = (PIMAGE_DOS_HEADER) handle.image->image;
PeHeader = (PIMAGE_NT_HEADERS)(handle.image->image + DosHeader->e_lfanew);
// Load any additional exports.
if (!process_extra_exports(&handle, PeHeader->OptionalHeader.BaseOfCode, "TestDll.map")) {
#ifndef NDEBUG
LogMessage("The map file wasn't found, symbols wont be available");
#endif
} else {
// Calculate the commands needed to get export and map symbols visible in gdb.
if (IsGdbPresent()) {
LogMessage("GDB: add-symbol-file %s %#x+%#x",
handle.image->name,
handle.image->image,
PeHeader->OptionalHeader.BaseOfCode);
LogMessage("GDB: shell bash genmapsym.sh %#x+%#x symbols_%d.o < %s",
handle.image->image,
PeHeader->OptionalHeader.BaseOfCode,
getpid(),
"TestDll.map");
LogMessage("GDB: add-symbol-file symbols_%d.o 0", getpid());
__debugbreak();
}
}
// Call DllMain()
// image.entry((HANDLE) 'FIBONACCI', DLL_PROCESS_ATTACH, NULL);
// Install usage limits to prevent system crash.
setrlimit(RLIMIT_CORE, &kUsageLimits[RLIMIT_CORE]);
setrlimit(RLIMIT_CPU, &kUsageLimits[RLIMIT_CPU]);
setrlimit(RLIMIT_FSIZE, &kUsageLimits[RLIMIT_FSIZE]);
setrlimit(RLIMIT_NOFILE, &kUsageLimits[RLIMIT_NOFILE]);
signal(SIGXCPU, ResourceExhaustedHandler);
signal(SIGXFSZ, ResourceExhaustedHandler);
// # ifndef NDEBUG
// // Enable Maximum heap checking.
// mcheck_pedantic(NULL);
// # endif
pe_handle handle2;
// Load the another module.
if (!pe_load_library("HelloWorldDll.dll", &handle2)) {
LogMessage("Load hello world dll failed");
return 1;
}
if (get_export(&handle2, "HelloWorld", &hello_world) == -1) {
errx(EXIT_FAILURE, "failed to resolve function hello_world");
}
fibonacci_init(1, 1);
do {
fprintf(stdout, "%d: %llu\n", fibonacci_index(), fibonacci_current());
} while (fibonacci_next());
// Report count of values written before overflow.
fprintf(stdout, "%d Fibonacci sequence values fit in an unsigned 64-bit integer.\n", fibonacci_index() + 1);
fprintf(stdout, "%s\n", hello_world());
pe_unload_library(&handle);
return 0;
}