-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbldd.py
119 lines (103 loc) · 3.64 KB
/
bldd.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
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
import argparse
import os
import time
import threading
SCRIPT_NAME = "elfs_parser.sh"
OUTFILE_NAME = "report.txt"
RUNNING = False
class bcolors:
OKGREEN = '\033[92m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
def print_path(path):
print(f"{bcolors.OKGREEN}Path: {bcolors.ENDC}{path}")
def print_error(error):
print(bcolors.FAIL + "Error:" + bcolors.ENDC + " " + error)
exit(1)
def parse_data(lines):
data = {}
file_name = None
machine = None
libraries = None
for line in lines:
words = line.split()
if len(words) <= 1:
continue
if words[0] == "File:":
file_name = words[1]
elif words[0] == "Machine:":
machine = " ".join(words[1:])
elif words[0] == "Libraries:":
libraries = []
for word in words[1:]:
libraries.append(word[1:-1])
if machine == None or file_name == None or libraries == None:
print("Some data are missing! exiting.")
exit(2)
if machine not in data.keys():
data[machine] = {}
for lib in libraries:
if lib not in data[machine].keys():
data[machine][lib] = []
data[machine][lib].append(file_name)
return data
def waiting():
actions = ['-','\\','|','/']
colors = [bcolors.OKGREEN,bcolors.FAIL,bcolors.OKGREEN,bcolors.FAIL]
while RUNNING:
for i in range(0,len(actions)):
if not RUNNING:
break;
print('[' + colors[i] + actions[i] + bcolors.ENDC + '] Working...',end="")
time.sleep(0.25)
print("\r",end="")
time.sleep(0.25)
def write_data():
file = open(OUTFILE_NAME, "w")
for machine,libraries in data.items():
file.write(f'------------------------------ {machine} ------------------------------\n')
libs = list(libraries.items())
sorted_libs = sorted(libs, key = lambda item: -len(item[1]))
for library,files in sorted_libs:
file.write(f"{library} ({len(files)} execs)\n")
for file_name in files:
file.write(f"\t\t-> {file_name}\n")
file.write("\n")
file.close()
def prase_args():
def valid_path(path):
if os.path.exists(path):
return path
else:
raise FileNotFoundError(path)
parser = argparse.ArgumentParser(
prog='bldd.py',
description="""Backward ldd: shows all elf executable
files that use specified shared library files."""
)
parser.add_argument("-o <file>","--output_file",type=str,default=OUTFILE_NAME,
help="Place the output into <file>. Default value is <report.txt>")
parser.add_argument("-p <path>","--path",type=valid_path,default=os.getcwd(),
help="Find all shared library used by executables in <path> directory and subdirectories")
return parser.parse_args()
if __name__ == "__main__":
args = prase_args()
path = args.path
OUTFILE_NAME = args.output_file
print_path(path)
try:
RUNNING = True
thread = threading.Thread(target=waiting)
thread.start()
lines = os.popen(f"./{SCRIPT_NAME} {path}").read().split('\n')
RUNNING = False
thread.join()
data = parse_data(lines)
except KeyboardInterrupt:
print(bcolors.ENDC + "Terminating.")
RUNNING = False
thread.join()
exit(0)
write_data()
print(f"{bcolors.BOLD}Done! {bcolors.ENDC}Printed output to {OUTFILE_NAME}.")