September 18, 2018

Decoding of LoRaWAN Packets

Decoding of LoRaWAN packets

The above screenshot is the received LoRaWAN packet at the gateway which needs to be decoded to understand if the correct data are being sent to TTN.

From the LoRaWAN packet data (QNIfASaABAABA/uB+N3FPLnND5ufMW6z) which is in base 64 encoded format is decoded using step (1) to obtain FRMpayload.

  1. https://LoRaWAN-packet-decoder-0ta6puiniaut.runkit.sh

This web link helps decoding the base 64 data received at the gateway

Assuming base64-encoded packet

QNIfASaABAABA/uB+N3FPLnND5ufMW6z

Message Type = Data

PHYPayload = 40D21F01268004000103FB81F8DDC53CB9CD0F9B9F316EB3

( PHYPayload = MHDR[1] | MACPayload[..] | MIC[4] )

MHDR = 40

MACPayload = D21F01268004000103FB81F8DDC53CB9CD0F9B

MIC = 9F316EB3

( MACPayload = FHDR | FPort | FRMPayload )

FHDR = D21F0126800400

FPort = 01

FRMPayload = 03FB81F8DDC53CB9CD0F9B

( FHDR = DevAddr[4] | FCtrl[1] | FCnt[2] | FOpts[0..15] )

DevAddr = 26011FD2 (Big Endian)

FCtrl = 80

FCnt = 0004 (Big Endian)

FOpts =

Message Type = Unconfirmed Data Up

Direction = up

FCnt = 4

FCtrl.ACK = false

FCtrl.ADR = true

Download LoRaWAN packet decoder

https://github.com/jieter/python-lora

https://github.com/jieter/python-lora/tree/master/tests

This script has dependency on cryptography.cryptography requires cffi which in turn requires libffi-dev, python-dev and openssl.

$sudo apt-get install libffi-dev

$sudo apt-get install python-dev

$sudo apt-get install python3-dev

$sudo apt-get install openssl

$pip install python-lora

python-lora.png

The FRMpayload is then used in payload_decrypt.py along with sequence counter (fcnt), dev_addr of the node and Appskey to obtain the decimal equivalent of the payload.

$ mkdir lora_decrypt

$ cd lora_decrypt

$ sudo nano payload_decrypt.py

Copy the FRMpayload from (1) to payload, fcnt to sequence_counter, and enter the dev_addr and Appskey used to encrypt the payload.

Press ctrl+x -> y -> ENTER to save the python script.

To run the script type “python payload_decrypt.py” and hit enter.

Upon execution of the payload_decrypt.py a string of decimal values is obtained; this string is then decoded using decimal to hex converter using online tool.

The cryptology library has dependency on setuptools and python version in Linux and Raspbian, which takes a quite a bit effort. However, I migrated to Anaconda IDE in windows to execute the python decrypt script.

Assuming base64-encoded packet

QIsbASaABQABlKmBif/36J2UWcXZ/hdK

Message Type = Data

PHYPayload = 408B1B01268005000194A98189FFF7E89D9459C5D9FE174A

( PHYPayload = MHDR[1] | MACPayload[..] | MIC[4] )

MHDR = 40

MACPayload = 8B1B01268005000194A98189FFF7E89D9459C5

MIC = D9FE174A

( MACPayload = FHDR | FPort | FRMPayload )

FHDR = 8B1B0126800500

FPort = 01

FRMPayload = 94A98189FFF7E89D9459C5

( FHDR = DevAddr[4] | FCtrl[1] | FCnt[2] | FOpts[0..15] )

DevAddr = 26011B8B (Big Endian)

FCtrl = 80

FCnt = 0005 (Big Endian)

FOpts =

Message Type = Unconfirmed Data Up

Direction = up

FCnt = 5

FCtrl.ACK = false

FCtrl.ADR = true

from lora.crypto import loramac_decrypt

payload = ’94A98189FFF7E89D9459C5′

sequence_counter = 5

key = ‘DF2FE61E548EF67A186ECFF15E2A09AB’

dev_addr = ‘26011B8B’

print (loramac_decrypt(payload, sequence_counter, key, dev_addr))

Result obtained by executing the payload_decrypt.py

[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

To convert this base 64 values to ASCII Value, let us uses an online converter

https://www.branah.com/ascii-converter

dec to ascii.png

Leave a Reply

Your email address will not be published. Required fields are marked *