Debian Codegraph @

internal/magic/ms_office.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 )
7
8 // Xlsx matches a Microsoft Excel 2007 file.
9 func Xlsx(raw []byte, limit uint32) bool {
10 return msoxml(raw, zipEntries{{
11 name: []byte("xl/"),
12 dir: true,
13 }}, 100)
14 }
15
16 // Docx matches a Microsoft Word 2007 file.
17 func Docx(raw []byte, limit uint32) bool {
18 return msoxml(raw, zipEntries{{
19 name: []byte("word/"),
20 dir: true,
21 }}, 100)
22 }
23
24 // Pptx matches a Microsoft PowerPoint 2007 file.
25 func Pptx(raw []byte, limit uint32) bool {
26 return msoxml(raw, zipEntries{{
27 name: []byte("ppt/"),
28 dir: true,
29 }}, 100)
30 }
31
32 // Visio matches a Microsoft Visio 2013+ file.
33 func Visio(raw []byte, limit uint32) bool {
34 return msoxml(raw, zipEntries{{
35 name: []byte("visio/"),
36 dir: true,
37 }}, 100)
38 }
39
40 // Ole matches an Open Linking and Embedding file.
41 //
42 // https://en.wikipedia.org/wiki/Object_Linking_and_Embedding
43 func Ole(raw []byte, limit uint32) bool {
44 return bytes.HasPrefix(raw, []byte{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1})
45 }
46
47 // Doc matches a Microsoft Word 97-2003 file.
48 // See: https://github.com/decalage2/oletools/blob/412ee36ae45e70f42123e835871bac956d958461/oletools/common/clsid.py
49 func Doc(raw []byte, _ uint32) bool {
50 clsids := [][]byte{
51 // Microsoft Word 97-2003 Document (Word.Document.8)
52 {0x06, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
53 // Microsoft Word 6.0-7.0 Document (Word.Document.6)
54 {0x00, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
55 // Microsoft Word Picture (Word.Picture.8)
56 {0x07, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
57 }
58
59 for _, clsid := range clsids {
60 if matchOleClsid(raw, clsid) {
61 return true
62 }
63 }
64
65 return false
66 }
67
68 // Ppt matches a Microsoft PowerPoint 97-2003 file or a PowerPoint 95 presentation.
69 func Ppt(raw []byte, limit uint32) bool {
70 // Root CLSID test is the safest way to detect identify OLE, however, the format
71 // often places the root CLSID at the end of the file.
72 if matchOleClsid(raw, []byte{
73 0x10, 0x8d, 0x81, 0x64, 0x9b, 0x4f, 0xcf, 0x11,
74 0x86, 0xea, 0x00, 0xaa, 0x00, 0xb9, 0x29, 0xe8,
75 }) || matchOleClsid(raw, []byte{
76 0x70, 0xae, 0x7b, 0xea, 0x3b, 0xfb, 0xcd, 0x11,
77 0xa9, 0x03, 0x00, 0xaa, 0x00, 0x51, 0x0e, 0xa3,
78 }) {
79 return true
80 }
81
82 lin := len(raw)
83 if lin < 520 {
84 return false
85 }
86 pptSubHeaders := [][]byte{
87 {0xA0, 0x46, 0x1D, 0xF0},
88 {0x00, 0x6E, 0x1E, 0xF0},
89 {0x0F, 0x00, 0xE8, 0x03},
90 }
91 for _, h := range pptSubHeaders {
92 if bytes.HasPrefix(raw[512:], h) {
93 return true
94 }
95 }
96
97 if bytes.HasPrefix(raw[512:], []byte{0xFD, 0xFF, 0xFF, 0xFF}) &&
98 raw[518] == 0x00 && raw[519] == 0x00 {
99 return true
100 }
101
102 return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)],
103 []byte("P\x00o\x00w\x00e\x00r\x00P\x00o\x00i\x00n\x00t\x00 D\x00o\x00c\x00u\x00m\x00e\x00n\x00t"))
104 }
105
106 // Xls matches a Microsoft Excel 97-2003 file.
107 func Xls(raw []byte, limit uint32) bool {
108 // Root CLSID test is the safest way to detect identify OLE, however, the format
109 // often places the root CLSID at the end of the file.
110 if matchOleClsid(raw, []byte{
111 0x10, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
112 }) || matchOleClsid(raw, []byte{
113 0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
114 }) {
115 return true
116 }
117
118 lin := len(raw)
119 if lin < 520 {
120 return false
121 }
122 xlsSubHeaders := [][]byte{
123 {0x09, 0x08, 0x10, 0x00, 0x00, 0x06, 0x05, 0x00},
124 {0xFD, 0xFF, 0xFF, 0xFF, 0x10},
125 {0xFD, 0xFF, 0xFF, 0xFF, 0x1F},
126 {0xFD, 0xFF, 0xFF, 0xFF, 0x22},
127 {0xFD, 0xFF, 0xFF, 0xFF, 0x23},
128 {0xFD, 0xFF, 0xFF, 0xFF, 0x28},
129 {0xFD, 0xFF, 0xFF, 0xFF, 0x29},
130 }
131 for _, h := range xlsSubHeaders {
132 if bytes.HasPrefix(raw[512:], h) {
133 return true
134 }
135 }
136
137 return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)],
138 []byte("W\x00k\x00s\x00S\x00S\x00W\x00o\x00r\x00k\x00B\x00o\x00o\x00k"))
139 }
140
141 // Pub matches a Microsoft Publisher file.
142 func Pub(raw []byte, limit uint32) bool {
143 return matchOleClsid(raw, []byte{
144 0x01, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
146 })
147 }
148
149 // Msg matches a Microsoft Outlook email file.
150 func Msg(raw []byte, limit uint32) bool {
151 return matchOleClsid(raw, []byte{
152 0x0B, 0x0D, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
154 })
155 }
156
157 // Msi matches a Microsoft Windows Installer file.
158 // http://fileformats.archiveteam.org/wiki/Microsoft_Compound_File
159 func Msi(raw []byte, limit uint32) bool {
160 return matchOleClsid(raw, []byte{
161 0x84, 0x10, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
163 })
164 }
165
166 // One matches a Microsoft OneNote file.
167 func One(raw []byte, limit uint32) bool {
168 return bytes.HasPrefix(raw, []byte{
169 0xe4, 0x52, 0x5c, 0x7b, 0x8c, 0xd8, 0xa7, 0x4d,
170 0xae, 0xb1, 0x53, 0x78, 0xd0, 0x29, 0x96, 0xd3,
171 })
172 }
173
174 // Helper to match by a specific CLSID of a compound file.
175 //
176 // http://fileformats.archiveteam.org/wiki/Microsoft_Compound_File
177 func matchOleClsid(in []byte, clsid []byte) bool {
178 // Microsoft Compound files v3 have a sector length of 512, while v4 has 4096.
179 // Change sector offset depending on file version.
180 // https://www.loc.gov/preservation/digital/formats/fdd/fdd000392.shtml
181 sectorLength := 512
182 if len(in) < sectorLength {
183 return false
184 }
185 if in[26] == 0x04 && in[27] == 0x00 {
186 sectorLength = 4096
187 }
188
189 // SecID of first sector of the directory stream.
190 firstSecID := int(binary.LittleEndian.Uint32(in[48:52]))
191
192 // Expected offset of CLSID for root storage object.
193 clsidOffset := sectorLength*(1+firstSecID) + 80
194
195 // #731 offset is outside in or wrapped around due to integer overflow.
196 if len(in) <= clsidOffset+16 || clsidOffset < 0 {
197 return false
198 }
199
200 return bytes.HasPrefix(in[clsidOffset:], clsid)
201 }
202
203 // WPD matches a WordPerfect document.
204 func WPD(raw []byte, _ uint32) bool {
205 if len(raw) < 10 {
206 return false
207 }
208 if !bytes.HasPrefix(raw, []byte("\xffWPC")) {
209 return false
210 }
211 return raw[8] == 1 && raw[9] == 10
212 }