-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathdecrypt_AES.py
238 lines (183 loc) · 5.87 KB
/
decrypt_AES.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#------------------------------------------------------------
# Copyright 2019 Congduc Pham, University of Pau, France.
#
# Congduc.Pham@univ-pau.fr
#
# This file is part of the low-cost LoRa gateway developped at University of Pau
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------
import sys
import re
import string
import base64
import os
import os.path
try:
import LoRaWAN
from LoRaWAN.MHDR import MHDR
except ImportError:
print "LoRaWAN python lib must be installed"
#don't generate pyc (no compilation of imported module) so change in key_* file can be done dynamically
sys.dont_write_bytecode = True
#contains the 2 encryption keys used by LoRaWAN-like AES: AppSKey and NwkSKey
import key_AES as key
def import_LoRaWAN_lib():
try:
import LoRaWAN
from LoRaWAN.MHDR import MHDR
except ImportError:
print "LoRaWAN python lib must be installed"
print "loraWAN.py: checking for LoRaWAN lib"
if os.path.isfile(os.path.expanduser("aes-python-lib/LoRaWAN/MHDR.py")):
cmd_arg="sudo cp -r aes-python-lib/LoRaWAN /usr/lib/python2.7/dist-packages"
print "copying LoRaWAN lib in python distribution"
try:
os.system(cmd_arg)
except:
print "Error when copying LoRaWAN lib"
return False
print "try importing LoRaWAN lib again"
try:
import LoRaWAN
from LoRaWAN.MHDR import MHDR
print "import now ok"
return True
except ImportError:
print "sorry, error."
return False
else:
return False
print "import ok"
return True
PKT_TYPE_DATA=0x10
PKT_FLAG_DATA_ENCRYPTED=0x04
#to display non printable characters
replchars = re.compile(r'[\x00-\x1f]')
def replchars_to_hex(match):
return r'\x{0:02x}'.format(ord(match.group()))
def loraWAN_process_pkt(lorapkt):
#print "entering loraWAN_process_pkt"
if (lorapkt[0] == 0x40) or (lorapkt[0] == 0x80):
src = lorapkt[4]*256*256*256
src += lorapkt[3]*256*256
src += lorapkt[2]*256
src += lorapkt[1]
src_str="%0.8X" % src
if src_str in key.device_key:
print "found device 0x%s in device key list" % src_str
else:
print "did not find device 0x%s in device key list" % src_str
print "using AppSKey and NwkSKey from default device"
src_str = 'default'
AppSKey = key.device_key[src_str]['AppSKey']
NwkSKey = key.device_key[src_str]['NwkSKey']
#print "start decryption"
appskey=bytearray.fromhex(AppSKey)
appskeylist=[]
for i in range (0,len(appskey)):
appskeylist.append(appskey[i])
nwkskey=bytearray.fromhex(NwkSKey)
nwkskeylist=[]
for i in range (0,len(nwkskey)):
nwkskeylist.append(nwkskey[i])
lorawan = LoRaWAN.new(nwkskeylist)
lorawan.read(lorapkt)
lorawan.compute_mic()
if lorawan.valid_mic():
print "?loraWAN: valid MIC"
lorawan = LoRaWAN.new(appskeylist)
lorawan.read(lorapkt)
plain_payload = ''.join(chr(x) for x in lorawan.get_payload())
print "?loraWAN: plain payload is "+replchars.sub(replchars_to_hex, plain_payload)
return plain_payload
else:
return "###BADMIC###"
else:
return "###BADMIC###"
def loraWAN_get_MIC(src, lorapktstr):
src_str="%0.8X" % src
if src_str in key.device_key:
print "found device %0.8X in device key list" % src_str
else:
print "did not find device 0x%s in device key list" % src_str
print "using AppSKey and NwkSKey from default device"
src_str = 'default'
AppSKey = key.device_key[src_str]['AppSKey']
NwkSKey = key.device_key[src_str]['NwkSKey']
appskey=bytearray.fromhex(AppSKey)
appskeylist=[]
for i in range (0,len(appskey)):
appskeylist.append(appskey[i])
nwkskey=bytearray.fromhex(NwkSKey)
nwkskeylist=[]
for i in range (0,len(nwkskey)):
nwkskeylist.append(nwkskey[i])
srcHex = "%0.8X" % src
srcArray=bytearray.fromhex(srcHex)
srcaddr=[]
for i in range (0,len(srcArray)):
srcaddr.append(srcArray[len(srcArray)-1-i])
#print srcaddr
#print '[{}]'.format(', '.join(hex(x) for x in srcaddr))
lorawan = LoRaWAN.new(appskey)
lorawan.create(MHDR.UNCONF_DATA_UP, {'devaddr': srcaddr, 'data': list(map(ord, lorapktstr)) })
lorawan.__init__(nwkskey)
MIC=lorawan.compute_mic()
#print '[{}]'.format(', '.join(hex(x) for x in MIC))
return MIC
if __name__ == "__main__":
argc=len(sys.argv)
if argc>1:
#we assume that the input frame is given in base64 format
lorapktstr_b64=sys.argv[1]
else:
sys.exit("loraWAN.py needs at least a base64 encoded string argument")
if argc>2:
pdata=sys.argv[2]
arr = map(int,pdata.split(','))
dst=arr[0]
ptype=arr[1]
src=arr[2]
seq=arr[3]
datalen=arr[4]
SNR=arr[5]
RSSI=arr[6]
#LoRaWAN packet
if dst==256:
src_str="%0.8X" % src
else:
src_str=str(src)
if argc>3:
rdata=sys.argv[3]
plain_payload="###BADMIC###"
try:
lorapktstr=base64.b64decode(lorapktstr_b64)
lorapkt=[]
for i in range (0,len(lorapktstr)):
lorapkt.append(ord(lorapktstr[i]))
plain_payload=loraWAN_process_pkt(lorapkt)
except TypeError:
plain_payload="###BADMIC###"
if plain_payload=="###BADMIC###":
print '?'+plain_payload
else:
#the output is clear data
print "?plain payload is: "+plain_payload
if argc>2:
ptype = ptype & (~PKT_FLAG_DATA_ENCRYPTED)
print "^p%d,%d,%d,%d,%d,%d,%d" % (dst,ptype,src,seq,len(plain_payload),SNR,RSSI)
if argc>3:
print "^r"+rdata
print "\xFF\xFE"+plain_payload