summaryrefslogtreecommitdiff
path: root/other/burneye2/testcases/2-obf/reducebind.c
blob: 6432670e75321e7dfa30f7d8ac59efac7a71f13b (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* reducebind.c - dynamic to static binary conversion utility
 *
 * by scut
 *
 * BETA SOFTWARE, USE ON YOUR OWN RISK
 *
 * x86/linux only so far. some binaries segfault deep in their code, but this
 * does not seem to relate to the binary size. some binaries that have a 19mb
 * size statically linked (qt designer for example ;) work, some small
 * binaries, such as bash do not work.
 */

#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <elf.h>

#define	VERSION "0.1.0"

/*** local prototypes */
static void elf_dump_new (pid_t pid, const char *pathname_new,
	const char *pathname_old);
static void file_advance_roundup (FILE *fp, unsigned int padding);
static Elf32_Addr elf_get_entrypoint (const char *pathname);


int
main (int argc, char *argv[], char *envp[])
{
	char *		pathname;
	char *		f_argv[2];
	pid_t		fpid;	/* child pid, gets ptraced */
	struct user	regs;	/* PTRACE pulled registers */
	Elf32_Addr	entry;
	char *		output = "output";

	fprintf (stderr, "reducebind version "VERSION"\n\n");

	if (argc < 2) {
		fprintf (stderr, "usage: %s <binary> [output]\n\n", argv[0]);

		exit (EXIT_FAILURE);
	}
	pathname = argv[1];
	if (argc >= 3)
		output = argv[2];

	entry = elf_get_entrypoint (pathname);

	fpid = fork ();
	if (fpid < 0) {
		perror ("fork");
		exit (EXIT_FAILURE);
	}

	/* child process.
	 */
	if (fpid == 0) {
		if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) {
			perror ("ptrace PTRACE_TRACEME");
			exit (EXIT_FAILURE);
		}
		fprintf (stderr, "  child: TRACEME set\n");

		fprintf (stderr, "  child: executing: %s\n", pathname);
		close (1);
		dup2 (2, 1);

		/* prepare arguments and environment.
		 */
		f_argv[0] = pathname;
		f_argv[1] = NULL;

		putenv ("LD_BIND_NOW=1");
		execve (f_argv[0], f_argv, envp);

		/* failed ? */
		perror ("execve");
		exit (EXIT_FAILURE);
	}

	wait (NULL);

	memset (&regs, 0, sizeof (regs));

	if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
		perror ("ptrace PTRACE_GETREGS");
		exit (EXIT_FAILURE);
	}
	fprintf (stderr, "(%d) [0x%08lx] first stop\n", fpid, regs.regs.eip);
	fprintf (stderr, "(%d) tracing until entry point is reached (0x%08x)\n",
		fpid, entry);

	while (regs.regs.eip != entry) {
		if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) {
			perror ("ptrace PTRACE_SINGLESTEP");
			exit (EXIT_FAILURE);
		}
		wait (NULL);

		memset (&regs, 0, sizeof (regs));
		if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
			perror ("ptrace PTRACE_GETREGS");
			exit (EXIT_FAILURE);
		}
		fprintf (stderr, "\r(%d) [0x%08lx]", fpid, regs.regs.eip);
	}

	fprintf (stderr, "\n(%d) entry point reached\n", fpid);
	fprintf (stderr, "(%d) dumping process memory to new ELF ET_EXEC file\n",
		fpid);

	elf_dump_new (fpid, output, pathname);

	exit (EXIT_SUCCESS);
}


