Add xorShuffle obfuscator (#85)

* Refactoring

* Rename Xor2 to XorShuffle
pull/88/head
pagran 4 years ago committed by GitHub
parent 0dd97ed0fa
commit 9c25f4c2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,6 +20,7 @@ var (
xor{},
swap{},
split{},
xorShuffle{},
}
envGarbleSeed = os.Getenv("GARBLE_SEED")
)

@ -0,0 +1,62 @@
package literals
import (
"go/ast"
"go/token"
mathrand "math/rand"
ah "mvdan.cc/garble/internal/asthelper"
)
type xorShuffle struct{}
// check that the obfuscator interface is implemented
var _ obfuscator = xorShuffle{}
func (x xorShuffle) obfuscate(data []byte) *ast.BlockStmt {
key := make([]byte, len(data))
genRandBytes(key)
fullData := make([]byte, len(data)+len(key))
for i, b := range key {
fullData[i], fullData[i+len(data)] = data[i]^b, b
}
shuffledIdxs := mathrand.Perm(len(fullData))
shuffledFullData := make([]byte, len(fullData))
for i, b := range fullData {
shuffledFullData[shuffledIdxs[i]] = b
}
args := []ast.Expr{ah.Ident("data")}
for i := range data {
args = append(args, &ast.BinaryExpr{
X: ah.IndexExpr("fullData", ah.IntLit(shuffledIdxs[i])),
Op: token.XOR,
Y: ah.IndexExpr("fullData", ah.IntLit(shuffledIdxs[len(data)+i])),
})
}
return ah.BlockStmt(
&ast.AssignStmt{
Lhs: []ast.Expr{ah.Ident("fullData")},
Tok: token.DEFINE,
Rhs: []ast.Expr{ah.DataToByteSlice(shuffledFullData)},
},
&ast.DeclStmt{
Decl: &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{&ast.ValueSpec{
Names: []*ast.Ident{ah.Ident("data")},
Type: &ast.ArrayType{Elt: ah.Ident("byte")},
}},
},
},
&ast.AssignStmt{
Lhs: []ast.Expr{ah.Ident("data")},
Tok: token.ASSIGN,
Rhs: []ast.Expr{ah.CallExpr(ah.Ident("append"), args...)},
},
)
}

@ -45,6 +45,9 @@ grep '^\s+\w+ := \[\.{3}\](byte|uint16|uint32|uint64)\{[0-9\s,]+\}$' .obf-src/ma
# Split obfuscator. Detect decryptKey ^= i * counter
grep '^\s+\w+ \^= \w+ \* \w+$' .obf-src/main/z0.go
# XorShuffle obfuscator. Detect data = append(data, x ^ y...)
grep '^\s+\w+ = append\(\w+,(\s+\w+\[\d+\]\^\w+\[\d+\],?)+\)$' .obf-src/main/z0.go
-- go.mod --
module test/main

Loading…
Cancel
Save