-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-debuggable.c
More file actions
173 lines (143 loc) · 5.28 KB
/
set-debuggable.c
File metadata and controls
173 lines (143 loc) · 5.28 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
/*
* Copyright (C) Barebit s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include "set-debuggable.h"
static const char filename[] = "AndroidManifest.xml";
static struct ResChunk_header chunk;
//static struct ResXMLTree_node node;
static struct ResXMLTree_attrExt attrExt;
static struct ResXMLTree_attribute attribute;
static void success (FILE *stream)
{
fclose (stream);
exit (EXIT_SUCCESS);
}
static void failure (FILE *stream)
{
fclose (stream);
exit (EXIT_FAILURE);
}
static void fseekx (FILE *stream, long offset, int origin)
{
if (fseek (stream, offset, origin) != 0) {
printf ("error: fseek()\n");
failure (stream);
}
}
static void freadx (void *buffer, size_t size, FILE *stream)
{
if (fread (buffer, size, 1, stream) != 1) {
printf ("error: fread()\n");
failure (stream);
}
}
int main ()
{
unsigned node_bytes_read = 0;
unsigned xml_bytes_total;
unsigned debuggable_attr_index;
unsigned debuggable_attr_found = 0;
FILE *file = fopen (filename, "rb+");
if (file == 0) {
printf ("error: couldn't open %s\n", filename);
return EXIT_FAILURE;
}
freadx (&chunk, sizeof (chunk), file);
if (chunk.type != RES_XML_TYPE) {
printf ("error: unexpected header type (not XML resource type)\n");
failure (file);
}
if (chunk.headerSize != sizeof (chunk)) {
printf ("error: unexpected header size\n");
failure (file);
}
xml_bytes_total = chunk.size;
node_bytes_read = ftell (file);
while (node_bytes_read < xml_bytes_total) {
freadx (&chunk, sizeof (chunk), file);
if (chunk.type == RES_XML_RESOURCE_MAP_TYPE) {
unsigned dwords_read;
unsigned dwords_total = (chunk.size - chunk.headerSize) / sizeof (uint32_t);
uint32_t item;
printf ("RESOURCE_MAP: (offset %08X, type %08X, size %08X)\n", node_bytes_read, chunk.type, chunk.size);
for (dwords_read = 0; dwords_read < dwords_total; dwords_read++) {
freadx (&item, sizeof (uint32_t), file);
printf ("[%04X] %08X", dwords_read, item);
if (item == DEBUGGABLE_ATTR) {
debuggable_attr_found = 1;
debuggable_attr_index = dwords_read;
printf (": DEBUGGABLE_ATTR");
}
printf ("\n");
}
if (!debuggable_attr_found) {
printf ("error: debuggable attribute not found (not indicated in resource map)\n");
failure (file);
}
}
else if (chunk.type == RES_XML_START_ELEMENT_TYPE) {
unsigned x;
// it is expected that RESOURCE_MAP has been processed up to this point
if (!debuggable_attr_found) {
printf ("error: RESOURCE_MAP not found prior to START_ELEMENT\n");
failure (file);
}
// move to extended info
fseekx (file, chunk.headerSize - sizeof (chunk), SEEK_CUR);
// attrExt node with attribute information follows
freadx (&attrExt, sizeof (attrExt), file);
printf ("START_ELEMENT with %u attribute(s) found\n", attrExt.attributeCount);
if (attrExt.attributeCount > 0) {
// move to first attribute
fseekx (file, attrExt.attributeStart - sizeof (attrExt), SEEK_CUR);
for (x = 0; x < attrExt.attributeCount; x++) {
freadx (&attribute, sizeof (attribute), file);
if (attribute.name.index == debuggable_attr_index) {
printf ("'debuggable' attribute: name ref %08X, value %08X\n", attribute.name.index, attribute.typedValue.data);
if (attribute.typedValue.data != 0) {
printf ("-> the debuggable attribute is already set.\n");
success (file);
}
else {
uint32_t data = 0xFFFFFFFF; // value of "true"
int seek = offsetof (struct ResXMLTree_attribute, typedValue.data) - sizeof (attrExt);
fseekx (file, seek, SEEK_CUR);
if (fwrite (&data, sizeof (uint32_t), 1, file) != 1) {
printf ("error: couldn't write new debuggable attribute value %08X at offset %08X.\n", data, ftell (file));
failure (file);
}
printf ("the debuggable attribute value successfully rewritten to %08X.\n", data);
success (file);
}
}
else {
printf ("skipping attribute with name ref %08X, value %08X\n", attribute.name.index, attribute.typedValue.data);
}
}
}
}
else {
printf ("skipping node at offset %08X of type %08X, size %08X\n", node_bytes_read, chunk.type, chunk.size);
fseekx (file, chunk.size - sizeof (chunk), SEEK_CUR);
}
node_bytes_read = ftell (file);
}
printf ("error: unexpected manifest format (debuggable attribute indicated in resource map but not really referenced)\n");
failure (file);
}