Advertisement
Guest User

lab4c

a guest
May 30th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. /*
  2.  *   Format String Lab - C Problem
  3.  *   gcc -z execstack -z norelro -fno-stack-protector -o lab4C lab4C.c
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. #define PASS_LEN 30
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     char username[100] = {0};
  15.     char real_pass[PASS_LEN] = {0};
  16.     char in_pass[100] = {0};
  17.     FILE *pass_file = NULL;
  18.     int rsize = 0;
  19.  
  20.     /* open the password file */
  21.     pass_file = fopen("/home/lab4B/.pass", "r");
  22.     if (pass_file == NULL) {
  23.         fprintf(stderr, "ERROR: failed to open password file\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     /* read the contents of the password file */
  28.     rsize = fread(real_pass, 1, PASS_LEN, pass_file);
  29.     real_pass[strcspn(real_pass, "\n")] = '\0';  // strip \n
  30.     if (rsize != PASS_LEN) {
  31.         fprintf(stderr, "ERROR: failed to read password file\n");
  32.         exit(EXIT_FAILURE);
  33.     }
  34.  
  35.     /* close the password file */
  36.     fclose(pass_file);
  37.  
  38.     puts("===== [ Secure Access System v1.0 ] =====");
  39.     puts("-----------------------------------------");
  40.     puts("- You must login to access this system. -");
  41.     puts("-----------------------------------------");
  42.  
  43.     /* read username securely */
  44.     printf("--[ Username: ");
  45.     fgets(username, 100, stdin);
  46.     username[strcspn(username, "\n")] = '\0';    // strip \n
  47.  
  48.     /* read input password securely */
  49.     printf("--[ Password: ");
  50.     fgets(in_pass, sizeof(in_pass), stdin);
  51.     in_pass[strcspn(in_pass, "\n")] = '\0';      // strip \n
  52.  
  53.     puts("-----------------------------------------");
  54.  
  55.     /* log the user in if the password is correct */
  56.     if(!strncmp(real_pass, in_pass, PASS_LEN)){
  57.         printf("Greetings, %s!\n", username);
  58.         system("/bin/sh");
  59.     } else {
  60.         printf(username);
  61.         printf(" does not have access!\n");
  62.         exit(EXIT_FAILURE);
  63.     }
  64.  
  65.     return EXIT_SUCCESS;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement