-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckcert.go
More file actions
179 lines (156 loc) · 3.61 KB
/
Copy pathcheckcert.go
File metadata and controls
179 lines (156 loc) · 3.61 KB
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
package main
import (
"os"
"os/exec"
)
import (
"archive/zip"
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
)
var defaultCert = map[string]string{
// AOSP
"android_media": "B7:9D:F4:A8:2E:90:B5:7E:A7:65:25:AB:70:37:AB:23:8A:42:F5:D3",
"android_platform": "27:19:6E:38:6B:87:5E:76:AD:F7:00:E7:EA:84:E4:C6:EE:E3:3D:FA",
"android_release": "61:ED:37:7E:85:D3:86:A8:DF:EE:6B:86:4B:D8:5B:0B:FA:A5:AF:81",
"android_shared": "5B:36:8C:FF:2D:A2:68:69:96:BC:95:EA:C1:90:EA:A4:F5:63:0F:E5",
}
func readCert(certFile string) map[string]string {
cert := make(map[string]string)
for k, v := range defaultCert {
cert[k] = v
}
// load cert file into memory
if certFile != "" {
data, err := ioutil.ReadFile(certFile)
if err != nil {
log.Print(err)
return nil
}
certs := strings.Split(string(data), "\n")
matcher := regexp.MustCompile(`([^\s]+)\s+([^\s]+)\s+([^\s]+)$`)
commentMatcher := regexp.MustCompile(`^\s*#`)
for _, line := range certs {
res := commentMatcher.FindStringSubmatch(line)
if res != nil {
continue
}
res = matcher.FindStringSubmatch(line)
if res == nil {
continue
}
key := fmt.Sprintf("%s_%s", res[1], res[2])
cert[key] = res[3]
}
}
return cert
}
func getSha1HashInFile(filename string) (result string) {
result = ""
shaMatcher := regexp.MustCompile(`\s+SHA1: (.+)`)
// TODO: find an alternative way to get sha1 hash
// call keytool -printcert -file <filename> |
cmd := exec.Command("keytool", "-printcert", "-file", filename)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Println(err)
return
}
if err := cmd.Start(); err != nil {
log.Println(err)
return
}
o, err := ioutil.ReadAll(stdout)
output := strings.Split(string(o), "\n")
if err := cmd.Wait(); err != nil {
log.Println(err)
return
}
for _, line := range output {
res := shaMatcher.FindStringSubmatch(line)
if res != nil {
return res[1]
}
}
return
}
func getSha1Hash(apkFile string) (apkSha1Hash string) {
// open apk file
r, err := zip.OpenReader(apkFile)
if err != nil {
fmt.Println(err)
return
}
defer r.Close()
matcher := regexp.MustCompile(`META-INF/.+.RSA$`)
for _, f := range r.File {
res := matcher.FindStringSubmatch(f.Name)
if res == nil {
continue
}
rc, err := f.Open()
if err != nil {
log.Println(err)
continue
}
defer rc.Close()
rsaData, err := ioutil.ReadAll(rc)
if err != nil {
log.Println(err)
continue
}
fileName := os.TempDir() + "cert"
os.Remove(fileName)
f, err := os.Create(fileName)
if err != nil {
log.Println(err)
continue
}
defer f.Close()
if _, err := f.Write(rsaData); err != nil {
log.Println(err)
continue
}
// remember to delete file
defer os.Remove(fileName)
return getSha1HashInFile(fileName)
}
fmt.Println("There's no RSA file")
return
}
// Checkcert return false if vendor/certType incorrect
func Checkcert(apkFile, certFile, vendor, certType string) (result bool) {
cert := readCert(certFile)
if cert == nil {
return false
}
apkSha1Hash := getSha1Hash(apkFile)
if apkSha1Hash == "" {
return false
}
// get cert
apkVendor := ""
apkCertType := ""
for k, v := range cert {
if v == apkSha1Hash {
s := strings.Split(k, "_")
apkVendor = s[0]
apkCertType = s[1]
}
}
if apkVendor == "" || apkCertType == "" {
fmt.Printf("%s is using \x1b[41mUNKNOWN\x1b[m key: %s\n", apkFile, apkSha1Hash)
} else {
fmt.Printf("%s is using \x1b[32m%s\x1b[m's \x1b[33m%s\x1b[m key\n", apkFile, apkVendor, apkCertType)
}
if vendor != "" && apkVendor != vendor {
return false
}
if certType != "" && apkCertType != certType {
return false
}
return true
}