@@ -84,41 +84,43 @@ func (typ ProcessorType) String() string {
84
84
}
85
85
86
86
// parseImageFileCharacteristics parses the PE structure for the file path
87
- // residing in the given event parameters. The preferred method for getting
88
- // the file data is accessing the raw device and consuming the blob data.
89
- // If this operation fails, we fallback to using the regular file access.
87
+ // residing in the given event parameters. The preferred method for reading
88
+ // the PE metadata is by directly accessing the file.
89
+ // If this operation fails, the file data is read form the raw device and
90
+ // the blob is passed to the PE parser.
90
91
// The given event is decorated with various parameters extracted from PE
91
92
// data. Most notably, parameters that indicate whether the file is a DLL,
92
93
// executable image, or a Windows driver.
93
94
func parseImageFileCharacteristics (e * kevent.Kevent ) error {
95
+ var pefile * pe.PE
94
96
filename := e .GetParamAsString (kparams .FileName )
95
- data := make ([]byte , os .Getpagesize ())
96
97
f , err := os .Open (filename )
97
98
if err != nil {
98
99
// read file data blob from raw device
99
100
// if the regular file access fails
100
101
ntfs := libntfs .NewFS ()
101
- var n int
102
- data , n , err = ntfs .Read (filename , 0 , int64 (os .Getpagesize ()))
102
+ data , n , err := ntfs .Read (filename , 0 , int64 (os .Getpagesize ()))
103
103
defer ntfs .Close ()
104
104
if err != nil {
105
105
return err
106
106
}
107
107
if n > 0 {
108
108
data = data [:n ]
109
109
}
110
- goto parsePe
111
- }
112
- defer f .Close ()
113
- if _ , err = f .Read (data ); err != nil {
114
- return err
115
- }
116
- parsePe:
117
- // parse image file
118
- pefile , err := pe .ParseBytes (data , pe .WithSections (), pe .WithSymbols ())
119
- if err != nil {
120
- return err
110
+ // parse PE file from byte slice
111
+ pefile , err = pe .ParseBytes (data , pe .WithSections (), pe .WithSymbols ())
112
+ if err != nil {
113
+ return err
114
+ }
115
+ } else {
116
+ defer f .Close ()
117
+ // parse PE file from on-disk file
118
+ pefile , err = pe .ParseFile (filename , pe .WithSections (), pe .WithSymbols ())
119
+ if err != nil {
120
+ return err
121
+ }
121
122
}
123
+
122
124
// append parameters
123
125
e .AppendParam (kparams .FileIsDLL , kparams .Bool , pefile .IsDLL )
124
126
e .AppendParam (kparams .FileIsDriver , kparams .Bool , pefile .IsDriver )
0 commit comments