-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimestamp.go
158 lines (124 loc) · 3.57 KB
/
timestamp.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
package vtypes
import (
"fmt"
"io"
"time"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type EpochTimestampOptions struct {
Type string
TypeOptions *ordereddict.Dict
Factor int64
}
type EpochTimestamp struct {
options EpochTimestampOptions
profile *Profile
parser Parser
}
func (self *EpochTimestamp) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
var pres bool
result := &EpochTimestamp{profile: profile}
result.options.Type, pres = options.GetString("type")
if !pres {
result.options.Type = "uint64"
}
topts, pres := options.Get("type_options")
if !pres {
result.options.TypeOptions = ordereddict.NewDict()
} else {
topts_dict, ok := topts.(*ordereddict.Dict)
if !ok {
return nil, fmt.Errorf("Timestamp parser options should be a dict")
}
result.options.TypeOptions = topts_dict
}
result.options.Factor, pres = options.GetInt64("factor")
if !pres {
result.options.Factor = 1
}
return result, nil
}
func (self *EpochTimestamp) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
if self.parser == nil {
parser, err := self.profile.GetParser(
self.options.Type, self.options.TypeOptions)
if err != nil {
scope.Log("ERROR:binary_parser: EpochTimestamp: %v", err)
self.parser = NullParser{}
return vfilter.Null{}
}
// Cache the parser for next time.
self.parser = parser
}
value, ok := to_int64(self.parser.Parse(scope, reader, offset))
if !ok {
return vfilter.Null{}
}
return time.Unix(value/self.options.Factor, value%self.options.Factor).UTC()
}
type WinFileTime struct {
*EpochTimestamp
}
func (self *WinFileTime) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
result, err := self.EpochTimestamp.New(profile, options)
if err != nil {
return nil, err
}
return &WinFileTime{EpochTimestamp: result.(*EpochTimestamp)}, nil
}
func (self *WinFileTime) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
if self.parser == nil {
parser, err := self.profile.GetParser(
self.options.Type, self.options.TypeOptions)
if err != nil {
scope.Log("ERROR:binary_parser: WinFileTime: %v", err)
self.parser = NullParser{}
return vfilter.Null{}
}
// Cache the parser for next time.
self.parser = parser
}
value, ok := to_int64(self.parser.Parse(scope, reader, offset))
if !ok {
return vfilter.Null{}
}
return time.Unix((value/self.options.Factor/10000000)-11644473600, 0).UTC()
}
type FatTimestamp struct {
profile *Profile
}
func (self *FatTimestamp) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
return &FatTimestamp{profile: profile}, nil
}
func (self *FatTimestamp) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
parser, err := self.profile.GetParser("uint32", nil)
if err != nil {
scope.Log("ERROR:binary_parser: FatTimestamp: %v", err)
return vfilter.Null{}
}
date_int, ok := to_int64(parser.Parse(scope, reader, offset))
if !ok {
return vfilter.Null{}
}
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-dosdatetimetofiletime
fat_date := date_int & 0xFFFF
fat_time := date_int >> 16
// Bits 9-15
year := 1980 + (fat_date >> 9)
// Bits 5-8
month := (fat_date >> 5) & ((1 << 4) - 1)
// Bits 0-4
day := fat_date & ((1 << 5) - 1)
// Bits 11 - 15
hour := (fat_time >> 11)
// Bits 5-10
min := (fat_time >> 5) & ((1 << 6) - 1)
// Bits 0-4 divided by 2
sec := (fat_time & ((1 << 5) - 1)) * 2
return time.Date(int(year), time.Month(month), int(day),
int(hour), int(min), int(sec), 0, time.UTC).UTC()
}