forked from AdaLogics/go-fuzz-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.go
253 lines (227 loc) · 5.71 KB
/
seed.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gofuzzheaders
import (
"bytes"
//"fmt"
"reflect"
//"unsafe"
"strings"
)
type SeedGenerator struct {
curDepth int
b *bytes.Buffer
}
func NewSeedGenerator() *SeedGenerator {
by := make([]byte, 0)
b := bytes.NewBuffer(by)
return &SeedGenerator{
b: b,
}
}
func (f *SeedGenerator) GenerateSeed(targetStruct interface{}) ([]byte) {
f.GenerateStruct(targetStruct)
return f.b.Bytes()
}
func (f *SeedGenerator) GenerateStruct(targetStruct interface{}) error {
e := reflect.ValueOf(targetStruct).Elem()
f.fuzzStruct(e, false)
return nil
}
func (f *SeedGenerator) fuzzStruct(e reflect.Value, customFunctions bool) error {
if f.curDepth >= maxDepth {
// return err or nil here?
return nil
}
f.curDepth++
defer func() { f.curDepth-- }()
switch e.Kind() {
case reflect.Struct:
for i := 0; i < e.NumField(); i++ {
//fmt.Println("Field:")
if i == 30 {
//fmt.Println(f.b.Bytes())
//panic("Stop here")
}
var v reflect.Value
//fmt.Printf("%s: \n", e.Type().Field(i).Name)
jsonTag := e.Type().Field(i).Tag.Get("json")
if strings.Contains(jsonTag, ",omitempty") {
// do not skip:
f.b.Write([]byte{0x01})
}
if !e.Field(i).CanSet() {
if err := f.fuzzStruct(v, customFunctions); err != nil {
return err
}
} else {
v = e.Field(i)
if err := f.fuzzStruct(v, customFunctions); err != nil {
return err
}
}
}
case reflect.String:
_, err := f.GetString()
if err != nil {
return err
}
case reflect.Slice:
var maxElements uint32
// Byte slices should not be restricted
if e.Type().String() == "[]uint8" {
maxElements = 10000000
} else {
maxElements = 50
}
// Need uint32(2)
randQty, err := f.GetUint32()
if err != nil {
return err
}
numOfElements := randQty % maxElements
uu := reflect.MakeSlice(e.Type(), int(numOfElements), int(numOfElements))
for i := 0; i < int(numOfElements); i++ {
// If we have more than 10, then we can proceed with that.
if err := f.fuzzStruct(uu.Index(i), customFunctions); err != nil {
}
}
if e.CanSet() {
e.Set(uu)
}
case reflect.Uint16:
_, err := f.GetUint16()
if err != nil {
return err
}
case reflect.Uint32:
_, err := f.GetUint32()
if err != nil {
return err
}
case reflect.Uint64:
_, err := f.GetInt()
if err != nil {
return err
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
_, err := f.GetInt()
if err != nil {
return err
}
case reflect.Float32:
_, err := f.GetFloat32()
if err != nil {
return err
}
case reflect.Float64:
_, err := f.GetFloat64()
if err != nil {
return err
}
case reflect.Map:
if e.CanSet() {
e.Set(reflect.MakeMap(e.Type()))
const maxElements = 50
// int(2):
f.b.Write([]byte{0x02})
randQty := 2
numOfElements := randQty % maxElements
for i := 0; i < numOfElements; i++ {
if i == 2 {
}
key := reflect.New(e.Type().Key()).Elem()
if err := f.fuzzStruct(key, customFunctions); err != nil {
return err
}
val := reflect.New(e.Type().Elem()).Elem()
if err := f.fuzzStruct(val, customFunctions); err != nil {
return err
}
}
}
case reflect.Ptr:
if e.CanSet() {
e.Set(reflect.New(e.Type().Elem()))
if err := f.fuzzStruct(e.Elem(), customFunctions); err != nil {
return err
}
return nil
}
case reflect.Uint8:
_, err := f.GetByte()
if err != nil {
return err
}
}
return nil
}
func (f *SeedGenerator) GetStringArray() (reflect.Value, error) {
return reflect.Value{}, nil
}
func (f *SeedGenerator) GetInt() (int, error) {
//int(5):
f.b.Write([]byte{0x35}) // 5
return int(byte(0x35)), nil
}
func (f *SeedGenerator) GetByte() (byte, error) {
// "a":
f.b.Write([]byte{0x61})
return byte(0x61), nil
}
// Not used for Structs
func (f *SeedGenerator) GetNBytes(numberOfBytes int) ([]byte, error) {
return []byte{0x00}, nil
}
func (f *SeedGenerator) GetUint16() (uint16, error) {
// should be uint16(2)
f.b.Write([]byte{0x00, 0x02})
return uint16(2), nil
}
func (f *SeedGenerator) GetUint32() (uint32, error) {
// should be uint32(2)
f.b.Write([]byte{0x00, 0x00, 0x00, 0x02})
return uint32(2), nil
}
func (f *SeedGenerator) GetUint64() (uint64, error) {
// should be uint32(2)
f.b.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02})
return uint64(2), nil
}
func (f *SeedGenerator) GetBytes() ([]byte, error) {
f.b.Write([]byte{0x00, 0x00, 0x00, 0x03}) // length
f.b.Write([]byte{0x41,0x42,0x43})
return []byte{0x00}, nil
}
func (f *SeedGenerator) GetString() (string, error) {
//fmt.Println("Writing string")
f.b.Write([]byte{0x00, 0x00, 0x00, 0x03}) // length
f.b.Write([]byte{0x41,0x42,0x43})
return "ABC", nil
}
func (f *SeedGenerator) GetBool() (bool, error) {
// true:
f.b.Write([]byte{0x00})
return true, nil
}
// Not needed
func (f *SeedGenerator) FuzzMap(m interface{}) error { return nil }
func (f *SeedGenerator) TarBytes() ([]byte, error) { return []byte{0x00}, nil }
func (f *SeedGenerator) TarFiles() ([]*TarFile, error) {
return []*TarFile{nil}, nil
}
func (f *SeedGenerator) CreateFiles(rootDir string) error { return nil }
func (f *SeedGenerator) GetStringFrom(possibleChars string, length int) (string, error) { return "", nil }
func (f *SeedGenerator) GetRune() ([]rune, error) {
s := ""
return []rune(s), nil
}
func (f *SeedGenerator) GetFloat32() (float32, error) {
f.b.Write([]byte{0x00, 0x00, 0x00, 0x04})
f.b.Write([]byte{0x01}) // little endian: false
return float32(0), nil
}
func (f *SeedGenerator) GetFloat64() (float64, error) {
f.b.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04})
f.b.Write([]byte{0x01}) // little endian: false
return float64(0), nil
}
func (f *SeedGenerator) CreateSlice(targetSlice interface{}) error { return nil }