-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpm3_command_parser.py
More file actions
325 lines (258 loc) · 11 KB
/
pm3_command_parser.py
File metadata and controls
325 lines (258 loc) · 11 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
"""
🧠 PM3 Command Parser - Dynamic Command Discovery
Automatically discovers and parses PM3 commands from help output
Makes the GUI future-proof against firmware updates
"""
import re
import json
import logging
from typing import Dict, List, Optional, Tuple
from pathlib import Path
from dataclasses import dataclass, field
logger = logging.getLogger('CyberNinjaRFID')
@dataclass
class PM3Command:
"""Represents a single PM3 command"""
name: str
category: str
description: str = ""
usage: str = ""
examples: List[str] = field(default_factory=list)
subcommands: List[str] = field(default_factory=list)
class PM3CommandParser:
"""
Dynamically parses PM3 commands from help output
Future-proof against firmware updates
"""
def __init__(self, pm3_device=None):
self.pm3_device = pm3_device
self.commands = {}
self.categories = {}
def parse_help_output(self, help_text: str) -> Dict[str, PM3Command]:
"""
Parse 'help' output to discover available commands
"""
commands = {}
# Split into lines
lines = help_text.split('\n')
current_category = "general"
for line in lines:
line = line.strip()
# Detect category headers (e.g., "High Frequency commands:")
if any(keyword in line.lower() for keyword in ['commands:', 'category:', 'module:']):
# Extract category name
match = re.search(r'([\w\s]+)\s*commands?:', line, re.IGNORECASE)
if match:
current_category = match.group(1).strip().lower()
if current_category not in self.categories:
self.categories[current_category] = []
continue
# Parse command lines (format: "command -- description" or "command - description")
if '--' in line or (len(line) > 3 and line[0].isalpha() and ' - ' in line):
parts = re.split(r'\s+(?:--|-)\s+', line, 1)
if len(parts) == 2:
cmd_name = parts[0].strip()
description = parts[1].strip()
# Create command object
cmd = PM3Command(
name=cmd_name,
category=current_category,
description=description
)
commands[cmd_name] = cmd
# Add to category
if current_category in self.categories:
self.categories[current_category].append(cmd_name)
else:
self.categories[current_category] = [cmd_name]
self.commands = commands
logger.info(f"📚 Discovered {len(commands)} commands across {len(self.categories)} categories")
return commands
def parse_module_help(self, module: str, help_text: str) -> List[PM3Command]:
"""
Parse module-specific help (e.g., 'hf help', 'lf help')
"""
commands = []
lines = help_text.split('\n')
for line in lines:
line = line.strip()
# Look for command patterns
if line.startswith(module):
# Extract command and description
match = re.match(rf'{module}\s+([\w\s]+?)\s+(?:--|-)\s+(.+)', line)
if match:
subcmd = match.group(1).strip()
desc = match.group(2).strip()
cmd = PM3Command(
name=f"{module} {subcmd}",
category=module,
description=desc
)
commands.append(cmd)
logger.info(f"📚 Discovered {len(commands)} commands in '{module}' module")
return commands
def discover_all_commands(self) -> Dict[str, PM3Command]:
"""
Discover ALL commands by querying PM3 help system
"""
if not self.pm3_device:
logger.warning("⚠️ No PM3 device connected, can't discover commands")
return {}
all_commands = {}
try:
# Get main help
self.pm3_device.console("help", capture=True, quiet=True)
help_output = self.pm3_device.grabbed_output
main_commands = self.parse_help_output(help_output)
all_commands.update(main_commands)
# Discover module-specific commands
modules = ['hf', 'lf', 'hw', 'data', 'trace', 'mem', 'smart', 'script']
for module in modules:
try:
self.pm3_device.console(f"{module} help", capture=True, quiet=True)
module_help = self.pm3_device.grabbed_output
module_commands = self.parse_module_help(module, module_help)
for cmd in module_commands:
all_commands[cmd.name] = cmd
except Exception as e:
logger.debug(f"Could not parse '{module}' help: {e}")
self.commands = all_commands
logger.info(f"🎯 Total commands discovered: {len(all_commands)}")
except Exception as e:
logger.error(f"Error discovering commands: {e}")
return all_commands
def get_commands_by_category(self, category: str) -> List[PM3Command]:
"""Get all commands in a category"""
if category in self.categories:
return [self.commands[cmd] for cmd in self.categories[category] if cmd in self.commands]
return []
def search_commands(self, query: str) -> List[PM3Command]:
"""Search commands by name or description"""
query = query.lower()
results = []
for cmd in self.commands.values():
if query in cmd.name.lower() or query in cmd.description.lower():
results.append(cmd)
return results
def save_commands_cache(self, filepath: Path):
"""Save discovered commands to cache file"""
try:
cache_data = {
'commands': {
name: {
'name': cmd.name,
'category': cmd.category,
'description': cmd.description,
'usage': cmd.usage,
'examples': cmd.examples,
'subcommands': cmd.subcommands,
}
for name, cmd in self.commands.items()
},
'categories': self.categories,
}
with open(filepath, 'w') as f:
json.dump(cache_data, f, indent=2)
logger.info(f"💾 Saved command cache to {filepath}")
except Exception as e:
logger.error(f"Error saving command cache: {e}")
def load_commands_cache(self, filepath: Path) -> bool:
"""Load commands from cache file"""
try:
if not filepath.exists():
return False
with open(filepath, 'r') as f:
cache_data = json.load(f)
# Reconstruct commands
self.commands = {}
for name, data in cache_data.get('commands', {}).items():
self.commands[name] = PM3Command(
name=data['name'],
category=data['category'],
description=data.get('description', ''),
usage=data.get('usage', ''),
examples=data.get('examples', []),
subcommands=data.get('subcommands', []),
)
self.categories = cache_data.get('categories', {})
logger.info(f"📂 Loaded {len(self.commands)} commands from cache")
return True
except Exception as e:
logger.error(f"Error loading command cache: {e}")
return False
def get_smart_suggestions(self, partial_command: str) -> List[str]:
"""Get command suggestions based on partial input"""
partial = partial_command.lower().strip()
if not partial:
return []
suggestions = []
# Exact prefix match
for cmd_name in self.commands.keys():
if cmd_name.lower().startswith(partial):
suggestions.append(cmd_name)
# If no prefix matches, try substring
if not suggestions:
for cmd_name in self.commands.keys():
if partial in cmd_name.lower():
suggestions.append(cmd_name)
return sorted(suggestions)[:10] # Top 10 suggestions
def load_command_profiles(self, filepath: Path) -> Dict[str, str]:
"""
Load command profiles/presets from JSON/YAML file
Format: {"Profile Name": "pm3 command"}
"""
try:
if not filepath.exists():
logger.warning(f"Profile file not found: {filepath}")
return {}
with open(filepath, 'r') as f:
profiles = json.load(f)
logger.info(f"📋 Loaded {len(profiles)} command profiles")
return profiles
except Exception as e:
logger.error(f"Error loading profiles: {e}")
return {}
def save_command_profiles(self, profiles: Dict[str, str], filepath: Path):
"""Save command profiles to file"""
try:
with open(filepath, 'w') as f:
json.dump(profiles, f, indent=2)
logger.info(f"💾 Saved {len(profiles)} profiles to {filepath}")
except Exception as e:
logger.error(f"Error saving profiles: {e}")
# Example usage and testing
if __name__ == "__main__":
# Test with sample help output
sample_help = """
Help text
=========
General commands:
help -- This text
quit -- Exit program
exit -- Exit program
High Frequency commands:
hf search -- Search for HF tags
hf 14a reader -- Read ISO14443A tag
hf 14a info -- Get tag information
Low Frequency commands:
lf search -- Search for LF tags
lf em 410x reader -- Read EM410x tag
lf t55xx detect -- Detect T55xx tag
"""
parser = PM3CommandParser()
commands = parser.parse_help_output(sample_help)
print(f"\n📚 Discovered {len(commands)} commands:")
for name, cmd in commands.items():
print(f" {name:20} [{cmd.category:15}] - {cmd.description}")
print(f"\n📁 Categories: {list(parser.categories.keys())}")
# Test search
print(f"\n🔍 Search 'read':")
results = parser.search_commands('read')
for cmd in results:
print(f" {cmd.name} - {cmd.description}")
# Test suggestions
print(f"\n💡 Suggestions for 'hf 14':")
suggestions = parser.get_smart_suggestions('hf 14')
for sug in suggestions:
print(f" {sug}")