Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #define USERPASS "pentest:midterm"
  8.  
  9. // VS2005: cl /GS- /MD htpasswd.c
  10. // gcc: rename _snprintf -> snprintf
  11.  
  12. int verbose = 0;
  13.  
  14. static char authbuf[512];
  15. static int authenticated = 0;
  16.  
  17. int authenticate(char *username, char *password){  
  18.     _snprintf(authbuf, sizeof(authbuf)+1, "%s:%s", username, password);
  19.  
  20.     if (!authenticated) {
  21.         // If the user is not already authenticated, try and authenticate them now
  22.         authenticated = !strncmp(authbuf, USERPASS, strlen(USERPASS));
  23.     }
  24.    
  25.     return authenticated;
  26. }
  27.  
  28. void debugprint(char* str)
  29. {
  30.     char debugbuf[522];
  31.    
  32.     _snprintf(debugbuf, strlen(str)+10, "DEBUG: %s\n", str);
  33.     fprintf(stderr, debugbuf);
  34. }
  35.  
  36. void debug(char *username, char *password)
  37. {
  38.     char pass[512];
  39.     char user[512];
  40.    
  41.     strncpy(user, username, sizeof(user));
  42.     strncpy(pass, password, sizeof(pass));
  43.    
  44.     debugprint(user);
  45.     debugprint(pass);
  46. }
  47.  
  48. void update_file(char *new, char *outfile)
  49. {
  50.     char s_buffer[512];
  51.     unsigned char f;
  52.    
  53.     f = strlen(new);
  54.     if(f > sizeof(s_buffer)-10){
  55.             fprintf(stderr, "new username too long!\n");
  56.             exit(1);
  57.     }
  58.     strcpy(s_buffer, new);
  59.  
  60.     fprintf(stdout, "Username: %s added to %s!\n", s_buffer, outfile);
  61. }
  62.  
  63. void usage(void)
  64. {
  65.     fprintf(stderr,"Usage: adduser [options]\n");
  66.     fprintf(stderr,"Options\n");
  67.     fprintf(stderr,"  -v          verbose\n");
  68.     fprintf(stderr,"  -u<username>    Your username\n");
  69.     fprintf(stderr,"  -p<password>    Your password\n");
  70.     fprintf(stderr,"  -f<filename>    htpasswd file to update\n");
  71.     fprintf(stderr,"  -n<newusername>    New username to add to htpasswd file\n");
  72.     exit (8);
  73. }
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77.     char *username = NULL, *password = NULL, *outfile = NULL, *newuser = NULL;
  78.  
  79.     __asm int 3
  80.    
  81.     /* loop for each option.  
  82.      *  Stop if we run out of arguments
  83.      *  or we get an argument without a dash.
  84.      */
  85.     while ((argc > 1) && (argv[1][0] == '-')) {
  86.         /* argv[1][1] is the actual option character. */
  87.         switch (argv[1][1]) {
  88.         case 'v':
  89.             verbose = 1;
  90.             printf("DEBUG: verbose set\n");
  91.             break;
  92.             /* -f<name>  output file
  93.              *    [0] is the dash
  94.              *    [1] is the "f"
  95.              *    [2] starts the name */
  96.         case 'f':
  97.             outfile = &argv[1][2];
  98.             break;
  99.         case 'u':
  100.             username = &argv[1][2];
  101.             break;
  102.         case 'p':
  103.             password = &argv[1][2];
  104.             break;
  105.         case 'n':
  106.             newuser = &argv[1][2];
  107.             break;
  108.         default:
  109.             fprintf(stderr,"Bad option %s\n", argv[1]);
  110.             usage();
  111.         }
  112.        
  113.         /* move the argument list up one
  114.         * move the count down one
  115.         */
  116.         ++argv;
  117.         --argc;
  118.     }
  119.  
  120.     if (!username || !password || !outfile || !newuser) {
  121.         usage();
  122.     }
  123.    
  124.     if(verbose)
  125.         debug(username, password);
  126.        
  127.     if(authenticate(username, password)) {
  128.         fprintf(stdout, "You correctly authenticated!\nAdding new user to %s...\n", outfile);
  129.         update_file(newuser, outfile);
  130.     }
  131.    
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement