forked from elfmaster/binflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
210 lines (180 loc) · 5.73 KB
/
Copy pathmain.c
File metadata and controls
210 lines (180 loc) · 5.73 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "binflow.h"
struct options opts;
void usage(void)
{
fprintf(stderr, "Usage: binflow [-secftvd] [-b <binary> [arg0,arg1,...] [-p <pid>]\n");
fprintf(stderr, "[-b] Specify binary\n"
"[-p] Specify process ID\n"
"[-s] Show strings\n"
"[-e] Extensive ELF info\n"
"[-c] Control flow analysis (Every branch instruction)\n"
"[-f] eh_frame (Exception handling frame) for function identification\n"
"[-t] Thread (clone/fork) support\n"
"[-v] Verbose output\n"
"[-d] Debugging output\n\n"
"Examples:\n"
"binflow -sc -b /bin/ls -R\n"
"binflow -fe -p 996\n");
exit(0);
}
int parse_sub_args(char **argv, int argc, char ***args, char *argv0)
{
int tokens = 0, i;
if ((*args = calloc(argc + 2, sizeof(char *))) == NULL)
tokens = -1;
*((*args) + 0) = xstrdup(argv0);
for (i = 1; i <= argc; i++, tokens++) {
*((*args) + i) = xstrdup(argv[i - 1]);
}
*((*args) + i) = NULL;
return tokens + 1;
}
int main(int argc, char **argv, char **envp)
{
handle_t *handle;
char *token = NULL, **args;
int ac, i, status, ret;
pid_t pid;
long ptraceOpts;
char *argv0;
if (argc < 3)
usage();
if (argv[1][0] != '-')
usage();
/*
* XXX handle is huge because of branch_site (Which is a massive array)
* so we must use heap. In the future move branch_site from array to
* list or tree for fast lookup.
*/
handle = (handle_t *)heapAlloc(sizeof(handle_t));
memset((void *)handle, 0, sizeof(handle_t));
memset((void *)&opts, 0, sizeof(opts));
if (argv[1][0] != '-')
usage();
if (argv[1][1] != 'b' && argv[1][1] != 'p') {
for (token = (argv[1] + 1); *token != '\0'; token++) {
switch(*token) {
case 'v':
opts.verbose++;
break;
case 's':
opts.strings++;
break;
case 'c':
opts.cflow++;
break;
case 'e':
opts.elfdata++;
break;
case 'f':
opts.ehframe++;
break;
case 't':
opts.threads++;
break;
case 'd':
opts.debug++;
break;
default:
printf("Unknown option: '%c'\n", *token);
usage();
}
}
if (argc < 4)
usage();
if (argv[2][0] != '-')
usage();
if (argv[2][1] == 'b')
handle->path = strdup(argv[3]);
else
if (argv[2][1] == 'p')
handle->pid = atoi(argv[3]);
else
usage();
argv0 = handle->path ? xstrdup(argv[3]) : get_path(handle->pid);
ac = parse_sub_args(&argv[4], argc - 4, &handle->args, argv0);
} else {
if (argv[1][0] != '-')
usage();
if (argv[1][1] == 'b')
handle->path = strdup(argv[2]);
else
if (argv[1][1] == 'p')
handle->pid = atoi(argv[2]);
else
usage();
argv0 = handle->path ? xstrdup(argv[2]) : get_path(handle->pid);
ac = parse_sub_args(&argv[3], argc - 3, &handle->args, argv0);
}
switch(__ELF_NATIVE_CLASS) {
case 32:
opts.arch = 32;
break;
case 64:
opts.arch = 64;
break;
default:
fprintf(stderr, "[!] Unsupported architecture: %d\n", __ELF_NATIVE_CLASS);
exit(0);
}
handle->arch = opts.arch;
pid = handle->pid;
if (pid) {
if (opts.verbose)
printf("[+] Attaching to pid: %d\n", pid);
opts.attach++;
handle->path = get_path(handle->pid);
}
if (!validate_em_type(handle->path)) {
printf("[!] ELF Architecture is set to %d, the target %s is not the same architecture\n", opts.arch, handle->path);
exit(-1);
}
/*
* process_binary() will create the mapping of branch instructions
* and layout of the executable, so that we can instrument it before
* execution.
*/
if ((ret = process_binary(handle) < 0)) {
fprintf(stderr, "process_binary() failed on [%s]\n", handle->path);
exit(-1);
}
if (!opts.attach) {
if ((pid = fork()) < 0) {
perror("fork");
exit(-1);
}
if (pid == 0) {
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) {
perror("PTRACE_TRACEME");
exit(-1);
}
ptraceOpts = PTRACE_O_TRACECLONE|PTRACE_O_TRACEFORK|PTRACE_O_TRACEEXEC|PTRACE_O_TRACEEXIT|PTRACE_O_EXITKILL;
ptrace(PTRACE_SETOPTIONS, 0, 0, ptraceOpts);
execve(handle->path, handle->args, envp);
exit(0);
}
wait(&status);
handle->pid = pid;
if (opts.debug)
printf("[+] Calling instrument_process()\n");
instrument_process(handle);
if (opts.debug)
printf("[+] Calling examine_process()\n");
examine_process(handle);
goto done;
}
printf("Attaching to %d\n", handle->pid);
if (ptrace(PTRACE_ATTACH, handle->pid, NULL, NULL) == -1) {
perror("PTRACE_ATTACH");
exit(-1);
}
wait(&status);
// waitpid(handle->pid, &status, WUNTRACED);
instrument_process(handle);
examine_process(handle);
for (i = 0; i < ac; i++)
printf("arg[%d]: %s\n", i, handle->args[i]);
done:
free(handle);
exit(0);
}