Blob


1 /*-
2 * Copyright (c) 2006 Matthias Schmidt <xhr @ giessen.ccc.de>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
27 /* $Id: dermob-cli.c,v 1.11 2006/09/02 01:29:51 matthias Exp $ */
29 #include "dermob.h"
30 #include "mach.h"
31 #include "defs.h"
32 #include "list.h"
34 void
35 usage(const char *file)
36 {
37 printf("Usage: %s [-cdhstux] <binary>\n", file);
38 printf(" -c: Display complete header\n");
39 printf(" -d: Display __DATA,__data section\n");
40 printf(" -h: Display mach-o header\n");
41 printf(" -s: Display __TEXT,__csting section\n");
42 printf(" -t: Display __TEXT,__text section\n");
43 printf(" -u: Display universal header\n");
44 printf(" -x: Display hexdump\n");
45 exit(1);
46 }
48 int
49 main (int argc, char **argv)
50 {
51 struct stat sb;
52 char *buffer, ch;
53 int fd, len=1, offset = 0, ncmds = 0, flag = 0, ret;
55 trigger = 0;
56 dynamic = 0;
57 dyn_display = 0;
59 if (argc < 2) {
60 usage(argv[0]);
61 }
63 while ((ch = getopt(argc, argv, "uhctxds")) != -1) {
64 switch (ch) {
65 case 'u': flag |= 0x1; break;
66 case 'h': flag |= 0x2; break;
67 case 'c': flag |= 0x4; break;
68 case 't': flag |= 0x8; break;
69 case 'x': flag |= 0x16; break;
70 case 'd': flag |= 0x32; break;
71 case 's': flag |= 0x64; break;
72 default: usage(argv[0]); break;
73 }
74 }
76 argc -= optind;
77 argv += optind;
79 printf("dermob -- mach-o binary analyzer\n");
80 printf(" (c) 2006-12 by xhr (@giessen.ccc.de)\n\n");
82 if ((stat(argv[0], &sb)) < 0)
83 errx(1, "Cannot open %s", argv[0]);
85 if ((fd = open(argv[0], O_RDONLY, 0)) < 0)
86 errx(1, "Cannot open %s", argv[0]);
88 if ((buffer = malloc(sb.st_size)) == NULL)
89 errx(1, "Cannot allocate memory");
91 size = sb.st_size;
92 len = read(fd, buffer, size);
94 cpu = get_cpu_information();
95 bo_a = get_bo_information();
97 lst = list_create_list();
99 switch (flag) {
100 // -u
101 case 0x1:
102 display_fat_header(lst, buffer, &offset);
103 list_traverse_list(lst);
104 break;
105 // -h
106 case 0x2:
107 display_fat_header(lst, buffer, &offset);
108 display_mo_header(lst, buffer, &offset, &ncmds);
109 list_traverse_list(lst);
110 break;
111 // -uh
112 case 0x3:
113 display_fat_header(lst, buffer, &offset);
114 display_mo_header(lst, buffer, &offset, &ncmds);
115 list_traverse_list(lst);
116 break;
117 // -c
118 case 0x4:
119 display_fat_header(lst, buffer, &offset);
120 display_mo_header(lst, buffer, &offset, &ncmds);
121 display_load_commands(lst, buffer, &offset, ncmds);
122 list_traverse_list(lst);
123 break;
124 // -t
125 case 0x8:
126 display_fat_header(lst, buffer, &offset);
127 display_mo_header(lst, buffer, &offset, &ncmds);
128 display_load_commands(lst, buffer, &offset, ncmds);
129 display_buffer(buffer, text_addr, text_offset, text_size);
130 break;
131 // -x
132 case 0x16:
133 display_buffer(buffer, 0, 0, len);
134 break;
135 // -d
136 case 0x32:
137 display_fat_header(lst, buffer, &offset);
138 display_mo_header(lst, buffer, &offset, &ncmds);
139 display_load_commands(lst, buffer, &offset, ncmds);
140 display_buffer(buffer, data_addr, data_offset, data_size);
141 break;
142 // -s
143 case 0x64:
144 display_fat_header(lst, buffer, &offset);
145 display_mo_header(lst, buffer, &offset, &ncmds);
146 display_load_commands(lst, buffer, &offset, ncmds);
147 display_buffer(buffer, cs_addr, cs_offset, cs_size);
148 break;
149 default:
150 ret = display_fat_header(lst, buffer, &offset);
151 if (ret > 0)
152 printf("- Universal Binary for %d architectures\n", ret);
153 ret = display_mo_header(lst, buffer, &offset, &ncmds);
154 if (ret > 0)
155 printf("- Vaild mach-o binary\n");
156 else {
157 printf("No mach-o file\n");
158 exit(1);
160 dyn_display = 1;
161 display_load_commands(lst, buffer, &offset, ncmds);
162 printf("%s", dynamic ? "" : "- Statically linked\n");
163 break;
166 list_free_list(lst);
168 close(fd);
169 free(buffer);
171 return (0);