Repository navigation
Expand file tree
/
Copy pathcontainer.go
More file actions
179 lines (171 loc) · 7.19 KB
/
Copy pathcontainer.go
File metadata and controls
179 lines (171 loc) · 7.19 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
171
172
173
174
175
176
177
178
179
package maldoctrace
import (
"bytes"
"context"
"fmt"
"path"
"strings"
"github.com/debugactiveprocess/MaldocTrace/extractor"
"github.com/debugactiveprocess/MaldocTrace/payload"
"github.com/debugactiveprocess/MaldocTrace/report"
)
// analyzeContainer handles inputs that carry documents rather than being
// one: archives (and, later, emails and notebooks). Every member is listed
// and described, and supported documents are analysed as children.
func analyzeContainer(ctx context.Context, kind string, data []byte, r *report.Report, opts Options) {
switch kind {
case "zip", "7z", "iso":
analyzeArchive(ctx, kind, data, r, opts)
case "eml", "msg":
analyzeEmail(ctx, kind, data, r, opts)
case "onenote":
analyzeOneNote(ctx, data, r, opts)
default:
r.Status = "partial"
r.Macros = "unknown"
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: "warning", Stage: "container", Message: "unsupported container " + kind})
}
}
// analyzeEmail reports the sender, subject and attachments of a message,
// takes indicators from its bodies and analyses every attachment.
func analyzeEmail(ctx context.Context, kind string, data []byte, r *report.Report, opts Options) {
r.Macros = "absent"
var e *extractor.Email
var err error
if kind == "msg" {
e, err = extractor.ParseMSG(data, &opts.Limits)
} else {
e, err = extractor.ParseEML(data)
}
if err != nil {
r.Status = "partial"
r.Macros = "unknown"
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: "warning", Stage: kind, Message: err.Error()})
return
}
for _, d := range e.Diagnostics {
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: d.Severity, Stage: d.Stage, Message: d.Message})
}
r.LimitsHit = append(r.LimitsHit, e.LimitsHit...)
for _, h := range []struct{ name, value string }{{"From", e.From}, {"To", e.To}, {"Subject", e.Subject}, {"Date", e.Date}, {"Message-ID", e.MessageID}} {
if h.value != "" {
r.Properties = append(r.Properties, report.Property{Name: h.name, Value: h.value, Kind: "email", Source: kind + " header"})
}
}
var files []childFile
for i, a := range e.Attachments {
r.Properties = append(r.Properties, report.Property{Name: fmt.Sprintf("Attachment %d", i+1),
Value: fmt.Sprintf("%s (%s, %d bytes)", a.Name, firstOf(a.ContentType, "unknown type"), len(a.Data)), Kind: "email", Source: kind + " attachment"})
files = append(files, childFile{source: "email_attachment", name: a.Name, location: a.Name, class: a.ContentType, data: a.Data})
}
for i, b := range e.Bodies {
scanEvidence(r, b, "static", fmt.Sprintf("email-body-%d", i+1), report.Source{Module: kind + " body"})
}
r.Checks = append(r.Checks, fmt.Sprintf("%s headers, %d body part(s) for indicators, %d attachment(s)", kind, len(e.Bodies), len(e.Attachments)))
processChildren(ctx, r, files, opts)
}
// oneNoteExt names an embedded file from its detected type.
var oneNoteExt = []struct{ prefix, ext string }{
{"HTML application", ".hta"}, {"Windows Script File", ".wsf"}, {"PowerShell", ".ps1"}, {"batch", ".bat"},
{"VBScript", ".vbs"}, {"JScript", ".js"}, {"HTML", ".html"}, {"Windows shortcut", ".lnk"}, {"PE", ".exe"},
{"PDF", ".pdf"}, {"Windows Installer", ".msi"}, {"ISO 9660", ".iso"}, {"OLE/CFB", ".doc"}, {"OOXML", ".docx"}, {"ZIP", ".zip"}, {"Java archive", ".jar"}, {"RTF", ".rtf"},
}
// analyzeOneNote lists the files embedded in a OneNote section and analyses
// them: scripts and executables are reported, documents analysed as children.
func analyzeOneNote(ctx context.Context, data []byte, r *report.Report, opts Options) {
r.Macros = "absent"
files, diags := extractor.OneNoteFiles(data)
for _, d := range diags {
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: d.Severity, Stage: d.Stage, Message: d.Message})
}
var children []childFile
opaque := 0
for i, f := range files {
kind, _ := payload.ClassifyFile("", f.Data)
if kind == "" && isImage(f.Data) {
continue // the lure picture
}
if kind == "" && len(f.Data) >= 4096 && payload.Entropy(f.Data) > 7.9 {
opaque++
}
name := fmt.Sprintf("embedded_%d", i+1)
for _, e := range oneNoteExt {
if strings.HasPrefix(kind, e.prefix) {
name += e.ext
break
}
}
children = append(children, childFile{source: "onenote_file", name: name, location: fmt.Sprintf("FileDataStoreObject at offset %d", f.Offset), data: f.Data})
}
if len(children) > 0 {
addFinding(r, report.StaticFinding{Kind: "onenote_embedded_file", Severity: "medium", Location: "OneNote section", Target: fmt.Sprintf("%d file(s)", len(children)),
Detail: "OneNote section embeds files that open on a double click (lure pictures are often placed over them)"})
}
if opaque > 0 {
// Random-looking data of no known type: a password-protected
// section or an encrypted payload. Nothing in it can be analysed,
// which is itself a reason to treat the file with suspicion.
addFinding(r, report.StaticFinding{Kind: "onenote_encrypted_data", Severity: "medium", Location: "OneNote section", Target: fmt.Sprintf("%d file(s)", opaque),
Detail: "embedded data is encrypted or random (entropy above 7.9 bits/byte) and has no known file type: a password-protected section or an encrypted payload whose content cannot be analysed"})
}
r.Checks = append(r.Checks, fmt.Sprintf("OneNote FileDataStoreObject embedded files (%d, %d not pictures)", len(files), len(children)))
processChildren(ctx, r, children, opts)
}
func isImage(b []byte) bool {
for _, m := range [][]byte{{0x89, 'P', 'N', 'G'}, {0xFF, 0xD8, 0xFF}, []byte("GIF8"), []byte("BM"), {0x01, 0, 0, 0}, {0xD7, 0xCD, 0xC6, 0x9A}} {
if bytes.HasPrefix(b, m) {
return true
}
}
return false
}
func analyzeArchive(ctx context.Context, kind string, data []byte, r *report.Report, opts Options) {
r.Macros = "absent"
a, err := extractor.OpenArchive(data, kind, &opts.Limits)
if err != nil {
r.Status = "partial"
r.Macros = "unknown"
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: "warning", Stage: "archive", Message: err.Error()})
return
}
for _, d := range a.Diagnostics {
r.Diagnostics = append(r.Diagnostics, report.Diagnostic{Severity: d.Severity, Stage: d.Stage, Message: d.Message})
}
r.LimitsHit = append(r.LimitsHit, a.LimitsHit...)
var files []childFile
passwords := map[string]bool{}
unread := 0
for _, m := range a.Members {
if m.Password != "" {
passwords[m.Password] = true
}
if m.Err != "" {
unread++
if m.Encrypted {
r.Encrypted = true
}
continue
}
files = append(files, childFile{source: "archive_member", name: path.Base(strings.ReplaceAll(m.Name, `\`, "/")), location: m.Name, data: m.Data})
}
if len(passwords) > 0 || r.Encrypted {
r.Encrypted = true
var pws []string
for p := range passwords {
pws = append(pws, p)
}
scheme := "ZIP (ZipCrypto or WinZip AES)"
if kind == "7z" {
scheme = "7z AES-256"
}
r.Decryption = &report.Decryption{Scheme: scheme, Decrypted: unread == 0, Password: strings.Join(pws, ", ")}
if unread > 0 {
r.Decryption.Note = fmt.Sprintf("%d member(s) could not be read", unread)
}
}
if unread > 0 {
r.Macros = "unknown"
}
r.Checks = append(r.Checks, fmt.Sprintf("%s archive members (%d read, %d unreadable), analysed in memory", kind, len(files), unread))
processChildren(ctx, r, files, opts)
}