Skip to content

Commit

Permalink
feat: thread the 🆕 AEA1 decryption transparently through all relevant…
Browse files Browse the repository at this point in the history
… `ipsw` cmds
  • Loading branch information
blacktop committed Jun 11, 2024
1 parent e6cda25 commit b92ac6c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 14 deletions.
8 changes: 8 additions & 0 deletions api/server/routes/ipsw/ipsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/blacktop/ipsw/internal/commands/ent"
"github.com/blacktop/ipsw/internal/commands/extract"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -69,6 +70,13 @@ func getFsFiles(c *gin.Context) {
utils.Indent(log.Debug, 2)(fmt.Sprintf("Found extracted %s", dmgPath))
}

if filepath.Ext(dmgPath) == ".aea" {
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: fmt.Sprintf("failed to parse AEA encrypted DMG: %v", err)})
}
}

// mount filesystem DMG
utils.Indent(log.Info, 2)(fmt.Sprintf("Mounting %s", dmgPath))
mountPoint, alreadyMounted, err := utils.MountDMG(dmgPath)
Expand Down
8 changes: 8 additions & 0 deletions cmd/ipsw/cmd/mdevs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/apex/log"
"github.com/blacktop/go-plist"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -98,6 +99,13 @@ var mdevsCmd = &cobra.Command{
} else {
log.Debugf("Found extracted %s", dmgPath)
}
if filepath.Ext(dmgPath) == ".aea" {
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
defer os.Remove(dmgPath)
}
// mount filesystem DMG
log.Debugf("Mounting %s", dmgPath)
mountPoint, alreadyMounted, err := utils.MountDMG(dmgPath)
Expand Down
9 changes: 9 additions & 0 deletions cmd/ipsw/cmd/sb/sb_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/alecthomas/chroma/v2/quick"
"github.com/apex/log"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -113,6 +114,14 @@ var sbDiffCmd = &cobra.Command{
utils.Indent(log.Debug, 2)(fmt.Sprintf("Found extracted %s", dmgPath))
}

if filepath.Ext(dmgPath) == ".aea" {
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
defer os.Remove(dmgPath)
}

utils.Indent(log.Debug, 2)(fmt.Sprintf("Mounting FS %s", dmgPath))
mountPoint, alreadyMounted, err := utils.MountDMG(dmgPath)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/commands/ent/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/apex/log"
"github.com/blacktop/go-macho"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
"github.com/fatih/color"
)
Expand Down Expand Up @@ -269,6 +270,15 @@ func scanEnts(ipswPath, dmgPath, dmgType string) (map[string]string, error) {
utils.Indent(log.Debug, 2)(fmt.Sprintf("Found extracted %s", dmgPath))
}

if filepath.Ext(dmgPath) == ".aea" {
var err error
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return nil, fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
defer os.Remove(dmgPath)
}

utils.Indent(log.Debug, 2)(fmt.Sprintf("Mounting %s %s", dmgType, dmgPath))
mountPoint, alreadyMounted, err := utils.MountDMG(dmgPath)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/commands/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/apex/log"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
)

Expand Down Expand Up @@ -98,6 +99,13 @@ func DmgInIPSW(path, typ string) (*Context, error) {
}
}

