-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (128 loc) · 3.09 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/fs"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"github.com/zergon321/arrayqueue"
)
var (
dumpbinPath string
exePath string
dllPath string
)
func parseFlags() {
flag.StringVar(&dumpbinPath, "dumpbin", "C:\\Program Files\\Microsoft Visual Studio"+
"\\2022\\Community\\VC\\Tools\\MSVC\\14.30.30705\\bin\\Hostx64\\x64\\dumpbin.exe",
"An absolute path to the 'dumpbin' command line utility")
flag.StringVar(&exePath, "exe", "", "A path to the executable file to be analyzed")
flag.StringVar(&dllPath, "dll", "C:\\msys64\\mingw64\\bin",
"A path to the directory where all the possible DLLs are stored")
flag.Parse()
}
func createGetDependentsCommand(dumpbinPath string) string {
return "& \"" + dumpbinPath + "\" /dependents"
}
func getDependents(targetPath string) ([]string, error) {
if !filepath.IsAbs(targetPath) {
var err error
targetPath, err = filepath.Abs(targetPath)
if err != nil {
return nil, err
}
}
buffer := bytes.NewBuffer([]byte{})
cmd := exec.Command("powershell", "-Command",
createGetDependentsCommand(dumpbinPath)+" "+targetPath)
cmd.Stdout = buffer
err := cmd.Run()
if err != nil {
return nil, err
}
out := buffer.String()
return getDLLsFromOutput(out), nil
}
func getDLLsFromOutput(out string) []string {
lines := strings.Split(out, "\n")
dlls := []string{}
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasSuffix(line, ".dll") {
dlls = append(dlls, line)
}
}
return dlls
}
func containsDLL(dllEntries []fs.FileInfo, dllName string) bool {
contains := false
for _, dllEntry := range dllEntries {
if dllEntry.Name() == dllName {
contains = true
break
}
}
return contains
}
func contains(strs []string, str string) bool {
contains := false
for _, strEntry := range strs {
if strEntry == str {
contains = true
break
}
}
return contains
}
func main() {
parseFlags()
var err error
entries, err := ioutil.ReadDir(dllPath)
handleError(err)
dllEntries := []fs.FileInfo{}
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".dll") {
dllEntries = append(dllEntries, entry)
}
}
directDependencies, err := getDependents(exePath)
handleError(err)
inspectQueue, err := arrayqueue.NewQueue(64)
handleError(err)
for _, dependency := range directDependencies {
inspectQueue.Enqueue(dependency)
}
bundleDLLs := []string{}
for inspectQueue.Length() > 0 {
item, err := inspectQueue.Dequeue()
handleError(err)
dependency, ok := item.(string)
if !ok {
handleError(fmt.Errorf(
"wrong dependency item type: %s", item))
}
if contains(bundleDLLs, dependency) {
continue
}
if containsDLL(dllEntries, dependency) {
bundleDLLs = append(bundleDLLs, dependency)
fmt.Println(dependency)
} else {
continue
}
dependencyPath := filepath.Join(dllPath, dependency)
dependencies, err := getDependents(dependencyPath)
handleError(err)
for _, dependency := range dependencies {
inspectQueue.Enqueue(dependency)
}
}
}
func handleError(err error) {
if err != nil {
panic(err)
}
}