diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index be95a68..2c0425c 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -34,6 +34,14 @@ data_frame_tx_t *cmd_processor_get_git_version(uint16_t cmd, uint16_t status, ui return data_frame_make(cmd, status, strlen(GIT_VERSION), (uint8_t *)GIT_VERSION); } +data_frame_tx_t *cmd_processor_get_device(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { +#if defined(PROJECT_CHAMELEON_ULTRA) + return data_frame_make(cmd, status, 1, (uint8_t *)1); +#else + return data_frame_make(cmd, status, 1, (uint8_t *)0); +#endif +} + data_frame_tx_t *cmd_processor_change_device_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { if (length == 1) { @@ -860,6 +868,9 @@ static cmd_data_map_t m_data_cmd_map[] = { { DATA_CMD_GET_BLE_CONNECT_KEY_CONFIG, NULL, cmd_processor_get_ble_connect_key, NULL }, { DATA_CMD_SET_BLE_CONNECT_KEY_CONFIG, NULL, cmd_processor_set_ble_connect_key, NULL }, { DATA_CMD_DELETE_ALL_BLE_BONDS, NULL, cmd_processor_del_ble_all_bonds, NULL }, + { DATA_CMD_GET_DEVICE, NULL, cmd_processor_get_device, NULL }, + // { DATA_CMD_GET_SETTINGS, NULL, NULL, NULL }, + { DATA_CMD_GET_DEVICE_CAPABILITIES, NULL, NULL, NULL }, #if defined(PROJECT_CHAMELEON_ULTRA) @@ -919,6 +930,31 @@ static cmd_data_map_t m_data_cmd_map[] = { }; +data_frame_tx_t *cmd_processor_get_capabilities(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { + size_t count = sizeof(m_data_cmd_map) / sizeof(m_data_cmd_map[0]); + uint16_t commands[count]; + memset(commands, 0, count * sizeof(uint16_t)); + + for (size_t i = 0; i < count; i++) { + // beware: wrong endianness + commands[i] = m_data_cmd_map[i].cmd; + } + + return data_frame_make(cmd, status, count * sizeof(uint16_t), (uint8_t *)commands); +} + + +void cmd_map_init() { + size_t count = sizeof(m_data_cmd_map) / sizeof(m_data_cmd_map[0]); + + for (size_t i = 0; i < count; i++) { + if (m_data_cmd_map[i].cmd == DATA_CMD_GET_DEVICE_CAPABILITIES) { + m_data_cmd_map[i].cmd_processor = cmd_processor_get_capabilities; + return; + } + } +} + /** * @brief Auto select source to response * diff --git a/firmware/application/src/app_cmd.h b/firmware/application/src/app_cmd.h index 538c069..02b963e 100644 --- a/firmware/application/src/app_cmd.h +++ b/firmware/application/src/app_cmd.h @@ -15,5 +15,6 @@ typedef struct { } cmd_data_map_t; void on_data_frame_received(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data); +void cmd_map_init(); #endif diff --git a/firmware/application/src/app_main.c b/firmware/application/src/app_main.c index d398149..f303d7d 100644 --- a/firmware/application/src/app_main.c +++ b/firmware/application/src/app_main.c @@ -767,6 +767,7 @@ static void ble_passkey_init(void) { */ int main(void) { hw_connect_init(); // Remember to initialize the pins first + cmd_map_init(); // Set function in CMD map for DATA_CMD_GET_DEVICE_CAPABILITIES init_leds(); // LED initialization log_init(); // Log initialization diff --git a/firmware/application/src/data_cmd.h b/firmware/application/src/data_cmd.h index 4be3c5e..c44723d 100644 --- a/firmware/application/src/data_cmd.h +++ b/firmware/application/src/data_cmd.h @@ -38,6 +38,10 @@ #define DATA_CMD_SET_BLE_CONNECT_KEY_CONFIG (1030) #define DATA_CMD_GET_BLE_CONNECT_KEY_CONFIG (1031) #define DATA_CMD_DELETE_ALL_BLE_BONDS (1032) +#define DATA_CMD_GET_DEVICE (1033) +#define DATA_CMD_GET_SETTINGS (1034) +#define DATA_CMD_GET_DEVICE_CAPABILITIES (1035) + // // ****************************************************************** @@ -80,7 +84,7 @@ // #define DATA_CMD_LOAD_MF1_EMU_BLOCK_DATA (4000) #define DATA_CMD_SET_MF1_ANTI_COLLISION_RES (4001) -#define DATA_CMD_SET_MF1_ANTICOLLISION_INFO (4002) +#define DATA_CMD_SET_MF1_ANTI_COLLISION_INFO (4002) #define DATA_CMD_SET_MF1_ATS_RESOURCE (4003) #define DATA_CMD_SET_MF1_DETECTION_ENABLE (4004) #define DATA_CMD_GET_MF1_DETECTION_COUNT (4005) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 28059ba..cbc6792 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -160,29 +160,20 @@ hw_slot_nick = hw_slot.subgroup('nick', 'Get/Set tag nick name for slot') hw_ble = hw.subgroup('ble', 'Bluetooth low energy') hw_ble_bonds = hw_ble.subgroup('bonds', 'All devices bound by chameleons.') hw_settings = hw.subgroup('settings', 'Chameleon settings management') -hw_settings_animation = hw_settings.subgroup( - 'animation', 'Manage wake-up and sleep animation modes') -hw_settings_button_press = hw_settings.subgroup( - 'btnpress', 'Manage button press function') -hw_settings_ble_key = hw_settings.subgroup( - 'blekey', 'Manage ble connect key') +hw_settings_animation = hw_settings.subgroup('animation', 'Manage wake-up and sleep animation modes') +hw_settings_button_press = hw_settings.subgroup('btnpress', 'Manage button press function') +hw_settings_ble_key = hw_settings.subgroup('blekey', 'Manage ble connect key') hf = CLITree('hf', 'high frequency tag/reader') hf_14a = hf.subgroup('14a', 'ISO14443-a tag read/write/info...') hf_mf = hf.subgroup('mf', 'Mifare Classic mini/1/2/4, attack/read/write') -hf_mf_detection = hf.subgroup( - 'detection', 'Mifare Classic detection log') +hf_mf_detection = hf.subgroup('detection', 'Mifare Classic detection log') lf = CLITree('lf', 'low frequency tag/reader') lf_em = lf.subgroup('em', 'EM410x read/write/emulator') -lf_em_sim = lf_em.subgroup( - 'sim', 'Manage EM410x emulation data for selected slot') +lf_em_sim = lf_em.subgroup('sim', 'Manage EM410x emulation data for selected slot') -root_commands: dict[str, CLITree] = { - 'hw': hw, - 'hf': hf, - 'lf': lf, -} +root_commands: dict[str, CLITree] = {'hw': hw, 'hf': hf, 'lf': lf} @hw.command('connect', 'Connect to chameleon by serial port') @@ -201,23 +192,20 @@ class HWConnect(BaseCLIUnit): platform_name = uname().release if 'Microsoft' in platform_name: path = os.environ["PATH"].split(os.pathsep) - path.append( - "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/") + path.append("/mnt/c/Windows/System32/WindowsPowerShell/v1.0/") + powershell_path = None for prefix in path: fn = os.path.join(prefix, "powershell.exe") if not os.path.isdir(fn) and os.access(fn, os.X_OK): - PSHEXE = fn + powershell_path = fn break - if PSHEXE: - # process = subprocess.Popen([PSHEXE,"Get-CimInstance -ClassName Win32_serialport |" - # " Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} |" - # " Select -expandproperty DeviceID"],stdout=subprocess.PIPE); - process = subprocess.Popen([PSHEXE, "Get-PnPDevice -Class Ports -PresentOnly |" - " where {$_.DeviceID -like '*VID_6868&PID_8686*'} |" - " Select-Object -First 1 FriendlyName |" - " % FriendlyName |" - " select-string COM\d+ |" - "% { $_.matches.value }"], stdout=subprocess.PIPE) + if powershell_path: + process = subprocess.Popen([powershell_path, "Get-PnPDevice -Class Ports -PresentOnly |" + " where {$_.DeviceID -like '*VID_6868&PID_8686*'} |" + " Select-Object -First 1 FriendlyName |" + " % FriendlyName |" + " select-string COM\d+ |" + "% { $_.matches.value }"], stdout=subprocess.PIPE) res = process.communicate()[0] _comport = res.decode('utf-8').strip() if _comport: @@ -229,8 +217,7 @@ class HWConnect(BaseCLIUnit): args.port = port.device break if args.port is None: # If no chameleon was found, exit - print( - "Chameleon not found, please connect the device or try connecting manually with the -p flag.") + print("Chameleon not found, please connect the device or try connecting manually with the -p flag.") return self.device_com.open(args.port) print(" { Chameleon connected } ") @@ -262,8 +249,7 @@ class HWModeGet(DeviceRequiredUnit): pass def on_exec(self, args: argparse.Namespace): - print( - f"- Device Mode ( Tag {'Reader' if self.cmd.is_reader_device_mode() else 'Emulator'} )") + print(f"- Device Mode ( Tag {'Reader' if self.cmd.is_reader_device_mode() else 'Emulator'} )") @hw_chipid.command('get', 'Get device chipset ID') @@ -320,7 +306,6 @@ class HF14AScan(ReaderRequiredUnit): @hf_14a.command('info', 'Scan 14a tag, and print detail information') class HF14AInfo(ReaderRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: pass @@ -351,7 +336,6 @@ class HF14AInfo(ReaderRequiredUnit): @hf_mf.command('nested', 'Mifare Classic nested recover key') class HFMFNested(ReaderRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: type_choices = ['A', 'B', 'a', 'b'] parser = ArgumentParserNoExit() @@ -361,8 +345,7 @@ class HFMFNested(ReaderRequiredUnit): help="The block where the key of the card is known") parser.add_argument('--type-known', type=str, required=True, choices=type_choices, help="The key type of the tag") - parser.add_argument('--key-known', type=str, - required=True, metavar="hex", help="tag sector key") + parser.add_argument('--key-known', type=str, required=True, metavar="hex", help="tag sector key") parser.add_argument('--block-target', type=int, metavar="decimal", help="The key of the target block to recover") parser.add_argument('--type-target', type=str, choices=type_choices, @@ -381,13 +364,10 @@ class HFMFNested(ReaderRequiredUnit): :return: """ # acquire - dist_resp = self.cmd.detect_nt_distance( - block_known, type_known, key_known) - nt_resp = self.cmd.acquire_nested( - block_known, type_known, key_known, block_target, type_target) + dist_resp = self.cmd.detect_nt_distance(block_known, type_known, key_known) + nt_resp = self.cmd.acquire_nested(block_known, type_known, key_known, block_target, type_target) # parse - dist_obj = chameleon_cstruct.parse_nt_distance_detect_result( - dist_resp.data) + dist_obj = chameleon_cstruct.parse_nt_distance_detect_result(dist_resp.data) nt_obj = chameleon_cstruct.parse_nested_nt_acquire_group(nt_resp.data) # create cmd cmd_param = f"{dist_obj['uid']} {dist_obj['dist']}" @@ -419,8 +399,7 @@ class HFMFNested(ReaderRequiredUnit): print(f" - [{len(key_list)} candidate keys found ]") for key in key_list: key_bytes = bytearray.fromhex(key) - ret = self.cmd.auth_mf1_key( - block_target, type_target, key_bytes) + ret = self.cmd.auth_mf1_key(block_target, type_target, key_bytes) if ret.status == chameleon_status.Device.HF_TAG_OK: return key else: @@ -444,10 +423,8 @@ class HFMFNested(ReaderRequiredUnit): type_target = args.type_target if block_target is not None and type_target is not None: type_target = 0x60 if type_target == 'A' or type_target == 'a' else 0x61 - print( - f" - {colorama.Fore.CYAN}Nested recover one key running...{colorama.Style.RESET_ALL}") - key = self.recover_a_key( - block_known, type_known, key_known, block_target, type_target) + print(f" - {colorama.Fore.CYAN}Nested recover one key running...{colorama.Style.RESET_ALL}") + key = self.recover_a_key(block_known, type_known, key_known, block_target, type_target) if key is None: print("No keys found, you can retry recover.") else: @@ -456,15 +433,13 @@ class HFMFNested(ReaderRequiredUnit): print("Please input block_target and type_target") self.args_parser().print_help() else: - raise NotImplementedError( - "hf mf nested recover all key not implement.") + raise NotImplementedError("hf mf nested recover all key not implement.") return @hf_mf.command('darkside', 'Mifare Classic darkside recover key') class HFMFDarkside(ReaderRequiredUnit): - def __init__(self): super().__init__() self.darkside_list = [] @@ -482,11 +457,9 @@ class HFMFDarkside(ReaderRequiredUnit): first_recover = True retry_count = 0 while retry_count < 0xFF: - darkside_resp = self.cmd.acquire_darkside( - block_target, type_target, first_recover, 15) - first_recover = False # not first run. - darkside_obj = chameleon_cstruct.parse_darkside_acquire_result( - darkside_resp.data) + darkside_resp = self.cmd.acquire_darkside(block_target, type_target, first_recover, 15) + first_recover = False # not first run. + darkside_obj = chameleon_cstruct.parse_darkside_acquire_result(darkside_resp.data) self.darkside_list.append(darkside_obj) recover_params = f"{darkside_obj['uid']}" for darkside_item in self.darkside_list: @@ -517,8 +490,7 @@ class HFMFDarkside(ReaderRequiredUnit): # auth key for key in key_list: key_bytes = bytearray.fromhex(key) - auth_ret = self.cmd.auth_mf1_key( - block_target, type_target, key_bytes) + auth_ret = self.cmd.auth_mf1_key(block_target, type_target, key_bytes) if auth_ret.status == chameleon_status.Device.HF_TAG_OK: return key return None @@ -533,7 +505,6 @@ class HFMFDarkside(ReaderRequiredUnit): class BaseMF1AuthOpera(ReaderRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: type_choices = ['A', 'B', 'a', 'b'] parser = ArgumentParserNoExit() @@ -541,8 +512,7 @@ class BaseMF1AuthOpera(ReaderRequiredUnit): help="The block where the key of the card is known") parser.add_argument('-t', '--type', type=str, required=True, choices=type_choices, help="The key type of the tag") - parser.add_argument('-k', '--key', type=str, - required=True, metavar="hex", help="tag sector key") + parser.add_argument('-k', '--key', type=str, required=True, metavar="hex", help="tag sector key") return parser def get_param(self, args): @@ -584,11 +554,9 @@ class HFMFWRBL(BaseMF1AuthOpera): if not re.match(r"^[a-fA-F0-9]{32}$", args.data): raise ArgsParserError("Data must include 32 HEX symbols") param.data = bytearray.fromhex(args.data) - resp = self.cmd.write_mf1_block( - param.block, param.type, param.key, param.data) + resp = self.cmd.write_mf1_block(param.block, param.type, param.key, param.data) if resp.status == chameleon_status.Device.HF_TAG_OK: - print( - f" - {colorama.Fore.GREEN}Write done.{colorama.Style.RESET_ALL}") + print(f" - {colorama.Fore.GREEN}Write done.{colorama.Style.RESET_ALL}") else: print(f" - {colorama.Fore.RED}Write fail.{colorama.Style.RESET_ALL}") @@ -597,15 +565,14 @@ class HFMFWRBL(BaseMF1AuthOpera): class HFMFDetectionEnable(DeviceRequiredUnit): def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('-e', '--enable', type=int, required=True, - choices=[1, 0], help="1 = enable, 0 = disable") + parser.add_argument('-e', '--enable', type=int, required=True, choices=[1, 0], help="1 = enable, 0 = disable") return parser # hf mf detection enable -e 1 def on_exec(self, args: argparse.Namespace): enable = True if args.enable == 1 else False self.cmd.set_mf1_detection_enable(enable) - print(f" - Set mf1 detection { 'enable' if enable else 'disable'}.") + print(f" - Set mf1 detection {'enable' if enable else 'disable'}.") @hf_mf_detection.command('count', 'Detection log count') @@ -653,8 +620,7 @@ class HFMFDetectionDecrypt(DeviceRequiredUnit): # get output output_str = process.get_output_sync() # print(output_str) - sea_obj = re.search( - r"([a-fA-F0-9]{12})", output_str, flags=re.MULTILINE) + sea_obj = re.search(r"([a-fA-F0-9]{12})", output_str, flags=re.MULTILINE) if sea_obj is not None: keys.append(sea_obj[1]) @@ -664,22 +630,19 @@ class HFMFDetectionDecrypt(DeviceRequiredUnit): def on_exec(self, args: argparse.Namespace): buffer = bytearray() index = 0 - count = int.from_bytes( - self.cmd.get_mf1_detection_count().data, "little", signed=False) + count = int.from_bytes(self.cmd.get_mf1_detection_count().data, "little", signed=False) if count == 0: print(" - No detection log to download") return print(f" - MF1 detection log count = {count}, start download", end="") while index < count: tmp = self.cmd.get_mf1_detection_log(index).data - recv_count = int( - len(tmp) / HFMFDetectionDecrypt.detection_log_size) + recv_count = int(len(tmp) / HFMFDetectionDecrypt.detection_log_size) index += recv_count buffer.extend(tmp) print(".", end="") print() - print( - f" - Download done ({len(buffer)}bytes), start parse and decrypt") + print(f" - Download done ({len(buffer)}bytes), start parse and decrypt") result_maps = chameleon_cstruct.parse_mf1_detection_result(buffer) for uid in result_maps.keys(): @@ -691,22 +654,18 @@ class HFMFDetectionDecrypt(DeviceRequiredUnit): # print(f" - A record: { result_maps[block]['A'] }") records = result_maps_for_uid[block]['A'] if len(records) > 1: - result_maps[uid][block]['A'] = self.decrypt_by_list( - records) + result_maps[uid][block]['A'] = self.decrypt_by_list(records) if 'B' in result_maps_for_uid[block]: # print(f" - B record: { result_maps[block]['B'] }") records = result_maps_for_uid[block]['B'] if len(records) > 1: - result_maps[uid][block]['B'] = self.decrypt_by_list( - records) + result_maps[uid][block]['B'] = self.decrypt_by_list(records) print(" > Result ---------------------------") for block in result_maps_for_uid.keys(): if 'A' in result_maps_for_uid[block]: - print( - f" > Block {block}, A key result: {result_maps_for_uid[block]['A']}") + print(f" > Block {block}, A key result: {result_maps_for_uid[block]['A']}") if 'B' in result_maps_for_uid[block]: - print( - f" > Block {block}, B key result: {result_maps_for_uid[block]['B']}") + print(f" > Block {block}, B key result: {result_maps_for_uid[block]['B']}") return @@ -714,10 +673,8 @@ class HFMFDetectionDecrypt(DeviceRequiredUnit): class HFMFELoad(DeviceRequiredUnit): def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('-f', '--file', type=str, - required=True, help="file path") - parser.add_argument('-t', '--type', type=str, required=False, - help="content type", choices=['bin', 'hex']) + parser.add_argument('-f', '--file', type=str, required=True, help="file path") + parser.add_argument('-t', '--type', type=str, required=False, help="content type", choices=['bin', 'hex']) return parser # hf mf eload -f test.bin -t bin @@ -730,8 +687,7 @@ class HFMFELoad(DeviceRequiredUnit): elif file.endswith('.eml'): content_type = 'hex' else: - raise Exception( - "Unknown file format, Specify content type with -t option") + raise Exception("Unknown file format, Specify content type with -t option") else: content_type = args.type buffer = bytearray() @@ -764,10 +720,8 @@ class HFMFELoad(DeviceRequiredUnit): class HFMFERead(DeviceRequiredUnit): def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('-f', '--file', type=str, - required=True, help="file path") - parser.add_argument('-t', '--type', type=str, required=False, - help="content type", choices=['bin', 'hex']) + parser.add_argument('-f', '--file', type=str, required=True, help="file path") + parser.add_argument('-t', '--type', type=str, required=False, help="content type", choices=['bin', 'hex']) return parser def on_exec(self, args: argparse.Namespace): @@ -778,8 +732,7 @@ class HFMFERead(DeviceRequiredUnit): elif file.endswith('.eml'): content_type = 'hex' else: - raise Exception( - "Unknown file format, Specify content type with -t option") + raise Exception("Unknown file format, Specify content type with -t option") else: content_type = args.type @@ -795,8 +748,7 @@ class HFMFERead(DeviceRequiredUnit): elif tag_type == chameleon_cmd.TagSpecificType.TAG_TYPE_MIFARE_4096: block_count = 256 else: - raise Exception( - "Card in current slot is not Mifare Classic/Plus in SL1 mode") + raise Exception("Card in current slot is not Mifare Classic/Plus in SL1 mode") with open(file, 'wb') as fd: block = 0 @@ -831,29 +783,24 @@ class HFMFSettings(DeviceRequiredUnit): parser.add_argument('--coll', type=int, required=False, help="Use anti-collision data from block 0 for 4 byte UID tags, 1 - enable, 0 - disable", default=-1, choices=[1, 0]) - parser.add_argument('--write', type=int, required=False, - help=f"Write mode: {help_str}", - default=-1, choices=chameleon_cmd.MifareClassicWriteMode.list()) + parser.add_argument('--write', type=int, required=False, help=f"Write mode: {help_str}", default=-1, + choices=chameleon_cmd.MifareClassicWriteMode.list()) return parser # hf mf settings def on_exec(self, args: argparse.Namespace): if args.gen1a != -1: self.cmd.set_mf1_gen1a_mode(args.gen1a) - print( - f' - Set gen1a mode to {"enabled" if args.gen1a else "disabled"} success') + print(f' - Set gen1a mode to {"enabled" if args.gen1a else "disabled"} success') if args.gen2 != -1: self.cmd.set_mf1_gen2_mode(args.gen2) - print( - f' - Set gen2 mode to {"enabled" if args.gen2 else "disabled"} success') + print(f' - Set gen2 mode to {"enabled" if args.gen2 else "disabled"} success') if args.coll != -1: self.cmd.set_mf1_block_anti_coll_mode(args.coll) - print( - f' - Set anti-collision mode to {"enabled" if args.coll else "disabled"} success') + print(f' - Set anti-collision mode to {"enabled" if args.coll else "disabled"} success') if args.write != -1: self.cmd.set_mf1_write_mode(args.write) - print( - f' - Set write mode to {chameleon_cmd.MifareClassicWriteMode(args.write)} success') + print(f' - Set write mode to {chameleon_cmd.MifareClassicWriteMode(args.write)} success') print(' - Emulator settings updated') @@ -861,12 +808,9 @@ class HFMFSettings(DeviceRequiredUnit): class HFMFSim(DeviceRequiredUnit): def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('--sak', type=str, required=True, - help="Select AcKnowledge(hex)", metavar="hex") - parser.add_argument('--atqa', type=str, required=True, - help="Answer To Request(hex)", metavar="hex") - parser.add_argument('--uid', type=str, required=True, - help="Unique ID(hex)", metavar="hex") + parser.add_argument('--sak', type=str, required=True, help="Select AcKnowledge(hex)", metavar="hex") + parser.add_argument('--atqa', type=str, required=True, help="Answer To Request(hex)", metavar="hex") + parser.add_argument('--uid', type=str, required=True, help="Unique ID(hex)", metavar="hex") return parser # hf mf sim --sak 08 --atqa 0400 --uid DEADBEEF @@ -899,23 +843,19 @@ class HFMFSim(DeviceRequiredUnit): @lf_em.command('read', 'Scan em410x tag and print id') class LFEMRead(ReaderRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: return None def on_exec(self, args: argparse.Namespace): resp = self.cmd.read_em_410x() id_hex = resp.data.hex() - print( - f" - EM410x ID(10H): {colorama.Fore.GREEN}{id_hex}{colorama.Style.RESET_ALL}") + print(f" - EM410x ID(10H): {colorama.Fore.GREEN}{id_hex}{colorama.Style.RESET_ALL}") class LFEMCardRequiredUnit(DeviceRequiredUnit): - @staticmethod def add_card_arg(parser: ArgumentParserNoExit): - parser.add_argument("--id", type=str, required=True, - help="EM410x tag id", metavar="hex") + parser.add_argument("--id", type=str, required=True, help="EM410x tag id", metavar="hex") return parser def before_exec(self, args: argparse.Namespace): @@ -934,7 +874,6 @@ class LFEMCardRequiredUnit(DeviceRequiredUnit): @lf_em.command('write', 'Write em410x id to t55xx') class LFEMWriteT55xx(LFEMCardRequiredUnit, ReaderRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() return self.add_card_arg(parser) @@ -953,7 +892,6 @@ class LFEMWriteT55xx(LFEMCardRequiredUnit, ReaderRequiredUnit): class SlotIndexRequireUnit(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: raise NotImplementedError() @@ -998,7 +936,7 @@ class HWSlotList(DeviceRequiredUnit): parser = ArgumentParserNoExit() parser.add_argument('-e', '--extend', type=int, required=False, help="Show slot nicknames and Mifare Classic emulator settings. 0 - skip, 1 - show (" - "default)", choices=[0, 1], default=1) + "default, 2 - show emulator settings for each slot)", choices=[0, 1, 2], default=1) return parser def get_slot_name(self, slot, sense): @@ -1012,39 +950,29 @@ class HWSlotList(DeviceRequiredUnit): # hw slot list def on_exec(self, args: argparse.Namespace): data = self.cmd.get_slot_info().data - selected = chameleon_cmd.SlotNumber.from_fw( - self.cmd.get_active_slot().data[0]) + selected = chameleon_cmd.SlotNumber.from_fw(self.cmd.get_active_slot().data[0]) enabled = self.cmd.get_enabled_slots().data for slot in chameleon_cmd.SlotNumber: - print( - f' - Slot {slot} data{" (active)" if slot == selected else ""}' - f'{" (disabled)" if not enabled[chameleon_cmd.SlotNumber.to_fw(slot)] else ""}:') - print( - f' HF: ' - f'{(self.get_slot_name(slot, chameleon_cmd.TagSenseType.TAG_SENSE_HF) + " - ") if args.extend else ""}' - f'{chameleon_cmd.TagSpecificType(data[chameleon_cmd.SlotNumber.to_fw(slot) * 2])}') - print( - f' LF: ' - f'{(self.get_slot_name(slot, chameleon_cmd.TagSenseType.TAG_SENSE_LF) + " - ") if args.extend else ""}' - f'{chameleon_cmd.TagSpecificType(data[chameleon_cmd.SlotNumber.to_fw(slot) * 2 + 1])}') - if args.extend: - config = self.cmd.get_mf1_emulator_settings().data - print(' - Mifare Classic emulator settings:') - print( - f' Detection (mfkey32) mode: {"enabled" if config[0] else "disabled"}') - print( - f' Gen1A magic mode: {"enabled" if config[1] else "disabled"}') - print( - f' Gen2 magic mode: {"enabled" if config[2] else "disabled"}') - print( - f' Use anti-collision data from block 0: {"enabled" if config[3] else "disabled"}') - print( - f' Write mode: {chameleon_cmd.MifareClassicWriteMode(config[4])}') + print(f' - Slot {slot} data{" (active)" if slot == selected else ""}' + f'{" (disabled)" if not enabled[chameleon_cmd.SlotNumber.to_fw(slot)] else ""}:') + print(f' HF: ' + f'{(self.get_slot_name(slot, chameleon_cmd.TagSenseType.TAG_SENSE_HF) + " - ") if args.extend else ""}' + f'{chameleon_cmd.TagSpecificType(data[chameleon_cmd.SlotNumber.to_fw(slot) * 2])}') + print(f' LF: ' + f'{(self.get_slot_name(slot, chameleon_cmd.TagSenseType.TAG_SENSE_LF) + " - ") if args.extend else ""}' + f'{chameleon_cmd.TagSpecificType(data[chameleon_cmd.SlotNumber.to_fw(slot) * 2 + 1])}') + if args.extend == 2 or args.extend == 1 and enabled[chameleon_cmd.SlotNumber.to_fw(slot)]: + config = self.cmd.get_mf1_emulator_settings().data + print(' - Mifare Classic emulator settings:') + print(f' Detection (mfkey32) mode: {"enabled" if config[0] else "disabled"}') + print(f' Gen1A magic mode: {"enabled" if config[1] else "disabled"}') + print(f' Gen2 magic mode: {"enabled" if config[2] else "disabled"}') + print(f' Use anti-collision data from block 0: {"enabled" if config[3] else "disabled"}') + print(f' Write mode: {chameleon_cmd.MifareClassicWriteMode(config[4])}') @hw_slot.command('change', 'Set emulation tag slot activated.') class HWSlotSet(SlotIndexRequireUnit): - def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() return self.add_slot_args(parser) @@ -1057,7 +985,6 @@ class HWSlotSet(SlotIndexRequireUnit): class TagTypeRequiredUnit(DeviceRequiredUnit): - @staticmethod def add_type_args(parser: ArgumentParserNoExit): type_choices = chameleon_cmd.TagSpecificType.list() @@ -1080,7 +1007,6 @@ class TagTypeRequiredUnit(DeviceRequiredUnit): @hw_slot.command('type', 'Set emulation tag type') class HWSlotTagType(TagTypeRequiredUnit, SlotIndexRequireUnit): - def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() self.add_type_args(parser) @@ -1112,7 +1038,6 @@ class HWDeleteSlotSense(SlotIndexRequireUnit, SenseTypeRequireUnit): @hw_slot.command('init', 'Set emulation tag data to default') class HWSlotDataDefault(TagTypeRequiredUnit, SlotIndexRequireUnit): - def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() self.add_type_args(parser) @@ -1133,8 +1058,7 @@ class HWSlotEnableSet(SlotIndexRequireUnit): def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() self.add_slot_args(parser) - parser.add_argument('-e', '--enable', type=int, required=True, - help="1 is Enable or 0 Disable", choices=[0, 1]) + parser.add_argument('-e', '--enable', type=int, required=True, help="1 is Enable or 0 Disable", choices=[0, 1]) return parser # hw slot enable -s 1 -e 0 @@ -1142,13 +1066,11 @@ class HWSlotEnableSet(SlotIndexRequireUnit): slot_num = args.slot enable = args.enable self.cmd.set_slot_enable(slot_num, enable) - print( - f' - Set slot {slot_num} {"enable" if enable else "disable"} success.') + print(f' - Set slot {slot_num} {"enable" if enable else "disable"} success.') @lf_em_sim.command('set', 'Set simulated em410x card id') class LFEMSimSet(LFEMCardRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() return self.add_card_arg(parser) @@ -1163,7 +1085,6 @@ class LFEMSimSet(LFEMCardRequiredUnit): @lf_em_sim.command('get', 'Get simulated em410x card id') class LFEMSimGet(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit or None: return None @@ -1180,8 +1101,7 @@ class HWSlotNickSet(SlotIndexRequireUnit, SenseTypeRequireUnit): parser = ArgumentParserNoExit() self.add_slot_args(parser) self.add_sense_type_args(parser) - parser.add_argument('-n', '--name', type=str, - required=True, help="Your tag nick name for slot") + parser.add_argument('-n', '--name', type=str, required=True, help="Your tag nick name for slot") return parser # hw slot nick set -s 1 -st 1 -n Save the test name @@ -1189,10 +1109,10 @@ class HWSlotNickSet(SlotIndexRequireUnit, SenseTypeRequireUnit): slot_num = args.slot sense_type = args.sense_type name: str = args.name - uname = name.encode(encoding="utf8") - if len(uname) > 32: + encoded_name = name.encode(encoding="utf8") + if len(encoded_name) > 32: raise ValueError("Your tag nick name too long.") - self.cmd.set_slot_tag_nick_name(slot_num, sense_type, uname) + self.cmd.set_slot_tag_nick_name(slot_num, sense_type, encoded_name) print(f' - Set tag nick name for slot {slot_num} success.') @@ -1300,8 +1220,7 @@ class HWSettingsAnimationSet(DeviceRequiredUnit): def on_exec(self, args: argparse.Namespace): mode = args.mode self.cmd.set_settings_animation(mode) - print( - "Animation mode change success. Do not forget to store your settings in flash!") + print("Animation mode change success. Do not forget to store your settings in flash!") @hw_settings.command('store', 'Store current settings to flash') @@ -1334,13 +1253,12 @@ class HWSettingsReset(DeviceRequiredUnit): @hw.command('factory_reset', 'Wipe all data and return to factory settings') class HWFactoryReset(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() parser.description = "Permanently wipes Chameleon to factory settings. " \ "This will delete all your slot data and custom settings. " \ "There's no going back." - parser.add_argument("--i-know-what-im-doing", default=False, - action="store_true", help="Just to be sure :)") + parser.add_argument("--i-know-what-im-doing", default=False, action="store_true", help="Just to be sure :)") return parser def on_exec(self, args: argparse.Namespace): @@ -1361,7 +1279,7 @@ class HWBatteryInfo(DeviceRequiredUnit): # How much remaining battery is considered low? BATTERY_LOW_LEVEL = 30 - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: return None def on_exec(self, args: argparse.Namespace): @@ -1372,30 +1290,24 @@ class HWBatteryInfo(DeviceRequiredUnit): print(f" voltage -> {voltage}mV") print(f" percentage -> {percentage}%") if percentage < HWBatteryInfo.BATTERY_LOW_LEVEL: - print( - f"{colorama.Fore.RED}[!] Low battery, please charge.{colorama.Style.RESET_ALL}") + print(f"{colorama.Fore.RED}[!] Low battery, please charge.{colorama.Style.RESET_ALL}") @hw_settings_button_press.command('get', 'Get button press function of Button A and Button B.') class HWButtonSettingsGet(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: return None def on_exec(self, args: argparse.Namespace): # all button in here. - button_list = [ - chameleon_cmd.ButtonType.ButtonA, - chameleon_cmd.ButtonType.ButtonB, - ] + button_list = [chameleon_cmd.ButtonType.ButtonA, chameleon_cmd.ButtonType.ButtonB, ] print("") for button in button_list: resp = self.cmd.get_button_press_fun(button) resp_long = self.cmd.get_long_button_press_fun(button) - button_fn = chameleon_cmd.ButtonPressFunction.from_int( - resp.data[0]) - button_long_fn = chameleon_cmd.ButtonPressFunction.from_int( - resp_long.data[0]) + button_fn = chameleon_cmd.ButtonPressFunction.from_int(resp.data[0]) + button_long_fn = chameleon_cmd.ButtonPressFunction.from_int(resp_long.data[0]) print(f" - {colorama.Fore.GREEN}{button} {colorama.Fore.YELLOW}short{colorama.Style.RESET_ALL}:" f" {button_fn}") print(f" usage: {button_fn.usage()}") @@ -1409,19 +1321,17 @@ class HWButtonSettingsGet(DeviceRequiredUnit): @hw_settings_button_press.command('set', 'Set button press function of Button A and Button B.') class HWButtonSettingsSet(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('-l', '--long', action='store_true', default=False, - help="set keybinding for long-press") - parser.add_argument('-b', type=str, required=True, - help="Change the function of the pressed button(?).", + parser.add_argument('-l', '--long', action='store_true', default=False, help="set keybinding for long-press") + parser.add_argument('-b', type=str, required=True, help="Change the function of the pressed button(?).", choices=chameleon_cmd.ButtonType.list_str()) function_usage = "" for fun in chameleon_cmd.ButtonPressFunction: function_usage += f"{int(fun)} = {fun.usage()}, " function_usage = function_usage.rstrip(' ').rstrip(',') - parser.add_argument('-f', type=int, required=True, - help=function_usage, choices=chameleon_cmd.ButtonPressFunction.list()) + parser.add_argument('-f', type=int, required=True, help=function_usage, + choices=chameleon_cmd.ButtonPressFunction.list()) return parser def on_exec(self, args: argparse.Namespace): @@ -1437,10 +1347,9 @@ class HWButtonSettingsSet(DeviceRequiredUnit): @hw_settings_ble_key.command('set', 'Set the ble connect key') class HWSettingsBLEKeySet(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: parser = ArgumentParserNoExit() - parser.add_argument('-k', '--key', required=True, - help="Ble connect key for your device") + parser.add_argument('-k', '--key', required=True, help="Ble connect key for your device") return parser def on_exec(self, args: argparse.Namespace): @@ -1457,7 +1366,7 @@ class HWSettingsBLEKeySet(DeviceRequiredUnit): @hw_settings_ble_key.command('get', 'Get the ble connect key') class HWSettingsBLEKeyGet(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: return None def on_exec(self, args: argparse.Namespace): @@ -1469,9 +1378,26 @@ class HWSettingsBLEKeyGet(DeviceRequiredUnit): @hw_ble_bonds.command('clear', 'Clear all bindings') class HWBLEBondsClear(DeviceRequiredUnit): - def args_parser(self) -> ArgumentParserNoExit: + def args_parser(self) -> ArgumentParserNoExit or None: return None def on_exec(self, args: argparse.Namespace): self.cmd.delete_ble_all_bonds() print(" - Successfully clear all bonds") + + +@hw.command('raw', 'Send raw command') +class HWRaw(DeviceRequiredUnit): + + def args_parser(self) -> ArgumentParserNoExit or None: + parser = ArgumentParserNoExit() + parser.add_argument('-c', '--command', type=int, required=True, help="Command (Int) to send") + parser.add_argument('-d', '--data', type=str, help="Data (HEX) to send", default="") + return parser + + def on_exec(self, args: argparse.Namespace): + response = self.cmd.device.send_cmd_sync(args.command, data=bytes.fromhex(args.data), status=0x0) + print(" - Received:") + print(f" Command: {response.cmd}") + print(f" Status: {response.status}") + print(f" Data (HEX): {response.data.hex()}") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 9bb4b13..fad9580 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -50,6 +50,10 @@ DATA_CMD_GET_BLE_CONNECT_KEY_CONFIG = 1031 DATA_CMD_DELETE_ALL_BLE_BONDS = 1032 +DATA_CMD_GET_DEVICE = 1033 +DATA_CMD_GET_SETTINGS = 1034 +DATA_CMD_GET_DEVICE_CAPABILITIES = 1035 + DATA_CMD_SCAN_14A_TAG = 2000 DATA_CMD_MF1_SUPPORT_DETECT = 2001 DATA_CMD_MF1_NT_LEVEL_DETECT = 2002 @@ -283,6 +287,8 @@ class ChameleonCMD: :param chameleon: chameleon instance, @see chameleon_device.Chameleon """ self.device = chameleon + if not len(self.device.commands): + self.get_device_capabilities() def get_firmware_version(self) -> int: """ @@ -810,7 +816,7 @@ class ChameleonCMD: data_bytes = key.encode(encoding='ascii') # check key length - if (len(data_bytes) != 6): + if len(data_bytes) != 6: raise ValueError("The ble connect key length must be 6") return self.device.send_cmd_sync( @@ -831,6 +837,27 @@ class ChameleonCMD: """ return self.device.send_cmd_sync(DATA_CMD_DELETE_ALL_BLE_BONDS, 0x00, None) + def get_device_capabilities(self): + """ + Get (and set) commands that client understands + """ + + commands = [] + + try: + ret = self.device.send_cmd_sync(DATA_CMD_GET_DEVICE_CAPABILITIES, 0x00) + + for i in range(0, len(ret.data), 2): + if i + 1 < len(ret.data): + commands.append((ret.data[i + 1] << 8) | ret.data[i]) + + self.device.commands = commands + except: + print("Chameleon doesn't understand get capabilities command. Please update firmware") + + return commands + + if __name__ == '__main__': # connect to chameleon dev = chameleon_com.ChameleonCom() diff --git a/software/script/chameleon_com.py b/software/script/chameleon_com.py index f3a62dbf..c309fb1 100644 --- a/software/script/chameleon_com.py +++ b/software/script/chameleon_com.py @@ -42,6 +42,7 @@ class ChameleonCom: """ data_frame_sof = 0x11 data_max_length = 512 + commands = [] def __init__(self): """ @@ -70,8 +71,7 @@ class ChameleonCom: error = None try: # open serial port - self.serial_instance = serial.Serial( - port=port, baudrate=115200) + self.serial_instance = serial.Serial(port=port, baudrate=115200) except Exception as e: error = e finally: @@ -99,8 +99,7 @@ class ChameleonCom: :return: """ if not self.isOpen(): - raise NotOpenException( - "Please call open() function to start device.") + raise NotOpenException("Please call open() function to start device.") @staticmethod def lrc_calc(array): @@ -198,8 +197,7 @@ class ChameleonCom: if callable(fn_call): # delete wait task from map del self.wait_response_map[data_cmd] - fn_call(data_cmd, data_status, - data_response) + fn_call(data_cmd, data_status, data_response) else: self.wait_response_map[data_cmd]['response'] = Response(data_cmd, data_status, data_response) @@ -230,8 +228,7 @@ class ChameleonCom: task_close = task['close'] # register to wait map if 'callback' in task and callable(task['callback']): - self.wait_response_map[task_cmd] = { - 'callback': task['callback']} # The callback for this task + self.wait_response_map[task_cmd] = {'callback': task['callback']} # The callback for this task else: self.wait_response_map[task_cmd] = {'response': None} # set start time @@ -262,8 +259,7 @@ class ChameleonCom: if time.time() > self.wait_response_map[task_cmd]['end_time']: if 'callback' in self.wait_response_map[task_cmd]: # not sync, call function to notify timeout. - self.wait_response_map[task_cmd]['callback']( - task_cmd, None, None) + self.wait_response_map[task_cmd]['callback'](task_cmd, None, None) else: # sync mode, set timeout flag self.wait_response_map[task_cmd]['is_timeout'] = True @@ -308,8 +304,7 @@ class ChameleonCom: del self.wait_response_map[cmd] # make data frame data_frame = self.make_data_frame_bytes(cmd, status, data) - task = {'cmd': cmd, 'frame': data_frame, - 'timeout': timeout, 'close': close} + task = {'cmd': cmd, 'frame': data_frame, 'timeout': timeout, 'close': close} if callable(callback): task['callback'] = callback self.send_data_queue.put(task) @@ -327,6 +322,12 @@ class ChameleonCom: """ if isinstance(data, int): data = [data] # warp array. + if len(self.commands): + # check if chameleon can understand this command + if cmd not in self.commands: + raise CMDInvalidException(f"This device doesn't declare that it can support this command: {cmd}.\nMake " + f"sure firmware is up to date and matches client") + # return Response(cmd=cmd, status=0, data=b"\0" * 32) # forge fake response to not break app # first to send cmd, no callback mode(sync) self.send_cmd_auto(cmd, status, data, None, timeout) # wait cmd start process