CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/osx/x86/src/test/test_component.c
Views: 11784
1
/*
2
* test_component: Read in a component and execute it
3
*/
4
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <fcntl.h>
8
#include <unistd.h>
9
#include <pthread.h>
10
11
int read_and_exec_file(char* file)
12
{
13
char* buf = malloc(10000);
14
int f, n;
15
16
if ((f = open(file, O_RDONLY, 0)) < 0) {
17
perror("open");
18
exit(EXIT_FAILURE);
19
}
20
21
if ((n = read(f, buf, 100000)) < 0) {
22
perror("read");
23
exit(EXIT_FAILURE);
24
}
25
26
//printf("==> Read %d bytes, executing component...\n", n);
27
28
((void(*)(void))buf)();
29
30
printf("==> Done.\n");
31
32
return 0;
33
}
34
35
int create_read_and_exec_thread(char* file)
36
{
37
int err;
38
pthread_t pthread;
39
void* return_value;
40
41
if ((err = pthread_create(&pthread, NULL,
42
read_and_exec_file, (void*)file)) != 0) {
43
fprintf(stderr, "pthread_create: %s\n", strerror(err));
44
return -1;
45
}
46
47
if ((err = pthread_join(pthread, &return_value)) != 0) {
48
fprintf(stderr, "pthread_join: %s\n", strerror(err));
49
return -1;
50
}
51
52
return 0;
53
}
54
55
int main(int argc, char* argv[])
56
{
57
int c;
58
int threaded = 0;
59
60
while ((c = getopt(argc, argv, "tp:")) != EOF) {
61
switch (c) {
62
case 't':
63
threaded = 1;
64
break;
65
default:
66
fprintf(stderr, "usage: %s [ -t ] payload_bin\n", argv[0]);
67
exit(EXIT_FAILURE);
68
}
69
}
70
71
72
if (threaded)
73
create_read_and_exec_thread(argv[optind]);
74
else
75
read_and_exec_file(argv[optind]);
76
}
77
78