« script/electric-sheep-emulator-geofix.pl
↑ Summary
script/electric-sheep-find-running-processes.pl »
| 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 $OUTFILE; | ||
| 19 | my $PACKAGE_NAME; | ||
| 20 | my $FAST = 0; | ||
| 21 | if( ! Getopt::Long::GetOptions( | ||
| 22 | 'device|d=s' => \$DEVICE, | ||
| 23 | 'verbosity|v=i' => \$VERBOSITY, | ||
| 24 | 'fast' => \$FAST, # if fast then it is not full | ||
| 25 | 'package=s' => \$PACKAGE_NAME, | ||
| 26 | 'output|o=s'=> \$OUTFILE, | ||
| 27 | 'configfile|c=s' => \$CONFIGFILE, | ||
| 28 | 'help|h' => sub { print STDOUT usage(); exit(0); } | ||
| 29 | ) ){ die usage() } | ||
| 30 | |||
| 31 | if( ! defined $CONFIGFILE ){ print STDERR usage(); print STDERR "\n$0 : error, a configuration file must be specified with '--configfile'.\n"; exit(1); } | ||
| 32 | if( ! -f $CONFIGFILE ){ die "$0 : failed to find config file '$CONFIGFILE'." } | ||
| 33 | |||
| 34 | my $params = { | ||
| 35 | 'configfile' => $CONFIGFILE, | ||
| 36 | 'verbosity' => $VERBOSITY, | ||
| 37 | 'device-connected' => 1, | ||
| 38 | }; | ||
| 39 | # we assume there is a device connected which the user | ||
| 40 | # must specify by serial, of if just one, we connect to | ||
| 41 | # it without the serial | ||
| 42 | if( defined $DEVICE ){ $params->{'device-serial'} = $DEVICE } | ||
| 43 | else { $params->{'device-is-connected'} = 1 } | ||
| 44 | |||
| 45 | my $client = Android::ElectricSheep::Automator->new($params); | ||
| 46 | if( ! defined($client) ){ die "$0 : failed to instantiate the automator." } | ||
| 47 | |||
| 48 | my $fpars = {}; | ||
| 49 | if( ! $FAST ){ | ||
| 50 | $fpars->{'lazy'} = 0; | ||
| 51 | } | ||
| 52 | if( defined $PACKAGE_NAME ){ | ||
| 53 | $fpars->{'packages'} = qr/\Q${PACKAGE_NAME}\E/i; | ||
| 54 | $fpars->{'lazy'} = 1; | ||
| 55 | } | ||
| 56 | my $apps = $client->find_installed_apps($fpars); | ||
| 57 | if( ! defined($apps) ){ die "$0 : failed to find installed apps on the connected device." } | ||
| 58 | if( scalar(keys %$apps) == 0 ){ print STDERR "$0 : WARNING, no apps were found, surely there is something wrong.\n"; exit(1) } | ||
| 59 | # remove the null value (because in FAST mode we have no properties, only keys) | ||
| 60 | if( $FAST ){ | ||
| 61 | for (keys %$apps){ | ||
| 62 | $apps->{$_} = {} unless defined($apps->{$_}) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | # $apps is a hash with keys the package name (e.g. com.google.settings whatever) | ||
| 67 | # and the value is an AppProperties object. | ||
| 68 | # you need convert_blessed and TO_JSON! | ||
| 69 | |||
| 70 | # if packages were specified we will filter the output | ||
| 71 | if( defined $PACKAGE_NAME ){ | ||
| 72 | my %newapps; | ||
| 73 | for (keys %$apps){ | ||
| 74 | if( $_ =~ $PACKAGE_NAME ){ $newapps{$_} = $apps->{$_} } | ||
| 75 | } | ||
| 76 | $apps = \%newapps | ||
| 77 | } | ||
| 78 | |||
| 79 | my $jsonstr = perl2json($apps, {terse=>1, pretty=>1, convert_blessed=>1}); | ||
| 80 | if( ! defined $jsonstr ){ print STDERR "$0 : error, failed to convert perl data structure to JSON.\n"; exit(1) } | ||
| 81 | |||
| 82 | if( defined $OUTFILE ){ | ||
| 83 | my $FH; | ||
| 84 | open($FH, '>', $OUTFILE) or die "$0 : failed to open output file '$OUTFILE' for writing, $!"; | ||
| 85 | print $FH $jsonstr; | ||
| 86 | close $FH; | ||
| 87 | } else { print STDOUT $jsonstr } | ||
| 88 | |||
| 89 | print STDOUT "$0 : done, success!".(defined($OUTFILE)?" Output written to file '$OUTFILE'.\n":"\n"); | ||
| 90 | |||
| 91 | sub usage { | ||
| 92 | return "Usage $0 --configfile CONFIGFILE [--package NAME] [--output file.json] [--fast] [--device DEVICE] [--verbosity v]" | ||
| 93 | . "\n\nThis script will find all installed apps or those matched partially and case-insensitively by 'NAME' and dump them as JSON with their properties (e.g. MainActivity, permissions, etc.). The latter takes some time (10,15 seconds) and so it can be skipped by specifying --fast (which will dump only the names of the apps).\n" | ||
| 94 | . "\nExample:\n" | ||
| 95 | . "$0 --configfile config/myapp.conf --device Pixel_2_API_30_x86_ --output myapps.json\n" | ||
| 96 | . "$0 --package clock --configfile config/myapp.conf --device Pixel_2_API_30_x86_ --output myapps.json --fast\n" | ||
| 97 | . "\n\nProgram by Andreas Hadjiprocopis (c) 2025 / bliako at cpan.org / andreashad2 at gmail.com\n\n" | ||
| 98 | ; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; |