-
Notifications
You must be signed in to change notification settings - Fork 10
/
forToTagLoops.go
143 lines (122 loc) · 3.32 KB
/
forToTagLoops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"go/ast"
"go/token"
log "github.com/sirupsen/logrus"
"golang.org/x/tools/go/ast/astutil"
)
func forToTagLoops(fset *token.FileSet, pkgs map[string]*ast.Package) map[string]string {
funcChangeHistory := make(map[string]string)
// randomize all top level function bodies
for _, pkg := range pkgs {
for _, fileast := range pkg.Files {
convertTopLevelFunctionsBodiesLoops(pkg.Name, fileast, funcChangeHistory)
}
}
return funcChangeHistory
}
func convertTopLevelFunctionsBodiesLoops(pkgName string, fileAst *ast.File, changeHistory map[string]string) {
astutil.Apply(fileAst, func(cr *astutil.Cursor) bool {
stmtType, ok := cr.Node().(*ast.ForStmt)
if !ok {
return true
}
init := stmtType.Init
cond := stmtType.Cond
post := stmtType.Post
body := stmtType.Body
// We only support IfStmt with full params (init,cond,post, and body)
if init == nil || cond == nil || post == nil || body == nil {
return true
}
if *verbose {
log.Printf("For loop detected (init=%v, cond=%v, post=%v, body=%v)", init, cond, post, body)
}
loopInitIdent := ast.NewIdent(fmt.Sprintf("LOOP_INIT_%s", randStringRunes(6)))
loopCondIdent := ast.NewIdent(fmt.Sprintf("LOOP_COND_%s", randStringRunes(6)))
loopBodyIdent := ast.NewIdent(fmt.Sprintf("LOOP_BODY_%s", randStringRunes(6)))
loopEndIdent := ast.NewIdent(fmt.Sprintf("LOOP_END_%s", randStringRunes(6)))
// Convert `break` in body to `goto loopEndIdent` since
// after obfuscation it'll be illegal to use `break`
// outside loop.
astutil.Apply(body, func(crn *astutil.Cursor) bool {
branch, ok := crn.Node().(*ast.BranchStmt)
if !ok {
return true
}
if branch.Tok == token.BREAK {
crn.Replace(&ast.BranchStmt{
Tok: token.GOTO,
Label: loopEndIdent,
})
}
// continue might find more breaks in the body
return true
}, nil)
body.List = append(body.List, post,
&ast.BranchStmt{
Tok: token.GOTO,
Label: loopCondIdent,
})
loopBegStmts := []ast.Stmt{
&ast.BranchStmt{
Tok: token.GOTO,
Label: loopInitIdent,
},
}
loopInitStmts := []ast.Stmt{
&ast.LabeledStmt{
Label: loopInitIdent,
Stmt: &ast.EmptyStmt{},
},
init,
&ast.BranchStmt{
Tok: token.GOTO,
Label: loopCondIdent,
},
}
loopCondStmts := []ast.Stmt{
&ast.LabeledStmt{
Label: loopCondIdent,
Stmt: &ast.IfStmt{
Cond: cond,
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.BranchStmt{
Tok: token.GOTO,
Label: loopBodyIdent,
},
},
},
Else: &ast.BranchStmt{
Tok: token.GOTO,
Label: loopEndIdent,
},
},
},
}
loopBodyStmts := []ast.Stmt{
&ast.LabeledStmt{
Label: loopBodyIdent,
Stmt: body,
},
}
loopEndStmts := []ast.Stmt{
&ast.LabeledStmt{
Label: loopEndIdent,
Stmt: &ast.BlockStmt{},
},
}
ObfuscationBody := []ast.Stmt{}
ObfuscationBody = append(ObfuscationBody, loopBegStmts...)
ObfuscationBody = append(ObfuscationBody, loopInitStmts...)
ObfuscationBody = append(ObfuscationBody, loopCondStmts...)
ObfuscationBody = append(ObfuscationBody, loopBodyStmts...)
ObfuscationBody = append(ObfuscationBody, loopEndStmts...)
cr.Replace(&ast.BlockStmt{
List: ObfuscationBody,
})
return true // replace more fors so we can cover nested ones
}, nil)
}