-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
main.go
192 lines (167 loc) · 5.68 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/EgeBalci/sgn/config"
sgn "github.com/EgeBalci/sgn/pkg"
"github.com/EgeBalci/sgn/utils"
"github.com/briandowns/spinner"
"github.com/fatih/color"
)
func main() {
printBanner()
// Configure the options from the flags/config file
opts, err := config.ConfigureOptions()
if err != nil {
utils.PrintFatal("%s", err)
}
// Setup a encoder struct
payload := []byte{}
encoder, err := sgn.NewEncoder(opts.Arch)
if err != nil {
utils.PrintFatal("%s", err)
}
encoder.ObfuscationLimit = opts.ObsLevel
encoder.PlainDecoder = opts.PlainDecoder
encoder.EncodingCount = opts.EncCount
encoder.SaveRegisters = opts.Safe
file, err := os.ReadFile(opts.Input)
if err != nil {
utils.PrintFatal("%s", err)
}
spinr := spinner.New(spinner.CharSets[9], 50*time.Millisecond)
if !opts.Verbose {
spinr.Start()
}
// Print encoder params...
utils.PrintVerbose("Architecture: x%d", encoder.GetArchitecture())
utils.PrintVerbose("Encode Count: %d", encoder.EncodingCount)
utils.PrintVerbose("Max. Obfuscation Size: %d", encoder.ObfuscationLimit)
utils.PrintVerbose("Bad Characters: %x", opts.BadChars)
utils.PrintVerbose("ASCII Mode: %t", opts.AsciiPayload)
utils.PrintVerbose("Plain Decoder: %t", encoder.PlainDecoder)
utils.PrintVerbose("Safe Registers: %t", encoder.SaveRegisters)
// Calculate evarage garbage instrunction size
average, err := encoder.CalculateAverageGarbageInstructionSize()
if err != nil {
utils.PrintFatal("%s", err)
}
utils.PrintVerbose("Avg. Garbage Size: %f", average)
if opts.BadChars != "" || opts.AsciiPayload {
// Need to disable verbosity now
if utils.Verbose {
spinr.Start()
utils.Verbose = false
}
spinr.Suffix = " Bruteforcing bad characters..."
badBytes, err := hex.DecodeString(strings.ReplaceAll(opts.BadChars, `\x`, ""))
if err != nil {
utils.PrintFatal("%s", err)
}
for {
p, err := encode(encoder, file)
if err != nil {
utils.PrintFatal("%s", err)
}
if (opts.AsciiPayload && utils.IsASCIIPrintable(string(p))) || (len(badBytes) > 0 && !bytes.Contains(p, badBytes)) {
payload = p
break
}
encoder.Seed = (encoder.Seed + 1) % 255
}
spinr.Stop()
utils.PrintStatus("Success ᕕ( ᐛ )ᕗ")
} else {
utils.PrintVerbose("Encoding payload...")
payload, err = encode(encoder, file)
if err != nil {
utils.PrintFatal("%s", err)
}
}
spinr.Stop()
if opts.Output == "" {
opts.Output = opts.Input + ".sgn"
}
utils.PrintStatus("Input: %s", opts.Input)
utils.PrintStatus("Input Size: %d", len(file))
utils.PrintStatus("Outfile: %s", opts.Output)
out, err := os.OpenFile(opts.Output, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
utils.PrintFatal("%s", err)
}
_, err = out.Write(payload)
if err != nil {
utils.PrintFatal("%s", err)
}
outputSize := len(payload)
if utils.Verbose {
color.Blue("\n" + hex.Dump(payload) + "\n")
}
utils.PrintVerbose("Total Garbage Size: %d", encoder.ObfuscationLimit)
utils.PrintSuccess("Final size: %d", outputSize)
utils.PrintSuccess("All done \(^O^)/")
}
// Encode function is the primary encode method for SGN
func encode(encoder *sgn.Encoder, payload []byte) ([]byte, error) {
red := color.New(color.Bold, color.FgRed).SprintfFunc()
green := color.New(color.Bold, color.FgGreen).SprintfFunc()
var final []byte
if encoder.SaveRegisters {
utils.PrintVerbose("Adding safe register suffix...")
payload = append(sgn.SafeRegisterSuffix[encoder.GetArchitecture()], payload...)
}
// Add garbage instrctions before the ciphered decoder stub
garbage, err := encoder.GenerateGarbageInstructions()
if err != nil {
return nil, err
}
payload = append(garbage, payload...)
encoder.ObfuscationLimit -= len(garbage)
utils.PrintVerbose("Ciphering payload...")
ciperedPayload := sgn.CipherADFL(payload, encoder.Seed)
decoderAssembly, err := encoder.NewDecoderAssembly(len(ciperedPayload))
if err != nil {
utils.PrintFatal("%s", err)
}
utils.PrintVerbose("Selected decoder: %s", green("\n%s\n", decoderAssembly))
decoder, ok := encoder.Assemble(decoderAssembly)
if !ok {
return nil, errors.New("decoder assembly failed")
}
encodedPayload := append(decoder, ciperedPayload...)
if encoder.PlainDecoder {
final = encodedPayload
} else {
schemaSize := ((len(encodedPayload) - len(ciperedPayload)) / (encoder.GetArchitecture() / 8)) + 1
randomSchema := encoder.NewCipherSchema(schemaSize)
utils.PrintVerbose("Cipher schema: %s", red("\n\n%s", sgn.GetSchemaTable(randomSchema)))
obfuscatedEncodedPayload := encoder.SchemaCipher(encodedPayload, 0, randomSchema)
final, err = encoder.AddSchemaDecoder(obfuscatedEncodedPayload, randomSchema)
if err != nil {
return nil, err
}
}
if encoder.SaveRegisters {
utils.PrintVerbose("Adding safe register prefix...")
final = append(sgn.SafeRegisterPrefix[encoder.GetArchitecture()], final...)
}
if encoder.EncodingCount > 1 {
encoder.EncodingCount--
encoder.Seed = sgn.GetRandomByte()
final, err = encode(encoder, final)
if err != nil {
return nil, err
}
}
return final, nil
}
func printBanner() {
banner, _ := base64.StdEncoding.DecodeString("ICAgICAgIF9fICAgXyBfXyAgICAgICAgX18gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXyAKICBfX18gLyAvICAoXykgL19fX19fIF8vIC9fX19fIF8gIF9fXyBfX19fIF8gIF9fXyAgX19fIF8oXykKIChfLTwvIF8gXC8gLyAgJ18vIF8gYC8gX18vIF8gYC8gLyBfIGAvIF8gYC8gLyBfIFwvIF8gYC8gLyAKL19fXy9fLy9fL18vXy9cX1xcXyxfL1xfXy9cXyxfLyAgXF8sIC9cXyxfLyAvXy8vXy9cXyxfL18vICAKPT09PT09PT1bQXV0aG9yOi1FZ2UtQmFsY8SxLV09PT09L19fXy89PT09PT09djIuMC4xPT09PT09PT09ICAKICAgIOKUu+KUgeKUuyDvuLXjg70oYNCUwrQp776J77i1IOKUu+KUgeKUuyAgICAgICAgICAgKOODjiDjgpzQlOOCnCnjg44g77i1IOS7leaWueOBjOOBquOBhAo=")
fmt.Println(string(banner))
}