Skip to content

Commit

Permalink
cli: add --pem-db option
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Oct 30, 2024
1 parent 9fb57a1 commit 94c2584
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 3 additions & 2 deletions ipsw_parser/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import logging
from pathlib import Path
from typing import IO
from typing import IO, Optional
from zipfile import ZipFile

import click
Expand Down Expand Up @@ -47,7 +47,8 @@ def info(file) -> None:
@cli.command('extract')
@click.argument('file', type=click.Path(exists=True, file_okay=True, dir_okay=False))
@click.argument('output', type=click.Path(exists=False))
def extract(file: IO, output: str) -> None:
@click.option('--pem-db', help='Path DB file url (can be either a filesystem path or an HTTP URL)')
def extract(file: IO, output: str, pem_db: Optional[str]) -> None:
""" Extract .ipsw into filesystem layout """
output = Path(output)

Expand Down
17 changes: 13 additions & 4 deletions ipsw_parser/build_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tempfile import TemporaryDirectory
from typing import Optional

import requests
from cached_property import cached_property
from plumbum import ProcessExecutionError, local
from pyimg4 import IM4P
Expand All @@ -14,7 +15,7 @@
logger = logging.getLogger(__name__)


def _extract_dmg(buf: bytes, output: Path, sub_path: Optional[Path] = None) -> None:
def _extract_dmg(buf: bytes, output: Path, sub_path: Optional[Path] = None, pem_db: Optional[str] = None) -> None:
ipsw = local['ipsw']
hdiutil = local['hdiutil']
# darwin system statistically have problems cleaning up after detaching the mountpoint
Expand All @@ -28,7 +29,15 @@ def _extract_dmg(buf: bytes, output: Path, sub_path: Optional[Path] = None) -> N
logger.debug('Found Apple Encrypted Archive. Decrypting...')
dmg_aea = Path(str(dmg) + '.aea')
dmg_aea.write_bytes(buf)
ipsw('fw', 'aea', dmg_aea, '-o', temp_dir)
args = ['fw', 'aea', dmg_aea, '-o', temp_dir]
if pem_db is not None:
if '://' in pem_db:
# create a local file containing it
temp_pem_db = temp_dir / 'pem-db.json'
temp_pem_db.write_text(requests.get(pem_db).text)
pem_db = temp_pem_db
args += ['--pem-db', pem_db]
ipsw(args)
else:
dmg.write_bytes(buf)

Expand Down Expand Up @@ -149,13 +158,13 @@ def extract_dsc(self, output: Path) -> None:
sub_path=Path('System'))
_split_dsc(output)

def extract(self, output: Path) -> None:
def extract(self, output: Path, pem_db: Optional[str] = None) -> None:
logger.info(f'extracting into: {output}')

build_identity = self.build_manifest.build_identities[0]

logger.info(f'extracting OS into: {output}')
_extract_dmg(build_identity.get_component('OS').data, output)
_extract_dmg(build_identity.get_component('OS').data, output, pem_db=pem_db)

kernel_component = build_identity.get_component('KernelCache')
kernel_path = Path(kernel_component.path)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coloredlogs
cached_property
plumbum
pyimg4>=0.8.6
requests

0 comments on commit 94c2584

Please sign in to comment.