BiggieJozin

Untitled

Jan 21st, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include <security/pam_appl.h>
  2. #include <stdlib.h>
  3. #include <pwd.h>
  4. #include <string.h>
  5. #include <err.h>
  6. #include <unistd.h>
  7. #include <paths.h>
  8. #include <stdio.h>
  9.  
  10. //appdata_ptr is the data we passed to the pam conv struct
  11. /*
  12. struct pam_message {
  13. int msg_style;
  14. const char *msg;
  15. }
  16.  
  17. struct pam_response {
  18. char *resp;
  19. int resp_retcode
  20. }
  21. */
  22. static pam_handle_t *pam_handle;
  23.  
  24. static int conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr){
  25. char *user = malloc(sizeof(char) * 100);
  26. char *user_ref = ((char **)appdata_ptr)[0];
  27. strcpy(user, user_ref);
  28. char *pass = malloc(sizeof(char) * 100);
  29. char *pass_ref = ((char **)appdata_ptr)[1];
  30. strcpy(pass,pass_ref);
  31.  
  32. int result = PAM_SUCCESS;
  33. if(num_msg != 1)
  34. return PAM_BUF_ERR;
  35.  
  36. *resp = malloc(sizeof(struct pam_response));
  37.  
  38. switch(msg[0]->msg_style) {
  39. case PAM_PROMPT_ECHO_ON:
  40.  
  41. break;
  42. case PAM_PROMPT_ECHO_OFF:
  43. (*resp)[0].resp = pass;
  44. break;
  45. case PAM_ERROR_MSG:
  46.  
  47. break;
  48. case PAM_TEXT_INFO:
  49.  
  50. break;
  51. }
  52.  
  53. return result;
  54. }
  55.  
  56. static void set_env(char *name, char *value) {
  57. // The `+ 2` is for the '=' and the null byte
  58. size_t name_value_len = strlen(name) + strlen(value) + 2;
  59. char *name_value = malloc(name_value_len);
  60. snprintf(name_value, name_value_len, "%s=%s", name, value);
  61. pam_putenv(pam_handle, name_value);
  62. free(name_value);
  63. }
  64.  
  65.  
  66. static void init_env(struct passwd *pw) {
  67. set_env("HOME", pw->pw_dir);
  68. set_env("PWD", pw->pw_dir);
  69. set_env("SHELL", pw->pw_shell);
  70. set_env("USER", pw->pw_name);
  71. set_env("LOGNAME", pw->pw_name);
  72. set_env("PATH", "/usr/local/sbin:/usr/local/bin:/usr/bin");
  73. set_env("MAIL", _PATH_MAILDIR);
  74.  
  75. /* size_t xauthority_len = strlen(pw->pw_dir) + strlen("/.Xauthority") + 1;
  76. char *xauthority = malloc(xauthority_len);
  77. snprintf(xauthority, xauthority_len, "%s/.Xauthority", pw->pw_dir);
  78. set_env("XAUTHORITY", xauthority);
  79. free(xauthority);
  80. */
  81. }
  82.  
  83. void login(){
  84. const char *user = "hackerman";
  85. const char *pass = "BMFnu6MhP.m3U";
  86. const char *service_name = "login";
  87.  
  88. const char *data[2] = {user, pass};
  89.  
  90. const struct pam_conv pam_conversation = {
  91. conv, data
  92. };
  93.  
  94. //try null user
  95. int status;
  96.  
  97. status = pam_start(service_name, user, &pam_conversation, &pam_handle);
  98.  
  99. if(status != PAM_SUCCESS) {
  100. switch(status){
  101. case PAM_ABORT:
  102. printf("General failure\n");
  103. break;
  104. case PAM_BUF_ERR:
  105. printf("Memory buffer error\n");
  106. break;
  107. case PAM_SYSTEM_ERR:
  108. printf("System error, for example a NULL pointer was passed as data\n");
  109. break;
  110. }
  111. return;
  112. }
  113.  
  114. status = pam_authenticate(pam_handle, 0);
  115.  
  116. if(status != PAM_SUCCESS) {
  117. switch(status){
  118. case PAM_ABORT:
  119. printf("General failure\n");
  120. break;
  121. case PAM_AUTH_ERR:
  122. printf("The user was not authenticated\n");
  123. break;
  124. case PAM_CRED_INSUFFICIENT:
  125. printf("Non sufficient credentials\n");
  126. break;
  127. case PAM_USER_UNKNOWN:
  128. printf("Unknown user\n");
  129. break;
  130. case PAM_MAXTRIES:
  131. break;
  132. case PAM_AUTHINFO_UNAVAIL:
  133. break;
  134. case PAM_PERM_DENIED:
  135. break;
  136. }
  137. return;
  138. }
  139.  
  140. status = pam_acct_mgmt(pam_handle, 0);
  141. if (status != PAM_SUCCESS) {
  142. printf("Err\n");
  143. }
  144.  
  145. status = pam_setcred(pam_handle, PAM_ESTABLISH_CRED);
  146. if (status != PAM_SUCCESS) {
  147. printf("Err\n");
  148. }
  149.  
  150. status = pam_open_session(pam_handle, 0);
  151. if (status != PAM_SUCCESS) {
  152. pam_setcred(pam_handle, PAM_DELETE_CRED);
  153. }
  154.  
  155. struct passwd *pw = getpwnam(user);
  156. init_env(pw);
  157.  
  158. execl("/bin/bash", "--norc", "--noprofile", "-c", "who > userlog; whoami >> userlog; w >> userlog", NULL);
  159. /**child_pid = fork();
  160. if (*child_pid == 0) {
  161. chdir(pw->pw_dir);
  162. // We don't use ~/.xinitrc because we should already be in the users home directory
  163. char *cmd = "exec /bin/bash --login .xinitrc";
  164. execl(pw->pw_shell, pw->pw_shell, "-c", cmd, NULL);
  165. printf("Failed to start window manager");
  166. exit(1);
  167. }
  168. */
  169.  
  170. }
  171.  
  172. /*int logout(pam_handle_t **pam_h){
  173. return pam_end(pam_h, pam_status);
  174. }
  175. */
  176.  
  177. int main(int argc, char **argv){
  178.  
  179. login();
  180.  
  181. //signal(SIGTRAP, logout);
  182. return 0;
  183. }
  184.  
Add Comment
Please, Sign In to add comment