-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimestamp.go
More file actions
170 lines (134 loc) · 3.99 KB
/
timestamp.go
File metadata and controls
170 lines (134 loc) · 3.99 KB
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
package vtypes
import (
"context"
"fmt"
"io"
"time"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type EpochTimestampOptions struct {
Type string `vfilter:"optional,field=type,doc=The underlying type of the choice"`
TypeOptions *ordereddict.Dict `vfilter:"optional,field=type_options,doc=Any additional options required to parse the type"`
Factor int64 `vfilter:"optional,field=factor,doc=A factor to be applied prior to parsing"`
}
type EpochTimestamp struct {
options EpochTimestampOptions
profile *Profile
parser Parser
}
func (self *EpochTimestamp) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
result := &EpochTimestamp{profile: profile}
ctx := context.Background()
err := ParseOptions(ctx, options, &result.options)
if err != nil {
return nil, fmt.Errorf("EpochTimestamp: %w", err)
}
if result.options.Type == "" {
result.options.Type = "uint64"
}
if result.options.Factor == 0 {
result.options.Factor = 1
}
parser, err := maybeGetParser(profile,
result.options.Type, result.options.TypeOptions)
if err != nil {
return nil, err
}
// Cache the parser for next time.
result.parser = parser
return result, nil
}
func (self *EpochTimestamp) Size() int {
return SizeOf(self.parser)
}
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{}
}
res := time.Unix(value/self.options.Factor,
value%self.options.Factor).UTC()
// Catch invalid timestamps.
_, err := res.MarshalJSON()
if err != nil {
return vfilter.Null{}
}
return res
}
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()
}