#!/usr/bin/perl

use IO::Socket;
use Time::HiRes qw( usleep );
use MIME::Base64 qw (encode_base64 decode_base64);

my $addr='80.152.195.209:701';
my $host='192.168.2.54';

  print "Opening connection 5 ...\n";
  my $sock5 = IO::Socket::INET->new(PeerAddr => $addr);
 usleep(19.208);
 usleep(164.032);
 usleep(24.904);
 usleep(0.019);
  print "Sending packet 54 ...\n";
  $sock5->print("\x47\x45\x54\x20\x2f\x63\x67\x69\x2d\x62\x69\x6e\x2f\x63\x6f\x6e\x74\x72\x6f\x6c\x2e\x63\x67\x69\x20\x48\x54\x54\x50\x2f\x31\x2e\x30\x0d\x0a\x55\x73\x65\x72\x2d\x41\x67\x65\x6e\x74\x3a\x20\x56\x56\x54\x4b\x20\x28\x76\x65\x72\x3d\x34\x30\x29\x0d\x0a\x78\x2d\x73\x65\x73\x73\x69\x6f\x6e\x63\x6f\x6f\x6b\x69\x65\x3a\x20\x37\x2b\x2f\x4c\x39\x2f\x63\x45\x31\x31\x68\x61\x50\x38\x2f\x30\x35\x36\x42\x31\x64\x51\x0d\x0a\x41\x63\x63\x65\x70\x74\x3a\x20\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x78\x2d\x76\x76\x74\x6b\x2d\x74\x75\x6e\x6e\x65\x6c\x6c\x65\x64\x0d\x0a\x50\x72\x61\x67\x6d\x61\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x41\x75\x74\x68\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e\x3a\x20\x42\x61\x73\x69\x63\x20\x63\x6d\x39\x76\x64\x44\x70\x53\x5a\x57\x6c\x75\x61\x57\x4e\x72\x5a\x57\x35\x6b\x62\x33\x4a\x6d\x3d\x0d\x0a\x0d\x0a"); # GET /cgi-bin/control.cgi HTTP/1.0\x0d\x0aUser-Agent: VVTK (ver=40)\x0d\x0ax-sessioncookie: 7+/L9/cE11haP8/056B1dQ\x0d\x0aAccept: application/x-vvtk-tunnelled\x0d\x0aPragma: no-cache\x0d\x0aCache-Control: no-cache\x0d\x0aAuthorization: Basic cm9vdDpSZWluaWNrZW5kb3Jm=\x0d\x0a\x0d\x0a
 usleep(0.097);
  print "Receiving packet 55 253 ...\n";
  print "Wanted: \x48\x54\x54\x50\x2f\x31\x2e\x31\x20\x32\x30\x30\x20\x4f\x4b\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x3a\x20\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x78\x2d\x76\x76\x74\x6b\x2d\x74\x75\x6e\x6e\x65\x6c\x6c\x65\x64\x0d\x0a\x44\x61\x74\x65\x3a\x20\x54\x68\x75\x2c\x20\x31\x39\x20\x41\x75\x67\x20\x31\x39\x38\x32\x20\x31\x38\x3a\x33\x30\x3a\x30\x30\x20\x47\x4d\x54\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20\x6e\x6f\x2d\x73\x74\x6f\x72\x65\x0d\x0a\x50\x72\x61\x67\x6d\x61\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x61\x6e\x67\x75\x61\x67\x65\x3a\x20\x65\x6e\x0d\x0a\x53\x49\x44\x20\x3a\x20\x30\x0d\x0a\x41\x75\x64\x69\x6f\x20\x4d\x6f\x64\x65\x20\x3a\x20\x4e\x6f\x6e\x65\x0d\x0a\x50\x72\x69\x76\x69\x6c\x65\x67\x65\x20\x3a\x20\x31\x34\x33\x0d\x0a\x53\x65\x72\x76\x65\x72\x3a\x20\x4e\x65\x74\x77\x6f\x72\x6b\x20\x43\x61\x6d\x65\x72\x61\x0d\x0a\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x0d\x0a\n\n";
  $sock5->read($data,253);
  print "Received: $data\n\n";
 usleep(278.892);
  print "Opening connection 6 ...\n";
  my $sock6 = IO::Socket::INET->new(PeerAddr => $addr);
 usleep(1.122);
 usleep(130.99);
 usleep(96.335);
 usleep(0.022);
  print "Sending packet 60 ...\n";
  $sock6->print("\x50\x4f\x53\x54\x20\x2f\x63\x67\x69\x2d\x62\x69\x6e\x2f\x63\x6f\x6e\x74\x72\x6f\x6c\x2e\x63\x67\x69\x20\x48\x54\x54\x50\x2f\x31\x2e\x30\x0d\x0a\x55\x73\x65\x72\x2d\x41\x67\x65\x6e\x74\x3a\x20\x56\x56\x54\x4b\x20\x28\x76\x65\x72\x3d\x34\x30\x29\x0d\x0a\x78\x2d\x73\x65\x73\x73\x69\x6f\x6e\x63\x6f\x6f\x6b\x69\x65\x3a\x20\x37\x2b\x2f\x4c\x39\x2f\x63\x45\x31\x31\x68\x61\x50\x38\x2f\x30\x35\x36\x42\x31\x64\x51\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x3a\x20\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x2f\x78\x2d\x76\x76\x74\x6b\x2d\x74\x75\x6e\x6e\x65\x6c\x6c\x65\x64\x0d\x0a\x50\x72\x61\x67\x6d\x61\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x33\x32\x37\x36\x37\x0d\x0a\x45\x78\x70\x69\x72\x65\x73\x3a\x20\x53\x75\x6e\x2c\x20\x39\x20\x4a\x61\x6e\x20\x31\x39\x37\x32\x20\x30\x30\x3a\x30\x30\x3a\x30\x30\x20\x47\x4d\x54\x0d\x0a\x41\x75\x74\x68\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e\x3a\x20\x42\x61\x73\x69\x63\x20\x63\x6d\x39\x76\x64\x44\x70\x53\x5a\x57\x6c\x75\x61\x57\x4e\x72\x5a\x57\x35\x6b\x62\x33\x4a\x6d\x3d\x0d\x0a\x0d\x0a"); # POST /cgi-bin/control.cgi HTTP/1.0\x0d\x0aUser-Agent: VVTK (ver=40)\x0d\x0ax-sessioncookie: 7+/L9/cE11haP8/056B1dQ\x0d\x0aContent-Type: application/x-vvtk-tunnelled\x0d\x0aPragma: no-cache\x0d\x0aCache-Control: no-cache\x0d\x0aContent-Length: 32767\x0d\x0aExpires: Sun, 9 Jan 1972 00:00:00 GMT\x0d\x0aAuthorization: Basic cm9vdDpSZWluaWNrZW5kb3Jm=\x0d\x0a\x0d\x0a
 usleep(0.183);
  print "Receiving packet 61 17 ...\n";
  print "Wanted: \x00\x0f\x4f\x50\x45\x4e\x20\x56\x43\x4d\x2f\x31\x2e\x30\x20\x0d\x0a\n\n";
  $sock5->read($data,17);
  print "Received: $data\n\n";
 usleep(262095);
  print "Sending packet 62 ...\n";
  $sock6->print("\x00\x91\x55\x30\x56\x55\x56\x56\x41\x67\x56\x6b\x4e\x4e\x4c\x7a\x45\x75\x4d\x43\x41\x4e\x43\x6b\x4e\x54\x5a\x58\x45\x36\x49\x44\x49\x4e\x43\x6b\x4e\x6f\x52\x47\x6c\x79\x4f\x69\x42\x6b\x62\x33\x64\x75\x63\x33\x52\x79\x5a\x57\x46\x74\x44\x51\x70\x44\x61\x45\x35\x76\x4f\x69\x42\x32\x62\x32\x35\x73\x65\x51\x30\x4b\x51\x57\x4e\x6a\x5a\x58\x42\x30\x4f\x69\x42\x32\x61\x57\x52\x6c\x62\x79\x39\x71\x63\x47\x56\x6e\x4c\x48\x5a\x70\x5a\x47\x56\x76\x4c\x32\x31\x77\x4e\x48\x59\x73\x44\x51\x70\x55\x63\x6d\x46\x75\x63\x33\x42\x76\x63\x6e\x51\x36\x49\x45\x68\x55\x56\x46\x41\x4e\x43\x67\x30\x4b\x3d"); # \x00\x91U0VUVVAgVkNNLzEuMCANCkNTZXE6IDINCkNoRGlyOiBkb3duc3RyZWFtDQpDaE5vOiB2b25seQ0KQWNjZXB0OiB2aWRlby9qcGVnLHZpZGVvL21wNHYsDQpUcmFuc3BvcnQ6IEhUVFANCg0K=(SETUP VCM/1.0 CSeq: 2ChDir: downstreamChNo: vonlyAccept: video/jpeg,video/mp4v,Transport: HTTP)
 usleep(264347);
  print "Receiving packet 66 78 ...\n";
  print "Wanted: \x00\x4c\x53\x45\x54\x55\x50\x5f\x53\x59\x4e\x5f\x41\x43\x4b\x20\x56\x43\x4d\x2f\x31\x2e\x30\x20\x20\x32\x30\x30\x0d\x0a\x53\x65\x73\x73\x69\x6f\x6e\x3a\x20\x32\x35\x31\x33\x37\x0d\x0a\x43\x53\x65\x71\x3a\x20\x32\x0d\x0a\x53\x75\x62\x6d\x69\x74\x3a\x20\x76\x69\x64\x65\x6f\x2f\x6a\x70\x65\x67\x2c\x0d\x0a\x0d\x0a\n\n";
  $sock5->read($data,70);
  print "Received: $data\n\n";
 usleep(152.272);
  print "Opening connection 7 ...\n";
  my $sock7 = IO::Socket::INET->new(PeerAddr => $addr);
 usleep(49.883);
 usleep(137.012);
 usleep(169.799);
 usleep(0.021);
  print "Sending packet 71 ...\n";
  $sock7->print("\x47\x45\x54\x20\x2f\x63\x67\x69\x2d\x62\x69\x6e\x2f\x64\x6f\x77\x6e\x73\x74\x72\x65\x61\x6d\x2e\x63\x67\x69\x3f\x53\x49\x44\x3d\x30\x20\x48\x54\x54\x50\x2f\x31\x2e\x30\x0d\x0a\x55\x73\x65\x72\x2d\x41\x67\x65\x6e\x74\x3a\x20\x56\x56\x54\x4b\x20\x28\x76\x65\x72\x3d\x34\x30\x29\x0d\x0a\x41\x63\x63\x65\x70\x74\x3a\x20\x2a\x2f\x2a\x0d\x0a\x48\x6f\x73\x74\x3a\x20\x38\x30\x2e\x31\x35\x32\x2e\x31\x39\x35\x2e\x32\x30\x39\x0d\x0a\x41\x75\x74\x68\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e\x3a\x20\x42\x61\x73\x69\x63\x20\x63\x6d\x39\x76\x64\x44\x70\x53\x5a\x57\x6c\x75\x61\x57\x4e\x72\x5a\x57\x35\x6b\x62\x33\x4a\x6d\x3d\x0d\x0a\x0d\x0a"); # GET /cgi-bin/downstream.cgi?SID=0 HTTP/1.0\x0d\x0aUser-Agent: VVTK (ver=40)\x0d\x0aAccept: */*\x0d\x0aHost: 80.152.195.209\x0d\x0aAuthorization: Basic cm9vdDpSZWluaWNrZW5kb3Jm=\x0d\x0a\x0d\x0a
 usleep(271000);
  print "Receiving packet 72 76 ...\n";
  print "Wanted: \x00\x4a\x4d\x45\x44\x49\x41\x5f\x43\x48\x5f\x41\x43\x4b\x20\x20\x56\x43\x4d\x2f\x31\x2e\x30\x20\x20\x32\x30\x30\x0d\x0a\x53\x65\x73\x73\x69\x6f\x6e\x3a\x20\x32\x35\x31\x33\x37\x0d\x0a\x43\x53\x65\x71\x3a\x20\x33\x0d\x0a\x43\x68\x44\x69\x72\x3a\x20\x64\x6f\x77\x6e\x73\x74\x72\x65\x61\x6d\x0d\x0a\x0d\x0a\n\n";
  $sock5->read($data,70);
  print "Received WICHTIG: $data\n\n";
  my $session="";
  if($data=~m/Session: (\d+)/)
  {
    print "SESSION DETECTED: $1\n";
    $session=$1;
  }
 usleep(384.545);
  print "Receiving packet 73 237 ...\n";
  print "Wanted: \x48\x54\x54\x50\x2f\x31\x2e\x31\x20\x32\x30\x30\x20\x4f\x4b\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x3a\x20\x42\x82\xd2\x83\xc1\x80\x40\x35\xd0\x91\x12\x10\x02\x0d\x0a\x44\x61\x74\x65\x3a\x20\x54\x68\x75\x2c\x20\x31\x39\x20\x41\x75\x67\x20\x31\x39\x38\x32\x20\x31\x38\x3a\x33\x30\x3a\x30\x30\x20\x47\x4d\x54\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20\x6e\x6f\x2d\x73\x74\x6f\x72\x65\x0d\x0a\x50\x72\x61\x67\x6d\x61\x3a\x20\x6e\x6f\x2d\x63\x61\x63\x68\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x61\x6e\x67\x75\x61\x67\x65\x3a\x20\x65\x6e\x0d\x0a\x53\x49\x44\x20\x3a\x20\x39\x39\x0d\x0a\x41\x75\x64\x69\x6f\x20\x4d\x6f\x64\x65\x20\x3a\x20\x4e\x6f\x6e\x65\x0d\x0a\x50\x72\x69\x76\x69\x6c\x65\x67\x65\x20\x3a\x20\x30\x0d\x0a\x53\x65\x72\x76\x65\x72\x3a\x20\x4e\x65\x74\x77\x6f\x72\x6b\x20\x43\x61\x6d\x65\x72\x61\x0d\x0a\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x0d\x0a\n\n";
  $sock7->read($data,237);
  print "Received: $data\n\n";
 usleep(6.762);
  print "Sending packet 74 ...\n";

  my $senddata="\x00\x5d\x55\x31\x52\x42\x55\x6c\x52\x66\x55\x31\x52\x53\x52\x55\x46\x4e\x49\x46\x5a\x44\x54\x53\x38\x78\x4c\x6a\x41\x67\x44\x51\x70\x54\x5a\x58\x4e\x7a\x61\x57\x39\x75\x4f\x69\x41\x79\x4e\x54\x45\x7a\x4e\x77\x30\x4b\x51\x31\x4e\x6c\x63\x54\x6f\x67\x4e\x41\x30\x4b\x51\x32\x68\x45\x61\x58\x49\x36\x49\x47\x52\x76\x64\x32\x35\x7a\x64\x48\x4a\x6c\x59\x57\x30\x4e\x43\x67\x30\x4b\x3d"; # \x00]U1RBUlRfU1RSRUFNIFZDTS8xLjAgDQpTZXNzaW9uOiAyNTEzNw0KQ1NlcTogNA0KQ2hEaXI6IGRvd25zdHJlYW0NCg0K=

  my $decoded=decode_base64(substr($senddata,2));
  print "DECODED1: $decoded\n";
  $decoded=~s/Session: \d+/Session: $session/s;
  $decoded=~s/CSeq: \d+/CSeq: 3/s;

  print "DECODED2: $decoded\n";

  print "SENDDATA: $senddata-\n";
  $senddata=encode_base64($decoded); $senddata=~s/\n//g; $senddata.="=";
  print "SENDDATA: $senddata-\n";

  $sock6->print(pack("n",length($senddata)).$senddata);
 usleep(0.056);
 usleep(142.706);
 usleep(0.042);
 usleep(226.49);
 usleep(386.998);
  print "Ignoring packet 79 ...\n";
 usleep(283.406);
  print "Opening connection 8 ...\n";
 usleep(58.678);
  print "Sending packet 81 ...\n";
  $sock6->print("\x00\x25\x52\x30\x56\x55\x58\x30\x78\x50\x51\x30\x46\x55\x53\x55\x39\x4f\x49\x46\x5a\x44\x54\x53\x38\x78\x4c\x6a\x41\x67\x44\x51\x6f\x4e\x43\x67\x3d\x3d\x3d"); # \x00%R0VUX0xPQ0FUSU9OIFZDTS8xLjAgDQoNCg===
 usleep(15.339);
  print "Receiving packet 82 1452 ...\n";

  my $header="";
  my $count=0;
  open TRAFFIC,">traffic.bin";
  close TRAFFIC;
  open HEADERS,">headers.bin";
  foreach (1 .. 200)
  {
    $count++;
    $sock7->read($header,29);

    open TRAFFIC,">>traffic.bin";
    print TRAFFIC $header;
    close TRAFFIC;

    if(substr($header,9,4) ne "jpeg")
    {
      print "Error: Not a jpeg\n";
    }
    my $len=unpack("N",substr($header,5,4)); 
    print "Laenge: $len\n";
    $data="";
    $sock7->read($data,$len+5);

    open TRAFFIC,">>traffic.bin";
    print TRAFFIC $data;
    close TRAFFIC;

    open OUT,">header$count.jpg";
    binmode OUT;
    print OUT $header;
    print HEADERS $header. "\x00\x00\x00";
    open OUT,">image$count.jpg";
    binmode OUT;
    print OUT $data;
    close OUT;
  }
  close HEADERS;


