perl脚本自动化控制之android模拟器

鄢修德
2023-12-01

在几年前的一个PC反病毒项目中利用过perl脚本实现过一套sandbox的自动化任务系统,当时的业务流程是:自动的取任务,执行任务,输出日志,分析日志,页面展示的系统。最近两年在研究android方面的一些东西。突然想到,android目前病毒的爆发,可能也需要类似的自动化系统来做一些自动化的分析:比如竞品分析系统,比如sandbox系统,都需要类似的业务逻辑。

下面分享一个用perl脚本自动化控制android模拟器运行一款android下的扫描程序的脚本程序:

control_emulator.pl

#!/usr/bin/perl


#----------------------------------------------------------

#------------------------created by linux_feixue-----------


#----------------------create time:2013-1-15---------------


BEGIN
{
    my $path;
    if ($^O eq 'linux')
    {
        if ($0=~m/^(.+)\//) { $path = $1; } else { $path = readpipe('pwd'); chomp($path); }
           unshift(@INC, $path);
           chdir($path);
     }else
     {
      die("Please run this script on linux.\n");
     }
}
my $num=500;
my $count=1;
my @devices=readpipe("adb devices");
if(scalar(@devices)>0)
{
foreach my $emulator (@devices)
 {
        chomp($emulator);
        if ($emulator=~m/(emulator-\w{4})\s/)
        {
                system("perl loop_download.pl --avd=$1 --num=$num --id=$count&");
                $count++;
        }
 }
}


loop_download.pl

#!/usr/bin/perl


#----------------------------------------------------------

#------------------------created by linux_feixue-----------

#----------------------create time:2013-1-15---------------


BEGIN
{
    my $path;
    if ($^O eq 'linux')
    {
        if ($0=~m/^(.+)\//) { $path = $1; } else { $path = readpipe('pwd'); chomp($path); }
           unshift(@INC, $path);
           chdir($path);
     }else
     {
      die("Please run this script on linux.\n");
     }
}
use config;
use database;

sub usage
{
        print "Usage:\n";
        print "\t--avd=[specify AVD which installed antiy sdk]\n";
        print "\t--num=[the num per process]\n";
        print "\t--id=[task id]\n";
        exit(0);
}

sub process
{
        my($avd,$num,$id)=@_;
        my $dbh = connect_db();
        my $sql = "update $scan_task_tb set antiy_bot_id=0,antiy_status='new' where antiy_status!='done' and antiy_bot_id=$id";
        my_update($dbh,$sql);
        $sql = "update $scan_task_tb  set antiy_bot_id=$id ,antiy_status='waitting' where antiy_bot_id=0 and antiy_status='new' limit $num";
        my_update($dbh,$sql);
        $sql = "select * from $scan_task_tb where antiy_bot_id=$id and antiy_status='waitting'";
        my $sth=my_query($dbh,$sql);
        $sql = "update $scan_task_tb set antiy_status='downloading' where antiy_bot_id=$id and antiy_status='waitting'";
        my_update($dbh,$sql);
        while(my @record = $sth->fetchrow_array())
        {
                if(!-e $avd)
                {
                        mkdir($avd);
                }
                system("wget -q $pre_url$record[0] -O $avd/$record[0]");
        }
        if(-e $avd)
        {
                $sql = "update $scan_task_tb set antiy_status='scanning' where antiy_bot_id=$id and antiy_status='downloading'";
               my_update($dbh,$sql);
                system("perl antiy_scan.pl --avd=$avd --aim=$avd");
                system("rm -r $avd");
                $sql = "update $scan_task_tb set antiy_status='done' where antiy_bot_id=$id and antiy_status='scanning'";
                my_update($dbh,$sql);
        }
        $dbh->disconnect();

}
sub main
{
        my ($avd,$num,$id) = ('', 0,0);
        foreach my $param (@ARGV)
        {
                if ($param=~m/^--avd=(.+)$/)
                {
                        $avd = $1;

                }elsif ($param=~m/^--num=(.+)$/)
                {
                        $num = $1;

                }elsif ($param=~m/^--id=(.+)$/)
                {
                        $id = $1;
                }else
                {
                        usage();
                }
        }
        usage() if ($avd eq '' || $id eq 0 ||$num eq 0);
        while(1)
        {
                process($avd,$num,$id);
        }
}

&main;

antiy_scan.pl


#!/usr/bin/perl


#----------------------------------------------------------

#------------------------created by linux_feixue ----------

#----------------------create time:2013-1-15---------------


BEGIN
{
    my $path;
    if ($^O eq 'linux')
    {
        if ($0=~m/^(.+)\//) { $path = $1; } else { $path = readpipe('pwd'); chomp($path); }
           unshift(@INC, $path);
           chdir($path);
     }else
     {
      die("Please run this script on linux.\n");
     }
}
use strict;
use config;
use database;
sub usage
{
        print "Usage:\n";
        print "\t--avd=[specify AVD which installed antiy sdk]\n";
        print "\t--aim=[specify file or directory to scan]\n";
        exit(0);
}
sub upload_file
{
        my($avd,$aim) = @_;
        my $cmd  = "adb -s $avd push $aim /mnt/sdcard/antiy/";
        printf("uploading samples to emulator:$avd\n");
        system($cmd);
}
sub del_file
{
        my($avd) = @_;
        my $cmd = "adb -s $avd shell rm -r /mnt/sdcard/antiy/*";
        my $check_cmd ="adb -s $avd shell ls /mnt/sdcard/antiy/";
        my @return=readpipe($check_cmd);
        if(scalar(@return)>1)
        {
                system($cmd);
        }
}
sub scan
{
        my($avd,$aim)=@_;
        del_file($avd);
        upload_file($avd,$aim);
        printf("$avd is Scanning...\n");
        my $cmd="adb -s $avd shell dalvikvm -cp /data/app/com.feixue.scanner-1.apk com.feixue.scanner.antiycmd /mnt/sdcard/antiy/";
        my @result=readpipe($cmd);
        my $dbh = connect_db();
        foreach my $line(@result)
        {
                chomp($line);
                if ($line=~m/\/(\w{40})->(.+)$/)
                {
                        my $sql = "insert into $antiy_result_tb(sha1,antiy,time) values(".$dbh->quote($1).",".$dbh->quote($2).",NOW())";
                        my_update($dbh,$sql);
                }
        }
        $dbh->disconnect();
        #unless(open WBOT,'>>',"$avd.log")
        #{#fail
        #       print "Can't write data to result.log: $!\n";
        #}
        #print WBOT @result;
        #close WBOT;    
}
sub main
{
        my ($avd,$aim) = ('','');
        foreach my $param (@ARGV)
        {
                if ($param=~m/^--avd=(.+)$/)
                {
                        $avd = $1;
                }elsif ($param=~m/^--aim=(.+)$/)
                {
                        $aim = $1;
                }else
                {
                        usage();
                }
        }
        usage() if ($avd eq '' || $aim eq '');
        scan($avd,$aim);
        del_file($avd);
}
&main;


 类似资料: