-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslotFunctions.go
525 lines (459 loc) · 13.5 KB
/
slotFunctions.go
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/WolfgangMau/chamgo-qt/nonces"
"github.com/WolfgangMau/chamgo-qt/xmodem"
"github.com/therecipe/qt/widgets"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var myTime time.Time
var GetUsbListTicker *time.Ticker
//noinspection GoPrintFunctions
func buttonClicked(btn int) {
switch ActionButtons[btn] {
case "Select All":
selectAllSlots(true)
if populated {
}
case "Select None":
selectAllSlots(false)
if populated {
}
case "Apply":
applySlot()
case "Clear":
clearSlot()
case "Refresh":
refreshSlot()
case "Set Active":
activateSlots()
case "mfkey32":
mfkey32Slots()
case "Upload":
uploadSlots()
case "Download":
downloadSlots()
case "RSSI":
getRssi()
default:
log.Printf("clicked on Button: %s\n", ActionButtons[btn])
}
}
func getRssi() {
if Connected {
sendSerialCmd(DeviceActions.GetRssi)
RssiVal.SetText(SerialResponse.Payload)
RssiVal.Repaint()
}
}
//noinspection GoPrintFunctions
func slotChecked(slot, state int) {
log.Printf(" Checked %d - state: %d\n", slot, state)
if state == 2 && Connected {
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(slot+Cfg.Device[SelectedDeviceId].Config.Slot.Offset))
}
Slots[slot].slot.Repaint()
}
func selectAllSlots(b bool) {
for _, s := range Slots {
s.slot.SetChecked(b)
s.slot.Repaint()
}
}
func applySlot() {
for i, s := range Slots {
if s.slot.IsChecked() {
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
//select slot
sendSerialCmd(Cfg.Device[SelectedDeviceId].CmdSet["config"] + "=" + s.mode.CurrentText())
//set mode
sendSerialCmd(Cfg.Device[SelectedDeviceId].CmdSet["config"] + "=" + s.mode.CurrentText())
//set uid
sendSerialCmd(Cfg.Device[SelectedDeviceId].CmdSet["uid"] + "=" + s.uid.Text())
//set button short
sendSerialCmd(Cfg.Device[SelectedDeviceId].CmdSet["button"] + "=" + s.btns.CurrentText())
//set button long
sendSerialCmd(Cfg.Device[SelectedDeviceId].CmdSet["buttonl"] + "=" + s.btnl.CurrentText())
}
}
populateSlots()
}
func countSelected() int {
c := 0
for _, s := range Slots {
if s.slot.IsChecked() {
c++
}
}
return c
}
//noinspection GoPrintFunctions
func clearSlot() {
c1 := 0
for i, s := range Slots {
sel := s.slot.IsChecked()
if sel {
c1++
log.Printf("clearing %s\n", s.slotl.Text())
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
sendSerialCmd(DeviceActions.ClearSlot)
}
}
time.Sleep(time.Millisecond * 50)
populateSlots()
}
func refreshSlot() {
populateSlots()
}
func activateSlots() {
if countSelected() > 1 {
widgets.QMessageBox_Information(nil, "OK", "please select only one Slot",
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
return
}
for i, s := range Slots {
sel := s.slot.IsChecked()
if sel {
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
Cfg.Device[SelectedDeviceId].Config.Slot.Selected = hardwareSlot
}
}
}
//ToDO: implemetation
//noinspection ALL
func mfkey32Slots() {
if !Connected || countSelected() < 1 {
if !Connected {
return
}
widgets.QMessageBox_Information(nil, "OK", "please select at least one Slot\nwhich was set to DETECTION",
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
return
}
detectionCmd, ok := Cfg.Device[SelectedDeviceId].CmdSet["detection"]
if !ok {
widgets.QMessageBox_Information(nil, "OK", "Sorry, but this Device hs not set a 'detection' cmd!",
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
return
}
for i, s := range Slots {
sel := s.slot.IsChecked()
if sel {
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
serialMonitor.AppendPlainText("-> " + strings.Replace(strings.Replace(detectionCmd, "\r", "", -1), "\n", "", -1))
// send cmd ang get the expected 218 bytes (208 nonce + 2 crc + 8 cmd-response (100:OK\n\r)
SerialSendOnly(detectionCmd + "?")
buff := GetSpecificBytes(218)
//buffer should be empty - only to get sure
SerialPort.ResetInputBuffer()
//responsecode := strings.Replace(strings.Replace(string(buff[len(buff)-8:]), "\r", "", -1), "\n", "", -1)
log.Println("len enc: ", len(buff))
buff = nonces.DecryptData(buff[0:len(buff)-10], 123321, 208)
uid := buff[0:4]
serialMonitor.AppendPlainText(fmt.Sprintf("uid: %x\n", uid))
noncemap := nonces.ExtractNonces(buff)
var skey string
if len(noncemap) > 0 {
MyTabs.SetCurrentIndex(1)
serialMonitor.AppendPlainText(fmt.Sprintf("Fond %d nonces for UID: %04X - test possible comboinations ...", len(noncemap), uid))
log.Println(" UID NT0 NR0 AR0 NT1 NR1 AR1")
for i1 := 0; i1 < len(noncemap); i1++ {
for i2 := 0; i2 < len(noncemap); i2++ {
if i1 == i2 || i1 > i2 {
continue
} else {
if noncemap[i1].Key == noncemap[i2].Key {
if noncemap[i1].Key == 0x60 {
skey = "A"
} else {
skey = "B"
}
args := []string{hex.EncodeToString(uid), hex.EncodeToString(noncemap[i1].Nt), hex.EncodeToString(noncemap[i1].Nr), hex.EncodeToString(noncemap[i1].Ar), hex.EncodeToString(noncemap[i2].Nt), hex.EncodeToString(noncemap[i2].Nr), hex.EncodeToString(noncemap[i2].Ar)}
log.Printf("%04X %04X %04X %04X %04X %04X %04X\n", uid, noncemap[i1].Nt, noncemap[i1].Nr, noncemap[i1].Ar, noncemap[i2].Nt, noncemap[i2].Nr, noncemap[i2].Ar)
res, err := execCmd("mfkey32v2", args)
if err != nil {
log.Println(err)
} else {
if strings.Contains(res, "Found Key") {
key := strings.Split(res, "[")[1]
key = key[:12]
serialMonitor.AppendPlainText(fmt.Sprintf("Slot %d: Possible Key %s for Nonces on Blocks %d & %d = %s", i+1, skey, noncemap[i1].Block, noncemap[i2].Block, key))
}
}
}
}
}
}
}
}
}
}
//noinspection ALL
func uploadSlots() bool {
if countSelected() > 1 {
widgets.QMessageBox_Information(nil, "OK", "please select only one Slot",
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
return false
}
var filename string
fileSelect := widgets.NewQFileDialog(nil, 0)
filename = fileSelect.GetOpenFileName(nil, "open Dump", "", "", "", fileSelect.Options())
if filename == "" {
log.Println("no file selected")
return false
}
for i, s := range Slots {
if s.slot.IsChecked() {
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
// Open file
log.Printf("loading file %s\n", filename)
fIn, err := os.Open(filename)
if err != nil {
log.Fatalln(err)
}
//readfile into buffer
data, err := ioutil.ReadAll(fIn)
if err != nil {
log.Println(err)
}
fIn.Close()
var p []xmodem.Xblock
p = Bytes2Packets(data)
//set chameleon into receiver-mode
sendSerialCmd(DeviceActions.StartUpload)
if SerialResponse.Code == 110 {
//start uploading packets
xmodem.Send(SerialPort, p)
}
}
}
refreshSlot()
return true
}
func Bytes2Packets(data []byte) []xmodem.Xblock {
var p []xmodem.Xblock
var p1 xmodem.Xblock
for _, d := range data {
p1.Payload = append(p1.Payload, d)
if len(p1.Payload) == 128 {
p1.Proto = []byte{xmodem.SOH}
p1.PacketNum = len(p)
p1.PacketInv = 255 - p1.PacketNum
p1.Checksum = int(xmodem.Checksum(p1.Payload, 0))
p = append(p, p1)
p1.Payload = []byte("")
}
}
return p
}
//noinspection ALL
func downloadSlots() {
if countSelected() > 1 {
widgets.QMessageBox_Information(nil, "OK", "please select only one Slot",
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
return
}
var (
success int
failed int
)
var filename string
var data bytes.Buffer
for i, s := range Slots {
hardwareSlot := i + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
sel := s.slot.IsChecked()
if sel {
fileSelect := widgets.NewQFileDialog(nil, 0)
filename = fileSelect.GetSaveFileName(nil, "save Data from "+s.slotl.Text()+" to File", "", "", "", fileSelect.Options())
if filename == "" {
log.Println("no file seleted")
return
}
log.Printf("download a dump from Slot %d into file %s\n", i, filename)
//select slot
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
//set chameleon into receiver-mode
sendSerialCmd(DeviceActions.StartDownload)
if SerialResponse.Code == 110 {
temp, _ := strconv.Atoi(s.size.Text())
success, failed, data = xmodem.Receive(SerialPort, temp)
log.Printf("Success: %d - failed: %d\n", success, failed)
}
if _, err := SerialPort.Write([]byte{xmodem.CAN}); err != nil {
log.Println(err)
break
}
if data.Len() > 0 {
log.Printf("got %d bytes to write to %s... ", data.Len(), filename)
// Write file
fOut, err := os.Create(filename)
if err != nil {
log.Println(filename, " - write failed")
log.Fatalln(err)
}
fOut.Write(data.Bytes())
fOut.Close()
log.Println(filename, " - write successful")
} else {
log.Println("bytes - file not written - bytes received: ", data.Len())
}
}
}
}
func getSlotBytes(sn int) (res []byte) {
var data bytes.Buffer
var failed = 0
var success = 0
hardwareSlot := sn + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
//select slot
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
//set chameleon into receiver-mode
sendSerialCmd(DeviceActions.StartDownload)
if SerialResponse.Code == 110 {
temp, _ := strconv.Atoi(Slots[sn].size.Text())
log.Printf("expecting %d bytes", temp)
success, failed, data = xmodem.Receive(SerialPort, temp)
SerialPort.Write([]byte{xmodem.EOT})
log.Printf("HardwareSlot: %d - Success: %d - failed: %d - data-len: %d\n", hardwareSlot, success, failed, len(data.Bytes()))
}
if _, err := SerialPort.Write([]byte{xmodem.CAN}); err != nil {
SerialPort.Write([]byte{xmodem.CAN})
log.Println(err)
return nil
}
//ToDo: dirty woraround to avoid 'runs one time'-problem
sendSerialCmd(DeviceActions.GetRssi)
return data.Bytes()
}
func populateSlots() {
if !Connected {
return
}
if populated == false {
//ToDo: error-handling
sendSerialCmd(DeviceActions.GetModes)
TagModes = strings.Split(SerialResponse.Payload, ",")
//ToDo: error-handling
sendSerialCmd(DeviceActions.GetButtons)
TagButtons = strings.Split(SerialResponse.Payload, ",")
populated = true
}
c := 0
hardwareSlot := 0
for sn, s := range Slots {
//update single slot
if s.slot.IsChecked() {
c++
hardwareSlot = sn + Cfg.Device[SelectedDeviceId].Config.Slot.Offset
log.Println("read data for Slot ", sn+1)
sendSerialCmd(DeviceActions.SelectSlot + strconv.Itoa(hardwareSlot))
//get slot uid
sendSerialCmd(DeviceActions.GetUid)
uid := SerialResponse.Payload
//set uid to lineedit
s.uid.SetText(uid)
sendSerialCmd(DeviceActions.GetSize)
size := SerialResponse.Payload
s.size.SetText(size)
sendSerialCmd(DeviceActions.GetMode)
mode := SerialResponse.Payload
_, modeindex := getPosFromList(mode, TagModes)
s.mode.Clear()
s.mode.AddItems(TagModes)
s.mode.SetCurrentIndex(modeindex)
s.mode.Repaint()
sendSerialCmd(DeviceActions.GetButtonl)
buttonl := SerialResponse.Payload
_, buttonlindex := getPosFromList(buttonl, TagButtons)
s.btnl.Clear()
s.btnl.AddItems(TagButtons)
s.btnl.SetCurrentIndex(buttonlindex)
s.btnl.Repaint()
// ToDo: currently mostly faked - currently not implemented in my revG
// unlear about RButton & LButton short and long -> 4 scenarios?
// but works on RevG
sendSerialCmd(DeviceActions.GetButton)
buttons := SerialResponse.Payload
_, buttonsindex := getPosFromList(buttons, TagButtons)
s.btns.Clear()
s.btns.AddItems(TagButtons)
s.btns.SetCurrentIndex(buttonsindex)
s.btns.Repaint()
}
}
}
func checkForDevices() {
GetUsbListTicker = time.NewTicker(time.Millisecond * 5000)
go func() {
for myTime = range GetUsbListTicker.C {
if !Connected {
serialPorts, err := getSerialPorts()
if err != nil {
log.Println(err)
}
if len(serialPorts) > 0 && serialPortSelect.CurrentText() != serialPorts[SelectedPortId] {
serialPortSelect.Clear()
serialPortSelect.AddItems(serialPorts)
serialPortSelect.SetCurrentIndex(SelectedPortId)
serialPortSelect.Repaint()
deviceSelect.SetCurrentIndex(SelectedDeviceId)
deviceSelect.Repaint()
} else {
if len(serialPorts) == 0 {
serialPortSelect.Clear()
}
}
} else {
GetUsbListTicker.Stop()
}
}
}()
}
func getPosFromList(val string, array []string) (exists bool, index int) {
exists = false
index = -1
for i, v := range array {
if val == v {
index = i
exists = true
return
}
}
return
}
func execCmd(cmdstr string, args []string) (res string, err error) {
res = ""
err = nil
//set local path
os.Setenv("PATH", os.Getenv("PATH")+":"+os.Getenv("PWD")+"/bin/")
// Create an *exec.Cmd
cmd := exec.Command(cmdstr, args...)
// Stdout buffer
cmdOutput := &bytes.Buffer{}
// Attach buffer to command
cmd.Stdout = cmdOutput
// Execute command
//log.Printf("run Cmd: %s %s\n",cmd.Path,cmd.Args)
err = cmd.Run()
if err != nil {
log.Println("Error: ", err)
return res, err
}
// Only output the commands stdout
res = string(cmdOutput.Bytes())
//log.Printf("RES: %s\n",res)
return res, err
}