| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package magic |
| 18 | |
| 19 | import ( |
| 20 | "archive/zip" |
| 21 | "bufio" |
| 22 | "bytes" |
| 23 | "compress/gzip" |
| 24 | "encoding/binary" |
| 25 | "errors" |
| 26 | "io" |
| 27 | "os" |
| 28 | "path" |
| 29 | "strings" |
| 30 | |
| 31 | "github.com/xi2/xz" |
| 32 | ) |
| 33 | |
| 34 | type FileType int |
| 35 | type CompressionType int |
| 36 | |
| 37 | const ( |
| 38 | FileTypeUnknown FileType = iota |
| 39 | FileTypeRPM |
| 40 | FileTypeDEB |
| 41 | FileTypePGP |
| 42 | FileTypeJAR |
| 43 | FileTypePKCS7 |
| 44 | FileTypePECOFF |
| 45 | FileTypeMSI |
| 46 | FileTypeCAB |
| 47 | FileTypeAppManifest |
| 48 | FileTypeCAT |
| 49 | FileTypeAPPX |
| 50 | FileTypeVSIX |
| 51 | FileTypeXAP |
| 52 | FileTypeAPK |
| 53 | FileTypeMachO |
| 54 | FileTypeMachOFat |
| 55 | FileTypeIPA |
| 56 | FileTypeXAR |
| 57 | ) |
| 58 | |
| 59 | const ( |
| 60 | CompressedNone CompressionType = iota |
| 61 | CompressedGzip |
| 62 | CompressedXz |
| 63 | ) |
| 64 | |
| 65 | func hasPrefix(br *bufio.Reader, blob []byte) bool { |
| 66 | return atPosition(br, blob, 0) |
| 67 | } |
| 68 | |
| 69 | func contains(br *bufio.Reader, blob []byte, n int) bool { |
| 70 | d, _ := br.Peek(n) |
| 71 | if len(d) < len(blob) { |
| 72 | return false |
| 73 | } |
| 74 | return bytes.Contains(d, blob) |
| 75 | } |
| 76 | |
| 77 | func atPosition(br *bufio.Reader, blob []byte, n int) bool { |
| 78 | l := n + len(blob) |
| 79 | d, _ := br.Peek(l) |
| 80 | if len(d) < l { |
| 81 | return false |
| 82 | } |
| 83 | return bytes.Equal(d[n:], blob) |
| 84 | } |
| 85 | |
| 86 | // Detect a handful of package and signature file types based on the first few |
| 87 | // bytes of the file contents. |
| 88 | func Detect(r io.Reader) FileType { |
| 89 | br := bufio.NewReader(r) |
| 90 | switch { |
| 91 | case hasPrefix(br, []byte{0xed, 0xab, 0xee, 0xdb}): |
| 92 | return FileTypeRPM |
| 93 | case hasPrefix(br, []byte("!<arch>\ndebian")): |
| 94 | return FileTypeDEB |
| 95 | case hasPrefix(br, []byte("-----BEGIN PGP")): |
| 96 | return FileTypePGP |
| 97 | case contains(br, []byte{0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0A, 0x01}, 256): |
| 98 | // OID certTrustList |
| 99 | return FileTypeCAT |
| 100 | case contains(br, []byte{0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02}, 256): |
| 101 | // OID signedData |
| 102 | return FileTypePKCS7 |
| 103 | case isTar(br): |
| 104 | return detectTar(br) |
| 105 | case hasPrefix(br, []byte("MZ")): |
| 106 | if blob, _ := br.Peek(0x3e); len(blob) == 0x3e { |
| 107 | reloc := binary.LittleEndian.Uint16(blob[0x3c:0x3e]) |
| 108 | if blob, err := br.Peek(int(reloc) + 4); err == nil { |
| 109 | if bytes.Equal(blob[reloc:reloc+4], []byte("PE\x00\x00")) { |
| 110 | return FileTypePECOFF |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | case hasPrefix(br, []byte{0xd0, 0xcf}): |
| 115 | return FileTypeMSI |
| 116 | case hasPrefix(br, []byte("MSCF")): |
| 117 | return FileTypeCAB |
| 118 | case contains(br, []byte("<assembly"), 256), |
| 119 | contains(br, []byte(":assembly"), 256): |
| 120 | return FileTypeAppManifest |
| 121 | case hasPrefix(br, []byte{0xcf, 0xfa, 0xed, 0xfe}), hasPrefix(br, []byte{0xce, 0xfa, 0xed, 0xfe}): |
| 122 | return FileTypeMachO |
| 123 | case hasPrefix(br, []byte{0xca, 0xfe, 0xba, 0xbe}): |
| 124 | return FileTypeMachOFat |
| 125 | case hasPrefix(br, []byte{0x78, 0x61, 0x72, 0x21}): |
| 126 | return FileTypeXAR |
| 127 | case hasPrefix(br, []byte{0x89}), hasPrefix(br, []byte{0xc2}), hasPrefix(br, []byte{0xc4}): |
| 128 | return FileTypePGP |
| 129 | } |
| 130 | return FileTypeUnknown |
| 131 | } |
| 132 | |
| 133 | func DetectCompressed(f *os.File) (FileType, CompressionType) { |
| 134 | br := bufio.NewReader(f) |
| 135 | ftype := FileTypeUnknown |
| 136 | switch { |
| 137 | case hasPrefix(br, []byte{0x1f, 0x8b}): |
| 138 | zr, err := gzip.NewReader(br) |
| 139 | if err == nil { |
| 140 | zbr := bufio.NewReader(zr) |
| 141 | if isTar(zbr) { |
| 142 | ftype = detectTar(zbr) |
| 143 | } |
| 144 | } |
| 145 | return ftype, CompressedGzip |
| 146 | case hasPrefix(br, []byte("\xfd7zXZ\x00")): |
| 147 | zr, err := xz.NewReader(br, 0) |
| 148 | if err == nil { |
| 149 | zbr := bufio.NewReader(zr) |
| 150 | if isTar(zbr) { |
| 151 | ftype = detectTar(zbr) |
| 152 | } |
| 153 | } |
| 154 | return ftype, CompressedXz |
| 155 | case hasPrefix(br, []byte{0x50, 0x4b, 0x03, 0x04}): |
| 156 | return detectZip(f), CompressedNone |
| 157 | } |
| 158 | return Detect(br), CompressedNone |
| 159 | } |
| 160 | |
| 161 | func Decompress(r io.Reader, ctype CompressionType) (io.Reader, error) { |
| 162 | switch ctype { |
| 163 | case CompressedNone: |
| 164 | return r, nil |
| 165 | case CompressedGzip: |
| 166 | return gzip.NewReader(r) |
| 167 | case CompressedXz: |
| 168 | return xz.NewReader(r, 0) |
| 169 | default: |
| 170 | return nil, errors.New("invalid compression type") |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func isTar(br *bufio.Reader) bool { |
| 175 | return atPosition(br, []byte("ustar"), 257) |
| 176 | } |
| 177 | |
| 178 | func detectTar(r io.Reader) FileType { |
| 179 | return FileTypeUnknown |
| 180 | } |
| 181 | |
| 182 | func detectZip(f *os.File) FileType { |
| 183 | size, err := f.Seek(0, io.SeekEnd) |
| 184 | if err != nil { |
| 185 | return FileTypeUnknown |
| 186 | } |
| 187 | inz, err := zip.NewReader(f, size) |
| 188 | if err != nil { |
| 189 | return FileTypeUnknown |
| 190 | } |
| 191 | var isJar bool |
| 192 | for _, zf := range inz.File { |
| 193 | name := zf.Name |
| 194 | if strings.HasPrefix(name, "/") { |
| 195 | name = "." + name |
| 196 | } |
| 197 | name = path.Clean(name) |
| 198 | switch name { |
| 199 | case "AndroidManifest.xml": |
| 200 | return FileTypeAPK |
| 201 | case "AppManifest.xaml": |
| 202 | return FileTypeXAP |
| 203 | case "AppxManifest.xml", "AppxMetadata/AppxBundleManifest.xml": |
| 204 | return FileTypeAPPX |
| 205 | case "extension.vsixmanifest": |
| 206 | return FileTypeVSIX |
| 207 | case "META-INF/MANIFEST.MF": |
| 208 | // APKs are also JARs so save this for last |
| 209 | isJar = true |
| 210 | } |
| 211 | switch { |
| 212 | case strings.HasSuffix(name, ".app/Info.plist"): |
| 213 | return FileTypeIPA |
| 214 | case strings.HasSuffix(name, ".app/Contents/Info.plist"): |
| 215 | return FileTypeIPA |
| 216 | } |
| 217 | } |
| 218 | if isJar { |
| 219 | return FileTypeJAR |
| 220 | } |
| 221 | return FileTypeUnknown |
| 222 | } |