You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.2 KiB
Go
109 lines
2.2 KiB
Go
5 years ago
|
package literals
|
||
|
|
||
|
import (
|
||
|
"go/ast"
|
||
|
"go/token"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
type swap struct{}
|
||
|
|
||
|
// check that the obfuscator interface is implemented
|
||
|
var _ obfuscator = swap{}
|
||
|
|
||
|
func dataToIntSlice(data []int) *ast.CompositeLit {
|
||
|
arr := &ast.CompositeLit{
|
||
|
Type: &ast.ArrayType{
|
||
|
Len: &ast.Ellipsis{}, // Performance optimization
|
||
|
Elt: &ast.Ident{
|
||
|
Name: "int",
|
||
|
},
|
||
|
},
|
||
|
Elts: []ast.Expr{},
|
||
|
}
|
||
|
for _, data := range data {
|
||
|
arr.Elts = append(arr.Elts, intLiteral(strconv.Itoa(data)))
|
||
|
}
|
||
|
return arr
|
||
|
}
|
||
|
|
||
|
func (x swap) obfuscate(data []byte) *ast.BlockStmt {
|
||
|
maxJunkIdxCount := len(data) / 2
|
||
|
if maxJunkIdxCount == 0 {
|
||
|
maxJunkIdxCount = 1
|
||
|
}
|
||
|
count := len(data) + genRandIntn(maxJunkIdxCount)
|
||
|
if count%2 != 0 {
|
||
|
count++
|
||
|
}
|
||
|
indexes := generateIntSlice(len(data), count)
|
||
|
for i := len(indexes) - 2; i >= 0; i -= 2 {
|
||
|
data[indexes[i]], data[indexes[i+1]] = data[indexes[i+1]], data[indexes[i]]
|
||
|
}
|
||
|
|
||
|
return &ast.BlockStmt{List: []ast.Stmt{
|
||
|
&ast.AssignStmt{
|
||
|
Lhs: []ast.Expr{ident("data")},
|
||
|
Tok: token.DEFINE,
|
||
|
Rhs: []ast.Expr{dataToByteSlice(data)},
|
||
|
},
|
||
|
&ast.AssignStmt{
|
||
|
Lhs: []ast.Expr{
|
||
|
ident("indexes"),
|
||
|
},
|
||
|
Tok: token.DEFINE,
|
||
|
Rhs: []ast.Expr{
|
||
|
dataToIntSlice(indexes),
|
||
|
},
|
||
|
},
|
||
|
&ast.ForStmt{
|
||
|
Init: &ast.AssignStmt{
|
||
|
Lhs: []ast.Expr{
|
||
|
ident("i"),
|
||
|
},
|
||
|
Tok: token.DEFINE,
|
||
|
Rhs: []ast.Expr{
|
||
|
intLiteral("0"),
|
||
|
},
|
||
|
},
|
||
|
Cond: &ast.BinaryExpr{
|
||
|
X: ident("i"),
|
||
|
Op: token.LSS,
|
||
|
Y: intLiteral(strconv.Itoa(len(indexes))),
|
||
|
},
|
||
|
Post: &ast.AssignStmt{
|
||
|
Lhs: []ast.Expr{
|
||
|
ident("i"),
|
||
|
},
|
||
|
Tok: token.ADD_ASSIGN,
|
||
|
Rhs: []ast.Expr{
|
||
|
intLiteral("2"),
|
||
|
},
|
||
|
},
|
||
|
Body: &ast.BlockStmt{
|
||
|
List: []ast.Stmt{
|
||
|
&ast.AssignStmt{
|
||
|
Lhs: []ast.Expr{
|
||
|
indexExpr("data", indexExpr("indexes", ident("i"))),
|
||
|
indexExpr("data", indexExpr("indexes", &ast.BinaryExpr{
|
||
|
X: ident("i"),
|
||
|
Op: token.ADD,
|
||
|
Y: intLiteral("1"),
|
||
|
})),
|
||
|
},
|
||
|
Tok: token.ASSIGN,
|
||
|
Rhs: []ast.Expr{
|
||
|
indexExpr("data", indexExpr("indexes", &ast.BinaryExpr{
|
||
|
X: ident("i"),
|
||
|
Op: token.ADD,
|
||
|
Y: intLiteral("1"),
|
||
|
})),
|
||
|
indexExpr("data", indexExpr("indexes", ident("i"))),
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}}
|
||
|
}
|