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.
pull/78/head
Daniel Martí 4 years ago committed by GitHub
parent 14a19b3e6b
commit 846ddb4097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 {

@ -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
}

@ -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"),
},
},
},
},
}},
},
}}
}

@ -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...")

Loading…
Cancel
Save