if filepath.Ext(extractedDMG) == ".aea" {
extractedDMG, err = aea.Parse(extractedDMG, filepath.Dir(extractedDMG), nil)
if err != nil {
return nil, fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
}

mp, am, err := utils.MountDMG(extractedDMG)
if err != nil {
return nil, fmt.Errorf("failed to mount %s: %v", extractedDMG, err)
Expand Down
7 changes: 7 additions & 0 deletions internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
kcmd "github.com/blacktop/ipsw/internal/commands/kernel"
mcmd "github.com/blacktop/ipsw/internal/commands/macho"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/dyld"
"github.com/blacktop/ipsw/pkg/info"
"github.com/blacktop/ipsw/pkg/kernelcache"
Expand Down Expand Up @@ -259,6 +260,12 @@ func mountDMG(ctx *Context) (err error) {
} else {
utils.Indent(log.Debug, 2)(fmt.Sprintf("Found extracted %s", ctx.SystemOsDmgPath))
}
if filepath.Ext(ctx.SystemOsDmgPath) == ".aea" {
ctx.SystemOsDmgPath, err = aea.Parse(ctx.SystemOsDmgPath, filepath.Dir(ctx.SystemOsDmgPath), nil)
if err != nil {
return fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
}
utils.Indent(log.Info, 2)(fmt.Sprintf("Mounting %s", ctx.SystemOsDmgPath))
ctx.MountPath, ctx.IsMounted, err = utils.MountDMG(ctx.SystemOsDmgPath)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
icmd "github.com/blacktop/ipsw/internal/commands/img4"
"github.com/blacktop/ipsw/internal/magic"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
)

Expand All @@ -35,6 +36,13 @@ func scanDmg(ipswPath, dmgPath, dmgType string, handler func(string, *macho.File
} else {
utils.Indent(log.Debug, 2)(fmt.Sprintf("Found extracted %s", dmgPath))
}
if filepath.Ext(dmgPath) == ".aea" {
var err error
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
}
utils.Indent(log.Debug, 2)(fmt.Sprintf("Mounting %s %s", dmgType, dmgPath))
mountPoint, alreadyMounted, err := utils.MountDMG(dmgPath)
if err != nil {
Expand Down
22 changes: 10 additions & 12 deletions internal/utils/macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/apex/log"
"github.com/blacktop/go-plist"
"github.com/blacktop/ipsw/internal/utils/lsof"
"github.com/blacktop/ipsw/pkg/aea"
semver "github.com/hashicorp/go-version"
)

Expand Down Expand Up @@ -601,6 +602,15 @@ func ExtractFromDMG(ipswPath, dmgPath, destPath string, pattern *regexp.Regexp)
defer os.Remove(filepath.Clean(dmgs[0]))
}

if filepath.Ext(dmgPath) == ".aea" {
var err error
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return nil, fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
defer os.Remove(dmgPath)
}

Indent(log.Info, 2)(fmt.Sprintf("Mounting DMG %s", dmgPath))
mountPoint, alreadyMounted, err := MountDMG(dmgPath)
if err != nil {
Expand Down Expand Up @@ -663,18 +673,6 @@ func PkgUtilExpand(src, dst string) (string, error) {
return "", fmt.Errorf("only supported on macOS")
}

func Aea(in, out, key string) (string, error) {
if runtime.GOOS == "darwin" {
cmd := exec.Command("aea", "decrypt", "-i", in, "-o", out, "-key-value", fmt.Sprintf("base64:%s", key))
cout, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v: %s", err, cout)
}
return out, nil
}
return "", fmt.Errorf("only supported on macOS")
}

func InstallXCodeSimRuntime(path string) error {
if runtime.GOOS == "darwin" {
cmd := exec.Command("xcrun", "simctl", "runtime", "add", path)
Expand Down
17 changes: 15 additions & 2 deletions pkg/aea/aea.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/blacktop/ipsw/internal/utils"
"github.com/cloudflare/circl/hpke"
)

Expand All @@ -30,6 +31,18 @@ type fcsResponse struct {
WrappedKey string `json:"wrapped-key,omitempty"`
}

func aea(in, out, key string) (string, error) {
if runtime.GOOS == "darwin" {
cmd := exec.Command("aea", "decrypt", "-i", in, "-o", out, "-key-value", fmt.Sprintf("base64:%s", key))
cout, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%v: %s", err, cout)
}
return out, nil
}
return "", fmt.Errorf("only supported on macOS")
}

func Parse(in, out string, privKey []byte) (string, error) {
metadata := make(map[string][]byte)

Expand Down Expand Up @@ -135,5 +148,5 @@ func Parse(in, out string, privKey []byte) (string, error) {
return "", err
}

return utils.Aea(in, filepath.Join(out, filepath.Base(strings.TrimSuffix(in, filepath.Ext(in)))), base64.StdEncoding.EncodeToString(wkey))
return aea(in, filepath.Join(out, filepath.Base(strings.TrimSuffix(in, filepath.Ext(in)))), base64.StdEncoding.EncodeToString(wkey))
}
8 changes: 8 additions & 0 deletions pkg/dyld/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/apex/log"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/aea"
"github.com/blacktop/ipsw/pkg/info"
"github.com/blacktop/ipsw/pkg/ota/ridiff"
"github.com/pkg/errors"
Expand Down Expand Up @@ -184,6 +185,13 @@ func Extract(ipsw, destPath string, arches []string, driverkit bool) ([]string,
defer os.Remove(dmgs[0])
}

if filepath.Ext(dmgPath) == ".aea" {
dmgPath, err = aea.Parse(dmgPath, filepath.Dir(dmgPath), nil)
if err != nil {
return nil, fmt.Errorf("failed to parse AEA encrypted DMG: %v", err)
}
}

return ExtractFromDMG(i, dmgPath, destPath, arches, driverkit)
}

Expand Down

0 comments on commit b92ac6c

Please sign in to comment.