| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.09'; | ||
| 7 | |||
| 8 | use lib ('blib/lib'); | ||
| 9 | |||
| 10 | use Getopt::Long qw(:config no_ignore_case); | ||
| 11 | use Data::Roundtrip qw/perl2dump perl2json no-unicode-escape-permanently/; | ||
| 12 | |||
| 13 | use Android::ElectricSheep::Automator; | ||
| 14 | |||
| 15 | my $VERBOSITY = 0; # we need verbosity of 10 (max), so this is not used | ||
| 16 | my $DEVICE; | ||
| 17 | my $CONFIGFILE; | ||
| 18 | my $OUTDIR; | ||
| 19 | my $PACKAGE_NAME; | ||
| 20 | my $WILDCARD = 0; | ||
| 21 | |||
| 22 | if( ! Getopt::Long::GetOptions( | ||
| 23 | 'device|d=s' => \$DEVICE, | ||
| 24 | 'verbosity|v=i' => \$VERBOSITY, | ||
| 25 | 'output|o=s'=> \$OUTDIR, | ||
| 26 | 'package|p=s', \$PACKAGE_NAME, | ||
| 27 | 'wildcard' => sub { $WILDCARD = 1 }, | ||
| 28 | 'configfile|c=s' => \$CONFIGFILE, | ||
| 29 | 'help|h' => sub { print STDOUT usage(); exit(0); } | ||
| 30 | ) ){ die usage() } | ||
| 31 | |||
| 32 | if( ! defined $OUTDIR ){ print STDERR usage(); print STDERR "\n$0 : error, an output filename must be specified with '--output'.\n"; exit(1); } | ||
| 33 | if( ! defined $CONFIGFILE ){ print STDERR usage(); print STDERR "\n$0 : error, a configuration file must be specified with '--configfile'.\n"; exit(1); } | ||
| 34 | if( ! -f $CONFIGFILE ){ die "$0 : failed to find config file '$CONFIGFILE'." } | ||
| 35 | |||
| 36 | my $params = { | ||
| 37 | 'configfile' => $CONFIGFILE, | ||
| 38 | 'verbosity' => $VERBOSITY, | ||
| 39 | }; | ||
| 40 | # we assume there is a device connected which the user | ||
| 41 | # must specify by serial, of if just one, we connect to | ||
| 42 | # it without the serial | ||
| 43 | if( defined $DEVICE ){ $params->{'device-serial'} = $DEVICE } | ||
| 44 | else { $params->{'device-is-connected'} = 1 } | ||
| 45 | |||
| 46 | my $client = Android::ElectricSheep::Automator->new($params); | ||
| 47 | if( ! defined($client) ){ die "$0 : failed to instantiate the automator." } | ||
| 48 | |||
| 49 | if( $WILDCARD > 0 ){ | ||
| 50 | $PACKAGE_NAME = qr/\Q${PACKAGE_NAME}\E/i | ||
| 51 | } | ||
| 52 | |||
| 53 | my $sparams = { | ||
| 54 | 'package' => $PACKAGE_NAME, | ||
| 55 | 'output-dir' => $OUTDIR, | ||
| 56 | }; | ||
| 57 | my $res = $client->pull_app_apk_from_device($sparams); | ||
| 58 | if( ! defined($res) ){ die perl2dump($sparams)."$0 : failed to pull APK of app specified using this name '$PACKAGE_NAME' and above parameters. Note that the error is not about not finding the app on the device, it is something else.\n"; } | ||
| 59 | my $Npackages = scalar keys %$res; | ||
| 60 | if( $Npackages == 0 ){ print STDERR "$0 : done, warning zero apps were pulled, nothing matched specified package name '$PACKAGE_NAME'.\n"; exit(0); } | ||
| 61 | |||
| 62 | my $Napks = 0; | ||
| 63 | for my $pname (sort keys %$res){ | ||
| 64 | my $arr = $res->{$pname}; | ||
| 65 | for my $v (@$arr){ | ||
| 66 | $Napks++; | ||
| 67 | if( $VERBOSITY > 0 ){ print STDOUT "$0 : package '$pname' : saved apk ${Napks} from '".$v->{'device-path'}."' into '".$v->{'local-path'}."'.\n" } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | print STDOUT "$0 : done, success! Extracted ${Napks} APK(s) from ${Npackages} packages, written to output dir '$OUTDIR'.\n"; | ||
| 71 | |||
| 72 | sub usage { | ||
| 73 | return "Usage $0 --package NAME --output apkdir --configfile CONFIGFILE [--wildcard] [--device DEVICE] [--verbosity v]\n" | ||
| 74 | . "\nThis script will pull the APK(s) of the specified package NAME into the local dir 'OUTPUT'. The package NAME can be the full package name, e.g. 'com.android.gallery2' or a part of it, e.g. 'gallery'. Use --wildcard option if you want to match partial package names. The OUTPUT will be a directory, which will be created if it does not exist, and at the end of a successful run, it will contain the APK files related to the package name, if any. These files have the '.apk' extension. They can be decoded/analysed using the wonderful JADX disassembler.\n" | ||
| 75 | . "\nExample:\n" | ||
| 76 | . " match all packages with 'gallery2' in their name:\n" | ||
| 77 | . "$0 --configfile config/myapp.conf --output apkdir --package 'calendar2' --wildcard\n" | ||
| 78 | . "\n match the package name 'com.android.gallery2' exactly:\n" | ||
| 79 | . "$0 --configfile config/myapp.conf --output apkdir --package 'com.android.gallery2'\n" | ||
| 80 | . "\nProgram by Andreas Hadjiprocopis (c) 2025 / bliako at cpan.org / andreashad2 at gmail.com\n\n" | ||
| 81 | ; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |