-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
72 lines (60 loc) · 2.2 KB
/
main.cpp
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smun <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 19:23:11 by smun #+# #+# */
/* Updated: 2022/06/01 20:05:57 by smun ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <string>
#include <filesystem>
#include <vector>
extern char **environ;
#define RED "\e[31m"
#define GREEN "\e[32m"
#define YELLOW "\e[33m"
#define RESET "\e[0m"
#define CYAN "\e[36m"
#define BLUE "\e[34m"
#define PURPLE "\e[35m"
static void set_dyld_library_path()
{
const auto libpath = std::filesystem::current_path() / "hooklib" / "testmalloc.dylib";
setenv("DYLD_INSERT_LIBRARIES", libpath.c_str(), 1);
}
int main(int argc, char *argv[])
{
std::cout << std::endl;
std::cout << CYAN " ::: MALLOC " RED "BOMBER :::" RESET << std::endl;
std::cout << std::endl;
if (argc <= 1)
{
std::cerr << "Usage:: " << std::endl;
std::cerr << argv[0] << " [Executable] (Arguments)" << std::endl;
return EXIT_FAILURE;
}
const auto pid = fork();
if (pid == 0)
return 0;
// Make arguments list
std::vector<char*> arguments;
for (int i = 1; i < argc; ++i)
arguments.push_back(argv[i]);
arguments.push_back(NULL);
// Set DYLD_LIBRARY_PATH environment variable
set_dyld_library_path();
if (-1 == execv(arguments[0], &arguments[0]))
{
std::perror("Failed to execv: ");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}