Skip to content

Commit

Permalink
chore: make ipsw img4 cmds --output flag consistant
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Jun 3, 2024
1 parent 2b5ccd5 commit 7236fb6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
36 changes: 21 additions & 15 deletions cmd/ipsw/cmd/img4/img4_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ import (
)

func init() {
Img4Cmd.AddCommand(decImg4Cmd)
Img4Cmd.AddCommand(img4DecCmd)

decImg4Cmd.PersistentFlags().String("iv-key", "", "AES iv+key")
decImg4Cmd.PersistentFlags().StringP("iv", "i", "", "AES iv")
decImg4Cmd.PersistentFlags().StringP("key", "k", "", "AES key")
decImg4Cmd.PersistentFlags().StringP("output", "o", "", "Output file")
img4DecCmd.Flags().String("iv-key", "", "AES iv+key")
img4DecCmd.Flags().StringP("iv", "i", "", "AES iv")
img4DecCmd.Flags().StringP("key", "k", "", "AES key")
img4DecCmd.Flags().StringP("output", "o", "", "Output folder")
img4DecCmd.MarkFlagDirname("output")
viper.BindPFlag("img4.dec.iv-key", img4DecCmd.Flags().Lookup("iv-key"))
viper.BindPFlag("img4.dec.iv", img4DecCmd.Flags().Lookup("iv"))
viper.BindPFlag("img4.dec.key", img4DecCmd.Flags().Lookup("key"))
viper.BindPFlag("img4.dec.output", img4DecCmd.Flags().Lookup("output"))
}

// decCmd represents the dec command
var decImg4Cmd = &cobra.Command{
var img4DecCmd = &cobra.Command{
Use: "dec <img4>",
Aliases: []string{"d"},
Short: "Decrypt img4 payloads",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

if viper.GetBool("verbose") {
Expand All @@ -58,10 +63,10 @@ var decImg4Cmd = &cobra.Command{
color.NoColor = viper.GetBool("no-color")

// flags
outputFile, _ := cmd.Flags().GetString("output")
ivkeyStr, _ := cmd.Flags().GetString("iv-key")
ivStr, _ := cmd.Flags().GetString("iv")
keyStr, _ := cmd.Flags().GetString("key")
ivkeyStr := viper.GetString("img4.dec.iv-key")
ivStr := viper.GetString("img4.dec.iv")
keyStr := viper.GetString("img4.dec.key")
outputDir := viper.GetString("img4.dec.output")
// validate flags
if len(ivkeyStr) != 0 && (len(ivStr) != 0 || len(keyStr) != 0) {
return fmt.Errorf("cannot specify both --iv-key AND --iv/--key")
Expand All @@ -70,9 +75,10 @@ var decImg4Cmd = &cobra.Command{
}

infile := filepath.Clean(args[0])
outfile := infile + ".dec"

if len(outputFile) == 0 {
outputFile = infile + ".dec"
if outputDir != "" {
outfile = filepath.Join(outputDir, filepath.Base(outfile))
}

var iv []byte
Expand All @@ -96,7 +102,7 @@ var decImg4Cmd = &cobra.Command{
return fmt.Errorf("failed to decode --iv-key: %v", err)
}
}
utils.Indent(log.Info, 2)(fmt.Sprintf("Decrypting file to %s", outputFile))
return icmd.DecryptPayload(infile, outputFile, iv, key)
utils.Indent(log.Info, 2)(fmt.Sprintf("Decrypting file to %s", outfile))
return icmd.DecryptPayload(infile, outfile, iv, key)
},
}
16 changes: 11 additions & 5 deletions cmd/ipsw/cmd/img4/img4_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func init() {
Img4Cmd.AddCommand(img4ExtractCmd)

img4ExtractCmd.Flags().Bool("img4", false, "Input file is an IMG4")
img4ExtractCmd.Flags().StringP("output", "o", "", "Output file")
img4ExtractCmd.Flags().StringP("output", "o", "", "Output folder")
img4ExtractCmd.MarkFlagDirname("output")
viper.BindPFlag("img4.extract.img4", img4DecCmd.Flags().Lookup("img4"))
viper.BindPFlag("img4.extract.output", img4DecCmd.Flags().Lookup("output"))
img4ExtractCmd.MarkZshCompPositionalArgumentFile(1)
}

Expand All @@ -53,11 +56,14 @@ var img4ExtractCmd = &cobra.Command{
log.SetLevel(log.DebugLevel)
}
color.NoColor = viper.GetBool("no-color")
// flags
isImg4 := viper.GetBool("img4.extract.img4")
outputDir := viper.GetString("img4.extract.output")

isImg4, _ := cmd.Flags().GetBool("img4")
outputDir, _ := cmd.Flags().GetString("output")

outFile := filepath.Join(outputDir, filepath.Clean(args[0])+".payload")
outFile := filepath.Clean(args[0]) + ".payload"
if outputDir != "" {
outFile = filepath.Join(outputDir, outFile)
}

utils.Indent(log.Info, 2)(fmt.Sprintf("Extracting payload to file %s", outFile))
return icmd.ExtractPayload(filepath.Clean(args[0]), outFile, isImg4)
Expand Down
14 changes: 11 additions & 3 deletions cmd/ipsw/cmd/img4/img4_img3.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import (
func init() {
Img4Cmd.AddCommand(img4Img3Cmd)

img4Img3Cmd.Flags().StringP("output", "o", "", "Output file")
img4Img3Cmd.MarkZshCompPositionalArgumentFile(1)
img4Img3Cmd.Flags().StringP("output", "o", "", "Output folder")
img4Img3Cmd.MarkFlagDirname("output")
viper.BindPFlag("img4.img3.output", img4Img3Cmd.Flags().Lookup("output"))

img4Img3Cmd.MarkZshCompPositionalArgumentFile(1)
}

// img4Img3Cmd represents the extract command
Expand All @@ -52,7 +54,13 @@ var img4Img3Cmd = &cobra.Command{
}
color.NoColor = viper.GetBool("no-color")

outFile := filepath.Join(viper.GetString("output"), filepath.Clean(args[0])+".payload")
// flags
outputDir := viper.GetString("img4.dec.output")

outFile := filepath.Clean(args[0]) + ".payload"
if outputDir != "" {
outFile = filepath.Join(outputDir, outFile)
}

log.Infof("Extracting payload to file %s", outFile)
return icmd.ParseImg3(filepath.Clean(args[0]), outFile)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipsw/cmd/img4/img4_kbag.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var img4KbagCmd = &cobra.Command{
Use: "kbag <IMG4>",
Aliases: []string{"k"},
Short: "Extract kbag from img4",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit 7236fb6

Please sign in to comment.