forked from marin-m/vmlinux-to-elf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·66 lines (47 loc) · 2.02 KB
/
tests.py
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
#!/usr/bin/env python3
#-*- encoding: Utf-8 -*-
"""
Check the program's ability to decompress
the files files whose the absolute path
is referenced by the "test_kernels.txt"
present in the current directory (one kernel
path per line, separated with LF)
As this file is local to your own machine,
it is ignored by the ".gitignore"
It will also write successfully reconstructed
ELF files to an automatically created "tests_output/" folder.
"""
try:
from .vmlinuz_decompressor import obtain_raw_kernel_from_file
from .elf_symbolizer import ElfSymbolizer
except ImportError:
from vmlinuz_decompressor import obtain_raw_kernel_from_file
from elf_symbolizer import ElfSymbolizer
from os.path import dirname, realpath, exists
from traceback import print_exc
from os import makedirs
from re import sub
from sys import stdout
import logging
SCRIPT_DIR = dirname(realpath(__file__))
TEST_KERNELS_PATH = realpath(SCRIPT_DIR + '/test_kernels.txt')
ELF_KERNELS_OUTPUT_PATH = realpath(SCRIPT_DIR + '/tests_output')
def slugify(file_path):
return sub('[^a-z0-9]+', '-', file_path.lower()).strip('-')
if __name__ == '__main__':
logging.basicConfig(stream=stdout, level=logging.INFO, format='%(message)s')
if not exists(TEST_KERNELS_PATH):
exit(('[!] In order to use this script, please ' +
'create a file at %s, containing to path ' +
'to one kernel to extract per line. Quitting.') % (TEST_KERNELS_PATH))
makedirs(ELF_KERNELS_OUTPUT_PATH, exist_ok = True)
for file_name in filter(None, map(str.strip, open(TEST_KERNELS_PATH, 'r'))):
logging.info('Testing ' + file_name)
with open(file_name, 'rb') as fd:
contents = fd.read()
raw_data = obtain_raw_kernel_from_file(contents)
try:
ElfSymbolizer(raw_data, ELF_KERNELS_OUTPUT_PATH + '/' + slugify(file_name) + '.elf')
except Exception:
logging.error('=> No symbols!')
print_exc()