Advertisement
opexxx

sniff-client-ciphers.pl

Apr 17th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.16 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use autodie qw(:all);
  4. use File::Temp qw(tempdir);
  5.  
  6. ##############################################################################
  7. #
  8. # take care -- this thing is fragile. That is, the emulator environment is
  9. # fragile.
  10. #
  11. # run all available versions of android in the emulator, open an HTTPS
  12. # URL in the system browser, and sniff the browser's cipher list
  13. #
  14. # make sure no other android devices/emulators are connected while running
  15. # this script!
  16. #
  17. # since the "android" command cannot reliably generate AVDs, you need to
  18. # generate those AVDs yourself, and name them "asc<apilevel>", e.g. "asc13".
  19. #
  20. #
  21. # prerequisites:
  22. # - an android SDK/emulator setup on a linux machine, with "android",
  23. #   "adb" and "emulator" in the $PATH
  24. # - all the system images for the versions you want to run
  25. # - sniff-client-ciphers from ../sniff-client-ciphers
  26. #
  27. # cm@coretec.at 20131116
  28. #
  29. ##############################################################################
  30.  
  31. my $usage = "usage: $0 pcap-output-dir apilevel\n";
  32.  
  33. my $outputdir = shift || die $usage;
  34. my $apilevel = shift || die $usage;
  35.  
  36. # versions indexed by API level
  37. # source: https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
  38. my %androidversion = ( 2 => '1.1',
  39.                3 => '1.5',
  40.                4 => '1.6',
  41.                5 => '2.0',
  42.                6 => '2.0.1',
  43.                7 => '2.1',
  44.                8 => '2.2',
  45.                9 => '2.3-2.3.2',
  46.                10 => '2.3.3-2.3.7',
  47.                11 => '3.0',
  48.                12 => '3.1',
  49.                13 => '3.2',
  50.                14 => '4.0-4.0.2',
  51.                15 => '4.0.3-4.0.4',
  52.                16 => '4.1',
  53.                17 => '4.2',
  54.                18 => '4.3',
  55.                19 => '4.4',
  56.              );
  57.  
  58. my $tmpdir = tempdir(CLEANUP => 1);
  59. print "Base dir: $tmpdir\n";
  60.  
  61. run_one_apilevel($apilevel);
  62.  
  63. sub run_one_apilevel {
  64.   my $apilevel = shift;
  65.  
  66. #  my $n = create_avd($apilevel);
  67.   my($pid, $pcap) = start_avd($apilevel);
  68.   # wait until the emulator has truly booted...
  69.   print "waiting for boot to complete...\n";
  70.   sleep(60);
  71.   print "starting browser...\n";
  72.   send_intent("-a android.intent.action.VIEW -d https://git.bettercrypto.org");
  73.   sleep(30);
  74.   stop_avd($pid);
  75. #  delete_avd($apilevel);
  76.   print "pcap file for API $apilevel is $pcap\n";
  77.   system("../sniff-client-ciphers/sniff-client-ciphers.pl 'tcp.port==443' $pcap 'Android $androidversion{$apilevel}' > $outputdir/Android_$androidversion{$apilevel}.txt");
  78. }
  79.  
  80. # send an intent via "am start" command in the emulator.
  81. # parameter is a string to be given as arguments to "am start"
  82. # returns 1 when OK, undef on error
  83. sub send_intent {
  84.   my($intent) = shift;
  85.  
  86.   my $count = 60;
  87.   while($count--) {
  88.     my @result = `adb shell am start $intent`;
  89.     if(grep /^Error/, @result) {
  90.       print "ERROR\n";
  91.     } else {
  92.       return 1;
  93.     }
  94.     sleep(1);
  95.   }
  96.   return undef;
  97. }
  98.  
  99. # fork a child and run the emulator
  100. # return the ($emulator_pid, $pcap_filename)
  101. sub start_avd {
  102.   my($apilevel) = shift;
  103.  
  104.   my $name = "asc$apilevel";
  105.   my $pcapfile = "$outputdir/$name.pcap";
  106.   my($pid) = fork();
  107.   if(!defined $pid) {
  108.     die "start_avd: can't fork: $!\n";
  109.   } elsif($pid) {
  110.     # wait for emulator to be ready...
  111.     print "emulator $name started, waiting for readyness...\n";
  112.     system(qw(adb wait-for-device));
  113.     return ($pid, $pcapfile);
  114.   } else {
  115.     exec(qw(emulator -avd), $name, qw(-no-boot-anim -tcpdump),
  116.      $pcapfile);
  117.   }
  118. }
  119.  
  120. sub stop_avd {
  121.   my $pid = shift;
  122.   kill("TERM", $pid);
  123.   print "waiting for $pid to terminate...\n";
  124.   waitpid($pid, 0);
  125. }
  126.  
  127.  
  128.  
  129. #adb shell am start -a android.intent.action.VIEW -d https://www.ssllabs.com/ssltest/viewMyClient.html
  130. # tshark -r /tmp/tls -d tcp.port==4433,ssl -O ssl
  131.  
  132.  
  133.  
  134. sub create_avd {
  135.   my($apilevel) = shift;
  136.   my $name = "asc$apilevel";
  137.   delete_avd($apilevel);
  138.   # "echo" is required because the command asks a question...
  139.   system("echo | android -s create avd --name $name --target $apilevel --path $tmpdir/$name");
  140.   print "created avd $name\n";
  141.   $name;
  142. }
  143.  
  144. sub delete_avd {
  145.   my($apilevel) = shift;
  146.   my $name = "asc$apilevel";
  147.   ## ignore errors
  148.   eval {
  149.     system(qw(android -s delete avd --name), $name);
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement