-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
179 lines (150 loc) · 4.01 KB
/
main_test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import "testing"
import "os/exec"
import "strings"
import "encoding/json"
import "fmt"
type JsonOutput struct {
Tid int `json:"tid"`
Addr int `json:"addr"`
Size int `json:"size"`
Opcode string `json:"opcode"`
Lib string `json:"lib"`
Fn string `json:"fn"`
Parameters []string `json:"parameters"`
Values []interface{} `json:"values"`
Return int `json:"return"`
}
type PfPair struct {
Name string
Value int
}
func (self *JsonOutput) CheckFn(fnName string, index int, compare func(b interface{}) bool) error {
if self.Fn != fnName || len(self.Values) < index-1 {
return nil
}
curValue := self.Values[index]
if compare(curValue) == false {
return fmt.Errorf("(%s) invalid: %v", fnName, curValue)
}
return nil
}
func (self *JsonOutput) checkPrintf(chks []PfPair) error {
// if not printf, skip
if self.Fn != "__stdio_common_vfprintf" {
return nil
}
// if not enough values, skip
if len(self.Values) < 5 {
return nil
}
// record observed name and value
s := self.Values[3].(string)
v := int(self.Values[4].(float64))
for _, chk := range chks {
// if the prefix matches, but the value doesn't throw an error
if strings.HasPrefix(s, chk.Name) && v != chk.Value {
return fmt.Errorf("(Printf) %s != %d: %d", chk.Name, chk.Value, v)
}
}
return nil
}
func (self *JsonOutput) checkTerminate() error {
if self.Fn != "TerminateProcess" {
return nil
}
if self.Return > 0 {
return nil
}
return fmt.Errorf("TerminateProcess returned 0")
}
func createJSONLines(fname string) ([]JsonOutput, error) {
cmd := exec.Command("./binee", "-j", fname)
jsonLines := []JsonOutput{}
// if the output errors out then error
out, err := cmd.Output()
if err != nil {
return jsonLines, err
}
// split by newline (and closing curly bracket)
lines := strings.Split(string(out), "}\n")
for _, line := range lines {
// if the line is empty, skip it
if strings.TrimSpace(line) == "" {
continue
}
// otherwise, add back the closing curly bracket
line += "}"
// create the struct and unmarshal
var output JsonOutput
err := json.Unmarshal([]byte(line), &output)
// if there was an error unmarshaling, error out and skip
if err != nil {
return jsonLines, err
}
// otherwise, append it
jsonLines = append(jsonLines, output)
}
return jsonLines, nil
}
func TestConsoleApplication1_x86(t *testing.T) {
jsonLines, err := createJSONLines("tests/ConsoleApplication1_x86.exe")
if err != nil {
t.Error(err)
return
}
// keep track of required names and values
chks := []PfPair{}
chks = append(chks, PfPair{"GENERIC_READ", 2147483648})
chks = append(chks, PfPair{"GENERIC_WRITE", 1073741824})
chks = append(chks, PfPair{"INVALID_HANDLE", 4294967295})
chks = append(chks, PfPair{"CREATE_ALWAYS", 2})
chks = append(chks, PfPair{"FILE_ATTRIBUTE_NORMAL", 128})
chks = append(chks, PfPair{"ERROR_SUCCESS", 0})
for i, output := range jsonLines {
err = output.checkPrintf(chks)
if err != nil {
t.Error(err)
}
if i+1 == len(jsonLines) {
err = output.checkTerminate()
if err != nil {
t.Error(err)
}
}
}
}
func TestConsoleApplication2_x86(t *testing.T) {
jsonLines, err := createJSONLines("tests/ConsoleApplication2_x86.exe")
if err != nil {
t.Error(err)
return
}
chks := []PfPair{}
chks = append(chks, PfPair{"argv", 0})
for _, output := range jsonLines {
err = output.checkPrintf(chks)
if err != nil {
t.Error(err)
}
}
}
func TestCalloc_x86(t *testing.T) {
jsonLines, err := createJSONLines("tests/test_calloc_vs17.exe")
if err != nil {
t.Error(err)
return
}
//func (self *JsonOutput) CheckFn(fnName string, index int, compare func(b interface{}) bool) error {
//if self.Fn != "__stdio_common_vfprintf" {
for _, output := range jsonLines {
if err := output.CheckFn("__stdio_common_vfprintf", 4, func(b interface{}) bool {
if int(b.(float64)) >= 0xa0000000 {
return true
}
return false
}); err != nil {
t.Error(err)
}
}
}