-
Notifications
You must be signed in to change notification settings - Fork 0
/
theC64-toolkit.go
200 lines (189 loc) · 5.17 KB
/
theC64-toolkit.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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"strconv"
"unsafe"
)
//#cgo LDFLAGS: -lgcrypt
//#include <stdlib.h>
//#include <dlfcn.h>
//#include "decryptor.h"
import "C"
type section struct {
offset uint32
length uint32
}
func newSection(offset uint32, length uint32) *section {
s := section{offset: offset, length: length}
return &s
}
func check(e error) {
if e != nil {
fmt.Println(e)
panic(e)
}
}
func main() {
flag.Parse()
fmt.Println("theC64-toolkit")
args := flag.Args()
if len(args) < 1 {
fmt.Println("you must provide a firmware to analyze")
os.Exit(1)
}
if len(args) > 1 {
fmt.Println("too many arguments provided")
os.Exit(1)
}
fw, err := os.Open(args[0])
check(err)
magic := make([]byte, 4)
_, err = fw.Read(magic)
check(err)
mgc := hex.EncodeToString(magic) // TODO: check magic
correct_mgc := "ac64ac64"
if mgc != correct_mgc {
fmt.Println("Invalid magic: " + mgc)
os.Exit(2)
}
fmt.Println("Magic: " + mgc)
version := make([]byte, 4)
_, err = fw.Read(version)
check(err)
fmt.Print("Version: ")
fmt.Println(binary.LittleEndian.Uint32(version))
fw_sha256 := make([]byte, 32)
_, err = fw.Read(fw_sha256)
check(err)
fmt.Print("FW SHA256: ")
fmt.Println(hex.EncodeToString(fw_sha256))
installer_offset := make([]byte, 4)
installer_size := make([]byte, 4)
_, err = fw.Read(installer_offset)
check(err)
_, err = fw.Read(installer_size)
check(err)
installer_section := newSection(binary.LittleEndian.Uint32(installer_offset), binary.LittleEndian.Uint32(installer_size))
old_offset := getFileOffset(fw)
writeSectionToDisk("installer", *installer_section, fw)
fw.Seek(old_offset, 0)
// add installer.enc
enc_ins_offset := make([]byte, 4)
enc_ins_size := make([]byte, 4)
_, err = fw.Read(enc_ins_offset)
check(err)
_, err = fw.Read(enc_ins_size)
enc_installer := newSection(binary.LittleEndian.Uint32(enc_ins_offset), binary.LittleEndian.Uint32(enc_ins_size))
old_offset = getFileOffset(fw)
writeSectionToDisk("installer.enc", *enc_installer, fw)
decryptSection("installer.enc", getEncryptionKey())
fw.Seek(old_offset, 0)
// find size of header
elf_sig, err := hex.DecodeString("7f454c46")
check(err)
firmware, err := os.ReadFile(args[0])
check(err)
size_of_header := bytes.Index(firmware, elf_sig)
no_of_ext_sections := (int64(size_of_header) - getFileOffset(fw)) / 8
fmt.Print("Calculated # of sections: ")
fmt.Println(no_of_ext_sections)
offsets := make([]uint32, no_of_ext_sections)
sizes := make([]uint32, no_of_ext_sections)
tmp := make([]byte, 4)
for i := 0; i < int(no_of_ext_sections); i++ {
_, err := fw.Read(tmp)
check(err)
offsets[i] = binary.LittleEndian.Uint32(tmp)
}
for i := 0; i < int(no_of_ext_sections); i++ {
_, err := fw.Read(tmp)
check(err)
sizes[i] = binary.LittleEndian.Uint32(tmp)
}
ext_sections := make([]*section, no_of_ext_sections)
for i := 0; i < int(no_of_ext_sections); i++ {
ext_sections[i] = newSection(offsets[i], sizes[i])
sectionFilename := "section." + strconv.Itoa(i) + ".enc"
writeSectionToDisk(sectionFilename, *ext_sections[i], fw)
decryptSection(sectionFilename, getEncryptionKey())
}
}
func getFileOffset(fw *os.File) int64 {
offset, err := fw.Seek(0, io.SeekCurrent)
check(err)
return offset
}
func writeSectionToDisk(name string, sect section, fw *os.File) {
if sect.length == 0 {
fmt.Println("Skipping " + name)
}
ofile, err := os.Create(name)
check(err)
fw.Seek(int64(sect.offset), 0)
tmp := make([]byte, sect.length)
_, err = fw.Read(tmp)
check(err)
_, err = ofile.Write(tmp)
check(err)
fmt.Println("Wrote: "+name, sect.offset+sect.length, strconv.FormatInt(int64(sect.offset+sect.length), 16))
}
func getEncryptionKey() []byte {
// sha256sum(1) of keyfile
kf256 := "650d9c3dc1860e6be17b3f55be1bf3825af718a43df1c510819ccf87e5ccf214"
// a little binary ninja goes a long way ...
recovered_key, err := hex.DecodeString("6631c06689df66f3ab6689e066e8d3ef")
check(err)
res := ""
for i := 0; i < len(recovered_key); i++ {
digit := byte(kf256[i]) ^ byte(recovered_key[i])
temp := strconv.FormatInt(int64(digit), 16)
// poor man's rjust
if len(temp) == 1 {
temp = "0" + temp
}
res += temp
}
foo, err := hex.DecodeString(res)
check(err)
return foo
}
func decryptSection(filename string, key []byte) {
libname := C.CString("./decryptor.so")
defer C.free(unsafe.Pointer(libname))
handle := C.dlopen(libname, C.RTLD_LAZY)
if handle == nil {
err := fmt.Errorf("error opening decryptor.so")
check(err)
}
defer func() {
if r := C.dlclose(handle); r != 0 {
err := fmt.Errorf("Error closing decryptor.so")
check(err)
}
}()
sym := C.CString("decrypt")
defer C.free(unsafe.Pointer(sym))
decrypt := C.dlsym(handle, sym)
if decrypt == nil {
err := fmt.Errorf("error resolving decrypt function")
check(err)
}
ret := C.decrypt(C.CString(filename), (*C.char)(unsafe.Pointer(&key[0])))
defer C.free(unsafe.Pointer(ret))
ofile, err := os.Create(filename + ".dec.tar.gz")
check(err)
// our assumption: files have identical sizes
fl, err := os.Stat(filename)
check(err)
// TODO: pad fl
err = os.Remove(filename) //TOCTOU
check(err)
buffer := C.GoBytes(unsafe.Pointer(ret), C.int(fl.Size()))
ofile.Write(buffer)
}