Commit Graph

24 Commits

Author SHA1 Message Date
Jeremy Brown 35d2f40ff5 Switched to function for color stings 2025-09-11 11:52:05 -04:00
unkernet 89e098c773 Make CLI compatible with Python 3.9
- Replace Python 3.10+ type hint syntax (`int | None`) with  `Union[int, None]`.
- Add explicit `byteorder='big'` argument to all `int.from_bytes()` calls.
2025-08-10 14:44:40 +07:00
Foxushka cf00761902 Key recovery via backdoor for static encrypted nonce cards (#263)
* Implement MF1_ENC_NESTED_ACQUIRE in firmware

* Implement MF1_ENC_NESTED_ACQUIRE in software + fix code formatter

* Remove xz and pthreads from sources, use CMake FetchContent

* Update changelog

* lzma.h

* Update CMakeLists.txt

* Update CMakeLists.txt

* Probably fix workflow taking wrong commit for building

* Fix CMake building tools into bin/Debug on Windows

* Added cmd for fetching all slots nicks (without 16 commands)

* Fix type and use temp directory instead cwd (https://github.com/RfidResearchGroup/ChameleonUltra/pull/261)

* Fix endian for mfu_read_emu_counter_data and mfu_write_emu_counter_data

* Fix  --key interpreted as list
2025-08-01 22:12:07 +02:00
simonemarfo 0bc01f565a Add CLI view commands to display memory content (#208)
* Add utility to print memory dump (xxd like)

* Add CLI eview. Dump emulation memory data

* Add CLI view. Display content from tag memory or dump file

---------

Co-authored-by: marfo <marfo@localhost.localdomain>
2024-04-24 10:51:11 +08:00
Philippe Teuwen af91ddcda9 CLI: Do not execute a command if help is printed
Previously, for example `hf 14a raw -b 7 -d 26 -h` was printing help but it was also executing the command.
2023-11-15 09:44:37 +01:00
Philippe Teuwen 4d670f051a Be robust to some argparse string variants 2023-11-06 22:45:48 +01:00
Philippe Teuwen ece5e79060 typechecking fixes 2023-10-24 23:44:56 +02:00
Philippe Teuwen fa0754aed2 cli: make status an enum. rename some status for consistency 2023-10-17 23:43:25 +02:00
Philippe Teuwen 4fe9e1ea5b docs 2023-10-11 10:28:45 +02:00
Philippe Teuwen 61cc4f5891 cli: color help and add epilog example 2023-10-10 01:58:26 +02:00
Philippe Teuwen 4927e0166c cli: one root CLITree, enhance dump_help 2023-10-10 00:17:17 +02:00
Philippe Teuwen 707b0c6d4a Changed CLI help:
- visually closer to pm3 (differenciate groups and commands)
- "dumphelp" allows to quickly dump all cmds and their options
- now all commands support '-h', so e.g. `hw settings store -h` will print help instead of storing
- no more command description in decorator, it is now taken from argparse description
2023-10-05 00:48:50 +02:00
Philippe Teuwen 14bffafb2a style 2023-09-20 01:26:47 +02:00
Philippe Teuwen 8499535aad Clarify protocol. Disruptive changes: see below
This huge commit tries to enhance several things related to the fw/cli protocol.
Generally, the idea is to be verbose, explicit and reuse conventions, in order to enhance code maintainability and understandability for the other contributors.

docs/protocol.md got heavily updated

Many commands have been renamed for consistency. you are invited to adapt your client for easier maintenance

Guidelines, also written in docs/protocol.md "New data payloads: guidelines for developers":
- Now protocol data exchanged over USB or BLE are defined in netdata.h as packed structs and values are stored in Network byte order (=Big Endian)
- Command-specific payloads are defined in their respective cmd_processor handler in app_cmd.c and chameleon_cmd.py
- Define C `struct` for cmd/resp data greater than a single byte, use and abuse of `struct.pack`/`struct.unpack` in Python. So one can understand the payload format at a simple glimpse.
- If single byte of data to return, still use a 1-byte `data`, not `status`.
- Use unambiguous types such as `uint16_t`, not `int` or `enum`. Cast explicitly `int` and `enum` to `uint_t` of proper size
- Use Network byte order for 16b and 32b integers
  - Macros `U16NTOHS`, `U32NTOHL` must be used on reception of a command payload.
  - Macros `U16HTONS`, `U32HTONL` must be used on creation of a response payload.
  - In Python, use the modifier `!` with all `struct.pack`/`struct.unpack`
- Concentrate payload parsing in the handlers, avoid further parsing in their callers. This is true for the firmware and the client.
- In cmd_processor handlers: don't reuse input `length`/`data` parameters for creating the response content
- Avoid hardcoding offsets, use `sizeof()`, `offsetof(struct, field)` in C and `struct.calcsize()` in Python
- Use the exact same command and fields names in firmware and in client, use function names matching the command names for their handlers unless there is a very good reason not to do so. This helps grepping around. Names must start with a letter, not a number, because some languages require it (e.g. `14a_scan` not possible in Python)
- Respect commands order in `m_data_cmd_map`, `data_cmd.h` and `chameleon_cmd.py` definitions
- Even if a command is not yet implemented in firmware or in client but a command number is allocated, add it to `data_cmd.h` and `chameleon_cmd.py` with some `FIXME: to be implemented` comment
- Validate data before using it, both when receiving command data in the firmware and when receiving response data in the client.
- Validate response status in client.

Disruptive changes:
- GET_DEVICE_CAPABILITIES: list of cmds in data are now really Big Endian
  Note: the initial attempt to use macros PP_HTONS were actually considering wrongly that the platform was Big Endian (BYTE_ORDER was actually undefined) while it is actually Little Endian.
- GET_APP_VERSION: response is now a tuple of bytes: major|minor (previously it was in reversed order as a single uint16_t in Little Endian)
- SET_SLOT_TAG_TYPE: tag_type now on 2 bytes, to prepare remapping of its enum
- SET_SLOT_DATA_DEFAULT: tag_type now on 2 bytes, to prepare remapping of its enum
- GET_SLOT_INFO: tag_type now on 2 bytes, to prepare remapping of its enum
- GET_DEVICE_CHIP_ID: now returns its 64b ID following Network byte order (previously, bytes were in the reverse order)
- GET_DEVICE_ADDRESS: now returns its 56b address following Network byte order (previously, bytes were in the reverse order). CLI does not reverse the response anymore so it displays the same value as before.
- MF1_GET_DETECTION_COUNT: now returns its 32b value following Network byte order (previously Little Endian)
- GET_GIT_VERSION response status is now STATUS_DEVICE_SUCCESS
- GET_DEVICE_MODEL response status is now STATUS_DEVICE_SUCCESS
- MF1_READ_EMU_BLOCK_DATA response status is now STATUS_DEVICE_SUCCESS
- GET_DEVICE_CAPABILITIES response status is now STATUS_DEVICE_SUCCESS
- HF14A_SCAN: entirely new response format, room for ATS and multiple tags
- MF1_DETECT_SUPPORT response status is now HF_TAG_OK and support is indicated as bool in 1 byte of data
- MF1_DETECT_PRNG response status is now HF_TAG_OK and prng_type is returned in 1 byte of data with a new enum mf1_prng_type_t == MifareClassicPrngType
- MF1_DETECT_DARKSIDE response status is now HF_TAG_OK and darkside_status is returned in 1 byte of data with a new enum mf1_darkside_status_t == MifareClassicDarksideStatus
- MF1_DARKSIDE_ACQUIRE response status is now HF_TAG_OK and darkside_status is returned in 1 byte of data. If OK, followed by 24 bytes as previously
- MF1_GET_ANTI_COLL_DATA: in case slot does not contain anticoll data, instead of STATUS_PAR_ERR, now it returns STATUS_DEVICE_SUCCESS with empty data
- MF1_SET_ANTI_COLL_DATA and MF1_GET_ANTI_COLL_DATA now use the same data format as HF14A_SCAN

For clients to detect Ultra/Lite with older firmwares, one can issue the GET_APP_VERSION and urge the user to flash his device if needed.
On older firmwares, it will return a status=b'\x00' and data=b'\x00\x01' while up-to-date firmwares will return status=STATUS_DEVICE_SUCCESS and data greater or equal to b'\x01\x00' (v1.0).

Other changes: cf CHANGELOG, and probably a few small changes I forgot about..

TODO:
- remap `tag_specific_type_t` enum to allow future tags (e.g. LF tags) without reshuffling enum and affecting users stored cards
- TEST!
2023-09-18 00:53:39 +02:00
Szymon Borecki ea25fd14c1 Merge branch 'main' into fix-argcomplete 2023-08-26 19:26:20 +02:00
Szymon Borecki 5122246072 Fix argument autocompleter bug (#97) 2023-08-26 19:24:41 +02:00
Philippe Teuwen 04741bdc2a autopep8 2023-08-26 12:06:15 +02:00
Philippe Teuwen d5d955d2e9 pylance 2023-08-26 02:55:46 +02:00
Philippe Teuwen e43e668594 Remove tabs & spaces at end of lines 2023-08-23 00:18:31 +02:00
Szymon Borecki f6e22328bb Add working argument completion 2023-08-19 13:44:15 +02:00
Szymon Borecki 0cfdff809f Add command autocompletion 2023-08-10 01:20:22 +02:00
Szymon Borecki e2b5b9b960 Use prompt-toolkit and switch to a different command registration system 2023-08-10 00:45:37 +02:00
Szymon Borecki e9fec48ede Fix docstring for expect_response and rename the associated exception 2023-08-09 23:51:12 +02:00
Szymon Borecki 3259de2f25 Replace the PositiveChameleonCMD class with an exception decorator 2023-08-09 23:37:52 +02:00