-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_exec.py
36 lines (28 loc) · 1.01 KB
/
read_exec.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
import os, sys, re
def main():
if len(sys.argv) != 3:
sys.exit('\r\nUsage: <file-path> <output.txt>\r\n')
if not os.path.exists(sys.argv[1]):
sys.exit('\r\nError! File not found!\r\n')
# open/read file
try:
with open(sys.argv[1], 'rb') as file:
# read binary content
bin = file.read()
# extract ascii (w/ 4+ printable chars)
text = re.findall(b'[ -~]{4,}', bin)
bin_decode = [t.decode('ascii', errors='ignore') for t in text]
# if content, dump to .txt
if not bin_decode:
sys.exit('\r\nNothing found!')
else:
# create .txt
with open(sys.argv[2], 'w') as file2:
# iterate through list
for line in bin_decode:
# append to file
file2.write(f"{line}\n")
except Exception as e:
sys.exit(f'\r\nError: {e}\r\n')
if __name__ == '__main__':
main()