Ejemplo n.º 1
0
def align_and_sign_output_apk(
    unaligned_apk_path,
    output_apk_path,
    reset_timestamps,
    sign,
    keystore,
    key_alias,
    key_password,
    ignore_zipalign,
    page_align,
):
    if isfile(output_apk_path):
        os.remove(output_apk_path)

    try:
        os.makedirs(dirname(output_apk_path))
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    zipalign(unaligned_apk_path, output_apk_path, ignore_zipalign, page_align)

    if reset_timestamps:
        ZipReset.reset_file(output_apk_path)

    # Add new signature
    if sign:
        sign_apk(keystore, key_password, key_alias, output_apk_path)
Ejemplo n.º 2
0
def create_output_apk(
    extracted_apk_dir,
    output_apk_path,
    sign,
    keystore,
    key_alias,
    key_password,
    ignore_zipalign,
    page_align,
):

    # Remove old signature files
    for f in abs_glob(extracted_apk_dir, "META-INF/*"):
        cert_path = join(extracted_apk_dir, f)
        if isfile(cert_path):
            os.remove(cert_path)

    directory = make_temp_dir(".redex_unaligned", False)
    unaligned_apk_path = join(directory, "redex-unaligned.apk")

    if isfile(unaligned_apk_path):
        os.remove(unaligned_apk_path)

    # Create new zip file
    with zipfile.ZipFile(unaligned_apk_path, "w") as unaligned_apk:
        for dirpath, _dirnames, filenames in os.walk(extracted_apk_dir):
            for filename in filenames:
                filepath = join(dirpath, filename)
                archivepath = filepath[len(extracted_apk_dir) + 1:]
                try:
                    compress = per_file_compression[archivepath]
                except KeyError:
                    compress = zipfile.ZIP_DEFLATED
                unaligned_apk.write(filepath,
                                    archivepath,
                                    compress_type=compress)

    # Add new signature
    if sign:
        sign_apk(keystore, key_password, key_alias, unaligned_apk_path)

    if isfile(output_apk_path):
        os.remove(output_apk_path)

    try:
        os.makedirs(dirname(output_apk_path))
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    zipalign(unaligned_apk_path, output_apk_path, ignore_zipalign, page_align)
Ejemplo n.º 3
0
def create_output_apk(extracted_apk_dir, output_apk_path, sign, keystore,
                      key_alias, key_password, ignore_zipalign, page_align):

    # Remove old signature files
    for f in abs_glob(extracted_apk_dir, 'META-INF/*'):
        cert_path = join(extracted_apk_dir, f)
        if isfile(cert_path):
            os.remove(cert_path)

    directory = make_temp_dir('.redex_unaligned', False)
    unaligned_apk_path = join(directory, 'redex-unaligned.apk')

    if isfile(unaligned_apk_path):
        os.remove(unaligned_apk_path)

    # Create new zip file
    with zipfile.ZipFile(unaligned_apk_path, 'w') as unaligned_apk:
        for dirpath, _dirnames, filenames in os.walk(extracted_apk_dir):
            for filename in filenames:
                filepath = join(dirpath, filename)
                archivepath = filepath[len(extracted_apk_dir) + 1:]
                try:
                    compress = per_file_compression[archivepath]
                except KeyError:
                    compress = zipfile.ZIP_DEFLATED
                unaligned_apk.write(filepath, archivepath,
                                    compress_type=compress)

    # Add new signature
    if sign:
        sign_apk(keystore, key_password, key_alias, unaligned_apk_path)

    if isfile(output_apk_path):
        os.remove(output_apk_path)

    try:
        os.makedirs(dirname(output_apk_path))
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    zipalign(unaligned_apk_path, output_apk_path, ignore_zipalign, page_align)
Ejemplo n.º 4
0
def run_debug_injector(args):
    extracted_apk_dir = make_temp_dir(".extracted_apk", False)
    dex_dir = make_temp_dir(".dexen", False)

    with ZipManager(
            args.input_apk, extracted_apk_dir, args.output_apk), UnpackManager(
                args.input_apk, extracted_apk_dir,
                dex_dir) as store_files, LibraryManager(extracted_apk_dir):
        dexen = move_dexen_to_directories(dex_dir,
                                          dex_glob(dex_dir)) + store_files
        try:
            subprocess.check_output(
                [args.bin_path, "-o", dex_dir, "--dex-files"] + dexen,
                stderr=subprocess.STDOUT,
            )
        except subprocess.CalledProcessError as e:
            sys.stderr.write("Error while running inject debug binary:\n")
            sys.stderr.write(e.output.decode("utf-8"))
            exit(1)

    if (args.keystore is not None and args.keyalias is not None
            and args.keypass is not None):
        sign_apk(args.keystore, args.keypass, args.keyalias, args.output_apk)
Ejemplo n.º 5
0
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import argparse
import os
import zipfile

from pyredex.utils import sign_apk

parser = argparse.ArgumentParser()
parser.add_argument("apk", help="Input APK file")
parser.add_argument("assets", help="Files to add to assets/", nargs="+")
parser.add_argument("--keystore")
parser.add_argument("--keypass")
parser.add_argument("--keyalias")
args = parser.parse_args()

with zipfile.ZipFile(args.apk, "a") as zf:
    for asset in args.assets:
        zf.write(asset, os.path.join("assets", os.path.basename(asset)))

sign_apk(args.keystore, args.keypass, args.keyalias, args.apk)
Ejemplo n.º 6
0
import tempfile
import zipfile
from os.path import join

from pyredex.utils import sign_apk

parser = argparse.ArgumentParser()
parser.add_argument("apk")
parser.add_argument("--generator", required=True)
parser.add_argument("--output", required=True)
parser.add_argument("--keystore", required=True)
parser.add_argument("--keypass", required=True)
parser.add_argument("--keyalias", required=True)
args = parser.parse_args()

with tempfile.TemporaryDirectory() as temp_dir:
    with zipfile.ZipFile(args.apk) as zip:
        zip.extractall(temp_dir)

    subprocess.check_call([args.generator, join(temp_dir, "classes.dex")])

    if os.path.exists(args.output):
        os.remove(args.output)

    shutil.rmtree(join(temp_dir, "META-INF"))
    shutil.make_archive(args.output, "zip", temp_dir)
    # `out`.zip was created, rename.
    os.rename(f"{args.output}.zip", args.output)

    sign_apk(args.keystore, args.keypass, args.keyalias, args.output)