Last active
January 10, 2022 18:07
-
-
Save chop0/bb1bd00a61fb56065531b7866489cb44 to your computer and use it in GitHub Desktop.
Kernel symbol parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include "parse.h" | |
#define BUFSIZE 256 | |
FILE* open_map() { | |
FILE* ver = fopen("/proc/version", "r"); | |
char buffer[BUFSIZE]; | |
if(!fgets(buffer, BUFSIZE, ver)) | |
return NULL; | |
char* version = buffer + strcspn(buffer, "5"); | |
version[strcspn(version, " ")] = 0; | |
char filename[BUFSIZE] = "/boot/System.map-"; | |
strcat(filename, version); | |
return fopen(filename, "r"); | |
} | |
symbol_list parse_symbols(char* names[], int num) { | |
FILE* fp = open_map(); | |
char buffer[BUFSIZE]; | |
int len = 0; | |
parsed_symbol* parsed_symbols = malloc(num * sizeof(parsed_symbol)); | |
parsed_symbol* curr_symbol = parsed_symbols; | |
while(fgets(buffer, BUFSIZE, fp)) { | |
buffer[strcspn(buffer, "\n")] = 0; | |
char* symbol_name = buffer + 19; | |
int symbol_name_length = strlen(symbol_name); | |
for(int i = 0; i < num; i++) { | |
if(strcmp(symbol_name, names[i]) == 0) { | |
len++; | |
curr_symbol->address = strtoul(buffer, NULL, 16) & ~0xffffffff81000000ull; | |
curr_symbol->name = malloc(symbol_name_length + 1); | |
strcpy(curr_symbol->name, symbol_name); | |
curr_symbol++; | |
break; | |
} | |
} | |
} | |
fclose(fp); | |
symbol_list result = { len, parsed_symbols }; | |
return result; | |
} | |
int main() { | |
char* names[] = {"commit_creds"}; | |
int num = 1; | |
symbol_list s = parse_symbols(names, num); | |
parsed_symbol* t = s.symbols; | |
for(int i = 0; i < num; i++, t++) { | |
printf("%s %p\n", t->name, t->address); | |
} | |
free_symbol_list(s); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _PARSE_H | |
#define _PARSE_H | |
#include <stdlib.h> | |
typedef struct parsed_symbol { | |
unsigned long address; | |
char* name; | |
} parsed_symbol; | |
typedef struct symbol_list { | |
int length; | |
parsed_symbol* symbols; | |
} symbol_list; | |
void free_symbol_list(symbol_list list) { | |
parsed_symbol* sym = list.symbols; | |
for(int i = 0; i < list.length; i++, sym++) { | |
free(sym->name); | |
} | |
free(list.symbols); | |
} | |
symbol_list parse_symbols(char* names[], int num); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment