From 846ddb4097dae7577455d932a3c1496186fb6985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 28 Jul 2020 22:38:30 +0100 Subject: [PATCH] internal/literals: minor adjustments to the last commits (#77) First, unindent some of the AST code. Second, genRandInt is unused; delete it. Third, genRandIntn is really just mathrand.Intn. Just use it directly. Fourth, don't use inline comments if they result in super long lines. --- internal/literals/literals.go | 1 - internal/literals/obfuscators.go | 12 +--- internal/literals/swap.go | 95 ++++++++++++++++---------------- main_test.go | 1 + 4 files changed, 50 insertions(+), 59 deletions(-) diff --git a/internal/literals/literals.go b/internal/literals/literals.go index 10d5c2d..35edffd 100644 --- a/internal/literals/literals.go +++ b/internal/literals/literals.go @@ -25,7 +25,6 @@ func randObfuscator() obfuscator { // Obfuscate replace literals with obfuscated lambda functions func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, blacklist map[types.Object]struct{}) []*ast.File { pre := func(cursor *astutil.Cursor) bool { - switch x := cursor.Node().(type) { case *ast.GenDecl: if x.Tok != token.CONST { diff --git a/internal/literals/obfuscators.go b/internal/literals/obfuscators.go index 80a8747..7b3cd49 100644 --- a/internal/literals/obfuscators.go +++ b/internal/literals/obfuscators.go @@ -56,18 +56,10 @@ func genRandBytes(buffer []byte) { } } -func genRandInt() int { - return mathrand.Int() -} - -func genRandIntn(max int) int { - return mathrand.Intn(max) -} - -func generateIntSlice(max, count int) []int { +func genRandIntSlice(max, count int) []int { indexes := make([]int, count) for i := 0; i < count; i++ { - indexes[i] = genRandIntn(max) + indexes[i] = mathrand.Intn(max) } return indexes } diff --git a/internal/literals/swap.go b/internal/literals/swap.go index 44db41e..402bd3d 100644 --- a/internal/literals/swap.go +++ b/internal/literals/swap.go @@ -4,6 +4,7 @@ import ( "go/ast" "go/token" "math" + mathrand "math/rand" ) type swap struct{} @@ -44,7 +45,7 @@ func generateSwapCount(dataLen int) int { maxExtraPositions := dataLen / 2 // Limit the number of extra positions to half the data length if maxExtraPositions > 1 { - swapCount += genRandIntn(maxExtraPositions) + swapCount += mathrand.Intn(maxExtraPositions) } if swapCount%2 != 0 { // Swap count must be even swapCount++ @@ -54,12 +55,14 @@ func generateSwapCount(dataLen int) int { func (x swap) obfuscate(data []byte) *ast.BlockStmt { swapCount := generateSwapCount(len(data)) - shiftKey := byte(genRandIntn(math.MaxUint8)) + shiftKey := byte(mathrand.Intn(math.MaxUint8)) - positions := generateIntSlice(len(data), swapCount) + positions := genRandIntSlice(len(data), swapCount) for i := len(positions) - 2; i >= 0; i -= 2 { - localKey := byte(i) + byte(positions[i]^positions[i+1]) + shiftKey // Generate local key for xor based on random key and byte position - data[positions[i]], data[positions[i+1]] = data[positions[i+1]]^localKey, data[positions[i]]^localKey // Swap bytes from i+1 to i and xor using local key + // Generate local key for xor based on random key and byte position + localKey := byte(i) + byte(positions[i]^positions[i+1]) + shiftKey + // Swap bytes from i+1 to i and xor using local key + data[positions[i]], data[positions[i+1]] = data[positions[i+1]]^localKey, data[positions[i]]^localKey } return &ast.BlockStmt{List: []ast.Stmt{ @@ -89,60 +92,56 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt { Tok: token.ADD_ASSIGN, Rhs: []ast.Expr{intLiteral(2)}, }, - Body: &ast.BlockStmt{ - List: []ast.Stmt{ - &ast.AssignStmt{ - Lhs: []ast.Expr{ident("localKey")}, - Tok: token.DEFINE, - Rhs: []ast.Expr{ - &ast.BinaryExpr{ - X: &ast.BinaryExpr{ - X: callExpr(ident("byte"), ident("i")), + Body: &ast.BlockStmt{List: []ast.Stmt{ + &ast.AssignStmt{ + Lhs: []ast.Expr{ident("localKey")}, + Tok: token.DEFINE, + Rhs: []ast.Expr{&ast.BinaryExpr{ + X: &ast.BinaryExpr{ + X: callExpr(ident("byte"), ident("i")), + Op: token.ADD, + Y: callExpr(ident("byte"), &ast.BinaryExpr{ + X: indexExpr("positions", ident("i")), + Op: token.XOR, + Y: indexExpr("positions", &ast.BinaryExpr{ + X: ident("i"), Op: token.ADD, - Y: callExpr(ident("byte"), &ast.BinaryExpr{ - X: indexExpr("positions", ident("i")), - Op: token.XOR, - Y: indexExpr("positions", &ast.BinaryExpr{ - X: ident("i"), - Op: token.ADD, - Y: intLiteral(1), - }), - }), - }, - Op: token.ADD, - Y: intLiteral(int(shiftKey)), - }, + Y: intLiteral(1), + }), + }), }, + Op: token.ADD, + Y: intLiteral(int(shiftKey)), + }}, + }, + &ast.AssignStmt{ + Lhs: []ast.Expr{ + indexExpr("data", indexExpr("positions", ident("i"))), + indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ + X: ident("i"), + Op: token.ADD, + Y: intLiteral(1), + })), }, - &ast.AssignStmt{ - Lhs: []ast.Expr{ - indexExpr("data", indexExpr("positions", ident("i"))), - indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ + Tok: token.ASSIGN, + Rhs: []ast.Expr{ + &ast.BinaryExpr{ + X: indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ X: ident("i"), Op: token.ADD, Y: intLiteral(1), })), + Op: token.XOR, + Y: ident("localKey"), }, - Tok: token.ASSIGN, - Rhs: []ast.Expr{ - &ast.BinaryExpr{ - X: indexExpr("data", indexExpr("positions", &ast.BinaryExpr{ - X: ident("i"), - Op: token.ADD, - Y: intLiteral(1), - })), - Op: token.XOR, - Y: ident("localKey"), - }, - &ast.BinaryExpr{ - X: indexExpr("data", indexExpr("positions", ident("i"))), - Op: token.XOR, - Y: ident("localKey"), - }, + &ast.BinaryExpr{ + X: indexExpr("data", indexExpr("positions", ident("i"))), + Op: token.XOR, + Y: ident("localKey"), }, }, }, - }, + }}, }, }} } diff --git a/main_test.go b/main_test.go index f655a55..60bf863 100644 --- a/main_test.go +++ b/main_test.go @@ -131,6 +131,7 @@ func readFile(ts *testscript.TestScript, file string) string { cachedBinary.content = ts.ReadFile(file) return cachedBinary.content } + func binsubstr(ts *testscript.TestScript, neg bool, args []string) { if len(args) < 2 { ts.Fatalf("usage: binsubstr file substr...")