-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsfxbuilder.go
136 lines (109 loc) · 2.59 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
package main
import (
"archive/zip"
"bytes"
"crypto/rc4"
_ "embed"
"fmt"
"io"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
//go:embed resources/winrar_sfx.exe
var sfxBinary []byte
func BuildSFX(yaraPath string, rc4key string, config Configuration, outputSfxExe string) error {
// compress inputDirectory into archive
archive := recursiveCompressFolder(yaraPath, rc4key, config)
file, err := os.Create(outputSfxExe)
if err != nil {
return err
}
defer file.Close()
// pack sfx binary and customized archive together
file.Write(sfxBinary)
file.Write(archive.Bytes())
return nil
}
func recursiveCompressFolder(yaraPath string, rc4key string, config Configuration) bytes.Buffer {
var buffer bytes.Buffer
archive := zip.NewWriter(&buffer)
files := SearchForYaraFiles(yaraPath, true)
// embed irma.exe executable
zipFile, err := archive.Create("irma.exe")
if err != nil {
log.Fatal("[ERROR] ", err)
}
fsFile, err := os.ReadFile(os.Args[0])
if err != nil {
log.Fatal("[ERROR] ", err)
}
r := bytes.NewReader(fsFile)
_, err = io.Copy(zipFile, r)
if err != nil {
log.Fatal("[ERROR] ", err)
}
// embed configuration file
zipFile, err = archive.Create("configuration.yaml")
if err != nil {
log.Fatal("[ERROR] ", err)
}
config.Yara.Path = "yara-signatures/"
d, err := yaml.Marshal(&config)
r = bytes.NewReader(d)
_, err = io.Copy(zipFile, r)
if err != nil {
log.Fatal("[ERROR] ", err)
}
// embed yara rules
for _, f := range files {
fileName := filepath.Base(f)
zipFile, err := archive.Create("yara-signatures/" + fileName)
if err != nil {
log.Fatal("[ERROR] ", err)
}
fsFile, err := os.ReadFile(f)
if err != nil {
log.Fatal("[ERROR] ", err)
}
// yara rule RC4 cipher
if len(rc4key) > 0 {
c, err := rc4.NewCipher([]byte(rc4key))
if err != nil {
log.Fatal("[ERROR] ", err)
}
c.XORKeyStream(fsFile, fsFile)
}
r := bytes.NewReader(fsFile)
_, err = io.Copy(zipFile, r)
if err != nil {
log.Fatal("[ERROR] ", err)
}
}
// sfx comment
var b2i int = 0
if config.Sfx.SilentMode {
b2i = 1
}
var sfxcomment = "the comment below contains sfx script commands\r\n\r\n" +
"Path=" + config.Sfx.ExtractDirectory + "\r\n"
if config.Sfx.Autoexec {
sfxcomment += "Setup=irma.exe -c configuration.yaml"
}
if len(config.Sfx.LogFile) > 0 {
sfxcomment += " -o \"" + config.Sfx.LogFile + "\""
}
sfxcomment += "\r\n" +
"Silent=" + fmt.Sprint(b2i) + "\r\n" +
"Overwrite=1"
archive.SetComment(sfxcomment)
if err != nil {
return buffer
}
err = archive.Close()
if err != nil {
log.Fatal("[ERROR] ", err)
}
return buffer
}