Blame


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