static void
elf_dump_new (pid_t pid, const char *pathname_new, const char *pathname_old)
{
	FILE *		fpn;
	FILE *		fpo;
	FILE *		mapsfp;
	char		maps_pathname[32];
	char		map_line[256];
	Elf32_Ehdr	eh;
	Elf32_Phdr	phdr[128];
	unsigned int	pn;		/* program header table index */


	fpn = fopen (pathname_new, "wb");
	fpo = fopen (pathname_old, "rb");
	if (fpn == NULL || fpo == NULL) {
		perror ("fopen output ELF file creation");
		exit (EXIT_FAILURE);
	}

	if (fread (&eh, sizeof (eh), 1, fpo) != 1) {
		perror ("fread ELF header");
		exit (EXIT_FAILURE);
	}
	fclose (fpo);

	/* kill header values */
	eh.e_shoff = 0x0;	/* we do not need any sections for loading */
	eh.e_shnum = 0;
	eh.e_shstrndx = 0;

	/* the program header table will be fixed later */
	eh.e_phoff = 0;
	eh.e_phnum = 0;

	fwrite (&eh, sizeof (eh), 1, fpn);

	snprintf (maps_pathname, sizeof (maps_pathname) - 1,
		"/proc/%d/maps", pid);
	maps_pathname[sizeof (maps_pathname) - 1] = '\0';
	mapsfp = fopen (maps_pathname, "r");
	if (mapsfp == NULL) {
		perror ("fopen map file");
		exit (EXIT_FAILURE);
	}

	while (1) {
		Elf32_Phdr *	ph;
		unsigned int	addr_start,
				addr_end,
				addr_walker;
		char		map_perm[8];
		unsigned char	data_saved[sizeof (unsigned long int)];


		memset (map_line, '\0', sizeof (map_line));
		if (fgets (map_line, sizeof (map_line) - 1, mapsfp) == NULL)
			break;
		map_line[sizeof (map_line) - 1] = '\0';

		fprintf (stderr, "%s", map_line);
		if (sscanf (map_line, "%08x-%08x %7[rwxp-] ",
			&addr_start, &addr_end, map_perm) != 3) 
		{
			perror ("invalid map-line");

			exit (EXIT_FAILURE);
		}

		/* we do not need the stack in here.
		 */
		if (addr_end == 0xc0000000)
			continue;

		file_advance_roundup (fpn, PAGE_SIZE);

		ph = &phdr[eh.e_phnum];
		eh.e_phnum += 1;
		memset (ph, 0x00, sizeof (Elf32_Phdr));

		ph->p_type = PT_LOAD;
		ph->p_offset = ftell (fpn);
		ph->p_vaddr = addr_start;
		ph->p_paddr = 0x0;
		ph->p_filesz = ph->p_memsz = addr_end - addr_start;
		ph->p_flags = 0;
		if (map_perm[0] == 'r')
			ph->p_flags |= PF_R;
		if (map_perm[1] == 'w')
			ph->p_flags |= PF_W;
		if (map_perm[2] == 'x')
			ph->p_flags |= PF_X;
		ph->p_align = PAGE_SIZE;

		/* save segment data, assuming addr is page aligned
		 */
		for (addr_walker = 0 ; addr_walker < (addr_end - addr_start);
			addr_walker += sizeof (data_saved))
		{
			errno = 0;

			*((unsigned long int *) &data_saved[0]) =
				ptrace (PTRACE_PEEKDATA, pid,
					addr_start + addr_walker, NULL);

			if (errno == 0 && fwrite (&data_saved[0],
				sizeof (data_saved), 1, fpn) != 1)
			{
				perror ("fwrite segment");

				exit (EXIT_FAILURE);
			} else if (errno != 0) {
				fprintf (stderr,
					"[0x%08x] invalid PTRACE_PEEKDATA\n",
					addr_start + addr_walker);

				exit (EXIT_FAILURE);
			}
		}
	}

	fclose (mapsfp);

	/* now write program header table
	 */
	file_advance_roundup (fpn, PAGE_SIZE);
	eh.e_phoff = ftell (fpn);

	for (pn = 0 ; pn < eh.e_phnum ; ++pn) {
		if (fwrite (&phdr[pn], sizeof (Elf32_Phdr), 1, fpn) != 1) {
			perror ("fwrite program header");
			exit (EXIT_FAILURE);
		}
	}

	fseek (fpn, 0, SEEK_SET);
	if (fwrite (&eh, sizeof (Elf32_Ehdr), 1, fpn) != 1) {
		perror ("fwrite final ELF header");
		exit (EXIT_FAILURE);
	}

	fclose (fpn);
	chmod (pathname_new, 0700);
}


static void
file_advance_roundup (FILE *fp, unsigned int padding)
{
	unsigned int	pos;

	pos = ftell (fp);
	if (pos % padding == 0)
		return;

	pos %= padding;
	pos = padding - pos;

	fseek (fp, pos, SEEK_CUR);
}


static Elf32_Addr
elf_get_entrypoint (const char *pathname)
{
	FILE *		fp;
	Elf32_Ehdr	eh;


	fp = fopen (pathname, "rb");
	if (fp == NULL) {
		perror ("fopen input ELF file");
		exit (EXIT_FAILURE);
	}

	if (fread (&eh, sizeof (eh), 1, fp) != 1) {
		perror ("fread");
		exit (EXIT_FAILURE);
	}

	fclose (fp);

	return (eh.e_entry);
}