-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcx-status
executable file
·97 lines (82 loc) · 2.83 KB
/
hcx-status
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
#!/usr/bin/env python
import os
import subprocess
import time
import tempfile
from contextlib import contextmanager
# Temporary file context manager
@contextmanager
def temporary_file(suffix='', prefix='temp', dir=None):
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix, prefix=prefix, dir=dir)
try:
yield temp_file.name
finally:
saferm(temp_file.name)
# Safe file removal function
def saferm(file_path):
try:
os.remove(file_path)
except FileNotFoundError:
print(f"File not found: {file_path}")
except PermissionError:
print(f"Permission denied: {file_path}")
except Exception as e:
print(f"Error removing file {file_path}: {e}")
# Hex to string conversion
def hex2str(hex_string):
try:
return bytes.fromhex(hex_string).decode('utf-8')
except ValueError:
return "HEX ERROR"
# Extract and print information from a line
def process_line(line, seen_bssids, counter):
split = line.split("*")
if len(split) < 6:
print("Invalid format")
return counter
htype = split[1]
type_str = "PMKID" if htype == "01" else "EAPOL" if htype == "02" else "UNKNOWN"
bssid = split[3].upper()
mac = split[4].upper()
essid = hex2str(split[5])
if bssid not in seen_bssids:
seen_bssids.add(bssid)
counter += 1
print(f"{counter}. {type_str} {bssid} {mac} {essid}")
return counter
def main():
seen_bssids = set()
counter = 0
try:
while True:
with temporary_file(suffix='.txt') as hash_file:
# Run the external command to generate the hash file
try:
subprocess.run(
f"hcxpcapngtool *.pcapng -o {hash_file}",
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error running hcxpcapngtool: {e}")
time.sleep(1)
continue
# Read and process the hash file
try:
with open(hash_file, 'r') as f:
lines = [line.strip() for line in f]
for line in lines:
if line:
counter = process_line(line, seen_bssids, counter)
except FileNotFoundError:
print(f"Hash file not found: {hash_file}")
except Exception as e:
print(f"Error reading hash file: {e}")
time.sleep(1)
except KeyboardInterrupt:
print("\nScript interrupted by user. Exiting gracefully...")
exit(0)
if __name__ == "__main__":
main()