1
+ #!/usr/bin/env python3
2
+ import json
3
+ import os
4
+ import sys
5
+ import binascii
6
+
7
+ def load_config ():
8
+ """Load configuration from config.json"""
9
+ try :
10
+ with open ('config.json' , 'r' ) as f :
11
+ return json .load (f )
12
+ except (FileNotFoundError , json .JSONDecodeError ) as e :
13
+ print (f"[!] Error loading config: { str (e )} " )
14
+ return {"card_data_dir" : "./cards" , "nfc_reader" : "usb" }
15
+
16
+ def get_card_path (uid ):
17
+ """Get the path for a card file based on its UID"""
18
+ config = load_config ()
19
+ card_data_dir = config .get ('card_data_dir' , './cards' )
20
+ return os .path .join (card_data_dir , f"card_{ uid } .json" )
21
+
22
+ def list_saved_cards ():
23
+ """List all saved cards"""
24
+ config = load_config ()
25
+ card_data_dir = config .get ('card_data_dir' , './cards' )
26
+
27
+ if not os .path .exists (card_data_dir ):
28
+ print (f"[!] Card directory not found: { card_data_dir } " )
29
+ return []
30
+
31
+ card_files = [f for f in os .listdir (card_data_dir ) if f .startswith ('card_' ) and f .endswith ('.json' )]
32
+
33
+ cards = []
34
+ for card_file in card_files :
35
+ try :
36
+ with open (os .path .join (card_data_dir , card_file ), 'r' ) as f :
37
+ card_data = json .load (f )
38
+ uid = card_data .get ('UID' , 'Unknown' )
39
+ card_type = card_data .get ('Type' , 'Unknown' )
40
+ timestamp = card_data .get ('Timestamp' , 0 )
41
+
42
+ cards .append ({
43
+ 'uid' : uid ,
44
+ 'type' : card_type ,
45
+ 'timestamp' : timestamp ,
46
+ 'file' : card_file
47
+ })
48
+ except Exception as e :
49
+ print (f"[!] Error reading card file { card_file } : { str (e )} " )
50
+
51
+ return cards
52
+
53
+ def parse_hex_string (hex_string ):
54
+ """Convert a hex string to byte array"""
55
+ try :
56
+ return binascii .unhexlify (hex_string )
57
+ except binascii .Error as e :
58
+ print (f"[!] Invalid hex string: { str (e )} " )
59
+ return None
60
+
61
+ def analyze_card (uid ):
62
+ """Analyze a saved card and print detailed information"""
63
+ card_path = get_card_path (uid )
64
+
65
+ if not os .path .exists (card_path ):
66
+ print (f"[!] Card not found: { uid } " )
67
+ return False
68
+
69
+ try :
70
+ with open (card_path , 'r' ) as f :
71
+ card_data = json .load (f )
72
+
73
+ print (f"\n [*] Card Analysis: { uid } " )
74
+ print ("-" * 50 )
75
+ print (f"Type: { card_data .get ('Type' , 'Unknown' )} " )
76
+
77
+ if 'Technologies' in card_data :
78
+ print ("\n Supported Technologies:" )
79
+ for tech in card_data ['Technologies' ]:
80
+ print (f"- { tech } " )
81
+
82
+ if 'NDEF' in card_data :
83
+ print ("\n NDEF Records:" )
84
+ for i , record in enumerate (card_data ['NDEF' ]):
85
+ print (f"\n Record { i + 1 } :" )
86
+ print (f" Type: { record .get ('type' , 'Unknown' )} " )
87
+ if 'text' in record :
88
+ print (f" Text: { record ['text' ]} " )
89
+
90
+ if 'MIFARE_Data' in card_data :
91
+ print ("\n MIFARE Sectors:" )
92
+ for sector , data in card_data ['MIFARE_Data' ].items ():
93
+ if not data .startswith ('Error' ):
94
+ print (f" { sector } : { data [:20 ]} ..." )
95
+ else :
96
+ print (f" { sector } : { data } " )
97
+
98
+ return True
99
+
100
+ except Exception as e :
101
+ print (f"[!] Error analyzing card: { str (e )} " )
102
+ return False
0 commit comments