Skip to content

Find, verify, and analyze leaked credentials

Add utf16 decoder (#1274)

ahrav4116a24b1c3c
diff --git a/pkg/decoders/decoders.go b/pkg/decoders/decoders.go
index d069ee551..8a98dc0c4 100644
--- a/pkg/decoders/decoders.go
+++ b/pkg/decoders/decoders.go
@@ -8,6 +8,7 @@ func DefaultDecoders() []Decoder {
return []Decoder{
&UTF8{},
&Base64{},
+ &UTF16{},
}
}
diff --git a/pkg/decoders/utf16.go b/pkg/decoders/utf16.go
new file mode 100644
index 000000000..c3bc8fbbc
--- /dev/null
+++ b/pkg/decoders/utf16.go
@@ -0,0 +1,68 @@
+package decoders
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "unicode/utf8"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
+)
+
+type UTF16 struct{}
+
+func (d *UTF16) FromChunk(chunk *sources.Chunk) *sources.Chunk {
+ if chunk == nil || len(chunk.Data) == 0 {
+ return nil
+ }
+
+ if utf16Data, err := utf16ToUTF8(chunk.Data); err == nil {
+ chunk.Data = utf16Data
+ return chunk
+ }
+
+ return nil
+}
+
+// utf16ToUTF8 converts a byte slice containing UTF-16 encoded data to a UTF-8 encoded byte slice.
+func utf16ToUTF8(b []byte) ([]byte, error) {
+ endianness, err := guessUTF16Endianness(b)
+ if err != nil {
+ return nil, err
+ }
+
+ buf := &bytes.Buffer{}
+ for i := 0; i < len(b); i += 2 {
+ r := rune(endianness.Uint16(b[i:]))
+ if utf8.ValidRune(r) {
+ buf.WriteRune(r)
+ }
+ }
+
+ return buf.Bytes(), nil
+}
+
+func guessUTF16Endianness(b []byte) (binary.ByteOrder, error) {
+ if len(b) < 2 || len(b)%2 != 0 {
+ return nil, fmt.Errorf("input length must be even and at least 2 bytes long")
+ }
+
+ var evenNullBytes, oddNullBytes int
+
+ for i := 0; i < len(b); i += 2 {
+ if b[i] == 0 {
+ oddNullBytes++
+ }
+ if b[i+1] == 0 {
+ evenNullBytes++
+ }
+ }
+
+ if evenNullBytes > oddNullBytes {
+ return binary.LittleEndian, nil
+ }
+ if oddNullBytes > evenNullBytes {
+ return binary.BigEndian, nil
+ }
+ return nil, fmt.Errorf("could not determine endianness")
+}
diff --git a/pkg/decoders/utf16_test.go b/pkg/decoders/utf16_test.go
new file mode 100644
index 000000000..92c13dd8d
--- /dev/null
+++ b/pkg/decoders/utf16_test.go
@@ -0,0 +1,73 @@
+package decoders
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
+)
+
+func TestUTF16Decoder(t *testing.T) {
+ testCases := []struct {
+ name string
+ input []byte
+ expected []byte
+ expectNil bool
+ }{
+ {
+ name: "Valid UTF-16LE input",
+ input: []byte{72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0},
+ expected: []byte("Hello World"),
+ expectNil: false,
+ },
+ {
+ name: "Valid UTF-16BE input",
+ input: []byte{0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100},
+ expected: []byte("Hello World"),
+ expectNil: false,
+ },
+ {
+ name: "Invalid UTF-16 input (it's UTF-8)",
+ input: []byte("Hello World!"),
+ expected: nil,
+ expectNil: true,
+ },
+ {
+ name: "Invalid UTF-16 input (odd length)",
+ input: []byte{72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 0},
+ expected: nil,
+ expectNil: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ chunk := &sources.Chunk{Data: tc.input}
+ decoder := &UTF16{}
+ decodedChunk := decoder.FromChunk(chunk)
+
+ if tc.expectNil {
+ if decodedChunk != nil {
+ t.Errorf("Expected nil, got chunk with data: %v", decodedChunk.Data)
+ }
+ return
+ }
+ if decodedChunk == nil {
+ t.Errorf("Expected chunk with data, got nil")
+ return
+ }
+ if !bytes.Equal(decodedChunk.Data, tc.expected) {
+ t.Errorf("Expected decoded data: %v, got: %v", tc.expected, decodedChunk.Data)
+ }
+ })
+ }
+}
+
+func BenchmarkUtf16ToUtf8(b *testing.B) {
+ // Example UTF-16LE encoded data
+ data := []byte{72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0}
+
+ for n := 0; n < b.N; n++ {
+ _, _ = utf16ToUTF8(data)
+ }
+}
diff --git a/pkg/decoders/utf8_test.go b/pkg/decoders/utf8_test.go
index 283195e66..18205d327 100644
--- a/pkg/decoders/utf8_test.go
+++ b/pkg/decoders/utf8_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/kylelemons/godebug/pretty"
+
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)