-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpsobf.go
180 lines (162 loc) · 5.55 KB
/
psobf.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
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
)
func obfuscate(word string, level int) string {
switch level {
case 1:
concatenated := ""
for _, char := range word {
concatenated += obfuscateCharacter(char)
}
concatenated = strings.TrimSuffix(concatenated, ",")
return fmt.Sprintf("$obfuscated = $([char[]](%s) -join ''); Invoke-Expression $obfuscated", concatenated)
case 2:
encoded := base64.StdEncoding.EncodeToString([]byte(word))
return fmt.Sprintf("$obfuscated = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('%s')); Invoke-Expression $obfuscated", encoded)
case 3:
encoded := base64.StdEncoding.EncodeToString([]byte(word))
return fmt.Sprintf("$e = [System.Convert]::FromBase64String('%s'); $obfuscated = [System.Text.Encoding]::UTF8.GetString($e); Invoke-Expression $obfuscated", encoded)
case 4:
encoded := compressAndEncode(word)
return fmt.Sprintf("$compressed = '%s'; $bytes = [System.Convert]::FromBase64String($compressed); $stream = New-Object IO.MemoryStream(, $bytes); $decompressed = New-Object IO.Compression.GzipStream($stream, [IO.Compression.CompressionMode]::Decompress); $reader = New-Object IO.StreamReader($decompressed); $obfuscated = $reader.ReadToEnd(); Invoke-Expression $obfuscated", encoded)
case 5:
fragments := fragmentScript(word)
return fmt.Sprintf("$fragments = @('%s'); $script = $fragments -join ''; Invoke-Expression $script", strings.Join(fragments, "','"))
default:
log.Panicf("Unsupported obfuscation level: %d", level)
return ""
}
}
func obfuscateCharacter(char rune) string {
switch char {
case '\n':
return "\"`n\","
case '\'':
return "\"'\","
case '`':
return "\"``\","
case '$':
return "\"`$\","
case '(':
return "\"`(\","
case ')':
return "\"`)\","
case '|':
return "\"`|\","
default:
return fmt.Sprintf("'%s',", string(char))
}
}
func compressAndEncode(word string) string {
var b bytes.Buffer
w := gzip.NewWriter(&b)
_, err := w.Write([]byte(word))
if err != nil {
log.Panicf("Failed to compress: %v", err)
}
w.Close()
return base64.StdEncoding.EncodeToString(b.Bytes())
}
func fragmentScript(script string) []string {
rand.Seed(time.Now().UnixNano())
length := len(script)
fragmentSize := rand.Intn(10) + 10
var fragments []string
for i := 0; i < length; i += fragmentSize {
end := i + fragmentSize
if end > length {
end = length
}
fragment := script[i:end]
fragment = strings.ReplaceAll(fragment, "'", "''")
fragments = append(fragments, fragment)
}
return fragments
}
func obfuscateVariables(script string) string {
variables := findVariables(script)
for _, variable := range variables {
obfuscated := randomString(len(variable))
script = strings.ReplaceAll(script, variable, obfuscated)
}
return script
}
func findVariables(script string) []string {
variables := make(map[string]bool)
lines := strings.Split(script, "\n")
for _, line := range lines {
if strings.Contains(line, "$") {
parts := strings.Fields(line)
for _, part := range parts {
if strings.HasPrefix(part, "$") {
variables[part] = true
}
}
}
}
keys := make([]string, 0, len(variables))
for key := range variables {
keys = append(keys, key)
}
return keys
}
func randomString(length int) string {
rand.Seed(time.Now().UnixNano())
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var b strings.Builder
for i := 0; i < length; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
return b.String()
}
func main() {
fmt.Println(`
██████╗ ███████╗ ██████╗ ██████╗ ███████╗
██╔══██╗██╔════╝██╔═══██╗██╔══██╗██╔════╝
██████╔╝███████╗██║ ██║██████╔╝█████╗
██╔═══╝ ╚════██║██║ ██║██╔══██╗██╔══╝
██║ ███████║╚██████╔╝██████╔╝██║
╚═╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝
@TaurusOmar
v.1.0
`)
inputFile := flag.String("i", "", "Name of the PowerShell script file.")
outputFile := flag.String("o", "obfuscated.ps1", "Name of the output file for the obfuscated script.")
level := flag.Int("level", 1, "Obfuscation level (1 to 5).")
flag.Usage = func() {
fmt.Println("Usage: ./obfuscator -i <inputFile> -o <outputFile> -level <1|2|3|4|5>")
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Println("\nObfuscation levels:")
fmt.Println(" 1: Basic obfuscation by splitting the script into individual characters.")
fmt.Println(" 2: Base64 encoding of the script.")
fmt.Println(" 3: Alternative Base64 encoding with a different PowerShell decoding method.")
fmt.Println(" 4: Compression and Base64 encoding of the script will be decoded and decompressed at runtime.")
fmt.Println(" 5: Fragmentation of the script into multiple parts and reconstruction at runtime.")
}
flag.Parse()
if *inputFile == "" {
flag.Usage()
return
}
psScript, err := os.ReadFile(*inputFile)
if err != nil {
log.Panicf("Could not read the file: %v", err)
}
obfuscated := obfuscate(string(psScript), *level)
err = os.WriteFile(*outputFile, []byte(obfuscated), 0644)
if err != nil {
log.Panicf("Could not write to the file: %v", err)
}
fmt.Println("The obfuscated script has been written to", *outputFile)
}