Advertisement
infodox

pjl-shell.pl

May 3rd, 2012
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 9.29 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # pjl-shell.pl
  3. # www.attackvector.org
  4. # Author of original parts unknown.
  5.  
  6. use IO::Socket;
  7. use Getopt::Long;
  8.  
  9. my $useuel=1,$listdisk=0,$target;
  10. $socket, $directory;
  11.  
  12. my $result=GetOptions
  13.    (
  14.       'useuel!'   => \$useuel,
  15.       'listdisk!' => \$listdisk,
  16.       'host=s'    => \$target,
  17.    );
  18.    
  19. if ((!defined $target) && (defined $ARGV[0])) { $target=$ARGV[0] };
  20.  
  21. if(!$target) {
  22.    print "Usage: $0 <ip>\n";
  23.    exit(0);
  24. }
  25.  
  26. if (defined $target) {
  27.    $socket=opensocket($target);
  28. }
  29.  
  30. my $finish=0;
  31.  
  32. my %cmdhash=
  33.    (
  34.       'open'   => \&cmd_open,
  35.       'quit'   => \&cmd_quit,
  36.       'bye'    => \&cmd_quit,
  37.       'drives' => \&cmd_drives,
  38.       'dir'    => \&cmd_dir,
  39.       'ls'     => \&cmd_dir,
  40.       'cd'     => \&cmd_cd,
  41.       'get'    => \&cmd_get,
  42.       'put'    => \&cmd_put,
  43.       'mkdir'  => \&cmd_mkdir,
  44.       'del'    => \&cmd_del,
  45.       'rm'     => \&cmd_del,
  46.       'readymsg'   => \&cmd_rmsg,
  47.       'config' => \&cmd_config,
  48.       'status' => \&cmd_status,
  49.       'raw'    => \&cmd_raw,
  50.       'help'   => \&cmd_help,
  51.    );
  52.  
  53. while (!$finished) {
  54.    print "Current dir: $directory\n";
  55.    print "pjlftp> ";
  56.    my $command=<STDIN>;
  57.    chomp($command);
  58.    my @cmdlist=split(/ /,$command);
  59.    $cmdlist[0]=lc($cmdlist[0]);
  60.  
  61.    while(my($cmd,$func) = (each %cmdhash)) {
  62.       if ($cmd eq $cmdlist[0]) {
  63.          shift(@cmdlist);
  64.          $finished=&$func(@cmdlist);
  65.       }
  66.          
  67.    }
  68. }
  69.  
  70. exit(0);
  71. foreach my $check (@checks) {
  72.    @response=sendprinter($socket, $check->{PJL});
  73.    if ($check->{name} eq "Filesystem") {
  74.       shift(@response);
  75.    }
  76.    foreach my $line (@response) {
  77.       chomp($line);
  78.       $line =~ s/\"//g;
  79.       $line =~ s/^\s+//;
  80.       if ($check->{name} eq "Filesystem") {
  81.          my @files=split(/\s+/,$line);
  82.          print "$check->{name} $files[0]\n";
  83.          print " Total Size: $files[1]\n";
  84.          print " Free Space: $files[2]\n";
  85.          print " Location: $files[3]\n";
  86.          print " Label: $files[4]\n";
  87.          if ($listdisk) {
  88.             print "  FILE\t\t\tTYPE\t\tSIZE\n";
  89.             my @objects=displaydir($socket,1,$files[0] . '\\');
  90.          }
  91.       } else {
  92.          print "$check->{name}: ${line}\n";
  93.       }
  94.    }
  95. }
  96.  
  97. close($socket);
  98. exit(0);
  99.  
  100. sub sendprinter {
  101.    my $socket=$_[0];
  102.    my $command='@PJL ' . $_[1];
  103.    my @strings;
  104.    my $result;
  105.    my $escape=pack("ca8",0x1b,'%-12345X');
  106.    my $term = "\r\n";
  107.    my $send = $command . $term;
  108.    
  109.    if ($useuel) {
  110.       $send=$escape . $command . $term . $escape;
  111.    }
  112.    
  113.    $socket->send("@PJL\r\n");
  114.    $socket->send($send);
  115.    
  116.    my $bytes;
  117.    my $firstnl=0;
  118.    if ($command =~ /PJL FSUPLOAD/) {
  119.       $bytes=$command;
  120.       $bytes =~ s/^.*SIZE=//;
  121.    }
  122.    while (sysread($socket, $byte, 1) == 1) {
  123.       if ($command =~ /PJL FSUPLOAD/) {
  124.          if ($firstnl == 0 && unpack("c",$byte) == 0x0a) {
  125.             $firstnl=1;
  126.          }
  127.          $bytes-- if ($firstnl==1);
  128.          last if ($bytes==0);
  129.       } else {
  130.          last if (unpack("c",$byte) == 0x0c);
  131.       }
  132.       $result .= $byte;
  133.    }
  134.    @strings=split(/\n/, $result);
  135.  
  136.    return @strings;
  137. }
  138.  
  139. sub displaydir {
  140.    my $socket=$_[0];
  141.    my $path=$_[1];
  142.    my @objects=cataloguedir($socket,$path);
  143.  
  144.    if ($#objects < 0) {
  145.       print "<EMPTY>\n";
  146.    }
  147.    foreach $object (@objects) {
  148.       my $sep=(substr($path,length($path)-1) ne '\\')?'\\':'';
  149.       print "$object->{type}\t$object->{size}\t$path$sep$object->{name}\n";
  150.    }
  151. }
  152.  
  153. sub cataloguedir {
  154.    my $socket=$_[0];
  155.    my $volume=$_[1];
  156.    
  157.    my @response;
  158.    my $base="FSDIRLIST NAME=";
  159.    my @objects;
  160.    my $command=$base . '"' . $volume . '"' . " ENTRY=1 COUNT=128";
  161.  
  162.    @response=sendprinter($socket, $command);
  163.    shift(@response);
  164.  
  165.    shift(@response);
  166.    foreach my $line (@response) {
  167.       chomp($line);
  168.       my @params=split(/\s+/,$line);
  169.       my $type=$params[1];
  170.       my $size=$params[2];
  171.       $type =~ s/^TYPE=//;
  172.       $size =~ s/^SIZE=//;
  173.       my $dirent = {
  174.          name => $params[0],
  175.          type => $type,
  176.          size => $size
  177.       };
  178.       push(@objects,$dirent);
  179.    }
  180.    
  181.    return @objects;
  182. }
  183.  
  184. sub opensocket {
  185.    my $target=$_[0];
  186.    my $socket=new IO::Socket::INET (
  187.       PeerAddr => $target,
  188.       PeerPort => '9100',
  189.       Proto => tcp,
  190.    );
  191.  
  192.    die "Error: $@\n" unless $socket;
  193.    return $socket;
  194. }
  195.  
  196. sub cmd_open {
  197.    $target=$_[0];
  198.    if (defined($target)) {
  199.       if (defined($socket)) { close($socket) };
  200.       $socket=opensocket($target);
  201.    } else {
  202.       print("No host provided\n");
  203.    }
  204.    return 0;
  205. }
  206.  
  207. sub cmd_close {
  208.    if (defined($socket)) { close($socket) };
  209.    return 0;
  210. }
  211.  
  212. sub cmd_quit {
  213.    return 1;
  214. }
  215.  
  216. sub cmd_drives {
  217.    my @response=sendprinter($socket,"INFO FILESYS");
  218.    shift(@response);
  219.    
  220.    if ($response[0] =~ '^VOLUME') {
  221.       shift(@response);
  222.    }
  223.    foreach my $line (@response) {
  224.       chomp($line);
  225.       $line =~ s/\"//g;
  226.       $line =~ s/^\s+//;
  227.       # my @details=split(/\s+/,$line);
  228.       # print("$details[0]\n");
  229.       print "$line\n";
  230.    }  
  231. }
  232.  
  233. sub cmd_dir {
  234.    if (!defined($directory)) {
  235.       print("No current directory\n");
  236.       return 0;
  237.    }
  238.    
  239.    displaydir($socket,$directory);
  240.    return 0;
  241. }
  242.  
  243. sub cmd_cd {
  244.    my $dir=$_[0];
  245.    
  246.    $dir=$dir . "\\";
  247.    
  248.    if (defined $directory) { $dir=$directory . $dir; };
  249.    if (defined $directory && $dir =~ /\.\.\\$/) {
  250.       my $position=rindex($directory,"\\",length($directory)-2);
  251.       if ($position > 0) {
  252.          $directory=substr($directory, 0, $position+1);
  253.       }
  254.       return 0
  255.    }
  256.    
  257.    my @response=sendprinter($socket, 'FSQUERY NAME="' . $dir . '"');
  258.    shift(@response);
  259.    if (scalar(@response) == 0) {
  260.       $directory=$dir;
  261.    } else {
  262.       print("Directory $dir is invalid\n");
  263.    }
  264.    return 0;
  265. }
  266.  
  267. sub cmd_get {
  268.    my $file=$_[0];
  269.    
  270.    open(OUTFILE,">","$file") or die $!;
  271.    binmode(OUTFILE);
  272.    
  273.    my $size;
  274.    my $file = $directory . $file;
  275.    my @response=sendprinter($socket, 'FSQUERY NAME="' . $file . '"');
  276.    if (scalar(@response) != 1) {
  277.       print("File $file doesn't exist\n");
  278.       return 0;
  279.    } else {
  280.       $size=$response[0];
  281.       $size =~ s/^.*SIZE=//;
  282.    }
  283.    
  284.    my @response=sendprinter($socket, 'FSUPLOAD NAME="' . $file . '" SIZE=' . $size);
  285.    shift(@response);
  286.    foreach my $item (@response) {
  287.       print OUTFILE "$item\n";
  288.    }
  289.    close(OUTFILE);
  290.    return 0;
  291. }
  292.  
  293. sub cmd_get {
  294.    my $file=$_[0];
  295.    
  296.  
  297.    open(OUTFILE,">","$file") or die $!;
  298.    binmode(OUTFILE);
  299.    
  300.    my $file = $directory . $file;
  301.    my @response=sendprinter($socket, 'FSQUERY NAME="' . $file . '"');
  302.    if (scalar(@response) != 1) {
  303.       print("File $file doesn't exist\n");
  304.       return 0;
  305.    } else {
  306.       $size=$response[0];
  307.       $size =~ s/^.*SIZE=//;
  308.    }
  309.    
  310.    my @response=sendprinter($socket, 'FSUPLOAD NAME="' . $file . '" SIZE=' . $size);
  311.    shift(@response);
  312.    foreach my $item (@response) {
  313.       print OUTFILE "$item\n";
  314.    }
  315.    close(OUTFILE);
  316.    return 0;
  317. }
  318.    
  319. sub cmd_put {
  320.    my $file=$_[0];
  321.    
  322.    open(INFILE,"<","$file") or die $!;
  323.    binmode(INFILE);
  324.    
  325.    my $size = -s "$file";
  326.    my $ofile = $directory . $file;
  327.    my $buffer;
  328.    
  329.    my $count=read(INFILE,$buffer,$size);
  330.    my @response=sendprinter($socket, 'FSDOWNLOAD FORMAT:BINARY SIZE=' . $size . ' NAME="' . $ofile . '"' . "\r\n" . $buffer);
  331.    close(OUTFILE);
  332.    return 0;
  333. }  
  334.  
  335. sub cmd_mkdir {
  336.    my $dir=$_[0];
  337.    my $dir=$directory . $dir;
  338.    my @response=sendprinter($socket, 'FSMKDIR NAME="' . $dir . '"');
  339.    return 0;
  340. }
  341.  
  342. sub cmd_del {
  343.    my $dir=$_[0];
  344.    my $dir=$directory . $dir;
  345.    my @response=sendprinter($socket, 'FSDELETE NAME="' . $dir . '"');
  346.    return 0;
  347. }
  348.  
  349. sub cmd_rmsg {
  350.    my $rmsg=$_[0];
  351.    print "Setting message to: $rmsg - This takes a minute, be patient\n";
  352.    my @response = sendprinter($socket, 'RDYMSG DISPLAY="' . $rmsg . '"');
  353.    return 0;
  354. }
  355.  
  356. sub cmd_status {
  357.    my @response = sendprinter($socket, "INFO STATUS");
  358.    shift(@response);
  359.    foreach $line (@response) {
  360.       print "$line\n";
  361.    }
  362.    return 0;
  363. }
  364.  
  365. sub cmd_config {
  366.    my @response = sendprinter($socket, "INFO CONFIG");
  367.    shift(@response);
  368.    foreach $line (@response) {
  369.       print "$line\n";
  370.    }
  371.    return 0;
  372. }
  373.  
  374. sub cmd_raw {
  375.    my @response = sendprinter($socket, "@_");
  376.    shift(@response);
  377.    foreach $line (@response) {
  378.       print "$line\n";
  379.    }
  380.    return 0;
  381. }
  382.  
  383. sub cmd_help {
  384.    print "Available commands:\n";
  385.    print "\topen\t - Open a new connection\n";
  386.    print "\tquit\t - Terminate session\n";
  387.    print "\tbye\t - Terminate session\n";
  388.    print "\tdrives\t - List available volumes\n";
  389.    print "\tdir\t - List files and directories\n";
  390.    print "\tls\t - List files and directories\n";
  391.    print "\tcd\t - Change directory\n";
  392.    print "\tget\t - Download a file\n";
  393.    print "\tput\t - Upload a file\n";
  394.    print "\tmkdir\t - Make a new directory\n";
  395.    print "\tdel\t - Delete a file\n";
  396.    print "\trm\t - Delete a file\n";
  397.    print "\treadymsg\t - Change the ready message\n";
  398.    print "\tconfig\t - Show the hardware config\n";
  399.    print "\tstatus\t - Show the current printer status\n";
  400.    print "\traw\t - Manually type commands - ie: raw INFO STATUS\n";
  401.    print "\thelp\t - Durr..\n";
  402.    return 0;
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement