-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
216 lines (189 loc) · 4.55 KB
/
main.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
package main
import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math"
"github.com/h2non/filetype"
pefile "github.com/wanglei-coder/pefile"
)
var filename string
func init() {
flag.StringVar(&filename, "filename", "", "Please enter the file path")
flag.Parse()
}
type Info struct {
MachineType uint16
EntryPoint uint32
CompilationTime uint32
ImpHash string
RichHeaderHash string
Authentihash string
Exports string
Imports string
Overlay *Overlay
Sections []*Section
ResourceDetails []*ResourceDetail
}
type Overlay struct {
MD5 string
FileType string
Offset uint64
Size int64
Chi2 float64
Entropy float64
}
type Section struct {
Name string
MD5 string
Flags string
RawSize uint32
VirtualAddress uint32
VirtualSize uint32
Entropy float64
}
type ResourceDetail struct {
Language string
Type string
FileType string
SHA256 string
Chi2 float64
Entropy float64
}
func getSections(f *pefile.File) []*Section {
sections := make([]*Section, 0, f.FileHeader.NumberOfSections)
for _, s := range f.Sections {
var section Section
section.Name = s.Name
section.RawSize = s.Size
section.VirtualAddress = s.VirtualAddress
section.VirtualSize = s.VirtualSize
section.Flags = s.Flags()
section.MD5 = s.MD5()
section.Entropy = s.Entropy()
sections = append(sections, §ion)
}
return sections
}
func getResourceDetails(f *pefile.File) []*ResourceDetail {
resourceDetails := make([]*ResourceDetail, 0)
for _, resourceType := range f.Resources.Entries {
resourceTypeName := pefile.GetResourceTypeName(resourceType)
for _, resourceId := range resourceType.Directory.Entries {
for _, resourceLang := range resourceId.Directory.Entries {
rd := new(ResourceDetail)
resourceDetails = append(resourceDetails, rd)
rd.Language = pefile.GetSubLangNameForLang(resourceLang.Data.Lang, resourceLang.Data.SubLang)
rd.Type = resourceTypeName
data, err := f.GetData(resourceLang.Data.Struct.OffsetToData, resourceLang.Data.Struct.Size)
if err != nil {
continue
}
rd.SHA256 = fmt.Sprintf("%x", sha256.Sum256(data))
rd.Entropy = CalculateEntropy(data)
rd.FileType = GetFileType(data)
}
}
}
return resourceDetails
}
func getOverlay(f *pefile.File) *Overlay {
rs := f.GetOverlay()
if rs == nil {
return nil
}
overlay := Overlay{
Offset: uint64(f.OverlayOffset),
Size: int64(f.GetSize()) - f.OverlayOffset,
}
hasher := md5.New()
var entropyCalculator EntropyCalculator
ws := io.MultiWriter(hasher, &entropyCalculator)
_, _ = io.Copy(ws, rs)
overlay.MD5 = hex.EncodeToString(hasher.Sum(nil))
overlay.Entropy = entropyCalculator.Sum()
data := make([]byte, 1024)
_, _ = rs.ReadAt(data, 0)
overlay.FileType = GetFileType(data)
return &overlay
}
func main() {
f, err := pefile.NewFile(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if f.OptionalHeader == nil {
return
}
info := Info{
CompilationTime: f.FileHeader.TimeDateStamp,
MachineType: f.FileHeader.Machine,
RichHeaderHash: f.RichHeaderHash(),
Authentihash: hex.EncodeToString(f.Authentihash()),
Sections: getSections(f),
ResourceDetails: getResourceDetails(f),
Overlay: getOverlay(f),
}
if f.Is64 {
info.EntryPoint = f.OptionalHeader.(*pefile.OptionalHeader64).AddressOfEntryPoint
} else {
info.EntryPoint = f.OptionalHeader.(*pefile.OptionalHeader32).AddressOfEntryPoint
}
info.ImpHash, _ = f.ImpHash()
data, _ := json.MarshalIndent(&info, "", " ")
fmt.Printf("%s\n", data)
}
func GetFileType(data []byte) string {
kind, _ := filetype.Match(data)
if kind == filetype.Unknown {
return "Data"
}
return kind.MIME.Value
}
func CalculateEntropy(data []byte) float64 {
size := float64(len(data))
if size == 0.0 {
return 0.0
}
var frequencies [256]uint64
for _, v := range data {
frequencies[v]++
}
var entropy float64
for _, p := range frequencies {
if p > 0 {
freq := float64(p) / size
entropy += freq * math.Log2(freq)
}
}
return -entropy
}
type EntropyCalculator struct {
size int
frequencies [256]uint64
}
func (e *EntropyCalculator) Write(p []byte) (n int, err error) {
e.size += len(p)
for _, v := range p {
e.frequencies[v]++
}
return len(p), err
}
func (e *EntropyCalculator) Sum() (entropy float64) {
if e.size == 0 {
return
}
for _, p := range e.frequencies {
if p > 0 {
freq := float64(p) / float64(e.size)
entropy += freq * math.Log2(freq)
}
}
return -entropy
}