-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (104 loc) · 3.59 KB
/
main.go
File metadata and controls
128 lines (104 loc) · 3.59 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
package main
import (
"fmt"
"os"
"strings"
"time"
)
var (
// the list of protobuf message class files found
protoMessageFiles []string
// the list of protobuf enum class files found
protoEnumFiles []string
)
func main() {
// parse arguments
if len(os.Args) < 3 {
fmt.Println("[x] Usage: protoextract <package name> <decompiled apk dir>")
os.Exit(1)
}
// store the package name
packageName := os.Args[1]
// store start time
startTime := time.Now()
// create the output directory
baseDir := os.Args[2]
outputDir := baseDir + "_protoextract/proto"
err := os.MkdirAll(outputDir, 0755)
if err != nil {
fmt.Printf("[x] Error creating output directory: '%s'\n", err)
os.Exit(1)
}
fmt.Printf("[!] Beginning extraction of .proto files from message classes in '%s'...\n", baseDir)
err = findProtoMessageClasses(baseDir)
if err != nil {
fmt.Printf("[x] Error finding protobuf message classes: '%s'\n", err)
os.Exit(1)
}
fmt.Printf("[!] Found %d protobuf message classes. Starting parsing process...\n", len(protoMessageFiles))
// parse all protobuf classes
for _, filePath := range protoMessageFiles {
// parse the .proto file
classPath, neededImports, protoFields, err := parseProtoFile(filePath)
if err != nil {
fmt.Printf("[x] Error parsing '%s' file: '%s'\n", filePath, err)
continue
}
// create the output directory
err = os.MkdirAll(fmt.Sprintf("%s/%s", outputDir, classPath[:strings.LastIndex(classPath, "/")]), 0755)
if err != nil {
fmt.Printf("[x] Error creating output directory: '%s'\n", err)
continue
}
// create the output .proto file
outputProtoFile := fmt.Sprintf("%s/%s.proto", outputDir, classPath)
protoFile, err := os.Create(outputProtoFile)
if err != nil {
fmt.Printf("[x] Error creating .proto file: '%s'\n", err)
continue
}
// write the .proto file
_, err = protoFile.WriteString(generateProtoFileContent(packageName, classPath, neededImports, protoFields, false))
if err != nil {
fmt.Printf("[x] Error writing .proto file: '%s'\n", err)
continue
}
fmt.Printf("[!] Successfully generated message schema '%s'\n", outputProtoFile)
}
fmt.Printf("[!] Beginning extraction of .proto files from enum classes in '%s'...\n", baseDir)
err = findProtoEnumClasses(baseDir)
if err != nil {
fmt.Printf("[x] Error finding protobuf enum classes: '%s'\n", err)
os.Exit(1)
}
fmt.Printf("[!] Found %d protobuf enum classes. Starting parsing process...\n", len(protoEnumFiles))
for _, filePath := range protoEnumFiles {
// parse the .proto file
classPath, protoFields, err := parseWireEnum(filePath)
if err != nil {
fmt.Printf("[x] Error parsing '%s' file: '%s'\n", filePath, err)
continue
}
// create the output directory
err = os.MkdirAll(fmt.Sprintf("%s/%s", outputDir, classPath[:strings.LastIndex(classPath, "/")]), 0755)
if err != nil {
fmt.Printf("[x] Error creating output directory: '%s'\n", err)
continue
}
// create the output .proto file
outputProtoFile := fmt.Sprintf("%s/%s.proto", outputDir, classPath)
protoFile, err := os.Create(outputProtoFile)
if err != nil {
fmt.Printf("[x] Error creating .proto file: '%s'\n", err)
continue
}
// write the .proto file
_, err = protoFile.WriteString(generateProtoFileContent(packageName, classPath, nil, protoFields, true))
if err != nil {
fmt.Printf("[x] Error writing .proto file: '%s'\n", err)
continue
}
fmt.Printf("[!] Successfully generated enum schema '%s'\n", outputProtoFile)
}
fmt.Printf("[!] Finished generating %d .proto files in %.3fs\n", len(protoMessageFiles)+len(protoEnumFiles), time.Since(startTime).Seconds())
}