-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmain.c
More file actions
71 lines (56 loc) · 1.83 KB
/
libmain.c
File metadata and controls
71 lines (56 loc) · 1.83 KB
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
// loader code is taken from https://gist.github.com/apsun/1e144bf7639b22ff0097171fa0f8c6b1
// this is just for testing with lighthouse_console. For the driver, a shim library is a much better solution
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include "redirect.h"
#include "common.h"
#include <hidapi.h>
#include "lh_hidapi.h"
/* Trampoline for the real main() */
static int (*main_orig)(int, char **, char **);
void* get_text_addr(){
FILE* maps = fopen("/proc/self/maps", "r");
if(!maps){
printf("failed to open /proc/self/maps, can't get .text base address! exiting\n");
exit(1);
}
char buf[256] = {0};
fread(buf, sizeof(buf)-1, 1, maps);
long text_base = strtol(buf, NULL, 16);
//printf(".text base: 0x%lx\n", text_base);
return (void*)text_base;
}
int main_hook(int argc, char** argv, char** c){
printf("fake main called instead!\n");
void* text_base = get_text_addr();
lib_context* ctx = open_library("/proc/self/exe");
set_text_base(ctx, text_base);
setup_hooks(ctx);
//redirect_function(orig_hid_init, lh_hid_init);
main_orig(argc, argv, c);
return 0;
}
/*
* Wrapper for __libc_start_main() that replaces the real main
* function with our hooked version.
*/
int __libc_start_main(
int (*main)(int, char **, char **),
int argc,
char **argv,
int (*init)(int, char **, char **),
void (*fini)(void),
void (*rtld_fini)(void),
void *stack_end)
{
/* Save the real main function address */
main_orig = main;
/* Find the real __libc_start_main()... */
typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
/* ... and call it with our custom main function */
return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
}