diff --git a/cmd/ipsw/cmd/img4/img4_dec.go b/cmd/ipsw/cmd/img4/img4_dec.go index b5dbe036a..57d2b5a16 100644 --- a/cmd/ipsw/cmd/img4/img4_dec.go +++ b/cmd/ipsw/cmd/img4/img4_dec.go @@ -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 ", 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") { @@ -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") @@ -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 @@ -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) }, } diff --git a/cmd/ipsw/cmd/img4/img4_extract.go b/cmd/ipsw/cmd/img4/img4_extract.go index 6ac40d598..4e905686f 100644 --- a/cmd/ipsw/cmd/img4/img4_extract.go +++ b/cmd/ipsw/cmd/img4/img4_extract.go @@ -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) } @@ -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) diff --git a/cmd/ipsw/cmd/img4/img4_img3.go b/cmd/ipsw/cmd/img4/img4_img3.go index 999b67221..9a54bbda0 100644 --- a/cmd/ipsw/cmd/img4/img4_img3.go +++ b/cmd/ipsw/cmd/img4/img4_img3.go @@ -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 @@ -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) diff --git a/cmd/ipsw/cmd/img4/img4_kbag.go b/cmd/ipsw/cmd/img4/img4_kbag.go index ee23318bd..e7f8b14e9 100644 --- a/cmd/ipsw/cmd/img4/img4_kbag.go +++ b/cmd/ipsw/cmd/img4/img4_kbag.go @@ -47,7 +47,7 @@ var img4KbagCmd = &cobra.Command{ Use: "kbag ", 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 {