-
Notifications
You must be signed in to change notification settings - Fork 28
/
sfxbuilder.go
166 lines (136 loc) · 4.26 KB
/
sfxbuilder.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
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"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(configuration Configuration, outputSfxExe string, logLevel int, logFileLocation string, noAdvUI bool, hideWindow bool) {
// compress inputDirectory into archive
archive := fastfinderResourcesCompress(configuration, logLevel, logFileLocation, noAdvUI, hideWindow)
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, logFileLocation string, noAdvUI bool, hideWindow 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 = ioutil.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] = "'./fastfinder_resources/" + 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"
}
// output log file
if len(logFileLocation) > 0 {
//sfxcomment += " -o \"" + logFileLocation + "\""
sfxcomment += fmt.Sprintf(" -o %s", logFileLocation)
}
if hideWindow && runtime.GOOS == "windows" {
sfxcomment += " -n"
sfxcomment += "\r\n" +
"Silent=1"
}
archive.SetComment(sfxcomment)
if err != nil {
return buffer
}
err = archive.Close()
if err != nil {
LogFatal(fmt.Sprintf("(ERROR) %v", err))
}
return buffer
}