Esempio n. 1
0
def decodePhyPayload(msg, AppSKey, DevAddr):
    # extract the physical payload from the message
    # and convert to hex
    char_seq = "phyPayload"
    idx = msg.payload.rfind(char_seq) + 13
    PHYPayload = msg.payload[idx:-2]
    PHYPayload = base64.decodestring(PHYPayload).encode('hex')

    # extract the MAC payload
    MACPayload = PHYPayload[2:-8]

    # decode the MAC payload
    DevAddr = flipEndianess(MACPayload[0:8])
    FOptsLen = int(MACPayload[9], 16)
    FCnt = int(flipEndianess(MACPayload[10:14]), 16)
    FRMPayload = MACPayload[14 + 2 * FOptsLen + 2:]

    # decrypt the FRM payload
    payload = loramac_decrypt(FRMPayload, FCnt, AppSKey, DevAddr)

    # convert to hex
    payload_int = 0
    byte_num = len(payload) - 1
    for byte in payload:
        payload_int += int(byte) * 2**(8 * byte_num)
        byte_num -= 1
    print format(payload_int, '#04x')
Esempio n. 2
0
def lorawan_points_all(request):
    ''' All LoRaWAN raw points, but try to decrypt them '''

    out = {'points': []}
    for point in LoRaWANRawPoint.objects.all().order_by('-id')[:1000]:

        p = {}

        p['id'] = point.id
        p['FRMPayload'] = point.FRMPayload
        p['FCnt'] = point.FCnt
        p['FPort'] = point.FPort
        p['DevAddr'] = point.DevAddr

        MTypes = (
            'Join Request',
            'Join Accept',
            'Unconfirmed Data Up',
            'Unconfirmed Data Down',
            'Confirmed Data Up',
            'Confirmed Data Down',
            'RFU',
            'Proprietary',
        )

        if point.MType:
            p['MType_description'] = MTypes[point.MType]
        else:
            pass

        try:
            # LoRaWAN spec 1.0; section 4.3.3.1
            if point.FPort == 0:
                SKey = point.node.lorawan_NwkSKey
                p['SKey_used'] = 'NwkSKey'
                p['NwkSKey'] = point.node.lorawan_NwkSKey
            else:
                SKey = point.node.lorawan_AppSKey
                p['SKey_used'] = 'AppSKey'
                p['AppSKey'] = point.node.lorawan_AppSKey
            FRMPayload_decrypted = ""
            FRMPayload_decrypted = loramac_decrypt(point.FRMPayload,
                                                   point.FCnt, SKey,
                                                   point.DevAddr)
            p['FRMPayload_decrypted'] = "".join("{:02x}".format(c)
                                                for c in FRMPayload_decrypted)
        except Exception as e:
            p['error'] = "Unable to decrypt LoRaWAN packet " + str(e)

        out['points'].append(p)

    pretty_json = json.dumps(out, indent=4)
    response = HttpResponse(pretty_json, content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Esempio n. 3
0
    def test_loramac_decrypt(self):
        key = "271E403DF4225EEF7E90836494A5B345"
        dev_addr = "000015E4"

        payloads = ((0, "73100b90"), (1, "68d388f0"), (2, "0a12e808"), (3, "e3413bee"))
        expected = "cafebabe"

        for sequence_counter, payload_hex in payloads:
            plaintext_ints = loramac_decrypt(payload_hex, sequence_counter, key, dev_addr)
            plaintext_hex = "".join("{:02x}".format(x) for x in plaintext_ints)

            self.assertEquals(plaintext_hex, expected)
Esempio n. 4
0
    def test_loramac_decrypt(self):
        key = "271E403DF4225EEF7E90836494A5B345"
        dev_addr = "000015E4"

        payloads = ((0, "73100b90"), (1, "68d388f0"), (2, "0a12e808"),
                    (3, "e3413bee"))
        expected = "cafebabe"

        for sequence_counter, payload_hex in payloads:
            plaintext_ints = loramac_decrypt(payload_hex, sequence_counter,
                                             key, dev_addr)
            plaintext_hex = "".join("{:02x}".format(x) for x in plaintext_ints)

            self.assertEquals(plaintext_hex, expected)
Esempio n. 5
0
    def test_loramac_decrypt(self):
        key = '271E403DF4225EEF7E90836494A5B345'
        dev_addr = '000015E4'

        payloads = (
            (0, '73100b90'),
            (1, '68d388f0'),
            (2, '0a12e808'),
            (3, 'e3413bee'),
        )
        expected = 'cafebabe'

        for sequence_counter, payload_hex in payloads:
            plaintext_ints = loramac_decrypt(payload_hex, sequence_counter, key, dev_addr)
            plaintext_hex = ''.join('{:02x}'.format(x) for x in plaintext_ints)

            self.assertEquals(plaintext_hex, expected)
Esempio n. 6
0
from lora.crypto import loramac_decrypt
import base64

x = (base64.b64decode('QESufgGAAAABizI7/Kupf6U='))
payload = x
key = '283EFC8ED5A378712420CE8568A461D3'
sequence_counter = 2
dev_addr = '017EAE44'
print(loramac_decrypt(payload, sequence_counter, key, dev_addr))

#283EFC8ED5A378712420CE8568A461D3
#593AE59422E01E88ED8FF9584B632C89
#514553756667474141414142697a49372f4b75706636553d
#514553756667474141414142697a49372f4b75706636553d

#QESufgGAAAABizI7/Kupf6U=

#247, 28, 213, 236, 58, 52, 99, 66, 128, 157, 71, 44, 18, 145, 99, 223, 147, 159, 88, 182, 186, 204, 168, 255
Esempio n. 7
0
        pprint(d)

        info['PHYPayload'] = []
        for c in base64.decodestring(d):
            info['PHYPayload'].append(ord(c))

        info['MIC'] = info['PHYPayload'][-4:]
        info['MHDR'] = info['PHYPayload'][0]
        info['MACPayload'] = info['PHYPayload'][1:]
        info['MACPayload'] = info['MACPayload'][:-4]
        info['FPort'] = info['MACPayload'][7]
        info['FRMPayload'] = info['MACPayload'][8:]
        info['FHDR'] = info['MACPayload'][:7]

        info['DevAddr'] = "".join("{:02x}".format(info['FHDR'][c]) for c in range(3,-1,-1))
        info['FCnt'] = struct.unpack("<H", "".join(chr(c) for c in info['FHDR'][5:7]))[0] 
        info['FCtrl'] = info['FHDR'][4]
        info['FCtrl_bin'] = bin(info['FCtrl'])

        payload = ''.join("{:02x}".format(c) for c in info['FRMPayload'])

        if args.verbose:
            print("Will try to decode the FRMPayload payload " + payload) 
        key = '820EB5127B0B98C8CC0B7EE43253E0D1'

        out = loramac_decrypt(payload, info['FCnt'], key, info['DevAddr'])
        info['decrypted_payload'] = out
        if args.verbose:
            pprint(info)
Esempio n. 8
0
 def decrypt(self, Appskey):
     return loramac_decrypt(self.FRMPayload, self.FCnt, Appskey,
                            self.DevAddr, 0)
Esempio n. 9
0
bDevAddr=bytes(_DevAddr)
   
SDevAddr=DevAddr.decode(encoding="utf-8")

FCTR=frame[10:12]

FCNT=frame[14:16]+frame[12:14]

FPORT=frame[16:18]

Payload = frame[18:-8]

MIC= frame[-8:]

data= loramac_decrypt(Payload,int(FCNT,16),key,SDevAddr)

hexData =""
for x in data:
    if(x < 16):
        hexData+= '0'
        hexData+=   hex(x)
    else:
        hexData+=   hex(x)
    
hexData=hexData.replace('0x','')

#hexData=''.join(hex(x) for x in data).replace('0x','').upper()


print("MHDR: ",MHDR,"\nDevAddr: ",SDevAddr,"\nFCTR: ",FCTR,"\nFCNT: ",FCNT,"\nFPORT: ",FPORT,"\nPayload: ",Payload,"\nMIC: ",MIC)