-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
powershell.go
54 lines (43 loc) · 1.34 KB
/
powershell.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
package powershell
import (
"encoding/base64"
"encoding/binary"
"unicode/utf16"
"unicode/utf8"
"golang.org/x/text/encoding/unicode"
)
func convertUtf8ToUtf16LE(message string) (string, error) {
utf16le := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
utfEncoder := utf16le.NewEncoder()
ut16LeEncodedMessage, err := utfEncoder.String(message)
return ut16LeEncodedMessage, err
}
// UTF16BytesToString converts UTF-16 encoded bytes, in big or little endian byte order,
// to a UTF-8 encoded string.
func UTF16BytesToString(b []byte, o binary.ByteOrder) string {
utf := make([]uint16, (len(b)+(2-1))/2)
for i := 0; i+(2-1) < len(b); i += 2 {
utf[i/2] = o.Uint16(b[i:])
}
if len(b)/2 < len(utf) {
utf[len(utf)-1] = utf8.RuneError
}
return string(utf16.Decode(utf))
}
func powershellEncode(message string) (string, error) {
utf16LEEncodedMessage, err := convertUtf8ToUtf16LE(message)
if err != nil {
return "", err
}
// Base64 encode the command
input := []uint8(utf16LEEncodedMessage)
return base64.StdEncoding.EncodeToString(input), nil
}
func powershellDecode(messageBase64 string) (retour string, err error) {
messageUtf16LeByteArray, err := base64.StdEncoding.DecodeString(messageBase64)
if err != nil {
return "", err
}
message := UTF16BytesToString(messageUtf16LeByteArray, binary.LittleEndian)
return message, nil
}