-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsfxbuilder.go
More file actions
178 lines (146 loc) · 4.84 KB
/
sfxbuilder.go
File metadata and controls
178 lines (146 loc) · 4.84 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
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"gopkg.in/yaml.v3"
)
// BuildSFX creates a self-extracting rar zip and embed the fastfinder executable / configuration file / yara rules
func BuildSFX(configPath string, outputSfxExe string, logLevel int, noAdvUI bool) {
var configuration Configuration
yamlContent, err := os.ReadFile(configPath)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) Reading config for SFX: %v", err))
}
// We handle optional cipher
if !bytes.Contains(yamlContent, []byte("input")) {
yamlContent = RC4Cipher(yamlContent, ">\u00D5\u00B0\u00AAKb{\u00A1\u00A7\u00CCB$lM\u00D5\u00B19l.t\u00F2\u00D1\u00E9\u00A6\u00D8\u00BF") // Using the constant value manually or via reference if public
}
err = yaml.Unmarshal(yamlContent, &configuration)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) Parsing config for SFX: %v", err))
}
// Resolve YARA paths relative to configuration file (Critical to find files during build)
configBaseDir := filepath.Dir(configPath)
if configBaseDir != "" && configBaseDir != "." {
for i := 0; i < len(configuration.Input.Content.Yara); i++ {
p := strings.TrimSpace(configuration.Input.Content.Yara[i])
if len(p) == 0 || IsValidUrl(p) || filepath.IsAbs(p) {
continue
}
configuration.Input.Content.Yara[i] = filepath.Clean(filepath.Join(configBaseDir, p))
}
}
// compress inputDirectory into archive
archive := fastfinderResourcesCompress(configuration, logLevel, noAdvUI)
file, err := os.Create(outputSfxExe)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
defer file.Close()
// pack sfx binary and customized archive together
file.Write(sfxBinary)
file.Write(archive.Bytes())
}
// fastfinderResourcesCompress compress every package file into the zip archive
func fastfinderResourcesCompress(configuration Configuration, logLevel int, noAdvUI bool) bytes.Buffer {
var buffer bytes.Buffer
archive := zip.NewWriter(&buffer)
// embed fastfinder executable
exeName := "fastfinder"
if runtime.GOOS == "windows" {
exeName += ".exe"
}
zipFile, err := archive.Create(exeName)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
fsFile, err := os.ReadFile(os.Args[0])
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
r := bytes.NewReader(fsFile)
_, err = io.Copy(zipFile, r)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
// embed yara rules
configuration.Input.Content.Yara = EnumerateYaraInFolders(configuration.Input.Content.Yara)
for i := 0; i < len(configuration.Input.Content.Yara); i++ {
var fileName string
var fsFile []byte
if IsValidUrl(configuration.Input.Content.Yara[i]) {
response, err := http.Get(configuration.Input.Content.Yara[i])
if err != nil {
LogMessage(LOG_ERROR, "YARA file URL unreachable", configuration.Input.Content.Yara[i], err)
}
fsFile, err = io.ReadAll(response.Body)
if err != nil {
LogMessage(LOG_ERROR, "YARA file URL content unreadable", configuration.Input.Content.Yara[i], err)
}
response.Body.Close()
fileName = filepath.Base(configuration.Input.Content.Yara[i])[:len(filepath.Base(configuration.Input.Content.Yara[i]))-4]
if !strings.HasSuffix(fileName, ".yar") {
fileName += ".yar"
}
} else {
fileName = filepath.Base(configuration.Input.Content.Yara[i])
fsFile, err = os.ReadFile(configuration.Input.Content.Yara[i])
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
}
zipFile, err := archive.Create("fastfinder_resources/" + fileName)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
// cipher rules
if configuration.AdvancedParameters.YaraRC4Key != "" {
fsFile = RC4Cipher(fsFile, configuration.AdvancedParameters.YaraRC4Key)
}
r := bytes.NewReader(fsFile)
_, err = io.Copy(zipFile, r)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
configuration.Input.Content.Yara[i] = fileName
}
// embed configuration file
zipFile, err = archive.Create("fastfinder_resources/configuration.yaml")
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
d, err := yaml.Marshal(&configuration)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
// cipher configuration file
d = RC4Cipher(d, BUILDER_RC4_KEY)
r = bytes.NewReader(d)
_, err = io.Copy(zipFile, r)
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
// sfx exec instructions
var sfxcomment = "the comment below contains sfx script commands\r\n\r\n" +
"Path=" + tempFolder + "\r\n" +
"Setup=" + exeName + " -c " + "fastfinder_resources/configuration.yaml"
// propagate loglevel param
sfxcomment += fmt.Sprintf(" -v %d", logLevel)
// propagage advanced UI param
if noAdvUI {
sfxcomment += " -u"
}
archive.SetComment(sfxcomment)
err = archive.Close()
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
return buffer
}