|
| 1 | +/* |
| 2 | +Copyright © 2024 blacktop |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | +package cmd |
| 23 | + |
| 24 | +import ( |
| 25 | + "bytes" |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "path/filepath" |
| 29 | + "runtime" |
| 30 | + "sort" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "github.com/apex/log" |
| 34 | + "github.com/spf13/cobra" |
| 35 | + |
| 36 | + "github.com/blacktop/go-macho/pkg/cpio" |
| 37 | + "github.com/blacktop/go-macho/pkg/xar" |
| 38 | + "github.com/blacktop/ipsw/internal/magic" |
| 39 | + "github.com/blacktop/ipsw/pkg/ota/pbzx" |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + rootCmd.AddCommand(pkgCmd) |
| 44 | +} |
| 45 | + |
| 46 | +// pkgCmd represents the pkg command |
| 47 | +var pkgCmd = &cobra.Command{ |
| 48 | + Use: "pkg", |
| 49 | + Short: "List contents of a DMG/PKG file", |
| 50 | + Args: cobra.ExactArgs(1), |
| 51 | + SilenceUsage: true, |
| 52 | + SilenceErrors: true, |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + |
| 55 | + if Verbose { |
| 56 | + log.SetLevel(log.DebugLevel) |
| 57 | + } |
| 58 | + |
| 59 | + infile := filepath.Clean(args[0]) |
| 60 | + |
| 61 | + isDMG, err := magic.IsDMG(infile) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + if !isDMG { |
| 66 | + if isXar, err := magic.IsXar(infile); err != nil { |
| 67 | + return err |
| 68 | + } else if !isXar { |
| 69 | + return fmt.Errorf("file is not a dmg OR pkg file") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if isDMG { |
| 74 | + log.Fatal("DMG files are not supported yet") |
| 75 | + // d, err := dmg.Open(infile, nil) |
| 76 | + // if err != nil { |
| 77 | + // return err |
| 78 | + // } |
| 79 | + // defer d.Close() |
| 80 | + // if err := d.Load(); err != nil { |
| 81 | + // return err |
| 82 | + // } |
| 83 | + } else { // PKG/XAR |
| 84 | + pkg, err := xar.OpenReader(infile) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + // FIXME defer xar.Close() |
| 89 | + if !pkg.ValidSignature() { |
| 90 | + log.Warn("PKG/XAR file signature is invalid, this may be a corrupted file") |
| 91 | + } |
| 92 | + var names []string |
| 93 | + var payload *xar.File |
| 94 | + for _, file := range pkg.File { |
| 95 | + names = append(names, file.Name) |
| 96 | + if strings.Contains(file.Name, "Payload") { |
| 97 | + payload = file |
| 98 | + } |
| 99 | + } |
| 100 | + sort.StringSlice(names).Sort() |
| 101 | + for _, name := range names { |
| 102 | + fmt.Println(name) |
| 103 | + } |
| 104 | + if payload != nil { |
| 105 | + f, err := payload.Open() |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + defer f.Close() |
| 110 | + log.Infof("Parsing %s...", payload.Name) |
| 111 | + var pbuf bytes.Buffer |
| 112 | + if err := pbzx.Extract(context.Background(), f, &pbuf, runtime.NumCPU()); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + cr, err := cpio.NewReader(bytes.NewReader(pbuf.Bytes()), int64(pbuf.Len())) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + var cnames []string |
| 120 | + for _, file := range cr.Files { |
| 121 | + cnames = append(cnames, file.Name) |
| 122 | + } |
| 123 | + sort.StringSlice(cnames).Sort() |
| 124 | + for _, name := range cnames { |
| 125 | + fmt.Println(name) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | + }, |
| 132 | +} |
0 commit comments