Skip to content

DokPlay/ble-lock-set-pin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“˜ πŸ‡·πŸ‡Ί Russian

βœ… What the script ble_lock_set_pin.py does The script automatically changes the PIN code of the lock using a BLE capture (pcapng) that contains the handshake:

Extracts SKDm, SKDs and IV from capture.pcapng.

Forms a TLV with the new PIN and calculates CRC16.

Encrypts using AES-CTR, like in the Tuya Smart app.

Sends delete and create commands via Tuya OpenAPI.

Verifies successful PIN installation via DP unlock_password.

πŸ“¦ 1. How to prepare the BLE capture Every time you want to change the PIN:

Start Wireshark

Capture for 5–10 seconds after the lock connects to the Tuya Smart app.

-----------Important: you must start capturing before turning on the lock!-------------

Save the file as capture.pcapng and place it next to the script.

--------⚠️ Important: the file must be named exactly capture.pcapng β€” the script expects this name.-------- If you want to use a different name β€” just replace "capture.pcapng" in the script.

πŸ› οΈ 2. What to configure in the script β€” I’ve already done it, just double-check it β€” In ble_lock_set_pin.py:

ACCESS_ID    = "..."      # Your API ID from Tuya  
ACCESS_KEY   = "..."      # Your Secret Key  
DEVICE_ID    = "..."      # The device (lock) ID  
LOCAL_KEY    = b"..."     # 16-byte local_key of the lock  
NEW_PIN      = "124578"   # New PIN code (6 digits)  
PASSWORD_ID  = 1          # Slot ID, usually 1  

Everything else is automated, no need to touch it. All steps are marked in the code as STEP 1, STEP 2, etc. with detailed comments.

πŸ“₯ 3. Install dependencies (if not yet installed) -------------Run once in the console:-------------------

Each command is separate β€” run one by one in the command line. First install the dependencies, then go to the folder with the files.

Python 3.10.11 was used β€” but the script should work from Python 3.7 to 3.12 (not tested, but by code it should).

To check your version:

python --version

If the version is wrong, remove it completely including all folders in your user profile β€” go to your username folder on the disk, find AppData, and delete the Python folder. Then reboot and install the required version.

Dependencies:

pip install tuya-connector-python pycryptodome crcmod # First command, mandatory

pip install pyshark # PyShark / TShark β€” the script uses pyshark.
# You need Wireshark/TShark installed on your PC (runtime is enough).
# If TShark is not in PATH, PyShark will throw an error.
# ----β€œInstall Wireshark β‰₯ 3.4 and make sure TShark is accessible from command line.”----

pip install tuya-iot-py-sdk # Tuya SDK β€” not required for this script, but I had it installed, might be helpful

pip install rich # 🌈 rich β€” pretty log output in the console

pip install ipython # 🧠 ipython β€” interactive Python shell. Very convenient for testing and live script editing.

----------Capture file-----------

Must include both packets: LL_ENC_REQ / LL_ENC_RSP.

Default name: capture.pcapng; if using another name β€” change PCAP_FILE.

πŸš€ 4. How to run

python ble_lock_set_pin.py

(Obviously in the console, run: cd path_to_folder then python ble_lock_set_pin.py)

If everything goes well, you will see:

------------- LOCAL_KEY --------------

Must be 16 bytes, no spaces, in the format b"...".

If you're using another lock β€” you must replace it with its local_key.

------------------------------Limitations--------------------------------

The lock must be online at the time the script runs. πŸŽ‰ PIN applied β€” enter 124578 on the lock!

πŸ“Œ Summary:

What Required
πŸ” New PIN Set NEW_PIN in the script
πŸ“„ New capture capture.pcapng next to script
πŸ“¦ Dependencies See above
πŸ’¬ Language Python 3.10.11

License

This project is licensed under the MIT License. See LICENSE for details.

About

BLE PIN updater for Tuya smart lock using packet capture and API.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages