Skip to content

Instantly share code, notes, and snippets.

@numinit
Last active Jul 1, 2017
Embed
What would you like to do?
dexstrings - see older revisions for what it used to output before I made it less verbose
/*
* gcc -Os -odexstrings dexstrings.c
* ./dexstrings /path/to/dex.dex
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
typedef uint8_t u8;
typedef uint32_t u32;
typedef struct dex_header {
u8 magic[8];
u32 checksum;
u8 signature[20];
u32 file_size;
u32 header_size;
u32 endian_tag;
u32 link_size;
u32 link_off;
u32 map_off;
u32 string_ids_size;
u32 string_ids_off;
u32 type_ids_size;
u32 type_ids_off;
u32 proto_ids_size;
u32 proto_ids_off;
u32 field_ids_size;
u32 field_ids_off;
u32 method_ids_size;
u32 method_ids_off;
u32 class_defs_size;
u32 class_defs_off;
u32 data_size;
u32 data_off;
} __attribute__ ((packed)) dex_header_t;
u32 dex_unleb128(FILE *f) {
u32 ret = 0;
u8 byte;
for (int i = 0; i < 5; i++) {
fread(&byte, 1, 1, f);
ret |= (byte & 0x7f) << (7 * i);
if ((byte & 0x80) == 0) {
break;
}
}
return ret;
}
u32 dex_print_string(FILE *f, u32 *string_ids, u32 id) {
u32 length;
fseek(f, string_ids[id], SEEK_SET);
length = dex_unleb128(f);
for (u32 j = 0; j < length; j++) {
u8 byte;
fread(&byte, 1, sizeof(u8), f);
if (isprint(byte)) {
printf("%c", byte);
} else {
printf("[%02x]", byte);
}
}
printf("\n");
return length;
}
int main(int argc, char **argv) {
dex_header_t header;
u32 *string_ids;
u32 string_ids_size;
FILE *f = fopen(argv[argc - 1], "r");
if (f == NULL) {
fprintf(stderr, "error opening %s\n", argv[argc - 1]);
return 1;
}
// read the header
fread(&header, sizeof(header), 1, f);
// create a string table
string_ids_size = header.string_ids_size;
string_ids = malloc(sizeof(u32) * string_ids_size);
// seek to the string id offset
fseek(f, header.string_ids_off, SEEK_SET);
// read the string table
fread(string_ids, sizeof(u32), string_ids_size, f);
// print strings
for (u32 i = 0; i < string_ids_size; i++) {
dex_print_string(f, string_ids, i);
}
free(string_ids);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment